probe-event.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 "util.h"
  35. #include "event.h"
  36. #include "string.h"
  37. #include "strlist.h"
  38. #include "debug.h"
  39. #include "cache.h"
  40. #include "color.h"
  41. #include "symbol.h"
  42. #include "thread.h"
  43. #include "parse-events.h" /* For debugfs_path */
  44. #include "probe-event.h"
  45. #define MAX_CMDLEN 256
  46. #define MAX_PROBE_ARGS 128
  47. #define PERFPROBE_GROUP "probe"
  48. bool probe_event_dry_run; /* Dry run flag */
  49. #define semantic_error(msg ...) die("Semantic error :" msg)
  50. /* If there is no space to write, returns -E2BIG. */
  51. static int e_snprintf(char *str, size_t size, const char *format, ...)
  52. __attribute__((format(printf, 3, 4)));
  53. static int e_snprintf(char *str, size_t size, const char *format, ...)
  54. {
  55. int ret;
  56. va_list ap;
  57. va_start(ap, format);
  58. ret = vsnprintf(str, size, format, ap);
  59. va_end(ap);
  60. if (ret >= (int)size)
  61. ret = -E2BIG;
  62. return ret;
  63. }
  64. static struct map_groups kmap_groups;
  65. static struct map *kmaps[MAP__NR_TYPES];
  66. /* Initialize symbol maps for vmlinux */
  67. static void init_vmlinux(void)
  68. {
  69. symbol_conf.sort_by_name = true;
  70. if (symbol_conf.vmlinux_name == NULL)
  71. symbol_conf.try_vmlinux_path = true;
  72. else
  73. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  74. if (symbol__init() < 0)
  75. die("Failed to init symbol map.");
  76. map_groups__init(&kmap_groups);
  77. if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0)
  78. die("Failed to create kernel maps.");
  79. }
  80. #ifndef NO_DWARF_SUPPORT
  81. static int open_vmlinux(void)
  82. {
  83. if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
  84. pr_debug("Failed to load kernel map.\n");
  85. return -EINVAL;
  86. }
  87. pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
  88. return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
  89. }
  90. #endif
  91. void parse_line_range_desc(const char *arg, struct line_range *lr)
  92. {
  93. const char *ptr;
  94. char *tmp;
  95. /*
  96. * <Syntax>
  97. * SRC:SLN[+NUM|-ELN]
  98. * FUNC[:SLN[+NUM|-ELN]]
  99. */
  100. ptr = strchr(arg, ':');
  101. if (ptr) {
  102. lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
  103. if (*tmp == '+')
  104. lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
  105. &tmp, 0);
  106. else if (*tmp == '-')
  107. lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
  108. else
  109. lr->end = 0;
  110. pr_debug("Line range is %u to %u\n", lr->start, lr->end);
  111. if (lr->end && lr->start > lr->end)
  112. semantic_error("Start line must be smaller"
  113. " than end line.");
  114. if (*tmp != '\0')
  115. semantic_error("Tailing with invalid character '%d'.",
  116. *tmp);
  117. tmp = xstrndup(arg, (ptr - arg));
  118. } else
  119. tmp = xstrdup(arg);
  120. if (strchr(tmp, '.'))
  121. lr->file = tmp;
  122. else
  123. lr->function = tmp;
  124. }
  125. /* Check the name is good for event/group */
  126. static bool check_event_name(const char *name)
  127. {
  128. if (!isalpha(*name) && *name != '_')
  129. return false;
  130. while (*++name != '\0') {
  131. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  132. return false;
  133. }
  134. return true;
  135. }
  136. /* Parse probepoint definition. */
  137. static void parse_perf_probe_probepoint(char *arg, struct probe_point *pp)
  138. {
  139. char *ptr, *tmp;
  140. char c, nc = 0;
  141. /*
  142. * <Syntax>
  143. * perf probe [EVENT=]SRC[:LN|;PTN]
  144. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  145. *
  146. * TODO:Group name support
  147. */
  148. ptr = strpbrk(arg, ";=@+%");
  149. if (ptr && *ptr == '=') { /* Event name */
  150. *ptr = '\0';
  151. tmp = ptr + 1;
  152. ptr = strchr(arg, ':');
  153. if (ptr) /* Group name is not supported yet. */
  154. semantic_error("Group name is not supported yet.");
  155. if (!check_event_name(arg))
  156. semantic_error("%s is bad for event name -it must "
  157. "follow C symbol-naming rule.", arg);
  158. pp->event = xstrdup(arg);
  159. arg = tmp;
  160. }
  161. ptr = strpbrk(arg, ";:+@%");
  162. if (ptr) {
  163. nc = *ptr;
  164. *ptr++ = '\0';
  165. }
  166. /* Check arg is function or file and copy it */
  167. if (strchr(arg, '.')) /* File */
  168. pp->file = xstrdup(arg);
  169. else /* Function */
  170. pp->function = xstrdup(arg);
  171. /* Parse other options */
  172. while (ptr) {
  173. arg = ptr;
  174. c = nc;
  175. if (c == ';') { /* Lazy pattern must be the last part */
  176. pp->lazy_line = xstrdup(arg);
  177. break;
  178. }
  179. ptr = strpbrk(arg, ";:+@%");
  180. if (ptr) {
  181. nc = *ptr;
  182. *ptr++ = '\0';
  183. }
  184. switch (c) {
  185. case ':': /* Line number */
  186. pp->line = strtoul(arg, &tmp, 0);
  187. if (*tmp != '\0')
  188. semantic_error("There is non-digit char"
  189. " in line number.");
  190. break;
  191. case '+': /* Byte offset from a symbol */
  192. pp->offset = strtoul(arg, &tmp, 0);
  193. if (*tmp != '\0')
  194. semantic_error("There is non-digit character"
  195. " in offset.");
  196. break;
  197. case '@': /* File name */
  198. if (pp->file)
  199. semantic_error("SRC@SRC is not allowed.");
  200. pp->file = xstrdup(arg);
  201. break;
  202. case '%': /* Probe places */
  203. if (strcmp(arg, "return") == 0) {
  204. pp->retprobe = 1;
  205. } else /* Others not supported yet */
  206. semantic_error("%%%s is not supported.", arg);
  207. break;
  208. default:
  209. DIE_IF("Program has a bug.");
  210. break;
  211. }
  212. }
  213. /* Exclusion check */
  214. if (pp->lazy_line && pp->line)
  215. semantic_error("Lazy pattern can't be used with line number.");
  216. if (pp->lazy_line && pp->offset)
  217. semantic_error("Lazy pattern can't be used with offset.");
  218. if (pp->line && pp->offset)
  219. semantic_error("Offset can't be used with line number.");
  220. if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
  221. semantic_error("File always requires line number or "
  222. "lazy pattern.");
  223. if (pp->offset && !pp->function)
  224. semantic_error("Offset requires an entry function.");
  225. if (pp->retprobe && !pp->function)
  226. semantic_error("Return probe requires an entry function.");
  227. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
  228. semantic_error("Offset/Line/Lazy pattern can't be used with "
  229. "return probe.");
  230. pr_debug("symbol:%s file:%s line:%d offset:%d return:%d lazy:%s\n",
  231. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  232. pp->lazy_line);
  233. }
  234. /* Parse perf-probe event definition */
  235. void parse_perf_probe_event(const char *str, struct probe_point *pp,
  236. bool *need_dwarf)
  237. {
  238. char **argv;
  239. int argc, i;
  240. *need_dwarf = false;
  241. argv = argv_split(str, &argc);
  242. if (!argv)
  243. die("argv_split failed.");
  244. if (argc > MAX_PROBE_ARGS + 1)
  245. semantic_error("Too many arguments");
  246. /* Parse probe point */
  247. parse_perf_probe_probepoint(argv[0], pp);
  248. if (pp->file || pp->line || pp->lazy_line)
  249. *need_dwarf = true;
  250. /* Copy arguments and ensure return probe has no C argument */
  251. pp->nr_args = argc - 1;
  252. pp->args = xzalloc(sizeof(char *) * pp->nr_args);
  253. for (i = 0; i < pp->nr_args; i++) {
  254. pp->args[i] = xstrdup(argv[i + 1]);
  255. if (is_c_varname(pp->args[i])) {
  256. if (pp->retprobe)
  257. semantic_error("You can't specify local"
  258. " variable for kretprobe");
  259. *need_dwarf = true;
  260. }
  261. }
  262. argv_free(argv);
  263. }
  264. /* Parse kprobe_events event into struct probe_point */
  265. void parse_trace_kprobe_event(const char *str, struct probe_point *pp)
  266. {
  267. char pr;
  268. char *p;
  269. int ret, i, argc;
  270. char **argv;
  271. pr_debug("Parsing kprobe_events: %s\n", str);
  272. argv = argv_split(str, &argc);
  273. if (!argv)
  274. die("argv_split failed.");
  275. if (argc < 2)
  276. semantic_error("Too less arguments.");
  277. /* Scan event and group name. */
  278. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  279. &pr, (float *)(void *)&pp->group,
  280. (float *)(void *)&pp->event);
  281. if (ret != 3)
  282. semantic_error("Failed to parse event name: %s", argv[0]);
  283. pr_debug("Group:%s Event:%s probe:%c\n", pp->group, pp->event, pr);
  284. pp->retprobe = (pr == 'r');
  285. /* Scan function name and offset */
  286. ret = sscanf(argv[1], "%a[^+]+%d", (float *)(void *)&pp->function,
  287. &pp->offset);
  288. if (ret == 1)
  289. pp->offset = 0;
  290. /* kprobe_events doesn't have this information */
  291. pp->line = 0;
  292. pp->file = NULL;
  293. pp->nr_args = argc - 2;
  294. pp->args = xzalloc(sizeof(char *) * pp->nr_args);
  295. for (i = 0; i < pp->nr_args; i++) {
  296. p = strchr(argv[i + 2], '=');
  297. if (p) /* We don't need which register is assigned. */
  298. *p = '\0';
  299. pp->args[i] = xstrdup(argv[i + 2]);
  300. }
  301. argv_free(argv);
  302. }
  303. /* Synthesize only probe point (not argument) */
  304. int synthesize_perf_probe_point(struct probe_point *pp)
  305. {
  306. char *buf;
  307. char offs[64] = "", line[64] = "";
  308. int ret;
  309. pp->probes[0] = buf = xzalloc(MAX_CMDLEN);
  310. pp->found = 1;
  311. if (pp->offset) {
  312. ret = e_snprintf(offs, 64, "+%d", pp->offset);
  313. if (ret <= 0)
  314. goto error;
  315. }
  316. if (pp->line) {
  317. ret = e_snprintf(line, 64, ":%d", pp->line);
  318. if (ret <= 0)
  319. goto error;
  320. }
  321. if (pp->function)
  322. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s", pp->function,
  323. offs, pp->retprobe ? "%return" : "", line);
  324. else
  325. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", pp->file, line);
  326. if (ret <= 0) {
  327. error:
  328. free(pp->probes[0]);
  329. pp->probes[0] = NULL;
  330. pp->found = 0;
  331. }
  332. return ret;
  333. }
  334. int synthesize_perf_probe_event(struct probe_point *pp)
  335. {
  336. char *buf;
  337. int i, len, ret;
  338. len = synthesize_perf_probe_point(pp);
  339. if (len < 0)
  340. return 0;
  341. buf = pp->probes[0];
  342. for (i = 0; i < pp->nr_args; i++) {
  343. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  344. pp->args[i]);
  345. if (ret <= 0)
  346. goto error;
  347. len += ret;
  348. }
  349. pp->found = 1;
  350. return pp->found;
  351. error:
  352. free(pp->probes[0]);
  353. pp->probes[0] = NULL;
  354. return ret;
  355. }
  356. int synthesize_trace_kprobe_event(struct probe_point *pp)
  357. {
  358. char *buf;
  359. int i, len, ret;
  360. pp->probes[0] = buf = xzalloc(MAX_CMDLEN);
  361. ret = e_snprintf(buf, MAX_CMDLEN, "%s+%d", pp->function, pp->offset);
  362. if (ret <= 0)
  363. goto error;
  364. len = ret;
  365. for (i = 0; i < pp->nr_args; i++) {
  366. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  367. pp->args[i]);
  368. if (ret <= 0)
  369. goto error;
  370. len += ret;
  371. }
  372. pp->found = 1;
  373. return pp->found;
  374. error:
  375. free(pp->probes[0]);
  376. pp->probes[0] = NULL;
  377. return ret;
  378. }
  379. static int open_kprobe_events(bool readwrite)
  380. {
  381. char buf[PATH_MAX];
  382. int ret;
  383. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  384. if (ret < 0)
  385. die("Failed to make kprobe_events path.");
  386. if (readwrite && !probe_event_dry_run)
  387. ret = open(buf, O_RDWR, O_APPEND);
  388. else
  389. ret = open(buf, O_RDONLY, 0);
  390. if (ret < 0) {
  391. if (errno == ENOENT)
  392. die("kprobe_events file does not exist -"
  393. " please rebuild with CONFIG_KPROBE_EVENT.");
  394. else
  395. die("Could not open kprobe_events file: %s",
  396. strerror(errno));
  397. }
  398. return ret;
  399. }
  400. /* Get raw string list of current kprobe_events */
  401. static struct strlist *get_trace_kprobe_event_rawlist(int fd)
  402. {
  403. int ret, idx;
  404. FILE *fp;
  405. char buf[MAX_CMDLEN];
  406. char *p;
  407. struct strlist *sl;
  408. sl = strlist__new(true, NULL);
  409. fp = fdopen(dup(fd), "r");
  410. while (!feof(fp)) {
  411. p = fgets(buf, MAX_CMDLEN, fp);
  412. if (!p)
  413. break;
  414. idx = strlen(p) - 1;
  415. if (p[idx] == '\n')
  416. p[idx] = '\0';
  417. ret = strlist__add(sl, buf);
  418. if (ret < 0)
  419. die("strlist__add failed: %s", strerror(-ret));
  420. }
  421. fclose(fp);
  422. return sl;
  423. }
  424. /* Free and zero clear probe_point */
  425. static void clear_probe_point(struct probe_point *pp)
  426. {
  427. int i;
  428. if (pp->event)
  429. free(pp->event);
  430. if (pp->group)
  431. free(pp->group);
  432. if (pp->function)
  433. free(pp->function);
  434. if (pp->file)
  435. free(pp->file);
  436. if (pp->lazy_line)
  437. free(pp->lazy_line);
  438. for (i = 0; i < pp->nr_args; i++)
  439. free(pp->args[i]);
  440. if (pp->args)
  441. free(pp->args);
  442. for (i = 0; i < pp->found; i++)
  443. free(pp->probes[i]);
  444. memset(pp, 0, sizeof(*pp));
  445. }
  446. /* Show an event */
  447. static void show_perf_probe_event(const char *event, const char *place,
  448. struct probe_point *pp)
  449. {
  450. int i, ret;
  451. char buf[128];
  452. ret = e_snprintf(buf, 128, "%s:%s", pp->group, event);
  453. if (ret < 0)
  454. die("Failed to copy event: %s", strerror(-ret));
  455. printf(" %-40s (on %s", buf, place);
  456. if (pp->nr_args > 0) {
  457. printf(" with");
  458. for (i = 0; i < pp->nr_args; i++)
  459. printf(" %s", pp->args[i]);
  460. }
  461. printf(")\n");
  462. }
  463. /* List up current perf-probe events */
  464. void show_perf_probe_events(void)
  465. {
  466. int fd;
  467. struct probe_point pp;
  468. struct strlist *rawlist;
  469. struct str_node *ent;
  470. setup_pager();
  471. memset(&pp, 0, sizeof(pp));
  472. fd = open_kprobe_events(false);
  473. rawlist = get_trace_kprobe_event_rawlist(fd);
  474. close(fd);
  475. strlist__for_each(ent, rawlist) {
  476. parse_trace_kprobe_event(ent->s, &pp);
  477. /* Synthesize only event probe point */
  478. synthesize_perf_probe_point(&pp);
  479. /* Show an event */
  480. show_perf_probe_event(pp.event, pp.probes[0], &pp);
  481. clear_probe_point(&pp);
  482. }
  483. strlist__delete(rawlist);
  484. }
  485. /* Get current perf-probe event names */
  486. static struct strlist *get_perf_event_names(int fd, bool include_group)
  487. {
  488. char buf[128];
  489. struct strlist *sl, *rawlist;
  490. struct str_node *ent;
  491. struct probe_point pp;
  492. memset(&pp, 0, sizeof(pp));
  493. rawlist = get_trace_kprobe_event_rawlist(fd);
  494. sl = strlist__new(true, NULL);
  495. strlist__for_each(ent, rawlist) {
  496. parse_trace_kprobe_event(ent->s, &pp);
  497. if (include_group) {
  498. if (e_snprintf(buf, 128, "%s:%s", pp.group,
  499. pp.event) < 0)
  500. die("Failed to copy group:event name.");
  501. strlist__add(sl, buf);
  502. } else
  503. strlist__add(sl, pp.event);
  504. clear_probe_point(&pp);
  505. }
  506. strlist__delete(rawlist);
  507. return sl;
  508. }
  509. static void write_trace_kprobe_event(int fd, const char *buf)
  510. {
  511. int ret;
  512. pr_debug("Writing event: %s\n", buf);
  513. if (!probe_event_dry_run) {
  514. ret = write(fd, buf, strlen(buf));
  515. if (ret <= 0)
  516. die("Failed to write event: %s", strerror(errno));
  517. }
  518. }
  519. static void get_new_event_name(char *buf, size_t len, const char *base,
  520. struct strlist *namelist, bool allow_suffix)
  521. {
  522. int i, ret;
  523. /* Try no suffix */
  524. ret = e_snprintf(buf, len, "%s", base);
  525. if (ret < 0)
  526. die("snprintf() failed: %s", strerror(-ret));
  527. if (!strlist__has_entry(namelist, buf))
  528. return;
  529. if (!allow_suffix) {
  530. pr_warning("Error: event \"%s\" already exists. "
  531. "(Use -f to force duplicates.)\n", base);
  532. die("Can't add new event.");
  533. }
  534. /* Try to add suffix */
  535. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  536. ret = e_snprintf(buf, len, "%s_%d", base, i);
  537. if (ret < 0)
  538. die("snprintf() failed: %s", strerror(-ret));
  539. if (!strlist__has_entry(namelist, buf))
  540. break;
  541. }
  542. if (i == MAX_EVENT_INDEX)
  543. die("Too many events are on the same function.");
  544. }
  545. static void __add_trace_kprobe_events(struct probe_point *probes,
  546. int nr_probes, bool force_add)
  547. {
  548. int i, j, fd;
  549. struct probe_point *pp;
  550. char buf[MAX_CMDLEN];
  551. char event[64];
  552. struct strlist *namelist;
  553. bool allow_suffix;
  554. fd = open_kprobe_events(true);
  555. /* Get current event names */
  556. namelist = get_perf_event_names(fd, false);
  557. for (j = 0; j < nr_probes; j++) {
  558. pp = probes + j;
  559. if (!pp->event)
  560. pp->event = xstrdup(pp->function);
  561. if (!pp->group)
  562. pp->group = xstrdup(PERFPROBE_GROUP);
  563. /* If force_add is true, suffix search is allowed */
  564. allow_suffix = force_add;
  565. for (i = 0; i < pp->found; i++) {
  566. /* Get an unused new event name */
  567. get_new_event_name(event, 64, pp->event, namelist,
  568. allow_suffix);
  569. snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s\n",
  570. pp->retprobe ? 'r' : 'p',
  571. pp->group, event,
  572. pp->probes[i]);
  573. write_trace_kprobe_event(fd, buf);
  574. printf("Added new event:\n");
  575. /* Get the first parameter (probe-point) */
  576. sscanf(pp->probes[i], "%s", buf);
  577. show_perf_probe_event(event, buf, pp);
  578. /* Add added event name to namelist */
  579. strlist__add(namelist, event);
  580. /*
  581. * Probes after the first probe which comes from same
  582. * user input are always allowed to add suffix, because
  583. * there might be several addresses corresponding to
  584. * one code line.
  585. */
  586. allow_suffix = true;
  587. }
  588. }
  589. /* Show how to use the event. */
  590. printf("\nYou can now use it on all perf tools, such as:\n\n");
  591. printf("\tperf record -e %s:%s -a sleep 1\n\n", PERFPROBE_GROUP, event);
  592. strlist__delete(namelist);
  593. close(fd);
  594. }
  595. /* Currently just checking function name from symbol map */
  596. static void evaluate_probe_point(struct probe_point *pp)
  597. {
  598. struct symbol *sym;
  599. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  600. pp->function, NULL);
  601. if (!sym)
  602. die("Kernel symbol \'%s\' not found - probe not added.",
  603. pp->function);
  604. }
  605. void add_trace_kprobe_events(struct probe_point *probes, int nr_probes,
  606. bool force_add, bool need_dwarf)
  607. {
  608. int i, ret;
  609. struct probe_point *pp;
  610. #ifndef NO_DWARF_SUPPORT
  611. int fd;
  612. #endif
  613. /* Add probes */
  614. init_vmlinux();
  615. if (need_dwarf)
  616. #ifdef NO_DWARF_SUPPORT
  617. die("Debuginfo-analysis is not supported");
  618. #else /* !NO_DWARF_SUPPORT */
  619. pr_debug("Some probes require debuginfo.\n");
  620. fd = open_vmlinux();
  621. if (fd < 0) {
  622. if (need_dwarf)
  623. die("Could not open debuginfo file.");
  624. pr_debug("Could not open vmlinux/module file."
  625. " Try to use symbols.\n");
  626. goto end_dwarf;
  627. }
  628. /* Searching probe points */
  629. for (i = 0; i < nr_probes; i++) {
  630. pp = &probes[i];
  631. if (pp->found)
  632. continue;
  633. lseek(fd, SEEK_SET, 0);
  634. ret = find_probe_point(fd, pp);
  635. if (ret > 0)
  636. continue;
  637. if (ret == 0) { /* No error but failed to find probe point. */
  638. synthesize_perf_probe_point(pp);
  639. die("Probe point '%s' not found. - probe not added.",
  640. pp->probes[0]);
  641. }
  642. /* Error path */
  643. if (need_dwarf) {
  644. if (ret == -ENOENT)
  645. pr_warning("No dwarf info found in the vmlinux - please rebuild with CONFIG_DEBUG_INFO=y.\n");
  646. die("Could not analyze debuginfo.");
  647. }
  648. pr_debug("An error occurred in debuginfo analysis."
  649. " Try to use symbols.\n");
  650. break;
  651. }
  652. close(fd);
  653. end_dwarf:
  654. #endif /* !NO_DWARF_SUPPORT */
  655. /* Synthesize probes without dwarf */
  656. for (i = 0; i < nr_probes; i++) {
  657. pp = &probes[i];
  658. if (pp->found) /* This probe is already found. */
  659. continue;
  660. evaluate_probe_point(pp);
  661. ret = synthesize_trace_kprobe_event(pp);
  662. if (ret == -E2BIG)
  663. die("probe point definition becomes too long.");
  664. else if (ret < 0)
  665. die("Failed to synthesize a probe point.");
  666. }
  667. /* Settng up probe points */
  668. __add_trace_kprobe_events(probes, nr_probes, force_add);
  669. }
  670. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  671. {
  672. char *p;
  673. char buf[128];
  674. /* Convert from perf-probe event to trace-kprobe event */
  675. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  676. die("Failed to copy event.");
  677. p = strchr(buf + 2, ':');
  678. if (!p)
  679. die("Internal error: %s should have ':' but not.", ent->s);
  680. *p = '/';
  681. write_trace_kprobe_event(fd, buf);
  682. printf("Remove event: %s\n", ent->s);
  683. }
  684. static void del_trace_kprobe_event(int fd, const char *group,
  685. const char *event, struct strlist *namelist)
  686. {
  687. char buf[128];
  688. struct str_node *ent, *n;
  689. int found = 0;
  690. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  691. die("Failed to copy event.");
  692. if (strpbrk(buf, "*?")) { /* Glob-exp */
  693. strlist__for_each_safe(ent, n, namelist)
  694. if (strglobmatch(ent->s, buf)) {
  695. found++;
  696. __del_trace_kprobe_event(fd, ent);
  697. strlist__remove(namelist, ent);
  698. }
  699. } else {
  700. ent = strlist__find(namelist, buf);
  701. if (ent) {
  702. found++;
  703. __del_trace_kprobe_event(fd, ent);
  704. strlist__remove(namelist, ent);
  705. }
  706. }
  707. if (found == 0)
  708. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  709. }
  710. void del_trace_kprobe_events(struct strlist *dellist)
  711. {
  712. int fd;
  713. const char *group, *event;
  714. char *p, *str;
  715. struct str_node *ent;
  716. struct strlist *namelist;
  717. fd = open_kprobe_events(true);
  718. /* Get current event names */
  719. namelist = get_perf_event_names(fd, true);
  720. strlist__for_each(ent, dellist) {
  721. str = xstrdup(ent->s);
  722. pr_debug("Parsing: %s\n", str);
  723. p = strchr(str, ':');
  724. if (p) {
  725. group = str;
  726. *p = '\0';
  727. event = p + 1;
  728. } else {
  729. group = "*";
  730. event = str;
  731. }
  732. pr_debug("Group: %s, Event: %s\n", group, event);
  733. del_trace_kprobe_event(fd, group, event, namelist);
  734. free(str);
  735. }
  736. strlist__delete(namelist);
  737. close(fd);
  738. }
  739. #define LINEBUF_SIZE 256
  740. #define NR_ADDITIONAL_LINES 2
  741. static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
  742. {
  743. char buf[LINEBUF_SIZE];
  744. const char *color = PERF_COLOR_BLUE;
  745. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  746. goto error;
  747. if (!skip) {
  748. if (show_num)
  749. fprintf(stdout, "%7u %s", l, buf);
  750. else
  751. color_fprintf(stdout, color, " %s", buf);
  752. }
  753. while (strlen(buf) == LINEBUF_SIZE - 1 &&
  754. buf[LINEBUF_SIZE - 2] != '\n') {
  755. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  756. goto error;
  757. if (!skip) {
  758. if (show_num)
  759. fprintf(stdout, "%s", buf);
  760. else
  761. color_fprintf(stdout, color, "%s", buf);
  762. }
  763. }
  764. return;
  765. error:
  766. if (feof(fp))
  767. die("Source file is shorter than expected.");
  768. else
  769. die("File read error: %s", strerror(errno));
  770. }
  771. void show_line_range(struct line_range *lr)
  772. {
  773. unsigned int l = 1;
  774. struct line_node *ln;
  775. FILE *fp;
  776. int fd, ret;
  777. /* Search a line range */
  778. init_vmlinux();
  779. fd = open_vmlinux();
  780. if (fd < 0)
  781. die("Could not open debuginfo file.");
  782. ret = find_line_range(fd, lr);
  783. if (ret <= 0)
  784. die("Source line is not found.\n");
  785. close(fd);
  786. setup_pager();
  787. if (lr->function)
  788. fprintf(stdout, "<%s:%d>\n", lr->function,
  789. lr->start - lr->offset);
  790. else
  791. fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
  792. fp = fopen(lr->path, "r");
  793. if (fp == NULL)
  794. die("Failed to open %s: %s", lr->path, strerror(errno));
  795. /* Skip to starting line number */
  796. while (l < lr->start)
  797. show_one_line(fp, l++, true, false);
  798. list_for_each_entry(ln, &lr->line_list, list) {
  799. while (ln->line > l)
  800. show_one_line(fp, (l++) - lr->offset, false, false);
  801. show_one_line(fp, (l++) - lr->offset, false, true);
  802. }
  803. if (lr->end == INT_MAX)
  804. lr->end = l + NR_ADDITIONAL_LINES;
  805. while (l < lr->end && !feof(fp))
  806. show_one_line(fp, (l++) - lr->offset, false, false);
  807. fclose(fp);
  808. }