probe-event.c 42 KB

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