File Index
Files and directories
Sign in
FILE

trace-event-scripting.c

File preview with syntax highlighting.

linux-6.19.14/tools/perf/util/trace-event-scripting.c C
  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
 25unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
 26
 27struct scripting_context *scripting_context;
 28
 29struct script_spec {
 30	struct list_head	node;
 31	struct scripting_ops	*ops;
 32	char			spec[];
 33};
 34
 35static LIST_HEAD(script_specs);
 36
 37static 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
 50static void script_spec__add(struct script_spec *s)
 51{
 52	list_add_tail(&s->node, &script_specs);
 53}
 54
 55static 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
 65static 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
 81struct 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
 91int 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
104void 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
126static int flush_script_unsupported(void)
127{
128	return 0;
129}
130
131static int stop_script_unsupported(void)
132{
133	return 0;
134}
135
136static 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
144static 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
153static 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
163static 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
173struct 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
183static 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
197void setup_python_scripting(void)
198{
199	register_python_scripting(&python_scripting_unsupported_ops);
200}
201#else
202extern struct scripting_ops python_scripting_ops;
203
204void setup_python_scripting(void)
205{
206	register_python_scripting(&python_scripting_ops);
207}
208#endif
209
210#ifdef HAVE_LIBTRACEEVENT
211static 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
220static 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
230static 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
239struct 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
249static 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
263void setup_perl_scripting(void)
264{
265	register_perl_scripting(&perl_scripting_unsupported_ops);
266}
267#else
268extern struct scripting_ops perl_scripting_ops;
269
270void 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__)
278void 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
285void 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
292static 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	{0, NULL}
313};
314
315static const struct {
316	u32 flags;
317	const char *name;
318} branch_events[] = {
319	{PERF_IP_FLAG_BRANCH_MISS, "miss"},
320	{PERF_IP_FLAG_NOT_TAKEN, "not_taken"},
321	{0, NULL}
322};
323
324static int sample_flags_to_name(u32 flags, char *str, size_t size)
325{
326	int i;
327	const char *prefix;
328	int pos = 0, ret, ev_idx = 0;
329	u32 xf = flags & PERF_ADDITIONAL_STATE_MASK;
330	u32 types, events;
331	char xs[16] = { 0 };
332
333	/* Clear additional state bits */
334	flags &= ~PERF_ADDITIONAL_STATE_MASK;
335
336	if (flags & PERF_IP_FLAG_TRACE_BEGIN)
337		prefix = "tr strt ";
338	else if (flags & PERF_IP_FLAG_TRACE_END)
339		prefix = "tr end  ";
340	else
341		prefix = "";
342
343	ret = snprintf(str + pos, size - pos, "%s", prefix);
344	if (ret < 0)
345		return ret;
346	pos += ret;
347
348	flags &= ~(PERF_IP_FLAG_TRACE_BEGIN | PERF_IP_FLAG_TRACE_END);
349
350	types = flags & ~PERF_IP_FLAG_BRANCH_EVENT_MASK;
351	for (i = 0; sample_flags[i].name; i++) {
352		if (sample_flags[i].flags != types)
353			continue;
354
355		ret = snprintf(str + pos, size - pos, "%s", sample_flags[i].name);
356		if (ret < 0)
357			return ret;
358		pos += ret;
359		break;
360	}
361
362	events = flags & PERF_IP_FLAG_BRANCH_EVENT_MASK;
363	for (i = 0; branch_events[i].name; i++) {
364		if (!(branch_events[i].flags & events))
365			continue;
366
367		ret = snprintf(str + pos, size - pos, !ev_idx ? "/%s" : ",%s",
368			       branch_events[i].name);
369		if (ret < 0)
370			return ret;
371		pos += ret;
372		ev_idx++;
373	}
374
375	/* Add an end character '/' for events */
376	if (ev_idx) {
377		ret = snprintf(str + pos, size - pos, "/");
378		if (ret < 0)
379			return ret;
380		pos += ret;
381	}
382
383	if (!xf)
384		return pos;
385
386	snprintf(xs, sizeof(xs), "(%s%s%s)",
387		 flags & PERF_IP_FLAG_IN_TX ? "x" : "",
388		 flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
389		 flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
390
391	/* Right align the string if its length is less than the limit */
392	if ((pos + strlen(xs)) < SAMPLE_FLAGS_STR_ALIGNED_SIZE)
393		ret = snprintf(str + pos, size - pos, "%*s",
394			       (int)(SAMPLE_FLAGS_STR_ALIGNED_SIZE - ret), xs);
395	else
396		ret = snprintf(str + pos, size - pos, " %s", xs);
397	if (ret < 0)
398		return ret;
399
400	return pos + ret;
401}
402
403int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
404{
405	const char *chars = PERF_IP_FLAG_CHARS;
406	const size_t n = strlen(PERF_IP_FLAG_CHARS);
407	size_t i, pos = 0;
408	int ret;
409
410	ret = sample_flags_to_name(flags, str, sz);
411	if (ret > 0)
412		return ret;
413
414	for (i = 0; i < n; i++, flags >>= 1) {
415		if ((flags & 1) && pos < sz)
416			str[pos++] = chars[i];
417	}
418	for (; i < 32; i++, flags >>= 1) {
419		if ((flags & 1) && pos < sz)
420			str[pos++] = '?';
421	}
422	if (pos < sz)
423		str[pos] = 0;
424
425	return pos;
426}
~/ Read-only file preview.