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" | |
23 | #include "event.h" | |
758008b2 | 24 | #include "util.h" |
88f50d60 | 25 | #include "thread-stack.h" |
451db126 | 26 | #include "call-path.h" |
0db15b1e AH |
27 | #include "db-export.h" |
28 | ||
758008b2 AH |
29 | struct deferred_export { |
30 | struct list_head node; | |
31 | struct comm *comm; | |
32 | }; | |
33 | ||
34 | static 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 | ||
52 | static 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 | ||
64 | static 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 | ||
0db15b1e AH |
78 | int db_export__init(struct db_export *dbe) |
79 | { | |
80 | memset(dbe, 0, sizeof(struct db_export)); | |
758008b2 | 81 | INIT_LIST_HEAD(&dbe->deferred); |
0db15b1e AH |
82 | return 0; |
83 | } | |
84 | ||
758008b2 AH |
85 | int db_export__flush(struct db_export *dbe) |
86 | { | |
87 | return db_export__deferred(dbe); | |
88 | } | |
89 | ||
88f50d60 | 90 | void db_export__exit(struct db_export *dbe) |
0db15b1e | 91 | { |
758008b2 | 92 | db_export__free_deferred(dbe); |
88f50d60 AH |
93 | call_return_processor__free(dbe->crp); |
94 | dbe->crp = NULL; | |
0db15b1e AH |
95 | } |
96 | ||
97 | int 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 | ||
110 | int 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 | ||
123 | int db_export__thread(struct db_export *dbe, struct thread *thread, | |
124 | struct machine *machine, struct comm *comm) | |
125 | { | |
b91fc39f | 126 | struct thread *main_thread; |
0db15b1e AH |
127 | 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) { | |
0db15b1e AH |
136 | 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) | |
b91fc39f | 147 | goto out_put; |
0db15b1e AH |
148 | if (comm) { |
149 | err = db_export__comm_thread(dbe, comm, thread); | |
150 | if (err) | |
b91fc39f | 151 | goto out_put; |
0db15b1e AH |
152 | } |
153 | } | |
154 | main_thread_db_id = main_thread->db_id; | |
b91fc39f ACM |
155 | if (main_thread != thread) |
156 | thread__put(main_thread); | |
0db15b1e AH |
157 | } |
158 | ||
159 | if (dbe->export_thread) | |
160 | return dbe->export_thread(dbe, thread, main_thread_db_id, | |
161 | machine); | |
162 | ||
163 | return 0; | |
b91fc39f ACM |
164 | |
165 | out_put: | |
166 | thread__put(main_thread); | |
167 | return err; | |
0db15b1e AH |
168 | } |
169 | ||
170 | int 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) { | |
758008b2 AH |
181 | if (main_thread->comm_set) |
182 | err = dbe->export_comm(dbe, comm); | |
183 | else | |
184 | err = db_export__defer_comm(dbe, comm); | |
0db15b1e AH |
185 | if (err) |
186 | return err; | |
187 | } | |
188 | ||
189 | return db_export__comm_thread(dbe, comm, main_thread); | |
190 | } | |
191 | ||
192 | int 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 | ||
205 | int 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 | ||
219 | int 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 | ||
235 | static struct thread *get_main_thread(struct machine *machine, struct thread *thread) | |
236 | { | |
237 | if (thread->pid_ == thread->tid) | |
427cde32 | 238 | return thread__get(thread); |
0db15b1e AH |
239 | |
240 | if (thread->pid_ == -1) | |
241 | return NULL; | |
242 | ||
243 | return machine__find_thread(machine, thread->pid_, thread->pid_); | |
244 | } | |
245 | ||
246 | static 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 | ||
f2bff007 AH |
280 | int 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 | ||
0db15b1e AH |
289 | int db_export__sample(struct db_export *dbe, union perf_event *event, |
290 | struct perf_sample *sample, struct perf_evsel *evsel, | |
7327259d | 291 | struct addr_location *al) |
0db15b1e | 292 | { |
7327259d | 293 | struct thread* thread = al->thread; |
0db15b1e AH |
294 | struct export_sample es = { |
295 | .event = event, | |
296 | .sample = sample, | |
297 | .evsel = evsel, | |
0db15b1e AH |
298 | .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) | |
427cde32 | 318 | goto out_put; |
0db15b1e AH |
319 | |
320 | if (comm) { | |
321 | err = db_export__comm(dbe, comm, main_thread); | |
322 | if (err) | |
427cde32 | 323 | goto out_put; |
0db15b1e AH |
324 | 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) | |
427cde32 | 331 | goto out_put; |
0db15b1e AH |
332 | |
333 | if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) && | |
334 | sample_addr_correlates_sym(&evsel->attr)) { | |
335 | struct addr_location addr_al; | |
336 | ||
c2740a87 | 337 | thread__resolve(thread, &addr_al, sample); |
0db15b1e AH |
338 | 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) | |
427cde32 | 341 | goto out_put; |
88f50d60 AH |
342 | if (dbe->crp) { |
343 | err = thread_stack__process(thread, comm, sample, al, | |
344 | &addr_al, es.db_id, | |
345 | dbe->crp); | |
346 | if (err) | |
427cde32 | 347 | goto out_put; |
88f50d60 | 348 | } |
0db15b1e AH |
349 | } |
350 | ||
351 | if (dbe->export_sample) | |
427cde32 | 352 | err = dbe->export_sample(dbe, &es); |
0db15b1e | 353 | |
427cde32 AH |
354 | out_put: |
355 | thread__put(main_thread); | |
356 | return err; | |
0db15b1e | 357 | } |
f2bff007 AH |
358 | |
359 | static 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 | ||
385 | int 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 | } | |
88f50d60 AH |
397 | |
398 | int 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 | ||
419 | int 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 | } |