blob: bd0000300c774210b386d952340fdd312bb4f2c3 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Tom Zanussi82d156c2010-01-27 02:27:55 -06002/*
3 * trace-event-scripting. Scripting engine common and initialization code.
4 *
5 * Copyright (C) 2009-2010 Tom Zanussi <tzanussi@gmail.com>
Tom Zanussi82d156c2010-01-27 02:27:55 -06006 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
Tom Zanussi82d156c2010-01-27 02:27:55 -060011#include <errno.h>
Adrian Hunter80c3a7d2023-03-15 10:43:21 +020012#ifdef HAVE_LIBTRACEEVENT
Ian Rogers378ef0f2022-12-05 14:59:39 -080013#include <traceevent/event-parse.h>
Adrian Hunter80c3a7d2023-03-15 10:43:21 +020014#endif
Tom Zanussi82d156c2010-01-27 02:27:55 -060015
Arnaldo Carvalho de Melo9a8860b2016-10-25 17:30:05 -030016#include "debug.h"
Tom Zanussi82d156c2010-01-27 02:27:55 -060017#include "trace-event.h"
Adrian Huntercac30402021-05-30 22:22:58 +030018#include "evsel.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030019#include <linux/zalloc.h>
Arnaldo Carvalho de Melo98231472022-10-26 17:24:27 -030020#include "util/sample.h"
Tom Zanussi82d156c2010-01-27 02:27:55 -060021
22struct scripting_context *scripting_context;
23
Adrian Huntercac30402021-05-30 22:22:58 +030024void scripting_context__update(struct scripting_context *c,
25 union perf_event *event,
26 struct perf_sample *sample,
27 struct evsel *evsel,
28 struct addr_location *al,
29 struct addr_location *addr_al)
30{
31 c->event_data = sample->raw_data;
Adrian Hunter80c3a7d2023-03-15 10:43:21 +020032 c->pevent = NULL;
33#ifdef HAVE_LIBTRACEEVENT
Adrian Huntercac30402021-05-30 22:22:58 +030034 if (evsel->tp_format)
35 c->pevent = evsel->tp_format->tep;
Adrian Hunter80c3a7d2023-03-15 10:43:21 +020036#endif
Adrian Huntercac30402021-05-30 22:22:58 +030037 c->event = event;
38 c->sample = sample;
39 c->evsel = evsel;
40 c->al = al;
41 c->addr_al = addr_al;
42}
43
Adrian Hunterd445dd22014-08-15 22:08:37 +030044static int flush_script_unsupported(void)
45{
46 return 0;
47}
48
Tom Zanussi82d156c2010-01-27 02:27:55 -060049static int stop_script_unsupported(void)
50{
51 return 0;
52}
53
Irina Tirdea1d037ca2012-09-11 01:15:03 +030054static void process_event_unsupported(union perf_event *event __maybe_unused,
55 struct perf_sample *sample __maybe_unused,
Jiri Olsa32dcd022019-07-21 13:23:51 +020056 struct evsel *evsel __maybe_unused,
Adrian Hunter3f8e0092021-05-25 12:51:05 +030057 struct addr_location *al __maybe_unused,
58 struct addr_location *addr_al __maybe_unused)
Tom Zanussi82d156c2010-01-27 02:27:55 -060059{
60}
61
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060062static void print_python_unsupported_msg(void)
63{
64 fprintf(stderr, "Python scripting not supported."
65 " Install libpython and rebuild perf to enable it.\n"
66 "For example:\n # apt-get install python-dev (ubuntu)"
67 "\n # yum install python-devel (Fedora)"
68 "\n etc.\n");
69}
70
Irina Tirdea1d037ca2012-09-11 01:15:03 +030071static int python_start_script_unsupported(const char *script __maybe_unused,
72 int argc __maybe_unused,
Adrian Hunter67e50ce2021-05-30 22:22:59 +030073 const char **argv __maybe_unused,
74 struct perf_session *session __maybe_unused)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060075{
76 print_python_unsupported_msg();
77
78 return -1;
79}
80
Tzvetomir Stoyanov (VMware)096177a82018-08-08 14:02:46 -040081static int python_generate_script_unsupported(struct tep_handle *pevent
Irina Tirdea1d037ca2012-09-11 01:15:03 +030082 __maybe_unused,
83 const char *outfile
84 __maybe_unused)
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060085{
86 print_python_unsupported_msg();
87
88 return -1;
89}
90
91struct scripting_ops python_scripting_unsupported_ops = {
92 .name = "Python",
Adrian Hunter6ea4b5d2021-05-24 09:57:18 +030093 .dirname = "python",
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060094 .start_script = python_start_script_unsupported,
Adrian Hunterd445dd22014-08-15 22:08:37 +030095 .flush_script = flush_script_unsupported,
Tom Zanussi7e4b21b2010-01-27 02:27:57 -060096 .stop_script = stop_script_unsupported,
97 .process_event = process_event_unsupported,
98 .generate_script = python_generate_script_unsupported,
99};
100
101static void register_python_scripting(struct scripting_ops *scripting_ops)
102{
Arnaldo Carvalho de Melocf346d52016-10-25 17:20:47 -0300103 if (scripting_context == NULL)
104 scripting_context = malloc(sizeof(*scripting_context));
Arnaldo Carvalho de Melo9a8860b2016-10-25 17:30:05 -0300105
106 if (scripting_context == NULL ||
107 script_spec_register("Python", scripting_ops) ||
108 script_spec_register("py", scripting_ops)) {
109 pr_err("Error registering Python script extension: disabling it\n");
110 zfree(&scripting_context);
111 }
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600112}
113
Jin Yao90ce61b2018-04-09 18:26:47 +0800114#ifndef HAVE_LIBPYTHON_SUPPORT
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600115void setup_python_scripting(void)
116{
117 register_python_scripting(&python_scripting_unsupported_ops);
118}
119#else
Stephane Eranian0f940cb2010-09-21 00:45:01 +0200120extern struct scripting_ops python_scripting_ops;
Tom Zanussi7e4b21b2010-01-27 02:27:57 -0600121
122void setup_python_scripting(void)
123{
124 register_python_scripting(&python_scripting_ops);
125}
126#endif
127
Adrian Hunter80c3a7d2023-03-15 10:43:21 +0200128#ifdef HAVE_LIBTRACEEVENT
Tom Zanussi82d156c2010-01-27 02:27:55 -0600129static void print_perl_unsupported_msg(void)
130{
131 fprintf(stderr, "Perl scripting not supported."
132 " Install libperl and rebuild perf to enable it.\n"
133 "For example:\n # apt-get install libperl-dev (ubuntu)"
134 "\n # yum install 'perl(ExtUtils::Embed)' (Fedora)"
135 "\n etc.\n");
136}
137
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300138static int perl_start_script_unsupported(const char *script __maybe_unused,
139 int argc __maybe_unused,
Adrian Hunter67e50ce2021-05-30 22:22:59 +0300140 const char **argv __maybe_unused,
141 struct perf_session *session __maybe_unused)
Tom Zanussi82d156c2010-01-27 02:27:55 -0600142{
143 print_perl_unsupported_msg();
144
145 return -1;
146}
147
Tzvetomir Stoyanov (VMware)096177a82018-08-08 14:02:46 -0400148static int perl_generate_script_unsupported(struct tep_handle *pevent
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300149 __maybe_unused,
150 const char *outfile __maybe_unused)
Tom Zanussi82d156c2010-01-27 02:27:55 -0600151{
152 print_perl_unsupported_msg();
153
154 return -1;
155}
156
157struct scripting_ops perl_scripting_unsupported_ops = {
158 .name = "Perl",
Adrian Hunter6ea4b5d2021-05-24 09:57:18 +0300159 .dirname = "perl",
Tom Zanussi82d156c2010-01-27 02:27:55 -0600160 .start_script = perl_start_script_unsupported,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300161 .flush_script = flush_script_unsupported,
Tom Zanussi82d156c2010-01-27 02:27:55 -0600162 .stop_script = stop_script_unsupported,
163 .process_event = process_event_unsupported,
164 .generate_script = perl_generate_script_unsupported,
165};
166
167static void register_perl_scripting(struct scripting_ops *scripting_ops)
168{
Arnaldo Carvalho de Melocf346d52016-10-25 17:20:47 -0300169 if (scripting_context == NULL)
170 scripting_context = malloc(sizeof(*scripting_context));
Arnaldo Carvalho de Melo9a8860b2016-10-25 17:30:05 -0300171
172 if (scripting_context == NULL ||
173 script_spec_register("Perl", scripting_ops) ||
174 script_spec_register("pl", scripting_ops)) {
175 pr_err("Error registering Perl script extension: disabling it\n");
176 zfree(&scripting_context);
177 }
Tom Zanussi82d156c2010-01-27 02:27:55 -0600178}
179
Jin Yao90ce61b2018-04-09 18:26:47 +0800180#ifndef HAVE_LIBPERL_SUPPORT
Tom Zanussi82d156c2010-01-27 02:27:55 -0600181void setup_perl_scripting(void)
182{
183 register_perl_scripting(&perl_scripting_unsupported_ops);
184}
185#else
Stephane Eranian0f940cb2010-09-21 00:45:01 +0200186extern struct scripting_ops perl_scripting_ops;
Tom Zanussi82d156c2010-01-27 02:27:55 -0600187
188void setup_perl_scripting(void)
189{
190 register_perl_scripting(&perl_scripting_ops);
191}
192#endif
Adrian Hunter80c3a7d2023-03-15 10:43:21 +0200193#endif