probe-event.c 44 KB

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