builtin-probe.c 6.2 KB

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