builtin-probe.c 7.1 KB

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