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