builtin-probe.c 8.6 KB

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