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