builtin-probe.c 9.1 KB

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