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,
82 * Export the "exec" comm. The "exec" comm is the program / application command
83 * name at the time it first executes. It is used to group threads for the same
84 * program. Note that the main thread pid (or thread group id tgid) cannot be
85 * used because it does not change when a new program is exec'ed.
87 int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
88 struct thread *main_thread)
95 comm->db_id = ++dbe->comm_last_db_id;
97 if (dbe->export_comm) {
98 err = dbe->export_comm(dbe, comm, main_thread);
104 * Record the main thread for this comm. Note that the main thread can
105 * have many "exec" comms because there will be a new one every time it
106 * exec's. An "exec" comm however will only ever have 1 main thread.
107 * That is different to any other threads for that same program because
108 * exec() will effectively kill them, so the relationship between the
109 * "exec" comm and non-main threads is 1-to-1. That is why
110 * db_export__comm_thread() is called here for the main thread, but it
111 * is called for non-main threads when they are exported.
113 return db_export__comm_thread(dbe, comm, main_thread);
116 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
117 struct thread *thread)
121 db_id = ++dbe->comm_thread_last_db_id;
123 if (dbe->export_comm_thread)
124 return dbe->export_comm_thread(dbe, db_id, comm, thread);
129 int db_export__dso(struct db_export *dbe, struct dso *dso,
130 struct machine *machine)
135 dso->db_id = ++dbe->dso_last_db_id;
138 return dbe->export_dso(dbe, dso, machine);
143 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
146 u64 *sym_db_id = symbol__priv(sym);
151 *sym_db_id = ++dbe->symbol_last_db_id;
153 if (dbe->export_symbol)
154 return dbe->export_symbol(dbe, sym, dso);
159 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
160 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
165 struct dso *dso = al->map->dso;
167 err = db_export__dso(dbe, dso, al->machine);
170 *dso_db_id = dso->db_id;
173 al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
175 dso__insert_symbol(dso, al->sym);
179 u64 *db_id = symbol__priv(al->sym);
181 err = db_export__symbol(dbe, al->sym, dso);
185 *offset = al->addr - al->sym->start;
192 static struct call_path *call_path_from_sample(struct db_export *dbe,
193 struct machine *machine,
194 struct thread *thread,
195 struct perf_sample *sample,
196 struct perf_evsel *evsel)
198 u64 kernel_start = machine__kernel_start(machine);
199 struct call_path *current = &dbe->cpr->call_path;
200 enum chain_order saved_order = callchain_param.order;
203 if (!symbol_conf.use_callchain || !sample->callchain)
207 * Since the call path tree must be built starting with the root, we
208 * must use ORDER_CALL for call chain resolution, in order to process
209 * the callchain starting with the root node and ending with the leaf.
211 callchain_param.order = ORDER_CALLER;
212 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
213 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
215 callchain_param.order = saved_order;
218 callchain_cursor_commit(&callchain_cursor);
221 struct callchain_cursor_node *node;
222 struct addr_location al;
223 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
225 memset(&al, 0, sizeof(al));
227 node = callchain_cursor_current(&callchain_cursor);
231 * Handle export of symbol and dso for this node by
232 * constructing an addr_location struct and then passing it to
233 * db_ids_from_al() to perform the export.
237 al.machine = machine;
240 if (al.map && !al.sym)
241 al.sym = dso__find_symbol(al.map->dso, al.addr);
243 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
245 /* add node to the call path tree if it doesn't exist */
246 current = call_path__findnew(dbe->cpr, current,
250 callchain_cursor_advance(&callchain_cursor);
253 /* Reset the callchain order to its prior value. */
254 callchain_param.order = saved_order;
256 if (current == &dbe->cpr->call_path) {
257 /* Bail because the callchain was empty. */
264 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
267 if (dbe->export_branch_type)
268 return dbe->export_branch_type(dbe, branch_type, name);
273 int db_export__sample(struct db_export *dbe, union perf_event *event,
274 struct perf_sample *sample, struct perf_evsel *evsel,
275 struct addr_location *al)
277 struct thread *thread = al->thread;
278 struct export_sample es = {
284 struct thread *main_thread;
285 struct comm *comm = NULL;
288 err = db_export__evsel(dbe, evsel);
292 err = db_export__machine(dbe, al->machine);
296 main_thread = thread__main_thread(al->machine, thread);
299 * A thread has a reference to the main thread, so export the
302 err = db_export__thread(dbe, main_thread, al->machine,
307 * Export comm before exporting the non-main thread because
308 * db_export__comm_thread() can be called further below.
310 comm = machine__thread_exec_comm(al->machine, main_thread);
312 err = db_export__exec_comm(dbe, comm, main_thread);
315 es.comm_db_id = comm->db_id;
319 if (thread != main_thread) {
321 * For a non-main thread, db_export__comm_thread() must be
322 * called only if thread has not previously been exported.
324 bool export_comm_thread = comm && !thread->db_id;
326 err = db_export__thread(dbe, thread, al->machine, main_thread);
330 if (export_comm_thread) {
331 err = db_export__comm_thread(dbe, comm, thread);
337 es.db_id = ++dbe->sample_last_db_id;
339 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
344 struct call_path *cp = call_path_from_sample(dbe, al->machine,
348 db_export__call_path(dbe, cp);
349 es.call_path_id = cp->db_id;
353 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
354 sample_addr_correlates_sym(&evsel->attr)) {
355 struct addr_location addr_al;
357 thread__resolve(thread, &addr_al, sample);
358 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
359 &es.addr_sym_db_id, &es.addr_offset);
363 err = thread_stack__process(thread, comm, sample, al,
371 if (dbe->export_sample)
372 err = dbe->export_sample(dbe, &es);
375 thread__put(main_thread);
384 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
385 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
386 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
387 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
388 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
389 "software interrupt"},
390 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
391 "return from interrupt"},
392 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
394 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
395 "return from system call"},
396 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
397 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
398 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
399 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
400 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
401 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
405 int db_export__branch_types(struct db_export *dbe)
409 for (i = 0; branch_types[i].name ; i++) {
410 err = db_export__branch_type(dbe, branch_types[i].branch_type,
411 branch_types[i].name);
416 /* Add trace begin / end variants */
417 for (i = 0; branch_types[i].name ; i++) {
418 const char *name = branch_types[i].name;
419 u32 type = branch_types[i].branch_type;
422 if (type == PERF_IP_FLAG_BRANCH ||
423 (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
426 snprintf(buf, sizeof(buf), "trace begin / %s", name);
427 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
431 snprintf(buf, sizeof(buf), "%s / trace end", name);
432 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
440 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
448 err = db_export__call_path(dbe, cp->parent);
453 cp->db_id = ++dbe->call_path_last_db_id;
455 if (dbe->export_call_path)
456 return dbe->export_call_path(dbe, cp);
461 int db_export__call_return(struct db_export *dbe, struct call_return *cr,
466 err = db_export__call_path(dbe, cr->cp);
471 cr->db_id = ++dbe->call_return_last_db_id;
475 *parent_db_id = ++dbe->call_return_last_db_id;
476 cr->parent_db_id = *parent_db_id;
479 if (dbe->export_call_return)
480 return dbe->export_call_return(dbe, cr);