probe-event.c 44 KB

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