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 <traceevent/event-parse.h> |
14 | #endif |
15 | |
16 | #include "debug.h" |
17 | #include "trace-event.h" |
18 | #include "evsel.h" |
19 | #include <linux/zalloc.h> |
20 | #include "util/sample.h" |
21 | |
22 | struct scripting_context *scripting_context; |
23 | |
24 | void 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; |
32 | c->pevent = NULL; |
33 | #ifdef HAVE_LIBTRACEEVENT |
34 | if (evsel->tp_format) |
35 | c->pevent = evsel->tp_format->tep; |
36 | #endif |
37 | c->event = event; |
38 | c->sample = sample; |
39 | c->evsel = evsel; |
40 | c->al = al; |
41 | c->addr_al = addr_al; |
42 | } |
43 | |
44 | static int flush_script_unsupported(void) |
45 | { |
46 | return 0; |
47 | } |
48 | |
49 | static int stop_script_unsupported(void) |
50 | { |
51 | return 0; |
52 | } |
53 | |
54 | static void process_event_unsupported(union perf_event *event __maybe_unused, |
55 | struct perf_sample *sample __maybe_unused, |
56 | struct evsel *evsel __maybe_unused, |
57 | struct addr_location *al __maybe_unused, |
58 | struct addr_location *addr_al __maybe_unused) |
59 | { |
60 | } |
61 | |
62 | static 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 | |
71 | static int python_start_script_unsupported(const char *script __maybe_unused, |
72 | int argc __maybe_unused, |
73 | const char **argv __maybe_unused, |
74 | struct perf_session *session __maybe_unused) |
75 | { |
76 | print_python_unsupported_msg(); |
77 | |
78 | return -1; |
79 | } |
80 | |
81 | static int python_generate_script_unsupported(struct tep_handle *pevent |
82 | __maybe_unused, |
83 | const char *outfile |
84 | __maybe_unused) |
85 | { |
86 | print_python_unsupported_msg(); |
87 | |
88 | return -1; |
89 | } |
90 | |
91 | struct scripting_ops python_scripting_unsupported_ops = { |
92 | .name = "Python" , |
93 | .dirname = "python" , |
94 | .start_script = python_start_script_unsupported, |
95 | .flush_script = flush_script_unsupported, |
96 | .stop_script = stop_script_unsupported, |
97 | .process_event = process_event_unsupported, |
98 | .generate_script = python_generate_script_unsupported, |
99 | }; |
100 | |
101 | static void register_python_scripting(struct scripting_ops *scripting_ops) |
102 | { |
103 | if (scripting_context == NULL) |
104 | scripting_context = malloc(sizeof(*scripting_context)); |
105 | |
106 | if (scripting_context == NULL || |
107 | script_spec_register(spec: "Python" , ops: scripting_ops) || |
108 | script_spec_register(spec: "py" , ops: scripting_ops)) { |
109 | pr_err("Error registering Python script extension: disabling it\n" ); |
110 | zfree(&scripting_context); |
111 | } |
112 | } |
113 | |
114 | #ifndef HAVE_LIBPYTHON_SUPPORT |
115 | void setup_python_scripting(void) |
116 | { |
117 | register_python_scripting(scripting_ops: &python_scripting_unsupported_ops); |
118 | } |
119 | #else |
120 | extern struct scripting_ops python_scripting_ops; |
121 | |
122 | void setup_python_scripting(void) |
123 | { |
124 | register_python_scripting(&python_scripting_ops); |
125 | } |
126 | #endif |
127 | |
128 | #ifdef HAVE_LIBTRACEEVENT |
129 | static 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 | |
138 | static int perl_start_script_unsupported(const char *script __maybe_unused, |
139 | int argc __maybe_unused, |
140 | const char **argv __maybe_unused, |
141 | struct perf_session *session __maybe_unused) |
142 | { |
143 | print_perl_unsupported_msg(); |
144 | |
145 | return -1; |
146 | } |
147 | |
148 | static int perl_generate_script_unsupported(struct tep_handle *pevent |
149 | __maybe_unused, |
150 | const char *outfile __maybe_unused) |
151 | { |
152 | print_perl_unsupported_msg(); |
153 | |
154 | return -1; |
155 | } |
156 | |
157 | struct scripting_ops perl_scripting_unsupported_ops = { |
158 | .name = "Perl" , |
159 | .dirname = "perl" , |
160 | .start_script = perl_start_script_unsupported, |
161 | .flush_script = flush_script_unsupported, |
162 | .stop_script = stop_script_unsupported, |
163 | .process_event = process_event_unsupported, |
164 | .generate_script = perl_generate_script_unsupported, |
165 | }; |
166 | |
167 | static void register_perl_scripting(struct scripting_ops *scripting_ops) |
168 | { |
169 | if (scripting_context == NULL) |
170 | scripting_context = malloc(sizeof(*scripting_context)); |
171 | |
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 | } |
178 | } |
179 | |
180 | #ifndef HAVE_LIBPERL_SUPPORT |
181 | void setup_perl_scripting(void) |
182 | { |
183 | register_perl_scripting(&perl_scripting_unsupported_ops); |
184 | } |
185 | #else |
186 | extern struct scripting_ops perl_scripting_ops; |
187 | |
188 | void setup_perl_scripting(void) |
189 | { |
190 | register_perl_scripting(&perl_scripting_ops); |
191 | } |
192 | #endif |
193 | #endif |
194 | |