builtin-probe.c 6.6 KB

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