probe-event.c 18 KB

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