probe-event.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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@%s:%d>\n", lr->function, lr->path,
  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[@SRC][:SLN[+NUM|-ELN]]
  525. */
  526. int parse_line_range_desc(const char *arg, struct line_range *lr)
  527. {
  528. char *range, *file, *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. file = strchr(name, '@');
  569. if (file) {
  570. *file = '\0';
  571. lr->file = strdup(++file);
  572. if (lr->file == NULL) {
  573. err = -ENOMEM;
  574. goto err;
  575. }
  576. lr->function = name;
  577. } else if (strchr(name, '.'))
  578. lr->file = name;
  579. else
  580. lr->function = name;
  581. return 0;
  582. err:
  583. free(name);
  584. return err;
  585. }
  586. /* Check the name is good for event/group */
  587. static bool check_event_name(const char *name)
  588. {
  589. if (!isalpha(*name) && *name != '_')
  590. return false;
  591. while (*++name != '\0') {
  592. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  593. return false;
  594. }
  595. return true;
  596. }
  597. /* Parse probepoint definition. */
  598. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  599. {
  600. struct perf_probe_point *pp = &pev->point;
  601. char *ptr, *tmp;
  602. char c, nc = 0;
  603. /*
  604. * <Syntax>
  605. * perf probe [EVENT=]SRC[:LN|;PTN]
  606. * perf probe [EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  607. *
  608. * TODO:Group name support
  609. */
  610. ptr = strpbrk(arg, ";=@+%");
  611. if (ptr && *ptr == '=') { /* Event name */
  612. *ptr = '\0';
  613. tmp = ptr + 1;
  614. if (strchr(arg, ':')) {
  615. semantic_error("Group name is not supported yet.\n");
  616. return -ENOTSUP;
  617. }
  618. if (!check_event_name(arg)) {
  619. semantic_error("%s is bad for event name -it must "
  620. "follow C symbol-naming rule.\n", arg);
  621. return -EINVAL;
  622. }
  623. pev->event = strdup(arg);
  624. if (pev->event == NULL)
  625. return -ENOMEM;
  626. pev->group = NULL;
  627. arg = tmp;
  628. }
  629. ptr = strpbrk(arg, ";:+@%");
  630. if (ptr) {
  631. nc = *ptr;
  632. *ptr++ = '\0';
  633. }
  634. tmp = strdup(arg);
  635. if (tmp == NULL)
  636. return -ENOMEM;
  637. /* Check arg is function or file and copy it */
  638. if (strchr(tmp, '.')) /* File */
  639. pp->file = tmp;
  640. else /* Function */
  641. pp->function = tmp;
  642. /* Parse other options */
  643. while (ptr) {
  644. arg = ptr;
  645. c = nc;
  646. if (c == ';') { /* Lazy pattern must be the last part */
  647. pp->lazy_line = strdup(arg);
  648. if (pp->lazy_line == NULL)
  649. return -ENOMEM;
  650. break;
  651. }
  652. ptr = strpbrk(arg, ";:+@%");
  653. if (ptr) {
  654. nc = *ptr;
  655. *ptr++ = '\0';
  656. }
  657. switch (c) {
  658. case ':': /* Line number */
  659. pp->line = strtoul(arg, &tmp, 0);
  660. if (*tmp != '\0') {
  661. semantic_error("There is non-digit char"
  662. " in line number.\n");
  663. return -EINVAL;
  664. }
  665. break;
  666. case '+': /* Byte offset from a symbol */
  667. pp->offset = strtoul(arg, &tmp, 0);
  668. if (*tmp != '\0') {
  669. semantic_error("There is non-digit character"
  670. " in offset.\n");
  671. return -EINVAL;
  672. }
  673. break;
  674. case '@': /* File name */
  675. if (pp->file) {
  676. semantic_error("SRC@SRC is not allowed.\n");
  677. return -EINVAL;
  678. }
  679. pp->file = strdup(arg);
  680. if (pp->file == NULL)
  681. return -ENOMEM;
  682. break;
  683. case '%': /* Probe places */
  684. if (strcmp(arg, "return") == 0) {
  685. pp->retprobe = 1;
  686. } else { /* Others not supported yet */
  687. semantic_error("%%%s is not supported.\n", arg);
  688. return -ENOTSUP;
  689. }
  690. break;
  691. default: /* Buggy case */
  692. pr_err("This program has a bug at %s:%d.\n",
  693. __FILE__, __LINE__);
  694. return -ENOTSUP;
  695. break;
  696. }
  697. }
  698. /* Exclusion check */
  699. if (pp->lazy_line && pp->line) {
  700. semantic_error("Lazy pattern can't be used with"
  701. " line number.\n");
  702. return -EINVAL;
  703. }
  704. if (pp->lazy_line && pp->offset) {
  705. semantic_error("Lazy pattern can't be used with offset.\n");
  706. return -EINVAL;
  707. }
  708. if (pp->line && pp->offset) {
  709. semantic_error("Offset can't be used with line number.\n");
  710. return -EINVAL;
  711. }
  712. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  713. semantic_error("File always requires line number or "
  714. "lazy pattern.\n");
  715. return -EINVAL;
  716. }
  717. if (pp->offset && !pp->function) {
  718. semantic_error("Offset requires an entry function.\n");
  719. return -EINVAL;
  720. }
  721. if (pp->retprobe && !pp->function) {
  722. semantic_error("Return probe requires an entry function.\n");
  723. return -EINVAL;
  724. }
  725. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  726. semantic_error("Offset/Line/Lazy pattern can't be used with "
  727. "return probe.\n");
  728. return -EINVAL;
  729. }
  730. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  731. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  732. pp->lazy_line);
  733. return 0;
  734. }
  735. /* Parse perf-probe event argument */
  736. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  737. {
  738. char *tmp, *goodname;
  739. struct perf_probe_arg_field **fieldp;
  740. pr_debug("parsing arg: %s into ", str);
  741. tmp = strchr(str, '=');
  742. if (tmp) {
  743. arg->name = strndup(str, tmp - str);
  744. if (arg->name == NULL)
  745. return -ENOMEM;
  746. pr_debug("name:%s ", arg->name);
  747. str = tmp + 1;
  748. }
  749. tmp = strchr(str, ':');
  750. if (tmp) { /* Type setting */
  751. *tmp = '\0';
  752. arg->type = strdup(tmp + 1);
  753. if (arg->type == NULL)
  754. return -ENOMEM;
  755. pr_debug("type:%s ", arg->type);
  756. }
  757. tmp = strpbrk(str, "-.[");
  758. if (!is_c_varname(str) || !tmp) {
  759. /* A variable, register, symbol or special value */
  760. arg->var = strdup(str);
  761. if (arg->var == NULL)
  762. return -ENOMEM;
  763. pr_debug("%s\n", arg->var);
  764. return 0;
  765. }
  766. /* Structure fields or array element */
  767. arg->var = strndup(str, tmp - str);
  768. if (arg->var == NULL)
  769. return -ENOMEM;
  770. goodname = arg->var;
  771. pr_debug("%s, ", arg->var);
  772. fieldp = &arg->field;
  773. do {
  774. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  775. if (*fieldp == NULL)
  776. return -ENOMEM;
  777. if (*tmp == '[') { /* Array */
  778. str = tmp;
  779. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  780. (*fieldp)->ref = true;
  781. if (*tmp != ']' || tmp == str + 1) {
  782. semantic_error("Array index must be a"
  783. " number.\n");
  784. return -EINVAL;
  785. }
  786. tmp++;
  787. if (*tmp == '\0')
  788. tmp = NULL;
  789. } else { /* Structure */
  790. if (*tmp == '.') {
  791. str = tmp + 1;
  792. (*fieldp)->ref = false;
  793. } else if (tmp[1] == '>') {
  794. str = tmp + 2;
  795. (*fieldp)->ref = true;
  796. } else {
  797. semantic_error("Argument parse error: %s\n",
  798. str);
  799. return -EINVAL;
  800. }
  801. tmp = strpbrk(str, "-.[");
  802. }
  803. if (tmp) {
  804. (*fieldp)->name = strndup(str, tmp - str);
  805. if ((*fieldp)->name == NULL)
  806. return -ENOMEM;
  807. if (*str != '[')
  808. goodname = (*fieldp)->name;
  809. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  810. fieldp = &(*fieldp)->next;
  811. }
  812. } while (tmp);
  813. (*fieldp)->name = strdup(str);
  814. if ((*fieldp)->name == NULL)
  815. return -ENOMEM;
  816. if (*str != '[')
  817. goodname = (*fieldp)->name;
  818. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  819. /* If no name is specified, set the last field name (not array index)*/
  820. if (!arg->name) {
  821. arg->name = strdup(goodname);
  822. if (arg->name == NULL)
  823. return -ENOMEM;
  824. }
  825. return 0;
  826. }
  827. /* Parse perf-probe event command */
  828. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  829. {
  830. char **argv;
  831. int argc, i, ret = 0;
  832. argv = argv_split(cmd, &argc);
  833. if (!argv) {
  834. pr_debug("Failed to split arguments.\n");
  835. return -ENOMEM;
  836. }
  837. if (argc - 1 > MAX_PROBE_ARGS) {
  838. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  839. ret = -ERANGE;
  840. goto out;
  841. }
  842. /* Parse probe point */
  843. ret = parse_perf_probe_point(argv[0], pev);
  844. if (ret < 0)
  845. goto out;
  846. /* Copy arguments and ensure return probe has no C argument */
  847. pev->nargs = argc - 1;
  848. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  849. if (pev->args == NULL) {
  850. ret = -ENOMEM;
  851. goto out;
  852. }
  853. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  854. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  855. if (ret >= 0 &&
  856. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  857. semantic_error("You can't specify local variable for"
  858. " kretprobe.\n");
  859. ret = -EINVAL;
  860. }
  861. }
  862. out:
  863. argv_free(argv);
  864. return ret;
  865. }
  866. /* Return true if this perf_probe_event requires debuginfo */
  867. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  868. {
  869. int i;
  870. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  871. return true;
  872. for (i = 0; i < pev->nargs; i++)
  873. if (is_c_varname(pev->args[i].var))
  874. return true;
  875. return false;
  876. }
  877. /* Parse probe_events event into struct probe_point */
  878. static int parse_probe_trace_command(const char *cmd,
  879. struct probe_trace_event *tev)
  880. {
  881. struct probe_trace_point *tp = &tev->point;
  882. char pr;
  883. char *p;
  884. int ret, i, argc;
  885. char **argv;
  886. pr_debug("Parsing probe_events: %s\n", cmd);
  887. argv = argv_split(cmd, &argc);
  888. if (!argv) {
  889. pr_debug("Failed to split arguments.\n");
  890. return -ENOMEM;
  891. }
  892. if (argc < 2) {
  893. semantic_error("Too few probe arguments.\n");
  894. ret = -ERANGE;
  895. goto out;
  896. }
  897. /* Scan event and group name. */
  898. ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
  899. &pr, (float *)(void *)&tev->group,
  900. (float *)(void *)&tev->event);
  901. if (ret != 3) {
  902. semantic_error("Failed to parse event name: %s\n", argv[0]);
  903. ret = -EINVAL;
  904. goto out;
  905. }
  906. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  907. tp->retprobe = (pr == 'r');
  908. /* Scan function name and offset */
  909. ret = sscanf(argv[1], "%a[^+]+%lu", (float *)(void *)&tp->symbol,
  910. &tp->offset);
  911. if (ret == 1)
  912. tp->offset = 0;
  913. tev->nargs = argc - 2;
  914. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  915. if (tev->args == NULL) {
  916. ret = -ENOMEM;
  917. goto out;
  918. }
  919. for (i = 0; i < tev->nargs; i++) {
  920. p = strchr(argv[i + 2], '=');
  921. if (p) /* We don't need which register is assigned. */
  922. *p++ = '\0';
  923. else
  924. p = argv[i + 2];
  925. tev->args[i].name = strdup(argv[i + 2]);
  926. /* TODO: parse regs and offset */
  927. tev->args[i].value = strdup(p);
  928. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  929. ret = -ENOMEM;
  930. goto out;
  931. }
  932. }
  933. ret = 0;
  934. out:
  935. argv_free(argv);
  936. return ret;
  937. }
  938. /* Compose only probe arg */
  939. int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
  940. {
  941. struct perf_probe_arg_field *field = pa->field;
  942. int ret;
  943. char *tmp = buf;
  944. if (pa->name && pa->var)
  945. ret = e_snprintf(tmp, len, "%s=%s", pa->name, pa->var);
  946. else
  947. ret = e_snprintf(tmp, len, "%s", pa->name ? pa->name : pa->var);
  948. if (ret <= 0)
  949. goto error;
  950. tmp += ret;
  951. len -= ret;
  952. while (field) {
  953. if (field->name[0] == '[')
  954. ret = e_snprintf(tmp, len, "%s", field->name);
  955. else
  956. ret = e_snprintf(tmp, len, "%s%s",
  957. field->ref ? "->" : ".", field->name);
  958. if (ret <= 0)
  959. goto error;
  960. tmp += ret;
  961. len -= ret;
  962. field = field->next;
  963. }
  964. if (pa->type) {
  965. ret = e_snprintf(tmp, len, ":%s", pa->type);
  966. if (ret <= 0)
  967. goto error;
  968. tmp += ret;
  969. len -= ret;
  970. }
  971. return tmp - buf;
  972. error:
  973. pr_debug("Failed to synthesize perf probe argument: %s\n",
  974. strerror(-ret));
  975. return ret;
  976. }
  977. /* Compose only probe point (not argument) */
  978. static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  979. {
  980. char *buf, *tmp;
  981. char offs[32] = "", line[32] = "", file[32] = "";
  982. int ret, len;
  983. buf = zalloc(MAX_CMDLEN);
  984. if (buf == NULL) {
  985. ret = -ENOMEM;
  986. goto error;
  987. }
  988. if (pp->offset) {
  989. ret = e_snprintf(offs, 32, "+%lu", pp->offset);
  990. if (ret <= 0)
  991. goto error;
  992. }
  993. if (pp->line) {
  994. ret = e_snprintf(line, 32, ":%d", pp->line);
  995. if (ret <= 0)
  996. goto error;
  997. }
  998. if (pp->file) {
  999. tmp = pp->file;
  1000. len = strlen(tmp);
  1001. if (len > 30) {
  1002. tmp = strchr(pp->file + len - 30, '/');
  1003. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1004. }
  1005. ret = e_snprintf(file, 32, "@%s", tmp);
  1006. if (ret <= 0)
  1007. goto error;
  1008. }
  1009. if (pp->function)
  1010. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s%s%s%s", pp->function,
  1011. offs, pp->retprobe ? "%return" : "", line,
  1012. file);
  1013. else
  1014. ret = e_snprintf(buf, MAX_CMDLEN, "%s%s", file, line);
  1015. if (ret <= 0)
  1016. goto error;
  1017. return buf;
  1018. error:
  1019. pr_debug("Failed to synthesize perf probe point: %s\n",
  1020. strerror(-ret));
  1021. if (buf)
  1022. free(buf);
  1023. return NULL;
  1024. }
  1025. #if 0
  1026. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1027. {
  1028. char *buf;
  1029. int i, len, ret;
  1030. buf = synthesize_perf_probe_point(&pev->point);
  1031. if (!buf)
  1032. return NULL;
  1033. len = strlen(buf);
  1034. for (i = 0; i < pev->nargs; i++) {
  1035. ret = e_snprintf(&buf[len], MAX_CMDLEN - len, " %s",
  1036. pev->args[i].name);
  1037. if (ret <= 0) {
  1038. free(buf);
  1039. return NULL;
  1040. }
  1041. len += ret;
  1042. }
  1043. return buf;
  1044. }
  1045. #endif
  1046. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1047. char **buf, size_t *buflen,
  1048. int depth)
  1049. {
  1050. int ret;
  1051. if (ref->next) {
  1052. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1053. buflen, depth + 1);
  1054. if (depth < 0)
  1055. goto out;
  1056. }
  1057. ret = e_snprintf(*buf, *buflen, "%+ld(", ref->offset);
  1058. if (ret < 0)
  1059. depth = ret;
  1060. else {
  1061. *buf += ret;
  1062. *buflen -= ret;
  1063. }
  1064. out:
  1065. return depth;
  1066. }
  1067. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1068. char *buf, size_t buflen)
  1069. {
  1070. struct probe_trace_arg_ref *ref = arg->ref;
  1071. int ret, depth = 0;
  1072. char *tmp = buf;
  1073. /* Argument name or separator */
  1074. if (arg->name)
  1075. ret = e_snprintf(buf, buflen, " %s=", arg->name);
  1076. else
  1077. ret = e_snprintf(buf, buflen, " ");
  1078. if (ret < 0)
  1079. return ret;
  1080. buf += ret;
  1081. buflen -= ret;
  1082. /* Special case: @XXX */
  1083. if (arg->value[0] == '@' && arg->ref)
  1084. ref = ref->next;
  1085. /* Dereferencing arguments */
  1086. if (ref) {
  1087. depth = __synthesize_probe_trace_arg_ref(ref, &buf,
  1088. &buflen, 1);
  1089. if (depth < 0)
  1090. return depth;
  1091. }
  1092. /* Print argument value */
  1093. if (arg->value[0] == '@' && arg->ref)
  1094. ret = e_snprintf(buf, buflen, "%s%+ld", arg->value,
  1095. arg->ref->offset);
  1096. else
  1097. ret = e_snprintf(buf, buflen, "%s", arg->value);
  1098. if (ret < 0)
  1099. return ret;
  1100. buf += ret;
  1101. buflen -= ret;
  1102. /* Closing */
  1103. while (depth--) {
  1104. ret = e_snprintf(buf, buflen, ")");
  1105. if (ret < 0)
  1106. return ret;
  1107. buf += ret;
  1108. buflen -= ret;
  1109. }
  1110. /* Print argument type */
  1111. if (arg->type) {
  1112. ret = e_snprintf(buf, buflen, ":%s", arg->type);
  1113. if (ret <= 0)
  1114. return ret;
  1115. buf += ret;
  1116. }
  1117. return buf - tmp;
  1118. }
  1119. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1120. {
  1121. struct probe_trace_point *tp = &tev->point;
  1122. char *buf;
  1123. int i, len, ret;
  1124. buf = zalloc(MAX_CMDLEN);
  1125. if (buf == NULL)
  1126. return NULL;
  1127. len = e_snprintf(buf, MAX_CMDLEN, "%c:%s/%s %s+%lu",
  1128. tp->retprobe ? 'r' : 'p',
  1129. tev->group, tev->event,
  1130. tp->symbol, tp->offset);
  1131. if (len <= 0)
  1132. goto error;
  1133. for (i = 0; i < tev->nargs; i++) {
  1134. ret = synthesize_probe_trace_arg(&tev->args[i], buf + len,
  1135. MAX_CMDLEN - len);
  1136. if (ret <= 0)
  1137. goto error;
  1138. len += ret;
  1139. }
  1140. return buf;
  1141. error:
  1142. free(buf);
  1143. return NULL;
  1144. }
  1145. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1146. struct perf_probe_event *pev)
  1147. {
  1148. char buf[64] = "";
  1149. int i, ret;
  1150. /* Convert event/group name */
  1151. pev->event = strdup(tev->event);
  1152. pev->group = strdup(tev->group);
  1153. if (pev->event == NULL || pev->group == NULL)
  1154. return -ENOMEM;
  1155. /* Convert trace_point to probe_point */
  1156. ret = kprobe_convert_to_perf_probe(&tev->point, &pev->point);
  1157. if (ret < 0)
  1158. return ret;
  1159. /* Convert trace_arg to probe_arg */
  1160. pev->nargs = tev->nargs;
  1161. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1162. if (pev->args == NULL)
  1163. return -ENOMEM;
  1164. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1165. if (tev->args[i].name)
  1166. pev->args[i].name = strdup(tev->args[i].name);
  1167. else {
  1168. ret = synthesize_probe_trace_arg(&tev->args[i],
  1169. buf, 64);
  1170. pev->args[i].name = strdup(buf);
  1171. }
  1172. if (pev->args[i].name == NULL && ret >= 0)
  1173. ret = -ENOMEM;
  1174. }
  1175. if (ret < 0)
  1176. clear_perf_probe_event(pev);
  1177. return ret;
  1178. }
  1179. void clear_perf_probe_event(struct perf_probe_event *pev)
  1180. {
  1181. struct perf_probe_point *pp = &pev->point;
  1182. struct perf_probe_arg_field *field, *next;
  1183. int i;
  1184. if (pev->event)
  1185. free(pev->event);
  1186. if (pev->group)
  1187. free(pev->group);
  1188. if (pp->file)
  1189. free(pp->file);
  1190. if (pp->function)
  1191. free(pp->function);
  1192. if (pp->lazy_line)
  1193. free(pp->lazy_line);
  1194. for (i = 0; i < pev->nargs; i++) {
  1195. if (pev->args[i].name)
  1196. free(pev->args[i].name);
  1197. if (pev->args[i].var)
  1198. free(pev->args[i].var);
  1199. if (pev->args[i].type)
  1200. free(pev->args[i].type);
  1201. field = pev->args[i].field;
  1202. while (field) {
  1203. next = field->next;
  1204. if (field->name)
  1205. free(field->name);
  1206. free(field);
  1207. field = next;
  1208. }
  1209. }
  1210. if (pev->args)
  1211. free(pev->args);
  1212. memset(pev, 0, sizeof(*pev));
  1213. }
  1214. static void clear_probe_trace_event(struct probe_trace_event *tev)
  1215. {
  1216. struct probe_trace_arg_ref *ref, *next;
  1217. int i;
  1218. if (tev->event)
  1219. free(tev->event);
  1220. if (tev->group)
  1221. free(tev->group);
  1222. if (tev->point.symbol)
  1223. free(tev->point.symbol);
  1224. for (i = 0; i < tev->nargs; i++) {
  1225. if (tev->args[i].name)
  1226. free(tev->args[i].name);
  1227. if (tev->args[i].value)
  1228. free(tev->args[i].value);
  1229. if (tev->args[i].type)
  1230. free(tev->args[i].type);
  1231. ref = tev->args[i].ref;
  1232. while (ref) {
  1233. next = ref->next;
  1234. free(ref);
  1235. ref = next;
  1236. }
  1237. }
  1238. if (tev->args)
  1239. free(tev->args);
  1240. memset(tev, 0, sizeof(*tev));
  1241. }
  1242. static int open_kprobe_events(bool readwrite)
  1243. {
  1244. char buf[PATH_MAX];
  1245. const char *__debugfs;
  1246. int ret;
  1247. __debugfs = debugfs_find_mountpoint();
  1248. if (__debugfs == NULL) {
  1249. pr_warning("Debugfs is not mounted.\n");
  1250. return -ENOENT;
  1251. }
  1252. ret = e_snprintf(buf, PATH_MAX, "%stracing/kprobe_events", __debugfs);
  1253. if (ret >= 0) {
  1254. pr_debug("Opening %s write=%d\n", buf, readwrite);
  1255. if (readwrite && !probe_event_dry_run)
  1256. ret = open(buf, O_RDWR, O_APPEND);
  1257. else
  1258. ret = open(buf, O_RDONLY, 0);
  1259. }
  1260. if (ret < 0) {
  1261. if (errno == ENOENT)
  1262. pr_warning("kprobe_events file does not exist - please"
  1263. " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
  1264. else
  1265. pr_warning("Failed to open kprobe_events file: %s\n",
  1266. strerror(errno));
  1267. }
  1268. return ret;
  1269. }
  1270. /* Get raw string list of current kprobe_events */
  1271. static struct strlist *get_probe_trace_command_rawlist(int fd)
  1272. {
  1273. int ret, idx;
  1274. FILE *fp;
  1275. char buf[MAX_CMDLEN];
  1276. char *p;
  1277. struct strlist *sl;
  1278. sl = strlist__new(true, NULL);
  1279. fp = fdopen(dup(fd), "r");
  1280. while (!feof(fp)) {
  1281. p = fgets(buf, MAX_CMDLEN, fp);
  1282. if (!p)
  1283. break;
  1284. idx = strlen(p) - 1;
  1285. if (p[idx] == '\n')
  1286. p[idx] = '\0';
  1287. ret = strlist__add(sl, buf);
  1288. if (ret < 0) {
  1289. pr_debug("strlist__add failed: %s\n", strerror(-ret));
  1290. strlist__delete(sl);
  1291. return NULL;
  1292. }
  1293. }
  1294. fclose(fp);
  1295. return sl;
  1296. }
  1297. /* Show an event */
  1298. static int show_perf_probe_event(struct perf_probe_event *pev)
  1299. {
  1300. int i, ret;
  1301. char buf[128];
  1302. char *place;
  1303. /* Synthesize only event probe point */
  1304. place = synthesize_perf_probe_point(&pev->point);
  1305. if (!place)
  1306. return -EINVAL;
  1307. ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
  1308. if (ret < 0)
  1309. return ret;
  1310. printf(" %-20s (on %s", buf, place);
  1311. if (pev->nargs > 0) {
  1312. printf(" with");
  1313. for (i = 0; i < pev->nargs; i++) {
  1314. ret = synthesize_perf_probe_arg(&pev->args[i],
  1315. buf, 128);
  1316. if (ret < 0)
  1317. break;
  1318. printf(" %s", buf);
  1319. }
  1320. }
  1321. printf(")\n");
  1322. free(place);
  1323. return ret;
  1324. }
  1325. /* List up current perf-probe events */
  1326. int show_perf_probe_events(void)
  1327. {
  1328. int fd, ret;
  1329. struct probe_trace_event tev;
  1330. struct perf_probe_event pev;
  1331. struct strlist *rawlist;
  1332. struct str_node *ent;
  1333. setup_pager();
  1334. ret = init_vmlinux();
  1335. if (ret < 0)
  1336. return ret;
  1337. memset(&tev, 0, sizeof(tev));
  1338. memset(&pev, 0, sizeof(pev));
  1339. fd = open_kprobe_events(false);
  1340. if (fd < 0)
  1341. return fd;
  1342. rawlist = get_probe_trace_command_rawlist(fd);
  1343. close(fd);
  1344. if (!rawlist)
  1345. return -ENOENT;
  1346. strlist__for_each(ent, rawlist) {
  1347. ret = parse_probe_trace_command(ent->s, &tev);
  1348. if (ret >= 0) {
  1349. ret = convert_to_perf_probe_event(&tev, &pev);
  1350. if (ret >= 0)
  1351. ret = show_perf_probe_event(&pev);
  1352. }
  1353. clear_perf_probe_event(&pev);
  1354. clear_probe_trace_event(&tev);
  1355. if (ret < 0)
  1356. break;
  1357. }
  1358. strlist__delete(rawlist);
  1359. return ret;
  1360. }
  1361. /* Get current perf-probe event names */
  1362. static struct strlist *get_probe_trace_event_names(int fd, bool include_group)
  1363. {
  1364. char buf[128];
  1365. struct strlist *sl, *rawlist;
  1366. struct str_node *ent;
  1367. struct probe_trace_event tev;
  1368. int ret = 0;
  1369. memset(&tev, 0, sizeof(tev));
  1370. rawlist = get_probe_trace_command_rawlist(fd);
  1371. sl = strlist__new(true, NULL);
  1372. strlist__for_each(ent, rawlist) {
  1373. ret = parse_probe_trace_command(ent->s, &tev);
  1374. if (ret < 0)
  1375. break;
  1376. if (include_group) {
  1377. ret = e_snprintf(buf, 128, "%s:%s", tev.group,
  1378. tev.event);
  1379. if (ret >= 0)
  1380. ret = strlist__add(sl, buf);
  1381. } else
  1382. ret = strlist__add(sl, tev.event);
  1383. clear_probe_trace_event(&tev);
  1384. if (ret < 0)
  1385. break;
  1386. }
  1387. strlist__delete(rawlist);
  1388. if (ret < 0) {
  1389. strlist__delete(sl);
  1390. return NULL;
  1391. }
  1392. return sl;
  1393. }
  1394. static int write_probe_trace_event(int fd, struct probe_trace_event *tev)
  1395. {
  1396. int ret = 0;
  1397. char *buf = synthesize_probe_trace_command(tev);
  1398. if (!buf) {
  1399. pr_debug("Failed to synthesize probe trace event.\n");
  1400. return -EINVAL;
  1401. }
  1402. pr_debug("Writing event: %s\n", buf);
  1403. if (!probe_event_dry_run) {
  1404. ret = write(fd, buf, strlen(buf));
  1405. if (ret <= 0)
  1406. pr_warning("Failed to write event: %s\n",
  1407. strerror(errno));
  1408. }
  1409. free(buf);
  1410. return ret;
  1411. }
  1412. static int get_new_event_name(char *buf, size_t len, const char *base,
  1413. struct strlist *namelist, bool allow_suffix)
  1414. {
  1415. int i, ret;
  1416. /* Try no suffix */
  1417. ret = e_snprintf(buf, len, "%s", base);
  1418. if (ret < 0) {
  1419. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1420. return ret;
  1421. }
  1422. if (!strlist__has_entry(namelist, buf))
  1423. return 0;
  1424. if (!allow_suffix) {
  1425. pr_warning("Error: event \"%s\" already exists. "
  1426. "(Use -f to force duplicates.)\n", base);
  1427. return -EEXIST;
  1428. }
  1429. /* Try to add suffix */
  1430. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  1431. ret = e_snprintf(buf, len, "%s_%d", base, i);
  1432. if (ret < 0) {
  1433. pr_debug("snprintf() failed: %s\n", strerror(-ret));
  1434. return ret;
  1435. }
  1436. if (!strlist__has_entry(namelist, buf))
  1437. break;
  1438. }
  1439. if (i == MAX_EVENT_INDEX) {
  1440. pr_warning("Too many events are on the same function.\n");
  1441. ret = -ERANGE;
  1442. }
  1443. return ret;
  1444. }
  1445. static int __add_probe_trace_events(struct perf_probe_event *pev,
  1446. struct probe_trace_event *tevs,
  1447. int ntevs, bool allow_suffix)
  1448. {
  1449. int i, fd, ret;
  1450. struct probe_trace_event *tev = NULL;
  1451. char buf[64];
  1452. const char *event, *group;
  1453. struct strlist *namelist;
  1454. fd = open_kprobe_events(true);
  1455. if (fd < 0)
  1456. return fd;
  1457. /* Get current event names */
  1458. namelist = get_probe_trace_event_names(fd, false);
  1459. if (!namelist) {
  1460. pr_debug("Failed to get current event list.\n");
  1461. return -EIO;
  1462. }
  1463. ret = 0;
  1464. printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
  1465. for (i = 0; i < ntevs; i++) {
  1466. tev = &tevs[i];
  1467. if (pev->event)
  1468. event = pev->event;
  1469. else
  1470. if (pev->point.function)
  1471. event = pev->point.function;
  1472. else
  1473. event = tev->point.symbol;
  1474. if (pev->group)
  1475. group = pev->group;
  1476. else
  1477. group = PERFPROBE_GROUP;
  1478. /* Get an unused new event name */
  1479. ret = get_new_event_name(buf, 64, event,
  1480. namelist, allow_suffix);
  1481. if (ret < 0)
  1482. break;
  1483. event = buf;
  1484. tev->event = strdup(event);
  1485. tev->group = strdup(group);
  1486. if (tev->event == NULL || tev->group == NULL) {
  1487. ret = -ENOMEM;
  1488. break;
  1489. }
  1490. ret = write_probe_trace_event(fd, tev);
  1491. if (ret < 0)
  1492. break;
  1493. /* Add added event name to namelist */
  1494. strlist__add(namelist, event);
  1495. /* Trick here - save current event/group */
  1496. event = pev->event;
  1497. group = pev->group;
  1498. pev->event = tev->event;
  1499. pev->group = tev->group;
  1500. show_perf_probe_event(pev);
  1501. /* Trick here - restore current event/group */
  1502. pev->event = (char *)event;
  1503. pev->group = (char *)group;
  1504. /*
  1505. * Probes after the first probe which comes from same
  1506. * user input are always allowed to add suffix, because
  1507. * there might be several addresses corresponding to
  1508. * one code line.
  1509. */
  1510. allow_suffix = true;
  1511. }
  1512. if (ret >= 0) {
  1513. /* Show how to use the event. */
  1514. printf("\nYou can now use it on all perf tools, such as:\n\n");
  1515. printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
  1516. tev->event);
  1517. }
  1518. strlist__delete(namelist);
  1519. close(fd);
  1520. return ret;
  1521. }
  1522. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  1523. struct probe_trace_event **tevs,
  1524. int max_tevs, const char *module)
  1525. {
  1526. struct symbol *sym;
  1527. int ret = 0, i;
  1528. struct probe_trace_event *tev;
  1529. /* Convert perf_probe_event with debuginfo */
  1530. ret = try_to_find_probe_trace_events(pev, tevs, max_tevs, module);
  1531. if (ret != 0)
  1532. return ret;
  1533. /* Allocate trace event buffer */
  1534. tev = *tevs = zalloc(sizeof(struct probe_trace_event));
  1535. if (tev == NULL)
  1536. return -ENOMEM;
  1537. /* Copy parameters */
  1538. tev->point.symbol = strdup(pev->point.function);
  1539. if (tev->point.symbol == NULL) {
  1540. ret = -ENOMEM;
  1541. goto error;
  1542. }
  1543. tev->point.offset = pev->point.offset;
  1544. tev->point.retprobe = pev->point.retprobe;
  1545. tev->nargs = pev->nargs;
  1546. if (tev->nargs) {
  1547. tev->args = zalloc(sizeof(struct probe_trace_arg)
  1548. * tev->nargs);
  1549. if (tev->args == NULL) {
  1550. ret = -ENOMEM;
  1551. goto error;
  1552. }
  1553. for (i = 0; i < tev->nargs; i++) {
  1554. if (pev->args[i].name) {
  1555. tev->args[i].name = strdup(pev->args[i].name);
  1556. if (tev->args[i].name == NULL) {
  1557. ret = -ENOMEM;
  1558. goto error;
  1559. }
  1560. }
  1561. tev->args[i].value = strdup(pev->args[i].var);
  1562. if (tev->args[i].value == NULL) {
  1563. ret = -ENOMEM;
  1564. goto error;
  1565. }
  1566. if (pev->args[i].type) {
  1567. tev->args[i].type = strdup(pev->args[i].type);
  1568. if (tev->args[i].type == NULL) {
  1569. ret = -ENOMEM;
  1570. goto error;
  1571. }
  1572. }
  1573. }
  1574. }
  1575. /* Currently just checking function name from symbol map */
  1576. sym = __find_kernel_function_by_name(tev->point.symbol, NULL);
  1577. if (!sym) {
  1578. pr_warning("Kernel symbol \'%s\' not found.\n",
  1579. tev->point.symbol);
  1580. ret = -ENOENT;
  1581. goto error;
  1582. }
  1583. return 1;
  1584. error:
  1585. clear_probe_trace_event(tev);
  1586. free(tev);
  1587. *tevs = NULL;
  1588. return ret;
  1589. }
  1590. struct __event_package {
  1591. struct perf_probe_event *pev;
  1592. struct probe_trace_event *tevs;
  1593. int ntevs;
  1594. };
  1595. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
  1596. int max_tevs, const char *module, bool force_add)
  1597. {
  1598. int i, j, ret;
  1599. struct __event_package *pkgs;
  1600. pkgs = zalloc(sizeof(struct __event_package) * npevs);
  1601. if (pkgs == NULL)
  1602. return -ENOMEM;
  1603. /* Init vmlinux path */
  1604. ret = init_vmlinux();
  1605. if (ret < 0) {
  1606. free(pkgs);
  1607. return ret;
  1608. }
  1609. /* Loop 1: convert all events */
  1610. for (i = 0; i < npevs; i++) {
  1611. pkgs[i].pev = &pevs[i];
  1612. /* Convert with or without debuginfo */
  1613. ret = convert_to_probe_trace_events(pkgs[i].pev,
  1614. &pkgs[i].tevs,
  1615. max_tevs,
  1616. module);
  1617. if (ret < 0)
  1618. goto end;
  1619. pkgs[i].ntevs = ret;
  1620. }
  1621. /* Loop 2: add all events */
  1622. for (i = 0; i < npevs; i++) {
  1623. ret = __add_probe_trace_events(pkgs[i].pev, pkgs[i].tevs,
  1624. pkgs[i].ntevs, force_add);
  1625. if (ret < 0)
  1626. break;
  1627. }
  1628. end:
  1629. /* Loop 3: cleanup and free trace events */
  1630. for (i = 0; i < npevs; i++) {
  1631. for (j = 0; j < pkgs[i].ntevs; j++)
  1632. clear_probe_trace_event(&pkgs[i].tevs[j]);
  1633. free(pkgs[i].tevs);
  1634. }
  1635. free(pkgs);
  1636. return ret;
  1637. }
  1638. static int __del_trace_probe_event(int fd, struct str_node *ent)
  1639. {
  1640. char *p;
  1641. char buf[128];
  1642. int ret;
  1643. /* Convert from perf-probe event to trace-probe event */
  1644. ret = e_snprintf(buf, 128, "-:%s", ent->s);
  1645. if (ret < 0)
  1646. goto error;
  1647. p = strchr(buf + 2, ':');
  1648. if (!p) {
  1649. pr_debug("Internal error: %s should have ':' but not.\n",
  1650. ent->s);
  1651. ret = -ENOTSUP;
  1652. goto error;
  1653. }
  1654. *p = '/';
  1655. pr_debug("Writing event: %s\n", buf);
  1656. ret = write(fd, buf, strlen(buf));
  1657. if (ret < 0)
  1658. goto error;
  1659. printf("Remove event: %s\n", ent->s);
  1660. return 0;
  1661. error:
  1662. pr_warning("Failed to delete event: %s\n", strerror(-ret));
  1663. return ret;
  1664. }
  1665. static int del_trace_probe_event(int fd, const char *group,
  1666. const char *event, struct strlist *namelist)
  1667. {
  1668. char buf[128];
  1669. struct str_node *ent, *n;
  1670. int found = 0, ret = 0;
  1671. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1672. if (ret < 0) {
  1673. pr_err("Failed to copy event.\n");
  1674. return ret;
  1675. }
  1676. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1677. strlist__for_each_safe(ent, n, namelist)
  1678. if (strglobmatch(ent->s, buf)) {
  1679. found++;
  1680. ret = __del_trace_probe_event(fd, ent);
  1681. if (ret < 0)
  1682. break;
  1683. strlist__remove(namelist, ent);
  1684. }
  1685. } else {
  1686. ent = strlist__find(namelist, buf);
  1687. if (ent) {
  1688. found++;
  1689. ret = __del_trace_probe_event(fd, ent);
  1690. if (ret >= 0)
  1691. strlist__remove(namelist, ent);
  1692. }
  1693. }
  1694. if (found == 0 && ret >= 0)
  1695. pr_info("Info: Event \"%s\" does not exist.\n", buf);
  1696. return ret;
  1697. }
  1698. int del_perf_probe_events(struct strlist *dellist)
  1699. {
  1700. int fd, ret = 0;
  1701. const char *group, *event;
  1702. char *p, *str;
  1703. struct str_node *ent;
  1704. struct strlist *namelist;
  1705. fd = open_kprobe_events(true);
  1706. if (fd < 0)
  1707. return fd;
  1708. /* Get current event names */
  1709. namelist = get_probe_trace_event_names(fd, true);
  1710. if (namelist == NULL)
  1711. return -EINVAL;
  1712. strlist__for_each(ent, dellist) {
  1713. str = strdup(ent->s);
  1714. if (str == NULL) {
  1715. ret = -ENOMEM;
  1716. break;
  1717. }
  1718. pr_debug("Parsing: %s\n", str);
  1719. p = strchr(str, ':');
  1720. if (p) {
  1721. group = str;
  1722. *p = '\0';
  1723. event = p + 1;
  1724. } else {
  1725. group = "*";
  1726. event = str;
  1727. }
  1728. pr_debug("Group: %s, Event: %s\n", group, event);
  1729. ret = del_trace_probe_event(fd, group, event, namelist);
  1730. free(str);
  1731. if (ret < 0)
  1732. break;
  1733. }
  1734. strlist__delete(namelist);
  1735. close(fd);
  1736. return ret;
  1737. }
  1738. /* TODO: don't use a global variable for filter ... */
  1739. static struct strfilter *available_func_filter;
  1740. /*
  1741. * If a symbol corresponds to a function with global binding and
  1742. * matches filter return 0. For all others return 1.
  1743. */
  1744. static int filter_available_functions(struct map *map __unused,
  1745. struct symbol *sym)
  1746. {
  1747. if (sym->binding == STB_GLOBAL &&
  1748. strfilter__compare(available_func_filter, sym->name))
  1749. return 0;
  1750. return 1;
  1751. }
  1752. int show_available_funcs(const char *module, struct strfilter *_filter)
  1753. {
  1754. struct map *map;
  1755. int ret;
  1756. setup_pager();
  1757. ret = init_vmlinux();
  1758. if (ret < 0)
  1759. return ret;
  1760. map = kernel_get_module_map(module);
  1761. if (!map) {
  1762. pr_err("Failed to find %s map.\n", (module) ? : "kernel");
  1763. return -EINVAL;
  1764. }
  1765. available_func_filter = _filter;
  1766. if (map__load(map, filter_available_functions)) {
  1767. pr_err("Failed to load map.\n");
  1768. return -EINVAL;
  1769. }
  1770. if (!dso__sorted_by_name(map->dso, map->type))
  1771. dso__sort_by_name(map->dso, map->type);
  1772. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  1773. return 0;
  1774. }