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