probe-event.c 19 KB

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