xref: /OK3568_Linux_fs/kernel/tools/perf/util/db-export.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * db-export.c: Support for exporting data suitable for import to a database
4*4882a593Smuzhiyun  * Copyright (c) 2014, Intel Corporation.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <errno.h>
8*4882a593Smuzhiyun #include <stdlib.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "dso.h"
11*4882a593Smuzhiyun #include "evsel.h"
12*4882a593Smuzhiyun #include "machine.h"
13*4882a593Smuzhiyun #include "thread.h"
14*4882a593Smuzhiyun #include "comm.h"
15*4882a593Smuzhiyun #include "symbol.h"
16*4882a593Smuzhiyun #include "map.h"
17*4882a593Smuzhiyun #include "event.h"
18*4882a593Smuzhiyun #include "thread-stack.h"
19*4882a593Smuzhiyun #include "callchain.h"
20*4882a593Smuzhiyun #include "call-path.h"
21*4882a593Smuzhiyun #include "db-export.h"
22*4882a593Smuzhiyun #include <linux/zalloc.h>
23*4882a593Smuzhiyun 
db_export__init(struct db_export * dbe)24*4882a593Smuzhiyun int db_export__init(struct db_export *dbe)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	memset(dbe, 0, sizeof(struct db_export));
27*4882a593Smuzhiyun 	return 0;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
db_export__exit(struct db_export * dbe)30*4882a593Smuzhiyun void db_export__exit(struct db_export *dbe)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	call_return_processor__free(dbe->crp);
33*4882a593Smuzhiyun 	dbe->crp = NULL;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
db_export__evsel(struct db_export * dbe,struct evsel * evsel)36*4882a593Smuzhiyun int db_export__evsel(struct db_export *dbe, struct evsel *evsel)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	if (evsel->db_id)
39*4882a593Smuzhiyun 		return 0;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	evsel->db_id = ++dbe->evsel_last_db_id;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	if (dbe->export_evsel)
44*4882a593Smuzhiyun 		return dbe->export_evsel(dbe, evsel);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	return 0;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
db_export__machine(struct db_export * dbe,struct machine * machine)49*4882a593Smuzhiyun int db_export__machine(struct db_export *dbe, struct machine *machine)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	if (machine->db_id)
52*4882a593Smuzhiyun 		return 0;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	machine->db_id = ++dbe->machine_last_db_id;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	if (dbe->export_machine)
57*4882a593Smuzhiyun 		return dbe->export_machine(dbe, machine);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
db_export__thread(struct db_export * dbe,struct thread * thread,struct machine * machine,struct thread * main_thread)62*4882a593Smuzhiyun int db_export__thread(struct db_export *dbe, struct thread *thread,
63*4882a593Smuzhiyun 		      struct machine *machine, struct thread *main_thread)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	u64 main_thread_db_id = 0;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (thread->db_id)
68*4882a593Smuzhiyun 		return 0;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	thread->db_id = ++dbe->thread_last_db_id;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	if (main_thread)
73*4882a593Smuzhiyun 		main_thread_db_id = main_thread->db_id;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	if (dbe->export_thread)
76*4882a593Smuzhiyun 		return dbe->export_thread(dbe, thread, main_thread_db_id,
77*4882a593Smuzhiyun 					  machine);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
__db_export__comm(struct db_export * dbe,struct comm * comm,struct thread * thread)82*4882a593Smuzhiyun static int __db_export__comm(struct db_export *dbe, struct comm *comm,
83*4882a593Smuzhiyun 			     struct thread *thread)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	comm->db_id = ++dbe->comm_last_db_id;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (dbe->export_comm)
88*4882a593Smuzhiyun 		return dbe->export_comm(dbe, comm, thread);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	return 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
db_export__comm(struct db_export * dbe,struct comm * comm,struct thread * thread)93*4882a593Smuzhiyun int db_export__comm(struct db_export *dbe, struct comm *comm,
94*4882a593Smuzhiyun 		    struct thread *thread)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	if (comm->db_id)
97*4882a593Smuzhiyun 		return 0;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	return __db_export__comm(dbe, comm, thread);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun  * Export the "exec" comm. The "exec" comm is the program / application command
104*4882a593Smuzhiyun  * name at the time it first executes. It is used to group threads for the same
105*4882a593Smuzhiyun  * program. Note that the main thread pid (or thread group id tgid) cannot be
106*4882a593Smuzhiyun  * used because it does not change when a new program is exec'ed.
107*4882a593Smuzhiyun  */
db_export__exec_comm(struct db_export * dbe,struct comm * comm,struct thread * main_thread)108*4882a593Smuzhiyun int db_export__exec_comm(struct db_export *dbe, struct comm *comm,
109*4882a593Smuzhiyun 			 struct thread *main_thread)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	int err;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	if (comm->db_id)
114*4882a593Smuzhiyun 		return 0;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	err = __db_export__comm(dbe, comm, main_thread);
117*4882a593Smuzhiyun 	if (err)
118*4882a593Smuzhiyun 		return err;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/*
121*4882a593Smuzhiyun 	 * Record the main thread for this comm. Note that the main thread can
122*4882a593Smuzhiyun 	 * have many "exec" comms because there will be a new one every time it
123*4882a593Smuzhiyun 	 * exec's. An "exec" comm however will only ever have 1 main thread.
124*4882a593Smuzhiyun 	 * That is different to any other threads for that same program because
125*4882a593Smuzhiyun 	 * exec() will effectively kill them, so the relationship between the
126*4882a593Smuzhiyun 	 * "exec" comm and non-main threads is 1-to-1. That is why
127*4882a593Smuzhiyun 	 * db_export__comm_thread() is called here for the main thread, but it
128*4882a593Smuzhiyun 	 * is called for non-main threads when they are exported.
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	return db_export__comm_thread(dbe, comm, main_thread);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
db_export__comm_thread(struct db_export * dbe,struct comm * comm,struct thread * thread)133*4882a593Smuzhiyun int db_export__comm_thread(struct db_export *dbe, struct comm *comm,
134*4882a593Smuzhiyun 			   struct thread *thread)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun 	u64 db_id;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	db_id = ++dbe->comm_thread_last_db_id;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (dbe->export_comm_thread)
141*4882a593Smuzhiyun 		return dbe->export_comm_thread(dbe, db_id, comm, thread);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return 0;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
db_export__dso(struct db_export * dbe,struct dso * dso,struct machine * machine)146*4882a593Smuzhiyun int db_export__dso(struct db_export *dbe, struct dso *dso,
147*4882a593Smuzhiyun 		   struct machine *machine)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	if (dso->db_id)
150*4882a593Smuzhiyun 		return 0;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	dso->db_id = ++dbe->dso_last_db_id;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (dbe->export_dso)
155*4882a593Smuzhiyun 		return dbe->export_dso(dbe, dso, machine);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
db_export__symbol(struct db_export * dbe,struct symbol * sym,struct dso * dso)160*4882a593Smuzhiyun int db_export__symbol(struct db_export *dbe, struct symbol *sym,
161*4882a593Smuzhiyun 		      struct dso *dso)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	u64 *sym_db_id = symbol__priv(sym);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (*sym_db_id)
166*4882a593Smuzhiyun 		return 0;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	*sym_db_id = ++dbe->symbol_last_db_id;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (dbe->export_symbol)
171*4882a593Smuzhiyun 		return dbe->export_symbol(dbe, sym, dso);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	return 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
db_ids_from_al(struct db_export * dbe,struct addr_location * al,u64 * dso_db_id,u64 * sym_db_id,u64 * offset)176*4882a593Smuzhiyun static int db_ids_from_al(struct db_export *dbe, struct addr_location *al,
177*4882a593Smuzhiyun 			  u64 *dso_db_id, u64 *sym_db_id, u64 *offset)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	int err;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (al->map) {
182*4882a593Smuzhiyun 		struct dso *dso = al->map->dso;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		err = db_export__dso(dbe, dso, al->maps->machine);
185*4882a593Smuzhiyun 		if (err)
186*4882a593Smuzhiyun 			return err;
187*4882a593Smuzhiyun 		*dso_db_id = dso->db_id;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 		if (!al->sym) {
190*4882a593Smuzhiyun 			al->sym = symbol__new(al->addr, 0, 0, 0, "unknown");
191*4882a593Smuzhiyun 			if (al->sym)
192*4882a593Smuzhiyun 				dso__insert_symbol(dso, al->sym);
193*4882a593Smuzhiyun 		}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 		if (al->sym) {
196*4882a593Smuzhiyun 			u64 *db_id = symbol__priv(al->sym);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 			err = db_export__symbol(dbe, al->sym, dso);
199*4882a593Smuzhiyun 			if (err)
200*4882a593Smuzhiyun 				return err;
201*4882a593Smuzhiyun 			*sym_db_id = *db_id;
202*4882a593Smuzhiyun 			*offset = al->addr - al->sym->start;
203*4882a593Smuzhiyun 		}
204*4882a593Smuzhiyun 	}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	return 0;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
call_path_from_sample(struct db_export * dbe,struct machine * machine,struct thread * thread,struct perf_sample * sample,struct evsel * evsel)209*4882a593Smuzhiyun static struct call_path *call_path_from_sample(struct db_export *dbe,
210*4882a593Smuzhiyun 					       struct machine *machine,
211*4882a593Smuzhiyun 					       struct thread *thread,
212*4882a593Smuzhiyun 					       struct perf_sample *sample,
213*4882a593Smuzhiyun 					       struct evsel *evsel)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	u64 kernel_start = machine__kernel_start(machine);
216*4882a593Smuzhiyun 	struct call_path *current = &dbe->cpr->call_path;
217*4882a593Smuzhiyun 	enum chain_order saved_order = callchain_param.order;
218*4882a593Smuzhiyun 	int err;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (!symbol_conf.use_callchain || !sample->callchain)
221*4882a593Smuzhiyun 		return NULL;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/*
224*4882a593Smuzhiyun 	 * Since the call path tree must be built starting with the root, we
225*4882a593Smuzhiyun 	 * must use ORDER_CALL for call chain resolution, in order to process
226*4882a593Smuzhiyun 	 * the callchain starting with the root node and ending with the leaf.
227*4882a593Smuzhiyun 	 */
228*4882a593Smuzhiyun 	callchain_param.order = ORDER_CALLER;
229*4882a593Smuzhiyun 	err = thread__resolve_callchain(thread, &callchain_cursor, evsel,
230*4882a593Smuzhiyun 					sample, NULL, NULL, PERF_MAX_STACK_DEPTH);
231*4882a593Smuzhiyun 	if (err) {
232*4882a593Smuzhiyun 		callchain_param.order = saved_order;
233*4882a593Smuzhiyun 		return NULL;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 	callchain_cursor_commit(&callchain_cursor);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	while (1) {
238*4882a593Smuzhiyun 		struct callchain_cursor_node *node;
239*4882a593Smuzhiyun 		struct addr_location al;
240*4882a593Smuzhiyun 		u64 dso_db_id = 0, sym_db_id = 0, offset = 0;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 		memset(&al, 0, sizeof(al));
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		node = callchain_cursor_current(&callchain_cursor);
245*4882a593Smuzhiyun 		if (!node)
246*4882a593Smuzhiyun 			break;
247*4882a593Smuzhiyun 		/*
248*4882a593Smuzhiyun 		 * Handle export of symbol and dso for this node by
249*4882a593Smuzhiyun 		 * constructing an addr_location struct and then passing it to
250*4882a593Smuzhiyun 		 * db_ids_from_al() to perform the export.
251*4882a593Smuzhiyun 		 */
252*4882a593Smuzhiyun 		al.sym = node->ms.sym;
253*4882a593Smuzhiyun 		al.map = node->ms.map;
254*4882a593Smuzhiyun 		al.maps = thread->maps;
255*4882a593Smuzhiyun 		al.addr = node->ip;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 		if (al.map && !al.sym)
258*4882a593Smuzhiyun 			al.sym = dso__find_symbol(al.map->dso, al.addr);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		db_ids_from_al(dbe, &al, &dso_db_id, &sym_db_id, &offset);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		/* add node to the call path tree if it doesn't exist */
263*4882a593Smuzhiyun 		current = call_path__findnew(dbe->cpr, current,
264*4882a593Smuzhiyun 					     al.sym, node->ip,
265*4882a593Smuzhiyun 					     kernel_start);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 		callchain_cursor_advance(&callchain_cursor);
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* Reset the callchain order to its prior value. */
271*4882a593Smuzhiyun 	callchain_param.order = saved_order;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (current == &dbe->cpr->call_path) {
274*4882a593Smuzhiyun 		/* Bail because the callchain was empty. */
275*4882a593Smuzhiyun 		return NULL;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	return current;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
db_export__branch_type(struct db_export * dbe,u32 branch_type,const char * name)281*4882a593Smuzhiyun int db_export__branch_type(struct db_export *dbe, u32 branch_type,
282*4882a593Smuzhiyun 			   const char *name)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	if (dbe->export_branch_type)
285*4882a593Smuzhiyun 		return dbe->export_branch_type(dbe, branch_type, name);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	return 0;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun 
db_export__threads(struct db_export * dbe,struct thread * thread,struct thread * main_thread,struct machine * machine,struct comm ** comm_ptr)290*4882a593Smuzhiyun static int db_export__threads(struct db_export *dbe, struct thread *thread,
291*4882a593Smuzhiyun 			      struct thread *main_thread,
292*4882a593Smuzhiyun 			      struct machine *machine, struct comm **comm_ptr)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	struct comm *comm = NULL;
295*4882a593Smuzhiyun 	struct comm *curr_comm;
296*4882a593Smuzhiyun 	int err;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (main_thread) {
299*4882a593Smuzhiyun 		/*
300*4882a593Smuzhiyun 		 * A thread has a reference to the main thread, so export the
301*4882a593Smuzhiyun 		 * main thread first.
302*4882a593Smuzhiyun 		 */
303*4882a593Smuzhiyun 		err = db_export__thread(dbe, main_thread, machine, main_thread);
304*4882a593Smuzhiyun 		if (err)
305*4882a593Smuzhiyun 			return err;
306*4882a593Smuzhiyun 		/*
307*4882a593Smuzhiyun 		 * Export comm before exporting the non-main thread because
308*4882a593Smuzhiyun 		 * db_export__comm_thread() can be called further below.
309*4882a593Smuzhiyun 		 */
310*4882a593Smuzhiyun 		comm = machine__thread_exec_comm(machine, main_thread);
311*4882a593Smuzhiyun 		if (comm) {
312*4882a593Smuzhiyun 			err = db_export__exec_comm(dbe, comm, main_thread);
313*4882a593Smuzhiyun 			if (err)
314*4882a593Smuzhiyun 				return err;
315*4882a593Smuzhiyun 			*comm_ptr = comm;
316*4882a593Smuzhiyun 		}
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	if (thread != main_thread) {
320*4882a593Smuzhiyun 		/*
321*4882a593Smuzhiyun 		 * For a non-main thread, db_export__comm_thread() must be
322*4882a593Smuzhiyun 		 * called only if thread has not previously been exported.
323*4882a593Smuzhiyun 		 */
324*4882a593Smuzhiyun 		bool export_comm_thread = comm && !thread->db_id;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		err = db_export__thread(dbe, thread, machine, main_thread);
327*4882a593Smuzhiyun 		if (err)
328*4882a593Smuzhiyun 			return err;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 		if (export_comm_thread) {
331*4882a593Smuzhiyun 			err = db_export__comm_thread(dbe, comm, thread);
332*4882a593Smuzhiyun 			if (err)
333*4882a593Smuzhiyun 				return err;
334*4882a593Smuzhiyun 		}
335*4882a593Smuzhiyun 	}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	curr_comm = thread__comm(thread);
338*4882a593Smuzhiyun 	if (curr_comm)
339*4882a593Smuzhiyun 		return db_export__comm(dbe, curr_comm, thread);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return 0;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
db_export__sample(struct db_export * dbe,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct addr_location * al)344*4882a593Smuzhiyun int db_export__sample(struct db_export *dbe, union perf_event *event,
345*4882a593Smuzhiyun 		      struct perf_sample *sample, struct evsel *evsel,
346*4882a593Smuzhiyun 		      struct addr_location *al)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct thread *thread = al->thread;
349*4882a593Smuzhiyun 	struct export_sample es = {
350*4882a593Smuzhiyun 		.event = event,
351*4882a593Smuzhiyun 		.sample = sample,
352*4882a593Smuzhiyun 		.evsel = evsel,
353*4882a593Smuzhiyun 		.al = al,
354*4882a593Smuzhiyun 	};
355*4882a593Smuzhiyun 	struct thread *main_thread;
356*4882a593Smuzhiyun 	struct comm *comm = NULL;
357*4882a593Smuzhiyun 	int err;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	err = db_export__evsel(dbe, evsel);
360*4882a593Smuzhiyun 	if (err)
361*4882a593Smuzhiyun 		return err;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	err = db_export__machine(dbe, al->maps->machine);
364*4882a593Smuzhiyun 	if (err)
365*4882a593Smuzhiyun 		return err;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	main_thread = thread__main_thread(al->maps->machine, thread);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	err = db_export__threads(dbe, thread, main_thread, al->maps->machine, &comm);
370*4882a593Smuzhiyun 	if (err)
371*4882a593Smuzhiyun 		goto out_put;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	if (comm)
374*4882a593Smuzhiyun 		es.comm_db_id = comm->db_id;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	es.db_id = ++dbe->sample_last_db_id;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	err = db_ids_from_al(dbe, al, &es.dso_db_id, &es.sym_db_id, &es.offset);
379*4882a593Smuzhiyun 	if (err)
380*4882a593Smuzhiyun 		goto out_put;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	if (dbe->cpr) {
383*4882a593Smuzhiyun 		struct call_path *cp = call_path_from_sample(dbe, al->maps->machine,
384*4882a593Smuzhiyun 							     thread, sample,
385*4882a593Smuzhiyun 							     evsel);
386*4882a593Smuzhiyun 		if (cp) {
387*4882a593Smuzhiyun 			db_export__call_path(dbe, cp);
388*4882a593Smuzhiyun 			es.call_path_id = cp->db_id;
389*4882a593Smuzhiyun 		}
390*4882a593Smuzhiyun 	}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
393*4882a593Smuzhiyun 	    sample_addr_correlates_sym(&evsel->core.attr)) {
394*4882a593Smuzhiyun 		struct addr_location addr_al;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 		thread__resolve(thread, &addr_al, sample);
397*4882a593Smuzhiyun 		err = db_ids_from_al(dbe, &addr_al, &es.addr_dso_db_id,
398*4882a593Smuzhiyun 				     &es.addr_sym_db_id, &es.addr_offset);
399*4882a593Smuzhiyun 		if (err)
400*4882a593Smuzhiyun 			goto out_put;
401*4882a593Smuzhiyun 		if (dbe->crp) {
402*4882a593Smuzhiyun 			err = thread_stack__process(thread, comm, sample, al,
403*4882a593Smuzhiyun 						    &addr_al, es.db_id,
404*4882a593Smuzhiyun 						    dbe->crp);
405*4882a593Smuzhiyun 			if (err)
406*4882a593Smuzhiyun 				goto out_put;
407*4882a593Smuzhiyun 		}
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (dbe->export_sample)
411*4882a593Smuzhiyun 		err = dbe->export_sample(dbe, &es);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun out_put:
414*4882a593Smuzhiyun 	thread__put(main_thread);
415*4882a593Smuzhiyun 	return err;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun static struct {
419*4882a593Smuzhiyun 	u32 branch_type;
420*4882a593Smuzhiyun 	const char *name;
421*4882a593Smuzhiyun } branch_types[] = {
422*4882a593Smuzhiyun 	{0, "no branch"},
423*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
424*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
425*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "conditional jump"},
426*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH, "unconditional jump"},
427*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT,
428*4882a593Smuzhiyun 	 "software interrupt"},
429*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT,
430*4882a593Smuzhiyun 	 "return from interrupt"},
431*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET,
432*4882a593Smuzhiyun 	 "system call"},
433*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET,
434*4882a593Smuzhiyun 	 "return from system call"},
435*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "asynchronous branch"},
436*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
437*4882a593Smuzhiyun 	 PERF_IP_FLAG_INTERRUPT, "hardware interrupt"},
438*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "transaction abort"},
439*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "trace begin"},
440*4882a593Smuzhiyun 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "trace end"},
441*4882a593Smuzhiyun 	{0, NULL}
442*4882a593Smuzhiyun };
443*4882a593Smuzhiyun 
db_export__branch_types(struct db_export * dbe)444*4882a593Smuzhiyun int db_export__branch_types(struct db_export *dbe)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	int i, err = 0;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	for (i = 0; branch_types[i].name ; i++) {
449*4882a593Smuzhiyun 		err = db_export__branch_type(dbe, branch_types[i].branch_type,
450*4882a593Smuzhiyun 					     branch_types[i].name);
451*4882a593Smuzhiyun 		if (err)
452*4882a593Smuzhiyun 			break;
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	/* Add trace begin / end variants */
456*4882a593Smuzhiyun 	for (i = 0; branch_types[i].name ; i++) {
457*4882a593Smuzhiyun 		const char *name = branch_types[i].name;
458*4882a593Smuzhiyun 		u32 type = branch_types[i].branch_type;
459*4882a593Smuzhiyun 		char buf[64];
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		if (type == PERF_IP_FLAG_BRANCH ||
462*4882a593Smuzhiyun 		    (type & (PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END)))
463*4882a593Smuzhiyun 			continue;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 		snprintf(buf, sizeof(buf), "trace begin / %s", name);
466*4882a593Smuzhiyun 		err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_BEGIN, buf);
467*4882a593Smuzhiyun 		if (err)
468*4882a593Smuzhiyun 			break;
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 		snprintf(buf, sizeof(buf), "%s / trace end", name);
471*4882a593Smuzhiyun 		err = db_export__branch_type(dbe, type | PERF_IP_FLAG_TRACE_END, buf);
472*4882a593Smuzhiyun 		if (err)
473*4882a593Smuzhiyun 			break;
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	return err;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
db_export__call_path(struct db_export * dbe,struct call_path * cp)479*4882a593Smuzhiyun int db_export__call_path(struct db_export *dbe, struct call_path *cp)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	int err;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	if (cp->db_id)
484*4882a593Smuzhiyun 		return 0;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (cp->parent) {
487*4882a593Smuzhiyun 		err = db_export__call_path(dbe, cp->parent);
488*4882a593Smuzhiyun 		if (err)
489*4882a593Smuzhiyun 			return err;
490*4882a593Smuzhiyun 	}
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	cp->db_id = ++dbe->call_path_last_db_id;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	if (dbe->export_call_path)
495*4882a593Smuzhiyun 		return dbe->export_call_path(dbe, cp);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	return 0;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
db_export__call_return(struct db_export * dbe,struct call_return * cr,u64 * parent_db_id)500*4882a593Smuzhiyun int db_export__call_return(struct db_export *dbe, struct call_return *cr,
501*4882a593Smuzhiyun 			   u64 *parent_db_id)
502*4882a593Smuzhiyun {
503*4882a593Smuzhiyun 	int err;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	err = db_export__call_path(dbe, cr->cp);
506*4882a593Smuzhiyun 	if (err)
507*4882a593Smuzhiyun 		return err;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	if (!cr->db_id)
510*4882a593Smuzhiyun 		cr->db_id = ++dbe->call_return_last_db_id;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	if (parent_db_id) {
513*4882a593Smuzhiyun 		if (!*parent_db_id)
514*4882a593Smuzhiyun 			*parent_db_id = ++dbe->call_return_last_db_id;
515*4882a593Smuzhiyun 		cr->parent_db_id = *parent_db_id;
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	if (dbe->export_call_return)
519*4882a593Smuzhiyun 		return dbe->export_call_return(dbe, cr);
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	return 0;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun 
db_export__pid_tid(struct db_export * dbe,struct machine * machine,pid_t pid,pid_t tid,u64 * db_id,struct comm ** comm_ptr,bool * is_idle)524*4882a593Smuzhiyun static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
525*4882a593Smuzhiyun 			      pid_t pid, pid_t tid, u64 *db_id,
526*4882a593Smuzhiyun 			      struct comm **comm_ptr, bool *is_idle)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	struct thread *thread = machine__find_thread(machine, pid, tid);
529*4882a593Smuzhiyun 	struct thread *main_thread;
530*4882a593Smuzhiyun 	int err = 0;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	if (!thread || !thread->comm_set)
533*4882a593Smuzhiyun 		goto out_put;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	*is_idle = !thread->pid_ && !thread->tid;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	main_thread = thread__main_thread(machine, thread);
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	*db_id = thread->db_id;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	thread__put(main_thread);
544*4882a593Smuzhiyun out_put:
545*4882a593Smuzhiyun 	thread__put(thread);
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	return err;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun 
db_export__switch(struct db_export * dbe,union perf_event * event,struct perf_sample * sample,struct machine * machine)550*4882a593Smuzhiyun int db_export__switch(struct db_export *dbe, union perf_event *event,
551*4882a593Smuzhiyun 		      struct perf_sample *sample, struct machine *machine)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun 	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
554*4882a593Smuzhiyun 	bool out_preempt = out &&
555*4882a593Smuzhiyun 		(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
556*4882a593Smuzhiyun 	int flags = out | (out_preempt << 1);
557*4882a593Smuzhiyun 	bool is_idle_a = false, is_idle_b = false;
558*4882a593Smuzhiyun 	u64 th_a_id = 0, th_b_id = 0;
559*4882a593Smuzhiyun 	u64 comm_out_id, comm_in_id;
560*4882a593Smuzhiyun 	struct comm *comm_a = NULL;
561*4882a593Smuzhiyun 	struct comm *comm_b = NULL;
562*4882a593Smuzhiyun 	u64 th_out_id, th_in_id;
563*4882a593Smuzhiyun 	u64 db_id;
564*4882a593Smuzhiyun 	int err;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	err = db_export__machine(dbe, machine);
567*4882a593Smuzhiyun 	if (err)
568*4882a593Smuzhiyun 		return err;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
571*4882a593Smuzhiyun 				 &th_a_id, &comm_a, &is_idle_a);
572*4882a593Smuzhiyun 	if (err)
573*4882a593Smuzhiyun 		return err;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
576*4882a593Smuzhiyun 		pid_t pid = event->context_switch.next_prev_pid;
577*4882a593Smuzhiyun 		pid_t tid = event->context_switch.next_prev_tid;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 		err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
580*4882a593Smuzhiyun 					 &comm_b, &is_idle_b);
581*4882a593Smuzhiyun 		if (err)
582*4882a593Smuzhiyun 			return err;
583*4882a593Smuzhiyun 	}
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	/*
586*4882a593Smuzhiyun 	 * Do not export if both threads are unknown (i.e. not being traced),
587*4882a593Smuzhiyun 	 * or one is unknown and the other is the idle task.
588*4882a593Smuzhiyun 	 */
589*4882a593Smuzhiyun 	if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
590*4882a593Smuzhiyun 		return 0;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	db_id = ++dbe->context_switch_last_db_id;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	if (out) {
595*4882a593Smuzhiyun 		th_out_id   = th_a_id;
596*4882a593Smuzhiyun 		th_in_id    = th_b_id;
597*4882a593Smuzhiyun 		comm_out_id = comm_a ? comm_a->db_id : 0;
598*4882a593Smuzhiyun 		comm_in_id  = comm_b ? comm_b->db_id : 0;
599*4882a593Smuzhiyun 	} else {
600*4882a593Smuzhiyun 		th_out_id   = th_b_id;
601*4882a593Smuzhiyun 		th_in_id    = th_a_id;
602*4882a593Smuzhiyun 		comm_out_id = comm_b ? comm_b->db_id : 0;
603*4882a593Smuzhiyun 		comm_in_id  = comm_a ? comm_a->db_id : 0;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	if (dbe->export_context_switch)
607*4882a593Smuzhiyun 		return dbe->export_context_switch(dbe, db_id, machine, sample,
608*4882a593Smuzhiyun 						  th_out_id, comm_out_id,
609*4882a593Smuzhiyun 						  th_in_id, comm_in_id, flags);
610*4882a593Smuzhiyun 	return 0;
611*4882a593Smuzhiyun }
612