probe-event.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /*
  2. * probe-event.c : perf-probe definition to probe_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 "debugfs.h"
  44. #include "trace-event.h" /* For __unused */
  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 ...) pr_err("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 machine machine;
  68. /* Initialize symbol maps and path of vmlinux/modules */
  69. static int init_vmlinux(void)
  70. {
  71. int ret;
  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. ret = symbol__init();
  78. if (ret < 0) {
  79. pr_debug("Failed to init symbol map.\n");
  80. goto out;
  81. }
  82. ret = machine__init(&machine, "", HOST_KERNEL_ID);
  83. if (ret < 0)
  84. goto out;
  85. if (machine__create_kernel_maps(&machine) < 0) {
  86. pr_debug("machine__create_kernel_maps() failed.\n");
  87. goto out;
  88. }
  89. out:
  90. if (ret < 0)
  91. pr_warning("Failed to init vmlinux path.\n");
  92. return ret;
  93. }
  94. static struct symbol *__find_kernel_function_by_name(const char *name,
  95. struct map **mapp)
  96. {
  97. return machine__find_kernel_function_by_name(&machine, name, mapp,
  98. NULL);
  99. }
  100. const char *kernel_get_module_path(const char *module)
  101. {
  102. struct dso *dso;
  103. if (module) {
  104. list_for_each_entry(dso, &machine.kernel_dsos, node) {
  105. if (strncmp(dso->short_name + 1, module,
  106. dso->short_name_len - 2) == 0)
  107. goto found;
  108. }
  109. pr_debug("Failed to find module %s.\n", module);
  110. return NULL;
  111. } else {
  112. dso = machine.vmlinux_maps[MAP__FUNCTION]->dso;
  113. if (dso__load_vmlinux_path(dso,
  114. machine.vmlinux_maps[MAP__FUNCTION], NULL) < 0) {
  115. pr_debug("Failed to load kernel map.\n");
  116. return NULL;
  117. }
  118. }
  119. found:
  120. return dso->long_name;
  121. }
  122. #ifdef DWARF_SUPPORT
  123. static int open_vmlinux(const char *module)
  124. {
  125. const char *path = kernel_get_module_path(module);
  126. if (!path) {
  127. pr_err("Failed to find path of %s module.\n",
  128. module ?: "kernel");
  129. return -ENOENT;
  130. }
  131. pr_debug("Try to open %s\n", path);
  132. return open(path, O_RDONLY);
  133. }
  134. /*
  135. * Convert trace point to probe point with debuginfo
  136. * Currently only handles kprobes.
  137. */
  138. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  139. struct perf_probe_point *pp)
  140. {
  141. struct symbol *sym;
  142. struct map *map;
  143. u64 addr;
  144. int ret = -ENOENT;
  145. sym = __find_kernel_function_by_name(tp->symbol, &map);
  146. if (sym) {
  147. addr = map->unmap_ip(map, sym->start + tp->offset);
  148. pr_debug("try to find %s+%ld@%llx\n", tp->symbol,
  149. tp->offset, addr);
  150. ret = find_perf_probe_point((unsigned long)addr, pp);
  151. }
  152. if (ret <= 0) {
  153. pr_debug("Failed to find corresponding probes from "
  154. "debuginfo. Use kprobe event information.\n");
  155. pp->function = strdup(tp->symbol);
  156. if (pp->function == NULL)
  157. return -ENOMEM;
  158. pp->offset = tp->offset;
  159. }
  160. pp->retprobe = tp->retprobe;
  161. return 0;
  162. }
  163. /* Try to find perf_probe_event with debuginfo */
  164. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  165. struct probe_trace_event **tevs,
  166. int max_tevs, const char *module)
  167. {
  168. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  169. int fd, ntevs;
  170. fd = open_vmlinux(module);
  171. if (fd < 0) {
  172. if (need_dwarf) {
  173. pr_warning("Failed to open debuginfo file.\n");
  174. return fd;
  175. }
  176. pr_debug("Could not open vmlinux. Try to use symbols.\n");
  177. return 0;
  178. }
  179. /* Searching trace events corresponding to probe event */
  180. ntevs = find_probe_trace_events(fd, pev, tevs, max_tevs);
  181. close(fd);
  182. if (ntevs > 0) { /* Succeeded to find trace events */
  183. pr_debug("find %d probe_trace_events.\n", ntevs);
  184. return ntevs;
  185. }
  186. if (ntevs == 0) { /* No error but failed to find probe point. */
  187. pr_warning("Probe point '%s' not found.\n",
  188. synthesize_perf_probe_point(&pev->point));
  189. return -ENOENT;
  190. }
  191. /* Error path : ntevs < 0 */
  192. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  193. if (ntevs == -EBADF) {
  194. pr_warning("Warning: No dwarf info found in the vmlinux - "
  195. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  196. if (!need_dwarf) {
  197. pr_debug("Trying to use symbols.\n");
  198. return 0;
  199. }
  200. }
  201. return ntevs;
  202. }
  203. /*
  204. * Find a src file from a DWARF tag path. Prepend optional source path prefix
  205. * and chop off leading directories that do not exist. Result is passed back as
  206. * a newly allocated path on success.
  207. * Return 0 if file was found and readable, -errno otherwise.
  208. */
  209. static int get_real_path(const char *raw_path, const char *comp_dir,
  210. char **new_path)
  211. {
  212. const char *prefix = symbol_conf.source_prefix;
  213. if (!prefix) {
  214. if (raw_path[0] != '/' && comp_dir)
  215. /* If not an absolute path, try to use comp_dir */
  216. prefix = comp_dir;
  217. else {
  218. if (access(raw_path, R_OK) == 0) {
  219. *new_path = strdup(raw_path);
  220. return 0;
  221. } else
  222. return -errno;
  223. }
  224. }
  225. *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2));
  226. if (!*new_path)
  227. return -ENOMEM;
  228. for (;;) {
  229. sprintf(*new_path, "%s/%s", prefix, raw_path);
  230. if (access(*new_path, R_OK) == 0)
  231. return 0;
  232. if (!symbol_conf.source_prefix)
  233. /* In case of searching comp_dir, don't retry */
  234. return -errno;
  235. switch (errno) {
  236. case ENAMETOOLONG:
  237. case ENOENT:
  238. case EROFS:
  239. case EFAULT:
  240. raw_path = strchr(++raw_path, '/');
  241. if (!raw_path) {
  242. free(*new_path);
  243. *new_path = NULL;
  244. return -ENOENT;
  245. }
  246. continue;
  247. default:
  248. free(*new_path);
  249. *new_path = NULL;
  250. return -errno;
  251. }
  252. }
  253. }
  254. #define LINEBUF_SIZE 256
  255. #define NR_ADDITIONAL_LINES 2
  256. static int show_one_line(FILE *fp, int l, bool skip, bool show_num)
  257. {
  258. char buf[LINEBUF_SIZE];
  259. const char *color = PERF_COLOR_BLUE;
  260. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  261. goto error;
  262. if (!skip) {
  263. if (show_num)
  264. fprintf(stdout, "%7d %s", l, buf);
  265. else
  266. color_fprintf(stdout, color, " %s", buf);
  267. }
  268. while (strlen(buf) == LINEBUF_SIZE - 1 &&
  269. buf[LINEBUF_SIZE - 2] != '\n') {
  270. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  271. goto error;
  272. if (!skip) {
  273. if (show_num)
  274. fprintf(stdout, "%s", buf);
  275. else
  276. color_fprintf(stdout, color, "%s", buf);
  277. }
  278. }
  279. return 0;
  280. error:
  281. if (feof(fp))
  282. pr_warning("Source file is shorter than expected.\n");
  283. else
  284. pr_warning("File read error: %s\n", strerror(errno));
  285. return -1;
  286. }
  287. /*
  288. * Show line-range always requires debuginfo to find source file and
  289. * line number.
  290. */
  291. int show_line_range(struct line_range *lr, const char *module)
  292. {
  293. int l = 1;
  294. struct line_node *ln;
  295. FILE *fp;
  296. int fd, ret;
  297. char *tmp;
  298. /* Search a line range */
  299. ret = init_vmlinux();
  300. if (ret < 0)
  301. return ret;
  302. fd = open_vmlinux(module);
  303. if (fd < 0) {
  304. pr_warning("Failed to open debuginfo file.\n");
  305. return fd;
  306. }
  307. ret = find_line_range(fd, lr);
  308. close(fd);
  309. if (ret == 0) {
  310. pr_warning("Specified source line is not found.\n");
  311. return -ENOENT;
  312. } else if (ret < 0) {
  313. pr_warning("Debuginfo analysis failed. (%d)\n", ret);
  314. return ret;
  315. }
  316. /* Convert source file path */
  317. tmp = lr->path;
  318. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  319. free(tmp); /* Free old path */
  320. if (ret < 0) {
  321. pr_warning("Failed to find source file. (%d)\n", ret);
  322. return ret;
  323. }
  324. setup_pager();
  325. if (lr->function)
  326. fprintf(stdout, "<%s:%d>\n", lr->function,
  327. lr->start - lr->offset);
  328. else
  329. fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
  330. fp = fopen(lr->path, "r");
  331. if (fp == NULL) {
  332. pr_warning("Failed to open %s: %s\n", lr->path,
  333. strerror(errno));
  334. return -errno;
  335. }
  336. /* Skip to starting line number */
  337. while (l < lr->start && ret >= 0)
  338. ret = show_one_line(fp, l++, true, false);
  339. if (ret < 0)
  340. goto end;
  341. list_for_each_entry(ln, &lr->line_list, list) {
  342. while (ln->line > l && ret >= 0)
  343. ret = show_one_line(fp, (l++) - lr->offset,
  344. false, false);
  345. if (ret >= 0)
  346. ret = show_one_line(fp, (l++) - lr->offset,
  347. false, true);
  348. if (ret < 0)
  349. goto end;
  350. }
  351. if (lr->end == INT_MAX)
  352. lr->end = l + NR_ADDITIONAL_LINES;
  353. while (l <= lr->end && !feof(fp) && ret >= 0)
  354. ret = show_one_line(fp, (l++) - lr->offset, false, false);
  355. end:
  356. fclose(fp);
  357. return ret;
  358. }
  359. static int show_available_vars_at(int fd, struct perf_probe_event *pev,
  360. int max_vls, bool externs)
  361. {
  362. char *buf;
  363. int ret, i;
  364. struct str_node *node;
  365. struct variable_list *vls = NULL, *vl;
  366. buf = synthesize_perf_probe_point(&pev->point);
  367. if (!buf)
  368. return -EINVAL;
  369. pr_debug("Searching variables at %s\n", buf);
  370. ret = find_available_vars_at(fd, pev, &vls, max_vls, externs);
  371. if (ret > 0) {
  372. /* Some variables were found */
  373. fprintf(stdout, "Available variables at %s\n", buf);
  374. for (i = 0; i < ret; i++) {
  375. vl = &vls[i];
  376. /*
  377. * A probe point might be converted to
  378. * several trace points.
  379. */
  380. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  381. vl->point.offset);
  382. free(vl->point.symbol);
  383. if (vl->vars) {
  384. strlist__for_each(node, vl->vars)
  385. fprintf(stdout, "\t\t%s\n", node->s);
  386. strlist__delete(vl->vars);
  387. } else
  388. fprintf(stdout, "(No variables)\n");
  389. }
  390. free(vls);
  391. } else
  392. pr_err("Failed to find variables at %s (%d)\n", buf, ret);
  393. free(buf);
  394. return ret;
  395. }
  396. /* Show available variables on given probe point */
  397. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  398. int max_vls, const char *module, bool externs)
  399. {
  400. int i, fd, ret = 0;
  401. ret = init_vmlinux();
  402. if (ret < 0)
  403. return ret;
  404. fd = open_vmlinux(module);
  405. if (fd < 0) {
  406. pr_warning("Failed to open debug information file.\n");
  407. return fd;
  408. }
  409. setup_pager();
  410. for (i = 0; i < npevs && ret >= 0; i++)
  411. ret = show_available_vars_at(fd, &pevs[i], max_vls, externs);
  412. close(fd);
  413. return ret;
  414. }
  415. #else /* !DWARF_SUPPORT */
  416. static int kprobe_convert_to_perf_probe(struct probe_trace_point *tp,
  417. struct perf_probe_point *pp)
  418. {
  419. struct symbol *sym;
  420. sym = __find_kernel_function_by_name(tp->symbol, NULL);
  421. if (!sym) {
  422. pr_err("Failed to find symbol %s in kernel.\n", tp->symbol);
  423. return -ENOENT;
  424. }
  425. pp->function = strdup(tp->symbol);
  426. if (pp->function == NULL)
  427. return -ENOMEM;
  428. pp->offset = tp->offset;
  429. pp->retprobe = tp->retprobe;
  430. return 0;
  431. }
  432. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  433. struct probe_trace_event **tevs __unused,
  434. int max_tevs __unused, const char *mod __unused)
  435. {
  436. if (perf_probe_event_need_dwarf(pev)) {
  437. pr_warning("Debuginfo-analysis is not supported.\n");
  438. return -ENOSYS;
  439. }
  440. return 0;
  441. }
  442. int show_line_range(struct line_range *lr __unused, const char *module __unused)
  443. {
  444. pr_warning("Debuginfo-analysis is not supported.\n");
  445. return -ENOSYS;
  446. }
  447. int show_available_vars(struct perf_probe_event *pevs __unused,
  448. int npevs __unused, int max_vls __unused,
  449. const char *module __unused, bool externs __unused)
  450. {
  451. pr_warning("Debuginfo-analysis is not supported.\n");
  452. return -ENOSYS;
  453. }
  454. #endif
  455. int parse_line_range_desc(const char *arg, struct line_range *lr)
  456. {
  457. const char *ptr;
  458. char *tmp;
  459. /*
  460. * <Syntax>
  461. * SRC:SLN[+NUM|-ELN]
  462. * FUNC[:SLN[+NUM|-ELN]]
  463. */
  464. ptr = strchr(arg, ':');
  465. if (ptr) {
  466. lr->start = (int)strtoul(ptr + 1, &tmp, 0);
  467. if (*tmp == '+') {
  468. lr->end = lr->start + (int)strtoul(tmp + 1, &tmp, 0);
  469. lr->end--; /*
  470. * Adjust the number of lines here.
  471. * If the number of lines == 1, the
  472. * the end of line should be equal to
  473. * the start of line.
  474. */
  475. } else if (*tmp == '-')
  476. lr->end = (int)strtoul(tmp + 1, &tmp, 0);
  477. else
  478. lr->end = INT_MAX;
  479. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  480. if (lr->start > lr->end) {
  481. semantic_error("Start line must be smaller"
  482. " than end line.\n");
  483. return -EINVAL;
  484. }
  485. if (*tmp != '\0') {
  486. semantic_error("Tailing with invalid character '%d'.\n",
  487. *tmp);
  488. return -EINVAL;
  489. }
  490. tmp = strndup(arg, (ptr - arg));
  491. } else {
  492. tmp = strdup(arg);
  493. lr->end = INT_MAX;
  494. }
  495. if (tmp == NULL)
  496. return -ENOMEM;
  497. if (strchr(tmp, '.'))
  498. lr->file = tmp;
  499. else
  500. lr->function = tmp;
  501. return 0;
  502. }
  503. /* Check the name is good for event/group */
  504. static bool check_event_name(const char *name)
  505. {
  506. if (!isalpha(*name) && *name != '_')
  507. return false;
  508. while (*++name != '\0') {
  509. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  510. return false;
  511. }
  512. return true;
  513. }
  514. /* Parse probepoint definition. */
  515. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  516. {
  517. struct perf_probe_point *pp = &pev->point;
  518. char *ptr, *tmp;
  519. char c, nc = 0;
  520. /*
  521. * <Syntax>
  522. * perf probe [EVENT=]SRC[:LN|;PTN]
  523. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  524. *
  525. * TODO:Group name support
  526. */
  527. ptr = strpbrk(arg, ";=@+%");
  528. if (ptr && *ptr == '=') { /* Event name */
  529. *ptr = '\0';
  530. tmp = ptr + 1;
  531. if (strchr(arg, ':')) {
  532. semantic_error("Group name is not supported yet.\n");
  533. return -ENOTSUP;
  534. }
  535. if (!check_event_name(arg)) {
  536. semantic_error("%s is bad for event name -it must "
  537. "follow C symbol-naming rule.\n", arg);
  538. return -EINVAL;
  539. }
  540. pev->event = strdup(arg);
  541. if (pev->event == NULL)
  542. return -ENOMEM;
  543. pev->group = NULL;
  544. arg = tmp;
  545. }
  546. ptr = strpbrk(arg, ";:+@%");
  547. if (ptr) {
  548. nc = *ptr;
  549. *ptr++ = '\0';
  550. }
  551. tmp = strdup(arg);
  552. if (tmp == NULL)
  553. return -ENOMEM;
  554. /* Check arg is function or file and copy it */
  555. if (strchr(tmp, '.')) /* File */
  556. pp->file = tmp;
  557. else /* Function */
  558. pp->function = tmp;
  559. /* Parse other options */
  560. while (ptr) {
  561. arg = ptr;
  562. c = nc;
  563. if (c == ';') { /* Lazy pattern must be the last part */
  564. pp->lazy_line = strdup(arg);
  565. if (pp->lazy_line == NULL)
  566. return -ENOMEM;
  567. break;
  568. }
  569. ptr = strpbrk(arg, ";:+@%");
  570. if (ptr) {
  571. nc = *ptr;
  572. *ptr++ = '\0';
  573. }
  574. switch (c) {
  575. case ':': /* Line number */
  576. pp->line = strtoul(arg, &tmp, 0);
  577. if (*tmp != '\0') {
  578. semantic_error("There is non-digit char"
  579. " in line number.\n");
  580. return -EINVAL;
  581. }
  582. break;
  583. case '+': /* Byte offset from a symbol */
  584. pp->offset = strtoul(arg, &tmp, 0);
  585. if (*tmp != '\0') {
  586. semantic_error("There is non-digit character"
  587. " in offset.\n");
  588. return -EINVAL;
  589. }
  590. break;
  591. case '@': /* File name */
  592. if (pp->file) {
  593. semantic_error("SRC@SRC is not allowed.\n");
  594. return -EINVAL;
  595. }
  596. pp->file = strdup(arg);
  597. if (pp->file == NULL)
  598. return -ENOMEM;
  599. break;
  600. case '%': /* Probe places */
  601. if (strcmp(arg, "return") == 0) {
  602. pp->retprobe = 1;
  603. } else { /* Others not supported yet */
  604. semantic_error("%%%s is not supported.\n", arg);
  605. return -ENOTSUP;
  606. }
  607. break;
  608. default: /* Buggy case */
  609. pr_err("This program has a bug at %s:%d.\n",
  610. __FILE__, __LINE__);
  611. return -ENOTSUP;
  612. break;
  613. }
  614. }
  615. /* Exclusion check */
  616. if (pp->lazy_line && pp->line) {
  617. semantic_error("Lazy pattern can't be used with"
  618. " line number.\n");
  619. return -EINVAL;
  620. }
  621. if (pp->lazy_line && pp->offset) {
  622. semantic_error("Lazy pattern can't be used with offset.\n");
  623. return -EINVAL;
  624. }
  625. if (pp->line && pp->offset) {
  626. semantic_error("Offset can't be used with line number.\n");
  627. return -EINVAL;
  628. }
  629. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  630. semantic_error("File always requires line number or "
  631. "lazy pattern.\n");
  632. return -EINVAL;
  633. }
  634. if (pp->offset && !pp->function) {
  635. semantic_error("Offset requires an entry function.\n");
  636. return -EINVAL;
  637. }
  638. if (pp->retprobe && !pp->function) {
  639. semantic_error("Return probe requires an entry function.\n");
  640. return -EINVAL;
  641. }
  642. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  643. semantic_error("Offset/Line/Lazy pattern can't be used with "
  644. "return probe.\n");
  645. return -EINVAL;
  646. }
  647. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  648. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  649. pp->lazy_line);
  650. return 0;
  651. }
  652. /* Parse perf-probe event argument */
  653. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  654. {
  655. char *tmp, *goodname;
  656. struct perf_probe_arg_field **fieldp;
  657. pr_debug("parsing arg: %s into ", str);
  658. tmp = strchr(str, '=');
  659. if (tmp) {
  660. arg->name = strndup(str, tmp - str);
  661. if (arg->name == NULL)
  662. return -ENOMEM;
  663. pr_debug("name:%s ", arg->name);
  664. str = tmp + 1;
  665. }
  666. tmp = strchr(str, ':');
  667. if (tmp) { /* Type setting */
  668. *tmp = '\0';
  669. arg->type = strdup(tmp + 1);
  670. if (arg->type == NULL)
  671. return -ENOMEM;
  672. pr_debug("type:%s ", arg->type);
  673. }
  674. tmp = strpbrk(str, "-.[");
  675. if (!is_c_varname(str) || !tmp) {
  676. /* A variable, register, symbol or special value */
  677. arg->var = strdup(str);
  678. if (arg->var == NULL)
  679. return -ENOMEM;
  680. pr_debug("%s\n", arg->var);
  681. return 0;
  682. }
  683. /* Structure fields or array element */
  684. arg->var = strndup(str, tmp - str);
  685. if (arg->var == NULL)
  686. return -ENOMEM;
  687. goodname = arg->var;
  688. pr_debug("%s, ", arg->var);
  689. fieldp = &arg->field;
  690. do {
  691. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  692. if (*fieldp == NULL)
  693. return -ENOMEM;
  694. if (*tmp == '[') { /* Array */
  695. str = tmp;
  696. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  697. (*fieldp)->ref = true;
  698. if (*tmp != ']' || tmp == str + 1) {
  699. semantic_error("Array index must be a"
  700. " number.\n");
  701. return -EINVAL;
  702. }
  703. tmp++;
  704. if (*tmp == '\0')
  705. tmp = NULL;
  706. } else { /* Structure */
  707. if (*tmp == '.') {
  708. str = tmp + 1;
  709. (*fieldp)->ref = false;
  710. } else if (tmp[1] == '>') {
  711. str = tmp + 2;
  712. (*fieldp)->ref = true;
  713. } else {
  714. semantic_error("Argument parse error: %s\n",
  715. str);
  716. return -EINVAL;
  717. }
  718. tmp = strpbrk(str, "-.[");
  719. }
  720. if (tmp) {
  721. (*fieldp)->name = strndup(str, tmp - str);
  722. if ((*fieldp)->name == NULL)
  723. return -ENOMEM;
  724. if (*str != '[')
  725. goodname = (*fieldp)->name;
  726. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  727. fieldp = &(*fieldp)->next;
  728. }
  729. } while (tmp);
  730. (*fieldp)->name = strdup(str);
  731. if ((*fieldp)->name == NULL)
  732. return -ENOMEM;
  733. if (*str != '[')
  734. goodname = (*fieldp)->name;
  735. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  736. /* If no name is specified, set the last field name (not array index)*/
  737. if (!arg->name) {
  738. arg->name = strdup(goodname);
  739. if (arg->name == NULL)
  740. return -ENOMEM;
  741. }
  742. return 0;
  743. }
  744. /* Parse perf-probe event command */
  745. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  746. {
  747. char **argv;
  748. int argc, i, ret = 0;
  749. argv = argv_split(cmd, &argc);
  750. if (!argv) {
  751. pr_debug("Failed to split arguments.\n");
  752. return -ENOMEM;
  753. }
  754. if (argc - 1 > MAX_PROBE_ARGS) {
  755. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  756. ret = -ERANGE;
  757. goto out;
  758. }
  759. /* Parse probe point */
  760. ret = parse_perf_probe_point(argv[0], pev);
  761. if (ret < 0)
  762. goto out;
  763. /* Copy arguments and ensure return probe has no C argument */
  764. pev->nargs = argc - 1;
  765. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  766. if (pev->args == NULL) {
  767. ret = -ENOMEM;
  768. goto out;
  769. }
  770. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  771. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  772. if (ret >= 0 &&
  773. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  774. semantic_error("You can't specify local variable for"
  775. " kretprobe.\n");
  776. ret = -EINVAL;
  777. }
  778. }
  779. out:
  780. argv_free(argv);
  781. return ret;
  782. }
  783. /* Return true if this perf_probe_event requires debuginfo */
  784. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  785. {
  786. int i;
  787. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  788. return true;
  789. for (i = 0; i < pev->nargs; i++)
  790. if (is_c_varname(pev->args[i].var))
  791. return true;
  792. return false;
  793. }
  794. /* Parse probe_events event into struct probe_point */
  795. static int parse_probe_trace_command(const char *cmd,
  796. struct probe_trace_event *tev)
  797. {
  798. struct probe_trace_point *tp = &tev->point;
  799. char pr;
  800. char *p;
  801. int ret, i, argc;
  802. char **argv;
  803. pr_debug("Parsing probe_events: %s\n", cmd);
  804. argv = argv_split(cmd, &argc);
  805. if (!argv) {
  806. pr_debug("Failed to split arguments.\n");
  807. return -ENOMEM;
  808. }
  809. if (argc < 2) {
  810. semantic_error("Too few probe arguments.\n");
  811. ret = -ERANGE;
  812. goto out;
  813. }
  814. /* Scan event and group name. */
  815. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  816. &pr, (float *)(void *)&tev->group,
  817. (float *)(void *)&tev->event);
  818. if (ret != 3) {
  819. semantic_error("Failed to parse event name: %s\n", argv[0]);
  820. ret = -EINVAL;
  821. goto out;
  822. }
  823. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  824. tp->retprobe = (pr == 'r');
  825. /* Scan function name and offset */
  826. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  827. &tp->offset);
  828. if (ret == 1)
  829. tp->offset = 0;
  830. tev->nargs = argc - 2;
  831. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  832. if (tev->args == NULL) {
  833. ret = -ENOMEM;
  834. goto out;
  835. }
  836. for (i = 0; i < tev->nargs; i++) {
  837. p = strchr(argv[i + 2], '=');
  838. if (p) /* We don't need which register is assigned. */
  839. *p++ = '\0';
  840. else
  841. p = argv[i + 2];
  842. tev->args[i].name = strdup(argv[i + 2]);
  843. /* TODO: parse regs and offset */
  844. tev->args[i].value = strdup(p);
  845. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  846. ret = -ENOMEM;
  847. goto out;
  848. }
  849. }
  850. ret = 0;
  851. out:
  852. argv_free(argv);
  853. return ret;
  854. }
  855. /* Compose only probe arg */
  856. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  857. {
  858. struct perf_probe_arg_field *field = pa->field;
  859. int ret;
  860. char *tmp = buf;
  861. if (pa->name && pa->var)
  862. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  863. else
  864. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  865. if (ret <= 0)
  866. goto error;
  867. tmp += ret;
  868. len -= ret;
  869. while (field) {
  870. if (field->name[0] == '[')
  871. ret = e_snprintf(tmp, len, "%s", field->name);
  872. else
  873. ret = e_snprintf(tmp, len, "%s%s",
  874. field->ref ? "->" : ".", field->name);
  875. if (ret <= 0)
  876. goto error;
  877. tmp += ret;
  878. len -= ret;
  879. field = field->next;
  880. }
  881. if (pa->type) {
  882. ret = e_snprintf(tmp, len, ":%s", pa->type);
  883. if (ret <= 0)
  884. goto error;
  885. tmp += ret;
  886. len -= ret;
  887. }
  888. return tmp - buf;
  889. error:
  890. pr_debug("Failed to synthesize perf probe argument: %s\n",
  891. strerror(-ret));
  892. return ret;
  893. }
  894. /* Compose only probe point (not argument) */
  895. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  896. {
  897. char *buf, *tmp;
  898. char offs[32] = "", line[32] = "", file[32] = "";
  899. int ret, len;
  900. buf = zalloc(MAX_CMDLEN);
  901. if (buf == NULL) {
  902. ret = -ENOMEM;
  903. goto error;
  904. }
  905. if (pp->offset) {
  906. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  907. if (ret <= 0)
  908. goto error;
  909. }
  910. if (pp->line) {
  911. ret = e_snprintf(line, 32, ":%d", pp->line);
  912. if (ret <= 0)
  913. goto error;
  914. }
  915. if (pp->file) {
  916. len = strlen(pp->file) - 31;
  917. if (len < 0)
  918. len = 0;
  919. tmp = strchr(pp->file + len, '/');
  920. if (!tmp)
  921. tmp = pp->file + len;
  922. ret = e_snprintf(file, 32, "@%s", tmp + 1);
  923. if (ret <= 0)
  924. goto error;
  925. }
  926. if (pp->function)
  927. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  928. offs, pp->retprobe ? "%return" : "", line,
  929. file);
  930. else
  931. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  932. if (ret <= 0)
  933. goto error;
  934. return buf;
  935. error:
  936. pr_debug("Failed to synthesize perf probe point: %s\n",
  937. strerror(-ret));
  938. if (buf)
  939. free(buf);
  940. return NULL;
  941. }
  942. #if 0
  943. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  944. {
  945. char *buf;
  946. int i, len, ret;
  947. buf = synthesize_perf_probe_point(&pev->point);
  948. if (!buf)
  949. return NULL;
  950. len = strlen(buf);
  951. for (i = 0; i < pev->nargs; i++) {
  952. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  953. pev->args[i].name);
  954. if (ret <= 0) {
  955. free(buf);
  956. return NULL;
  957. }
  958. len += ret;
  959. }
  960. return buf;
  961. }
  962. #endif
  963. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  964. char **buf, size_t *buflen,
  965. int depth)
  966. {
  967. int ret;
  968. if (ref->next) {
  969. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  970. buflen, depth + 1);
  971. if (depth < 0)
  972. goto out;
  973. }
  974. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  975. if (ret < 0)
  976. depth = ret;
  977. else {
  978. *buf += ret;
  979. *buflen -= ret;
  980. }
  981. out:
  982. return depth;
  983. }
  984. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  985. char *buf, size_t buflen)
  986. {
  987. struct probe_trace_arg_ref *ref = arg->ref;
  988. int ret, depth = 0;
  989. char *tmp = buf;
  990. /* Argument name or separator */
  991. if (arg->name)
  992. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  993. else
  994. ret = e_snprintf(buf, buflen, " ");
  995. if (ret < 0)
  996. return ret;
  997. buf += ret;
  998. buflen -= ret;
  999. /* Special case: @XXX */
  1000. if (arg->value[0] == '@' && arg->ref)
  1001. ref = ref->next;
  1002. /* Dereferencing arguments */
  1003. if (ref) {
  1004. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  1005. &buflen, 1);
  1006. if (depth < 0)
  1007. return depth;
  1008. }
  1009. /* Print argument value */
  1010. if (arg->value[0] == '@' && arg->ref)
  1011. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  1012. arg->ref->offset);
  1013. else
  1014. ret = e_snprintf(buf, buflen, "%s", arg->value);
  1015. if (ret < 0)
  1016. return ret;
  1017. buf += ret;
  1018. buflen -= ret;
  1019. /* Closing */
  1020. while (depth--) {
  1021. ret = e_snprintf(buf, buflen, ")");
  1022. if (ret < 0)
  1023. return ret;
  1024. buf += ret;
  1025. buflen -= ret;
  1026. }
  1027. /* Print argument type */
  1028. if (arg->type) {
  1029. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  1030. if (ret <= 0)
  1031. return ret;
  1032. buf += ret;
  1033. }
  1034. return buf - tmp;
  1035. }
  1036. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1037. {
  1038. struct probe_trace_point *tp = &tev->point;
  1039. char *buf;
  1040. int i, len, ret;
  1041. buf = zalloc(MAX_CMDLEN);
  1042. if (buf == NULL)
  1043. return NULL;
  1044. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  1045. tp->retprobe ? 'r' : 'p',
  1046. tev->group, tev->event,
  1047. tp->symbol, tp->offset);
  1048. if (len <= 0)
  1049. goto error;
  1050. for (i = 0; i < tev->nargs; i++) {
  1051. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  1052. MAX_CMDLEN - len);
  1053. if (ret <= 0)
  1054. goto error;
  1055. len += ret;
  1056. }
  1057. return buf;
  1058. error:
  1059. free(buf);
  1060. return NULL;
  1061. }
  1062. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1063. struct perf_probe_event *pev)
  1064. {
  1065. char buf[64] = "";
  1066. int i, ret;
  1067. /* Convert event/group name */
  1068. pev->event = strdup(tev->event);
  1069. pev->group = strdup(tev->group);
  1070. if (pev->event == NULL || pev->group == NULL)
  1071. return -ENOMEM;
  1072. /* Convert trace_point to probe_point */
  1073. ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
  1074. if (ret < 0)
  1075. return ret;
  1076. /* Convert trace_arg to probe_arg */
  1077. pev->nargs = tev->nargs;
  1078. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1079. if (pev->args == NULL)
  1080. return -ENOMEM;
  1081. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1082. if (tev->args[i].name)
  1083. pev->args[i].name = strdup(tev->args[i].name);
  1084. else {
  1085. ret = synthesize_probe_trace_arg(&tev->args[i],
  1086. buf, 64);
  1087. pev->args[i].name = strdup(buf);
  1088. }
  1089. if (pev->args[i].name == NULL && ret >= 0)
  1090. ret = -ENOMEM;
  1091. }
  1092. if (ret < 0)
  1093. clear_perf_probe_event(pev);
  1094. return ret;
  1095. }
  1096. void clear_perf_probe_event(struct perf_probe_event *pev)
  1097. {
  1098. struct perf_probe_point *pp = &pev->point;
  1099. struct perf_probe_arg_field *field, *next;
  1100. int i;
  1101. if (pev->event)
  1102. free(pev->event);
  1103. if (pev->group)
  1104. free(pev->group);
  1105. if (pp->file)
  1106. free(pp->file);
  1107. if (pp->function)
  1108. free(pp->function);
  1109. if (pp->lazy_line)
  1110. free(pp->lazy_line);
  1111. for (i = 0; i < pev->nargs; i++) {
  1112. if (pev->args[i].name)
  1113. free(pev->args[i].name);
  1114. if (pev->args[i].var)
  1115. free(pev->args[i].var);
  1116. if (pev->args[i].type)
  1117. free(pev->args[i].type);
  1118. field = pev->args[i].field;
  1119. while (field) {
  1120. next = field->next;
  1121. if (field->name)
  1122. free(field->name);
  1123. free(field);
  1124. field = next;
  1125. }
  1126. }
  1127. if (pev->args)
  1128. free(pev->args);
  1129. memset(pev, 0, sizeof(*pev));
  1130. }
  1131. static void clear_probe_trace_event(struct probe_trace_event *tev)
  1132. {
  1133. struct probe_trace_arg_ref *ref, *next;
  1134. int i;
  1135. if (tev->event)
  1136. free(tev->event);
  1137. if (tev->group)
  1138. free(tev->group);
  1139. if (tev->point.symbol)
  1140. free(tev->point.symbol);
  1141. for (i = 0; i < tev->nargs; i++) {
  1142. if (tev->args[i].name)
  1143. free(tev->args[i].name);
  1144. if (tev->args[i].value)
  1145. free(tev->args[i].value);
  1146. if (tev->args[i].type)
  1147. free(tev->args[i].type);
  1148. ref = tev->args[i].ref;
  1149. while (ref) {
  1150. next = ref->next;
  1151. free(ref);
  1152. ref = next;
  1153. }
  1154. }
  1155. if (tev->args)
  1156. free(tev->args);
  1157. memset(tev, 0, sizeof(*tev));
  1158. }
  1159. static int open_kprobe_events(bool readwrite)
  1160. {
  1161. char buf[PATH_MAX];
  1162. const char *__debugfs;
  1163. int ret;
  1164. __debugfs = debugfs_find_mountpoint();
  1165. if (__debugfs == NULL) {
  1166. pr_warning("Debugfs is not mounted.\n");
  1167. return -ENOENT;
  1168. }
  1169. ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
  1170. if (ret >= 0) {
  1171. pr_debug("Opening %s write=%d\n", buf, readwrite);
  1172. if (readwrite && !probe_event_dry_run)
  1173. ret = open(buf, O_RDWR, O_APPEND);
  1174. else
  1175. ret = open(buf, O_RDONLY, 0);
  1176. }
  1177. if (ret < 0) {
  1178. if (errno == ENOENT)
  1179. pr_warning("kprobe_events file does not exist - please"
  1180. " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
  1181. else
  1182. pr_warning("Failed to open kprobe_events file: %s\n",
  1183. strerror(errno));
  1184. }
  1185. return ret;
  1186. }
  1187. /* Get raw string list of current kprobe_events */
  1188. static struct strlist *get_probe_trace_command_rawlist(int fd)
  1189. {
  1190. int ret, idx;
  1191. FILE *fp;
  1192. char buf[MAX_CMDLEN];
  1193. char *p;
  1194. struct strlist *sl;
  1195. sl = strlist__new(true, NULL);
  1196. fp = fdopen(dup(fd), "r");
  1197. while (!feof(fp)) {
  1198. p = fgets(buf, MAX_CMDLEN, fp);
  1199. if (!p)
  1200. break;
  1201. idx = strlen(p) - 1;
  1202. if (p[idx] == '\n')
  1203. p[idx] = '\0';
  1204. ret = strlist__add(sl, buf);
  1205. if (ret < 0) {
  1206. pr_debug("strlist__add failed: %s\n", strerror(-ret));
  1207. strlist__delete(sl);
  1208. return NULL;
  1209. }
  1210. }
  1211. fclose(fp);
  1212. return sl;
  1213. }
  1214. /* Show an event */
  1215. static int show_perf_probe_event(struct perf_probe_event *pev)
  1216. {
  1217. int i, ret;
  1218. char buf[128];
  1219. char *place;
  1220. /* Synthesize only event probe point */
  1221. place = synthesize_perf_probe_point(&pev->point);
  1222. if (!place)
  1223. return -EINVAL;
  1224. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  1225. if (ret < 0)
  1226. return ret;
  1227. printf(" %-20s (on %s", buf, place);
  1228. if (pev->nargs > 0) {
  1229. printf(" with");
  1230. for (i = 0; i < pev->nargs; i++) {
  1231. ret = synthesize_perf_probe_arg(&pev->args[i],
  1232. buf, 128);
  1233. if (ret < 0)
  1234. break;
  1235. printf(" %s", buf);
  1236. }
  1237. }
  1238. printf(")\n");
  1239. free(place);
  1240. return ret;
  1241. }
  1242. /* List up current perf-probe events */
  1243. int show_perf_probe_events(void)
  1244. {
  1245. int fd, ret;
  1246. struct probe_trace_event tev;
  1247. struct perf_probe_event pev;
  1248. struct strlist *rawlist;
  1249. struct str_node *ent;
  1250. setup_pager();
  1251. ret = init_vmlinux();
  1252. if (ret < 0)
  1253. return ret;
  1254. memset(&tev, 0, sizeof(tev));
  1255. memset(&pev, 0, sizeof(pev));
  1256. fd = open_kprobe_events(false);
  1257. if (fd < 0)
  1258. return fd;
  1259. rawlist = get_probe_trace_command_rawlist(fd);
  1260. close(fd);
  1261. if (!rawlist)
  1262. return -ENOENT;
  1263. strlist__for_each(ent, rawlist) {
  1264. ret = parse_probe_trace_command(ent->s, &tev);
  1265. if (ret >= 0) {
  1266. ret = convert_to_perf_probe_event(&tev, &pev);
  1267. if (ret >= 0)
  1268. ret = show_perf_probe_event(&pev);
  1269. }
  1270. clear_perf_probe_event(&pev);
  1271. clear_probe_trace_event(&tev);
  1272. if (ret < 0)
  1273. break;
  1274. }
  1275. strlist__delete(rawlist);
  1276. return ret;
  1277. }
  1278. /* Get current perf-probe event names */
  1279. static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
  1280. {
  1281. char buf[128];
  1282. struct strlist *sl, *rawlist;
  1283. struct str_node *ent;
  1284. struct probe_trace_event tev;
  1285. int ret = 0;
  1286. memset(&tev, 0, sizeof(tev));
  1287. rawlist = get_probe_trace_command_rawlist(fd);
  1288. sl = strlist__new(true, NULL);
  1289. strlist__for_each(ent, rawlist) {
  1290. ret = parse_probe_trace_command(ent->s, &tev);
  1291. if (ret < 0)
  1292. break;
  1293. if (include_group) {
  1294. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  1295. tev.event);
  1296. if (ret >= 0)
  1297. ret = strlist__add(sl, buf);
  1298. } else
  1299. ret = strlist__add(sl, tev.event);
  1300. clear_probe_trace_event(&tev);
  1301. if (ret < 0)
  1302. break;
  1303. }
  1304. strlist__delete(rawlist);
  1305. if (ret < 0) {
  1306. strlist__delete(sl);
  1307. return NULL;
  1308. }
  1309. return sl;
  1310. }
  1311. static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
  1312. {
  1313. int ret = 0;
  1314. char *buf = synthesize_probe_trace_command(tev);
  1315. if (!buf) {
  1316. pr_debug("Failed to synthesize probe trace event.\n");
  1317. return -EINVAL;
  1318. }
  1319. pr_debug("Writing event: %s\n", buf);
  1320. if (!probe_event_dry_run) {
  1321. ret = write(fd, buf, strlen(buf));
  1322. if (ret <= 0)
  1323. pr_warning("Failed to write event: %s\n",
  1324. strerror(errno));
  1325. }
  1326. free(buf);
  1327. return ret;
  1328. }
  1329. static int get_new_event_name(char *buf, size_t len, const char *base,
  1330. struct strlist *namelist, bool allow_suffix)
  1331. {
  1332. int i, ret;
  1333. /* Try no suffix */
  1334. ret = e_snprintf(buf, len, "%s", base);
  1335. if (ret < 0) {
  1336. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1337. return ret;
  1338. }
  1339. if (!strlist__has_entry(namelist, buf))
  1340. return 0;
  1341. if (!allow_suffix) {
  1342. pr_warning("Error: event \"%s\" already exists. "
  1343. "(Use -f to force duplicates.)\n", base);
  1344. return -EEXIST;
  1345. }
  1346. /* Try to add suffix */
  1347. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  1348. ret = e_snprintf(buf, len, "%s_%d", base, i);
  1349. if (ret < 0) {
  1350. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1351. return ret;
  1352. }
  1353. if (!strlist__has_entry(namelist, buf))
  1354. break;
  1355. }
  1356. if (i == MAX_EVENT_INDEX) {
  1357. pr_warning("Too many events are on the same function.\n");
  1358. ret = -ERANGE;
  1359. }
  1360. return ret;
  1361. }
  1362. static int __add_probe_trace_events(struct perf_probe_event *pev,
  1363. struct probe_trace_event *tevs,
  1364. int ntevs, bool allow_suffix)
  1365. {
  1366. int i, fd, ret;
  1367. struct probe_trace_event *tev = NULL;
  1368. char buf[64];
  1369. const char *event, *group;
  1370. struct strlist *namelist;
  1371. fd = open_kprobe_events(true);
  1372. if (fd < 0)
  1373. return fd;
  1374. /* Get current event names */
  1375. namelist = get_probe_trace_event_names(fd, false);
  1376. if (!namelist) {
  1377. pr_debug("Failed to get current event list.\n");
  1378. return -EIO;
  1379. }
  1380. ret = 0;
  1381. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  1382. for (i = 0; i < ntevs; i++) {
  1383. tev = &tevs[i];
  1384. if (pev->event)
  1385. event = pev->event;
  1386. else
  1387. if (pev->point.function)
  1388. event = pev->point.function;
  1389. else
  1390. event = tev->point.symbol;
  1391. if (pev->group)
  1392. group = pev->group;
  1393. else
  1394. group = PERFPROBE_GROUP;
  1395. /* Get an unused new event name */
  1396. ret = get_new_event_name(buf, 64, event,
  1397. namelist, allow_suffix);
  1398. if (ret < 0)
  1399. break;
  1400. event = buf;
  1401. tev->event = strdup(event);
  1402. tev->group = strdup(group);
  1403. if (tev->event == NULL || tev->group == NULL) {
  1404. ret = -ENOMEM;
  1405. break;
  1406. }
  1407. ret = write_probe_trace_event(fd, tev);
  1408. if (ret < 0)
  1409. break;
  1410. /* Add added event name to namelist */
  1411. strlist__add(namelist, event);
  1412. /* Trick here - save current event/group */
  1413. event = pev->event;
  1414. group = pev->group;
  1415. pev->event = tev->event;
  1416. pev->group = tev->group;
  1417. show_perf_probe_event(pev);
  1418. /* Trick here - restore current event/group */
  1419. pev->event = (char *)event;
  1420. pev->group = (char *)group;
  1421. /*
  1422. * Probes after the first probe which comes from same
  1423. * user input are always allowed to add suffix, because
  1424. * there might be several addresses corresponding to
  1425. * one code line.
  1426. */
  1427. allow_suffix = true;
  1428. }
  1429. if (ret >= 0) {
  1430. /* Show how to use the event. */
  1431. printf("\nYou can now use it on all perf tools, such as:\n\n");
  1432. printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
  1433. tev->event);
  1434. }
  1435. strlist__delete(namelist);
  1436. close(fd);
  1437. return ret;
  1438. }
  1439. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  1440. struct probe_trace_event **tevs,
  1441. int max_tevs, const char *module)
  1442. {
  1443. struct symbol *sym;
  1444. int ret = 0, i;
  1445. struct probe_trace_event *tev;
  1446. /* Convert perf_probe_event with debuginfo */
  1447. ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
  1448. if (ret != 0)
  1449. return ret;
  1450. /* Allocate trace event buffer */
  1451. tev = *tevs = zalloc(sizeof(struct probe_trace_event));
  1452. if (tev == NULL)
  1453. return -ENOMEM;
  1454. /* Copy parameters */
  1455. tev->point.symbol = strdup(pev->point.function);
  1456. if (tev->point.symbol == NULL) {
  1457. ret = -ENOMEM;
  1458. goto error;
  1459. }
  1460. tev->point.offset = pev->point.offset;
  1461. tev->point.retprobe = pev->point.retprobe;
  1462. tev->nargs = pev->nargs;
  1463. if (tev->nargs) {
  1464. tev->args = zalloc(sizeof(struct probe_trace_arg)
  1465. * tev->nargs);
  1466. if (tev->args == NULL) {
  1467. ret = -ENOMEM;
  1468. goto error;
  1469. }
  1470. for (i = 0; i < tev->nargs; i++) {
  1471. if (pev->args[i].name) {
  1472. tev->args[i].name = strdup(pev->args[i].name);
  1473. if (tev->args[i].name == NULL) {
  1474. ret = -ENOMEM;
  1475. goto error;
  1476. }
  1477. }
  1478. tev->args[i].value = strdup(pev->args[i].var);
  1479. if (tev->args[i].value == NULL) {
  1480. ret = -ENOMEM;
  1481. goto error;
  1482. }
  1483. if (pev->args[i].type) {
  1484. tev->args[i].type = strdup(pev->args[i].type);
  1485. if (tev->args[i].type == NULL) {
  1486. ret = -ENOMEM;
  1487. goto error;
  1488. }
  1489. }
  1490. }
  1491. }
  1492. /* Currently just checking function name from symbol map */
  1493. sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
  1494. if (!sym) {
  1495. pr_warning("Kernel symbol \'%s\' not found.\n",
  1496. tev->point.symbol);
  1497. ret = -ENOENT;
  1498. goto error;
  1499. }
  1500. return 1;
  1501. error:
  1502. clear_probe_trace_event(tev);
  1503. free(tev);
  1504. *tevs = NULL;
  1505. return ret;
  1506. }
  1507. struct __event_package {
  1508. struct perf_probe_event *pev;
  1509. struct probe_trace_event *tevs;
  1510. int ntevs;
  1511. };
  1512. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1513. int max_tevs, const char *module, bool force_add)
  1514. {
  1515. int i, j, ret;
  1516. struct __event_package *pkgs;
  1517. pkgs = zalloc(sizeof(struct __event_package) * npevs);
  1518. if (pkgs == NULL)
  1519. return -ENOMEM;
  1520. /* Init vmlinux path */
  1521. ret = init_vmlinux();
  1522. if (ret < 0) {
  1523. free(pkgs);
  1524. return ret;
  1525. }
  1526. /* Loop 1: convert all events */
  1527. for (i = 0; i < npevs; i++) {
  1528. pkgs[i].pev = &pevs[i];
  1529. /* Convert with or without debuginfo */
  1530. ret = convert_to_probe_trace_events(pkgs[i].pev,
  1531. &pkgs[i].tevs,
  1532. max_tevs,
  1533. module);
  1534. if (ret < 0)
  1535. goto end;
  1536. pkgs[i].ntevs = ret;
  1537. }
  1538. /* Loop 2: add all events */
  1539. for (i = 0; i < npevs && ret >= 0; i++)
  1540. ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1541. pkgs[i].ntevs, force_add);
  1542. end:
  1543. /* Loop 3: cleanup and free trace events */
  1544. for (i = 0; i < npevs; i++) {
  1545. for (j = 0; j < pkgs[i].ntevs; j++)
  1546. clear_probe_trace_event(&pkgs[i].tevs[j]);
  1547. free(pkgs[i].tevs);
  1548. }
  1549. free(pkgs);
  1550. return ret;
  1551. }
  1552. static int __del_trace_probe_event(int fd, struct str_node *ent)
  1553. {
  1554. char *p;
  1555. char buf[128];
  1556. int ret;
  1557. /* Convert from perf-probe event to trace-probe event */
  1558. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  1559. if (ret < 0)
  1560. goto error;
  1561. p = strchr(buf + 2, ':');
  1562. if (!p) {
  1563. pr_debug("Internal error: %s should have ':' but not.\n",
  1564. ent->s);
  1565. ret = -ENOTSUP;
  1566. goto error;
  1567. }
  1568. *p = '/';
  1569. pr_debug("Writing event: %s\n", buf);
  1570. ret = write(fd, buf, strlen(buf));
  1571. if (ret < 0)
  1572. goto error;
  1573. printf("Remove event: %s\n", ent->s);
  1574. return 0;
  1575. error:
  1576. pr_warning("Failed to delete event: %s\n", strerror(-ret));
  1577. return ret;
  1578. }
  1579. static int del_trace_probe_event(int fd, const char *group,
  1580. const char *event, struct strlist *namelist)
  1581. {
  1582. char buf[128];
  1583. struct str_node *ent, *n;
  1584. int found = 0, ret = 0;
  1585. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1586. if (ret < 0) {
  1587. pr_err("Failed to copy event.\n");
  1588. return ret;
  1589. }
  1590. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1591. strlist__for_each_safe(ent, n, namelist)
  1592. if (strglobmatch(ent->s, buf)) {
  1593. found++;
  1594. ret = __del_trace_probe_event(fd, ent);
  1595. if (ret < 0)
  1596. break;
  1597. strlist__remove(namelist, ent);
  1598. }
  1599. } else {
  1600. ent = strlist__find(namelist, buf);
  1601. if (ent) {
  1602. found++;
  1603. ret = __del_trace_probe_event(fd, ent);
  1604. if (ret >= 0)
  1605. strlist__remove(namelist, ent);
  1606. }
  1607. }
  1608. if (found == 0 && ret >= 0)
  1609. pr_info("Info: Event \"%s\" does not exist.\n", buf);
  1610. return ret;
  1611. }
  1612. int del_perf_probe_events(struct strlist *dellist)
  1613. {
  1614. int fd, ret = 0;
  1615. const char *group, *event;
  1616. char *p, *str;
  1617. struct str_node *ent;
  1618. struct strlist *namelist;
  1619. fd = open_kprobe_events(true);
  1620. if (fd < 0)
  1621. return fd;
  1622. /* Get current event names */
  1623. namelist = get_probe_trace_event_names(fd, true);
  1624. if (namelist == NULL)
  1625. return -EINVAL;
  1626. strlist__for_each(ent, dellist) {
  1627. str = strdup(ent->s);
  1628. if (str == NULL) {
  1629. ret = -ENOMEM;
  1630. break;
  1631. }
  1632. pr_debug("Parsing: %s\n", str);
  1633. p = strchr(str, ':');
  1634. if (p) {
  1635. group = str;
  1636. *p = '\0';
  1637. event = p + 1;
  1638. } else {
  1639. group = "*";
  1640. event = str;
  1641. }
  1642. pr_debug("Group: %s, Event: %s\n", group, event);
  1643. ret = del_trace_probe_event(fd, group, event, namelist);
  1644. free(str);
  1645. if (ret < 0)
  1646. break;
  1647. }
  1648. strlist__delete(namelist);
  1649. close(fd);
  1650. return ret;
  1651. }