probe-event.c 58 KB

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