blob: 4fc607c130b8d053fbacc4e9aa2659fbffaa2c26 [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 Phlipot451db122016-04-28 01:19:07 -070026#include "call-path.h"
Adrian Hunter0db15b12014-10-23 13:45:13 +030027#include "db-export.h"
28
Adrian Hunter758008b2014-10-30 16:09:48 +020029struct deferred_export {
30 struct list_head node;
31 struct comm *comm;
32};
33
34static int db_export__deferred(struct db_export *dbe)
35{
36 struct deferred_export *de;
37 int err;
38
39 while (!list_empty(&dbe->deferred)) {
40 de = list_entry(dbe->deferred.next, struct deferred_export,
41 node);
42 err = dbe->export_comm(dbe, de->comm);
43 list_del(&de->node);
44 free(de);
45 if (err)
46 return err;
47 }
48
49 return 0;
50}
51
52static void db_export__free_deferred(struct db_export *dbe)
53{
54 struct deferred_export *de;
55
56 while (!list_empty(&dbe->deferred)) {
57 de = list_entry(dbe->deferred.next, struct deferred_export,
58 node);
59 list_del(&de->node);
60 free(de);
61 }
62}
63
64static int db_export__defer_comm(struct db_export *dbe, struct comm *comm)
65{
66 struct deferred_export *de;
67
68 de = zalloc(sizeof(struct deferred_export));
69 if (!de)
70 return -ENOMEM;
71
72 de->comm = comm;
73 list_add_tail(&de->node, &dbe->deferred);
74
75 return 0;
76}
77
Adrian Hunter0db15b12014-10-23 13:45:13 +030078int db_export__init(struct db_export *dbe)
79{
80 memset(dbe, 0, sizeof(struct db_export));
Adrian Hunter758008b2014-10-30 16:09:48 +020081 INIT_LIST_HEAD(&dbe->deferred);
Adrian Hunter0db15b12014-10-23 13:45:13 +030082 return 0;
83}
84
Adrian Hunter758008b2014-10-30 16:09:48 +020085int db_export__flush(struct db_export *dbe)
86{
87 return db_export__deferred(dbe);
88}
89
Adrian Hunter88f50d62014-10-30 16:09:46 +020090void db_export__exit(struct db_export *dbe)
Adrian Hunter0db15b12014-10-23 13:45:13 +030091{
Adrian Hunter758008b2014-10-30 16:09:48 +020092 db_export__free_deferred(dbe);
Adrian Hunter88f50d62014-10-30 16:09:46 +020093 call_return_processor__free(dbe->crp);
94 dbe->crp = NULL;
Adrian Hunter0db15b12014-10-23 13:45:13 +030095}
96
97int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
98{
99 if (evsel->db_id)
100 return 0;
101
102 evsel->db_id = ++dbe->evsel_last_db_id;
103
104 if (dbe->export_evsel)
105 return dbe->export_evsel(dbe, evsel);
106
107 return 0;
108}
109
110int db_export__machine(struct db_export *dbe, struct machine *machine)
111{
112 if (machine->db_id)
113 return 0;
114
115 machine->db_id = ++dbe->machine_last_db_id;
116
117 if (dbe->export_machine)
118 return dbe->export_machine(dbe, machine);
119
120 return 0;
121}
122
123int db_export__thread(struct db_export *dbe, struct thread *thread,
124 struct machine *machine, struct comm *comm)
125{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300126 struct thread *main_thread;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300127 u64 main_thread_db_id = 0;
128 int err;
129
130 if (thread->db_id)
131 return 0;
132
133 thread->db_id = ++dbe->thread_last_db_id;
134
135 if (thread->pid_ != -1) {
Adrian Hunter0db15b12014-10-23 13:45:13 +0300136 if (thread->pid_ == thread->tid) {
137 main_thread = thread;
138 } else {
139 main_thread = machine__findnew_thread(machine,
140 thread->pid_,
141 thread->pid_);
142 if (!main_thread)
143 return -ENOMEM;
144 err = db_export__thread(dbe, main_thread, machine,
145 comm);
146 if (err)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300147 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300148 if (comm) {
149 err = db_export__comm_thread(dbe, comm, thread);
150 if (err)
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300151 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300152 }
153 }
154 main_thread_db_id = main_thread->db_id;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300155 if (main_thread != thread)
156 thread__put(main_thread);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300157 }
158
159 if (dbe->export_thread)
160 return dbe->export_thread(dbe, thread, main_thread_db_id,
161 machine);
162
163 return 0;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300164
165out_put:
166 thread__put(main_thread);
167 return err;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300168}
169
170int db_export__comm(struct db_export *dbe, struct comm *comm,
171 struct thread *main_thread)
172{
173 int err;
174
175 if (comm->db_id)
176 return 0;
177
178 comm->db_id = ++dbe->comm_last_db_id;
179
180 if (dbe->export_comm) {
Adrian Hunter758008b2014-10-30 16:09:48 +0200181 if (main_thread->comm_set)
182 err = dbe->export_comm(dbe, comm);
183 else
184 err = db_export__defer_comm(dbe, comm);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300185 if (err)
186 return err;
187 }
188
189 return db_export__comm_thread(dbe, comm, main_thread);
190}
191
192int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
193 struct thread *thread)
194{
195 u64 db_id;
196
197 db_id = ++dbe->comm_thread_last_db_id;
198
199 if (dbe->export_comm_thread)
200 return dbe->export_comm_thread(dbe, db_id, comm, thread);
201
202 return 0;
203}
204
205int db_export__dso(struct db_export *dbe, struct dso *dso,
206 struct machine *machine)
207{
208 if (dso->db_id)
209 return 0;
210
211 dso->db_id = ++dbe->dso_last_db_id;
212
213 if (dbe->export_dso)
214 return dbe->export_dso(dbe, dso, machine);
215
216 return 0;
217}
218
219int db_export__symbol(struct db_export *dbe, struct symbol *sym,
220 struct dso *dso)
221{
222 u64 *sym_db_id = symbol__priv(sym);
223
224 if (*sym_db_id)
225 return 0;
226
227 *sym_db_id = ++dbe->symbol_last_db_id;
228
229 if (dbe->export_symbol)
230 return dbe->export_symbol(dbe, sym, dso);
231
232 return 0;
233}
234
235static struct thread *get_main_thread(struct machine *machine, struct thread *thread)
236{
237 if (thread->pid_ == thread->tid)
Adrian Hunter427cde32015-05-29 16:33:29 +0300238 return thread__get(thread);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300239
240 if (thread->pid_ == -1)
241 return NULL;
242
243 return machine__find_thread(machine, thread->pid_, thread->pid_);
244}
245
246static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
247 u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
248{
249 int err;
250
251 if (al->map) {
252 struct dso *dso = al->map->dso;
253
254 err = db_export__dso(dbe, dso, al->machine);
255 if (err)
256 return err;
257 *dso_db_id = dso->db_id;
258
259 if (!al->sym) {
260 al->sym = symbol__new(al->addr, 0, 0, "unknown");
261 if (al->sym)
262 symbols__insert(&dso->symbols[al->map->type],
263 al->sym);
264 }
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
Adrian Hunterf2bff002014-10-30 16:09:43 +0200280int db_export__branch_type(struct db_export *dbe, u32 branch_type,
281 const char *name)
282{
283 if (dbe->export_branch_type)
284 return dbe->export_branch_type(dbe, branch_type, name);
285
286 return 0;
287}
288
Adrian Hunter0db15b12014-10-23 13:45:13 +0300289int db_export__sample(struct db_export *dbe, union perf_event *event,
290 struct perf_sample *sample, struct perf_evsel *evsel,
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300291 struct addr_location *al)
Adrian Hunter0db15b12014-10-23 13:45:13 +0300292{
Arnaldo Carvalho de Melo73272592015-04-02 11:08:30 -0300293 struct thread* thread = al->thread;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300294 struct export_sample es = {
295 .event = event,
296 .sample = sample,
297 .evsel = evsel,
Adrian Hunter0db15b12014-10-23 13:45:13 +0300298 .al = al,
299 };
300 struct thread *main_thread;
301 struct comm *comm = NULL;
302 int err;
303
304 err = db_export__evsel(dbe, evsel);
305 if (err)
306 return err;
307
308 err = db_export__machine(dbe, al->machine);
309 if (err)
310 return err;
311
312 main_thread = get_main_thread(al->machine, thread);
313 if (main_thread)
314 comm = machine__thread_exec_comm(al->machine, main_thread);
315
316 err = db_export__thread(dbe, thread, al->machine, comm);
317 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300318 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300319
320 if (comm) {
321 err = db_export__comm(dbe, comm, main_thread);
322 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300323 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300324 es.comm_db_id = comm->db_id;
325 }
326
327 es.db_id = ++dbe->sample_last_db_id;
328
329 err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
330 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300331 goto out_put;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300332
333 if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
334 sample_addr_correlates_sym(&evsel->attr)) {
335 struct addr_location addr_al;
336
Arnaldo Carvalho de Meloc2740a82016-03-22 18:44:46 -0300337 thread__resolve(thread, &addr_al, sample);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300338 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
339 &es.addr_sym_db_id, &es.addr_offset);
340 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300341 goto out_put;
Adrian Hunter88f50d62014-10-30 16:09:46 +0200342 if (dbe->crp) {
343 err = thread_stack__process(thread, comm, sample, al,
344 &addr_al, es.db_id,
345 dbe->crp);
346 if (err)
Adrian Hunter427cde32015-05-29 16:33:29 +0300347 goto out_put;
Adrian Hunter88f50d62014-10-30 16:09:46 +0200348 }
Adrian Hunter0db15b12014-10-23 13:45:13 +0300349 }
350
351 if (dbe->export_sample)
Adrian Hunter427cde32015-05-29 16:33:29 +0300352 err = dbe->export_sample(dbe, &es);
Adrian Hunter0db15b12014-10-23 13:45:13 +0300353
Adrian Hunter427cde32015-05-29 16:33:29 +0300354out_put:
355 thread__put(main_thread);
356 return err;
Adrian Hunter0db15b12014-10-23 13:45:13 +0300357}
Adrian Hunterf2bff002014-10-30 16:09:43 +0200358
359static struct {
360 u32 branch_type;
361 const char *name;
362} branch_types[] = {
363 {0, "no branch"},
364 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
365 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
366 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
367 {PERF_IP_FLAG_BRANCH, "unconditional jump"},
368 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
369 "software interrupt"},
370 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
371 "return from interrupt"},
372 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
373 "system call"},
374 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
375 "return from system call"},
376 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
377 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
378 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
379 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
380 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
381 {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
382 {0, NULL}
383};
384
385int db_export__branch_types(struct db_export *dbe)
386{
387 int i, err = 0;
388
389 for (i = 0; branch_types[i].name ; i++) {
390 err = db_export__branch_type(dbe, branch_types[i].branch_type,
391 branch_types[i].name);
392 if (err)
393 break;
394 }
395 return err;
396}
Adrian Hunter88f50d62014-10-30 16:09:46 +0200397
398int db_export__call_path(struct db_export *dbe, struct call_path *cp)
399{
400 int err;
401
402 if (cp->db_id)
403 return 0;
404
405 if (cp->parent) {
406 err = db_export__call_path(dbe, cp->parent);
407 if (err)
408 return err;
409 }
410
411 cp->db_id = ++dbe->call_path_last_db_id;
412
413 if (dbe->export_call_path)
414 return dbe->export_call_path(dbe, cp);
415
416 return 0;
417}
418
419int db_export__call_return(struct db_export *dbe, struct call_return *cr)
420{
421 int err;
422
423 if (cr->db_id)
424 return 0;
425
426 err = db_export__call_path(dbe, cr->cp);
427 if (err)
428 return err;
429
430 cr->db_id = ++dbe->call_return_last_db_id;
431
432 if (dbe->export_call_return)
433 return dbe->export_call_return(dbe, cr);
434
435 return 0;
436}