builtin-probe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /* Default vmlinux search paths */
  43. #define NR_SEARCH_PATH 3
  44. const char *default_search_path[NR_SEARCH_PATH] = {
  45. "/lib/modules/%s/build/vmlinux", /* Custom build kernel */
  46. "/usr/lib/debug/lib/modules/%s/vmlinux", /* Red Hat debuginfo */
  47. "/boot/vmlinux-debug-%s", /* Ubuntu */
  48. };
  49. #define MAX_PATH_LEN 256
  50. #define MAX_PROBES 128
  51. #define MAX_PROBE_ARGS 128
  52. #define PERFPROBE_GROUP "probe"
  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. #define semantic_error(msg ...) die("Semantic error :" msg)
  62. /* Parse probe point. Return 1 if return probe */
  63. static void parse_probe_point(char *arg, struct probe_point *pp)
  64. {
  65. char *ptr, *tmp;
  66. char c, nc = 0;
  67. /*
  68. * <Syntax>
  69. * perf probe SRC:LN
  70. * perf probe FUNC[+OFFS|%return][@SRC]
  71. */
  72. ptr = strpbrk(arg, ":+@%");
  73. if (ptr) {
  74. nc = *ptr;
  75. *ptr++ = '\0';
  76. }
  77. /* Check arg is function or file and copy it */
  78. if (strchr(arg, '.')) /* File */
  79. pp->file = strdup(arg);
  80. else /* Function */
  81. pp->function = strdup(arg);
  82. DIE_IF(pp->file == NULL && pp->function == NULL);
  83. /* Parse other options */
  84. while (ptr) {
  85. arg = ptr;
  86. c = nc;
  87. ptr = strpbrk(arg, ":+@%");
  88. if (ptr) {
  89. nc = *ptr;
  90. *ptr++ = '\0';
  91. }
  92. switch (c) {
  93. case ':': /* Line number */
  94. pp->line = strtoul(arg, &tmp, 0);
  95. if (*tmp != '\0')
  96. semantic_error("There is non-digit charactor"
  97. " in line number.");
  98. break;
  99. case '+': /* Byte offset from a symbol */
  100. pp->offset = strtoul(arg, &tmp, 0);
  101. if (*tmp != '\0')
  102. semantic_error("There is non-digit charactor"
  103. " in offset.");
  104. break;
  105. case '@': /* File name */
  106. if (pp->file)
  107. semantic_error("SRC@SRC is not allowed.");
  108. pp->file = strdup(arg);
  109. DIE_IF(pp->file == NULL);
  110. if (ptr)
  111. semantic_error("@SRC must be the last "
  112. "option.");
  113. break;
  114. case '%': /* Probe places */
  115. if (strcmp(arg, "return") == 0) {
  116. pp->retprobe = 1;
  117. } else /* Others not supported yet */
  118. semantic_error("%%%s is not supported.", arg);
  119. break;
  120. default:
  121. DIE_IF("Program has a bug.");
  122. break;
  123. }
  124. }
  125. /* Exclusion check */
  126. if (pp->line && pp->offset)
  127. semantic_error("Offset can't be used with line number.");
  128. if (!pp->line && pp->file && !pp->function)
  129. semantic_error("File always requires line number.");
  130. if (pp->offset && !pp->function)
  131. semantic_error("Offset requires an entry function.");
  132. if (pp->retprobe && !pp->function)
  133. semantic_error("Return probe requires an entry function.");
  134. if ((pp->offset || pp->line) && pp->retprobe)
  135. semantic_error("Offset/Line can't be used with return probe.");
  136. pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
  137. pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
  138. }
  139. /* Parse an event definition. Note that any error must die. */
  140. static void parse_probe_event(const char *str)
  141. {
  142. char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
  143. int argc, i;
  144. struct probe_point *pp = &session.probes[session.nr_probe];
  145. pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
  146. if (++session.nr_probe == MAX_PROBES)
  147. semantic_error("Too many probes");
  148. /* Separate arguments, similar to argv_split */
  149. argc = 0;
  150. do {
  151. /* Skip separators */
  152. while (isspace(*str))
  153. str++;
  154. /* Add an argument */
  155. if (*str != '\0') {
  156. const char *s = str;
  157. /* Skip the argument */
  158. while (!isspace(*str) && *str != '\0')
  159. str++;
  160. /* Duplicate the argument */
  161. argv[argc] = strndup(s, str - s);
  162. if (argv[argc] == NULL)
  163. die("strndup");
  164. if (++argc == MAX_PROBE_ARGS)
  165. semantic_error("Too many arguments");
  166. pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]);
  167. }
  168. } while (*str != '\0');
  169. if (!argc)
  170. semantic_error("An empty argument.");
  171. /* Parse probe point */
  172. parse_probe_point(argv[0], pp);
  173. free(argv[0]);
  174. if (pp->file || pp->line)
  175. session.need_dwarf = 1;
  176. /* Copy arguments */
  177. pp->nr_args = argc - 1;
  178. if (pp->nr_args > 0) {
  179. pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
  180. if (!pp->args)
  181. die("malloc");
  182. memcpy(pp->args, &argv[1], sizeof(char *) * pp->nr_args);
  183. }
  184. /* Ensure return probe has no C argument */
  185. for (i = 0; i < pp->nr_args; i++)
  186. if (is_c_varname(pp->args[i])) {
  187. if (pp->retprobe)
  188. semantic_error("You can't specify local"
  189. " variable for kretprobe");
  190. session.need_dwarf = 1;
  191. }
  192. pr_debug("%d arguments\n", pp->nr_args);
  193. }
  194. static int opt_add_probe_event(const struct option *opt __used,
  195. const char *str, int unset __used)
  196. {
  197. if (str)
  198. parse_probe_event(str);
  199. return 0;
  200. }
  201. #ifndef NO_LIBDWARF
  202. static int open_default_vmlinux(void)
  203. {
  204. struct utsname uts;
  205. char fname[MAX_PATH_LEN];
  206. int fd, ret, i;
  207. ret = uname(&uts);
  208. if (ret) {
  209. pr_debug("uname() failed.\n");
  210. return -errno;
  211. }
  212. session.release = uts.release;
  213. for (i = 0; i < NR_SEARCH_PATH; i++) {
  214. ret = snprintf(fname, MAX_PATH_LEN,
  215. default_search_path[i], session.release);
  216. if (ret >= MAX_PATH_LEN || ret < 0) {
  217. pr_debug("Filename(%d,%s) is too long.\n", i,
  218. uts.release);
  219. errno = E2BIG;
  220. return -E2BIG;
  221. }
  222. pr_debug("try to open %s\n", fname);
  223. fd = open(fname, O_RDONLY);
  224. if (fd >= 0)
  225. break;
  226. }
  227. return fd;
  228. }
  229. #endif
  230. static const char * const probe_usage[] = {
  231. "perf probe [<options>] 'PROBEDEF' ['PROBEDEF' ...]",
  232. "perf probe [<options>] --add 'PROBEDEF' [--add 'PROBEDEF' ...]",
  233. NULL
  234. };
  235. static const struct option options[] = {
  236. OPT_BOOLEAN('v', "verbose", &verbose,
  237. "be more verbose (show parsed arguments, etc)"),
  238. #ifndef NO_LIBDWARF
  239. OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
  240. "vmlinux/module pathname"),
  241. #endif
  242. OPT_CALLBACK('a', "add", NULL,
  243. #ifdef NO_LIBDWARF
  244. "FUNC[+OFFS|%return] [ARG ...]",
  245. #else
  246. "FUNC[+OFFS|%return|:RLN][@SRC]|SRC:ALN [ARG ...]",
  247. #endif
  248. "probe point definition, where\n"
  249. "\t\tGRP:\tGroup name (optional)\n"
  250. "\t\tNAME:\tEvent name\n"
  251. "\t\tFUNC:\tFunction name\n"
  252. "\t\tOFFS:\tOffset from function entry (in byte)\n"
  253. "\t\t%return:\tPut the probe at function return\n"
  254. #ifdef NO_LIBDWARF
  255. "\t\tARG:\tProbe argument (only \n"
  256. #else
  257. "\t\tSRC:\tSource code path\n"
  258. "\t\tRLN:\tRelative line number from function entry.\n"
  259. "\t\tALN:\tAbsolute line number in file.\n"
  260. "\t\tARG:\tProbe argument (local variable name or\n"
  261. #endif
  262. "\t\t\tkprobe-tracer argument format is supported.)\n",
  263. opt_add_probe_event),
  264. OPT_END()
  265. };
  266. static int write_new_event(int fd, const char *buf)
  267. {
  268. int ret;
  269. ret = write(fd, buf, strlen(buf));
  270. if (ret <= 0)
  271. die("Failed to create event.");
  272. else
  273. printf("Added new event: %s\n", buf);
  274. return ret;
  275. }
  276. #define MAX_CMDLEN 256
  277. static int synthesize_probe_event(struct probe_point *pp)
  278. {
  279. char *buf;
  280. int i, len, ret;
  281. pp->probes[0] = buf = zalloc(MAX_CMDLEN);
  282. if (!buf)
  283. die("Failed to allocate memory by zalloc.");
  284. ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
  285. if (ret <= 0 || ret >= MAX_CMDLEN)
  286. goto error;
  287. len = ret;
  288. for (i = 0; i < pp->nr_args; i++) {
  289. ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  290. pp->args[i]);
  291. if (ret <= 0 || ret >= MAX_CMDLEN - len)
  292. goto error;
  293. len += ret;
  294. }
  295. pp->found = 1;
  296. return pp->found;
  297. error:
  298. free(pp->probes[0]);
  299. if (ret > 0)
  300. ret = -E2BIG;
  301. return ret;
  302. }
  303. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  304. {
  305. int i, j, fd, ret;
  306. struct probe_point *pp;
  307. char buf[MAX_CMDLEN];
  308. argc = parse_options(argc, argv, options, probe_usage,
  309. PARSE_OPT_STOP_AT_NON_OPTION);
  310. for (i = 0; i < argc; i++)
  311. parse_probe_event(argv[i]);
  312. if (session.nr_probe == 0)
  313. usage_with_options(probe_usage, options);
  314. if (session.need_dwarf)
  315. #ifdef NO_LIBDWARF
  316. semantic_error("Debuginfo-analysis is not supported");
  317. #else /* !NO_LIBDWARF */
  318. pr_info("Some probes require debuginfo.\n");
  319. if (session.vmlinux)
  320. fd = open(session.vmlinux, O_RDONLY);
  321. else
  322. fd = open_default_vmlinux();
  323. if (fd < 0) {
  324. if (session.need_dwarf)
  325. die("Could not open vmlinux/module file.");
  326. pr_warning("Could not open vmlinux/module file."
  327. " Try to use symbols.\n");
  328. goto end_dwarf;
  329. }
  330. /* Searching probe points */
  331. for (j = 0; j < session.nr_probe; j++) {
  332. pp = &session.probes[j];
  333. if (pp->found)
  334. continue;
  335. lseek(fd, SEEK_SET, 0);
  336. ret = find_probepoint(fd, pp);
  337. if (ret < 0) {
  338. if (session.need_dwarf)
  339. die("Could not analyze debuginfo.");
  340. pr_warning("An error occurred in debuginfo analysis. Try to use symbols.\n");
  341. break;
  342. }
  343. if (ret == 0) /* No error but failed to find probe point. */
  344. die("No probe point found.");
  345. }
  346. close(fd);
  347. end_dwarf:
  348. #endif /* !NO_LIBDWARF */
  349. /* Synthesize probes without dwarf */
  350. for (j = 0; j < session.nr_probe; j++) {
  351. pp = &session.probes[j];
  352. if (pp->found) /* This probe is already found. */
  353. continue;
  354. ret = synthesize_probe_event(pp);
  355. if (ret == -E2BIG)
  356. semantic_error("probe point is too long.");
  357. else if (ret < 0)
  358. die("Failed to synthesize a probe point.");
  359. }
  360. /* Settng up probe points */
  361. snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
  362. fd = open(buf, O_WRONLY, O_APPEND);
  363. if (fd < 0) {
  364. if (errno == ENOENT)
  365. die("kprobe_events file does not exist - please rebuild with CONFIG_KPROBE_TRACER.");
  366. else
  367. die("Could not open kprobe_events file: %s",
  368. strerror(errno));
  369. }
  370. for (j = 0; j < session.nr_probe; j++) {
  371. pp = &session.probes[j];
  372. if (pp->found == 1) {
  373. snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n",
  374. pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP,
  375. pp->function, pp->offset, pp->probes[0]);
  376. write_new_event(fd, buf);
  377. } else
  378. for (i = 0; i < pp->found; i++) {
  379. snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n",
  380. pp->retprobe ? 'r' : 'p',
  381. PERFPROBE_GROUP,
  382. pp->function, pp->offset, i,
  383. pp->probes[0]);
  384. write_new_event(fd, buf);
  385. }
  386. }
  387. close(fd);
  388. return 0;
  389. }