probe-event.c 56 KB

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