probe-event.c 67 KB

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