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