probe-event.c 64 KB

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