xref: /linux/tools/perf/util/trace-event-scripting.c (revision 00d1bfae1b9618c4986c1d175d916011ed084fa4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * trace-event-scripting.  Scripting engine common and initialization code.
4  *
5  * Copyright (C) 2009-2010 Tom Zanussi <tzanussi@gmail.com>
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #ifdef HAVE_LIBTRACEEVENT
13 #include <event-parse.h>
14 #endif
15 
16 #include "archinsn.h"
17 #include "debug.h"
18 #include "event.h"
19 #include "trace-event.h"
20 #include "evsel.h"
21 #include <linux/perf_event.h>
22 #include <linux/zalloc.h>
23 #include "util/sample.h"
24 
25 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
26 
27 struct scripting_context *scripting_context;
28 
29 struct script_spec {
30 	struct list_head	node;
31 	struct scripting_ops	*ops;
32 	char			spec[];
33 };
34 
35 static LIST_HEAD(script_specs);
36 
37 static struct script_spec *script_spec__new(const char *spec,
38 					    struct scripting_ops *ops)
39 {
40 	struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
41 
42 	if (s != NULL) {
43 		strcpy(s->spec, spec);
44 		s->ops = ops;
45 	}
46 
47 	return s;
48 }
49 
50 static void script_spec__add(struct script_spec *s)
51 {
52 	list_add_tail(&s->node, &script_specs);
53 }
54 
55 static struct script_spec *script_spec__find(const char *spec)
56 {
57 	struct script_spec *s;
58 
59 	list_for_each_entry(s, &script_specs, node)
60 		if (strcasecmp(s->spec, spec) == 0)
61 			return s;
62 	return NULL;
63 }
64 
65 static int script_spec_register(const char *spec, struct scripting_ops *ops)
66 {
67 	struct script_spec *s;
68 
69 	s = script_spec__find(spec);
70 	if (s)
71 		return -1;
72 
73 	s = script_spec__new(spec, ops);
74 	if (!s)
75 		return -1;
76 
77 	script_spec__add(s);
78 	return 0;
79 }
80 
81 struct scripting_ops *script_spec__lookup(const char *spec)
82 {
83 	struct script_spec *s = script_spec__find(spec);
84 
85 	if (!s)
86 		return NULL;
87 
88 	return s->ops;
89 }
90 
91 int script_spec__for_each(int (*cb)(struct scripting_ops *ops, const char *spec))
92 {
93 	struct script_spec *s;
94 	int ret = 0;
95 
96 	list_for_each_entry(s, &script_specs, node) {
97 		ret = cb(s->ops, s->spec);
98 		if (ret)
99 			break;
100 	}
101 	return ret;
102 }
103 
104 void scripting_context__update(struct scripting_context *c,
105 			       union perf_event *event,
106 			       struct perf_sample *sample,
107 			       struct evsel *evsel,
108 			       struct addr_location *al,
109 			       struct addr_location *addr_al)
110 {
111 #ifdef HAVE_LIBTRACEEVENT
112 	const struct tep_event *tp_format = evsel__tp_format(evsel);
113 
114 	c->pevent = tp_format ? tp_format->tep : NULL;
115 #else
116 	c->pevent = NULL;
117 #endif
118 	c->event_data = sample->raw_data;
119 	c->event = event;
120 	c->sample = sample;
121 	c->evsel = evsel;
122 	c->al = al;
123 	c->addr_al = addr_al;
124 }
125 
126 static int flush_script_unsupported(void)
127 {
128 	return 0;
129 }
130 
131 static int stop_script_unsupported(void)
132 {
133 	return 0;
134 }
135 
136 static void process_event_unsupported(union perf_event *event __maybe_unused,
137 				      struct perf_sample *sample __maybe_unused,
138 				      struct evsel *evsel __maybe_unused,
139 				      struct addr_location *al __maybe_unused,
140 				      struct addr_location *addr_al __maybe_unused)
141 {
142 }
143 
144 static void print_python_unsupported_msg(void)
145 {
146 	fprintf(stderr, "Python scripting not supported."
147 		"  Install libpython and rebuild perf to enable it.\n"
148 		"For example:\n  # apt-get install python-dev (ubuntu)"
149 		"\n  # yum install python-devel (Fedora)"
150 		"\n  etc.\n");
151 }
152 
153 static int python_start_script_unsupported(const char *script __maybe_unused,
154 					   int argc __maybe_unused,
155 					   const char **argv __maybe_unused,
156 					   struct perf_session *session __maybe_unused)
157 {
158 	print_python_unsupported_msg();
159 
160 	return -1;
161 }
162 
163 static int python_generate_script_unsupported(struct tep_handle *pevent
164 					      __maybe_unused,
165 					      const char *outfile
166 					      __maybe_unused)
167 {
168 	print_python_unsupported_msg();
169 
170 	return -1;
171 }
172 
173 struct scripting_ops python_scripting_unsupported_ops = {
174 	.name = "Python",
175 	.dirname = "python",
176 	.start_script = python_start_script_unsupported,
177 	.flush_script = flush_script_unsupported,
178 	.stop_script = stop_script_unsupported,
179 	.process_event = process_event_unsupported,
180 	.generate_script = python_generate_script_unsupported,
181 };
182 
183 static void register_python_scripting(struct scripting_ops *scripting_ops)
184 {
185 	if (scripting_context == NULL)
186 		scripting_context = malloc(sizeof(*scripting_context));
187 
188        if (scripting_context == NULL ||
189 	   script_spec_register("Python", scripting_ops) ||
190 	   script_spec_register("py", scripting_ops)) {
191 		pr_err("Error registering Python script extension: disabling it\n");
192 		zfree(&scripting_context);
193 	}
194 }
195 
196 #ifndef HAVE_LIBPYTHON_SUPPORT
197 void setup_python_scripting(void)
198 {
199 	register_python_scripting(&python_scripting_unsupported_ops);
200 }
201 #else
202 extern struct scripting_ops python_scripting_ops;
203 
204 void setup_python_scripting(void)
205 {
206 	register_python_scripting(&python_scripting_ops);
207 }
208 #endif
209 
210 #ifdef HAVE_LIBTRACEEVENT
211 static void print_perl_unsupported_msg(void)
212 {
213 	fprintf(stderr, "Perl scripting not supported."
214 		"  Install libperl and rebuild perf to enable it.\n"
215 		"For example:\n  # apt-get install libperl-dev (ubuntu)"
216 		"\n  # yum install 'perl(ExtUtils::Embed)' (Fedora)"
217 		"\n  etc.\n");
218 }
219 
220 static int perl_start_script_unsupported(const char *script __maybe_unused,
221 					 int argc __maybe_unused,
222 					 const char **argv __maybe_unused,
223 					 struct perf_session *session __maybe_unused)
224 {
225 	print_perl_unsupported_msg();
226 
227 	return -1;
228 }
229 
230 static int perl_generate_script_unsupported(struct tep_handle *pevent
231 					    __maybe_unused,
232 					    const char *outfile __maybe_unused)
233 {
234 	print_perl_unsupported_msg();
235 
236 	return -1;
237 }
238 
239 struct scripting_ops perl_scripting_unsupported_ops = {
240 	.name = "Perl",
241 	.dirname = "perl",
242 	.start_script = perl_start_script_unsupported,
243 	.flush_script = flush_script_unsupported,
244 	.stop_script = stop_script_unsupported,
245 	.process_event = process_event_unsupported,
246 	.generate_script = perl_generate_script_unsupported,
247 };
248 
249 static void register_perl_scripting(struct scripting_ops *scripting_ops)
250 {
251 	if (scripting_context == NULL)
252 		scripting_context = malloc(sizeof(*scripting_context));
253 
254        if (scripting_context == NULL ||
255 	   script_spec_register("Perl", scripting_ops) ||
256 	   script_spec_register("pl", scripting_ops)) {
257 		pr_err("Error registering Perl script extension: disabling it\n");
258 		zfree(&scripting_context);
259 	}
260 }
261 
262 #ifndef HAVE_LIBPERL_SUPPORT
263 void setup_perl_scripting(void)
264 {
265 	register_perl_scripting(&perl_scripting_unsupported_ops);
266 }
267 #else
268 extern struct scripting_ops perl_scripting_ops;
269 
270 void setup_perl_scripting(void)
271 {
272 	register_perl_scripting(&perl_scripting_ops);
273 }
274 #endif
275 #endif
276 
277 #if !defined(__i386__) && !defined(__x86_64__)
278 void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
279 		     struct thread *thread __maybe_unused,
280 		     struct machine *machine __maybe_unused)
281 {
282 }
283 #endif
284 
285 void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
286 		       struct machine *machine, bool native_arch)
287 {
288 	if (sample->insn_len == 0 && native_arch)
289 		arch_fetch_insn(sample, thread, machine);
290 }
291 
292 static const struct {
293 	u32 flags;
294 	const char *name;
295 } sample_flags[] = {
296 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
297 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
298 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
299 	{PERF_IP_FLAG_BRANCH, "jmp"},
300 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
301 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
302 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
303 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
304 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
305 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |	PERF_IP_FLAG_INTERRUPT,
306 	 "hw int"},
307 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
308 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
309 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
310 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"},
311 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"},
312 	{PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_BRANCH_MISS, "br miss"},
313 	{0, NULL}
314 };
315 
316 static const char *sample_flags_to_name(u32 flags)
317 {
318 	int i;
319 
320 	for (i = 0; sample_flags[i].name ; i++) {
321 		if (sample_flags[i].flags == flags)
322 			return sample_flags[i].name;
323 	}
324 
325 	return NULL;
326 }
327 
328 int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
329 {
330 	u32 xf = PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_INTR_DISABLE |
331 		 PERF_IP_FLAG_INTR_TOGGLE;
332 	const char *chars = PERF_IP_FLAG_CHARS;
333 	const size_t n = strlen(PERF_IP_FLAG_CHARS);
334 	const char *name = NULL;
335 	size_t i, pos = 0;
336 	char xs[16] = {0};
337 
338 	if (flags & xf)
339 		snprintf(xs, sizeof(xs), "(%s%s%s)",
340 			 flags & PERF_IP_FLAG_IN_TX ? "x" : "",
341 			 flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
342 			 flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
343 
344 	name = sample_flags_to_name(flags & ~xf);
345 	if (name)
346 		return snprintf(str, sz, "%-15s%6s", name, xs);
347 
348 	if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
349 		name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_BEGIN));
350 		if (name)
351 			return snprintf(str, sz, "tr strt %-7s%6s", name, xs);
352 	}
353 
354 	if (flags & PERF_IP_FLAG_TRACE_END) {
355 		name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_END));
356 		if (name)
357 			return snprintf(str, sz, "tr end  %-7s%6s", name, xs);
358 	}
359 
360 	for (i = 0; i < n; i++, flags >>= 1) {
361 		if ((flags & 1) && pos < sz)
362 			str[pos++] = chars[i];
363 	}
364 	for (; i < 32; i++, flags >>= 1) {
365 		if ((flags & 1) && pos < sz)
366 			str[pos++] = '?';
367 	}
368 	if (pos < sz)
369 		str[pos] = 0;
370 
371 	return pos;
372 }
373