builtin-probe.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. /* 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. char *events[MAX_PROBES];
  60. } session;
  61. #define semantic_error(msg ...) die("Semantic error :" msg)
  62. static int parse_probepoint(const struct option *opt __used,
  63. const char *str, int unset __used)
  64. {
  65. char *argv[MAX_PROBE_ARGS + 2]; /* Event + probe + args */
  66. int argc, i;
  67. char *arg, *ptr;
  68. struct probe_point *pp = &session.probes[session.nr_probe];
  69. char **event = &session.events[session.nr_probe];
  70. int retp = 0;
  71. if (!str) /* The end of probe points */
  72. return 0;
  73. pr_debug("probe-definition(%d): %s\n", session.nr_probe, str);
  74. if (++session.nr_probe == MAX_PROBES)
  75. semantic_error("Too many probes");
  76. /* Separate arguments, similar to argv_split */
  77. argc = 0;
  78. do {
  79. /* Skip separators */
  80. while (isspace(*str))
  81. str++;
  82. /* Add an argument */
  83. if (*str != '\0') {
  84. const char *s = str;
  85. /* Skip the argument */
  86. while (!isspace(*str) && *str != '\0')
  87. str++;
  88. /* Duplicate the argument */
  89. argv[argc] = strndup(s, str - s);
  90. if (argv[argc] == NULL)
  91. die("strndup");
  92. if (++argc == MAX_PROBE_ARGS)
  93. semantic_error("Too many arguments");
  94. pr_debug("argv[%d]=%s\n", argc, argv[argc - 1]);
  95. }
  96. } while (*str != '\0');
  97. if (argc < 2)
  98. semantic_error("Need event-name and probe-point at least.");
  99. /* Parse the event name */
  100. if (argv[0][0] == 'r')
  101. retp = 1;
  102. else if (argv[0][0] != 'p')
  103. semantic_error("You must specify 'p'(kprobe) or"
  104. " 'r'(kretprobe) first.");
  105. /* TODO: check event name */
  106. *event = argv[0];
  107. /* Parse probe point */
  108. arg = argv[1];
  109. if (arg[0] == '@') {
  110. /* Source Line */
  111. arg++;
  112. ptr = strchr(arg, ':');
  113. if (!ptr || !isdigit(ptr[1]))
  114. semantic_error("Line number is required.");
  115. *ptr++ = '\0';
  116. if (strlen(arg) == 0)
  117. semantic_error("No file name.");
  118. pp->file = strdup(arg);
  119. pp->line = atoi(ptr);
  120. if (!pp->file || !pp->line)
  121. semantic_error("Failed to parse line.");
  122. pr_debug("file:%s line:%d\n", pp->file, pp->line);
  123. } else {
  124. /* Function name */
  125. ptr = strchr(arg, '+');
  126. if (ptr) {
  127. if (!isdigit(ptr[1]))
  128. semantic_error("Offset is required.");
  129. *ptr++ = '\0';
  130. pp->offset = atoi(ptr);
  131. } else
  132. ptr = arg;
  133. ptr = strchr(ptr, '@');
  134. if (ptr) {
  135. *ptr++ = '\0';
  136. pp->file = strdup(ptr);
  137. }
  138. pp->function = strdup(arg);
  139. pr_debug("symbol:%s file:%s offset:%d\n",
  140. pp->function, pp->file, pp->offset);
  141. }
  142. free(argv[1]);
  143. if (pp->file)
  144. session.need_dwarf = 1;
  145. /* Copy arguments */
  146. pp->nr_args = argc - 2;
  147. if (pp->nr_args > 0) {
  148. pp->args = (char **)malloc(sizeof(char *) * pp->nr_args);
  149. if (!pp->args)
  150. die("malloc");
  151. memcpy(pp->args, &argv[2], sizeof(char *) * pp->nr_args);
  152. }
  153. /* Ensure return probe has no C argument */
  154. for (i = 0; i < pp->nr_args; i++)
  155. if (is_c_varname(pp->args[i])) {
  156. if (retp)
  157. semantic_error("You can't specify local"
  158. " variable for kretprobe");
  159. session.need_dwarf = 1;
  160. }
  161. pr_debug("%d arguments\n", pp->nr_args);
  162. return 0;
  163. }
  164. #ifndef NO_LIBDWARF
  165. static int open_default_vmlinux(void)
  166. {
  167. struct utsname uts;
  168. char fname[MAX_PATH_LEN];
  169. int fd, ret, i;
  170. ret = uname(&uts);
  171. if (ret) {
  172. pr_debug("uname() failed.\n");
  173. return -errno;
  174. }
  175. session.release = uts.release;
  176. for (i = 0; i < NR_SEARCH_PATH; i++) {
  177. ret = snprintf(fname, MAX_PATH_LEN,
  178. default_search_path[i], session.release);
  179. if (ret >= MAX_PATH_LEN || ret < 0) {
  180. pr_debug("Filename(%d,%s) is too long.\n", i,
  181. uts.release);
  182. errno = E2BIG;
  183. return -E2BIG;
  184. }
  185. pr_debug("try to open %s\n", fname);
  186. fd = open(fname, O_RDONLY);
  187. if (fd >= 0)
  188. break;
  189. }
  190. return fd;
  191. }
  192. #endif
  193. static const char * const probe_usage[] = {
  194. "perf probe [<options>] -P 'PROBEDEF' [-P 'PROBEDEF' ...]",
  195. NULL
  196. };
  197. static const struct option options[] = {
  198. OPT_BOOLEAN('v', "verbose", &verbose,
  199. "be more verbose (show parsed arguments, etc)"),
  200. #ifndef NO_LIBDWARF
  201. OPT_STRING('k', "vmlinux", &session.vmlinux, "file",
  202. "vmlinux/module pathname"),
  203. #endif
  204. OPT_CALLBACK('P', "probe", NULL,
  205. #ifdef NO_LIBDWARF
  206. "p|r:[GRP/]NAME FUNC[+OFFS] [ARG ...]",
  207. #else
  208. "p|r:[GRP/]NAME FUNC[+OFFS][@SRC]|@SRC:LINE [ARG ...]",
  209. #endif
  210. "probe point definition, where\n"
  211. "\t\tp:\tkprobe probe\n"
  212. "\t\tr:\tkretprobe probe\n"
  213. "\t\tGRP:\tGroup name (optional)\n"
  214. "\t\tNAME:\tEvent name\n"
  215. "\t\tFUNC:\tFunction name\n"
  216. "\t\tOFFS:\tOffset from function entry (in byte)\n"
  217. #ifdef NO_LIBDWARF
  218. "\t\tARG:\tProbe argument (only \n"
  219. #else
  220. "\t\tSRC:\tSource code path\n"
  221. "\t\tLINE:\tLine number\n"
  222. "\t\tARG:\tProbe argument (local variable name or\n"
  223. #endif
  224. "\t\t\tkprobe-tracer argument format is supported.)\n",
  225. parse_probepoint),
  226. OPT_END()
  227. };
  228. static int write_new_event(int fd, const char *buf)
  229. {
  230. int ret;
  231. printf("Adding new event: %s\n", buf);
  232. ret = write(fd, buf, strlen(buf));
  233. if (ret <= 0)
  234. die("failed to create event.");
  235. return ret;
  236. }
  237. #define MAX_CMDLEN 256
  238. static int synthesize_probepoint(struct probe_point *pp)
  239. {
  240. char *buf;
  241. int i, len, ret;
  242. pp->probes[0] = buf = (char *)calloc(MAX_CMDLEN, sizeof(char));
  243. if (!buf)
  244. die("calloc");
  245. ret = snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
  246. if (ret <= 0 || ret >= MAX_CMDLEN)
  247. goto error;
  248. len = ret;
  249. for (i = 0; i < pp->nr_args; i++) {
  250. ret = snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  251. pp->args[i]);
  252. if (ret <= 0 || ret >= MAX_CMDLEN - len)
  253. goto error;
  254. len += ret;
  255. }
  256. pp->found = 1;
  257. return pp->found;
  258. error:
  259. free(pp->probes[0]);
  260. if (ret > 0)
  261. ret = -E2BIG;
  262. return ret;
  263. }
  264. int cmd_probe(int argc, const char **argv, const char *prefix __used)
  265. {
  266. int i, j, fd, ret;
  267. struct probe_point *pp;
  268. char buf[MAX_CMDLEN];
  269. argc = parse_options(argc, argv, options, probe_usage,
  270. PARSE_OPT_STOP_AT_NON_OPTION);
  271. if (argc || session.nr_probe == 0)
  272. usage_with_options(probe_usage, options);
  273. #ifdef NO_LIBDWARF
  274. if (session.need_dwarf)
  275. semantic_error("Dwarf-analysis is not supported");
  276. #endif
  277. /* Synthesize probes without dwarf */
  278. for (j = 0; j < session.nr_probe; j++) {
  279. #ifndef NO_LIBDWARF
  280. if (session.events[j][0] != 'r') {
  281. session.need_dwarf = 1;
  282. continue;
  283. }
  284. #endif
  285. ret = synthesize_probepoint(&session.probes[j]);
  286. if (ret == -E2BIG)
  287. semantic_error("probe point is too long.");
  288. else if (ret < 0)
  289. die("snprintf");
  290. }
  291. #ifndef NO_LIBDWARF
  292. if (!session.need_dwarf)
  293. goto setup_probes;
  294. if (session.vmlinux)
  295. fd = open(session.vmlinux, O_RDONLY);
  296. else
  297. fd = open_default_vmlinux();
  298. if (fd < 0)
  299. die("vmlinux/module file open");
  300. /* Searching probe points */
  301. for (j = 0; j < session.nr_probe; j++) {
  302. pp = &session.probes[j];
  303. if (pp->found)
  304. continue;
  305. lseek(fd, SEEK_SET, 0);
  306. ret = find_probepoint(fd, pp);
  307. if (ret <= 0)
  308. die("No probe point found.\n");
  309. pr_debug("probe event %s found\n", session.events[j]);
  310. }
  311. close(fd);
  312. setup_probes:
  313. #endif /* !NO_LIBDWARF */
  314. /* Settng up probe points */
  315. snprintf(buf, MAX_CMDLEN, "%s/../kprobe_events", debugfs_path);
  316. fd = open(buf, O_WRONLY, O_APPEND);
  317. if (fd < 0)
  318. die("kprobe_events open");
  319. for (j = 0; j < session.nr_probe; j++) {
  320. pp = &session.probes[j];
  321. if (pp->found == 1) {
  322. snprintf(buf, MAX_CMDLEN, "%s %s\n",
  323. session.events[j], pp->probes[0]);
  324. write_new_event(fd, buf);
  325. } else
  326. for (i = 0; i < pp->found; i++) {
  327. snprintf(buf, MAX_CMDLEN, "%s%d %s\n",
  328. session.events[j], i, pp->probes[i]);
  329. write_new_event(fd, buf);
  330. }
  331. }
  332. close(fd);
  333. return 0;
  334. }