probe-event.c 46 KB

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