2 * db-export.c: Support for exporting data suitable for import to a database
3 * Copyright (c) 2014, Intel Corporation.
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.
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
25 #include "thread-stack.h"
26 #include "db-export.h"
28 struct deferred_export
{
29 struct list_head node
;
33 static int db_export__deferred(struct db_export
*dbe
)
35 struct deferred_export
*de
;
38 while (!list_empty(&dbe
->deferred
)) {
39 de
= list_entry(dbe
->deferred
.next
, struct deferred_export
,
41 err
= dbe
->export_comm(dbe
, de
->comm
);
51 static void db_export__free_deferred(struct db_export
*dbe
)
53 struct deferred_export
*de
;
55 while (!list_empty(&dbe
->deferred
)) {
56 de
= list_entry(dbe
->deferred
.next
, struct deferred_export
,
63 static int db_export__defer_comm(struct db_export
*dbe
, struct comm
*comm
)
65 struct deferred_export
*de
;
67 de
= zalloc(sizeof(struct deferred_export
));
72 list_add_tail(&de
->node
, &dbe
->deferred
);
77 int db_export__init(struct db_export
*dbe
)
79 memset(dbe
, 0, sizeof(struct db_export
));
80 INIT_LIST_HEAD(&dbe
->deferred
);
84 int db_export__flush(struct db_export
*dbe
)
86 return db_export__deferred(dbe
);
89 void db_export__exit(struct db_export
*dbe
)
91 db_export__free_deferred(dbe
);
92 call_return_processor__free(dbe
->crp
);
96 int db_export__evsel(struct db_export
*dbe
, struct perf_evsel
*evsel
)
101 evsel
->db_id
= ++dbe
->evsel_last_db_id
;
103 if (dbe
->export_evsel
)
104 return dbe
->export_evsel(dbe
, evsel
);
109 int db_export__machine(struct db_export
*dbe
, struct machine
*machine
)
114 machine
->db_id
= ++dbe
->machine_last_db_id
;
116 if (dbe
->export_machine
)
117 return dbe
->export_machine(dbe
, machine
);
122 int db_export__thread(struct db_export
*dbe
, struct thread
*thread
,
123 struct machine
*machine
, struct comm
*comm
)
125 struct thread
*main_thread
;
126 u64 main_thread_db_id
= 0;
132 thread
->db_id
= ++dbe
->thread_last_db_id
;
134 if (thread
->pid_
!= -1) {
135 if (thread
->pid_
== thread
->tid
) {
136 main_thread
= thread
;
138 main_thread
= machine__findnew_thread(machine
,
143 err
= db_export__thread(dbe
, main_thread
, machine
,
148 err
= db_export__comm_thread(dbe
, comm
, thread
);
153 main_thread_db_id
= main_thread
->db_id
;
154 if (main_thread
!= thread
)
155 thread__put(main_thread
);
158 if (dbe
->export_thread
)
159 return dbe
->export_thread(dbe
, thread
, main_thread_db_id
,
165 thread__put(main_thread
);
169 int db_export__comm(struct db_export
*dbe
, struct comm
*comm
,
170 struct thread
*main_thread
)
177 comm
->db_id
= ++dbe
->comm_last_db_id
;
179 if (dbe
->export_comm
) {
180 if (main_thread
->comm_set
)
181 err
= dbe
->export_comm(dbe
, comm
);
183 err
= db_export__defer_comm(dbe
, comm
);
188 return db_export__comm_thread(dbe
, comm
, main_thread
);
191 int db_export__comm_thread(struct db_export
*dbe
, struct comm
*comm
,
192 struct thread
*thread
)
196 db_id
= ++dbe
->comm_thread_last_db_id
;
198 if (dbe
->export_comm_thread
)
199 return dbe
->export_comm_thread(dbe
, db_id
, comm
, thread
);
204 int db_export__dso(struct db_export
*dbe
, struct dso
*dso
,
205 struct machine
*machine
)
210 dso
->db_id
= ++dbe
->dso_last_db_id
;
213 return dbe
->export_dso(dbe
, dso
, machine
);
218 int db_export__symbol(struct db_export
*dbe
, struct symbol
*sym
,
221 u64
*sym_db_id
= symbol__priv(sym
);
226 *sym_db_id
= ++dbe
->symbol_last_db_id
;
228 if (dbe
->export_symbol
)
229 return dbe
->export_symbol(dbe
, sym
, dso
);
234 static struct thread
*get_main_thread(struct machine
*machine
, struct thread
*thread
)
236 if (thread
->pid_
== thread
->tid
)
237 return thread__get(thread
);
239 if (thread
->pid_
== -1)
242 return machine__find_thread(machine
, thread
->pid_
, thread
->pid_
);
245 static int db_ids_from_al(struct db_export
*dbe
, struct addr_location
*al
,
246 u64
*dso_db_id
, u64
*sym_db_id
, u64
*offset
)
251 struct dso
*dso
= al
->map
->dso
;
253 err
= db_export__dso(dbe
, dso
, al
->machine
);
256 *dso_db_id
= dso
->db_id
;
259 al
->sym
= symbol__new(al
->addr
, 0, 0, "unknown");
261 symbols__insert(&dso
->symbols
[al
->map
->type
],
266 u64
*db_id
= symbol__priv(al
->sym
);
268 err
= db_export__symbol(dbe
, al
->sym
, dso
);
272 *offset
= al
->addr
- al
->sym
->start
;
279 int db_export__branch_type(struct db_export
*dbe
, u32 branch_type
,
282 if (dbe
->export_branch_type
)
283 return dbe
->export_branch_type(dbe
, branch_type
, name
);
288 int db_export__sample(struct db_export
*dbe
, union perf_event
*event
,
289 struct perf_sample
*sample
, struct perf_evsel
*evsel
,
290 struct addr_location
*al
)
292 struct thread
* thread
= al
->thread
;
293 struct export_sample es
= {
299 struct thread
*main_thread
;
300 struct comm
*comm
= NULL
;
303 err
= db_export__evsel(dbe
, evsel
);
307 err
= db_export__machine(dbe
, al
->machine
);
311 main_thread
= get_main_thread(al
->machine
, thread
);
313 comm
= machine__thread_exec_comm(al
->machine
, main_thread
);
315 err
= db_export__thread(dbe
, thread
, al
->machine
, comm
);
320 err
= db_export__comm(dbe
, comm
, main_thread
);
323 es
.comm_db_id
= comm
->db_id
;
326 es
.db_id
= ++dbe
->sample_last_db_id
;
328 err
= db_ids_from_al(dbe
, al
, &es
.dso_db_id
, &es
.sym_db_id
, &es
.offset
);
332 if ((evsel
->attr
.sample_type
& PERF_SAMPLE_ADDR
) &&
333 sample_addr_correlates_sym(&evsel
->attr
)) {
334 struct addr_location addr_al
;
336 perf_event__preprocess_sample_addr(event
, sample
, thread
, &addr_al
);
337 err
= db_ids_from_al(dbe
, &addr_al
, &es
.addr_dso_db_id
,
338 &es
.addr_sym_db_id
, &es
.addr_offset
);
342 err
= thread_stack__process(thread
, comm
, sample
, al
,
350 if (dbe
->export_sample
)
351 err
= dbe
->export_sample(dbe
, &es
);
354 thread__put(main_thread
);
363 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
, "call"},
364 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
, "return"},
365 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CONDITIONAL
, "conditional jump"},
366 {PERF_IP_FLAG_BRANCH
, "unconditional jump"},
367 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_INTERRUPT
,
368 "software interrupt"},
369 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_INTERRUPT
,
370 "return from interrupt"},
371 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_SYSCALLRET
,
373 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_RETURN
| PERF_IP_FLAG_SYSCALLRET
,
374 "return from system call"},
375 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_ASYNC
, "asynchronous branch"},
376 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_CALL
| PERF_IP_FLAG_ASYNC
|
377 PERF_IP_FLAG_INTERRUPT
, "hardware interrupt"},
378 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TX_ABORT
, "transaction abort"},
379 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_BEGIN
, "trace begin"},
380 {PERF_IP_FLAG_BRANCH
| PERF_IP_FLAG_TRACE_END
, "trace end"},
384 int db_export__branch_types(struct db_export
*dbe
)
388 for (i
= 0; branch_types
[i
].name
; i
++) {
389 err
= db_export__branch_type(dbe
, branch_types
[i
].branch_type
,
390 branch_types
[i
].name
);
397 int db_export__call_path(struct db_export
*dbe
, struct call_path
*cp
)
405 err
= db_export__call_path(dbe
, cp
->parent
);
410 cp
->db_id
= ++dbe
->call_path_last_db_id
;
412 if (dbe
->export_call_path
)
413 return dbe
->export_call_path(dbe
, cp
);
418 int db_export__call_return(struct db_export
*dbe
, struct call_return
*cr
)
425 err
= db_export__call_path(dbe
, cr
->cp
);
429 cr
->db_id
= ++dbe
->call_return_last_db_id
;
431 if (dbe
->export_call_return
)
432 return dbe
->export_call_return(dbe
, cr
);