probe-event.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. const char *tmp;
  386. struct perf_probe_arg_field **fieldp;
  387. pr_debug("parsing arg: %s into ", str);
  388. tmp = strpbrk(str, "-.");
  389. if (!is_c_varname(str) || !tmp) {
  390. /* A variable, register, symbol or special value */
  391. arg->name = xstrdup(str);
  392. pr_debug("%s\n", arg->name);
  393. return;
  394. }
  395. /* Structure fields */
  396. arg->name = xstrndup(str, tmp - str);
  397. pr_debug("%s, ", arg->name);
  398. fieldp = &arg->field;
  399. do {
  400. *fieldp = xzalloc(sizeof(struct perf_probe_arg_field));
  401. if (*tmp == '.') {
  402. str = tmp + 1;
  403. (*fieldp)->ref = false;
  404. } else if (tmp[1] == '>') {
  405. str = tmp + 2;
  406. (*fieldp)->ref = true;
  407. } else
  408. semantic_error("Argument parse error: %s", str);
  409. tmp = strpbrk(str, "-.");
  410. if (tmp) {
  411. (*fieldp)->name = xstrndup(str, tmp - str);
  412. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  413. fieldp = &(*fieldp)->next;
  414. }
  415. } while (tmp);
  416. (*fieldp)->name = xstrdup(str);
  417. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  418. }
  419. /* Parse perf-probe event command */
  420. void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  421. {
  422. char **argv;
  423. int argc, i;
  424. argv = argv_split(cmd, &argc);
  425. if (!argv)
  426. die("argv_split failed.");
  427. if (argc > MAX_PROBE_ARGS + 1)
  428. semantic_error("Too many arguments");
  429. /* Parse probe point */
  430. parse_perf_probe_point(argv[0], pev);
  431. /* Copy arguments and ensure return probe has no C argument */
  432. pev->nargs = argc - 1;
  433. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  434. for (i = 0; i < pev->nargs; i++) {
  435. parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  436. if (is_c_varname(pev->args[i].name) && pev->point.retprobe)
  437. semantic_error("You can't specify local variable for"
  438. " kretprobe");
  439. }
  440. argv_free(argv);
  441. }
  442. /* Return true if this perf_probe_event requires debuginfo */
  443. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  444. {
  445. int i;
  446. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  447. return true;
  448. for (i = 0; i < pev->nargs; i++)
  449. if (is_c_varname(pev->args[i].name))
  450. return true;
  451. return false;
  452. }
  453. /* Parse kprobe_events event into struct probe_point */
  454. void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
  455. {
  456. struct kprobe_trace_point *tp = &tev->point;
  457. char pr;
  458. char *p;
  459. int ret, i, argc;
  460. char **argv;
  461. pr_debug("Parsing kprobe_events: %s\n", cmd);
  462. argv = argv_split(cmd, &argc);
  463. if (!argv)
  464. die("argv_split failed.");
  465. if (argc < 2)
  466. semantic_error("Too less arguments.");
  467. /* Scan event and group name. */
  468. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  469. &pr, (float *)(void *)&tev->group,
  470. (float *)(void *)&tev->event);
  471. if (ret != 3)
  472. semantic_error("Failed to parse event name: %s", argv[0]);
  473. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  474. tp->retprobe = (pr == 'r');
  475. /* Scan function name and offset */
  476. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  477. &tp->offset);
  478. if (ret == 1)
  479. tp->offset = 0;
  480. tev->nargs = argc - 2;
  481. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  482. for (i = 0; i < tev->nargs; i++) {
  483. p = strchr(argv[i + 2], '=');
  484. if (p) /* We don't need which register is assigned. */
  485. *p++ = '\0';
  486. else
  487. p = argv[i + 2];
  488. tev->args[i].name = xstrdup(argv[i + 2]);
  489. /* TODO: parse regs and offset */
  490. tev->args[i].value = xstrdup(p);
  491. }
  492. argv_free(argv);
  493. }
  494. /* Compose only probe arg */
  495. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  496. {
  497. struct perf_probe_arg_field *field = pa->field;
  498. int ret;
  499. char *tmp = buf;
  500. ret = e_snprintf(tmp, len, "%s", pa->name);
  501. if (ret <= 0)
  502. goto error;
  503. tmp += ret;
  504. len -= ret;
  505. while (field) {
  506. ret = e_snprintf(tmp, len, "%s%s", field->ref ? "->" : ".",
  507. field->name);
  508. if (ret <= 0)
  509. goto error;
  510. tmp += ret;
  511. len -= ret;
  512. field = field->next;
  513. }
  514. return tmp - buf;
  515. error:
  516. die("Failed to synthesize perf probe argument: %s", strerror(-ret));
  517. }
  518. /* Compose only probe point (not argument) */
  519. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  520. {
  521. char *buf, *tmp;
  522. char offs[32] = "", line[32] = "", file[32] = "";
  523. int ret, len;
  524. buf = xzalloc(MAX_CMDLEN);
  525. if (pp->offset) {
  526. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  527. if (ret <= 0)
  528. goto error;
  529. }
  530. if (pp->line) {
  531. ret = e_snprintf(line, 32, ":%d", pp->line);
  532. if (ret <= 0)
  533. goto error;
  534. }
  535. if (pp->file) {
  536. len = strlen(pp->file) - 32;
  537. if (len < 0)
  538. len = 0;
  539. tmp = strchr(pp->file + len, '/');
  540. if (!tmp)
  541. tmp = pp->file + len - 1;
  542. ret = e_snprintf(file, 32, "@%s", tmp + 1);
  543. if (ret <= 0)
  544. goto error;
  545. }
  546. if (pp->function)
  547. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  548. offs, pp->retprobe ? "%return" : "", line,
  549. file);
  550. else
  551. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  552. if (ret <= 0)
  553. goto error;
  554. return buf;
  555. error:
  556. die("Failed to synthesize perf probe point: %s", strerror(-ret));
  557. }
  558. #if 0
  559. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  560. {
  561. char *buf;
  562. int i, len, ret;
  563. buf = synthesize_perf_probe_point(&pev->point);
  564. if (!buf)
  565. return NULL;
  566. len = strlen(buf);
  567. for (i = 0; i < pev->nargs; i++) {
  568. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  569. pev->args[i].name);
  570. if (ret <= 0) {
  571. free(buf);
  572. return NULL;
  573. }
  574. len += ret;
  575. }
  576. return buf;
  577. }
  578. #endif
  579. static int __synthesize_kprobe_trace_arg_ref(struct kprobe_trace_arg_ref *ref,
  580. char **buf, size_t *buflen,
  581. int depth)
  582. {
  583. int ret;
  584. if (ref->next) {
  585. depth = __synthesize_kprobe_trace_arg_ref(ref->next, buf,
  586. buflen, depth + 1);
  587. if (depth < 0)
  588. goto out;
  589. }
  590. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  591. if (ret < 0)
  592. depth = ret;
  593. else {
  594. *buf += ret;
  595. *buflen -= ret;
  596. }
  597. out:
  598. return depth;
  599. }
  600. static int synthesize_kprobe_trace_arg(struct kprobe_trace_arg *arg,
  601. char *buf, size_t buflen)
  602. {
  603. int ret, depth = 0;
  604. char *tmp = buf;
  605. /* Argument name or separator */
  606. if (arg->name)
  607. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  608. else
  609. ret = e_snprintf(buf, buflen, " ");
  610. if (ret < 0)
  611. return ret;
  612. buf += ret;
  613. buflen -= ret;
  614. /* Dereferencing arguments */
  615. if (arg->ref) {
  616. depth = __synthesize_kprobe_trace_arg_ref(arg->ref, &buf,
  617. &buflen, 1);
  618. if (depth < 0)
  619. return depth;
  620. }
  621. /* Print argument value */
  622. ret = e_snprintf(buf, buflen, "%s", arg->value);
  623. if (ret < 0)
  624. return ret;
  625. buf += ret;
  626. buflen -= ret;
  627. /* Closing */
  628. while (depth--) {
  629. ret = e_snprintf(buf, buflen, ")");
  630. if (ret < 0)
  631. return ret;
  632. buf += ret;
  633. buflen -= ret;
  634. }
  635. return buf - tmp;
  636. }
  637. char *synthesize_kprobe_trace_command(struct kprobe_trace_event *tev)
  638. {
  639. struct kprobe_trace_point *tp = &tev->point;
  640. char *buf;
  641. int i, len, ret;
  642. buf = xzalloc(MAX_CMDLEN);
  643. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  644. tp->retprobe ? 'r' : 'p',
  645. tev->group, tev->event,
  646. tp->symbol, tp->offset);
  647. if (len <= 0)
  648. goto error;
  649. for (i = 0; i < tev->nargs; i++) {
  650. ret = synthesize_kprobe_trace_arg(&tev->args[i], buf + len,
  651. MAX_CMDLEN - len);
  652. if (ret <= 0)
  653. goto error;
  654. len += ret;
  655. }
  656. return buf;
  657. error:
  658. free(buf);
  659. return NULL;
  660. }
  661. void convert_to_perf_probe_event(struct kprobe_trace_event *tev,
  662. struct perf_probe_event *pev)
  663. {
  664. char buf[64];
  665. int i;
  666. /* Convert event/group name */
  667. pev->event = xstrdup(tev->event);
  668. pev->group = xstrdup(tev->group);
  669. /* Convert trace_point to probe_point */
  670. convert_to_perf_probe_point(&tev->point, &pev->point);
  671. /* Convert trace_arg to probe_arg */
  672. pev->nargs = tev->nargs;
  673. pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  674. for (i = 0; i < tev->nargs; i++)
  675. if (tev->args[i].name)
  676. pev->args[i].name = xstrdup(tev->args[i].name);
  677. else {
  678. synthesize_kprobe_trace_arg(&tev->args[i], buf, 64);
  679. pev->args[i].name = xstrdup(buf);
  680. }
  681. }
  682. void clear_perf_probe_event(struct perf_probe_event *pev)
  683. {
  684. struct perf_probe_point *pp = &pev->point;
  685. struct perf_probe_arg_field *field, *next;
  686. int i;
  687. if (pev->event)
  688. free(pev->event);
  689. if (pev->group)
  690. free(pev->group);
  691. if (pp->file)
  692. free(pp->file);
  693. if (pp->function)
  694. free(pp->function);
  695. if (pp->lazy_line)
  696. free(pp->lazy_line);
  697. for (i = 0; i < pev->nargs; i++) {
  698. if (pev->args[i].name)
  699. free(pev->args[i].name);
  700. field = pev->args[i].field;
  701. while (field) {
  702. next = field->next;
  703. if (field->name)
  704. free(field->name);
  705. free(field);
  706. field = next;
  707. }
  708. }
  709. if (pev->args)
  710. free(pev->args);
  711. memset(pev, 0, sizeof(*pev));
  712. }
  713. void clear_kprobe_trace_event(struct kprobe_trace_event *tev)
  714. {
  715. struct kprobe_trace_arg_ref *ref, *next;
  716. int i;
  717. if (tev->event)
  718. free(tev->event);
  719. if (tev->group)
  720. free(tev->group);
  721. if (tev->point.symbol)
  722. free(tev->point.symbol);
  723. for (i = 0; i < tev->nargs; i++) {
  724. if (tev->args[i].name)
  725. free(tev->args[i].name);
  726. if (tev->args[i].value)
  727. free(tev->args[i].value);
  728. ref = tev->args[i].ref;
  729. while (ref) {
  730. next = ref->next;
  731. free(ref);
  732. ref = next;
  733. }
  734. }
  735. if (tev->args)
  736. free(tev->args);
  737. memset(tev, 0, sizeof(*tev));
  738. }
  739. static int open_kprobe_events(bool readwrite)
  740. {
  741. char buf[PATH_MAX];
  742. int ret;
  743. ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
  744. if (ret < 0)
  745. die("Failed to make kprobe_events path.");
  746. if (readwrite && !probe_event_dry_run)
  747. ret = open(buf, O_RDWR, O_APPEND);
  748. else
  749. ret = open(buf, O_RDONLY, 0);
  750. if (ret < 0) {
  751. if (errno == ENOENT)
  752. die("kprobe_events file does not exist -"
  753. " please rebuild with CONFIG_KPROBE_EVENT.");
  754. else
  755. die("Could not open kprobe_events file: %s",
  756. strerror(errno));
  757. }
  758. return ret;
  759. }
  760. /* Get raw string list of current kprobe_events */
  761. static struct strlist *get_kprobe_trace_command_rawlist(int fd)
  762. {
  763. int ret, idx;
  764. FILE *fp;
  765. char buf[MAX_CMDLEN];
  766. char *p;
  767. struct strlist *sl;
  768. sl = strlist__new(true, NULL);
  769. fp = fdopen(dup(fd), "r");
  770. while (!feof(fp)) {
  771. p = fgets(buf, MAX_CMDLEN, fp);
  772. if (!p)
  773. break;
  774. idx = strlen(p) - 1;
  775. if (p[idx] == '\n')
  776. p[idx] = '\0';
  777. ret = strlist__add(sl, buf);
  778. if (ret < 0)
  779. die("strlist__add failed: %s", strerror(-ret));
  780. }
  781. fclose(fp);
  782. return sl;
  783. }
  784. /* Show an event */
  785. static void show_perf_probe_event(struct perf_probe_event *pev)
  786. {
  787. int i, ret;
  788. char buf[128];
  789. char *place;
  790. /* Synthesize only event probe point */
  791. place = synthesize_perf_probe_point(&pev->point);
  792. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  793. if (ret < 0)
  794. die("Failed to copy event: %s", strerror(-ret));
  795. printf(" %-20s (on %s", buf, place);
  796. if (pev->nargs > 0) {
  797. printf(" with");
  798. for (i = 0; i < pev->nargs; i++) {
  799. synthesize_perf_probe_arg(&pev->args[i], buf, 128);
  800. printf(" %s", buf);
  801. }
  802. }
  803. printf(")\n");
  804. free(place);
  805. }
  806. /* List up current perf-probe events */
  807. void show_perf_probe_events(void)
  808. {
  809. int fd;
  810. struct kprobe_trace_event tev;
  811. struct perf_probe_event pev;
  812. struct strlist *rawlist;
  813. struct str_node *ent;
  814. setup_pager();
  815. init_vmlinux();
  816. memset(&tev, 0, sizeof(tev));
  817. memset(&pev, 0, sizeof(pev));
  818. fd = open_kprobe_events(false);
  819. rawlist = get_kprobe_trace_command_rawlist(fd);
  820. close(fd);
  821. strlist__for_each(ent, rawlist) {
  822. parse_kprobe_trace_command(ent->s, &tev);
  823. convert_to_perf_probe_event(&tev, &pev);
  824. /* Show an event */
  825. show_perf_probe_event(&pev);
  826. clear_perf_probe_event(&pev);
  827. clear_kprobe_trace_event(&tev);
  828. }
  829. strlist__delete(rawlist);
  830. }
  831. /* Get current perf-probe event names */
  832. static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
  833. {
  834. char buf[128];
  835. struct strlist *sl, *rawlist;
  836. struct str_node *ent;
  837. struct kprobe_trace_event tev;
  838. memset(&tev, 0, sizeof(tev));
  839. rawlist = get_kprobe_trace_command_rawlist(fd);
  840. sl = strlist__new(true, NULL);
  841. strlist__for_each(ent, rawlist) {
  842. parse_kprobe_trace_command(ent->s, &tev);
  843. if (include_group) {
  844. if (e_snprintf(buf, 128, "%s:%s", tev.group,
  845. tev.event) < 0)
  846. die("Failed to copy group:event name.");
  847. strlist__add(sl, buf);
  848. } else
  849. strlist__add(sl, tev.event);
  850. clear_kprobe_trace_event(&tev);
  851. }
  852. strlist__delete(rawlist);
  853. return sl;
  854. }
  855. static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
  856. {
  857. int ret;
  858. char *buf = synthesize_kprobe_trace_command(tev);
  859. pr_debug("Writing event: %s\n", buf);
  860. if (!probe_event_dry_run) {
  861. ret = write(fd, buf, strlen(buf));
  862. if (ret <= 0)
  863. die("Failed to write event: %s", strerror(errno));
  864. }
  865. free(buf);
  866. }
  867. static void get_new_event_name(char *buf, size_t len, const char *base,
  868. struct strlist *namelist, bool allow_suffix)
  869. {
  870. int i, ret;
  871. /* Try no suffix */
  872. ret = e_snprintf(buf, len, "%s", base);
  873. if (ret < 0)
  874. die("snprintf() failed: %s", strerror(-ret));
  875. if (!strlist__has_entry(namelist, buf))
  876. return;
  877. if (!allow_suffix) {
  878. pr_warning("Error: event \"%s\" already exists. "
  879. "(Use -f to force duplicates.)\n", base);
  880. die("Can't add new event.");
  881. }
  882. /* Try to add suffix */
  883. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  884. ret = e_snprintf(buf, len, "%s_%d", base, i);
  885. if (ret < 0)
  886. die("snprintf() failed: %s", strerror(-ret));
  887. if (!strlist__has_entry(namelist, buf))
  888. break;
  889. }
  890. if (i == MAX_EVENT_INDEX)
  891. die("Too many events are on the same function.");
  892. }
  893. static void __add_kprobe_trace_events(struct perf_probe_event *pev,
  894. struct kprobe_trace_event *tevs,
  895. int ntevs, bool allow_suffix)
  896. {
  897. int i, fd;
  898. struct kprobe_trace_event *tev = NULL;
  899. char buf[64];
  900. const char *event, *group;
  901. struct strlist *namelist;
  902. fd = open_kprobe_events(true);
  903. /* Get current event names */
  904. namelist = get_kprobe_trace_event_names(fd, false);
  905. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  906. for (i = 0; i < ntevs; i++) {
  907. tev = &tevs[i];
  908. if (pev->event)
  909. event = pev->event;
  910. else
  911. if (pev->point.function)
  912. event = pev->point.function;
  913. else
  914. event = tev->point.symbol;
  915. if (pev->group)
  916. group = pev->group;
  917. else
  918. group = PERFPROBE_GROUP;
  919. /* Get an unused new event name */
  920. get_new_event_name(buf, 64, event, namelist, allow_suffix);
  921. event = buf;
  922. tev->event = xstrdup(event);
  923. tev->group = xstrdup(group);
  924. write_kprobe_trace_event(fd, tev);
  925. /* Add added event name to namelist */
  926. strlist__add(namelist, event);
  927. /* Trick here - save current event/group */
  928. event = pev->event;
  929. group = pev->group;
  930. pev->event = tev->event;
  931. pev->group = tev->group;
  932. show_perf_probe_event(pev);
  933. /* Trick here - restore current event/group */
  934. pev->event = (char *)event;
  935. pev->group = (char *)group;
  936. /*
  937. * Probes after the first probe which comes from same
  938. * user input are always allowed to add suffix, because
  939. * there might be several addresses corresponding to
  940. * one code line.
  941. */
  942. allow_suffix = true;
  943. }
  944. /* Show how to use the event. */
  945. printf("\nYou can now use it on all perf tools, such as:\n\n");
  946. printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event);
  947. strlist__delete(namelist);
  948. close(fd);
  949. }
  950. static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
  951. struct kprobe_trace_event **tevs)
  952. {
  953. struct symbol *sym;
  954. int ntevs = 0, i;
  955. struct kprobe_trace_event *tev;
  956. /* Convert perf_probe_event with debuginfo */
  957. ntevs = try_to_find_kprobe_trace_events(pev, tevs);
  958. if (ntevs > 0)
  959. return ntevs;
  960. /* Allocate trace event buffer */
  961. ntevs = 1;
  962. tev = *tevs = xzalloc(sizeof(struct kprobe_trace_event));
  963. /* Copy parameters */
  964. tev->point.symbol = xstrdup(pev->point.function);
  965. tev->point.offset = pev->point.offset;
  966. tev->nargs = pev->nargs;
  967. if (tev->nargs) {
  968. tev->args = xzalloc(sizeof(struct kprobe_trace_arg)
  969. * tev->nargs);
  970. for (i = 0; i < tev->nargs; i++)
  971. tev->args[i].value = xstrdup(pev->args[i].name);
  972. }
  973. /* Currently just checking function name from symbol map */
  974. sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
  975. tev->point.symbol, NULL);
  976. if (!sym)
  977. die("Kernel symbol \'%s\' not found - probe not added.",
  978. tev->point.symbol);
  979. return ntevs;
  980. }
  981. struct __event_package {
  982. struct perf_probe_event *pev;
  983. struct kprobe_trace_event *tevs;
  984. int ntevs;
  985. };
  986. void add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  987. bool force_add)
  988. {
  989. int i;
  990. struct __event_package *pkgs;
  991. pkgs = xzalloc(sizeof(struct __event_package) * npevs);
  992. /* Init vmlinux path */
  993. init_vmlinux();
  994. /* Loop 1: convert all events */
  995. for (i = 0; i < npevs; i++) {
  996. pkgs[i].pev = &pevs[i];
  997. /* Convert with or without debuginfo */
  998. pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev,
  999. &pkgs[i].tevs);
  1000. }
  1001. /* Loop 2: add all events */
  1002. for (i = 0; i < npevs; i++)
  1003. __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1004. pkgs[i].ntevs, force_add);
  1005. /* TODO: cleanup all trace events? */
  1006. }
  1007. static void __del_trace_kprobe_event(int fd, struct str_node *ent)
  1008. {
  1009. char *p;
  1010. char buf[128];
  1011. int ret;
  1012. /* Convert from perf-probe event to trace-kprobe event */
  1013. if (e_snprintf(buf, 128, "-:%s", ent->s) < 0)
  1014. die("Failed to copy event.");
  1015. p = strchr(buf + 2, ':');
  1016. if (!p)
  1017. die("Internal error: %s should have ':' but not.", ent->s);
  1018. *p = '/';
  1019. pr_debug("Writing event: %s\n", buf);
  1020. ret = write(fd, buf, strlen(buf));
  1021. if (ret <= 0)
  1022. die("Failed to write event: %s", strerror(errno));
  1023. printf("Remove event: %s\n", ent->s);
  1024. }
  1025. static void del_trace_kprobe_event(int fd, const char *group,
  1026. const char *event, struct strlist *namelist)
  1027. {
  1028. char buf[128];
  1029. struct str_node *ent, *n;
  1030. int found = 0;
  1031. if (e_snprintf(buf, 128, "%s:%s", group, event) < 0)
  1032. die("Failed to copy event.");
  1033. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1034. strlist__for_each_safe(ent, n, namelist)
  1035. if (strglobmatch(ent->s, buf)) {
  1036. found++;
  1037. __del_trace_kprobe_event(fd, ent);
  1038. strlist__remove(namelist, ent);
  1039. }
  1040. } else {
  1041. ent = strlist__find(namelist, buf);
  1042. if (ent) {
  1043. found++;
  1044. __del_trace_kprobe_event(fd, ent);
  1045. strlist__remove(namelist, ent);
  1046. }
  1047. }
  1048. if (found == 0)
  1049. pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf);
  1050. }
  1051. void del_perf_probe_events(struct strlist *dellist)
  1052. {
  1053. int fd;
  1054. const char *group, *event;
  1055. char *p, *str;
  1056. struct str_node *ent;
  1057. struct strlist *namelist;
  1058. fd = open_kprobe_events(true);
  1059. /* Get current event names */
  1060. namelist = get_kprobe_trace_event_names(fd, true);
  1061. strlist__for_each(ent, dellist) {
  1062. str = xstrdup(ent->s);
  1063. pr_debug("Parsing: %s\n", str);
  1064. p = strchr(str, ':');
  1065. if (p) {
  1066. group = str;
  1067. *p = '\0';
  1068. event = p + 1;
  1069. } else {
  1070. group = "*";
  1071. event = str;
  1072. }
  1073. pr_debug("Group: %s, Event: %s\n", group, event);
  1074. del_trace_kprobe_event(fd, group, event, namelist);
  1075. free(str);
  1076. }
  1077. strlist__delete(namelist);
  1078. close(fd);
  1079. }