builtin-probe.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/event.h"
  39. #include "util/debug.h"
  40. #include "util/parse-options.h"
  41. #include "util/parse-events.h" /* For debugfs_path */
  42. #include "util/probe-finder.h"
  43. #include "util/probe-event.h"
  44. /* Default vmlinux search paths */
  45. #define NR_SEARCH_PATH 4
  46. const char *default_search_path[NR_SEARCH_PATH] = {
  47. "/lib/modules/%s/build/vmlinux", /* Custom build kernel */
  48. "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */
  49. "/boot/vmlinux-debug-%s", /* Ubuntu */
  50. "./vmlinux", /* CWD */
  51. };
  52. #define MAX_PATH_LEN 256
  53. #define MAX_PROBES 128
  54. /* Session management structure */
  55. static struct {
  56. char *vmlinux;
  57. char *release;
  58. bool need_dwarf;
  59. bool list_events;
  60. bool force_add;
  61. int nr_probe;
  62. struct probe_point probes[MAX_PROBES];
  63. struct strlist *dellist;
  64. } session;
  65. /* Parse an event definition. Note that any error must die. */
  66. static void parse_probe_event(const char *str)
  67. {
  68. struct probe_point *pp = &session.probes[session.nr_probe];
  69. pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
  70. if (++session.nr_probe == MAX_PROBES)
  71. die("Too many probes (> %d) are specified.", MAX_PROBES);
  72. /* Parse perf-probe event into probe_point */
  73. parse_perf_probe_event(str, pp, &session.need_dwarf);
  74. pr_debug("%d arguments\n", pp->nr_args);
  75. }
  76. static void parse_probe_event_argv(int argc, const char **argv)
  77. {
  78. int i, len;
  79. char *buf;
  80. /* Bind up rest arguments */
  81. len = 0;
  82. for (i = 0; i < argc; i++)
  83. len += strlen(argv[i]) + 1;
  84. buf = zalloc(len + 1);
  85. if (!buf)
  86. die("Failed to allocate memory for binding arguments.");
  87. len = 0;
  88. for (i = 0; i < argc; i++)
  89. len += sprintf(&buf[len], "%s ", argv[i]);
  90. parse_probe_event(buf);
  91. free(buf);
  92. }
  93. static int opt_add_probe_event(const struct option *opt __used,
  94. const char *str, int unset __used)
  95. {
  96. if (str)
  97. parse_probe_event(str);
  98. return 0;
  99. }
  100. static int opt_del_probe_event(const struct option *opt __used,
  101. const char *str, int unset __used)
  102. {
  103. if (str) {
  104. if (!session.dellist)
  105. session.dellist = strlist__new(true, NULL);
  106. strlist__add(session.dellist, str);
  107. }
  108. return 0;
  109. }
  110. #ifndef NO_LIBDWARF
  111. static int open_default_vmlinux(void)
  112. {
  113. struct utsname uts;
  114. char fname[MAX_PATH_LEN];
  115. int fd, ret, i;
  116. ret = uname(&uts);
  117. if (ret) {
  118. pr_debug("uname() failed.\n");
  119. return -errno;
  120. }
  121. session.release = uts.release;
  122. for (i = 0; i < NR_SEARCH_PATH; i++) {
  123. ret = snprintf(fname, MAX_PATH_LEN,
  124. default_search_path[i], session.release);
  125. if (ret >= MAX_PATH_LEN || ret < 0) {
  126. pr_debug("Filename(%d,%s) is too long.\n", i,
  127. uts.release);
  128. errno = E2BIG;
  129. return -E2BIG;
  130. }
  131. pr_debug("try to open %s\n", fname);
  132. fd = open(fname, O_RDONLY);
  133. if (fd >= 0)
  134. break;
  135. }
  136. return fd;
  137. }
  138. #endif
  139. static const char * const probe_usage[] = {
  140. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  141. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  142. "perf probe [<options>] --del '[GROUP:]EVENT' ...",
  143. "perf probe --list",
  144. NULL
  145. };
  146. static const struct option options[] = {
  147. OPT_BOOLEAN('v', "verbose", &verbose,
  148. "be more verbose (show parsed arguments, etc)"),
  149. #ifndef NO_LIBDWARF
  150. OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
  151. "vmlinux/module pathname"),
  152. #endif
  153. OPT_BOOLEAN('l', "list", &session.list_events,
  154. "list up current probe events"),
  155. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  156. opt_del_probe_event),
  157. OPT_CALLBACK('a', "add", NULL,
  158. #ifdef NO_LIBDWARF
  159. "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
  160. #else
  161. "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
  162. #endif
  163. "probe point definition, where\n"
  164. "\t\tGROUP:\tGroup name (optional)\n"
  165. "\t\tEVENT:\tEvent name\n"
  166. "\t\tFUNC:\tFunction name\n"
  167. "\t\tOFFS:\tOffset from function entry (in byte)\n"
  168. "\t\t%return:\tPut the probe at function return\n"
  169. #ifdef NO_LIBDWARF
  170. "\t\tARG:\tProbe argument (only \n"
  171. #else
  172. "\t\tSRC:\tSource code path\n"
  173. "\t\tRLN:\tRelative line number from function entry.\n"
  174. "\t\tALN:\tAbsolute line number in file.\n"
  175. "\t\tARG:\tProbe argument (local variable name or\n"
  176. #endif
  177. "\t\t\tkprobe-tracer argument format.)\n",
  178. opt_add_probe_event),
  179. OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
  180. " with existing name"),
  181. OPT_END()
  182. };
  183. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  184. {
  185. int i, ret;
  186. #ifndef NO_LIBDWARF
  187. int fd;
  188. #endif
  189. struct probe_point *pp;
  190. argc = parse_options(argc, argv, options, probe_usage,
  191. PARSE_OPT_STOP_AT_NON_OPTION);
  192. if (argc > 0) {
  193. if (strcmp(argv[0], "-") == 0) {
  194. pr_warning(" Error: '-' is not supported.\n");
  195. usage_with_options(probe_usage, options);
  196. }
  197. parse_probe_event_argv(argc, argv);
  198. }
  199. if ((!session.nr_probe && !session.dellist && !session.list_events))
  200. usage_with_options(probe_usage, options);
  201. if (session.list_events) {
  202. if (session.nr_probe != 0 || session.dellist) {
  203. pr_warning(" Error: Don't use --list with"
  204. " --add/--del.\n");
  205. usage_with_options(probe_usage, options);
  206. }
  207. show_perf_probe_events();
  208. return 0;
  209. }
  210. if (session.dellist) {
  211. del_trace_kprobe_events(session.dellist);
  212. strlist__delete(session.dellist);
  213. if (session.nr_probe == 0)
  214. return 0;
  215. }
  216. if (session.need_dwarf)
  217. #ifdef NO_LIBDWARF
  218. die("Debuginfo-analysis is not supported");
  219. #else /* !NO_LIBDWARF */
  220. pr_debug("Some probes require debuginfo.\n");
  221. if (session.vmlinux) {
  222. pr_debug("Try to open %s.", session.vmlinux);
  223. fd = open(session.vmlinux, O_RDONLY);
  224. } else
  225. fd = open_default_vmlinux();
  226. if (fd < 0) {
  227. if (session.need_dwarf)
  228. die("Could not open debuginfo file.");
  229. pr_debug("Could not open vmlinux/module file."
  230. " Try to use symbols.\n");
  231. goto end_dwarf;
  232. }
  233. /* Searching probe points */
  234. for (i = 0; i < session.nr_probe; i++) {
  235. pp = &session.probes[i];
  236. if (pp->found)
  237. continue;
  238. lseek(fd, SEEK_SET, 0);
  239. ret = find_probepoint(fd, pp);
  240. if (ret > 0)
  241. continue;
  242. if (ret == 0) /* No error but failed to find probe point. */
  243. die("No probe point found.");
  244. /* Error path */
  245. if (session.need_dwarf) {
  246. if (ret == -ENOENT)
  247. pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
  248. die("Could not analyze debuginfo.");
  249. }
  250. pr_debug("An error occurred in debuginfo analysis."
  251. " Try to use symbols.\n");
  252. break;
  253. }
  254. close(fd);
  255. end_dwarf:
  256. #endif /* !NO_LIBDWARF */
  257. /* Synthesize probes without dwarf */
  258. for (i = 0; i < session.nr_probe; i++) {
  259. pp = &session.probes[i];
  260. if (pp->found) /* This probe is already found. */
  261. continue;
  262. ret = synthesize_trace_kprobe_event(pp);
  263. if (ret == -E2BIG)
  264. die("probe point definition becomes too long.");
  265. else if (ret < 0)
  266. die("Failed to synthesize a probe point.");
  267. }
  268. /* Settng up probe points */
  269. add_trace_kprobe_events(session.probes, session.nr_probe,
  270. session.force_add);
  271. return 0;
  272. }