probe-event.c 22 KB

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