probe-event.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 "trace-event.h" /* For __unused */
  44. #include "parse-events.h" /* For debugfs_path */
  45. #include "probe-event.h"
  46. #include "probe-finder.h"
  47. #define MAX_CMDLEN 256
  48. #define MAX_PROBE_ARGS 128
  49. #define PERFPROBE_GROUP "probe"
  50. bool probe_event_dry_run; /* Dry run flag */
  51. #define semantic_error(msg ...) die("Semantic error :" msg)
  52. /* If there is no space to write, returns -E2BIG. */
  53. static int e_snprintf(char *str, size_t size, const char *format, ...)
  54. __attribute__((format(printf, 3, 4)));
  55. static int e_snprintf(char *str, size_t size, const char *format, ...)
  56. {
  57. int ret;
  58. va_list ap;
  59. va_start(ap, format);
  60. ret = vsnprintf(str, size, format, ap);
  61. va_end(ap);
  62. if (ret >= (int)size)
  63. ret = -E2BIG;
  64. return ret;
  65. }
  66. static char *synthesize_perf_probe_point(struct perf_probe_point *pp);
  67. static struct map_groups kmap_groups;
  68. static struct map *kmaps[MAP__NR_TYPES];
  69. /* Initialize symbol maps and path of vmlinux */
  70. static void init_vmlinux(void)
  71. {
  72. symbol_conf.sort_by_name = true;
  73. if (symbol_conf.vmlinux_name == NULL)
  74. symbol_conf.try_vmlinux_path = true;
  75. else
  76. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  77. if (symbol__init() < 0)
  78. die("Failed to init symbol map.");
  79. map_groups__init(&kmap_groups);
  80. if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0)
  81. die("Failed to create kernel maps.");
  82. }
  83. #ifdef DWARF_SUPPORT
  84. static int open_vmlinux(void)
  85. {
  86. if (map__load(kmaps[MAP__FUNCTION], NULL) < 0) {
  87. pr_debug("Failed to load kernel map.\n");
  88. return -EINVAL;
  89. }
  90. pr_debug("Try to open %s\n", kmaps[MAP__FUNCTION]->dso->long_name);
  91. return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
  92. }
  93. static void convert_to_perf_probe_point(struct kprobe_trace_point *tp,
  94. struct perf_probe_point *pp)
  95. {
  96. struct symbol *sym;
  97. int fd, ret = 0;
  98. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  99. tp->symbol, NULL);
  100. if (sym) {
  101. fd = open_vmlinux();
  102. ret = find_perf_probe_point(fd, sym->start + tp->offset, pp);
  103. close(fd);
  104. }
  105. if (ret <= 0) {
  106. pp->function = xstrdup(tp->symbol);
  107. pp->offset = tp->offset;
  108. }
  109. pp->retprobe = tp->retprobe;
  110. }
  111. /* Try to find perf_probe_event with debuginfo */
  112. static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
  113. struct kprobe_trace_event **tevs)
  114. {
  115. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  116. int fd, ntevs;
  117. fd = open_vmlinux();
  118. if (fd < 0) {
  119. if (need_dwarf)
  120. die("Could not open debuginfo file.");
  121. pr_debug("Could not open vmlinux. Try to use symbols.\n");
  122. return 0;
  123. }
  124. /* Searching trace events corresponding to probe event */
  125. ntevs = find_kprobe_trace_events(fd, pev, tevs);
  126. close(fd);
  127. if (ntevs > 0) /* Succeeded to find trace events */
  128. return ntevs;
  129. if (ntevs == 0) /* No error but failed to find probe point. */
  130. die("Probe point '%s' not found. - probe not added.",
  131. synthesize_perf_probe_point(&pev->point));
  132. /* Error path */
  133. if (need_dwarf) {
  134. if (ntevs == -ENOENT)
  135. pr_warning("No dwarf info found in the vmlinux - "
  136. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  137. die("Could not analyze debuginfo.");
  138. }
  139. pr_debug("An error occurred in debuginfo analysis."
  140. " Try to use symbols.\n");
  141. return 0;
  142. }
  143. #define LINEBUF_SIZE 256
  144. #define NR_ADDITIONAL_LINES 2
  145. static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
  146. {
  147. char buf[LINEBUF_SIZE];
  148. const char *color = PERF_COLOR_BLUE;
  149. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  150. goto error;
  151. if (!skip) {
  152. if (show_num)
  153. fprintf(stdout, "%7u %s", l, buf);
  154. else
  155. color_fprintf(stdout, color, " %s", buf);
  156. }
  157. while (strlen(buf) == LINEBUF_SIZE - 1 &&
  158. buf[LINEBUF_SIZE - 2] != '\n') {
  159. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  160. goto error;
  161. if (!skip) {
  162. if (show_num)
  163. fprintf(stdout, "%s", buf);
  164. else
  165. color_fprintf(stdout, color, "%s", buf);
  166. }
  167. }
  168. return;
  169. error:
  170. if (feof(fp))
  171. die("Source file is shorter than expected.");
  172. else
  173. die("File read error: %s", strerror(errno));
  174. }
  175. /*
  176. * Show line-range always requires debuginfo to find source file and
  177. * line number.
  178. */
  179. void show_line_range(struct line_range *lr)
  180. {
  181. unsigned int l = 1;
  182. struct line_node *ln;
  183. FILE *fp;
  184. int fd, ret;
  185. /* Search a line range */
  186. init_vmlinux();
  187. fd = open_vmlinux();
  188. if (fd < 0)
  189. die("Could not open debuginfo file.");
  190. ret = find_line_range(fd, lr);
  191. if (ret <= 0)
  192. die("Source line is not found.\n");
  193. close(fd);
  194. setup_pager();
  195. if (lr->function)
  196. fprintf(stdout, "<%s:%d>\n", lr->function,
  197. lr->start - lr->offset);
  198. else
  199. fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
  200. fp = fopen(lr->path, "r");
  201. if (fp == NULL)
  202. die("Failed to open %s: %s", lr->path, strerror(errno));
  203. /* Skip to starting line number */
  204. while (l < lr->start)
  205. show_one_line(fp, l++, true, false);
  206. list_for_each_entry(ln, &lr->line_list, list) {
  207. while (ln->line > l)
  208. show_one_line(fp, (l++) - lr->offset, false, false);
  209. show_one_line(fp, (l++) - lr->offset, false, true);
  210. }
  211. if (lr->end == INT_MAX)
  212. lr->end = l + NR_ADDITIONAL_LINES;
  213. while (l < lr->end && !feof(fp))
  214. show_one_line(fp, (l++) - lr->offset, false, false);
  215. fclose(fp);
  216. }
  217. #else /* !DWARF_SUPPORT */
  218. static void convert_to_perf_probe_point(struct kprobe_trace_point *tp,
  219. struct perf_probe_point *pp)
  220. {
  221. pp->function = xstrdup(tp->symbol);
  222. pp->offset = tp->offset;
  223. pp->retprobe = tp->retprobe;
  224. }
  225. static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
  226. struct kprobe_trace_event **tevs __unused)
  227. {
  228. if (perf_probe_event_need_dwarf(pev))
  229. die("Debuginfo-analysis is not supported");
  230. return 0;
  231. }
  232. void show_line_range(struct line_range *lr __unused)
  233. {
  234. die("Debuginfo-analysis is not supported");
  235. }
  236. #endif
  237. void parse_line_range_desc(const char *arg, struct line_range *lr)
  238. {
  239. const char *ptr;
  240. char *tmp;
  241. /*
  242. * <Syntax>
  243. * SRC:SLN[+NUM|-ELN]
  244. * FUNC[:SLN[+NUM|-ELN]]
  245. */
  246. ptr = strchr(arg, ':');
  247. if (ptr) {
  248. lr->start = (unsigned int)strtoul(ptr + 1, &tmp, 0);
  249. if (*tmp == '+')
  250. lr->end = lr->start + (unsigned int)strtoul(tmp + 1,
  251. &tmp, 0);
  252. else if (*tmp == '-')
  253. lr->end = (unsigned int)strtoul(tmp + 1, &tmp, 0);
  254. else
  255. lr->end = 0;
  256. pr_debug("Line range is %u to %u\n", lr->start, lr->end);
  257. if (lr->end && lr->start > lr->end)
  258. semantic_error("Start line must be smaller"
  259. " than end line.");
  260. if (*tmp != '\0')
  261. semantic_error("Tailing with invalid character '%d'.",
  262. *tmp);
  263. tmp = xstrndup(arg, (ptr - arg));
  264. } else
  265. tmp = xstrdup(arg);
  266. if (strchr(tmp, '.'))
  267. lr->file = tmp;
  268. else
  269. lr->function = tmp;
  270. }
  271. /* Check the name is good for event/group */
  272. static bool check_event_name(const char *name)
  273. {
  274. if (!isalpha(*name) && *name != '_')
  275. return false;
  276. while (*++name != '\0') {
  277. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  278. return false;
  279. }
  280. return true;
  281. }
  282. /* Parse probepoint definition. */
  283. static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  284. {
  285. struct perf_probe_point *pp = &pev->point;
  286. char *ptr, *tmp;
  287. char c, nc = 0;
  288. /*
  289. * <Syntax>
  290. * perf probe [EVENT=]SRC[:LN|;PTN]
  291. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  292. *
  293. * TODO:Group name support
  294. */
  295. ptr = strpbrk(arg, ";=@+%");
  296. if (ptr && *ptr == '=') { /* Event name */
  297. *ptr = '\0';
  298. tmp = ptr + 1;
  299. ptr = strchr(arg, ':');
  300. if (ptr) /* Group name is not supported yet. */
  301. semantic_error("Group name is not supported yet.");
  302. if (!check_event_name(arg))
  303. semantic_error("%s is bad for event name -it must "
  304. "follow C symbol-naming rule.", arg);
  305. pev->event = xstrdup(arg);
  306. pev->group = NULL;
  307. arg = tmp;
  308. }
  309. ptr = strpbrk(arg, ";:+@%");
  310. if (ptr) {
  311. nc = *ptr;
  312. *ptr++ = '\0';
  313. }
  314. /* Check arg is function or file and copy it */
  315. if (strchr(arg, '.')) /* File */
  316. pp->file = xstrdup(arg);
  317. else /* Function */
  318. pp->function = xstrdup(arg);
  319. /* Parse other options */
  320. while (ptr) {
  321. arg = ptr;
  322. c = nc;
  323. if (c == ';') { /* Lazy pattern must be the last part */
  324. pp->lazy_line = xstrdup(arg);
  325. break;
  326. }
  327. ptr = strpbrk(arg, ";:+@%");
  328. if (ptr) {
  329. nc = *ptr;
  330. *ptr++ = '\0';
  331. }
  332. switch (c) {
  333. case ':': /* Line number */
  334. pp->line = strtoul(arg, &tmp, 0);
  335. if (*tmp != '\0')
  336. semantic_error("There is non-digit char"
  337. " in line number.");
  338. break;
  339. case '+': /* Byte offset from a symbol */
  340. pp->offset = strtoul(arg, &tmp, 0);
  341. if (*tmp != '\0')
  342. semantic_error("There is non-digit character"
  343. " in offset.");
  344. break;
  345. case '@': /* File name */
  346. if (pp->file)
  347. semantic_error("SRC@SRC is not allowed.");
  348. pp->file = xstrdup(arg);
  349. break;
  350. case '%': /* Probe places */
  351. if (strcmp(arg, "return") == 0) {
  352. pp->retprobe = 1;
  353. } else /* Others not supported yet */
  354. semantic_error("%%%s is not supported.", arg);
  355. break;
  356. default:
  357. DIE_IF("Program has a bug.");
  358. break;
  359. }
  360. }
  361. /* Exclusion check */
  362. if (pp->lazy_line && pp->line)
  363. semantic_error("Lazy pattern can't be used with line number.");
  364. if (pp->lazy_line && pp->offset)
  365. semantic_error("Lazy pattern can't be used with offset.");
  366. if (pp->line && pp->offset)
  367. semantic_error("Offset can't be used with line number.");
  368. if (!pp->line && !pp->lazy_line && pp->file && !pp->function)
  369. semantic_error("File always requires line number or "
  370. "lazy pattern.");
  371. if (pp->offset && !pp->function)
  372. semantic_error("Offset requires an entry function.");
  373. if (pp->retprobe && !pp->function)
  374. semantic_error("Return probe requires an entry function.");
  375. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe)
  376. semantic_error("Offset/Line/Lazy pattern can't be used with "
  377. "return probe.");
  378. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  379. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  380. pp->lazy_line);
  381. }
  382. /* Parse perf-probe event argument */
  383. static void parse_perf_probe_arg(const char *str, struct perf_probe_arg *arg)
  384. {
  385. char *tmp;
  386. struct perf_probe_arg_field **fieldp;
  387. pr_debug("parsing arg: %s into ", str);
  388. tmp = strchr(str, '=');
  389. if (tmp) {
  390. arg->name = xstrndup(str, tmp - str);
  391. str = tmp + 1;
  392. }
  393. tmp = strpbrk(str, "-.");
  394. if (!is_c_varname(str) || !tmp) {
  395. /* A variable, register, symbol or special value */
  396. arg->var = xstrdup(str);
  397. pr_debug("%s\n", arg->var);
  398. return;
  399. }
  400. /* Structure fields */
  401. arg->var = xstrndup(str, tmp - str);
  402. pr_debug("%s, ", arg->var);
  403. fieldp = &arg->field;
  404. do {
  405. *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
  406. if (*tmp == '.') {
  407. str = tmp + 1;
  408. (*fieldp)->ref = false;
  409. } else if (tmp[1] == '>') {
  410. str = tmp + 2;
  411. (*fieldp)->ref = true;
  412. } else
  413. semantic_error("Argument parse error: %s", str);
  414. tmp = strpbrk(str, "-.");
  415. if (tmp) {
  416. (*fieldp)->name = xstrndup(str, tmp - str);
  417. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  418. fieldp = &(*fieldp)->next;
  419. }
  420. } while (tmp);
  421. (*fieldp)->name = xstrdup(str);
  422. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  423. /* If no name is specified, set the last field name */
  424. if (!arg->name)
  425. arg->name = xstrdup((*fieldp)->name);
  426. }
  427. /* Parse perf-probe event command */
  428. void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  429. {
  430. char **argv;
  431. int argc, i;
  432. argv = argv_split(cmd, &argc);
  433. if (!argv)
  434. die("argv_split failed.");
  435. if (argc > MAX_PROBE_ARGS + 1)
  436. semantic_error("Too many arguments");
  437. /* Parse probe point */
  438. parse_perf_probe_point(argv[0], pev);
  439. /* Copy arguments and ensure return probe has no C argument */
  440. pev->nargs = argc - 1;
  441. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  442. for (i = 0; i < pev->nargs; i++) {
  443. parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  444. if (is_c_varname(pev->args[i].var) && pev->point.retprobe)
  445. semantic_error("You can't specify local variable for"
  446. " kretprobe");
  447. }
  448. argv_free(argv);
  449. }
  450. /* Return true if this perf_probe_event requires debuginfo */
  451. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  452. {
  453. int i;
  454. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  455. return true;
  456. for (i = 0; i < pev->nargs; i++)
  457. if (is_c_varname(pev->args[i].var))
  458. return true;
  459. return false;
  460. }
  461. /* Parse kprobe_events event into struct probe_point */
  462. void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
  463. {
  464. struct kprobe_trace_point *tp = &tev->point;
  465. char pr;
  466. char *p;
  467. int ret, i, argc;
  468. char **argv;
  469. pr_debug("Parsing kprobe_events: %s\n", cmd);
  470. argv = argv_split(cmd, &argc);
  471. if (!argv)
  472. die("argv_split failed.");
  473. if (argc < 2)
  474. semantic_error("Too less arguments.");
  475. /* Scan event and group name. */
  476. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  477. &pr, (float *)(void *)&tev->group,
  478. (float *)(void *)&tev->event);
  479. if (ret != 3)
  480. semantic_error("Failed to parse event name: %s", argv[0]);
  481. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  482. tp->retprobe = (pr == 'r');
  483. /* Scan function name and offset */
  484. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  485. &tp->offset);
  486. if (ret == 1)
  487. tp->offset = 0;
  488. tev->nargs = argc - 2;
  489. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  490. for (i = 0; i < tev->nargs; i++) {
  491. p = strchr(argv[i + 2], '=');
  492. if (p) /* We don't need which register is assigned. */
  493. *p++ = '\0';
  494. else
  495. p = argv[i + 2];
  496. tev->args[i].name = xstrdup(argv[i + 2]);
  497. /* TODO: parse regs and offset */
  498. tev->args[i].value = xstrdup(p);
  499. }
  500. argv_free(argv);
  501. }
  502. /* Compose only probe arg */
  503. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  504. {
  505. struct perf_probe_arg_field *field = pa->field;
  506. int ret;
  507. char *tmp = buf;
  508. if (pa->name && pa->var)
  509. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  510. else
  511. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  512. if (ret <= 0)
  513. goto error;
  514. tmp += ret;
  515. len -= ret;
  516. while (field) {
  517. ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
  518. field->name);
  519. if (ret <= 0)
  520. goto error;
  521. tmp += ret;
  522. len -= ret;
  523. field = field->next;
  524. }
  525. return tmp - buf;
  526. error:
  527. die("Failed to synthesize perf probe argument: %s", strerror(-ret));
  528. }
  529. /* Compose only probe point (not argument) */
  530. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  531. {
  532. char *buf, *tmp;
  533. char offs[32] = "", line[32] = "", file[32] = "";
  534. int ret, len;
  535. buf = xzalloc(MAX_CMDLEN);
  536. if (pp->offset) {
  537. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  538. if (ret <= 0)
  539. goto error;
  540. }
  541. if (pp->line) {
  542. ret = e_snprintf(line, 32, ":%d", pp->line);
  543. if (ret <= 0)
  544. goto error;
  545. }
  546. if (pp->file) {
  547. len = strlen(pp->file) - 32;
  548. if (len < 0)
  549. len = 0;
  550. tmp = strchr(pp->file + len, '/');
  551. if (!tmp)
  552. tmp = pp->file + len - 1;
  553. ret = e_snprintf(file, 32, "@%s", tmp + 1);
  554. if (ret <= 0)
  555. goto error;
  556. }
  557. if (pp->function)
  558. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  559. offs, pp->retprobe ? "%return" : "", line,
  560. file);
  561. else
  562. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  563. if (ret <= 0)
  564. goto error;
  565. return buf;
  566. error:
  567. die("Failed to synthesize perf probe point: %s", strerror(-ret));
  568. }
  569. #if 0
  570. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  571. {
  572. char *buf;
  573. int i, len, ret;
  574. buf = synthesize_perf_probe_point(&pev->point);
  575. if (!buf)
  576. return NULL;
  577. len = strlen(buf);
  578. for (i = 0; i < pev->nargs; i++) {
  579. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  580. pev->args[i].name);
  581. if (ret <= 0) {
  582. free(buf);
  583. return NULL;
  584. }
  585. len += ret;
  586. }
  587. return buf;
  588. }
  589. #endif
  590. static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
  591. char **buf, size_t *buflen,
  592. int depth)
  593. {
  594. int ret;
  595. if (ref->next) {
  596. depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
  597. buflen, depth + 1);
  598. if (depth < 0)
  599. goto out;
  600. }
  601. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  602. if (ret < 0)
  603. depth = ret;
  604. else {
  605. *buf += ret;
  606. *buflen -= ret;
  607. }
  608. out:
  609. return depth;
  610. }
  611. static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
  612. char *buf, size_t buflen)
  613. {
  614. int ret, depth = 0;
  615. char *tmp = buf;
  616. /* Argument name or separator */
  617. if (arg->name)
  618. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  619. else
  620. ret = e_snprintf(buf, buflen, " ");
  621. if (ret < 0)
  622. return ret;
  623. buf += ret;
  624. buflen -= ret;
  625. /* Dereferencing arguments */
  626. if (arg->ref) {
  627. depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
  628. &buflen, 1);
  629. if (depth < 0)
  630. return depth;
  631. }
  632. /* Print argument value */
  633. ret = e_snprintf(buf, buflen, "%s", arg->value);
  634. if (ret < 0)
  635. return ret;
  636. buf += ret;
  637. buflen -= ret;
  638. /* Closing */
  639. while (depth--) {
  640. ret = e_snprintf(buf, buflen, ")");
  641. if (ret < 0)
  642. return ret;
  643. buf += ret;
  644. buflen -= ret;
  645. }
  646. return buf - tmp;
  647. }
  648. char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
  649. {
  650. struct kprobe_trace_point *tp = &tev->point;
  651. char *buf;
  652. int i, len, ret;
  653. buf = xzalloc(MAX_CMDLEN);
  654. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  655. tp->retprobe ? 'r' : 'p',
  656. tev->group, tev->event,
  657. tp->symbol, tp->offset);
  658. if (len <= 0)
  659. goto error;
  660. for (i = 0; i < tev->nargs; i++) {
  661. ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
  662. MAX_CMDLEN - len);
  663. if (ret <= 0)
  664. goto error;
  665. len += ret;
  666. }
  667. return buf;
  668. error:
  669. free(buf);
  670. return NULL;
  671. }
  672. void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
  673. struct perf_probe_event *pev)
  674. {
  675. char buf[64];
  676. int i;
  677. /* Convert event/group name */
  678. pev->event = xstrdup(tev->event);
  679. pev->group = xstrdup(tev->group);
  680. /* Convert trace_point to probe_point */
  681. convert_to_perf_probe_point(&tev->point, &pev->point);
  682. /* Convert trace_arg to probe_arg */
  683. pev->nargs = tev->nargs;
  684. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  685. for (i = 0; i < tev->nargs; i++)
  686. if (tev->args[i].name)
  687. pev->args[i].name = xstrdup(tev->args[i].name);
  688. else {
  689. synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
  690. pev->args[i].name = xstrdup(buf);
  691. }
  692. }
  693. void clear_perf_probe_event(struct perf_probe_event *pev)
  694. {
  695. struct perf_probe_point *pp = &pev->point;
  696. struct perf_probe_arg_field *field, *next;
  697. int i;
  698. if (pev->event)
  699. free(pev->event);
  700. if (pev->group)
  701. free(pev->group);
  702. if (pp->file)
  703. free(pp->file);
  704. if (pp->function)
  705. free(pp->function);
  706. if (pp->lazy_line)
  707. free(pp->lazy_line);
  708. for (i = 0; i < pev->nargs; i++) {
  709. if (pev->args[i].name)
  710. free(pev->args[i].name);
  711. if (pev->args[i].var)
  712. free(pev->args[i].var);
  713. field = pev->args[i].field;
  714. while (field) {
  715. next = field->next;
  716. if (field->name)
  717. free(field->name);
  718. free(field);
  719. field = next;
  720. }
  721. }
  722. if (pev->args)
  723. free(pev->args);
  724. memset(pev, 0, sizeof(*pev));
  725. }
  726. void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
  727. {
  728. struct kprobe_trace_arg_ref *ref, *next;
  729. int i;
  730. if (tev->event)
  731. free(tev->event);
  732. if (tev->group)
  733. free(tev->group);
  734. if (tev->point.symbol)
  735. free(tev->point.symbol);
  736. for (i = 0; i < tev->nargs; i++) {
  737. if (tev->args[i].name)
  738. free(tev->args[i].name);
  739. if (tev->args[i].value)
  740. free(tev->args[i].value);
  741. ref = tev->args[i].ref;
  742. while (ref) {
  743. next = ref->next;
  744. free(ref);
  745. ref = next;
  746. }
  747. }
  748. if (tev->args)
  749. free(tev->args);
  750. memset(tev, 0, sizeof(*tev));
  751. }
  752. static int open_kprobe_events(bool readwrite)
  753. {
  754. char buf[PATH_MAX];
  755. int ret;
  756. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  757. if (ret < 0)
  758. die("Failed to make kprobe_events path.");
  759. if (readwrite && !probe_event_dry_run)
  760. ret = open(buf, O_RDWR, O_APPEND);
  761. else
  762. ret = open(buf, O_RDONLY, 0);
  763. if (ret < 0) {
  764. if (errno == ENOENT)
  765. die("kprobe_events file does not exist -"
  766. " please rebuild with CONFIG_KPROBE_EVENT.");
  767. else
  768. die("Could not open kprobe_events file: %s",
  769. strerror(errno));
  770. }
  771. return ret;
  772. }
  773. /* Get raw string list of current kprobe_events */
  774. static struct strlist *get_kprobe_trace_command_rawlist(int fd)
  775. {
  776. int ret, idx;
  777. FILE *fp;
  778. char buf[MAX_CMDLEN];
  779. char *p;
  780. struct strlist *sl;
  781. sl = strlist__new(true, NULL);
  782. fp = fdopen(dup(fd), "r");
  783. while (!feof(fp)) {
  784. p = fgets(buf, MAX_CMDLEN, fp);
  785. if (!p)
  786. break;
  787. idx = strlen(p) - 1;
  788. if (p[idx] == '\n')
  789. p[idx] = '\0';
  790. ret = strlist__add(sl, buf);
  791. if (ret < 0)
  792. die("strlist__add failed: %s", strerror(-ret));
  793. }
  794. fclose(fp);
  795. return sl;
  796. }
  797. /* Show an event */
  798. static void show_perf_probe_event(struct perf_probe_event *pev)
  799. {
  800. int i, ret;
  801. char buf[128];
  802. char *place;
  803. /* Synthesize only event probe point */
  804. place = synthesize_perf_probe_point(&pev->point);
  805. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  806. if (ret < 0)
  807. die("Failed to copy event: %s", strerror(-ret));
  808. printf(" %-20s (on %s", buf, place);
  809. if (pev->nargs > 0) {
  810. printf(" with");
  811. for (i = 0; i < pev->nargs; i++) {
  812. synthesize_perf_probe_arg(&pev->args[i], buf, 128);
  813. printf(" %s", buf);
  814. }
  815. }
  816. printf(")\n");
  817. free(place);
  818. }
  819. /* List up current perf-probe events */
  820. void show_perf_probe_events(void)
  821. {
  822. int fd;
  823. struct kprobe_trace_event tev;
  824. struct perf_probe_event pev;
  825. struct strlist *rawlist;
  826. struct str_node *ent;
  827. setup_pager();
  828. init_vmlinux();
  829. memset(&tev, 0, sizeof(tev));
  830. memset(&pev, 0, sizeof(pev));
  831. fd = open_kprobe_events(false);
  832. rawlist = get_kprobe_trace_command_rawlist(fd);
  833. close(fd);
  834. strlist__for_each(ent, rawlist) {
  835. parse_kprobe_trace_command(ent->s, &tev);
  836. convert_to_perf_probe_event(&tev, &pev);
  837. /* Show an event */
  838. show_perf_probe_event(&pev);
  839. clear_perf_probe_event(&pev);
  840. clear_kprobe_trace_event(&tev);
  841. }
  842. strlist__delete(rawlist);
  843. }
  844. /* Get current perf-probe event names */
  845. static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
  846. {
  847. char buf[128];
  848. struct strlist *sl, *rawlist;
  849. struct str_node *ent;
  850. struct kprobe_trace_event tev;
  851. memset(&tev, 0, sizeof(tev));
  852. rawlist = get_kprobe_trace_command_rawlist(fd);
  853. sl = strlist__new(true, NULL);
  854. strlist__for_each(ent, rawlist) {
  855. parse_kprobe_trace_command(ent->s, &tev);
  856. if (include_group) {
  857. if (e_snprintf(buf, 128, "%s:%s", tev.group,
  858. tev.event) < 0)
  859. die("Failed to copy group:event name.");
  860. strlist__add(sl, buf);
  861. } else
  862. strlist__add(sl, tev.event);
  863. clear_kprobe_trace_event(&tev);
  864. }
  865. strlist__delete(rawlist);
  866. return sl;
  867. }
  868. static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
  869. {
  870. int ret;
  871. char *buf = synthesize_kprobe_trace_command(tev);
  872. pr_debug("Writing event: %s\n", buf);
  873. if (!probe_event_dry_run) {
  874. ret = write(fd, buf, strlen(buf));
  875. if (ret <= 0)
  876. die("Failed to write event: %s", strerror(errno));
  877. }
  878. free(buf);
  879. }
  880. static void get_new_event_name(char *buf, size_t len, const char *base,
  881. struct strlist *namelist, bool allow_suffix)
  882. {
  883. int i, ret;
  884. /* Try no suffix */
  885. ret = e_snprintf(buf, len, "%s", base);
  886. if (ret < 0)
  887. die("snprintf() failed: %s", strerror(-ret));
  888. if (!strlist__has_entry(namelist, buf))
  889. return;
  890. if (!allow_suffix) {
  891. pr_warning("Error: event \"%s\" already exists. "
  892. "(Use -f to force duplicates.)\n", base);
  893. die("Can't add new event.");
  894. }
  895. /* Try to add suffix */
  896. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  897. ret = e_snprintf(buf, len, "%s_%d", base, i);
  898. if (ret < 0)
  899. die("snprintf() failed: %s", strerror(-ret));
  900. if (!strlist__has_entry(namelist, buf))
  901. break;
  902. }
  903. if (i == MAX_EVENT_INDEX)
  904. die("Too many events are on the same function.");
  905. }
  906. static void __add_kprobe_trace_events(struct perf_probe_event *pev,
  907. struct kprobe_trace_event *tevs,
  908. int ntevs, bool allow_suffix)
  909. {
  910. int i, fd;
  911. struct kprobe_trace_event *tev = NULL;
  912. char buf[64];
  913. const char *event, *group;
  914. struct strlist *namelist;
  915. fd = open_kprobe_events(true);
  916. /* Get current event names */
  917. namelist = get_kprobe_trace_event_names(fd, false);
  918. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  919. for (i = 0; i < ntevs; i++) {
  920. tev = &tevs[i];
  921. if (pev->event)
  922. event = pev->event;
  923. else
  924. if (pev->point.function)
  925. event = pev->point.function;
  926. else
  927. event = tev->point.symbol;
  928. if (pev->group)
  929. group = pev->group;
  930. else
  931. group = PERFPROBE_GROUP;
  932. /* Get an unused new event name */
  933. get_new_event_name(buf, 64, event, namelist, allow_suffix);
  934. event = buf;
  935. tev->event = xstrdup(event);
  936. tev->group = xstrdup(group);
  937. write_kprobe_trace_event(fd, tev);
  938. /* Add added event name to namelist */
  939. strlist__add(namelist, event);
  940. /* Trick here - save current event/group */
  941. event = pev->event;
  942. group = pev->group;
  943. pev->event = tev->event;
  944. pev->group = tev->group;
  945. show_perf_probe_event(pev);
  946. /* Trick here - restore current event/group */
  947. pev->event = (char *)event;
  948. pev->group = (char *)group;
  949. /*
  950. * Probes after the first probe which comes from same
  951. * user input are always allowed to add suffix, because
  952. * there might be several addresses corresponding to
  953. * one code line.
  954. */
  955. allow_suffix = true;
  956. }
  957. /* Show how to use the event. */
  958. printf("\nYou can now use it on all perf tools, such as:\n\n");
  959. printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
  960. strlist__delete(namelist);
  961. close(fd);
  962. }
  963. static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
  964. struct kprobe_trace_event **tevs)
  965. {
  966. struct symbol *sym;
  967. int ntevs = 0, i;
  968. struct kprobe_trace_event *tev;
  969. /* Convert perf_probe_event with debuginfo */
  970. ntevs = try_to_find_kprobe_trace_events(pev, tevs);
  971. if (ntevs > 0)
  972. return ntevs;
  973. /* Allocate trace event buffer */
  974. ntevs = 1;
  975. tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
  976. /* Copy parameters */
  977. tev->point.symbol = xstrdup(pev->point.function);
  978. tev->point.offset = pev->point.offset;
  979. tev->nargs = pev->nargs;
  980. if (tev->nargs) {
  981. tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
  982. * tev->nargs);
  983. for (i = 0; i < tev->nargs; i++) {
  984. if (pev->args[i].name)
  985. tev->args[i].name = xstrdup(pev->args[i].name);
  986. tev->args[i].value = xstrdup(pev->args[i].var);
  987. }
  988. }
  989. /* Currently just checking function name from symbol map */
  990. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  991. tev->point.symbol, NULL);
  992. if (!sym)
  993. die("Kernel symbol \'%s\' not found - probe not added.",
  994. tev->point.symbol);
  995. return ntevs;
  996. }
  997. struct __event_package {
  998. struct perf_probe_event *pev;
  999. struct kprobe_trace_event *tevs;
  1000. int ntevs;
  1001. };
  1002. void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1003. bool force_add)
  1004. {
  1005. int i;
  1006. struct __event_package *pkgs;
  1007. pkgs = xzalloc(sizeof(struct __event_package) * npevs);
  1008. /* Init vmlinux path */
  1009. init_vmlinux();
  1010. /* Loop 1: convert all events */
  1011. for (i = 0; i < npevs; i++) {
  1012. pkgs[i].pev = &pevs[i];
  1013. /* Convert with or without debuginfo */
  1014. pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
  1015. &pkgs[i].tevs);
  1016. }
  1017. /* Loop 2: add all events */
  1018. for (i = 0; i < npevs; i++)
  1019. __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1020. pkgs[i].ntevs, force_add);
  1021. /* TODO: cleanup all trace events? */
  1022. }
  1023. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  1024. {
  1025. char *p;
  1026. char buf[128];
  1027. int ret;
  1028. /* Convert from perf-probe event to trace-kprobe event */
  1029. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  1030. die("Failed to copy event.");
  1031. p = strchr(buf + 2, ':');
  1032. if (!p)
  1033. die("Internal error: %s should have ':' but not.", ent->s);
  1034. *p = '/';
  1035. pr_debug("Writing event: %s\n", buf);
  1036. ret = write(fd, buf, strlen(buf));
  1037. if (ret <= 0)
  1038. die("Failed to write event: %s", strerror(errno));
  1039. printf("Remove event: %s\n", ent->s);
  1040. }
  1041. static void del_trace_kprobe_event(int fd, const char *group,
  1042. const char *event, struct strlist *namelist)
  1043. {
  1044. char buf[128];
  1045. struct str_node *ent, *n;
  1046. int found = 0;
  1047. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  1048. die("Failed to copy event.");
  1049. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1050. strlist__for_each_safe(ent, n, namelist)
  1051. if (strglobmatch(ent->s, buf)) {
  1052. found++;
  1053. __del_trace_kprobe_event(fd, ent);
  1054. strlist__remove(namelist, ent);
  1055. }
  1056. } else {
  1057. ent = strlist__find(namelist, buf);
  1058. if (ent) {
  1059. found++;
  1060. __del_trace_kprobe_event(fd, ent);
  1061. strlist__remove(namelist, ent);
  1062. }
  1063. }
  1064. if (found == 0)
  1065. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  1066. }
  1067. void del_perf_probe_events(struct strlist *dellist)
  1068. {
  1069. int fd;
  1070. const char *group, *event;
  1071. char *p, *str;
  1072. struct str_node *ent;
  1073. struct strlist *namelist;
  1074. fd = open_kprobe_events(true);
  1075. /* Get current event names */
  1076. namelist = get_kprobe_trace_event_names(fd, true);
  1077. strlist__for_each(ent, dellist) {
  1078. str = xstrdup(ent->s);
  1079. pr_debug("Parsing: %s\n", str);
  1080. p = strchr(str, ':');
  1081. if (p) {
  1082. group = str;
  1083. *p = '\0';
  1084. event = p + 1;
  1085. } else {
  1086. group = "*";
  1087. event = str;
  1088. }
  1089. pr_debug("Group: %s, Event: %s\n", group, event);
  1090. del_trace_kprobe_event(fd, group, event, namelist);
  1091. free(str);
  1092. }
  1093. strlist__delete(namelist);
  1094. close(fd);
  1095. }