builtin-probe.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/symbol.h"
  41. #include "util/thread.h"
  42. #include "util/session.h"
  43. #include "util/parse-options.h"
  44. #include "util/parse-events.h" /* For debugfs_path */
  45. #include "util/probe-finder.h"
  46. #include "util/probe-event.h"
  47. #define MAX_PATH_LEN 256
  48. #define MAX_PROBES 128
  49. /* Session management structure */
  50. static struct {
  51. bool need_dwarf;
  52. bool list_events;
  53. bool force_add;
  54. int nr_probe;
  55. struct probe_point probes[MAX_PROBES];
  56. struct strlist *dellist;
  57. struct perf_session *psession;
  58. struct map *kmap;
  59. } session;
  60. /* Parse an event definition. Note that any error must die. */
  61. static void parse_probe_event(const char *str)
  62. {
  63. struct probe_point *pp = &session.probes[session.nr_probe];
  64. pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
  65. if (++session.nr_probe == MAX_PROBES)
  66. die("Too many probes (> %d) are specified.", MAX_PROBES);
  67. /* Parse perf-probe event into probe_point */
  68. parse_perf_probe_event(str, pp, &session.need_dwarf);
  69. pr_debug("%d arguments\n", pp->nr_args);
  70. }
  71. static void parse_probe_event_argv(int argc, const char **argv)
  72. {
  73. int i, len;
  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)
  81. die("Failed to allocate memory for binding arguments.");
  82. len = 0;
  83. for (i = 0; i < argc; i++)
  84. len += sprintf(&buf[len], "%s ", argv[i]);
  85. parse_probe_event(buf);
  86. free(buf);
  87. }
  88. static int opt_add_probe_event(const struct option *opt __used,
  89. const char *str, int unset __used)
  90. {
  91. if (str)
  92. parse_probe_event(str);
  93. return 0;
  94. }
  95. static int opt_del_probe_event(const struct option *opt __used,
  96. const char *str, int unset __used)
  97. {
  98. if (str) {
  99. if (!session.dellist)
  100. session.dellist = strlist__new(true, NULL);
  101. strlist__add(session.dellist, str);
  102. }
  103. return 0;
  104. }
  105. /* Currently just checking function name from symbol map */
  106. static void evaluate_probe_point(struct probe_point *pp)
  107. {
  108. struct symbol *sym;
  109. sym = map__find_symbol_by_name(session.kmap, pp->function,
  110. session.psession, NULL);
  111. if (!sym)
  112. die("Kernel symbol \'%s\' not found - probe not added.",
  113. pp->function);
  114. }
  115. #ifndef NO_LIBDWARF
  116. static int open_vmlinux(void)
  117. {
  118. if (map__load(session.kmap, session.psession, NULL) < 0) {
  119. pr_debug("Failed to load kernel map.\n");
  120. return -EINVAL;
  121. }
  122. pr_debug("Try to open %s\n", session.kmap->dso->long_name);
  123. return open(session.kmap->dso->long_name, O_RDONLY);
  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 [<options>] --del '[GROUP:]EVENT' ...",
  130. "perf probe --list",
  131. NULL
  132. };
  133. static const struct option options[] = {
  134. OPT_BOOLEAN('v', "verbose", &verbose,
  135. "be more verbose (show parsed arguments, etc)"),
  136. #ifndef NO_LIBDWARF
  137. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  138. "file", "vmlinux pathname"),
  139. #endif
  140. OPT_BOOLEAN('l', "list", &session.list_events,
  141. "list up current probe events"),
  142. OPT_CALLBACK('d', "del", NULL, "[GROUP:]EVENT", "delete a probe event.",
  143. opt_del_probe_event),
  144. OPT_CALLBACK('a', "add", NULL,
  145. #ifdef NO_LIBDWARF
  146. "[EVENT=]FUNC[+OFFS|%return] [ARG ...]",
  147. #else
  148. "[EVENT=]FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
  149. #endif
  150. "probe point definition, where\n"
  151. "\t\tGROUP:\tGroup name (optional)\n"
  152. "\t\tEVENT:\tEvent name\n"
  153. "\t\tFUNC:\tFunction name\n"
  154. "\t\tOFFS:\tOffset from function entry (in byte)\n"
  155. "\t\t%return:\tPut the probe at function return\n"
  156. #ifdef NO_LIBDWARF
  157. "\t\tARG:\tProbe argument (only \n"
  158. #else
  159. "\t\tSRC:\tSource code path\n"
  160. "\t\tRLN:\tRelative line number from function entry.\n"
  161. "\t\tALN:\tAbsolute line number in file.\n"
  162. "\t\tARG:\tProbe argument (local variable name or\n"
  163. #endif
  164. "\t\t\tkprobe-tracer argument format.)\n",
  165. opt_add_probe_event),
  166. OPT_BOOLEAN('f', "force", &session.force_add, "forcibly add events"
  167. " with existing name"),
  168. OPT_END()
  169. };
  170. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  171. {
  172. int i, ret;
  173. #ifndef NO_LIBDWARF
  174. int fd;
  175. #endif
  176. struct probe_point *pp;
  177. argc = parse_options(argc, argv, options, probe_usage,
  178. PARSE_OPT_STOP_AT_NON_OPTION);
  179. if (argc > 0) {
  180. if (strcmp(argv[0], "-") == 0) {
  181. pr_warning(" Error: '-' is not supported.\n");
  182. usage_with_options(probe_usage, options);
  183. }
  184. parse_probe_event_argv(argc, argv);
  185. }
  186. if ((!session.nr_probe && !session.dellist && !session.list_events))
  187. usage_with_options(probe_usage, options);
  188. if (session.list_events) {
  189. if (session.nr_probe != 0 || session.dellist) {
  190. pr_warning(" Error: Don't use --list with"
  191. " --add/--del.\n");
  192. usage_with_options(probe_usage, options);
  193. }
  194. show_perf_probe_events();
  195. return 0;
  196. }
  197. if (session.dellist) {
  198. del_trace_kprobe_events(session.dellist);
  199. strlist__delete(session.dellist);
  200. if (session.nr_probe == 0)
  201. return 0;
  202. }
  203. /* Initialize symbol maps for vmlinux */
  204. symbol_conf.sort_by_name = true;
  205. if (symbol_conf.vmlinux_name == NULL)
  206. symbol_conf.try_vmlinux_path = true;
  207. if (symbol__init() < 0)
  208. die("Failed to init symbol map.");
  209. session.psession = perf_session__new(NULL, O_WRONLY, false);
  210. if (session.psession == NULL)
  211. die("Failed to init perf_session.");
  212. session.kmap = map_groups__find_by_name(&session.psession->kmaps,
  213. MAP__FUNCTION,
  214. "[kernel.kallsyms]");
  215. if (!session.kmap)
  216. die("Could not find kernel map.\n");
  217. if (session.need_dwarf)
  218. #ifdef NO_LIBDWARF
  219. die("Debuginfo-analysis is not supported");
  220. #else /* !NO_LIBDWARF */
  221. pr_debug("Some probes require debuginfo.\n");
  222. fd = open_vmlinux();
  223. if (fd < 0) {
  224. if (session.need_dwarf)
  225. die("Could not open debuginfo file.");
  226. pr_debug("Could not open vmlinux/module file."
  227. " Try to use symbols.\n");
  228. goto end_dwarf;
  229. }
  230. /* Searching probe points */
  231. for (i = 0; i < session.nr_probe; i++) {
  232. pp = &session.probes[i];
  233. if (pp->found)
  234. continue;
  235. lseek(fd, SEEK_SET, 0);
  236. ret = find_probepoint(fd, pp);
  237. if (ret > 0)
  238. continue;
  239. if (ret == 0) { /* No error but failed to find probe point. */
  240. synthesize_perf_probe_point(pp);
  241. die("Probe point '%s' not found. - probe not added.",
  242. pp->probes[0]);
  243. }
  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. evaluate_probe_point(pp);
  263. ret = synthesize_trace_kprobe_event(pp);
  264. if (ret == -E2BIG)
  265. die("probe point definition becomes too long.");
  266. else if (ret < 0)
  267. die("Failed to synthesize a probe point.");
  268. }
  269. /* Settng up probe points */
  270. add_trace_kprobe_events(session.probes, session.nr_probe,
  271. session.force_add);
  272. return 0;
  273. }