probe-event.c 63 KB

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