probe-event.c 53 KB

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