b0504d3eb13036f108b95ab6a7c010b28be9c2cc
[linux-2.6-microblaze.git] / tools / perf / util / db-export.c
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 "evsel.h"
11 #include "machine.h"
12 #include "thread.h"
13 #include "comm.h"
14 #include "symbol.h"
15 #include "map.h"
16 #include "event.h"
17 #include "thread-stack.h"
18 #include "callchain.h"
19 #include "call-path.h"
20 #include "db-export.h"
21 #include <linux/zalloc.h>
22
23 int db_export__init(struct db_export *dbe)
24 {
25         memset(dbe, 0, sizeof(struct db_export));
26         return 0;
27 }
28
29 void db_export__exit(struct db_export *dbe)
30 {
31         call_return_processor__free(dbe->crp);
32         dbe->crp = NULL;
33 }
34
35 int db_export__evsel(struct db_export *dbe, struct perf_evsel *evsel)
36 {
37         if (evsel->db_id)
38                 return 0;
39
40         evsel->db_id = ++dbe->evsel_last_db_id;
41
42         if (dbe->export_evsel)
43                 return dbe->export_evsel(dbe, evsel);
44
45         return 0;
46 }
47
48 int db_export__machine(struct db_export *dbe, struct machine *machine)
49 {
50         if (machine->db_id)
51                 return 0;
52
53         machine->db_id = ++dbe->machine_last_db_id;
54
55         if (dbe->export_machine)
56                 return dbe->export_machine(dbe, machine);
57
58         return 0;
59 }
60
61 int db_export__thread(struct db_export *dbe, struct thread *thread,
62                       struct machine *machine, struct thread *main_thread)
63 {
64         u64 main_thread_db_id = 0;
65
66         if (thread->db_id)
67                 return 0;
68
69         thread->db_id = ++dbe->thread_last_db_id;
70
71         if (main_thread)
72                 main_thread_db_id = main_thread->db_id;
73
74         if (dbe->export_thread)
75                 return dbe->export_thread(dbe, thread, main_thread_db_id,
76                                           machine);
77
78         return 0;
79 }
80
81 /*
82  * Export the "exec" comm. The "exec" comm is the program / application command
83  * name at the time it first executes. It is used to group threads for the same
84  * program. Note that the main thread pid (or thread group id tgid) cannot be
85  * used because it does not change when a new program is exec'ed.
86  */
87 int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
88                          struct thread *main_thread)
89 {
90         int err;
91
92         if (comm->db_id)
93                 return 0;
94
95         comm->db_id = ++dbe->comm_last_db_id;
96
97         if (dbe->export_comm) {
98                 err = dbe->export_comm(dbe, comm, main_thread);
99                 if (err)
100                         return err;
101         }
102
103         /*
104          * Record the main thread for this comm. Note that the main thread can
105          * have many "exec" comms because there will be a new one every time it
106          * exec's. An "exec" comm however will only ever have 1 main thread.
107          * That is different to any other threads for that same program because
108          * exec() will effectively kill them, so the relationship between the
109          * "exec" comm and non-main threads is 1-to-1. That is why
110          * db_export__comm_thread() is called here for the main thread, but it
111          * is called for non-main threads when they are exported.
112          */
113         return db_export__comm_thread(dbe, comm, main_thread);
114 }
115
116 int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
117                            struct thread *thread)
118 {
119         u64 db_id;
120
121         db_id = ++dbe->comm_thread_last_db_id;
122
123         if (dbe->export_comm_thread)
124                 return dbe->export_comm_thread(dbe, db_id, comm, thread);
125
126         return 0;
127 }
128
129 int db_export__dso(struct db_export *dbe, struct dso *dso,
130                    struct machine *machine)
131 {
132         if (dso->db_id)
133                 return 0;
134
135         dso->db_id = ++dbe->dso_last_db_id;
136
137         if (dbe->export_dso)
138                 return dbe->export_dso(dbe, dso, machine);
139
140         return 0;
141 }
142
143 int db_export__symbol(struct db_export *dbe, struct symbol *sym,
144                       struct dso *dso)
145 {
146         u64 *sym_db_id = symbol__priv(sym);
147
148         if (*sym_db_id)
149                 return 0;
150
151         *sym_db_id = ++dbe->symbol_last_db_id;
152
153         if (dbe->export_symbol)
154                 return dbe->export_symbol(dbe, sym, dso);
155
156         return 0;
157 }
158
159 static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
160                           u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
161 {
162         int err;
163
164         if (al->map) {
165                 struct dso *dso = al->map->dso;
166
167                 err = db_export__dso(dbe, dso, al->machine);
168                 if (err)
169                         return err;
170                 *dso_db_id = dso->db_id;
171
172                 if (!al->sym) {
173                         al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
174                         if (al->sym)
175                                 dso__insert_symbol(dso, al->sym);
176                 }
177
178                 if (al->sym) {
179                         u64 *db_id = symbol__priv(al->sym);
180
181                         err = db_export__symbol(dbe, al->sym, dso);
182                         if (err)
183                                 return err;
184                         *sym_db_id = *db_id;
185                         *offset = al->addr - al->sym->start;
186                 }
187         }
188
189         return 0;
190 }
191
192 static struct call_path *call_path_from_sample(struct db_export *dbe,
193                                                struct machine *machine,
194                                                struct thread *thread,
195                                                struct perf_sample *sample,
196                                                struct perf_evsel *evsel)
197 {
198         u64 kernel_start = machine__kernel_start(machine);
199         struct call_path *current = &dbe->cpr->call_path;
200         enum chain_order saved_order = callchain_param.order;
201         int err;
202
203         if (!symbol_conf.use_callchain || !sample->callchain)
204                 return NULL;
205
206         /*
207          * Since the call path tree must be built starting with the root, we
208          * must use ORDER_CALL for call chain resolution, in order to process
209          * the callchain starting with the root node and ending with the leaf.
210          */
211         callchain_param.order = ORDER_CALLER;
212         err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
213                                         sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
214         if (err) {
215                 callchain_param.order = saved_order;
216                 return NULL;
217         }
218         callchain_cursor_commit(&callchain_cursor);
219
220         while (1) {
221                 struct callchain_cursor_node *node;
222                 struct addr_location al;
223                 u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
224
225                 memset(&al, 0, sizeof(al));
226
227                 node = callchain_cursor_current(&callchain_cursor);
228                 if (!node)
229                         break;
230                 /*
231                  * Handle export of symbol and dso for this node by
232                  * constructing an addr_location struct and then passing it to
233                  * db_ids_from_al() to perform the export.
234                  */
235                 al.sym = node->sym;
236                 al.map = node->map;
237                 al.machine = machine;
238                 al.addr = node->ip;
239
240                 if (al.map && !al.sym)
241                         al.sym = dso__find_symbol(al.map->dso, al.addr);
242
243                 db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
244
245                 /* add node to the call path tree if it doesn't exist */
246                 current = call_path__findnew(dbe->cpr, current,
247                                              al.sym, node->ip,
248                                              kernel_start);
249
250                 callchain_cursor_advance(&callchain_cursor);
251         }
252
253         /* Reset the callchain order to its prior value. */
254         callchain_param.order = saved_order;
255
256         if (current == &dbe->cpr->call_path) {
257                 /* Bail because the callchain was empty. */
258                 return NULL;
259         }
260
261         return current;
262 }
263
264 int db_export__branch_type(struct db_export *dbe, u32 branch_type,
265                            const char *name)
266 {
267         if (dbe->export_branch_type)
268                 return dbe->export_branch_type(dbe, branch_type, name);
269
270         return 0;
271 }
272
273 int db_export__sample(struct db_export *dbe, union perf_event *event,
274                       struct perf_sample *sample, struct perf_evsel *evsel,
275                       struct addr_location *al)
276 {
277         struct thread *thread = al->thread;
278         struct export_sample es = {
279                 .event = event,
280                 .sample = sample,
281                 .evsel = evsel,
282                 .al = al,
283         };
284         struct thread *main_thread;
285         struct comm *comm = NULL;
286         int err;
287
288         err = db_export__evsel(dbe, evsel);
289         if (err)
290                 return err;
291
292         err = db_export__machine(dbe, al->machine);
293         if (err)
294                 return err;
295
296         main_thread = thread__main_thread(al->machine, thread);
297         if (main_thread) {
298                 /*
299                  * A thread has a reference to the main thread, so export the
300                  * main thread first.
301                  */
302                 err = db_export__thread(dbe, main_thread, al->machine,
303                                         main_thread);
304                 if (err)
305                         goto out_put;
306                 /*
307                  * Export comm before exporting the non-main thread because
308                  * db_export__comm_thread() can be called further below.
309                  */
310                 comm = machine__thread_exec_comm(al->machine, main_thread);
311                 if (comm) {
312                         err = db_export__exec_comm(dbe, comm, main_thread);
313                         if (err)
314                                 goto out_put;
315                         es.comm_db_id = comm->db_id;
316                 }
317         }
318
319         if (thread != main_thread) {
320                 /*
321                  * For a non-main thread, db_export__comm_thread() must be
322                  * called only if thread has not previously been exported.
323                  */
324                 bool export_comm_thread = comm && !thread->db_id;
325
326                 err = db_export__thread(dbe, thread, al->machine, main_thread);
327                 if (err)
328                         goto out_put;
329
330                 if (export_comm_thread) {
331                         err = db_export__comm_thread(dbe, comm, thread);
332                         if (err)
333                                 goto out_put;
334                 }
335         }
336
337         es.db_id = ++dbe->sample_last_db_id;
338
339         err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
340         if (err)
341                 goto out_put;
342
343         if (dbe->cpr) {
344                 struct call_path *cp = call_path_from_sample(dbe, al->machine,
345                                                              thread, sample,
346                                                              evsel);
347                 if (cp) {
348                         db_export__call_path(dbe, cp);
349                         es.call_path_id = cp->db_id;
350                 }
351         }
352
353         if ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
354             sample_addr_correlates_sym(&evsel->attr)) {
355                 struct addr_location addr_al;
356
357                 thread__resolve(thread, &addr_al, sample);
358                 err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
359                                      &es.addr_sym_db_id, &es.addr_offset);
360                 if (err)
361                         goto out_put;
362                 if (dbe->crp) {
363                         err = thread_stack__process(thread, comm, sample, al,
364                                                     &addr_al, es.db_id,
365                                                     dbe->crp);
366                         if (err)
367                                 goto out_put;
368                 }
369         }
370
371         if (dbe->export_sample)
372                 err = dbe->export_sample(dbe, &es);
373
374 out_put:
375         thread__put(main_thread);
376         return err;
377 }
378
379 static struct {
380         u32 branch_type;
381         const char *name;
382 } branch_types[] = {
383         {0, "no branch"},
384         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
385         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
386         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
387         {PERF_IP_FLAG_BRANCH, "unconditional jump"},
388         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
389          "software interrupt"},
390         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
391          "return from interrupt"},
392         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
393          "system call"},
394         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
395          "return from system call"},
396         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
397         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
398          PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
399         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
400         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
401         {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
402         {0, NULL}
403 };
404
405 int db_export__branch_types(struct db_export *dbe)
406 {
407         int i, err = 0;
408
409         for (i = 0; branch_types[i].name ; i++) {
410                 err = db_export__branch_type(dbe, branch_types[i].branch_type,
411                                              branch_types[i].name);
412                 if (err)
413                         break;
414         }
415
416         /* Add trace begin / end variants */
417         for (i = 0; branch_types[i].name ; i++) {
418                 const char *name = branch_types[i].name;
419                 u32 type = branch_types[i].branch_type;
420                 char buf[64];
421
422                 if (type == PERF_IP_FLAG_BRANCH ||
423                     (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
424                         continue;
425
426                 snprintf(buf, sizeof(buf), "trace begin / %s", name);
427                 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
428                 if (err)
429                         break;
430
431                 snprintf(buf, sizeof(buf), "%s / trace end", name);
432                 err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
433                 if (err)
434                         break;
435         }
436
437         return err;
438 }
439
440 int db_export__call_path(struct db_export *dbe, struct call_path *cp)
441 {
442         int err;
443
444         if (cp->db_id)
445                 return 0;
446
447         if (cp->parent) {
448                 err = db_export__call_path(dbe, cp->parent);
449                 if (err)
450                         return err;
451         }
452
453         cp->db_id = ++dbe->call_path_last_db_id;
454
455         if (dbe->export_call_path)
456                 return dbe->export_call_path(dbe, cp);
457
458         return 0;
459 }
460
461 int db_export__call_return(struct db_export *dbe, struct call_return *cr,
462                            u64 *parent_db_id)
463 {
464         int err;
465
466         err = db_export__call_path(dbe, cr->cp);
467         if (err)
468                 return err;
469
470         if (!cr->db_id)
471                 cr->db_id = ++dbe->call_return_last_db_id;
472
473         if (parent_db_id) {
474                 if (!*parent_db_id)
475                         *parent_db_id = ++dbe->call_return_last_db_id;
476                 cr->parent_db_id = *parent_db_id;
477         }
478
479         if (dbe->export_call_return)
480                 return dbe->export_call_return(dbe, cr);
481
482         return 0;
483 }