1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * db-export.c: Support for exporting data suitable for import to a database
4 * Copyright (c) 2014, Intel Corporation.
5 */
6
7 #include <errno.h>
8 #include <stdlib.h>
9
10 #include "dso.h"
11 #include "evsel.h"
12 #include "machine.h"
13 #include "thread.h"
14 #include "comm.h"
15 #include "symbol.h"
16 #include "map.h"
17 #include "event.h"
18 #include "thread-stack.h"
19 #include "callchain.h"
20 #include "call-path.h"
21 #include "db-export.h"
22 #include <linux/zalloc.h>
23
db_export__init(struct db_export * dbe)24 int db_export__init(struct db_export *dbe)
25 {
26 memset(dbe, 0, sizeof(struct db_export));
27 return 0;
28 }
29
db_export__exit(struct db_export * dbe)30 void db_export__exit(struct db_export *dbe)
31 {
32 call_return_processor__free(dbe->crp);
33 dbe->crp = NULL;
34 }
35
db_export__evsel(struct db_export * dbe,struct evsel * evsel)36 int db_export__evsel(struct db_export *dbe, struct evsel *evsel)
37 {
38 if (evsel->db_id)
39 return 0;
40
41 evsel->db_id = ++dbe->evsel_last_db_id;
42
43 if (dbe->export_evsel)
44 return dbe->export_evsel(dbe, evsel);
45
46 return 0;
47 }
48
db_export__machine(struct db_export * dbe,struct machine * machine)49 int db_export__machine(struct db_export *dbe, struct machine *machine)
50 {
51 if (machine->db_id)
52 return 0;
53
54 machine->db_id = ++dbe->machine_last_db_id;
55
56 if (dbe->export_machine)
57 return dbe->export_machine(dbe, machine);
58
59 return 0;
60 }
61
db_export__thread(struct db_export * dbe,struct thread * thread,struct machine * machine,struct thread * main_thread)62 int db_export__thread(struct db_export *dbe, struct thread *thread,
63 struct machine *machine, struct thread *main_thread)
64 {
65 u64 main_thread_db_id = 0;
66
67 if (thread__db_id(thread))
68 return 0;
69
70 thread__set_db_id(thread, ++dbe->thread_last_db_id);
71
72 if (main_thread)
73 main_thread_db_id = thread__db_id(main_thread);
74
75 if (dbe->export_thread)
76 return dbe->export_thread(dbe, thread, main_thread_db_id,
77 machine);
78
79 return 0;
80 }
81
__db_export__comm(struct db_export * dbe,struct comm * comm,struct thread * thread)82 static int __db_export__comm(struct db_export *dbe, struct comm *comm,
83 struct thread *thread)
84 {
85 comm->db_id = ++dbe->comm_last_db_id;
86
87 if (dbe->export_comm)
88 return dbe->export_comm(dbe, comm, thread);
89
90 return 0;
91 }
92
db_export__comm(struct db_export * dbe,struct comm * comm,struct thread * thread)93 int db_export__comm(struct db_export *dbe, struct comm *comm,
94 struct thread *thread)
95 {
96 if (comm->db_id)
97 return 0;
98
99 return __db_export__comm(dbe, comm, thread);
100 }
101
102 /*
103 * Export the "exec" comm. The "exec" comm is the program / application command
104 * name at the time it first executes. It is used to group threads for the same
105 * program. Note that the main thread pid (or thread group id tgid) cannot be
106 * used because it does not change when a new program is exec'ed.
107 */
db_export__exec_comm(struct db_export * dbe,struct comm * comm,struct thread * main_thread)108 int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
109 struct thread *main_thread)
110 {
111 int err;
112
113 if (comm->db_id)
114 return 0;
115
116 err = __db_export__comm(dbe, comm, main_thread);
117 if (err)
118 return err;
119
120 /*
121 * Record the main thread for this comm. Note that the main thread can
122 * have many "exec" comms because there will be a new one every time it
123 * exec's. An "exec" comm however will only ever have 1 main thread.
124 * That is different to any other threads for that same program because
125 * exec() will effectively kill them, so the relationship between the
126 * "exec" comm and non-main threads is 1-to-1. That is why
127 * db_export__comm_thread() is called here for the main thread, but it
128 * is called for non-main threads when they are exported.
129 */
130 return db_export__comm_thread(dbe, comm, main_thread);
131 }
132
db_export__comm_thread(struct db_export * dbe,struct comm * comm,struct thread * thread)133 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
134 struct thread *thread)
135 {
136 u64 db_id;
137
138 db_id = ++dbe->comm_thread_last_db_id;
139
140 if (dbe->export_comm_thread)
141 return dbe->export_comm_thread(dbe, db_id, comm, thread);
142
143 return 0;
144 }
145
db_export__dso(struct db_export * dbe,struct dso * dso,struct machine * machine)146 int db_export__dso(struct db_export *dbe, struct dso *dso,
147 struct machine *machine)
148 {
149 if (dso__db_id(dso))
150 return 0;
151
152 dso__set_db_id(dso, ++dbe->dso_last_db_id);
153
154 if (dbe->export_dso)
155 return dbe->export_dso(dbe, dso, machine);
156
157 return 0;
158 }
159
db_export__symbol(struct db_export * dbe,struct symbol * sym,struct dso * dso)160 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
161 struct dso *dso)
162 {
163 u64 *sym_db_id = symbol__priv(sym);
164
165 if (*sym_db_id)
166 return 0;
167
168 *sym_db_id = ++dbe->symbol_last_db_id;
169
170 if (dbe->export_symbol)
171 return dbe->export_symbol(dbe, sym, dso);
172
173 return 0;
174 }
175
db_ids_from_al(struct db_export * dbe,struct addr_location * al,u64 * dso_db_id,u64 * sym_db_id,u64 * offset)176 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
177 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
178 {
179 int err;
180
181 if (al->map) {
182 struct dso *dso = map__dso(al->map);
183
184 err = db_export__dso(dbe, dso, maps__machine(thread__maps(al->thread)));
185 if (err)
186 return err;
187 *dso_db_id = dso__db_id(dso);
188
189 if (!al->sym) {
190 al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
191 if (al->sym)
192 dso__insert_symbol(dso, al->sym);
193 }
194
195 if (al->sym) {
196 u64 *db_id = symbol__priv(al->sym);
197
198 err = db_export__symbol(dbe, al->sym, dso);
199 if (err)
200 return err;
201 *sym_db_id = *db_id;
202 *offset = al->addr - al->sym->start;
203 }
204 }
205
206 return 0;
207 }
208
call_path_from_sample(struct db_export * dbe,struct machine * machine,struct thread * thread,struct perf_sample * sample,struct evsel * evsel)209 static struct call_path *call_path_from_sample(struct db_export *dbe,
210 struct machine *machine,
211 struct thread *thread,
212 struct perf_sample *sample,
213 struct evsel *evsel)
214 {
215 u64 kernel_start = machine__kernel_start(machine);
216 struct call_path *current = &dbe->cpr->call_path;
217 enum chain_order saved_order = callchain_param.order;
218 struct callchain_cursor *cursor;
219 int err;
220
221 if (!symbol_conf.use_callchain || !sample->callchain)
222 return NULL;
223
224 /*
225 * Since the call path tree must be built starting with the root, we
226 * must use ORDER_CALL for call chain resolution, in order to process
227 * the callchain starting with the root node and ending with the leaf.
228 */
229 callchain_param.order = ORDER_CALLER;
230 cursor = get_tls_callchain_cursor();
231 err = thread__resolve_callchain(thread, cursor, evsel,
232 sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
233 if (err) {
234 callchain_param.order = saved_order;
235 return NULL;
236 }
237 callchain_cursor_commit(cursor);
238
239 while (1) {
240 struct callchain_cursor_node *node;
241 struct addr_location al;
242 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
243
244
245 node = callchain_cursor_current(cursor);
246 if (!node)
247 break;
248
249 /*
250 * Handle export of symbol and dso for this node by
251 * constructing an addr_location struct and then passing it to
252 * db_ids_from_al() to perform the export.
253 */
254 addr_location__init(&al);
255 al.sym = node->ms.sym;
256 al.map = map__get(node->ms.map);
257 al.maps = maps__get(thread__maps(thread));
258 al.addr = node->ip;
259 al.thread = thread__get(thread);
260
261 if (al.map && !al.sym)
262 al.sym = dso__find_symbol(map__dso(al.map), al.addr);
263
264 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
265
266 /* add node to the call path tree if it doesn't exist */
267 current = call_path__findnew(dbe->cpr, current,
268 al.sym, node->ip,
269 kernel_start);
270
271 callchain_cursor_advance(cursor);
272 addr_location__exit(&al);
273 }
274
275 /* Reset the callchain order to its prior value. */
276 callchain_param.order = saved_order;
277
278 if (current == &dbe->cpr->call_path) {
279 /* Bail because the callchain was empty. */
280 return NULL;
281 }
282
283 return current;
284 }
285
db_export__branch_type(struct db_export * dbe,u32 branch_type,const char * name)286 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
287 const char *name)
288 {
289 if (dbe->export_branch_type)
290 return dbe->export_branch_type(dbe, branch_type, name);
291
292 return 0;
293 }
294
db_export__threads(struct db_export * dbe,struct thread * thread,struct thread * main_thread,struct machine * machine,struct comm ** comm_ptr)295 static int db_export__threads(struct db_export *dbe, struct thread *thread,
296 struct thread *main_thread,
297 struct machine *machine, struct comm **comm_ptr)
298 {
299 struct comm *comm = NULL;
300 struct comm *curr_comm;
301 int err;
302
303 if (main_thread) {
304 /*
305 * A thread has a reference to the main thread, so export the
306 * main thread first.
307 */
308 err = db_export__thread(dbe, main_thread, machine, main_thread);
309 if (err)
310 return err;
311 /*
312 * Export comm before exporting the non-main thread because
313 * db_export__comm_thread() can be called further below.
314 */
315 comm = machine__thread_exec_comm(machine, main_thread);
316 if (comm) {
317 err = db_export__exec_comm(dbe, comm, main_thread);
318 if (err)
319 return err;
320 *comm_ptr = comm;
321 }
322 }
323
324 if (thread != main_thread) {
325 /*
326 * For a non-main thread, db_export__comm_thread() must be
327 * called only if thread has not previously been exported.
328 */
329 bool export_comm_thread = comm && !thread__db_id(thread);
330
331 err = db_export__thread(dbe, thread, machine, main_thread);
332 if (err)
333 return err;
334
335 if (export_comm_thread) {
336 err = db_export__comm_thread(dbe, comm, thread);
337 if (err)
338 return err;
339 }
340 }
341
342 curr_comm = thread__comm(thread);
343 if (curr_comm)
344 return db_export__comm(dbe, curr_comm, thread);
345
346 return 0;
347 }
348
db_export__sample(struct db_export * dbe,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct addr_location * al,struct addr_location * addr_al)349 int db_export__sample(struct db_export *dbe, union perf_event *event,
350 struct perf_sample *sample, struct evsel *evsel,
351 struct addr_location *al, struct addr_location *addr_al)
352 {
353 struct thread *thread = al->thread;
354 struct export_sample es = {
355 .event = event,
356 .sample = sample,
357 .evsel = evsel,
358 .al = al,
359 };
360 struct thread *main_thread;
361 struct comm *comm = NULL;
362 struct machine *machine = NULL;
363 int err;
364
365 if (thread__maps(thread))
366 machine = maps__machine(thread__maps(thread));
367 if (!machine)
368 return -1;
369
370 err = db_export__evsel(dbe, evsel);
371 if (err)
372 return err;
373
374 err = db_export__machine(dbe, machine);
375 if (err)
376 return err;
377
378 main_thread = thread__main_thread(machine, thread);
379
380 err = db_export__threads(dbe, thread, main_thread, machine, &comm);
381 if (err)
382 goto out_put;
383
384 if (comm)
385 es.comm_db_id = comm->db_id;
386
387 es.db_id = ++dbe->sample_last_db_id;
388
389 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
390 if (err)
391 goto out_put;
392
393 if (dbe->cpr) {
394 struct call_path *cp = call_path_from_sample(dbe, machine,
395 thread, sample,
396 evsel);
397 if (cp) {
398 db_export__call_path(dbe, cp);
399 es.call_path_id = cp->db_id;
400 }
401 }
402
403 if (addr_al) {
404 err = db_ids_from_al(dbe, addr_al, &es.addr_dso_db_id,
405 &es.addr_sym_db_id, &es.addr_offset);
406 if (err)
407 goto out_put;
408 if (dbe->crp) {
409 err = thread_stack__process(thread, comm, sample, al,
410 addr_al, es.db_id,
411 dbe->crp);
412 if (err)
413 goto out_put;
414 }
415 }
416
417 if (dbe->export_sample)
418 err = dbe->export_sample(dbe, &es);
419
420 out_put:
421 thread__put(main_thread);
422 return err;
423 }
424
425 static struct {
426 u32 branch_type;
427 const char *name;
428 } branch_types[] = {
429 {0, "no branch"},
430 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
431 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
432 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
433 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
434 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
435 "software interrupt"},
436 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
437 "return from interrupt"},
438 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
439 "system call"},
440 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
441 "return from system call"},
442 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
443 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
444 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
445 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
446 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
447 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
448 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vm entry"},
449 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vm exit"},
450 {0, NULL}
451 };
452
db_export__branch_types(struct db_export * dbe)453 int db_export__branch_types(struct db_export *dbe)
454 {
455 int i, err = 0;
456
457 for (i = 0; branch_types[i].name ; i++) {
458 err = db_export__branch_type(dbe, branch_types[i].branch_type,
459 branch_types[i].name);
460 if (err)
461 break;
462 }
463
464 /* Add trace begin / end variants */
465 for (i = 0; branch_types[i].name ; i++) {
466 const char *name = branch_types[i].name;
467 u32 type = branch_types[i].branch_type;
468 char buf[64];
469
470 if (type == PERF_IP_FLAG_BRANCH ||
471 (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
472 continue;
473
474 snprintf(buf, sizeof(buf), "trace begin / %s", name);
475 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
476 if (err)
477 break;
478
479 snprintf(buf, sizeof(buf), "%s / trace end", name);
480 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
481 if (err)
482 break;
483 }
484
485 return err;
486 }
487
db_export__call_path(struct db_export * dbe,struct call_path * cp)488 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
489 {
490 int err;
491
492 if (cp->db_id)
493 return 0;
494
495 if (cp->parent) {
496 err = db_export__call_path(dbe, cp->parent);
497 if (err)
498 return err;
499 }
500
501 cp->db_id = ++dbe->call_path_last_db_id;
502
503 if (dbe->export_call_path)
504 return dbe->export_call_path(dbe, cp);
505
506 return 0;
507 }
508
db_export__call_return(struct db_export * dbe,struct call_return * cr,u64 * parent_db_id)509 int db_export__call_return(struct db_export *dbe, struct call_return *cr,
510 u64 *parent_db_id)
511 {
512 int err;
513
514 err = db_export__call_path(dbe, cr->cp);
515 if (err)
516 return err;
517
518 if (!cr->db_id)
519 cr->db_id = ++dbe->call_return_last_db_id;
520
521 if (parent_db_id) {
522 if (!*parent_db_id)
523 *parent_db_id = ++dbe->call_return_last_db_id;
524 cr->parent_db_id = *parent_db_id;
525 }
526
527 if (dbe->export_call_return)
528 return dbe->export_call_return(dbe, cr);
529
530 return 0;
531 }
532
db_export__pid_tid(struct db_export * dbe,struct machine * machine,pid_t pid,pid_t tid,u64 * db_id,struct comm ** comm_ptr,bool * is_idle)533 static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
534 pid_t pid, pid_t tid, u64 *db_id,
535 struct comm **comm_ptr, bool *is_idle)
536 {
537 struct thread *thread = machine__find_thread(machine, pid, tid);
538 struct thread *main_thread;
539 int err = 0;
540
541 if (!thread || !thread__comm_set(thread))
542 goto out_put;
543
544 *is_idle = !thread__pid(thread) && !thread__tid(thread);
545
546 main_thread = thread__main_thread(machine, thread);
547
548 err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);
549
550 *db_id = thread__db_id(thread);
551
552 thread__put(main_thread);
553 out_put:
554 thread__put(thread);
555
556 return err;
557 }
558
db_export__switch(struct db_export * dbe,union perf_event * event,struct perf_sample * sample,struct machine * machine)559 int db_export__switch(struct db_export *dbe, union perf_event *event,
560 struct perf_sample *sample, struct machine *machine)
561 {
562 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
563 bool out_preempt = out &&
564 (event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
565 int flags = out | (out_preempt << 1);
566 bool is_idle_a = false, is_idle_b = false;
567 u64 th_a_id = 0, th_b_id = 0;
568 u64 comm_out_id, comm_in_id;
569 struct comm *comm_a = NULL;
570 struct comm *comm_b = NULL;
571 u64 th_out_id, th_in_id;
572 u64 db_id;
573 int err;
574
575 err = db_export__machine(dbe, machine);
576 if (err)
577 return err;
578
579 err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
580 &th_a_id, &comm_a, &is_idle_a);
581 if (err)
582 return err;
583
584 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
585 pid_t pid = event->context_switch.next_prev_pid;
586 pid_t tid = event->context_switch.next_prev_tid;
587
588 err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
589 &comm_b, &is_idle_b);
590 if (err)
591 return err;
592 }
593
594 /*
595 * Do not export if both threads are unknown (i.e. not being traced),
596 * or one is unknown and the other is the idle task.
597 */
598 if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
599 return 0;
600
601 db_id = ++dbe->context_switch_last_db_id;
602
603 if (out) {
604 th_out_id = th_a_id;
605 th_in_id = th_b_id;
606 comm_out_id = comm_a ? comm_a->db_id : 0;
607 comm_in_id = comm_b ? comm_b->db_id : 0;
608 } else {
609 th_out_id = th_b_id;
610 th_in_id = th_a_id;
611 comm_out_id = comm_b ? comm_b->db_id : 0;
612 comm_in_id = comm_a ? comm_a->db_id : 0;
613 }
614
615 if (dbe->export_context_switch)
616 return dbe->export_context_switch(dbe, db_id, machine, sample,
617 th_out_id, comm_out_id,
618 th_in_id, comm_in_id, flags);
619 return 0;
620 }
621