Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 2 | /* |
| 3 | * trace-event-scripting. Scripting engine common and initialization code. |
| 4 | * |
| 5 | * Copyright (C) 2009-2010 Tom Zanussi <tzanussi@gmail.com> |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Adrian Hunter | 80c3a7d | 2023-03-15 10:43:21 +0200 | [diff] [blame] | 12 | #ifdef HAVE_LIBTRACEEVENT |
Ian Rogers | 378ef0f | 2022-12-05 14:59:39 -0800 | [diff] [blame] | 13 | #include <traceevent/event-parse.h> |
Adrian Hunter | 80c3a7d | 2023-03-15 10:43:21 +0200 | [diff] [blame] | 14 | #endif |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 9a8860b | 2016-10-25 17:30:05 -0300 | [diff] [blame] | 16 | #include "debug.h" |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 17 | #include "trace-event.h" |
Adrian Hunter | cac3040 | 2021-05-30 22:22:58 +0300 | [diff] [blame] | 18 | #include "evsel.h" |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 19 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 9823147 | 2022-10-26 17:24:27 -0300 | [diff] [blame] | 20 | #include "util/sample.h" |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 21 | |
| 22 | struct scripting_context *scripting_context; |
| 23 | |
Adrian Hunter | cac3040 | 2021-05-30 22:22:58 +0300 | [diff] [blame] | 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; |
Adrian Hunter | 80c3a7d | 2023-03-15 10:43:21 +0200 | [diff] [blame] | 32 | c->pevent = NULL; |
| 33 | #ifdef HAVE_LIBTRACEEVENT |
Adrian Hunter | cac3040 | 2021-05-30 22:22:58 +0300 | [diff] [blame] | 34 | if (evsel->tp_format) |
| 35 | c->pevent = evsel->tp_format->tep; |
Adrian Hunter | 80c3a7d | 2023-03-15 10:43:21 +0200 | [diff] [blame] | 36 | #endif |
Adrian Hunter | cac3040 | 2021-05-30 22:22:58 +0300 | [diff] [blame] | 37 | c->event = event; |
| 38 | c->sample = sample; |
| 39 | c->evsel = evsel; |
| 40 | c->al = al; |
| 41 | c->addr_al = addr_al; |
| 42 | } |
| 43 | |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 44 | static int flush_script_unsupported(void) |
| 45 | { |
| 46 | return 0; |
| 47 | } |
| 48 | |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 49 | static int stop_script_unsupported(void) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 54 | static void process_event_unsupported(union perf_event *event __maybe_unused, |
| 55 | struct perf_sample *sample __maybe_unused, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 56 | struct evsel *evsel __maybe_unused, |
Adrian Hunter | 3f8e009 | 2021-05-25 12:51:05 +0300 | [diff] [blame] | 57 | struct addr_location *al __maybe_unused, |
| 58 | struct addr_location *addr_al __maybe_unused) |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 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 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 71 | static int python_start_script_unsupported(const char *script __maybe_unused, |
| 72 | int argc __maybe_unused, |
Adrian Hunter | 67e50ce | 2021-05-30 22:22:59 +0300 | [diff] [blame] | 73 | const char **argv __maybe_unused, |
| 74 | struct perf_session *session __maybe_unused) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 75 | { |
| 76 | print_python_unsupported_msg(); |
| 77 | |
| 78 | return -1; |
| 79 | } |
| 80 | |
Tzvetomir Stoyanov (VMware) | 096177a8 | 2018-08-08 14:02:46 -0400 | [diff] [blame] | 81 | static int python_generate_script_unsupported(struct tep_handle *pevent |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 82 | __maybe_unused, |
| 83 | const char *outfile |
| 84 | __maybe_unused) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 85 | { |
| 86 | print_python_unsupported_msg(); |
| 87 | |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | struct scripting_ops python_scripting_unsupported_ops = { |
| 92 | .name = "Python", |
Adrian Hunter | 6ea4b5d | 2021-05-24 09:57:18 +0300 | [diff] [blame] | 93 | .dirname = "python", |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 94 | .start_script = python_start_script_unsupported, |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 95 | .flush_script = flush_script_unsupported, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 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 | { |
Arnaldo Carvalho de Melo | cf346d5 | 2016-10-25 17:20:47 -0300 | [diff] [blame] | 103 | if (scripting_context == NULL) |
| 104 | scripting_context = malloc(sizeof(*scripting_context)); |
Arnaldo Carvalho de Melo | 9a8860b | 2016-10-25 17:30:05 -0300 | [diff] [blame] | 105 | |
| 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 Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 112 | } |
| 113 | |
Jin Yao | 90ce61b | 2018-04-09 18:26:47 +0800 | [diff] [blame] | 114 | #ifndef HAVE_LIBPYTHON_SUPPORT |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 115 | void setup_python_scripting(void) |
| 116 | { |
| 117 | register_python_scripting(&python_scripting_unsupported_ops); |
| 118 | } |
| 119 | #else |
Stephane Eranian | 0f940cb | 2010-09-21 00:45:01 +0200 | [diff] [blame] | 120 | extern struct scripting_ops python_scripting_ops; |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 121 | |
| 122 | void setup_python_scripting(void) |
| 123 | { |
| 124 | register_python_scripting(&python_scripting_ops); |
| 125 | } |
| 126 | #endif |
| 127 | |
Adrian Hunter | 80c3a7d | 2023-03-15 10:43:21 +0200 | [diff] [blame] | 128 | #ifdef HAVE_LIBTRACEEVENT |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 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 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 138 | static int perl_start_script_unsupported(const char *script __maybe_unused, |
| 139 | int argc __maybe_unused, |
Adrian Hunter | 67e50ce | 2021-05-30 22:22:59 +0300 | [diff] [blame] | 140 | const char **argv __maybe_unused, |
| 141 | struct perf_session *session __maybe_unused) |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 142 | { |
| 143 | print_perl_unsupported_msg(); |
| 144 | |
| 145 | return -1; |
| 146 | } |
| 147 | |
Tzvetomir Stoyanov (VMware) | 096177a8 | 2018-08-08 14:02:46 -0400 | [diff] [blame] | 148 | static int perl_generate_script_unsupported(struct tep_handle *pevent |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 149 | __maybe_unused, |
| 150 | const char *outfile __maybe_unused) |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 151 | { |
| 152 | print_perl_unsupported_msg(); |
| 153 | |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | struct scripting_ops perl_scripting_unsupported_ops = { |
| 158 | .name = "Perl", |
Adrian Hunter | 6ea4b5d | 2021-05-24 09:57:18 +0300 | [diff] [blame] | 159 | .dirname = "perl", |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 160 | .start_script = perl_start_script_unsupported, |
Adrian Hunter | d445dd2 | 2014-08-15 22:08:37 +0300 | [diff] [blame] | 161 | .flush_script = flush_script_unsupported, |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 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 | { |
Arnaldo Carvalho de Melo | cf346d5 | 2016-10-25 17:20:47 -0300 | [diff] [blame] | 169 | if (scripting_context == NULL) |
| 170 | scripting_context = malloc(sizeof(*scripting_context)); |
Arnaldo Carvalho de Melo | 9a8860b | 2016-10-25 17:30:05 -0300 | [diff] [blame] | 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 | } |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Jin Yao | 90ce61b | 2018-04-09 18:26:47 +0800 | [diff] [blame] | 180 | #ifndef HAVE_LIBPERL_SUPPORT |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 181 | void setup_perl_scripting(void) |
| 182 | { |
| 183 | register_perl_scripting(&perl_scripting_unsupported_ops); |
| 184 | } |
| 185 | #else |
Stephane Eranian | 0f940cb | 2010-09-21 00:45:01 +0200 | [diff] [blame] | 186 | extern struct scripting_ops perl_scripting_ops; |
Tom Zanussi | 82d156c | 2010-01-27 02:27:55 -0600 | [diff] [blame] | 187 | |
| 188 | void setup_perl_scripting(void) |
| 189 | { |
| 190 | register_perl_scripting(&perl_scripting_ops); |
| 191 | } |
| 192 | #endif |
Adrian Hunter | 80c3a7d | 2023-03-15 10:43:21 +0200 | [diff] [blame] | 193 | #endif |