builtin-probe.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. die("Too many probes (> %d) are specified.", MAX_PROBES);
  64. /* Parse a perf-probe command into event */
  65. ret = parse_perf_probe_command(str, pev);
  66. pr_debug("%d arguments\n", pev->nargs);
  67. return ret;
  68. }
  69. static int parse_probe_event_argv(int argc, const char **argv)
  70. {
  71. int i, len, ret;
  72. char *buf;
  73. /* Bind up rest arguments */
  74. len = 0;
  75. for (i = 0; i < argc; i++)
  76. len += strlen(argv[i]) + 1;
  77. buf = xzalloc(len + 1);
  78. len = 0;
  79. for (i = 0; i < argc; i++)
  80. len += sprintf(&buf[len], "%s ", argv[i]);
  81. ret = parse_probe_event(buf);
  82. free(buf);
  83. return ret;
  84. }
  85. static int opt_add_probe_event(const struct option *opt __used,
  86. const char *str, int unset __used)
  87. {
  88. if (str)
  89. return parse_probe_event(str);
  90. else
  91. return 0;
  92. }
  93. static int opt_del_probe_event(const struct option *opt __used,
  94. const char *str, int unset __used)
  95. {
  96. if (str) {
  97. if (!params.dellist)
  98. params.dellist = strlist__new(true, NULL);
  99. strlist__add(params.dellist, str);
  100. }
  101. return 0;
  102. }
  103. #ifdef DWARF_SUPPORT
  104. static int opt_show_lines(const struct option *opt __used,
  105. const char *str, int unset __used)
  106. {
  107. int ret = 0;
  108. if (str)
  109. ret = parse_line_range_desc(str, &params.line_range);
  110. INIT_LIST_HEAD(&params.line_range.line_list);
  111. params.show_lines = true;
  112. return ret;
  113. }
  114. #endif
  115. static const char * const probe_usage[] = {
  116. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  117. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  118. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  119. "perf probe --list",
  120. #ifdef DWARF_SUPPORT
  121. "perf probe --line 'LINEDESC'",
  122. #endif
  123. NULL
  124. };
  125. static const struct option options[] = {
  126. OPT_INCR('v', "verbose", &verbose,
  127. "be more verbose (show parsed arguments, etc)"),
  128. OPT_BOOLEAN('l', "list", &params.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 DWARF_SUPPORT
  134. "[EVENT=]FUNC[@SRC][+OFF|%return|:RL|;PT]|SRC:AL|SRC;PT"
  135. " [[NAME=]ARG ...]",
  136. #else
  137. "[EVENT=]FUNC[+OFF|%return] [[NAME=]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 DWARF_SUPPORT
  146. "\t\tSRC:\tSource code path\n"
  147. "\t\tRL:\tRelative line number from function entry.\n"
  148. "\t\tAL:\tAbsolute line number in file.\n"
  149. "\t\tPT:\tLazy expression of line code.\n"
  150. "\t\tARG:\tProbe argument (local variable name or\n"
  151. "\t\t\tkprobe-tracer argument format.)\n",
  152. #else
  153. "\t\tARG:\tProbe argument (kprobe-tracer argument format.)\n",
  154. #endif
  155. opt_add_probe_event),
  156. OPT_BOOLEAN('f', "force", &params.force_add, "forcibly add events"
  157. " with existing name"),
  158. #ifdef 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. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  163. "file", "vmlinux pathname"),
  164. #endif
  165. OPT__DRY_RUN(&probe_event_dry_run),
  166. OPT_INTEGER('\0', "max-probes", &params.max_probe_points,
  167. "Set how many probe points can be found for a probe."),
  168. OPT_END()
  169. };
  170. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  171. {
  172. int ret;
  173. argc = parse_options(argc, argv, options, probe_usage,
  174. PARSE_OPT_STOP_AT_NON_OPTION);
  175. if (argc > 0) {
  176. if (strcmp(argv[0], "-") == 0) {
  177. pr_warning(" Error: '-' is not supported.\n");
  178. usage_with_options(probe_usage, options);
  179. }
  180. ret = parse_probe_event_argv(argc, argv);
  181. if (ret < 0) {
  182. pr_err(" Error: Parse Error. (%d)\n", ret);
  183. return ret;
  184. }
  185. }
  186. if (params.max_probe_points == 0)
  187. params.max_probe_points = MAX_PROBES;
  188. if ((!params.nevents && !params.dellist && !params.list_events &&
  189. !params.show_lines))
  190. usage_with_options(probe_usage, options);
  191. if (params.list_events) {
  192. if (params.nevents != 0 || params.dellist) {
  193. pr_err(" Error: Don't use --list with --add/--del.\n");
  194. usage_with_options(probe_usage, options);
  195. }
  196. if (params.show_lines) {
  197. pr_err(" Error: Don't use --list with --line.\n");
  198. usage_with_options(probe_usage, options);
  199. }
  200. ret = show_perf_probe_events();
  201. if (ret < 0)
  202. pr_err(" Error: Failed to show event list. (%d)\n",
  203. ret);
  204. return ret;
  205. }
  206. #ifdef DWARF_SUPPORT
  207. if (params.show_lines) {
  208. if (params.nevents != 0 || params.dellist) {
  209. pr_warning(" Error: Don't use --line with"
  210. " --add/--del.\n");
  211. usage_with_options(probe_usage, options);
  212. }
  213. ret = show_line_range(&params.line_range);
  214. if (ret < 0)
  215. pr_err(" Error: Failed to show lines. (%d)\n", ret);
  216. return ret;
  217. }
  218. #endif
  219. if (params.dellist) {
  220. ret = del_perf_probe_events(params.dellist);
  221. strlist__delete(params.dellist);
  222. if (ret < 0) {
  223. pr_err(" Error: Failed to delete events. (%d)\n", ret);
  224. return ret;
  225. }
  226. }
  227. if (params.nevents) {
  228. ret = add_perf_probe_events(params.events, params.nevents,
  229. params.force_add,
  230. params.max_probe_points);
  231. if (ret < 0) {
  232. pr_err(" Error: Failed to add events. (%d)\n", ret);
  233. return ret;
  234. }
  235. }
  236. return 0;
  237. }