probe-event.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  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. tev->point.module = strdup(module);
  1608. if (tev->point.module == NULL) {
  1609. ret = -ENOMEM;
  1610. goto error;
  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. goto error;
  1728. printf("Remove event: %s\n", ent->s);
  1729. return 0;
  1730. error:
  1731. pr_warning("Failed to delete event: %s\n", strerror(-ret));
  1732. return ret;
  1733. }
  1734. static int del_trace_probe_event(int fd, const char *group,
  1735. const char *event, struct strlist *namelist)
  1736. {
  1737. char buf[128];
  1738. struct str_node *ent, *n;
  1739. int found = 0, ret = 0;
  1740. ret = e_snprintf(buf, 128, "%s:%s", group, event);
  1741. if (ret < 0) {
  1742. pr_err("Failed to copy event.\n");
  1743. return ret;
  1744. }
  1745. if (strpbrk(buf, "*?")) { /* Glob-exp */
  1746. strlist__for_each_safe(ent, n, namelist)
  1747. if (strglobmatch(ent->s, buf)) {
  1748. found++;
  1749. ret = __del_trace_probe_event(fd, ent);
  1750. if (ret < 0)
  1751. break;
  1752. strlist__remove(namelist, ent);
  1753. }
  1754. } else {
  1755. ent = strlist__find(namelist, buf);
  1756. if (ent) {
  1757. found++;
  1758. ret = __del_trace_probe_event(fd, ent);
  1759. if (ret >= 0)
  1760. strlist__remove(namelist, ent);
  1761. }
  1762. }
  1763. if (found == 0 && ret >= 0)
  1764. pr_info("Info: Event \"%s\" does not exist.\n", buf);
  1765. return ret;
  1766. }
  1767. int del_perf_probe_events(struct strlist *dellist)
  1768. {
  1769. int fd, ret = 0;
  1770. const char *group, *event;
  1771. char *p, *str;
  1772. struct str_node *ent;
  1773. struct strlist *namelist;
  1774. fd = open_kprobe_events(true);
  1775. if (fd < 0)
  1776. return fd;
  1777. /* Get current event names */
  1778. namelist = get_probe_trace_event_names(fd, true);
  1779. if (namelist == NULL)
  1780. return -EINVAL;
  1781. strlist__for_each(ent, dellist) {
  1782. str = strdup(ent->s);
  1783. if (str == NULL) {
  1784. ret = -ENOMEM;
  1785. break;
  1786. }
  1787. pr_debug("Parsing: %s\n", str);
  1788. p = strchr(str, ':');
  1789. if (p) {
  1790. group = str;
  1791. *p = '\0';
  1792. event = p + 1;
  1793. } else {
  1794. group = "*";
  1795. event = str;
  1796. }
  1797. pr_debug("Group: %s, Event: %s\n", group, event);
  1798. ret = del_trace_probe_event(fd, group, event, namelist);
  1799. free(str);
  1800. if (ret < 0)
  1801. break;
  1802. }
  1803. strlist__delete(namelist);
  1804. close(fd);
  1805. return ret;
  1806. }
  1807. /* TODO: don't use a global variable for filter ... */
  1808. static struct strfilter *available_func_filter;
  1809. /*
  1810. * If a symbol corresponds to a function with global binding and
  1811. * matches filter return 0. For all others return 1.
  1812. */
  1813. static int filter_available_functions(struct map *map __unused,
  1814. struct symbol *sym)
  1815. {
  1816. if (sym->binding == STB_GLOBAL &&
  1817. strfilter__compare(available_func_filter, sym->name))
  1818. return 0;
  1819. return 1;
  1820. }
  1821. int show_available_funcs(const char *module, struct strfilter *_filter)
  1822. {
  1823. struct map *map;
  1824. int ret;
  1825. setup_pager();
  1826. ret = init_vmlinux();
  1827. if (ret < 0)
  1828. return ret;
  1829. map = kernel_get_module_map(module);
  1830. if (!map) {
  1831. pr_err("Failed to find %s map.\n", (module) ? : "kernel");
  1832. return -EINVAL;
  1833. }
  1834. available_func_filter = _filter;
  1835. if (map__load(map, filter_available_functions)) {
  1836. pr_err("Failed to load map.\n");
  1837. return -EINVAL;
  1838. }
  1839. if (!dso__sorted_by_name(map->dso, map->type))
  1840. dso__sort_by_name(map->dso, map->type);
  1841. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  1842. return 0;
  1843. }