builtin-probe.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * builtin-probe.c
  3. *
  4. * Builtin probe command: Set up probe events by C expression
  5. *
  6. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. */
  23. #define _GNU_SOURCE
  24. #include <sys/utsname.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #undef _GNU_SOURCE
  34. #include "perf.h"
  35. #include "builtin.h"
  36. #include "util/util.h"
  37. #include "util/strlist.h"
  38. #include "util/symbol.h"
  39. #include "util/debug.h"
  40. #include "util/debugfs.h"
  41. #include "util/parse-options.h"
  42. #include "util/parse-events.h" /* For debugfs_path */
  43. #include "util/probe-finder.h"
  44. #include "util/probe-event.h"
  45. #define MAX_PATH_LEN 256
  46. /* Session management structure */
  47. static struct {
  48. bool need_dwarf;
  49. bool list_events;
  50. bool force_add;
  51. bool show_lines;
  52. int nr_probe;
  53. struct probe_point probes[MAX_PROBES];
  54. struct strlist *dellist;
  55. struct line_range line_range;
  56. } session;
  57. /* Parse an event definition. Note that any error must die. */
  58. static void parse_probe_event(const char *str)
  59. {
  60. struct probe_point *pp = &session.probes[session.nr_probe];
  61. pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
  62. if (++session.nr_probe == MAX_PROBES)
  63. die("Too many probes (> %d) are specified.", MAX_PROBES);
  64. /* Parse perf-probe event into probe_point */
  65. parse_perf_probe_event(str, pp, &session.need_dwarf);
  66. pr_debug("%d arguments\n", pp->nr_args);
  67. }
  68. static void parse_probe_event_argv(int argc, const char **argv)
  69. {
  70. int i, len;
  71. char *buf;
  72. /* Bind up rest arguments */
  73. len = 0;
  74. for (i = 0; i < argc; i++)
  75. len += strlen(argv[i]) + 1;
  76. buf = xzalloc(len + 1);
  77. len = 0;
  78. for (i = 0; i < argc; i++)
  79. len += sprintf(&buf[len], "%s ", argv[i]);
  80. parse_probe_event(buf);
  81. free(buf);
  82. }
  83. static int opt_add_probe_event(const struct option *opt __used,
  84. const char *str, int unset __used)
  85. {
  86. if (str)
  87. parse_probe_event(str);
  88. return 0;
  89. }
  90. static int opt_del_probe_event(const struct option *opt __used,
  91. const char *str, int unset __used)
  92. {
  93. if (str) {
  94. if (!session.dellist)
  95. session.dellist = strlist__new(true, NULL);
  96. strlist__add(session.dellist, str);
  97. }
  98. return 0;
  99. }
  100. #ifndef NO_DWARF_SUPPORT
  101. static int opt_show_lines(const struct option *opt __used,
  102. const char *str, int unset __used)
  103. {
  104. if (str)
  105. parse_line_range_desc(str, &session.line_range);
  106. INIT_LIST_HEAD(&session.line_range.line_list);
  107. session.show_lines = true;
  108. return 0;
  109. }
  110. #endif
  111. static const char * const probe_usage[] = {
  112. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  113. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  114. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  115. "perf probe --list",
  116. #ifndef NO_DWARF_SUPPORT
  117. "perf probe --line 'LINEDESC'",
  118. #endif
  119. NULL
  120. };
  121. static const struct option options[] = {
  122. OPT_BOOLEAN('v', "verbose", &verbose,
  123. "be more verbose (show parsed arguments, etc)"),
  124. #ifndef NO_DWARF_SUPPORT
  125. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  126. "file", "vmlinux pathname"),
  127. #endif
  128. OPT_BOOLEAN('l', "list", &session.list_events,
  129. "list up current probe events"),
  130. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  131. opt_del_probe_event),
  132. OPT_CALLBACK('a', "add", NULL,
  133. #ifdef NO_DWARF_SUPPORT
  134. "[EVENT=]FUNC[+OFF|%return] [ARG ...]",
  135. #else
  136. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  137. " [ARG ...]",
  138. #endif
  139. "probe point definition, where\n"
  140. "\t\tGROUP:\tGroup name (optional)\n"
  141. "\t\tEVENT:\tEvent name\n"
  142. "\t\tFUNC:\tFunction name\n"
  143. "\t\tOFF:\tOffset from function entry (in byte)\n"
  144. "\t\t%return:\tPut the probe at function return\n"
  145. #ifdef NO_DWARF_SUPPORT
  146. "\t\tARG:\tProbe argument (only \n"
  147. #else
  148. "\t\tSRC:\tSource code path\n"
  149. "\t\tRL:\tRelative line number from function entry.\n"
  150. "\t\tAL:\tAbsolute line number in file.\n"
  151. "\t\tPT:\tLazy expression of line code.\n"
  152. "\t\tARG:\tProbe argument (local variable name or\n"
  153. #endif
  154. "\t\t\tkprobe-tracer argument format.)\n",
  155. opt_add_probe_event),
  156. OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
  157. " with existing name"),
  158. #ifndef NO_DWARF_SUPPORT
  159. OPT_CALLBACK('L', "line", NULL,
  160. "FUNC[:RLN[+NUM|:RLN2]]|SRC:ALN[+NUM|:ALN2]",
  161. "Show source code lines.", opt_show_lines),
  162. #endif
  163. OPT_END()
  164. };
  165. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  166. {
  167. argc = parse_options(argc, argv, options, probe_usage,
  168. PARSE_OPT_STOP_AT_NON_OPTION);
  169. if (argc > 0) {
  170. if (strcmp(argv[0], "-") == 0) {
  171. pr_warning(" Error: '-' is not supported.\n");
  172. usage_with_options(probe_usage, options);
  173. }
  174. parse_probe_event_argv(argc, argv);
  175. }
  176. if ((!session.nr_probe && !session.dellist && !session.list_events &&
  177. !session.show_lines))
  178. usage_with_options(probe_usage, options);
  179. if (debugfs_valid_mountpoint(debugfs_path) < 0)
  180. die("Failed to find debugfs path.");
  181. if (session.list_events) {
  182. if (session.nr_probe != 0 || session.dellist) {
  183. pr_warning(" Error: Don't use --list with"
  184. " --add/--del.\n");
  185. usage_with_options(probe_usage, options);
  186. }
  187. if (session.show_lines) {
  188. pr_warning(" Error: Don't use --list with --line.\n");
  189. usage_with_options(probe_usage, options);
  190. }
  191. show_perf_probe_events();
  192. return 0;
  193. }
  194. #ifndef NO_DWARF_SUPPORT
  195. if (session.show_lines) {
  196. if (session.nr_probe != 0 || session.dellist) {
  197. pr_warning(" Error: Don't use --line with"
  198. " --add/--del.\n");
  199. usage_with_options(probe_usage, options);
  200. }
  201. show_line_range(&session.line_range);
  202. return 0;
  203. }
  204. #endif
  205. if (session.dellist) {
  206. del_trace_kprobe_events(session.dellist);
  207. strlist__delete(session.dellist);
  208. if (session.nr_probe == 0)
  209. return 0;
  210. }
  211. add_trace_kprobe_events(session.probes, session.nr_probe,
  212. session.force_add, session.need_dwarf);
  213. return 0;
  214. }