probe-event.c 19 KB

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