]>
Commit | Line | Data |
---|---|---|
0db15b1e AH |
1 | /* |
2 | * db-export.c: Support for exporting data suitable for import to a database | |
3 | * Copyright (c) 2014, Intel Corporation. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <errno.h> | |
17 | ||
18 | #include "evsel.h" | |
19 | #include "machine.h" | |
20 | #include "thread.h" | |
21 | #include "comm.h" | |
22 | #include "symbol.h" | |
1101f69a | 23 | #include "map.h" |
0db15b1e | 24 | #include "event.h" |
758008b2 | 25 | #include "util.h" |
88f50d60 | 26 | #include "thread-stack.h" |
0a3eba3a | 27 | #include "callchain.h" |
451db126 | 28 | #include "call-path.h" |
0db15b1e AH |
29 | #include "db-export.h" |
30 | ||
758008b2 AH |
31 | struct deferred_export { |
32 | struct list_head node; | |
33 | struct comm *comm; | |
34 | }; | |
35 | ||
36 | static int db_export__deferred(struct db_export *dbe) | |
37 | { | |
38 | struct deferred_export *de; | |
39 | int err; | |
40 | ||
41 | while (!list_empty(&dbe->deferred)) { | |
42 | de = list_entry(dbe->deferred.next, struct deferred_export, | |
43 | node); | |
44 | err = dbe->export_comm(dbe, de->comm); | |
45 | list_del(&de->node); | |
46 | free(de); | |
47 | if (err) | |
48 | return err; | |
49 | } | |
50 | ||
51 | return 0; | |
52 | } | |
53 | ||
54 | static void db_export__free_deferred(struct db_export *dbe) | |
55 | { | |
56 | struct deferred_export *de; | |
57 | ||
58 | while (!list_empty(&dbe->deferred)) { | |
59 | de = list_entry(dbe->deferred.next, struct deferred_export, | |
60 | node); | |
61 | list_del(&de->node); | |
62 | free(de); | |
63 | } | |
64 | } | |
65 | ||
66 | static int db_export__defer_comm(struct db_export *dbe, struct comm *comm) | |
67 | { | |
68 | struct deferred_export *de; | |
69 | ||
70 | de = zalloc(sizeof(struct deferred_export)); | |
71 | if (!de) | |
72 | return -ENOMEM; | |
73 | ||
74 | de->comm = comm; | |
75 | list_add_tail(&de->node, &dbe->deferred); | |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
0db15b1e AH |
80 | int db_export__init(struct db_export *dbe) |
81 | { | |
82 | memset(dbe, 0, sizeof(struct db_export)); | |
758008b2 | 83 | INIT_LIST_HEAD(&dbe->deferred); |
0db15b1e AH |
84 | return 0; |
85 | } | |
86 | ||
758008b2 AH |
87 | int db_export__flush(struct db_export *dbe) |
88 | { | |
89 | return db_export__deferred(dbe); | |
90 | } | |
91 | ||
88f50d60 | 92 | void db_export__exit(struct db_export *dbe) |
0db15b1e | 93 | { |
758008b2 | 94 | db_export__free_deferred(dbe); |
88f50d60 AH |
95 | call_return_processor__free(dbe->crp); |
96 | dbe->crp = NULL; | |
0db15b1e AH |
97 | } |
98 | ||
99 | int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel) | |
100 | { | |
101 | if (evsel->db_id) | |
102 | return 0; | |
103 | ||
104 | evsel->db_id = ++dbe->evsel_last_db_id; | |
105 | ||
106 | if (dbe->export_evsel) | |
107 | return dbe->export_evsel(dbe, evsel); | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | int db_export__machine(struct db_export *dbe, struct machine *machine) | |
113 | { | |
114 | if (machine->db_id) | |
115 | return 0; | |
116 | ||
117 | machine->db_id = ++dbe->machine_last_db_id; | |
118 | ||
119 | if (dbe->export_machine) | |
120 | return dbe->export_machine(dbe, machine); | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
125 | int db_export__thread(struct db_export *dbe, struct thread *thread, | |
126 | struct machine *machine, struct comm *comm) | |
127 | { | |
b91fc39f | 128 | struct thread *main_thread; |
0db15b1e AH |
129 | u64 main_thread_db_id = 0; |
130 | int err; | |
131 | ||
132 | if (thread->db_id) | |
133 | return 0; | |
134 | ||
135 | thread->db_id = ++dbe->thread_last_db_id; | |
136 | ||
137 | if (thread->pid_ != -1) { | |
0db15b1e AH |
138 | if (thread->pid_ == thread->tid) { |
139 | main_thread = thread; | |
140 | } else { | |
141 | main_thread = machine__findnew_thread(machine, | |
142 | thread->pid_, | |
143 | thread->pid_); | |
144 | if (!main_thread) | |
145 | return -ENOMEM; | |
146 | err = db_export__thread(dbe, main_thread, machine, | |
147 | comm); | |
148 | if (err) | |
b91fc39f | 149 | goto out_put; |
0db15b1e AH |
150 | if (comm) { |
151 | err = db_export__comm_thread(dbe, comm, thread); | |
152 | if (err) | |
b91fc39f | 153 | goto out_put; |
0db15b1e AH |
154 | } |
155 | } | |
156 | main_thread_db_id = main_thread->db_id; | |
b91fc39f ACM |
157 | if (main_thread != thread) |
158 | thread__put(main_thread); | |
0db15b1e AH |
159 | } |
160 | ||
161 | if (dbe->export_thread) | |
162 | return dbe->export_thread(dbe, thread, main_thread_db_id, | |
163 | machine); | |
164 | ||
165 | return 0; | |
b91fc39f ACM |
166 | |
167 | out_put: | |
168 | thread__put(main_thread); | |
169 | return err; | |
0db15b1e AH |
170 | } |
171 | ||
172 | int db_export__comm(struct db_export *dbe, struct comm *comm, | |
173 | struct thread *main_thread) | |
174 | { | |
175 | int err; | |
176 | ||
177 | if (comm->db_id) | |
178 | return 0; | |
179 | ||
180 | comm->db_id = ++dbe->comm_last_db_id; | |
181 | ||
182 | if (dbe->export_comm) { | |
758008b2 AH |
183 | if (main_thread->comm_set) |
184 | err = dbe->export_comm(dbe, comm); | |
185 | else | |
186 | err = db_export__defer_comm(dbe, comm); | |
0db15b1e AH |
187 | if (err) |
188 | return err; | |
189 | } | |
190 | ||
191 | return db_export__comm_thread(dbe, comm, main_thread); | |
192 | } | |
193 | ||
194 | int db_export__comm_thread(struct db_export *dbe, struct comm *comm, | |
195 | struct thread *thread) | |
196 | { | |
197 | u64 db_id; | |
198 | ||
199 | db_id = ++dbe->comm_thread_last_db_id; | |
200 | ||
201 | if (dbe->export_comm_thread) | |
202 | return dbe->export_comm_thread(dbe, db_id, comm, thread); | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | int db_export__dso(struct db_export *dbe, struct dso *dso, | |
208 | struct machine *machine) | |
209 | { | |
210 | if (dso->db_id) | |
211 | return 0; | |
212 | ||
213 | dso->db_id = ++dbe->dso_last_db_id; | |
214 | ||
215 | if (dbe->export_dso) | |
216 | return dbe->export_dso(dbe, dso, machine); | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | int db_export__symbol(struct db_export *dbe, struct symbol *sym, | |
222 | struct dso *dso) | |
223 | { | |
224 | u64 *sym_db_id = symbol__priv(sym); | |
225 | ||
226 | if (*sym_db_id) | |
227 | return 0; | |
228 | ||
229 | *sym_db_id = ++dbe->symbol_last_db_id; | |
230 | ||
231 | if (dbe->export_symbol) | |
232 | return dbe->export_symbol(dbe, sym, dso); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
0db15b1e AH |
237 | static int db_ids_from_al(struct db_export *dbe, struct addr_location *al, |
238 | u64 *dso_db_id, u64 *sym_db_id, u64 *offset) | |
239 | { | |
240 | int err; | |
241 | ||
242 | if (al->map) { | |
243 | struct dso *dso = al->map->dso; | |
244 | ||
245 | err = db_export__dso(dbe, dso, al->machine); | |
246 | if (err) | |
247 | return err; | |
248 | *dso_db_id = dso->db_id; | |
249 | ||
250 | if (!al->sym) { | |
af30bffa | 251 | al->sym = symbol__new(al->addr, 0, 0, 0, "unknown"); |
0db15b1e | 252 | if (al->sym) |
3183f8ca | 253 | dso__insert_symbol(dso, al->sym); |
0db15b1e AH |
254 | } |
255 | ||
256 | if (al->sym) { | |
257 | u64 *db_id = symbol__priv(al->sym); | |
258 | ||
259 | err = db_export__symbol(dbe, al->sym, dso); | |
260 | if (err) | |
261 | return err; | |
262 | *sym_db_id = *db_id; | |
263 | *offset = al->addr - al->sym->start; | |
264 | } | |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
0a3eba3a CP |
270 | static struct call_path *call_path_from_sample(struct db_export *dbe, |
271 | struct machine *machine, | |
272 | struct thread *thread, | |
273 | struct perf_sample *sample, | |
274 | struct perf_evsel *evsel) | |
275 | { | |
276 | u64 kernel_start = machine__kernel_start(machine); | |
277 | struct call_path *current = &dbe->cpr->call_path; | |
278 | enum chain_order saved_order = callchain_param.order; | |
279 | int err; | |
280 | ||
281 | if (!symbol_conf.use_callchain || !sample->callchain) | |
282 | return NULL; | |
283 | ||
284 | /* | |
285 | * Since the call path tree must be built starting with the root, we | |
286 | * must use ORDER_CALL for call chain resolution, in order to process | |
287 | * the callchain starting with the root node and ending with the leaf. | |
288 | */ | |
289 | callchain_param.order = ORDER_CALLER; | |
290 | err = thread__resolve_callchain(thread, &callchain_cursor, evsel, | |
fe176085 | 291 | sample, NULL, NULL, PERF_MAX_STACK_DEPTH); |
0a3eba3a CP |
292 | if (err) { |
293 | callchain_param.order = saved_order; | |
294 | return NULL; | |
295 | } | |
296 | callchain_cursor_commit(&callchain_cursor); | |
297 | ||
298 | while (1) { | |
299 | struct callchain_cursor_node *node; | |
300 | struct addr_location al; | |
301 | u64 dso_db_id = 0, sym_db_id = 0, offset = 0; | |
302 | ||
303 | memset(&al, 0, sizeof(al)); | |
304 | ||
305 | node = callchain_cursor_current(&callchain_cursor); | |
306 | if (!node) | |
307 | break; | |
308 | /* | |
309 | * Handle export of symbol and dso for this node by | |
310 | * constructing an addr_location struct and then passing it to | |
311 | * db_ids_from_al() to perform the export. | |
312 | */ | |
313 | al.sym = node->sym; | |
314 | al.map = node->map; | |
315 | al.machine = machine; | |
7a2544c0 | 316 | al.addr = node->ip; |
0a3eba3a | 317 | |
83302e79 | 318 | if (al.map && !al.sym) |
af07eeb0 | 319 | al.sym = dso__find_symbol(al.map->dso, al.addr); |
83302e79 | 320 | |
0a3eba3a CP |
321 | db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset); |
322 | ||
323 | /* add node to the call path tree if it doesn't exist */ | |
324 | current = call_path__findnew(dbe->cpr, current, | |
325 | al.sym, node->ip, | |
326 | kernel_start); | |
327 | ||
328 | callchain_cursor_advance(&callchain_cursor); | |
329 | } | |
330 | ||
331 | /* Reset the callchain order to its prior value. */ | |
332 | callchain_param.order = saved_order; | |
333 | ||
334 | if (current == &dbe->cpr->call_path) { | |
335 | /* Bail because the callchain was empty. */ | |
336 | return NULL; | |
337 | } | |
338 | ||
339 | return current; | |
340 | } | |
341 | ||
f2bff007 AH |
342 | int db_export__branch_type(struct db_export *dbe, u32 branch_type, |
343 | const char *name) | |
344 | { | |
345 | if (dbe->export_branch_type) | |
346 | return dbe->export_branch_type(dbe, branch_type, name); | |
347 | ||
348 | return 0; | |
349 | } | |
350 | ||
0db15b1e AH |
351 | int db_export__sample(struct db_export *dbe, union perf_event *event, |
352 | struct perf_sample *sample, struct perf_evsel *evsel, | |
7327259d | 353 | struct addr_location *al) |
0db15b1e | 354 | { |
7327259d | 355 | struct thread* thread = al->thread; |
0db15b1e AH |
356 | struct export_sample es = { |
357 | .event = event, | |
358 | .sample = sample, | |
359 | .evsel = evsel, | |
0db15b1e AH |
360 | .al = al, |
361 | }; | |
362 | struct thread *main_thread; | |
363 | struct comm *comm = NULL; | |
364 | int err; | |
365 | ||
366 | err = db_export__evsel(dbe, evsel); | |
367 | if (err) | |
368 | return err; | |
369 | ||
370 | err = db_export__machine(dbe, al->machine); | |
371 | if (err) | |
372 | return err; | |
373 | ||
480ca357 | 374 | main_thread = thread__main_thread(al->machine, thread); |
0db15b1e AH |
375 | if (main_thread) |
376 | comm = machine__thread_exec_comm(al->machine, main_thread); | |
377 | ||
378 | err = db_export__thread(dbe, thread, al->machine, comm); | |
379 | if (err) | |
427cde32 | 380 | goto out_put; |
0db15b1e AH |
381 | |
382 | if (comm) { | |
383 | err = db_export__comm(dbe, comm, main_thread); | |
384 | if (err) | |
427cde32 | 385 | goto out_put; |
0db15b1e AH |
386 | es.comm_db_id = comm->db_id; |
387 | } | |
388 | ||
389 | es.db_id = ++dbe->sample_last_db_id; | |
390 | ||
391 | err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset); | |
392 | if (err) | |
427cde32 | 393 | goto out_put; |
0db15b1e | 394 | |
0a3eba3a CP |
395 | if (dbe->cpr) { |
396 | struct call_path *cp = call_path_from_sample(dbe, al->machine, | |
397 | thread, sample, | |
398 | evsel); | |
568850ea | 399 | if (cp) { |
0a3eba3a | 400 | db_export__call_path(dbe, cp); |
568850ea CP |
401 | es.call_path_id = cp->db_id; |
402 | } | |
0a3eba3a CP |
403 | } |
404 | ||
0db15b1e AH |
405 | if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && |
406 | sample_addr_correlates_sym(&evsel->attr)) { | |
407 | struct addr_location addr_al; | |
408 | ||
c2740a87 | 409 | thread__resolve(thread, &addr_al, sample); |
0db15b1e AH |
410 | err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id, |
411 | &es.addr_sym_db_id, &es.addr_offset); | |
412 | if (err) | |
427cde32 | 413 | goto out_put; |
88f50d60 AH |
414 | if (dbe->crp) { |
415 | err = thread_stack__process(thread, comm, sample, al, | |
416 | &addr_al, es.db_id, | |
417 | dbe->crp); | |
418 | if (err) | |
427cde32 | 419 | goto out_put; |
88f50d60 | 420 | } |
0db15b1e AH |
421 | } |
422 | ||
423 | if (dbe->export_sample) | |
427cde32 | 424 | err = dbe->export_sample(dbe, &es); |
0db15b1e | 425 | |
427cde32 AH |
426 | out_put: |
427 | thread__put(main_thread); | |
428 | return err; | |
0db15b1e | 429 | } |
f2bff007 AH |
430 | |
431 | static struct { | |
432 | u32 branch_type; | |
433 | const char *name; | |
434 | } branch_types[] = { | |
435 | {0, "no branch"}, | |
436 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"}, | |
437 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"}, | |
438 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"}, | |
439 | {PERF_IP_FLAG_BRANCH, "unconditional jump"}, | |
440 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, | |
441 | "software interrupt"}, | |
442 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, | |
443 | "return from interrupt"}, | |
444 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, | |
445 | "system call"}, | |
446 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, | |
447 | "return from system call"}, | |
448 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"}, | |
449 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | | |
450 | PERF_IP_FLAG_INTERRUPT, "hardware interrupt"}, | |
451 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"}, | |
452 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"}, | |
453 | {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"}, | |
454 | {0, NULL} | |
455 | }; | |
456 | ||
457 | int db_export__branch_types(struct db_export *dbe) | |
458 | { | |
459 | int i, err = 0; | |
460 | ||
461 | for (i = 0; branch_types[i].name ; i++) { | |
462 | err = db_export__branch_type(dbe, branch_types[i].branch_type, | |
463 | branch_types[i].name); | |
464 | if (err) | |
465 | break; | |
466 | } | |
ff645daf AH |
467 | |
468 | /* Add trace begin / end variants */ | |
469 | for (i = 0; branch_types[i].name ; i++) { | |
470 | const char *name = branch_types[i].name; | |
471 | u32 type = branch_types[i].branch_type; | |
472 | char buf[64]; | |
473 | ||
474 | if (type == PERF_IP_FLAG_BRANCH || | |
475 | (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END))) | |
476 | continue; | |
477 | ||
478 | snprintf(buf, sizeof(buf), "trace begin / %s", name); | |
479 | err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf); | |
480 | if (err) | |
481 | break; | |
482 | ||
483 | snprintf(buf, sizeof(buf), "%s / trace end", name); | |
484 | err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf); | |
485 | if (err) | |
486 | break; | |
487 | } | |
488 | ||
f2bff007 AH |
489 | return err; |
490 | } | |
88f50d60 AH |
491 | |
492 | int db_export__call_path(struct db_export *dbe, struct call_path *cp) | |
493 | { | |
494 | int err; | |
495 | ||
496 | if (cp->db_id) | |
497 | return 0; | |
498 | ||
499 | if (cp->parent) { | |
500 | err = db_export__call_path(dbe, cp->parent); | |
501 | if (err) | |
502 | return err; | |
503 | } | |
504 | ||
505 | cp->db_id = ++dbe->call_path_last_db_id; | |
506 | ||
507 | if (dbe->export_call_path) | |
508 | return dbe->export_call_path(dbe, cp); | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
f435887e AH |
513 | int db_export__call_return(struct db_export *dbe, struct call_return *cr, |
514 | u64 *parent_db_id) | |
88f50d60 AH |
515 | { |
516 | int err; | |
517 | ||
88f50d60 AH |
518 | err = db_export__call_path(dbe, cr->cp); |
519 | if (err) | |
520 | return err; | |
521 | ||
f435887e AH |
522 | if (!cr->db_id) |
523 | cr->db_id = ++dbe->call_return_last_db_id; | |
524 | ||
525 | if (parent_db_id) { | |
526 | if (!*parent_db_id) | |
527 | *parent_db_id = ++dbe->call_return_last_db_id; | |
528 | cr->parent_db_id = *parent_db_id; | |
529 | } | |
88f50d60 AH |
530 | |
531 | if (dbe->export_call_return) | |
532 | return dbe->export_call_return(dbe, cr); | |
533 | ||
534 | return 0; | |
535 | } |