xref: /OK3568_Linux_fs/kernel/tools/lib/traceevent/plugins/plugin_function.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: LGPL-2.1
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #include <stdio.h>
6*4882a593Smuzhiyun #include <stdlib.h>
7*4882a593Smuzhiyun #include <string.h>
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include "event-parse.h"
10*4882a593Smuzhiyun #include "event-utils.h"
11*4882a593Smuzhiyun #include "trace-seq.h"
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun static struct func_stack {
14*4882a593Smuzhiyun 	int size;
15*4882a593Smuzhiyun 	char **stack;
16*4882a593Smuzhiyun } *fstack;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun static int cpus = -1;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define STK_BLK 10
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct tep_plugin_option plugin_options[] =
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	{
25*4882a593Smuzhiyun 		.name = "parent",
26*4882a593Smuzhiyun 		.plugin_alias = "ftrace",
27*4882a593Smuzhiyun 		.description =
28*4882a593Smuzhiyun 		"Print parent of functions for function events",
29*4882a593Smuzhiyun 	},
30*4882a593Smuzhiyun 	{
31*4882a593Smuzhiyun 		.name = "indent",
32*4882a593Smuzhiyun 		.plugin_alias = "ftrace",
33*4882a593Smuzhiyun 		.description =
34*4882a593Smuzhiyun 		"Try to show function call indents, based on parents",
35*4882a593Smuzhiyun 		.set = 1,
36*4882a593Smuzhiyun 	},
37*4882a593Smuzhiyun 	{
38*4882a593Smuzhiyun 		.name = "offset",
39*4882a593Smuzhiyun 		.plugin_alias = "ftrace",
40*4882a593Smuzhiyun 		.description =
41*4882a593Smuzhiyun 		"Show function names as well as their offsets",
42*4882a593Smuzhiyun 		.set = 0,
43*4882a593Smuzhiyun 	},
44*4882a593Smuzhiyun 	{
45*4882a593Smuzhiyun 		.name = NULL,
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static struct tep_plugin_option *ftrace_parent = &plugin_options[0];
50*4882a593Smuzhiyun static struct tep_plugin_option *ftrace_indent = &plugin_options[1];
51*4882a593Smuzhiyun static struct tep_plugin_option *ftrace_offset = &plugin_options[2];
52*4882a593Smuzhiyun 
add_child(struct func_stack * stack,const char * child,int pos)53*4882a593Smuzhiyun static void add_child(struct func_stack *stack, const char *child, int pos)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	int i;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (!child)
58*4882a593Smuzhiyun 		return;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	if (pos < stack->size)
61*4882a593Smuzhiyun 		free(stack->stack[pos]);
62*4882a593Smuzhiyun 	else {
63*4882a593Smuzhiyun 		char **ptr;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 		ptr = realloc(stack->stack, sizeof(char *) *
66*4882a593Smuzhiyun 			      (stack->size + STK_BLK));
67*4882a593Smuzhiyun 		if (!ptr) {
68*4882a593Smuzhiyun 			warning("could not allocate plugin memory\n");
69*4882a593Smuzhiyun 			return;
70*4882a593Smuzhiyun 		}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 		stack->stack = ptr;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		for (i = stack->size; i < stack->size + STK_BLK; i++)
75*4882a593Smuzhiyun 			stack->stack[i] = NULL;
76*4882a593Smuzhiyun 		stack->size += STK_BLK;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	stack->stack[pos] = strdup(child);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
add_and_get_index(const char * parent,const char * child,int cpu)82*4882a593Smuzhiyun static int add_and_get_index(const char *parent, const char *child, int cpu)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	int i;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	if (cpu < 0)
87*4882a593Smuzhiyun 		return 0;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (cpu > cpus) {
90*4882a593Smuzhiyun 		struct func_stack *ptr;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 		ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1));
93*4882a593Smuzhiyun 		if (!ptr) {
94*4882a593Smuzhiyun 			warning("could not allocate plugin memory\n");
95*4882a593Smuzhiyun 			return 0;
96*4882a593Smuzhiyun 		}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		fstack = ptr;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		/* Account for holes in the cpu count */
101*4882a593Smuzhiyun 		for (i = cpus + 1; i <= cpu; i++)
102*4882a593Smuzhiyun 			memset(&fstack[i], 0, sizeof(fstack[i]));
103*4882a593Smuzhiyun 		cpus = cpu;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) {
107*4882a593Smuzhiyun 		if (strcmp(parent, fstack[cpu].stack[i]) == 0) {
108*4882a593Smuzhiyun 			add_child(&fstack[cpu], child, i+1);
109*4882a593Smuzhiyun 			return i;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Not found */
114*4882a593Smuzhiyun 	add_child(&fstack[cpu], parent, 0);
115*4882a593Smuzhiyun 	add_child(&fstack[cpu], child, 1);
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
show_function(struct trace_seq * s,struct tep_handle * tep,const char * func,unsigned long long function)119*4882a593Smuzhiyun static void show_function(struct trace_seq *s, struct tep_handle *tep,
120*4882a593Smuzhiyun 			  const char *func, unsigned long long function)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	unsigned long long offset;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	trace_seq_printf(s, "%s", func);
125*4882a593Smuzhiyun 	if (ftrace_offset->set) {
126*4882a593Smuzhiyun 		offset = tep_find_function_address(tep, function);
127*4882a593Smuzhiyun 		trace_seq_printf(s, "+0x%x ", (int)(function - offset));
128*4882a593Smuzhiyun 	}
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
function_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * context)131*4882a593Smuzhiyun static int function_handler(struct trace_seq *s, struct tep_record *record,
132*4882a593Smuzhiyun 			    struct tep_event *event, void *context)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct tep_handle *tep = event->tep;
135*4882a593Smuzhiyun 	unsigned long long function;
136*4882a593Smuzhiyun 	unsigned long long pfunction;
137*4882a593Smuzhiyun 	const char *func;
138*4882a593Smuzhiyun 	const char *parent;
139*4882a593Smuzhiyun 	int index = 0;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	if (tep_get_field_val(s, event, "ip", record, &function, 1))
142*4882a593Smuzhiyun 		return trace_seq_putc(s, '!');
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	func = tep_find_function(tep, function);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	if (tep_get_field_val(s, event, "parent_ip", record, &pfunction, 1))
147*4882a593Smuzhiyun 		return trace_seq_putc(s, '!');
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	parent = tep_find_function(tep, pfunction);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	if (parent && ftrace_indent->set)
152*4882a593Smuzhiyun 		index = add_and_get_index(parent, func, record->cpu);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	trace_seq_printf(s, "%*s", index*3, "");
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (func)
157*4882a593Smuzhiyun 		show_function(s, tep, func, function);
158*4882a593Smuzhiyun 	else
159*4882a593Smuzhiyun 		trace_seq_printf(s, "0x%llx", function);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	if (ftrace_parent->set) {
162*4882a593Smuzhiyun 		trace_seq_printf(s, " <-- ");
163*4882a593Smuzhiyun 		if (parent)
164*4882a593Smuzhiyun 			show_function(s, tep, parent, pfunction);
165*4882a593Smuzhiyun 		else
166*4882a593Smuzhiyun 			trace_seq_printf(s, "0x%llx", pfunction);
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun static int
trace_stack_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * context)173*4882a593Smuzhiyun trace_stack_handler(struct trace_seq *s, struct tep_record *record,
174*4882a593Smuzhiyun 		    struct tep_event *event, void *context)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct tep_format_field *field;
177*4882a593Smuzhiyun 	unsigned long long addr;
178*4882a593Smuzhiyun 	const char *func;
179*4882a593Smuzhiyun 	int long_size;
180*4882a593Smuzhiyun 	void *data = record->data;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	field = tep_find_any_field(event, "caller");
183*4882a593Smuzhiyun 	if (!field) {
184*4882a593Smuzhiyun 		trace_seq_printf(s, "<CANT FIND FIELD %s>", "caller");
185*4882a593Smuzhiyun 		return 0;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	trace_seq_puts(s, "<stack trace >\n");
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	long_size = tep_get_long_size(event->tep);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	for (data += field->offset; data < record->data + record->size;
193*4882a593Smuzhiyun 	     data += long_size) {
194*4882a593Smuzhiyun 		addr = tep_read_number(event->tep, data, long_size);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		if ((long_size == 8 && addr == (unsigned long long)-1) ||
197*4882a593Smuzhiyun 		    ((int)addr == -1))
198*4882a593Smuzhiyun 			break;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		func = tep_find_function(event->tep, addr);
201*4882a593Smuzhiyun 		if (func)
202*4882a593Smuzhiyun 			trace_seq_printf(s, "=> %s (%llx)\n", func, addr);
203*4882a593Smuzhiyun 		else
204*4882a593Smuzhiyun 			trace_seq_printf(s, "=> %llx\n", addr);
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	return 0;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun static int
trace_raw_data_handler(struct trace_seq * s,struct tep_record * record,struct tep_event * event,void * context)211*4882a593Smuzhiyun trace_raw_data_handler(struct trace_seq *s, struct tep_record *record,
212*4882a593Smuzhiyun 		    struct tep_event *event, void *context)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	struct tep_format_field *field;
215*4882a593Smuzhiyun 	unsigned long long id;
216*4882a593Smuzhiyun 	int long_size;
217*4882a593Smuzhiyun 	void *data = record->data;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	if (tep_get_field_val(s, event, "id", record, &id, 1))
220*4882a593Smuzhiyun 		return trace_seq_putc(s, '!');
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	trace_seq_printf(s, "# %llx", id);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	field = tep_find_any_field(event, "buf");
225*4882a593Smuzhiyun 	if (!field) {
226*4882a593Smuzhiyun 		trace_seq_printf(s, "<CANT FIND FIELD %s>", "buf");
227*4882a593Smuzhiyun 		return 0;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	long_size = tep_get_long_size(event->tep);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	for (data += field->offset; data < record->data + record->size;
233*4882a593Smuzhiyun 	     data += long_size) {
234*4882a593Smuzhiyun 		int size = sizeof(long);
235*4882a593Smuzhiyun 		int left = (record->data + record->size) - data;
236*4882a593Smuzhiyun 		int i;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 		if (size > left)
239*4882a593Smuzhiyun 			size = left;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 		for (i = 0; i < size; i++)
242*4882a593Smuzhiyun 			trace_seq_printf(s, " %02x", *(unsigned char *)(data + i));
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	return 0;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun 
TEP_PLUGIN_LOADER(struct tep_handle * tep)248*4882a593Smuzhiyun int TEP_PLUGIN_LOADER(struct tep_handle *tep)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	tep_register_event_handler(tep, -1, "ftrace", "function",
251*4882a593Smuzhiyun 				   function_handler, NULL);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	tep_register_event_handler(tep, -1, "ftrace", "kernel_stack",
254*4882a593Smuzhiyun 				      trace_stack_handler, NULL);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	tep_register_event_handler(tep, -1, "ftrace", "raw_data",
257*4882a593Smuzhiyun 				      trace_raw_data_handler, NULL);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	tep_plugin_add_options("ftrace", plugin_options);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	return 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
TEP_PLUGIN_UNLOADER(struct tep_handle * tep)264*4882a593Smuzhiyun void TEP_PLUGIN_UNLOADER(struct tep_handle *tep)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	int i, x;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	tep_unregister_event_handler(tep, -1, "ftrace", "function",
269*4882a593Smuzhiyun 				     function_handler, NULL);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	for (i = 0; i <= cpus; i++) {
272*4882a593Smuzhiyun 		for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++)
273*4882a593Smuzhiyun 			free(fstack[i].stack[x]);
274*4882a593Smuzhiyun 		free(fstack[i].stack);
275*4882a593Smuzhiyun 	}
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	tep_plugin_remove_options(plugin_options);
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	free(fstack);
280*4882a593Smuzhiyun 	fstack = NULL;
281*4882a593Smuzhiyun 	cpus = -1;
282*4882a593Smuzhiyun }
283