blob: 2ef1f692c35ba8a7070b4b12f7b8187cdd310af4 [file] [log] [blame]
Adrian Hunter0db15b12014-10-23 13:45:13 +03001/*
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"
23#include "event.h"
Adrian Hunter758008b2014-10-30 16:09:48 +020024#include "util.h"
Adrian Hunter88f50d62014-10-30 16:09:46 +020025#include "thread-stack.h"
Chris Phlipot0a3eba32016-04-28 01:19:08 -070026#include "callchain.h"
Chris Phlipot451db122016-04-28 01:19:07 -070027#include "call-path.h"
Adrian Hunter0db15b12014-10-23 13:45:13 +030028#include "db-export.h"
29
Adrian Hunter758008b2014-10-30 16:09:48 +020030struct deferred_export {
31 struct list_head node;
32 struct comm *comm;
33};
34
35static int db_export__deferred(struct db_export *dbe)
36{
37 struct deferred_export *de;
38 int err;
39
40 while (!list_empty(&dbe->deferred)) {
41 de = list_entry(dbe->deferred.next, struct deferred_export,
42 node);
43 err = dbe->export_comm(dbe, de->comm);
44 list_del(&de->node);
45 free(de);
46 if (err)
47 return err;
48 }
49
50 return 0;
51}
52
53static void db_export__free_deferred(struct db_export *dbe)
54{
55 struct deferred_export *de;
56
57 while (!list_empty(&dbe->deferred)) {
58 de = list_entry(dbe->deferred.next, struct deferred_export,
59 node);
60 list_del(&de->node);
61 free(de);
62 }
63}
64
65static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
66{
67 struct deferred_export *de;
68
69 de = zalloc(sizeof(struct deferred_export));
70 if (!de)
71 return -ENOMEM;
72
73 de->comm = comm;
74 list_add_tail(&de->node, &dbe->deferred);
75
76 return 0;
77}
78
Adrian Hunter0db15b12014-10-23 13:45:13 +030079int db_export__init(struct db_export *dbe)
80{
81 memset(dbe, 0, sizeof(struct db_export));
Adrian Hunter758008b2014-10-30 16:09:48 +020082 INIT_LIST_HEAD(&dbe->deferred);
Adrian Hunter0db15b12014-10-23 13:45:13 +030083 return 0;
84}
85
Adrian Hunter758008b2014-10-30 16:09:48 +020086int db_export__flush(struct db_export *dbe)
87{
88 return db_export__deferred(dbe);
89}
90
Adrian Hunter88f50d62014-10-30 16:09:46 +020091void db_export__exit(struct db_export *dbe)
Adrian Hunter0db15b12014-10-23 13:45:13 +030092{
Adrian Hunter758008b2014-10-30 16:09:48 +020093 db_export__free_deferred(dbe);
Adrian Hunter88f50d62014-10-30 16:09:46 +020094 call_return_processor__free(dbe->crp);
95 dbe->crp = NULL;
Adrian Hunter0db15b12014-10-23 13:45:13 +030096}
97
98int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
99{
100 if (evsel->db_id)
101 return 0;
102
103 evsel->db_id = ++dbe->evsel_last_db_id;
104
105 if (dbe->export_evsel)
106 return dbe->export_evsel(dbe, evsel);
107
108 return 0;
109}
110
111int db_export__machine(struct db_export *dbe, struct machine *machine)
112{
113 if (machine->db_id)
114 return 0;
115
116 machine->db_id = ++dbe->machine_last_db_id;
117
118 if (dbe->export_machine)
119 return dbe->export_machine(dbe, machine);
120
121 return 0;
122}
123
124int db_export__thread(struct db_export *dbe, struct thread *thread,
125 struct machine *machine, struct comm *comm)
126{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300127 struct thread *main_thread;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300128 u64 main_thread_db_id = 0;
129 int err;
130
131 if (thread->db_id)
132 return 0;
133
134 thread->db_id = ++dbe->thread_last_db_id;
135
136 if (thread->pid_ != -1) {
Adrian Hunter0db15b12014-10-23 13:45:13 +0300137 if (thread->pid_ == thread->tid) {
138 main_thread = thread;
139 } else {
140 main_thread = machine__findnew_thread(machine,
141 thread->pid_,
142 thread->pid_);
143 if (!main_thread)
144 return -ENOMEM;
145 err = db_export__thread(dbe, main_thread, machine,
146 comm);
147 if (err)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300148 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300149 if (comm) {
150 err = db_export__comm_thread(dbe, comm, thread);
151 if (err)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300152 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300153 }
154 }
155 main_thread_db_id = main_thread->db_id;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300156 if (main_thread != thread)
157 thread__put(main_thread);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300158 }
159
160 if (dbe->export_thread)
161 return dbe->export_thread(dbe, thread, main_thread_db_id,
162 machine);
163
164 return 0;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300165
166out_put:
167 thread__put(main_thread);
168 return err;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300169}
170
171int db_export__comm(struct db_export *dbe, struct comm *comm,
172 struct thread *main_thread)
173{
174 int err;
175
176 if (comm->db_id)
177 return 0;
178
179 comm->db_id = ++dbe->comm_last_db_id;
180
181 if (dbe->export_comm) {
Adrian Hunter758008b2014-10-30 16:09:48 +0200182 if (main_thread->comm_set)
183 err = dbe->export_comm(dbe, comm);
184 else
185 err = db_export__defer_comm(dbe, comm);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300186 if (err)
187 return err;
188 }
189
190 return db_export__comm_thread(dbe, comm, main_thread);
191}
192
193int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
194 struct thread *thread)
195{
196 u64 db_id;
197
198 db_id = ++dbe->comm_thread_last_db_id;
199
200 if (dbe->export_comm_thread)
201 return dbe->export_comm_thread(dbe, db_id, comm, thread);
202
203 return 0;
204}
205
206int db_export__dso(struct db_export *dbe, struct dso *dso,
207 struct machine *machine)
208{
209 if (dso->db_id)
210 return 0;
211
212 dso->db_id = ++dbe->dso_last_db_id;
213
214 if (dbe->export_dso)
215 return dbe->export_dso(dbe, dso, machine);
216
217 return 0;
218}
219
220int db_export__symbol(struct db_export *dbe, struct symbol *sym,
221 struct dso *dso)
222{
223 u64 *sym_db_id = symbol__priv(sym);
224
225 if (*sym_db_id)
226 return 0;
227
228 *sym_db_id = ++dbe->symbol_last_db_id;
229
230 if (dbe->export_symbol)
231 return dbe->export_symbol(dbe, sym, dso);
232
233 return 0;
234}
235
236static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
237{
238 if (thread->pid_ == thread->tid)
Adrian Hunter427cde32015-05-29 16:33:29 +0300239 return thread__get(thread);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300240
241 if (thread->pid_ == -1)
242 return NULL;
243
244 return machine__find_thread(machine, thread->pid_, thread->pid_);
245}
246
247static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
248 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
249{
250 int err;
251
252 if (al->map) {
253 struct dso *dso = al->map->dso;
254
255 err = db_export__dso(dbe, dso, al->machine);
256 if (err)
257 return err;
258 *dso_db_id = dso->db_id;
259
260 if (!al->sym) {
261 al->sym = symbol__new(al->addr, 0, 0, "unknown");
262 if (al->sym)
Chris Phlipotbd0a51d2016-05-10 20:26:47 -0700263 dso__insert_symbol(dso, al->map->type, al->sym);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300264 }
265
266 if (al->sym) {
267 u64 *db_id = symbol__priv(al->sym);
268
269 err = db_export__symbol(dbe, al->sym, dso);
270 if (err)
271 return err;
272 *sym_db_id = *db_id;
273 *offset = al->addr - al->sym->start;
274 }
275 }
276
277 return 0;
278}
279
Chris Phlipot0a3eba32016-04-28 01:19:08 -0700280static struct call_path *call_path_from_sample(struct db_export *dbe,
281 struct machine *machine,
282 struct thread *thread,
283 struct perf_sample *sample,
284 struct perf_evsel *evsel)
285{
286 u64 kernel_start = machine__kernel_start(machine);
287 struct call_path *current = &dbe->cpr->call_path;
288 enum chain_order saved_order = callchain_param.order;
289 int err;
290
291 if (!symbol_conf.use_callchain || !sample->callchain)
292 return NULL;
293
294 /*
295 * Since the call path tree must be built starting with the root, we
296 * must use ORDER_CALL for call chain resolution, in order to process
297 * the callchain starting with the root node and ending with the leaf.
298 */
299 callchain_param.order = ORDER_CALLER;
300 err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
301 sample, NULL, NULL,
302 sysctl_perf_event_max_stack);
303 if (err) {
304 callchain_param.order = saved_order;
305 return NULL;
306 }
307 callchain_cursor_commit(&callchain_cursor);
308
309 while (1) {
310 struct callchain_cursor_node *node;
311 struct addr_location al;
312 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
313
314 memset(&al, 0, sizeof(al));
315
316 node = callchain_cursor_current(&callchain_cursor);
317 if (!node)
318 break;
319 /*
320 * Handle export of symbol and dso for this node by
321 * constructing an addr_location struct and then passing it to
322 * db_ids_from_al() to perform the export.
323 */
324 al.sym = node->sym;
325 al.map = node->map;
326 al.machine = machine;
327 if (al.map)
328 al.addr = al.map->map_ip(al.map, node->ip);
329 else
330 al.addr = node->ip;
331
332 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
333
334 /* add node to the call path tree if it doesn't exist */
335 current = call_path__findnew(dbe->cpr, current,
336 al.sym, node->ip,
337 kernel_start);
338
339 callchain_cursor_advance(&callchain_cursor);
340 }
341
342 /* Reset the callchain order to its prior value. */
343 callchain_param.order = saved_order;
344
345 if (current == &dbe->cpr->call_path) {
346 /* Bail because the callchain was empty. */
347 return NULL;
348 }
349
350 return current;
351}
352
Adrian Hunterf2bff002014-10-30 16:09:43 +0200353int db_export__branch_type(struct db_export *dbe, u32 branch_type,
354 const char *name)
355{
356 if (dbe->export_branch_type)
357 return dbe->export_branch_type(dbe, branch_type, name);
358
359 return 0;
360}
361
Adrian Hunter0db15b12014-10-23 13:45:13 +0300362int db_export__sample(struct db_export *dbe, union perf_event *event,
363 struct perf_sample *sample, struct perf_evsel *evsel,
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300364 struct addr_location *al)
Adrian Hunter0db15b12014-10-23 13:45:13 +0300365{
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300366 struct thread* thread = al->thread;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300367 struct export_sample es = {
368 .event = event,
369 .sample = sample,
370 .evsel = evsel,
Adrian Hunter0db15b12014-10-23 13:45:13 +0300371 .al = al,
372 };
373 struct thread *main_thread;
374 struct comm *comm = NULL;
375 int err;
376
377 err = db_export__evsel(dbe, evsel);
378 if (err)
379 return err;
380
381 err = db_export__machine(dbe, al->machine);
382 if (err)
383 return err;
384
385 main_thread = get_main_thread(al->machine, thread);
386 if (main_thread)
387 comm = machine__thread_exec_comm(al->machine, main_thread);
388
389 err = db_export__thread(dbe, thread, al->machine, comm);
390 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300391 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300392
393 if (comm) {
394 err = db_export__comm(dbe, comm, main_thread);
395 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300396 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300397 es.comm_db_id = comm->db_id;
398 }
399
400 es.db_id = ++dbe->sample_last_db_id;
401
402 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
403 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300404 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300405
Chris Phlipot0a3eba32016-04-28 01:19:08 -0700406 if (dbe->cpr) {
407 struct call_path *cp = call_path_from_sample(dbe, al->machine,
408 thread, sample,
409 evsel);
Chris Phlipot568850e2016-04-28 01:19:09 -0700410 if (cp) {
Chris Phlipot0a3eba32016-04-28 01:19:08 -0700411 db_export__call_path(dbe, cp);
Chris Phlipot568850e2016-04-28 01:19:09 -0700412 es.call_path_id = cp->db_id;
413 }
Chris Phlipot0a3eba32016-04-28 01:19:08 -0700414 }
415
Adrian Hunter0db15b12014-10-23 13:45:13 +0300416 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
417 sample_addr_correlates_sym(&evsel->attr)) {
418 struct addr_location addr_al;
419
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300420 thread__resolve(thread, &addr_al, sample);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300421 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
422 &es.addr_sym_db_id, &es.addr_offset);
423 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300424 goto out_put;
Adrian Hunter88f50d62014-10-30 16:09:46 +0200425 if (dbe->crp) {
426 err = thread_stack__process(thread, comm, sample, al,
427 &addr_al, es.db_id,
428 dbe->crp);
429 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300430 goto out_put;
Adrian Hunter88f50d62014-10-30 16:09:46 +0200431 }
Adrian Hunter0db15b12014-10-23 13:45:13 +0300432 }
433
434 if (dbe->export_sample)
Adrian Hunter427cde32015-05-29 16:33:29 +0300435 err = dbe->export_sample(dbe, &es);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300436
Adrian Hunter427cde32015-05-29 16:33:29 +0300437out_put:
438 thread__put(main_thread);
439 return err;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300440}
Adrian Hunterf2bff002014-10-30 16:09:43 +0200441
442static struct {
443 u32 branch_type;
444 const char *name;
445} branch_types[] = {
446 {0, "no branch"},
447 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
448 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
449 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
450 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
451 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
452 "software interrupt"},
453 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
454 "return from interrupt"},
455 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
456 "system call"},
457 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
458 "return from system call"},
459 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
460 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
461 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
462 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
463 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
464 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
465 {0, NULL}
466};
467
468int db_export__branch_types(struct db_export *dbe)
469{
470 int i, err = 0;
471
472 for (i = 0; branch_types[i].name ; i++) {
473 err = db_export__branch_type(dbe, branch_types[i].branch_type,
474 branch_types[i].name);
475 if (err)
476 break;
477 }
478 return err;
479}
Adrian Hunter88f50d62014-10-30 16:09:46 +0200480
481int db_export__call_path(struct db_export *dbe, struct call_path *cp)
482{
483 int err;
484
485 if (cp->db_id)
486 return 0;
487
488 if (cp->parent) {
489 err = db_export__call_path(dbe, cp->parent);
490 if (err)
491 return err;
492 }
493
494 cp->db_id = ++dbe->call_path_last_db_id;
495
496 if (dbe->export_call_path)
497 return dbe->export_call_path(dbe, cp);
498
499 return 0;
500}
501
502int db_export__call_return(struct db_export *dbe, struct call_return *cr)
503{
504 int err;
505
506 if (cr->db_id)
507 return 0;
508
509 err = db_export__call_path(dbe, cr->cp);
510 if (err)
511 return err;
512
513 cr->db_id = ++dbe->call_return_last_db_id;
514
515 if (dbe->export_call_return)
516 return dbe->export_call_return(dbe, cr);
517
518 return 0;
519}