trace-event-scripting.c 4.0 KB

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