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 #include <traceevent/event-parse.h>
13
14 #include "debug.h"
15 #include "trace-event.h"
16 #include "evsel.h"
17 #include <linux/zalloc.h>
18 #include "util/sample.h"
19
20 struct scripting_context *scripting_context;
21
scripting_context__update(struct scripting_context * c,union perf_event * event,struct perf_sample * sample,struct evsel * evsel,struct addr_location * al,struct addr_location * addr_al)22 void scripting_context__update(struct scripting_context *c,
23 union perf_event *event,
24 struct perf_sample *sample,
25 struct evsel *evsel,
26 struct addr_location *al,
27 struct addr_location *addr_al)
28 {
29 c->event_data = sample->raw_data;
30 if (evsel->tp_format)
31 c->pevent = evsel->tp_format->tep;
32 else
33 c->pevent = NULL;
34 c->event = event;
35 c->sample = sample;
36 c->evsel = evsel;
37 c->al = al;
38 c->addr_al = addr_al;
39 }
40
flush_script_unsupported(void)41 static int flush_script_unsupported(void)
42 {
43 return 0;
44 }
45
stop_script_unsupported(void)46 static int stop_script_unsupported(void)
47 {
48 return 0;
49 }
50
process_event_unsupported(union perf_event * event __maybe_unused,struct perf_sample * sample __maybe_unused,struct evsel * evsel __maybe_unused,struct addr_location * al __maybe_unused,struct addr_location * addr_al __maybe_unused)51 static void process_event_unsupported(union perf_event *event __maybe_unused,
52 struct perf_sample *sample __maybe_unused,
53 struct evsel *evsel __maybe_unused,
54 struct addr_location *al __maybe_unused,
55 struct addr_location *addr_al __maybe_unused)
56 {
57 }
58
print_python_unsupported_msg(void)59 static void print_python_unsupported_msg(void)
60 {
61 fprintf(stderr, "Python scripting not supported."
62 " Install libpython and rebuild perf to enable it.\n"
63 "For example:\n # apt-get install python-dev (ubuntu)"
64 "\n # yum install python-devel (Fedora)"
65 "\n etc.\n");
66 }
67
python_start_script_unsupported(const char * script __maybe_unused,int argc __maybe_unused,const char ** argv __maybe_unused,struct perf_session * session __maybe_unused)68 static int python_start_script_unsupported(const char *script __maybe_unused,
69 int argc __maybe_unused,
70 const char **argv __maybe_unused,
71 struct perf_session *session __maybe_unused)
72 {
73 print_python_unsupported_msg();
74
75 return -1;
76 }
77
python_generate_script_unsupported(struct tep_handle * pevent __maybe_unused,const char * outfile __maybe_unused)78 static int python_generate_script_unsupported(struct tep_handle *pevent
79 __maybe_unused,
80 const char *outfile
81 __maybe_unused)
82 {
83 print_python_unsupported_msg();
84
85 return -1;
86 }
87
88 struct scripting_ops python_scripting_unsupported_ops = {
89 .name = "Python",
90 .dirname = "python",
91 .start_script = python_start_script_unsupported,
92 .flush_script = flush_script_unsupported,
93 .stop_script = stop_script_unsupported,
94 .process_event = process_event_unsupported,
95 .generate_script = python_generate_script_unsupported,
96 };
97
register_python_scripting(struct scripting_ops * scripting_ops)98 static void register_python_scripting(struct scripting_ops *scripting_ops)
99 {
100 if (scripting_context == NULL)
101 scripting_context = malloc(sizeof(*scripting_context));
102
103 if (scripting_context == NULL ||
104 script_spec_register("Python", scripting_ops) ||
105 script_spec_register("py", scripting_ops)) {
106 pr_err("Error registering Python script extension: disabling it\n");
107 zfree(&scripting_context);
108 }
109 }
110
111 #ifndef HAVE_LIBPYTHON_SUPPORT
setup_python_scripting(void)112 void setup_python_scripting(void)
113 {
114 register_python_scripting(&python_scripting_unsupported_ops);
115 }
116 #else
117 extern struct scripting_ops python_scripting_ops;
118
setup_python_scripting(void)119 void setup_python_scripting(void)
120 {
121 register_python_scripting(&python_scripting_ops);
122 }
123 #endif
124
print_perl_unsupported_msg(void)125 static void print_perl_unsupported_msg(void)
126 {
127 fprintf(stderr, "Perl scripting not supported."
128 " Install libperl and rebuild perf to enable it.\n"
129 "For example:\n # apt-get install libperl-dev (ubuntu)"
130 "\n # yum install 'perl(ExtUtils::Embed)' (Fedora)"
131 "\n etc.\n");
132 }
133
perl_start_script_unsupported(const char * script __maybe_unused,int argc __maybe_unused,const char ** argv __maybe_unused,struct perf_session * session __maybe_unused)134 static int perl_start_script_unsupported(const char *script __maybe_unused,
135 int argc __maybe_unused,
136 const char **argv __maybe_unused,
137 struct perf_session *session __maybe_unused)
138 {
139 print_perl_unsupported_msg();
140
141 return -1;
142 }
143
perl_generate_script_unsupported(struct tep_handle * pevent __maybe_unused,const char * outfile __maybe_unused)144 static int perl_generate_script_unsupported(struct tep_handle *pevent
145 __maybe_unused,
146 const char *outfile __maybe_unused)
147 {
148 print_perl_unsupported_msg();
149
150 return -1;
151 }
152
153 struct scripting_ops perl_scripting_unsupported_ops = {
154 .name = "Perl",
155 .dirname = "perl",
156 .start_script = perl_start_script_unsupported,
157 .flush_script = flush_script_unsupported,
158 .stop_script = stop_script_unsupported,
159 .process_event = process_event_unsupported,
160 .generate_script = perl_generate_script_unsupported,
161 };
162
register_perl_scripting(struct scripting_ops * scripting_ops)163 static void register_perl_scripting(struct scripting_ops *scripting_ops)
164 {
165 if (scripting_context == NULL)
166 scripting_context = malloc(sizeof(*scripting_context));
167
168 if (scripting_context == NULL ||
169 script_spec_register("Perl", scripting_ops) ||
170 script_spec_register("pl", scripting_ops)) {
171 pr_err("Error registering Perl script extension: disabling it\n");
172 zfree(&scripting_context);
173 }
174 }
175
176 #ifndef HAVE_LIBPERL_SUPPORT
setup_perl_scripting(void)177 void setup_perl_scripting(void)
178 {
179 register_perl_scripting(&perl_scripting_unsupported_ops);
180 }
181 #else
182 extern struct scripting_ops perl_scripting_ops;
183
setup_perl_scripting(void)184 void setup_perl_scripting(void)
185 {
186 register_perl_scripting(&perl_scripting_ops);
187 }
188 #endif
189