probe-event.c 51 KB

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