builtin-probe.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 list_events;
  49. bool force_add;
  50. bool show_lines;
  51. int nevents;
  52. struct perf_probe_event events[MAX_PROBES];
  53. struct strlist *dellist;
  54. struct line_range line_range;
  55. } params;
  56. /* Parse an event definition. Note that any error must die. */
  57. static void parse_probe_event(const char *str)
  58. {
  59. struct perf_probe_event *pev = &params.events[params.nevents];
  60. pr_debug("probe-definition(%d): %s\n", params.nevents, str);
  61. if (++params.nevents == MAX_PROBES)
  62. die("Too many probes (> %d) are specified.", MAX_PROBES);
  63. /* Parse a perf-probe command into event */
  64. parse_perf_probe_command(str, pev);
  65. pr_debug("%d arguments\n", pev->nargs);
  66. }
  67. static void parse_probe_event_argv(int argc, const char **argv)
  68. {
  69. int i, len;
  70. char *buf;
  71. /* Bind up rest arguments */
  72. len = 0;
  73. for (i = 0; i < argc; i++)
  74. len += strlen(argv[i]) + 1;
  75. buf = xzalloc(len + 1);
  76. len = 0;
  77. for (i = 0; i < argc; i++)
  78. len += sprintf(&buf[len], "%s ", argv[i]);
  79. parse_probe_event(buf);
  80. free(buf);
  81. }
  82. static int opt_add_probe_event(const struct option *opt __used,
  83. const char *str, int unset __used)
  84. {
  85. if (str)
  86. parse_probe_event(str);
  87. return 0;
  88. }
  89. static int opt_del_probe_event(const struct option *opt __used,
  90. const char *str, int unset __used)
  91. {
  92. if (str) {
  93. if (!params.dellist)
  94. params.dellist = strlist__new(true, NULL);
  95. strlist__add(params.dellist, str);
  96. }
  97. return 0;
  98. }
  99. #ifdef DWARF_SUPPORT
  100. static int opt_show_lines(const struct option *opt __used,
  101. const char *str, int unset __used)
  102. {
  103. if (str)
  104. parse_line_range_desc(str, &params.line_range);
  105. INIT_LIST_HEAD(&params.line_range.line_list);
  106. params.show_lines = true;
  107. return 0;
  108. }
  109. #endif
  110. static const char * const probe_usage[] = {
  111. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  112. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  113. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  114. "perf probe --list",
  115. #ifdef DWARF_SUPPORT
  116. "perf probe --line 'LINEDESC'",
  117. #endif
  118. NULL
  119. };
  120. static const struct option options[] = {
  121. OPT_INCR('v', "verbose", &verbose,
  122. "be more verbose (show parsed arguments, etc)"),
  123. OPT_BOOLEAN('l', "list", &params.list_events,
  124. "list up current probe events"),
  125. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  126. opt_del_probe_event),
  127. OPT_CALLBACK('a', "add", NULL,
  128. #ifdef DWARF_SUPPORT
  129. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  130. " [[NAME=]ARG ...]",
  131. #else
  132. "[EVENT=]FUNC[+OFF|%return] [[NAME=]ARG ...]",
  133. #endif
  134. "probe point definition, where\n"
  135. "\t\tGROUP:\tGroup name (optional)\n"
  136. "\t\tEVENT:\tEvent name\n"
  137. "\t\tFUNC:\tFunction name\n"
  138. "\t\tOFF:\tOffset from function entry (in byte)\n"
  139. "\t\t%return:\tPut the probe at function return\n"
  140. #ifdef DWARF_SUPPORT
  141. "\t\tSRC:\tSource code path\n"
  142. "\t\tRL:\tRelative line number from function entry.\n"
  143. "\t\tAL:\tAbsolute line number in file.\n"
  144. "\t\tPT:\tLazy expression of line code.\n"
  145. "\t\tARG:\tProbe argument (local variable name or\n"
  146. "\t\t\tkprobe-tracer argument format.)\n",
  147. #else
  148. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  149. #endif
  150. opt_add_probe_event),
  151. OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
  152. " with existing name"),
  153. #ifdef DWARF_SUPPORT
  154. OPT_CALLBACK('L', "line", NULL,
  155. "FUNC[:RLN[+NUM|-RLN2]]|SRC:ALN[+NUM|-ALN2]",
  156. "Show source code lines.", opt_show_lines),
  157. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  158. "file", "vmlinux pathname"),
  159. #endif
  160. OPT__DRY_RUN(&probe_event_dry_run),
  161. OPT_END()
  162. };
  163. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  164. {
  165. argc = parse_options(argc, argv, options, probe_usage,
  166. PARSE_OPT_STOP_AT_NON_OPTION);
  167. if (argc > 0) {
  168. if (strcmp(argv[0], "-") == 0) {
  169. pr_warning(" Error: '-' is not supported.\n");
  170. usage_with_options(probe_usage, options);
  171. }
  172. parse_probe_event_argv(argc, argv);
  173. }
  174. if ((!params.nevents && !params.dellist && !params.list_events &&
  175. !params.show_lines))
  176. usage_with_options(probe_usage, options);
  177. if (debugfs_valid_mountpoint(debugfs_path) < 0)
  178. die("Failed to find debugfs path.");
  179. if (params.list_events) {
  180. if (params.nevents != 0 || params.dellist) {
  181. pr_warning(" Error: Don't use --list with"
  182. " --add/--del.\n");
  183. usage_with_options(probe_usage, options);
  184. }
  185. if (params.show_lines) {
  186. pr_warning(" Error: Don't use --list with --line.\n");
  187. usage_with_options(probe_usage, options);
  188. }
  189. show_perf_probe_events();
  190. return 0;
  191. }
  192. #ifdef DWARF_SUPPORT
  193. if (params.show_lines) {
  194. if (params.nevents != 0 || params.dellist) {
  195. pr_warning(" Error: Don't use --line with"
  196. " --add/--del.\n");
  197. usage_with_options(probe_usage, options);
  198. }
  199. show_line_range(&params.line_range);
  200. return 0;
  201. }
  202. #endif
  203. if (params.dellist) {
  204. del_perf_probe_events(params.dellist);
  205. strlist__delete(params.dellist);
  206. if (params.nevents == 0)
  207. return 0;
  208. }
  209. add_perf_probe_events(params.events, params.nevents, params.force_add);
  210. return 0;
  211. }