1 // SPDX-License-Identifier: GPL-2.0-only
3 * db-export.c: Support for exporting data suitable for import to a database
4 * Copyright (c) 2014, Intel Corporation.
17 #include "thread-stack.h"
18 #include "callchain.h"
19 #include "call-path.h"
20 #include "db-export.h"
21 #include <linux/zalloc.h>
23 int db_export__init(struct db_export *dbe)
25 memset(dbe, 0, sizeof(struct db_export));
29 void db_export__exit(struct db_export *dbe)
31 call_return_processor__free(dbe->crp);
35 int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
40 evsel->db_id = ++dbe->evsel_last_db_id;
42 if (dbe->export_evsel)
43 return dbe->export_evsel(dbe, evsel);
48 int db_export__machine(struct db_export *dbe, struct machine *machine)
53 machine->db_id = ++dbe->machine_last_db_id;
55 if (dbe->export_machine)
56 return dbe->export_machine(dbe, machine);
61 int db_export__thread(struct db_export *dbe, struct thread *thread,
62 struct machine *machine, struct thread *main_thread)
64 u64 main_thread_db_id = 0;
69 thread->db_id = ++dbe->thread_last_db_id;
72 main_thread_db_id = main_thread->db_id;
74 if (dbe->export_thread)
75 return dbe->export_thread(dbe, thread, main_thread_db_id,
81 static int __db_export__comm(struct db_export *dbe, struct comm *comm,
82 struct thread *thread)
84 comm->db_id = ++dbe->comm_last_db_id;
87 return dbe->export_comm(dbe, comm, thread);
92 int db_export__comm(struct db_export *dbe, struct comm *comm,
93 struct thread *thread)
98 return __db_export__comm(dbe, comm, thread);
102 * Export the "exec" comm. The "exec" comm is the program / application command
103 * name at the time it first executes. It is used to group threads for the same
104 * program. Note that the main thread pid (or thread group id tgid) cannot be
105 * used because it does not change when a new program is exec'ed.
107 int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
108 struct thread *main_thread)
115 err = __db_export__comm(dbe, comm, main_thread);
120 * Record the main thread for this comm. Note that the main thread can
121 * have many "exec" comms because there will be a new one every time it
122 * exec's. An "exec" comm however will only ever have 1 main thread.
123 * That is different to any other threads for that same program because
124 * exec() will effectively kill them, so the relationship between the
125 * "exec" comm and non-main threads is 1-to-1. That is why
126 * db_export__comm_thread() is called here for the main thread, but it
127 * is called for non-main threads when they are exported.
129 return db_export__comm_thread(dbe, comm, main_thread);
132 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
133 struct thread *thread)
137 db_id = ++dbe->comm_thread_last_db_id;
139 if (dbe->export_comm_thread)
140 return dbe->export_comm_thread(dbe, db_id, comm, thread);
145 int db_export__dso(struct db_export *dbe, struct dso *dso,
146 struct machine *machine)
151 dso->db_id = ++dbe->dso_last_db_id;
154 return dbe->export_dso(dbe, dso, machine);
159 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
162 u64 *sym_db_id = symbol__priv(sym);
167 *sym_db_id = ++dbe->symbol_last_db_id;
169 if (dbe->export_symbol)
170 return dbe->export_symbol(dbe, sym, dso);
175 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
176 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
181 struct dso *dso = al->map->dso;
183 err = db_export__dso(dbe, dso, al->machine);
186 *dso_db_id = dso->db_id;
189 al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
191 dso__insert_symbol(dso, al->sym);
195 u64 *db_id = symbol__priv(al->sym);
197 err = db_export__symbol(dbe, al->sym, dso);
201 *offset = al->addr - al->sym->start;
208 static struct call_path *call_path_from_sample(struct db_export *dbe,
209 struct machine *machine,
210 struct thread *thread,
211 struct perf_sample *sample,
212 struct perf_evsel *evsel)
214 u64 kernel_start = machine__kernel_start(machine);
215 struct call_path *current = &dbe->cpr->call_path;
216 enum chain_order saved_order = callchain_param.order;
219 if (!symbol_conf.use_callchain || !sample->callchain)
223 * Since the call path tree must be built starting with the root, we
224 * must use ORDER_CALL for call chain resolution, in order to process
225 * the callchain starting with the root node and ending with the leaf.
227 callchain_param.order = ORDER_CALLER;
228 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
229 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
231 callchain_param.order = saved_order;
234 callchain_cursor_commit(&callchain_cursor);
237 struct callchain_cursor_node *node;
238 struct addr_location al;
239 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
241 memset(&al, 0, sizeof(al));
243 node = callchain_cursor_current(&callchain_cursor);
247 * Handle export of symbol and dso for this node by
248 * constructing an addr_location struct and then passing it to
249 * db_ids_from_al() to perform the export.
253 al.machine = machine;
256 if (al.map && !al.sym)
257 al.sym = dso__find_symbol(al.map->dso, al.addr);
259 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
261 /* add node to the call path tree if it doesn't exist */
262 current = call_path__findnew(dbe->cpr, current,
266 callchain_cursor_advance(&callchain_cursor);
269 /* Reset the callchain order to its prior value. */
270 callchain_param.order = saved_order;
272 if (current == &dbe->cpr->call_path) {
273 /* Bail because the callchain was empty. */
280 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
283 if (dbe->export_branch_type)
284 return dbe->export_branch_type(dbe, branch_type, name);
289 int db_export__sample(struct db_export *dbe, union perf_event *event,
290 struct perf_sample *sample, struct perf_evsel *evsel,
291 struct addr_location *al)
293 struct thread *thread = al->thread;
294 struct export_sample es = {
300 struct thread *main_thread;
301 struct comm *comm = NULL;
304 err = db_export__evsel(dbe, evsel);
308 err = db_export__machine(dbe, al->machine);
312 main_thread = thread__main_thread(al->machine, thread);
315 * A thread has a reference to the main thread, so export the
318 err = db_export__thread(dbe, main_thread, al->machine,
323 * Export comm before exporting the non-main thread because
324 * db_export__comm_thread() can be called further below.
326 comm = machine__thread_exec_comm(al->machine, main_thread);
328 err = db_export__exec_comm(dbe, comm, main_thread);
331 es.comm_db_id = comm->db_id;
335 if (thread != main_thread) {
337 * For a non-main thread, db_export__comm_thread() must be
338 * called only if thread has not previously been exported.
340 bool export_comm_thread = comm && !thread->db_id;
342 err = db_export__thread(dbe, thread, al->machine, main_thread);
346 if (export_comm_thread) {
347 err = db_export__comm_thread(dbe, comm, thread);
353 es.db_id = ++dbe->sample_last_db_id;
355 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
360 struct call_path *cp = call_path_from_sample(dbe, al->machine,
364 db_export__call_path(dbe, cp);
365 es.call_path_id = cp->db_id;
369 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
370 sample_addr_correlates_sym(&evsel->attr)) {
371 struct addr_location addr_al;
373 thread__resolve(thread, &addr_al, sample);
374 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
375 &es.addr_sym_db_id, &es.addr_offset);
379 err = thread_stack__process(thread, comm, sample, al,
387 if (dbe->export_sample)
388 err = dbe->export_sample(dbe, &es);
391 thread__put(main_thread);
400 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
401 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
402 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
403 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
404 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
405 "software interrupt"},
406 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
407 "return from interrupt"},
408 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
410 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
411 "return from system call"},
412 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
413 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
414 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
415 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
416 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
417 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
421 int db_export__branch_types(struct db_export *dbe)
425 for (i = 0; branch_types[i].name ; i++) {
426 err = db_export__branch_type(dbe, branch_types[i].branch_type,
427 branch_types[i].name);
432 /* Add trace begin / end variants */
433 for (i = 0; branch_types[i].name ; i++) {
434 const char *name = branch_types[i].name;
435 u32 type = branch_types[i].branch_type;
438 if (type == PERF_IP_FLAG_BRANCH ||
439 (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
442 snprintf(buf, sizeof(buf), "trace begin / %s", name);
443 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
447 snprintf(buf, sizeof(buf), "%s / trace end", name);
448 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
456 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
464 err = db_export__call_path(dbe, cp->parent);
469 cp->db_id = ++dbe->call_path_last_db_id;
471 if (dbe->export_call_path)
472 return dbe->export_call_path(dbe, cp);
477 int db_export__call_return(struct db_export *dbe, struct call_return *cr,
482 err = db_export__call_path(dbe, cr->cp);
487 cr->db_id = ++dbe->call_return_last_db_id;
491 *parent_db_id = ++dbe->call_return_last_db_id;
492 cr->parent_db_id = *parent_db_id;
495 if (dbe->export_call_return)
496 return dbe->export_call_return(dbe, cr);