probe-event.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * probe-event.c : perf-probe definition to kprobe_events format converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #define _GNU_SOURCE
  22. #include <sys/utsname.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #undef _GNU_SOURCE
  34. #include "event.h"
  35. #include "string.h"
  36. #include "strlist.h"
  37. #include "debug.h"
  38. #include "parse-events.h" /* For debugfs_path */
  39. #include "probe-event.h"
  40. #define MAX_CMDLEN 256
  41. #define MAX_PROBE_ARGS 128
  42. #define PERFPROBE_GROUP "probe"
  43. #define semantic_error(msg ...) die("Semantic error :" msg)
  44. /* If there is no space to write, returns -E2BIG. */
  45. static int e_snprintf(char *str, size_t size, const char *format, ...)
  46. {
  47. int ret;
  48. va_list ap;
  49. va_start(ap, format);
  50. ret = vsnprintf(str, size, format, ap);
  51. va_end(ap);
  52. if (ret >= (int)size)
  53. ret = -E2BIG;
  54. return ret;
  55. }
  56. /* Parse probepoint definition. */
  57. static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
  58. {
  59. char *ptr, *tmp;
  60. char c, nc = 0;
  61. /*
  62. * <Syntax>
  63. * perf probe SRC:LN
  64. * perf probe FUNC[+OFFS|%return][@SRC]
  65. */
  66. ptr = strpbrk(arg, ":+@%");
  67. if (ptr) {
  68. nc = *ptr;
  69. *ptr++ = '\0';
  70. }
  71. /* Check arg is function or file and copy it */
  72. if (strchr(arg, '.')) /* File */
  73. pp->file = strdup(arg);
  74. else /* Function */
  75. pp->function = strdup(arg);
  76. DIE_IF(pp->file == NULL && pp->function == NULL);
  77. /* Parse other options */
  78. while (ptr) {
  79. arg = ptr;
  80. c = nc;
  81. ptr = strpbrk(arg, ":+@%");
  82. if (ptr) {
  83. nc = *ptr;
  84. *ptr++ = '\0';
  85. }
  86. switch (c) {
  87. case ':': /* Line number */
  88. pp->line = strtoul(arg, &tmp, 0);
  89. if (*tmp != '\0')
  90. semantic_error("There is non-digit charactor"
  91. " in line number.");
  92. break;
  93. case '+': /* Byte offset from a symbol */
  94. pp->offset = strtoul(arg, &tmp, 0);
  95. if (*tmp != '\0')
  96. semantic_error("There is non-digit charactor"
  97. " in offset.");
  98. break;
  99. case '@': /* File name */
  100. if (pp->file)
  101. semantic_error("SRC@SRC is not allowed.");
  102. pp->file = strdup(arg);
  103. DIE_IF(pp->file == NULL);
  104. if (ptr)
  105. semantic_error("@SRC must be the last "
  106. "option.");
  107. break;
  108. case '%': /* Probe places */
  109. if (strcmp(arg, "return") == 0) {
  110. pp->retprobe = 1;
  111. } else /* Others not supported yet */
  112. semantic_error("%%%s is not supported.", arg);
  113. break;
  114. default:
  115. DIE_IF("Program has a bug.");
  116. break;
  117. }
  118. }
  119. /* Exclusion check */
  120. if (pp->line && pp->offset)
  121. semantic_error("Offset can't be used with line number.");
  122. if (!pp->line && pp->file && !pp->function)
  123. semantic_error("File always requires line number.");
  124. if (pp->offset && !pp->function)
  125. semantic_error("Offset requires an entry function.");
  126. if (pp->retprobe && !pp->function)
  127. semantic_error("Return probe requires an entry function.");
  128. if ((pp->offset || pp->line) && pp->retprobe)
  129. semantic_error("Offset/Line can't be used with return probe.");
  130. pr_debug("symbol:%s file:%s line:%d offset:%d, return:%d\n",
  131. pp->function, pp->file, pp->line, pp->offset, pp->retprobe);
  132. }
  133. /* Parse perf-probe event definition */
  134. int parse_perf_probe_event(const char *str, struct probe_point *pp)
  135. {
  136. char **argv;
  137. int argc, i, need_dwarf = 0;
  138. argv = argv_split(str, &argc);
  139. if (!argv)
  140. die("argv_split failed.");
  141. if (argc > MAX_PROBE_ARGS + 1)
  142. semantic_error("Too many arguments");
  143. /* Parse probe point */
  144. parse_perf_probe_probepoint(argv[0], pp);
  145. if (pp->file || pp->line)
  146. need_dwarf = 1;
  147. /* Copy arguments and ensure return probe has no C argument */
  148. pp->nr_args = argc - 1;
  149. pp->args = zalloc(sizeof(char *) * pp->nr_args);
  150. for (i = 0; i < pp->nr_args; i++) {
  151. pp->args[i] = strdup(argv[i + 1]);
  152. if (!pp->args[i])
  153. die("Failed to copy argument.");
  154. if (is_c_varname(pp->args[i])) {
  155. if (pp->retprobe)
  156. semantic_error("You can't specify local"
  157. " variable for kretprobe");
  158. need_dwarf = 1;
  159. }
  160. }
  161. argv_free(argv);
  162. return need_dwarf;
  163. }
  164. /* Parse kprobe_events event into struct probe_point */
  165. void parse_trace_kprobe_event(const char *str, char **group, char **event,
  166. struct probe_point *pp)
  167. {
  168. char pr;
  169. char *p;
  170. int ret, i, argc;
  171. char **argv;
  172. pr_debug("Parsing kprobe_events: %s\n", str);
  173. argv = argv_split(str, &argc);
  174. if (!argv)
  175. die("argv_split failed.");
  176. if (argc < 2)
  177. semantic_error("Too less arguments.");
  178. /* Scan event and group name. */
  179. ret = sscanf(argv[0], "%c:%m[^/ \t]/%m[^ \t]",
  180. &pr, group, event);
  181. if (ret != 3)
  182. semantic_error("Failed to parse event name: %s", argv[0]);
  183. pr_debug("Group:%s Event:%s probe:%c\n", *group, *event, pr);
  184. if (!pp)
  185. goto end;
  186. pp->retprobe = (pr == 'r');
  187. /* Scan function name and offset */
  188. ret = sscanf(argv[1], "%m[^+]+%d", &pp->function, &pp->offset);
  189. if (ret == 1)
  190. pp->offset = 0;
  191. /* kprobe_events doesn't have this information */
  192. pp->line = 0;
  193. pp->file = NULL;
  194. pp->nr_args = argc - 2;
  195. pp->args = zalloc(sizeof(char *) * pp->nr_args);
  196. for (i = 0; i < pp->nr_args; i++) {
  197. p = strchr(argv[i + 2], '=');
  198. if (p) /* We don't need which register is assigned. */
  199. *p = '\0';
  200. pp->args[i] = strdup(argv[i + 2]);
  201. if (!pp->args[i])
  202. die("Failed to copy argument.");
  203. }
  204. end:
  205. argv_free(argv);
  206. }
  207. int synthesize_perf_probe_event(struct probe_point *pp)
  208. {
  209. char *buf;
  210. char offs[64] = "", line[64] = "";
  211. int i, len, ret;
  212. pp->probes[0] = buf = zalloc(MAX_CMDLEN);
  213. if (!buf)
  214. die("Failed to allocate memory by zalloc.");
  215. if (pp->offset) {
  216. ret = e_snprintf(offs, 64, "+%d", pp->offset);
  217. if (ret <= 0)
  218. goto error;
  219. }
  220. if (pp->line) {
  221. ret = e_snprintf(line, 64, ":%d", pp->line);
  222. if (ret <= 0)
  223. goto error;
  224. }
  225. if (pp->function)
  226. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
  227. offs, pp->retprobe ? "%return" : "", line);
  228. else
  229. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->file, line);
  230. if (ret <= 0)
  231. goto error;
  232. len = ret;
  233. for (i = 0; i < pp->nr_args; i++) {
  234. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  235. pp->args[i]);
  236. if (ret <= 0)
  237. goto error;
  238. len += ret;
  239. }
  240. pp->found = 1;
  241. return pp->found;
  242. error:
  243. free(pp->probes[0]);
  244. return ret;
  245. }
  246. int synthesize_trace_kprobe_event(struct probe_point *pp)
  247. {
  248. char *buf;
  249. int i, len, ret;
  250. pp->probes[0] = buf = zalloc(MAX_CMDLEN);
  251. if (!buf)
  252. die("Failed to allocate memory by zalloc.");
  253. ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
  254. if (ret <= 0)
  255. goto error;
  256. len = ret;
  257. for (i = 0; i < pp->nr_args; i++) {
  258. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  259. pp->args[i]);
  260. if (ret <= 0)
  261. goto error;
  262. len += ret;
  263. }
  264. pp->found = 1;
  265. return pp->found;
  266. error:
  267. free(pp->probes[0]);
  268. return ret;
  269. }
  270. static int open_kprobe_events(int flags, int mode)
  271. {
  272. char buf[PATH_MAX];
  273. int ret;
  274. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  275. if (ret < 0)
  276. die("Failed to make kprobe_events path.");
  277. ret = open(buf, flags, mode);
  278. if (ret < 0) {
  279. if (errno == ENOENT)
  280. die("kprobe_events file does not exist -"
  281. " please rebuild with CONFIG_KPROBE_TRACER.");
  282. else
  283. die("Could not open kprobe_events file: %s",
  284. strerror(errno));
  285. }
  286. return ret;
  287. }
  288. /* Get raw string list of current kprobe_events */
  289. static struct strlist *get_trace_kprobe_event_rawlist(int fd)
  290. {
  291. int ret, idx;
  292. FILE *fp;
  293. char buf[MAX_CMDLEN];
  294. char *p;
  295. struct strlist *sl;
  296. sl = strlist__new(true, NULL);
  297. fp = fdopen(dup(fd), "r");
  298. while (!feof(fp)) {
  299. p = fgets(buf, MAX_CMDLEN, fp);
  300. if (!p)
  301. break;
  302. idx = strlen(p) - 1;
  303. if (p[idx] == '\n')
  304. p[idx] = '\0';
  305. ret = strlist__add(sl, buf);
  306. if (ret < 0)
  307. die("strlist__add failed: %s", strerror(-ret));
  308. }
  309. fclose(fp);
  310. return sl;
  311. }
  312. /* Free and zero clear probe_point */
  313. static void clear_probe_point(struct probe_point *pp)
  314. {
  315. int i;
  316. if (pp->function)
  317. free(pp->function);
  318. if (pp->file)
  319. free(pp->file);
  320. for (i = 0; i < pp->nr_args; i++)
  321. free(pp->args[i]);
  322. if (pp->args)
  323. free(pp->args);
  324. for (i = 0; i < pp->found; i++)
  325. free(pp->probes[i]);
  326. memset(pp, 0, sizeof(pp));
  327. }
  328. /* List up current perf-probe events */
  329. void show_perf_probe_events(void)
  330. {
  331. unsigned int i;
  332. int fd;
  333. char *group, *event;
  334. struct probe_point pp;
  335. struct strlist *rawlist;
  336. struct str_node *ent;
  337. fd = open_kprobe_events(O_RDONLY, 0);
  338. rawlist = get_trace_kprobe_event_rawlist(fd);
  339. close(fd);
  340. for (i = 0; i < strlist__nr_entries(rawlist); i++) {
  341. ent = strlist__entry(rawlist, i);
  342. parse_trace_kprobe_event(ent->s, &group, &event, &pp);
  343. synthesize_perf_probe_event(&pp);
  344. printf("[%s:%s]\t%s\n", group, event, pp.probes[0]);
  345. free(group);
  346. free(event);
  347. clear_probe_point(&pp);
  348. }
  349. strlist__delete(rawlist);
  350. }
  351. static int write_trace_kprobe_event(int fd, const char *buf)
  352. {
  353. int ret;
  354. ret = write(fd, buf, strlen(buf));
  355. if (ret <= 0)
  356. die("Failed to create event.");
  357. else
  358. printf("Added new event: %s\n", buf);
  359. return ret;
  360. }
  361. void add_trace_kprobe_events(struct probe_point *probes, int nr_probes)
  362. {
  363. int i, j, fd;
  364. struct probe_point *pp;
  365. char buf[MAX_CMDLEN];
  366. fd = open_kprobe_events(O_WRONLY, O_APPEND);
  367. for (j = 0; j < nr_probes; j++) {
  368. pp = probes + j;
  369. if (pp->found == 1) {
  370. snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x %s\n",
  371. pp->retprobe ? 'r' : 'p', PERFPROBE_GROUP,
  372. pp->function, pp->offset, pp->probes[0]);
  373. write_trace_kprobe_event(fd, buf);
  374. } else
  375. for (i = 0; i < pp->found; i++) {
  376. snprintf(buf, MAX_CMDLEN, "%c:%s/%s_%x_%d %s\n",
  377. pp->retprobe ? 'r' : 'p',
  378. PERFPROBE_GROUP,
  379. pp->function, pp->offset, i,
  380. pp->probes[i]);
  381. write_trace_kprobe_event(fd, buf);
  382. }
  383. }
  384. close(fd);
  385. }