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