probe-event.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531
  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 <inttypes.h>
  22. #include <sys/utsname.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #include <elf.h>
  34. #include "util.h"
  35. #include "event.h"
  36. #include "strlist.h"
  37. #include "strfilter.h"
  38. #include "debug.h"
  39. #include "cache.h"
  40. #include "color.h"
  41. #include "symbol.h"
  42. #include "thread.h"
  43. #include <api/fs/fs.h>
  44. #include "trace-event.h" /* For __maybe_unused */
  45. #include "probe-event.h"
  46. #include "probe-finder.h"
  47. #include "probe-file.h"
  48. #include "session.h"
  49. #include "string2.h"
  50. #include "sane_ctype.h"
  51. #define PERFPROBE_GROUP "probe"
  52. bool probe_event_dry_run; /* Dry run flag */
  53. struct probe_conf probe_conf;
  54. #define semantic_error(msg ...) pr_err("Semantic error :" msg)
  55. 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 struct machine *host_machine;
  67. /* Initialize symbol maps and path of vmlinux/modules */
  68. int init_probe_symbol_maps(bool user_only)
  69. {
  70. int ret;
  71. symbol_conf.sort_by_name = true;
  72. symbol_conf.allow_aliases = true;
  73. ret = symbol__init(NULL);
  74. if (ret < 0) {
  75. pr_debug("Failed to init symbol map.\n");
  76. goto out;
  77. }
  78. if (host_machine || user_only) /* already initialized */
  79. return 0;
  80. if (symbol_conf.vmlinux_name)
  81. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  82. host_machine = machine__new_host();
  83. if (!host_machine) {
  84. pr_debug("machine__new_host() failed.\n");
  85. symbol__exit();
  86. ret = -1;
  87. }
  88. out:
  89. if (ret < 0)
  90. pr_warning("Failed to init vmlinux path.\n");
  91. return ret;
  92. }
  93. void exit_probe_symbol_maps(void)
  94. {
  95. machine__delete(host_machine);
  96. host_machine = NULL;
  97. symbol__exit();
  98. }
  99. static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
  100. {
  101. /* kmap->ref_reloc_sym should be set if host_machine is initialized */
  102. struct kmap *kmap;
  103. struct map *map = machine__kernel_map(host_machine);
  104. if (map__load(map) < 0)
  105. return NULL;
  106. kmap = map__kmap(map);
  107. if (!kmap)
  108. return NULL;
  109. return kmap->ref_reloc_sym;
  110. }
  111. static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
  112. bool reloc, bool reladdr)
  113. {
  114. struct ref_reloc_sym *reloc_sym;
  115. struct symbol *sym;
  116. struct map *map;
  117. /* ref_reloc_sym is just a label. Need a special fix*/
  118. reloc_sym = kernel_get_ref_reloc_sym();
  119. if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
  120. *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
  121. else {
  122. sym = machine__find_kernel_symbol_by_name(host_machine, name, &map);
  123. if (!sym)
  124. return -ENOENT;
  125. *addr = map->unmap_ip(map, sym->start) -
  126. ((reloc) ? 0 : map->reloc) -
  127. ((reladdr) ? map->start : 0);
  128. }
  129. return 0;
  130. }
  131. static struct map *kernel_get_module_map(const char *module)
  132. {
  133. struct maps *maps = machine__kernel_maps(host_machine);
  134. struct map *pos;
  135. /* A file path -- this is an offline module */
  136. if (module && strchr(module, '/'))
  137. return dso__new_map(module);
  138. if (!module)
  139. module = "kernel";
  140. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  141. /* short_name is "[module]" */
  142. if (strncmp(pos->dso->short_name + 1, module,
  143. pos->dso->short_name_len - 2) == 0 &&
  144. module[pos->dso->short_name_len - 2] == '\0') {
  145. return map__get(pos);
  146. }
  147. }
  148. return NULL;
  149. }
  150. struct map *get_target_map(const char *target, struct nsinfo *nsi, bool user)
  151. {
  152. /* Init maps of given executable or kernel */
  153. if (user) {
  154. struct map *map;
  155. map = dso__new_map(target);
  156. if (map && map->dso)
  157. map->dso->nsinfo = nsinfo__get(nsi);
  158. return map;
  159. } else {
  160. return kernel_get_module_map(target);
  161. }
  162. }
  163. static int convert_exec_to_group(const char *exec, char **result)
  164. {
  165. char *ptr1, *ptr2, *exec_copy;
  166. char buf[64];
  167. int ret;
  168. exec_copy = strdup(exec);
  169. if (!exec_copy)
  170. return -ENOMEM;
  171. ptr1 = basename(exec_copy);
  172. if (!ptr1) {
  173. ret = -EINVAL;
  174. goto out;
  175. }
  176. for (ptr2 = ptr1; *ptr2 != '\0'; ptr2++) {
  177. if (!isalnum(*ptr2) && *ptr2 != '_') {
  178. *ptr2 = '\0';
  179. break;
  180. }
  181. }
  182. ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
  183. if (ret < 0)
  184. goto out;
  185. *result = strdup(buf);
  186. ret = *result ? 0 : -ENOMEM;
  187. out:
  188. free(exec_copy);
  189. return ret;
  190. }
  191. static void clear_perf_probe_point(struct perf_probe_point *pp)
  192. {
  193. free(pp->file);
  194. free(pp->function);
  195. free(pp->lazy_line);
  196. }
  197. static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
  198. {
  199. int i;
  200. for (i = 0; i < ntevs; i++)
  201. clear_probe_trace_event(tevs + i);
  202. }
  203. static bool kprobe_blacklist__listed(unsigned long address);
  204. static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
  205. {
  206. u64 etext_addr = 0;
  207. int ret;
  208. /* Get the address of _etext for checking non-probable text symbol */
  209. ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
  210. false, false);
  211. if (ret == 0 && etext_addr < address)
  212. pr_warning("%s is out of .text, skip it.\n", symbol);
  213. else if (kprobe_blacklist__listed(address))
  214. pr_warning("%s is blacklisted function, skip it.\n", symbol);
  215. else
  216. return false;
  217. return true;
  218. }
  219. /*
  220. * @module can be module name of module file path. In case of path,
  221. * inspect elf and find out what is actual module name.
  222. * Caller has to free mod_name after using it.
  223. */
  224. static char *find_module_name(const char *module)
  225. {
  226. int fd;
  227. Elf *elf;
  228. GElf_Ehdr ehdr;
  229. GElf_Shdr shdr;
  230. Elf_Data *data;
  231. Elf_Scn *sec;
  232. char *mod_name = NULL;
  233. int name_offset;
  234. fd = open(module, O_RDONLY);
  235. if (fd < 0)
  236. return NULL;
  237. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  238. if (elf == NULL)
  239. goto elf_err;
  240. if (gelf_getehdr(elf, &ehdr) == NULL)
  241. goto ret_err;
  242. sec = elf_section_by_name(elf, &ehdr, &shdr,
  243. ".gnu.linkonce.this_module", NULL);
  244. if (!sec)
  245. goto ret_err;
  246. data = elf_getdata(sec, NULL);
  247. if (!data || !data->d_buf)
  248. goto ret_err;
  249. /*
  250. * NOTE:
  251. * '.gnu.linkonce.this_module' section of kernel module elf directly
  252. * maps to 'struct module' from linux/module.h. This section contains
  253. * actual module name which will be used by kernel after loading it.
  254. * But, we cannot use 'struct module' here since linux/module.h is not
  255. * exposed to user-space. Offset of 'name' has remained same from long
  256. * time, so hardcoding it here.
  257. */
  258. if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
  259. name_offset = 12;
  260. else /* expect ELFCLASS64 by default */
  261. name_offset = 24;
  262. mod_name = strdup((char *)data->d_buf + name_offset);
  263. ret_err:
  264. elf_end(elf);
  265. elf_err:
  266. close(fd);
  267. return mod_name;
  268. }
  269. #ifdef HAVE_DWARF_SUPPORT
  270. static int kernel_get_module_dso(const char *module, struct dso **pdso)
  271. {
  272. struct dso *dso;
  273. struct map *map;
  274. const char *vmlinux_name;
  275. int ret = 0;
  276. if (module) {
  277. char module_name[128];
  278. snprintf(module_name, sizeof(module_name), "[%s]", module);
  279. map = map_groups__find_by_name(&host_machine->kmaps, module_name);
  280. if (map) {
  281. dso = map->dso;
  282. goto found;
  283. }
  284. pr_debug("Failed to find module %s.\n", module);
  285. return -ENOENT;
  286. }
  287. map = machine__kernel_map(host_machine);
  288. dso = map->dso;
  289. vmlinux_name = symbol_conf.vmlinux_name;
  290. dso->load_errno = 0;
  291. if (vmlinux_name)
  292. ret = dso__load_vmlinux(dso, map, vmlinux_name, false);
  293. else
  294. ret = dso__load_vmlinux_path(dso, map);
  295. found:
  296. *pdso = dso;
  297. return ret;
  298. }
  299. /*
  300. * Some binaries like glibc have special symbols which are on the symbol
  301. * table, but not in the debuginfo. If we can find the address of the
  302. * symbol from map, we can translate the address back to the probe point.
  303. */
  304. static int find_alternative_probe_point(struct debuginfo *dinfo,
  305. struct perf_probe_point *pp,
  306. struct perf_probe_point *result,
  307. const char *target, struct nsinfo *nsi,
  308. bool uprobes)
  309. {
  310. struct map *map = NULL;
  311. struct symbol *sym;
  312. u64 address = 0;
  313. int ret = -ENOENT;
  314. /* This can work only for function-name based one */
  315. if (!pp->function || pp->file)
  316. return -ENOTSUP;
  317. map = get_target_map(target, nsi, uprobes);
  318. if (!map)
  319. return -EINVAL;
  320. /* Find the address of given function */
  321. map__for_each_symbol_by_name(map, pp->function, sym) {
  322. if (uprobes)
  323. address = sym->start;
  324. else
  325. address = map->unmap_ip(map, sym->start) - map->reloc;
  326. break;
  327. }
  328. if (!address) {
  329. ret = -ENOENT;
  330. goto out;
  331. }
  332. pr_debug("Symbol %s address found : %" PRIx64 "\n",
  333. pp->function, address);
  334. ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
  335. result);
  336. if (ret <= 0)
  337. ret = (!ret) ? -ENOENT : ret;
  338. else {
  339. result->offset += pp->offset;
  340. result->line += pp->line;
  341. result->retprobe = pp->retprobe;
  342. ret = 0;
  343. }
  344. out:
  345. map__put(map);
  346. return ret;
  347. }
  348. static int get_alternative_probe_event(struct debuginfo *dinfo,
  349. struct perf_probe_event *pev,
  350. struct perf_probe_point *tmp)
  351. {
  352. int ret;
  353. memcpy(tmp, &pev->point, sizeof(*tmp));
  354. memset(&pev->point, 0, sizeof(pev->point));
  355. ret = find_alternative_probe_point(dinfo, tmp, &pev->point, pev->target,
  356. pev->nsi, pev->uprobes);
  357. if (ret < 0)
  358. memcpy(&pev->point, tmp, sizeof(*tmp));
  359. return ret;
  360. }
  361. static int get_alternative_line_range(struct debuginfo *dinfo,
  362. struct line_range *lr,
  363. const char *target, bool user)
  364. {
  365. struct perf_probe_point pp = { .function = lr->function,
  366. .file = lr->file,
  367. .line = lr->start };
  368. struct perf_probe_point result;
  369. int ret, len = 0;
  370. memset(&result, 0, sizeof(result));
  371. if (lr->end != INT_MAX)
  372. len = lr->end - lr->start;
  373. ret = find_alternative_probe_point(dinfo, &pp, &result,
  374. target, NULL, user);
  375. if (!ret) {
  376. lr->function = result.function;
  377. lr->file = result.file;
  378. lr->start = result.line;
  379. if (lr->end != INT_MAX)
  380. lr->end = lr->start + len;
  381. clear_perf_probe_point(&pp);
  382. }
  383. return ret;
  384. }
  385. /* Open new debuginfo of given module */
  386. static struct debuginfo *open_debuginfo(const char *module, struct nsinfo *nsi,
  387. bool silent)
  388. {
  389. const char *path = module;
  390. char reason[STRERR_BUFSIZE];
  391. struct debuginfo *ret = NULL;
  392. struct dso *dso = NULL;
  393. struct nscookie nsc;
  394. int err;
  395. if (!module || !strchr(module, '/')) {
  396. err = kernel_get_module_dso(module, &dso);
  397. if (err < 0) {
  398. if (!dso || dso->load_errno == 0) {
  399. if (!str_error_r(-err, reason, STRERR_BUFSIZE))
  400. strcpy(reason, "(unknown)");
  401. } else
  402. dso__strerror_load(dso, reason, STRERR_BUFSIZE);
  403. if (!silent)
  404. pr_err("Failed to find the path for %s: %s\n",
  405. module ?: "kernel", reason);
  406. return NULL;
  407. }
  408. path = dso->long_name;
  409. }
  410. nsinfo__mountns_enter(nsi, &nsc);
  411. ret = debuginfo__new(path);
  412. if (!ret && !silent) {
  413. pr_warning("The %s file has no debug information.\n", path);
  414. if (!module || !strtailcmp(path, ".ko"))
  415. pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
  416. else
  417. pr_warning("Rebuild with -g, ");
  418. pr_warning("or install an appropriate debuginfo package.\n");
  419. }
  420. nsinfo__mountns_exit(&nsc);
  421. return ret;
  422. }
  423. /* For caching the last debuginfo */
  424. static struct debuginfo *debuginfo_cache;
  425. static char *debuginfo_cache_path;
  426. static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
  427. {
  428. const char *path = module;
  429. /* If the module is NULL, it should be the kernel. */
  430. if (!module)
  431. path = "kernel";
  432. if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
  433. goto out;
  434. /* Copy module path */
  435. free(debuginfo_cache_path);
  436. debuginfo_cache_path = strdup(path);
  437. if (!debuginfo_cache_path) {
  438. debuginfo__delete(debuginfo_cache);
  439. debuginfo_cache = NULL;
  440. goto out;
  441. }
  442. debuginfo_cache = open_debuginfo(module, NULL, silent);
  443. if (!debuginfo_cache)
  444. zfree(&debuginfo_cache_path);
  445. out:
  446. return debuginfo_cache;
  447. }
  448. static void debuginfo_cache__exit(void)
  449. {
  450. debuginfo__delete(debuginfo_cache);
  451. debuginfo_cache = NULL;
  452. zfree(&debuginfo_cache_path);
  453. }
  454. static int get_text_start_address(const char *exec, unsigned long *address,
  455. struct nsinfo *nsi)
  456. {
  457. Elf *elf;
  458. GElf_Ehdr ehdr;
  459. GElf_Shdr shdr;
  460. int fd, ret = -ENOENT;
  461. struct nscookie nsc;
  462. nsinfo__mountns_enter(nsi, &nsc);
  463. fd = open(exec, O_RDONLY);
  464. nsinfo__mountns_exit(&nsc);
  465. if (fd < 0)
  466. return -errno;
  467. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  468. if (elf == NULL) {
  469. ret = -EINVAL;
  470. goto out_close;
  471. }
  472. if (gelf_getehdr(elf, &ehdr) == NULL)
  473. goto out;
  474. if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
  475. goto out;
  476. *address = shdr.sh_addr - shdr.sh_offset;
  477. ret = 0;
  478. out:
  479. elf_end(elf);
  480. out_close:
  481. close(fd);
  482. return ret;
  483. }
  484. /*
  485. * Convert trace point to probe point with debuginfo
  486. */
  487. static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
  488. struct perf_probe_point *pp,
  489. bool is_kprobe)
  490. {
  491. struct debuginfo *dinfo = NULL;
  492. unsigned long stext = 0;
  493. u64 addr = tp->address;
  494. int ret = -ENOENT;
  495. /* convert the address to dwarf address */
  496. if (!is_kprobe) {
  497. if (!addr) {
  498. ret = -EINVAL;
  499. goto error;
  500. }
  501. ret = get_text_start_address(tp->module, &stext, NULL);
  502. if (ret < 0)
  503. goto error;
  504. addr += stext;
  505. } else if (tp->symbol) {
  506. /* If the module is given, this returns relative address */
  507. ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
  508. false, !!tp->module);
  509. if (ret != 0)
  510. goto error;
  511. addr += tp->offset;
  512. }
  513. pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
  514. tp->module ? : "kernel");
  515. dinfo = debuginfo_cache__open(tp->module, verbose <= 0);
  516. if (dinfo)
  517. ret = debuginfo__find_probe_point(dinfo,
  518. (unsigned long)addr, pp);
  519. else
  520. ret = -ENOENT;
  521. if (ret > 0) {
  522. pp->retprobe = tp->retprobe;
  523. return 0;
  524. }
  525. error:
  526. pr_debug("Failed to find corresponding probes from debuginfo.\n");
  527. return ret ? : -ENOENT;
  528. }
  529. /* Adjust symbol name and address */
  530. static int post_process_probe_trace_point(struct probe_trace_point *tp,
  531. struct map *map, unsigned long offs)
  532. {
  533. struct symbol *sym;
  534. u64 addr = tp->address - offs;
  535. sym = map__find_symbol(map, addr);
  536. if (!sym)
  537. return -ENOENT;
  538. if (strcmp(sym->name, tp->symbol)) {
  539. /* If we have no realname, use symbol for it */
  540. if (!tp->realname)
  541. tp->realname = tp->symbol;
  542. else
  543. free(tp->symbol);
  544. tp->symbol = strdup(sym->name);
  545. if (!tp->symbol)
  546. return -ENOMEM;
  547. }
  548. tp->offset = addr - sym->start;
  549. tp->address -= offs;
  550. return 0;
  551. }
  552. /*
  553. * Rename DWARF symbols to ELF symbols -- gcc sometimes optimizes functions
  554. * and generate new symbols with suffixes such as .constprop.N or .isra.N
  555. * etc. Since those symbols are not recorded in DWARF, we have to find
  556. * correct generated symbols from offline ELF binary.
  557. * For online kernel or uprobes we don't need this because those are
  558. * rebased on _text, or already a section relative address.
  559. */
  560. static int
  561. post_process_offline_probe_trace_events(struct probe_trace_event *tevs,
  562. int ntevs, const char *pathname)
  563. {
  564. struct map *map;
  565. unsigned long stext = 0;
  566. int i, ret = 0;
  567. /* Prepare a map for offline binary */
  568. map = dso__new_map(pathname);
  569. if (!map || get_text_start_address(pathname, &stext, NULL) < 0) {
  570. pr_warning("Failed to get ELF symbols for %s\n", pathname);
  571. return -EINVAL;
  572. }
  573. for (i = 0; i < ntevs; i++) {
  574. ret = post_process_probe_trace_point(&tevs[i].point,
  575. map, stext);
  576. if (ret < 0)
  577. break;
  578. }
  579. map__put(map);
  580. return ret;
  581. }
  582. static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
  583. int ntevs, const char *exec,
  584. struct nsinfo *nsi)
  585. {
  586. int i, ret = 0;
  587. unsigned long stext = 0;
  588. if (!exec)
  589. return 0;
  590. ret = get_text_start_address(exec, &stext, nsi);
  591. if (ret < 0)
  592. return ret;
  593. for (i = 0; i < ntevs && ret >= 0; i++) {
  594. /* point.address is the addres of point.symbol + point.offset */
  595. tevs[i].point.address -= stext;
  596. tevs[i].point.module = strdup(exec);
  597. if (!tevs[i].point.module) {
  598. ret = -ENOMEM;
  599. break;
  600. }
  601. tevs[i].uprobes = true;
  602. }
  603. return ret;
  604. }
  605. static int
  606. post_process_module_probe_trace_events(struct probe_trace_event *tevs,
  607. int ntevs, const char *module,
  608. struct debuginfo *dinfo)
  609. {
  610. Dwarf_Addr text_offs = 0;
  611. int i, ret = 0;
  612. char *mod_name = NULL;
  613. struct map *map;
  614. if (!module)
  615. return 0;
  616. map = get_target_map(module, NULL, false);
  617. if (!map || debuginfo__get_text_offset(dinfo, &text_offs, true) < 0) {
  618. pr_warning("Failed to get ELF symbols for %s\n", module);
  619. return -EINVAL;
  620. }
  621. mod_name = find_module_name(module);
  622. for (i = 0; i < ntevs; i++) {
  623. ret = post_process_probe_trace_point(&tevs[i].point,
  624. map, (unsigned long)text_offs);
  625. if (ret < 0)
  626. break;
  627. tevs[i].point.module =
  628. strdup(mod_name ? mod_name : module);
  629. if (!tevs[i].point.module) {
  630. ret = -ENOMEM;
  631. break;
  632. }
  633. }
  634. free(mod_name);
  635. map__put(map);
  636. return ret;
  637. }
  638. static int
  639. post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
  640. int ntevs)
  641. {
  642. struct ref_reloc_sym *reloc_sym;
  643. char *tmp;
  644. int i, skipped = 0;
  645. /* Skip post process if the target is an offline kernel */
  646. if (symbol_conf.ignore_vmlinux_buildid)
  647. return post_process_offline_probe_trace_events(tevs, ntevs,
  648. symbol_conf.vmlinux_name);
  649. reloc_sym = kernel_get_ref_reloc_sym();
  650. if (!reloc_sym) {
  651. pr_warning("Relocated base symbol is not found!\n");
  652. return -EINVAL;
  653. }
  654. for (i = 0; i < ntevs; i++) {
  655. if (!tevs[i].point.address)
  656. continue;
  657. if (tevs[i].point.retprobe && !kretprobe_offset_is_supported())
  658. continue;
  659. /* If we found a wrong one, mark it by NULL symbol */
  660. if (kprobe_warn_out_range(tevs[i].point.symbol,
  661. tevs[i].point.address)) {
  662. tmp = NULL;
  663. skipped++;
  664. } else {
  665. tmp = strdup(reloc_sym->name);
  666. if (!tmp)
  667. return -ENOMEM;
  668. }
  669. /* If we have no realname, use symbol for it */
  670. if (!tevs[i].point.realname)
  671. tevs[i].point.realname = tevs[i].point.symbol;
  672. else
  673. free(tevs[i].point.symbol);
  674. tevs[i].point.symbol = tmp;
  675. tevs[i].point.offset = tevs[i].point.address -
  676. reloc_sym->unrelocated_addr;
  677. }
  678. return skipped;
  679. }
  680. void __weak
  681. arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
  682. int ntevs __maybe_unused)
  683. {
  684. }
  685. /* Post processing the probe events */
  686. static int post_process_probe_trace_events(struct perf_probe_event *pev,
  687. struct probe_trace_event *tevs,
  688. int ntevs, const char *module,
  689. bool uprobe, struct debuginfo *dinfo)
  690. {
  691. int ret;
  692. if (uprobe)
  693. ret = add_exec_to_probe_trace_events(tevs, ntevs, module,
  694. pev->nsi);
  695. else if (module)
  696. /* Currently ref_reloc_sym based probe is not for drivers */
  697. ret = post_process_module_probe_trace_events(tevs, ntevs,
  698. module, dinfo);
  699. else
  700. ret = post_process_kernel_probe_trace_events(tevs, ntevs);
  701. if (ret >= 0)
  702. arch__post_process_probe_trace_events(pev, ntevs);
  703. return ret;
  704. }
  705. /* Try to find perf_probe_event with debuginfo */
  706. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  707. struct probe_trace_event **tevs)
  708. {
  709. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  710. struct perf_probe_point tmp;
  711. struct debuginfo *dinfo;
  712. int ntevs, ret = 0;
  713. dinfo = open_debuginfo(pev->target, pev->nsi, !need_dwarf);
  714. if (!dinfo) {
  715. if (need_dwarf)
  716. return -ENOENT;
  717. pr_debug("Could not open debuginfo. Try to use symbols.\n");
  718. return 0;
  719. }
  720. pr_debug("Try to find probe point from debuginfo.\n");
  721. /* Searching trace events corresponding to a probe event */
  722. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  723. if (ntevs == 0) { /* Not found, retry with an alternative */
  724. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  725. if (!ret) {
  726. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  727. /*
  728. * Write back to the original probe_event for
  729. * setting appropriate (user given) event name
  730. */
  731. clear_perf_probe_point(&pev->point);
  732. memcpy(&pev->point, &tmp, sizeof(tmp));
  733. }
  734. }
  735. if (ntevs > 0) { /* Succeeded to find trace events */
  736. pr_debug("Found %d probe_trace_events.\n", ntevs);
  737. ret = post_process_probe_trace_events(pev, *tevs, ntevs,
  738. pev->target, pev->uprobes, dinfo);
  739. if (ret < 0 || ret == ntevs) {
  740. pr_debug("Post processing failed or all events are skipped. (%d)\n", ret);
  741. clear_probe_trace_events(*tevs, ntevs);
  742. zfree(tevs);
  743. ntevs = 0;
  744. }
  745. }
  746. debuginfo__delete(dinfo);
  747. if (ntevs == 0) { /* No error but failed to find probe point. */
  748. pr_warning("Probe point '%s' not found.\n",
  749. synthesize_perf_probe_point(&pev->point));
  750. return -ENOENT;
  751. } else if (ntevs < 0) {
  752. /* Error path : ntevs < 0 */
  753. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  754. if (ntevs == -EBADF)
  755. pr_warning("Warning: No dwarf info found in the vmlinux - "
  756. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  757. if (!need_dwarf) {
  758. pr_debug("Trying to use symbols.\n");
  759. return 0;
  760. }
  761. }
  762. return ntevs;
  763. }
  764. #define LINEBUF_SIZE 256
  765. #define NR_ADDITIONAL_LINES 2
  766. static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
  767. {
  768. char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
  769. const char *color = show_num ? "" : PERF_COLOR_BLUE;
  770. const char *prefix = NULL;
  771. do {
  772. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  773. goto error;
  774. if (skip)
  775. continue;
  776. if (!prefix) {
  777. prefix = show_num ? "%7d " : " ";
  778. color_fprintf(stdout, color, prefix, l);
  779. }
  780. color_fprintf(stdout, color, "%s", buf);
  781. } while (strchr(buf, '\n') == NULL);
  782. return 1;
  783. error:
  784. if (ferror(fp)) {
  785. pr_warning("File read error: %s\n",
  786. str_error_r(errno, sbuf, sizeof(sbuf)));
  787. return -1;
  788. }
  789. return 0;
  790. }
  791. static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
  792. {
  793. int rv = __show_one_line(fp, l, skip, show_num);
  794. if (rv == 0) {
  795. pr_warning("Source file is shorter than expected.\n");
  796. rv = -1;
  797. }
  798. return rv;
  799. }
  800. #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
  801. #define show_one_line(f,l) _show_one_line(f,l,false,false)
  802. #define skip_one_line(f,l) _show_one_line(f,l,true,false)
  803. #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
  804. /*
  805. * Show line-range always requires debuginfo to find source file and
  806. * line number.
  807. */
  808. static int __show_line_range(struct line_range *lr, const char *module,
  809. bool user)
  810. {
  811. int l = 1;
  812. struct int_node *ln;
  813. struct debuginfo *dinfo;
  814. FILE *fp;
  815. int ret;
  816. char *tmp;
  817. char sbuf[STRERR_BUFSIZE];
  818. /* Search a line range */
  819. dinfo = open_debuginfo(module, NULL, false);
  820. if (!dinfo)
  821. return -ENOENT;
  822. ret = debuginfo__find_line_range(dinfo, lr);
  823. if (!ret) { /* Not found, retry with an alternative */
  824. ret = get_alternative_line_range(dinfo, lr, module, user);
  825. if (!ret)
  826. ret = debuginfo__find_line_range(dinfo, lr);
  827. }
  828. debuginfo__delete(dinfo);
  829. if (ret == 0 || ret == -ENOENT) {
  830. pr_warning("Specified source line is not found.\n");
  831. return -ENOENT;
  832. } else if (ret < 0) {
  833. pr_warning("Debuginfo analysis failed.\n");
  834. return ret;
  835. }
  836. /* Convert source file path */
  837. tmp = lr->path;
  838. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  839. /* Free old path when new path is assigned */
  840. if (tmp != lr->path)
  841. free(tmp);
  842. if (ret < 0) {
  843. pr_warning("Failed to find source file path.\n");
  844. return ret;
  845. }
  846. setup_pager();
  847. if (lr->function)
  848. fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
  849. lr->start - lr->offset);
  850. else
  851. fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
  852. fp = fopen(lr->path, "r");
  853. if (fp == NULL) {
  854. pr_warning("Failed to open %s: %s\n", lr->path,
  855. str_error_r(errno, sbuf, sizeof(sbuf)));
  856. return -errno;
  857. }
  858. /* Skip to starting line number */
  859. while (l < lr->start) {
  860. ret = skip_one_line(fp, l++);
  861. if (ret < 0)
  862. goto end;
  863. }
  864. intlist__for_each_entry(ln, lr->line_list) {
  865. for (; ln->i > l; l++) {
  866. ret = show_one_line(fp, l - lr->offset);
  867. if (ret < 0)
  868. goto end;
  869. }
  870. ret = show_one_line_with_num(fp, l++ - lr->offset);
  871. if (ret < 0)
  872. goto end;
  873. }
  874. if (lr->end == INT_MAX)
  875. lr->end = l + NR_ADDITIONAL_LINES;
  876. while (l <= lr->end) {
  877. ret = show_one_line_or_eof(fp, l++ - lr->offset);
  878. if (ret <= 0)
  879. break;
  880. }
  881. end:
  882. fclose(fp);
  883. return ret;
  884. }
  885. int show_line_range(struct line_range *lr, const char *module,
  886. struct nsinfo *nsi, bool user)
  887. {
  888. int ret;
  889. struct nscookie nsc;
  890. ret = init_probe_symbol_maps(user);
  891. if (ret < 0)
  892. return ret;
  893. nsinfo__mountns_enter(nsi, &nsc);
  894. ret = __show_line_range(lr, module, user);
  895. nsinfo__mountns_exit(&nsc);
  896. exit_probe_symbol_maps();
  897. return ret;
  898. }
  899. static int show_available_vars_at(struct debuginfo *dinfo,
  900. struct perf_probe_event *pev,
  901. struct strfilter *_filter)
  902. {
  903. char *buf;
  904. int ret, i, nvars;
  905. struct str_node *node;
  906. struct variable_list *vls = NULL, *vl;
  907. struct perf_probe_point tmp;
  908. const char *var;
  909. buf = synthesize_perf_probe_point(&pev->point);
  910. if (!buf)
  911. return -EINVAL;
  912. pr_debug("Searching variables at %s\n", buf);
  913. ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
  914. if (!ret) { /* Not found, retry with an alternative */
  915. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  916. if (!ret) {
  917. ret = debuginfo__find_available_vars_at(dinfo, pev,
  918. &vls);
  919. /* Release the old probe_point */
  920. clear_perf_probe_point(&tmp);
  921. }
  922. }
  923. if (ret <= 0) {
  924. if (ret == 0 || ret == -ENOENT) {
  925. pr_err("Failed to find the address of %s\n", buf);
  926. ret = -ENOENT;
  927. } else
  928. pr_warning("Debuginfo analysis failed.\n");
  929. goto end;
  930. }
  931. /* Some variables are found */
  932. fprintf(stdout, "Available variables at %s\n", buf);
  933. for (i = 0; i < ret; i++) {
  934. vl = &vls[i];
  935. /*
  936. * A probe point might be converted to
  937. * several trace points.
  938. */
  939. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  940. vl->point.offset);
  941. zfree(&vl->point.symbol);
  942. nvars = 0;
  943. if (vl->vars) {
  944. strlist__for_each_entry(node, vl->vars) {
  945. var = strchr(node->s, '\t') + 1;
  946. if (strfilter__compare(_filter, var)) {
  947. fprintf(stdout, "\t\t%s\n", node->s);
  948. nvars++;
  949. }
  950. }
  951. strlist__delete(vl->vars);
  952. }
  953. if (nvars == 0)
  954. fprintf(stdout, "\t\t(No matched variables)\n");
  955. }
  956. free(vls);
  957. end:
  958. free(buf);
  959. return ret;
  960. }
  961. /* Show available variables on given probe point */
  962. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  963. struct strfilter *_filter)
  964. {
  965. int i, ret = 0;
  966. struct debuginfo *dinfo;
  967. ret = init_probe_symbol_maps(pevs->uprobes);
  968. if (ret < 0)
  969. return ret;
  970. dinfo = open_debuginfo(pevs->target, pevs->nsi, false);
  971. if (!dinfo) {
  972. ret = -ENOENT;
  973. goto out;
  974. }
  975. setup_pager();
  976. for (i = 0; i < npevs && ret >= 0; i++)
  977. ret = show_available_vars_at(dinfo, &pevs[i], _filter);
  978. debuginfo__delete(dinfo);
  979. out:
  980. exit_probe_symbol_maps();
  981. return ret;
  982. }
  983. #else /* !HAVE_DWARF_SUPPORT */
  984. static void debuginfo_cache__exit(void)
  985. {
  986. }
  987. static int
  988. find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
  989. struct perf_probe_point *pp __maybe_unused,
  990. bool is_kprobe __maybe_unused)
  991. {
  992. return -ENOSYS;
  993. }
  994. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  995. struct probe_trace_event **tevs __maybe_unused)
  996. {
  997. if (perf_probe_event_need_dwarf(pev)) {
  998. pr_warning("Debuginfo-analysis is not supported.\n");
  999. return -ENOSYS;
  1000. }
  1001. return 0;
  1002. }
  1003. int show_line_range(struct line_range *lr __maybe_unused,
  1004. const char *module __maybe_unused,
  1005. struct nsinfo *nsi __maybe_unused,
  1006. bool user __maybe_unused)
  1007. {
  1008. pr_warning("Debuginfo-analysis is not supported.\n");
  1009. return -ENOSYS;
  1010. }
  1011. int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
  1012. int npevs __maybe_unused,
  1013. struct strfilter *filter __maybe_unused)
  1014. {
  1015. pr_warning("Debuginfo-analysis is not supported.\n");
  1016. return -ENOSYS;
  1017. }
  1018. #endif
  1019. void line_range__clear(struct line_range *lr)
  1020. {
  1021. free(lr->function);
  1022. free(lr->file);
  1023. free(lr->path);
  1024. free(lr->comp_dir);
  1025. intlist__delete(lr->line_list);
  1026. memset(lr, 0, sizeof(*lr));
  1027. }
  1028. int line_range__init(struct line_range *lr)
  1029. {
  1030. memset(lr, 0, sizeof(*lr));
  1031. lr->line_list = intlist__new(NULL);
  1032. if (!lr->line_list)
  1033. return -ENOMEM;
  1034. else
  1035. return 0;
  1036. }
  1037. static int parse_line_num(char **ptr, int *val, const char *what)
  1038. {
  1039. const char *start = *ptr;
  1040. errno = 0;
  1041. *val = strtol(*ptr, ptr, 0);
  1042. if (errno || *ptr == start) {
  1043. semantic_error("'%s' is not a valid number.\n", what);
  1044. return -EINVAL;
  1045. }
  1046. return 0;
  1047. }
  1048. /* Check the name is good for event, group or function */
  1049. static bool is_c_func_name(const char *name)
  1050. {
  1051. if (!isalpha(*name) && *name != '_')
  1052. return false;
  1053. while (*++name != '\0') {
  1054. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  1055. return false;
  1056. }
  1057. return true;
  1058. }
  1059. /*
  1060. * Stuff 'lr' according to the line range described by 'arg'.
  1061. * The line range syntax is described by:
  1062. *
  1063. * SRC[:SLN[+NUM|-ELN]]
  1064. * FNC[@SRC][:SLN[+NUM|-ELN]]
  1065. */
  1066. int parse_line_range_desc(const char *arg, struct line_range *lr)
  1067. {
  1068. char *range, *file, *name = strdup(arg);
  1069. int err;
  1070. if (!name)
  1071. return -ENOMEM;
  1072. lr->start = 0;
  1073. lr->end = INT_MAX;
  1074. range = strchr(name, ':');
  1075. if (range) {
  1076. *range++ = '\0';
  1077. err = parse_line_num(&range, &lr->start, "start line");
  1078. if (err)
  1079. goto err;
  1080. if (*range == '+' || *range == '-') {
  1081. const char c = *range++;
  1082. err = parse_line_num(&range, &lr->end, "end line");
  1083. if (err)
  1084. goto err;
  1085. if (c == '+') {
  1086. lr->end += lr->start;
  1087. /*
  1088. * Adjust the number of lines here.
  1089. * If the number of lines == 1, the
  1090. * the end of line should be equal to
  1091. * the start of line.
  1092. */
  1093. lr->end--;
  1094. }
  1095. }
  1096. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  1097. err = -EINVAL;
  1098. if (lr->start > lr->end) {
  1099. semantic_error("Start line must be smaller"
  1100. " than end line.\n");
  1101. goto err;
  1102. }
  1103. if (*range != '\0') {
  1104. semantic_error("Tailing with invalid str '%s'.\n", range);
  1105. goto err;
  1106. }
  1107. }
  1108. file = strchr(name, '@');
  1109. if (file) {
  1110. *file = '\0';
  1111. lr->file = strdup(++file);
  1112. if (lr->file == NULL) {
  1113. err = -ENOMEM;
  1114. goto err;
  1115. }
  1116. lr->function = name;
  1117. } else if (strchr(name, '/') || strchr(name, '.'))
  1118. lr->file = name;
  1119. else if (is_c_func_name(name))/* We reuse it for checking funcname */
  1120. lr->function = name;
  1121. else { /* Invalid name */
  1122. semantic_error("'%s' is not a valid function name.\n", name);
  1123. err = -EINVAL;
  1124. goto err;
  1125. }
  1126. return 0;
  1127. err:
  1128. free(name);
  1129. return err;
  1130. }
  1131. static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
  1132. {
  1133. char *ptr;
  1134. ptr = strpbrk_esc(*arg, ":");
  1135. if (ptr) {
  1136. *ptr = '\0';
  1137. if (!pev->sdt && !is_c_func_name(*arg))
  1138. goto ng_name;
  1139. pev->group = strdup_esc(*arg);
  1140. if (!pev->group)
  1141. return -ENOMEM;
  1142. *arg = ptr + 1;
  1143. } else
  1144. pev->group = NULL;
  1145. pev->event = strdup_esc(*arg);
  1146. if (pev->event == NULL)
  1147. return -ENOMEM;
  1148. if (!pev->sdt && !is_c_func_name(pev->event)) {
  1149. zfree(&pev->event);
  1150. ng_name:
  1151. zfree(&pev->group);
  1152. semantic_error("%s is bad for event name -it must "
  1153. "follow C symbol-naming rule.\n", *arg);
  1154. return -EINVAL;
  1155. }
  1156. return 0;
  1157. }
  1158. /* Parse probepoint definition. */
  1159. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  1160. {
  1161. struct perf_probe_point *pp = &pev->point;
  1162. char *ptr, *tmp;
  1163. char c, nc = 0;
  1164. bool file_spec = false;
  1165. int ret;
  1166. /*
  1167. * <Syntax>
  1168. * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
  1169. * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  1170. * perf probe %[GRP:]SDT_EVENT
  1171. */
  1172. if (!arg)
  1173. return -EINVAL;
  1174. if (is_sdt_event(arg)) {
  1175. pev->sdt = true;
  1176. if (arg[0] == '%')
  1177. arg++;
  1178. }
  1179. ptr = strpbrk_esc(arg, ";=@+%");
  1180. if (pev->sdt) {
  1181. if (ptr) {
  1182. if (*ptr != '@') {
  1183. semantic_error("%s must be an SDT name.\n",
  1184. arg);
  1185. return -EINVAL;
  1186. }
  1187. /* This must be a target file name or build id */
  1188. tmp = build_id_cache__complement(ptr + 1);
  1189. if (tmp) {
  1190. pev->target = build_id_cache__origname(tmp);
  1191. free(tmp);
  1192. } else
  1193. pev->target = strdup_esc(ptr + 1);
  1194. if (!pev->target)
  1195. return -ENOMEM;
  1196. *ptr = '\0';
  1197. }
  1198. ret = parse_perf_probe_event_name(&arg, pev);
  1199. if (ret == 0) {
  1200. if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
  1201. ret = -errno;
  1202. }
  1203. return ret;
  1204. }
  1205. if (ptr && *ptr == '=') { /* Event name */
  1206. *ptr = '\0';
  1207. tmp = ptr + 1;
  1208. ret = parse_perf_probe_event_name(&arg, pev);
  1209. if (ret < 0)
  1210. return ret;
  1211. arg = tmp;
  1212. }
  1213. /*
  1214. * Check arg is function or file name and copy it.
  1215. *
  1216. * We consider arg to be a file spec if and only if it satisfies
  1217. * all of the below criteria::
  1218. * - it does not include any of "+@%",
  1219. * - it includes one of ":;", and
  1220. * - it has a period '.' in the name.
  1221. *
  1222. * Otherwise, we consider arg to be a function specification.
  1223. */
  1224. if (!strpbrk_esc(arg, "+@%")) {
  1225. ptr = strpbrk_esc(arg, ";:");
  1226. /* This is a file spec if it includes a '.' before ; or : */
  1227. if (ptr && memchr(arg, '.', ptr - arg))
  1228. file_spec = true;
  1229. }
  1230. ptr = strpbrk_esc(arg, ";:+@%");
  1231. if (ptr) {
  1232. nc = *ptr;
  1233. *ptr++ = '\0';
  1234. }
  1235. if (arg[0] == '\0')
  1236. tmp = NULL;
  1237. else {
  1238. tmp = strdup_esc(arg);
  1239. if (tmp == NULL)
  1240. return -ENOMEM;
  1241. }
  1242. if (file_spec)
  1243. pp->file = tmp;
  1244. else {
  1245. pp->function = tmp;
  1246. /*
  1247. * Keep pp->function even if this is absolute address,
  1248. * so it can mark whether abs_address is valid.
  1249. * Which make 'perf probe lib.bin 0x0' possible.
  1250. *
  1251. * Note that checking length of tmp is not needed
  1252. * because when we access tmp[1] we know tmp[0] is '0',
  1253. * so tmp[1] should always valid (but could be '\0').
  1254. */
  1255. if (tmp && !strncmp(tmp, "0x", 2)) {
  1256. pp->abs_address = strtoul(pp->function, &tmp, 0);
  1257. if (*tmp != '\0') {
  1258. semantic_error("Invalid absolute address.\n");
  1259. return -EINVAL;
  1260. }
  1261. }
  1262. }
  1263. /* Parse other options */
  1264. while (ptr) {
  1265. arg = ptr;
  1266. c = nc;
  1267. if (c == ';') { /* Lazy pattern must be the last part */
  1268. pp->lazy_line = strdup(arg); /* let leave escapes */
  1269. if (pp->lazy_line == NULL)
  1270. return -ENOMEM;
  1271. break;
  1272. }
  1273. ptr = strpbrk_esc(arg, ";:+@%");
  1274. if (ptr) {
  1275. nc = *ptr;
  1276. *ptr++ = '\0';
  1277. }
  1278. switch (c) {
  1279. case ':': /* Line number */
  1280. pp->line = strtoul(arg, &tmp, 0);
  1281. if (*tmp != '\0') {
  1282. semantic_error("There is non-digit char"
  1283. " in line number.\n");
  1284. return -EINVAL;
  1285. }
  1286. break;
  1287. case '+': /* Byte offset from a symbol */
  1288. pp->offset = strtoul(arg, &tmp, 0);
  1289. if (*tmp != '\0') {
  1290. semantic_error("There is non-digit character"
  1291. " in offset.\n");
  1292. return -EINVAL;
  1293. }
  1294. break;
  1295. case '@': /* File name */
  1296. if (pp->file) {
  1297. semantic_error("SRC@SRC is not allowed.\n");
  1298. return -EINVAL;
  1299. }
  1300. pp->file = strdup_esc(arg);
  1301. if (pp->file == NULL)
  1302. return -ENOMEM;
  1303. break;
  1304. case '%': /* Probe places */
  1305. if (strcmp(arg, "return") == 0) {
  1306. pp->retprobe = 1;
  1307. } else { /* Others not supported yet */
  1308. semantic_error("%%%s is not supported.\n", arg);
  1309. return -ENOTSUP;
  1310. }
  1311. break;
  1312. default: /* Buggy case */
  1313. pr_err("This program has a bug at %s:%d.\n",
  1314. __FILE__, __LINE__);
  1315. return -ENOTSUP;
  1316. break;
  1317. }
  1318. }
  1319. /* Exclusion check */
  1320. if (pp->lazy_line && pp->line) {
  1321. semantic_error("Lazy pattern can't be used with"
  1322. " line number.\n");
  1323. return -EINVAL;
  1324. }
  1325. if (pp->lazy_line && pp->offset) {
  1326. semantic_error("Lazy pattern can't be used with offset.\n");
  1327. return -EINVAL;
  1328. }
  1329. if (pp->line && pp->offset) {
  1330. semantic_error("Offset can't be used with line number.\n");
  1331. return -EINVAL;
  1332. }
  1333. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  1334. semantic_error("File always requires line number or "
  1335. "lazy pattern.\n");
  1336. return -EINVAL;
  1337. }
  1338. if (pp->offset && !pp->function) {
  1339. semantic_error("Offset requires an entry function.\n");
  1340. return -EINVAL;
  1341. }
  1342. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  1343. semantic_error("Offset/Line/Lazy pattern can't be used with "
  1344. "return probe.\n");
  1345. return -EINVAL;
  1346. }
  1347. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  1348. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  1349. pp->lazy_line);
  1350. return 0;
  1351. }
  1352. /* Parse perf-probe event argument */
  1353. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  1354. {
  1355. char *tmp, *goodname;
  1356. struct perf_probe_arg_field **fieldp;
  1357. pr_debug("parsing arg: %s into ", str);
  1358. tmp = strchr(str, '=');
  1359. if (tmp) {
  1360. arg->name = strndup(str, tmp - str);
  1361. if (arg->name == NULL)
  1362. return -ENOMEM;
  1363. pr_debug("name:%s ", arg->name);
  1364. str = tmp + 1;
  1365. }
  1366. tmp = strchr(str, ':');
  1367. if (tmp) { /* Type setting */
  1368. *tmp = '\0';
  1369. arg->type = strdup(tmp + 1);
  1370. if (arg->type == NULL)
  1371. return -ENOMEM;
  1372. pr_debug("type:%s ", arg->type);
  1373. }
  1374. tmp = strpbrk(str, "-.[");
  1375. if (!is_c_varname(str) || !tmp) {
  1376. /* A variable, register, symbol or special value */
  1377. arg->var = strdup(str);
  1378. if (arg->var == NULL)
  1379. return -ENOMEM;
  1380. pr_debug("%s\n", arg->var);
  1381. return 0;
  1382. }
  1383. /* Structure fields or array element */
  1384. arg->var = strndup(str, tmp - str);
  1385. if (arg->var == NULL)
  1386. return -ENOMEM;
  1387. goodname = arg->var;
  1388. pr_debug("%s, ", arg->var);
  1389. fieldp = &arg->field;
  1390. do {
  1391. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  1392. if (*fieldp == NULL)
  1393. return -ENOMEM;
  1394. if (*tmp == '[') { /* Array */
  1395. str = tmp;
  1396. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  1397. (*fieldp)->ref = true;
  1398. if (*tmp != ']' || tmp == str + 1) {
  1399. semantic_error("Array index must be a"
  1400. " number.\n");
  1401. return -EINVAL;
  1402. }
  1403. tmp++;
  1404. if (*tmp == '\0')
  1405. tmp = NULL;
  1406. } else { /* Structure */
  1407. if (*tmp == '.') {
  1408. str = tmp + 1;
  1409. (*fieldp)->ref = false;
  1410. } else if (tmp[1] == '>') {
  1411. str = tmp + 2;
  1412. (*fieldp)->ref = true;
  1413. } else {
  1414. semantic_error("Argument parse error: %s\n",
  1415. str);
  1416. return -EINVAL;
  1417. }
  1418. tmp = strpbrk(str, "-.[");
  1419. }
  1420. if (tmp) {
  1421. (*fieldp)->name = strndup(str, tmp - str);
  1422. if ((*fieldp)->name == NULL)
  1423. return -ENOMEM;
  1424. if (*str != '[')
  1425. goodname = (*fieldp)->name;
  1426. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  1427. fieldp = &(*fieldp)->next;
  1428. }
  1429. } while (tmp);
  1430. (*fieldp)->name = strdup(str);
  1431. if ((*fieldp)->name == NULL)
  1432. return -ENOMEM;
  1433. if (*str != '[')
  1434. goodname = (*fieldp)->name;
  1435. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  1436. /* If no name is specified, set the last field name (not array index)*/
  1437. if (!arg->name) {
  1438. arg->name = strdup(goodname);
  1439. if (arg->name == NULL)
  1440. return -ENOMEM;
  1441. }
  1442. return 0;
  1443. }
  1444. /* Parse perf-probe event command */
  1445. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  1446. {
  1447. char **argv;
  1448. int argc, i, ret = 0;
  1449. argv = argv_split(cmd, &argc);
  1450. if (!argv) {
  1451. pr_debug("Failed to split arguments.\n");
  1452. return -ENOMEM;
  1453. }
  1454. if (argc - 1 > MAX_PROBE_ARGS) {
  1455. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  1456. ret = -ERANGE;
  1457. goto out;
  1458. }
  1459. /* Parse probe point */
  1460. ret = parse_perf_probe_point(argv[0], pev);
  1461. if (ret < 0)
  1462. goto out;
  1463. /* Copy arguments and ensure return probe has no C argument */
  1464. pev->nargs = argc - 1;
  1465. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1466. if (pev->args == NULL) {
  1467. ret = -ENOMEM;
  1468. goto out;
  1469. }
  1470. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  1471. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  1472. if (ret >= 0 &&
  1473. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  1474. semantic_error("You can't specify local variable for"
  1475. " kretprobe.\n");
  1476. ret = -EINVAL;
  1477. }
  1478. }
  1479. out:
  1480. argv_free(argv);
  1481. return ret;
  1482. }
  1483. /* Returns true if *any* ARG is either C variable, $params or $vars. */
  1484. bool perf_probe_with_var(struct perf_probe_event *pev)
  1485. {
  1486. int i = 0;
  1487. for (i = 0; i < pev->nargs; i++)
  1488. if (is_c_varname(pev->args[i].var) ||
  1489. !strcmp(pev->args[i].var, PROBE_ARG_PARAMS) ||
  1490. !strcmp(pev->args[i].var, PROBE_ARG_VARS))
  1491. return true;
  1492. return false;
  1493. }
  1494. /* Return true if this perf_probe_event requires debuginfo */
  1495. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  1496. {
  1497. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  1498. return true;
  1499. if (perf_probe_with_var(pev))
  1500. return true;
  1501. return false;
  1502. }
  1503. /* Parse probe_events event into struct probe_point */
  1504. int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
  1505. {
  1506. struct probe_trace_point *tp = &tev->point;
  1507. char pr;
  1508. char *p;
  1509. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  1510. int ret, i, argc;
  1511. char **argv;
  1512. pr_debug("Parsing probe_events: %s\n", cmd);
  1513. argv = argv_split(cmd, &argc);
  1514. if (!argv) {
  1515. pr_debug("Failed to split arguments.\n");
  1516. return -ENOMEM;
  1517. }
  1518. if (argc < 2) {
  1519. semantic_error("Too few probe arguments.\n");
  1520. ret = -ERANGE;
  1521. goto out;
  1522. }
  1523. /* Scan event and group name. */
  1524. argv0_str = strdup(argv[0]);
  1525. if (argv0_str == NULL) {
  1526. ret = -ENOMEM;
  1527. goto out;
  1528. }
  1529. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  1530. fmt2_str = strtok_r(NULL, "/", &fmt);
  1531. fmt3_str = strtok_r(NULL, " \t", &fmt);
  1532. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  1533. || fmt3_str == NULL) {
  1534. semantic_error("Failed to parse event name: %s\n", argv[0]);
  1535. ret = -EINVAL;
  1536. goto out;
  1537. }
  1538. pr = fmt1_str[0];
  1539. tev->group = strdup(fmt2_str);
  1540. tev->event = strdup(fmt3_str);
  1541. if (tev->group == NULL || tev->event == NULL) {
  1542. ret = -ENOMEM;
  1543. goto out;
  1544. }
  1545. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1546. tp->retprobe = (pr == 'r');
  1547. /* Scan module name(if there), function name and offset */
  1548. p = strchr(argv[1], ':');
  1549. if (p) {
  1550. tp->module = strndup(argv[1], p - argv[1]);
  1551. if (!tp->module) {
  1552. ret = -ENOMEM;
  1553. goto out;
  1554. }
  1555. tev->uprobes = (tp->module[0] == '/');
  1556. p++;
  1557. } else
  1558. p = argv[1];
  1559. fmt1_str = strtok_r(p, "+", &fmt);
  1560. /* only the address started with 0x */
  1561. if (fmt1_str[0] == '0') {
  1562. /*
  1563. * Fix a special case:
  1564. * if address == 0, kernel reports something like:
  1565. * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
  1566. * Newer kernel may fix that, but we want to
  1567. * support old kernel also.
  1568. */
  1569. if (strcmp(fmt1_str, "0x") == 0) {
  1570. if (!argv[2] || strcmp(argv[2], "(null)")) {
  1571. ret = -EINVAL;
  1572. goto out;
  1573. }
  1574. tp->address = 0;
  1575. free(argv[2]);
  1576. for (i = 2; argv[i + 1] != NULL; i++)
  1577. argv[i] = argv[i + 1];
  1578. argv[i] = NULL;
  1579. argc -= 1;
  1580. } else
  1581. tp->address = strtoul(fmt1_str, NULL, 0);
  1582. } else {
  1583. /* Only the symbol-based probe has offset */
  1584. tp->symbol = strdup(fmt1_str);
  1585. if (tp->symbol == NULL) {
  1586. ret = -ENOMEM;
  1587. goto out;
  1588. }
  1589. fmt2_str = strtok_r(NULL, "", &fmt);
  1590. if (fmt2_str == NULL)
  1591. tp->offset = 0;
  1592. else
  1593. tp->offset = strtoul(fmt2_str, NULL, 10);
  1594. }
  1595. tev->nargs = argc - 2;
  1596. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1597. if (tev->args == NULL) {
  1598. ret = -ENOMEM;
  1599. goto out;
  1600. }
  1601. for (i = 0; i < tev->nargs; i++) {
  1602. p = strchr(argv[i + 2], '=');
  1603. if (p) /* We don't need which register is assigned. */
  1604. *p++ = '\0';
  1605. else
  1606. p = argv[i + 2];
  1607. tev->args[i].name = strdup(argv[i + 2]);
  1608. /* TODO: parse regs and offset */
  1609. tev->args[i].value = strdup(p);
  1610. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1611. ret = -ENOMEM;
  1612. goto out;
  1613. }
  1614. }
  1615. ret = 0;
  1616. out:
  1617. free(argv0_str);
  1618. argv_free(argv);
  1619. return ret;
  1620. }
  1621. /* Compose only probe arg */
  1622. char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
  1623. {
  1624. struct perf_probe_arg_field *field = pa->field;
  1625. struct strbuf buf;
  1626. char *ret = NULL;
  1627. int err;
  1628. if (strbuf_init(&buf, 64) < 0)
  1629. return NULL;
  1630. if (pa->name && pa->var)
  1631. err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
  1632. else
  1633. err = strbuf_addstr(&buf, pa->name ?: pa->var);
  1634. if (err)
  1635. goto out;
  1636. while (field) {
  1637. if (field->name[0] == '[')
  1638. err = strbuf_addstr(&buf, field->name);
  1639. else
  1640. err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
  1641. field->name);
  1642. field = field->next;
  1643. if (err)
  1644. goto out;
  1645. }
  1646. if (pa->type)
  1647. if (strbuf_addf(&buf, ":%s", pa->type) < 0)
  1648. goto out;
  1649. ret = strbuf_detach(&buf, NULL);
  1650. out:
  1651. strbuf_release(&buf);
  1652. return ret;
  1653. }
  1654. /* Compose only probe point (not argument) */
  1655. char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1656. {
  1657. struct strbuf buf;
  1658. char *tmp, *ret = NULL;
  1659. int len, err = 0;
  1660. if (strbuf_init(&buf, 64) < 0)
  1661. return NULL;
  1662. if (pp->function) {
  1663. if (strbuf_addstr(&buf, pp->function) < 0)
  1664. goto out;
  1665. if (pp->offset)
  1666. err = strbuf_addf(&buf, "+%lu", pp->offset);
  1667. else if (pp->line)
  1668. err = strbuf_addf(&buf, ":%d", pp->line);
  1669. else if (pp->retprobe)
  1670. err = strbuf_addstr(&buf, "%return");
  1671. if (err)
  1672. goto out;
  1673. }
  1674. if (pp->file) {
  1675. tmp = pp->file;
  1676. len = strlen(tmp);
  1677. if (len > 30) {
  1678. tmp = strchr(pp->file + len - 30, '/');
  1679. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1680. }
  1681. err = strbuf_addf(&buf, "@%s", tmp);
  1682. if (!err && !pp->function && pp->line)
  1683. err = strbuf_addf(&buf, ":%d", pp->line);
  1684. }
  1685. if (!err)
  1686. ret = strbuf_detach(&buf, NULL);
  1687. out:
  1688. strbuf_release(&buf);
  1689. return ret;
  1690. }
  1691. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1692. {
  1693. struct strbuf buf;
  1694. char *tmp, *ret = NULL;
  1695. int i;
  1696. if (strbuf_init(&buf, 64))
  1697. return NULL;
  1698. if (pev->event)
  1699. if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
  1700. pev->event) < 0)
  1701. goto out;
  1702. tmp = synthesize_perf_probe_point(&pev->point);
  1703. if (!tmp || strbuf_addstr(&buf, tmp) < 0)
  1704. goto out;
  1705. free(tmp);
  1706. for (i = 0; i < pev->nargs; i++) {
  1707. tmp = synthesize_perf_probe_arg(pev->args + i);
  1708. if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
  1709. goto out;
  1710. free(tmp);
  1711. }
  1712. ret = strbuf_detach(&buf, NULL);
  1713. out:
  1714. strbuf_release(&buf);
  1715. return ret;
  1716. }
  1717. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1718. struct strbuf *buf, int depth)
  1719. {
  1720. int err;
  1721. if (ref->next) {
  1722. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1723. depth + 1);
  1724. if (depth < 0)
  1725. return depth;
  1726. }
  1727. err = strbuf_addf(buf, "%+ld(", ref->offset);
  1728. return (err < 0) ? err : depth;
  1729. }
  1730. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1731. struct strbuf *buf)
  1732. {
  1733. struct probe_trace_arg_ref *ref = arg->ref;
  1734. int depth = 0, err;
  1735. /* Argument name or separator */
  1736. if (arg->name)
  1737. err = strbuf_addf(buf, " %s=", arg->name);
  1738. else
  1739. err = strbuf_addch(buf, ' ');
  1740. if (err)
  1741. return err;
  1742. /* Special case: @XXX */
  1743. if (arg->value[0] == '@' && arg->ref)
  1744. ref = ref->next;
  1745. /* Dereferencing arguments */
  1746. if (ref) {
  1747. depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
  1748. if (depth < 0)
  1749. return depth;
  1750. }
  1751. /* Print argument value */
  1752. if (arg->value[0] == '@' && arg->ref)
  1753. err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
  1754. else
  1755. err = strbuf_addstr(buf, arg->value);
  1756. /* Closing */
  1757. while (!err && depth--)
  1758. err = strbuf_addch(buf, ')');
  1759. /* Print argument type */
  1760. if (!err && arg->type)
  1761. err = strbuf_addf(buf, ":%s", arg->type);
  1762. return err;
  1763. }
  1764. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1765. {
  1766. struct probe_trace_point *tp = &tev->point;
  1767. struct strbuf buf;
  1768. char *ret = NULL;
  1769. int i, err;
  1770. /* Uprobes must have tp->module */
  1771. if (tev->uprobes && !tp->module)
  1772. return NULL;
  1773. if (strbuf_init(&buf, 32) < 0)
  1774. return NULL;
  1775. if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
  1776. tev->group, tev->event) < 0)
  1777. goto error;
  1778. /*
  1779. * If tp->address == 0, then this point must be a
  1780. * absolute address uprobe.
  1781. * try_to_find_absolute_address() should have made
  1782. * tp->symbol to "0x0".
  1783. */
  1784. if (tev->uprobes && !tp->address) {
  1785. if (!tp->symbol || strcmp(tp->symbol, "0x0"))
  1786. goto error;
  1787. }
  1788. /* Use the tp->address for uprobes */
  1789. if (tev->uprobes)
  1790. err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
  1791. else if (!strncmp(tp->symbol, "0x", 2))
  1792. /* Absolute address. See try_to_find_absolute_address() */
  1793. err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
  1794. tp->module ? ":" : "", tp->address);
  1795. else
  1796. err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
  1797. tp->module ? ":" : "", tp->symbol, tp->offset);
  1798. if (err)
  1799. goto error;
  1800. for (i = 0; i < tev->nargs; i++)
  1801. if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
  1802. goto error;
  1803. ret = strbuf_detach(&buf, NULL);
  1804. error:
  1805. strbuf_release(&buf);
  1806. return ret;
  1807. }
  1808. static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
  1809. struct perf_probe_point *pp,
  1810. bool is_kprobe)
  1811. {
  1812. struct symbol *sym = NULL;
  1813. struct map *map = NULL;
  1814. u64 addr = tp->address;
  1815. int ret = -ENOENT;
  1816. if (!is_kprobe) {
  1817. map = dso__new_map(tp->module);
  1818. if (!map)
  1819. goto out;
  1820. sym = map__find_symbol(map, addr);
  1821. } else {
  1822. if (tp->symbol && !addr) {
  1823. if (kernel_get_symbol_address_by_name(tp->symbol,
  1824. &addr, true, false) < 0)
  1825. goto out;
  1826. }
  1827. if (addr) {
  1828. addr += tp->offset;
  1829. sym = machine__find_kernel_symbol(host_machine, addr, &map);
  1830. }
  1831. }
  1832. if (!sym)
  1833. goto out;
  1834. pp->retprobe = tp->retprobe;
  1835. pp->offset = addr - map->unmap_ip(map, sym->start);
  1836. pp->function = strdup(sym->name);
  1837. ret = pp->function ? 0 : -ENOMEM;
  1838. out:
  1839. if (map && !is_kprobe) {
  1840. map__put(map);
  1841. }
  1842. return ret;
  1843. }
  1844. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  1845. struct perf_probe_point *pp,
  1846. bool is_kprobe)
  1847. {
  1848. char buf[128];
  1849. int ret;
  1850. ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
  1851. if (!ret)
  1852. return 0;
  1853. ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
  1854. if (!ret)
  1855. return 0;
  1856. pr_debug("Failed to find probe point from both of dwarf and map.\n");
  1857. if (tp->symbol) {
  1858. pp->function = strdup(tp->symbol);
  1859. pp->offset = tp->offset;
  1860. } else {
  1861. ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
  1862. if (ret < 0)
  1863. return ret;
  1864. pp->function = strdup(buf);
  1865. pp->offset = 0;
  1866. }
  1867. if (pp->function == NULL)
  1868. return -ENOMEM;
  1869. pp->retprobe = tp->retprobe;
  1870. return 0;
  1871. }
  1872. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1873. struct perf_probe_event *pev, bool is_kprobe)
  1874. {
  1875. struct strbuf buf = STRBUF_INIT;
  1876. int i, ret;
  1877. /* Convert event/group name */
  1878. pev->event = strdup(tev->event);
  1879. pev->group = strdup(tev->group);
  1880. if (pev->event == NULL || pev->group == NULL)
  1881. return -ENOMEM;
  1882. /* Convert trace_point to probe_point */
  1883. ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
  1884. if (ret < 0)
  1885. return ret;
  1886. /* Convert trace_arg to probe_arg */
  1887. pev->nargs = tev->nargs;
  1888. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1889. if (pev->args == NULL)
  1890. return -ENOMEM;
  1891. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1892. if (tev->args[i].name)
  1893. pev->args[i].name = strdup(tev->args[i].name);
  1894. else {
  1895. if ((ret = strbuf_init(&buf, 32)) < 0)
  1896. goto error;
  1897. ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
  1898. pev->args[i].name = strbuf_detach(&buf, NULL);
  1899. }
  1900. if (pev->args[i].name == NULL && ret >= 0)
  1901. ret = -ENOMEM;
  1902. }
  1903. error:
  1904. if (ret < 0)
  1905. clear_perf_probe_event(pev);
  1906. return ret;
  1907. }
  1908. void clear_perf_probe_event(struct perf_probe_event *pev)
  1909. {
  1910. struct perf_probe_arg_field *field, *next;
  1911. int i;
  1912. free(pev->event);
  1913. free(pev->group);
  1914. free(pev->target);
  1915. clear_perf_probe_point(&pev->point);
  1916. for (i = 0; i < pev->nargs; i++) {
  1917. free(pev->args[i].name);
  1918. free(pev->args[i].var);
  1919. free(pev->args[i].type);
  1920. field = pev->args[i].field;
  1921. while (field) {
  1922. next = field->next;
  1923. zfree(&field->name);
  1924. free(field);
  1925. field = next;
  1926. }
  1927. }
  1928. free(pev->args);
  1929. memset(pev, 0, sizeof(*pev));
  1930. }
  1931. #define strdup_or_goto(str, label) \
  1932. ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
  1933. static int perf_probe_point__copy(struct perf_probe_point *dst,
  1934. struct perf_probe_point *src)
  1935. {
  1936. dst->file = strdup_or_goto(src->file, out_err);
  1937. dst->function = strdup_or_goto(src->function, out_err);
  1938. dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
  1939. dst->line = src->line;
  1940. dst->retprobe = src->retprobe;
  1941. dst->offset = src->offset;
  1942. return 0;
  1943. out_err:
  1944. clear_perf_probe_point(dst);
  1945. return -ENOMEM;
  1946. }
  1947. static int perf_probe_arg__copy(struct perf_probe_arg *dst,
  1948. struct perf_probe_arg *src)
  1949. {
  1950. struct perf_probe_arg_field *field, **ppfield;
  1951. dst->name = strdup_or_goto(src->name, out_err);
  1952. dst->var = strdup_or_goto(src->var, out_err);
  1953. dst->type = strdup_or_goto(src->type, out_err);
  1954. field = src->field;
  1955. ppfield = &(dst->field);
  1956. while (field) {
  1957. *ppfield = zalloc(sizeof(*field));
  1958. if (!*ppfield)
  1959. goto out_err;
  1960. (*ppfield)->name = strdup_or_goto(field->name, out_err);
  1961. (*ppfield)->index = field->index;
  1962. (*ppfield)->ref = field->ref;
  1963. field = field->next;
  1964. ppfield = &((*ppfield)->next);
  1965. }
  1966. return 0;
  1967. out_err:
  1968. return -ENOMEM;
  1969. }
  1970. int perf_probe_event__copy(struct perf_probe_event *dst,
  1971. struct perf_probe_event *src)
  1972. {
  1973. int i;
  1974. dst->event = strdup_or_goto(src->event, out_err);
  1975. dst->group = strdup_or_goto(src->group, out_err);
  1976. dst->target = strdup_or_goto(src->target, out_err);
  1977. dst->uprobes = src->uprobes;
  1978. if (perf_probe_point__copy(&dst->point, &src->point) < 0)
  1979. goto out_err;
  1980. dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
  1981. if (!dst->args)
  1982. goto out_err;
  1983. dst->nargs = src->nargs;
  1984. for (i = 0; i < src->nargs; i++)
  1985. if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
  1986. goto out_err;
  1987. return 0;
  1988. out_err:
  1989. clear_perf_probe_event(dst);
  1990. return -ENOMEM;
  1991. }
  1992. void clear_probe_trace_event(struct probe_trace_event *tev)
  1993. {
  1994. struct probe_trace_arg_ref *ref, *next;
  1995. int i;
  1996. free(tev->event);
  1997. free(tev->group);
  1998. free(tev->point.symbol);
  1999. free(tev->point.realname);
  2000. free(tev->point.module);
  2001. for (i = 0; i < tev->nargs; i++) {
  2002. free(tev->args[i].name);
  2003. free(tev->args[i].value);
  2004. free(tev->args[i].type);
  2005. ref = tev->args[i].ref;
  2006. while (ref) {
  2007. next = ref->next;
  2008. free(ref);
  2009. ref = next;
  2010. }
  2011. }
  2012. free(tev->args);
  2013. memset(tev, 0, sizeof(*tev));
  2014. }
  2015. struct kprobe_blacklist_node {
  2016. struct list_head list;
  2017. unsigned long start;
  2018. unsigned long end;
  2019. char *symbol;
  2020. };
  2021. static void kprobe_blacklist__delete(struct list_head *blacklist)
  2022. {
  2023. struct kprobe_blacklist_node *node;
  2024. while (!list_empty(blacklist)) {
  2025. node = list_first_entry(blacklist,
  2026. struct kprobe_blacklist_node, list);
  2027. list_del(&node->list);
  2028. free(node->symbol);
  2029. free(node);
  2030. }
  2031. }
  2032. static int kprobe_blacklist__load(struct list_head *blacklist)
  2033. {
  2034. struct kprobe_blacklist_node *node;
  2035. const char *__debugfs = debugfs__mountpoint();
  2036. char buf[PATH_MAX], *p;
  2037. FILE *fp;
  2038. int ret;
  2039. if (__debugfs == NULL)
  2040. return -ENOTSUP;
  2041. ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
  2042. if (ret < 0)
  2043. return ret;
  2044. fp = fopen(buf, "r");
  2045. if (!fp)
  2046. return -errno;
  2047. ret = 0;
  2048. while (fgets(buf, PATH_MAX, fp)) {
  2049. node = zalloc(sizeof(*node));
  2050. if (!node) {
  2051. ret = -ENOMEM;
  2052. break;
  2053. }
  2054. INIT_LIST_HEAD(&node->list);
  2055. list_add_tail(&node->list, blacklist);
  2056. if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
  2057. ret = -EINVAL;
  2058. break;
  2059. }
  2060. p = strchr(buf, '\t');
  2061. if (p) {
  2062. p++;
  2063. if (p[strlen(p) - 1] == '\n')
  2064. p[strlen(p) - 1] = '\0';
  2065. } else
  2066. p = (char *)"unknown";
  2067. node->symbol = strdup(p);
  2068. if (!node->symbol) {
  2069. ret = -ENOMEM;
  2070. break;
  2071. }
  2072. pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
  2073. node->start, node->end, node->symbol);
  2074. ret++;
  2075. }
  2076. if (ret < 0)
  2077. kprobe_blacklist__delete(blacklist);
  2078. fclose(fp);
  2079. return ret;
  2080. }
  2081. static struct kprobe_blacklist_node *
  2082. kprobe_blacklist__find_by_address(struct list_head *blacklist,
  2083. unsigned long address)
  2084. {
  2085. struct kprobe_blacklist_node *node;
  2086. list_for_each_entry(node, blacklist, list) {
  2087. if (node->start <= address && address < node->end)
  2088. return node;
  2089. }
  2090. return NULL;
  2091. }
  2092. static LIST_HEAD(kprobe_blacklist);
  2093. static void kprobe_blacklist__init(void)
  2094. {
  2095. if (!list_empty(&kprobe_blacklist))
  2096. return;
  2097. if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
  2098. pr_debug("No kprobe blacklist support, ignored\n");
  2099. }
  2100. static void kprobe_blacklist__release(void)
  2101. {
  2102. kprobe_blacklist__delete(&kprobe_blacklist);
  2103. }
  2104. static bool kprobe_blacklist__listed(unsigned long address)
  2105. {
  2106. return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
  2107. }
  2108. static int perf_probe_event__sprintf(const char *group, const char *event,
  2109. struct perf_probe_event *pev,
  2110. const char *module,
  2111. struct strbuf *result)
  2112. {
  2113. int i, ret;
  2114. char *buf;
  2115. if (asprintf(&buf, "%s:%s", group, event) < 0)
  2116. return -errno;
  2117. ret = strbuf_addf(result, " %-20s (on ", buf);
  2118. free(buf);
  2119. if (ret)
  2120. return ret;
  2121. /* Synthesize only event probe point */
  2122. buf = synthesize_perf_probe_point(&pev->point);
  2123. if (!buf)
  2124. return -ENOMEM;
  2125. ret = strbuf_addstr(result, buf);
  2126. free(buf);
  2127. if (!ret && module)
  2128. ret = strbuf_addf(result, " in %s", module);
  2129. if (!ret && pev->nargs > 0) {
  2130. ret = strbuf_add(result, " with", 5);
  2131. for (i = 0; !ret && i < pev->nargs; i++) {
  2132. buf = synthesize_perf_probe_arg(&pev->args[i]);
  2133. if (!buf)
  2134. return -ENOMEM;
  2135. ret = strbuf_addf(result, " %s", buf);
  2136. free(buf);
  2137. }
  2138. }
  2139. if (!ret)
  2140. ret = strbuf_addch(result, ')');
  2141. return ret;
  2142. }
  2143. /* Show an event */
  2144. int show_perf_probe_event(const char *group, const char *event,
  2145. struct perf_probe_event *pev,
  2146. const char *module, bool use_stdout)
  2147. {
  2148. struct strbuf buf = STRBUF_INIT;
  2149. int ret;
  2150. ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
  2151. if (ret >= 0) {
  2152. if (use_stdout)
  2153. printf("%s\n", buf.buf);
  2154. else
  2155. pr_info("%s\n", buf.buf);
  2156. }
  2157. strbuf_release(&buf);
  2158. return ret;
  2159. }
  2160. static bool filter_probe_trace_event(struct probe_trace_event *tev,
  2161. struct strfilter *filter)
  2162. {
  2163. char tmp[128];
  2164. /* At first, check the event name itself */
  2165. if (strfilter__compare(filter, tev->event))
  2166. return true;
  2167. /* Next, check the combination of name and group */
  2168. if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
  2169. return false;
  2170. return strfilter__compare(filter, tmp);
  2171. }
  2172. static int __show_perf_probe_events(int fd, bool is_kprobe,
  2173. struct strfilter *filter)
  2174. {
  2175. int ret = 0;
  2176. struct probe_trace_event tev;
  2177. struct perf_probe_event pev;
  2178. struct strlist *rawlist;
  2179. struct str_node *ent;
  2180. memset(&tev, 0, sizeof(tev));
  2181. memset(&pev, 0, sizeof(pev));
  2182. rawlist = probe_file__get_rawlist(fd);
  2183. if (!rawlist)
  2184. return -ENOMEM;
  2185. strlist__for_each_entry(ent, rawlist) {
  2186. ret = parse_probe_trace_command(ent->s, &tev);
  2187. if (ret >= 0) {
  2188. if (!filter_probe_trace_event(&tev, filter))
  2189. goto next;
  2190. ret = convert_to_perf_probe_event(&tev, &pev,
  2191. is_kprobe);
  2192. if (ret < 0)
  2193. goto next;
  2194. ret = show_perf_probe_event(pev.group, pev.event,
  2195. &pev, tev.point.module,
  2196. true);
  2197. }
  2198. next:
  2199. clear_perf_probe_event(&pev);
  2200. clear_probe_trace_event(&tev);
  2201. if (ret < 0)
  2202. break;
  2203. }
  2204. strlist__delete(rawlist);
  2205. /* Cleanup cached debuginfo if needed */
  2206. debuginfo_cache__exit();
  2207. return ret;
  2208. }
  2209. /* List up current perf-probe events */
  2210. int show_perf_probe_events(struct strfilter *filter)
  2211. {
  2212. int kp_fd, up_fd, ret;
  2213. setup_pager();
  2214. if (probe_conf.cache)
  2215. return probe_cache__show_all_caches(filter);
  2216. ret = init_probe_symbol_maps(false);
  2217. if (ret < 0)
  2218. return ret;
  2219. ret = probe_file__open_both(&kp_fd, &up_fd, 0);
  2220. if (ret < 0)
  2221. return ret;
  2222. if (kp_fd >= 0)
  2223. ret = __show_perf_probe_events(kp_fd, true, filter);
  2224. if (up_fd >= 0 && ret >= 0)
  2225. ret = __show_perf_probe_events(up_fd, false, filter);
  2226. if (kp_fd > 0)
  2227. close(kp_fd);
  2228. if (up_fd > 0)
  2229. close(up_fd);
  2230. exit_probe_symbol_maps();
  2231. return ret;
  2232. }
  2233. static int get_new_event_name(char *buf, size_t len, const char *base,
  2234. struct strlist *namelist, bool ret_event,
  2235. bool allow_suffix)
  2236. {
  2237. int i, ret;
  2238. char *p, *nbase;
  2239. if (*base == '.')
  2240. base++;
  2241. nbase = strdup(base);
  2242. if (!nbase)
  2243. return -ENOMEM;
  2244. /* Cut off the dot suffixes (e.g. .const, .isra) and version suffixes */
  2245. p = strpbrk(nbase, ".@");
  2246. if (p && p != nbase)
  2247. *p = '\0';
  2248. /* Try no suffix number */
  2249. ret = e_snprintf(buf, len, "%s%s", nbase, ret_event ? "__return" : "");
  2250. if (ret < 0) {
  2251. pr_debug("snprintf() failed: %d\n", ret);
  2252. goto out;
  2253. }
  2254. if (!strlist__has_entry(namelist, buf))
  2255. goto out;
  2256. if (!allow_suffix) {
  2257. pr_warning("Error: event \"%s\" already exists.\n"
  2258. " Hint: Remove existing event by 'perf probe -d'\n"
  2259. " or force duplicates by 'perf probe -f'\n"
  2260. " or set 'force=yes' in BPF source.\n",
  2261. buf);
  2262. ret = -EEXIST;
  2263. goto out;
  2264. }
  2265. /* Try to add suffix */
  2266. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  2267. ret = e_snprintf(buf, len, "%s_%d", nbase, i);
  2268. if (ret < 0) {
  2269. pr_debug("snprintf() failed: %d\n", ret);
  2270. goto out;
  2271. }
  2272. if (!strlist__has_entry(namelist, buf))
  2273. break;
  2274. }
  2275. if (i == MAX_EVENT_INDEX) {
  2276. pr_warning("Too many events are on the same function.\n");
  2277. ret = -ERANGE;
  2278. }
  2279. out:
  2280. free(nbase);
  2281. /* Final validation */
  2282. if (ret >= 0 && !is_c_func_name(buf)) {
  2283. pr_warning("Internal error: \"%s\" is an invalid event name.\n",
  2284. buf);
  2285. ret = -EINVAL;
  2286. }
  2287. return ret;
  2288. }
  2289. /* Warn if the current kernel's uprobe implementation is old */
  2290. static void warn_uprobe_event_compat(struct probe_trace_event *tev)
  2291. {
  2292. int i;
  2293. char *buf = synthesize_probe_trace_command(tev);
  2294. /* Old uprobe event doesn't support memory dereference */
  2295. if (!tev->uprobes || tev->nargs == 0 || !buf)
  2296. goto out;
  2297. for (i = 0; i < tev->nargs; i++)
  2298. if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
  2299. pr_warning("Please upgrade your kernel to at least "
  2300. "3.14 to have access to feature %s\n",
  2301. tev->args[i].value);
  2302. break;
  2303. }
  2304. out:
  2305. free(buf);
  2306. }
  2307. /* Set new name from original perf_probe_event and namelist */
  2308. static int probe_trace_event__set_name(struct probe_trace_event *tev,
  2309. struct perf_probe_event *pev,
  2310. struct strlist *namelist,
  2311. bool allow_suffix)
  2312. {
  2313. const char *event, *group;
  2314. char buf[64];
  2315. int ret;
  2316. /* If probe_event or trace_event already have the name, reuse it */
  2317. if (pev->event && !pev->sdt)
  2318. event = pev->event;
  2319. else if (tev->event)
  2320. event = tev->event;
  2321. else {
  2322. /* Or generate new one from probe point */
  2323. if (pev->point.function &&
  2324. (strncmp(pev->point.function, "0x", 2) != 0) &&
  2325. !strisglob(pev->point.function))
  2326. event = pev->point.function;
  2327. else
  2328. event = tev->point.realname;
  2329. }
  2330. if (pev->group && !pev->sdt)
  2331. group = pev->group;
  2332. else if (tev->group)
  2333. group = tev->group;
  2334. else
  2335. group = PERFPROBE_GROUP;
  2336. /* Get an unused new event name */
  2337. ret = get_new_event_name(buf, 64, event, namelist,
  2338. tev->point.retprobe, allow_suffix);
  2339. if (ret < 0)
  2340. return ret;
  2341. event = buf;
  2342. tev->event = strdup(event);
  2343. tev->group = strdup(group);
  2344. if (tev->event == NULL || tev->group == NULL)
  2345. return -ENOMEM;
  2346. /* Add added event name to namelist */
  2347. strlist__add(namelist, event);
  2348. return 0;
  2349. }
  2350. static int __open_probe_file_and_namelist(bool uprobe,
  2351. struct strlist **namelist)
  2352. {
  2353. int fd;
  2354. fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
  2355. if (fd < 0)
  2356. return fd;
  2357. /* Get current event names */
  2358. *namelist = probe_file__get_namelist(fd);
  2359. if (!(*namelist)) {
  2360. pr_debug("Failed to get current event list.\n");
  2361. close(fd);
  2362. return -ENOMEM;
  2363. }
  2364. return fd;
  2365. }
  2366. static int __add_probe_trace_events(struct perf_probe_event *pev,
  2367. struct probe_trace_event *tevs,
  2368. int ntevs, bool allow_suffix)
  2369. {
  2370. int i, fd[2] = {-1, -1}, up, ret;
  2371. struct probe_trace_event *tev = NULL;
  2372. struct probe_cache *cache = NULL;
  2373. struct strlist *namelist[2] = {NULL, NULL};
  2374. struct nscookie nsc;
  2375. up = pev->uprobes ? 1 : 0;
  2376. fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
  2377. if (fd[up] < 0)
  2378. return fd[up];
  2379. ret = 0;
  2380. for (i = 0; i < ntevs; i++) {
  2381. tev = &tevs[i];
  2382. up = tev->uprobes ? 1 : 0;
  2383. if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
  2384. fd[up] = __open_probe_file_and_namelist(up,
  2385. &namelist[up]);
  2386. if (fd[up] < 0)
  2387. goto close_out;
  2388. }
  2389. /* Skip if the symbol is out of .text or blacklisted */
  2390. if (!tev->point.symbol && !pev->uprobes)
  2391. continue;
  2392. /* Set new name for tev (and update namelist) */
  2393. ret = probe_trace_event__set_name(tev, pev, namelist[up],
  2394. allow_suffix);
  2395. if (ret < 0)
  2396. break;
  2397. nsinfo__mountns_enter(pev->nsi, &nsc);
  2398. ret = probe_file__add_event(fd[up], tev);
  2399. nsinfo__mountns_exit(&nsc);
  2400. if (ret < 0)
  2401. break;
  2402. /*
  2403. * Probes after the first probe which comes from same
  2404. * user input are always allowed to add suffix, because
  2405. * there might be several addresses corresponding to
  2406. * one code line.
  2407. */
  2408. allow_suffix = true;
  2409. }
  2410. if (ret == -EINVAL && pev->uprobes)
  2411. warn_uprobe_event_compat(tev);
  2412. if (ret == 0 && probe_conf.cache) {
  2413. cache = probe_cache__new(pev->target, pev->nsi);
  2414. if (!cache ||
  2415. probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
  2416. probe_cache__commit(cache) < 0)
  2417. pr_warning("Failed to add event to probe cache\n");
  2418. probe_cache__delete(cache);
  2419. }
  2420. close_out:
  2421. for (up = 0; up < 2; up++) {
  2422. strlist__delete(namelist[up]);
  2423. if (fd[up] >= 0)
  2424. close(fd[up]);
  2425. }
  2426. return ret;
  2427. }
  2428. static int find_probe_functions(struct map *map, char *name,
  2429. struct symbol **syms)
  2430. {
  2431. int found = 0;
  2432. struct symbol *sym;
  2433. struct rb_node *tmp;
  2434. const char *norm, *ver;
  2435. char *buf = NULL;
  2436. bool cut_version = true;
  2437. if (map__load(map) < 0)
  2438. return 0;
  2439. /* If user gives a version, don't cut off the version from symbols */
  2440. if (strchr(name, '@'))
  2441. cut_version = false;
  2442. map__for_each_symbol(map, sym, tmp) {
  2443. norm = arch__normalize_symbol_name(sym->name);
  2444. if (!norm)
  2445. continue;
  2446. if (cut_version) {
  2447. /* We don't care about default symbol or not */
  2448. ver = strchr(norm, '@');
  2449. if (ver) {
  2450. buf = strndup(norm, ver - norm);
  2451. if (!buf)
  2452. return -ENOMEM;
  2453. norm = buf;
  2454. }
  2455. }
  2456. if (strglobmatch(norm, name)) {
  2457. found++;
  2458. if (syms && found < probe_conf.max_probes)
  2459. syms[found - 1] = sym;
  2460. }
  2461. if (buf)
  2462. zfree(&buf);
  2463. }
  2464. return found;
  2465. }
  2466. void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
  2467. struct probe_trace_event *tev __maybe_unused,
  2468. struct map *map __maybe_unused,
  2469. struct symbol *sym __maybe_unused) { }
  2470. /*
  2471. * Find probe function addresses from map.
  2472. * Return an error or the number of found probe_trace_event
  2473. */
  2474. static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
  2475. struct probe_trace_event **tevs)
  2476. {
  2477. struct map *map = NULL;
  2478. struct ref_reloc_sym *reloc_sym = NULL;
  2479. struct symbol *sym;
  2480. struct symbol **syms = NULL;
  2481. struct probe_trace_event *tev;
  2482. struct perf_probe_point *pp = &pev->point;
  2483. struct probe_trace_point *tp;
  2484. int num_matched_functions;
  2485. int ret, i, j, skipped = 0;
  2486. char *mod_name;
  2487. map = get_target_map(pev->target, pev->nsi, pev->uprobes);
  2488. if (!map) {
  2489. ret = -EINVAL;
  2490. goto out;
  2491. }
  2492. syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
  2493. if (!syms) {
  2494. ret = -ENOMEM;
  2495. goto out;
  2496. }
  2497. /*
  2498. * Load matched symbols: Since the different local symbols may have
  2499. * same name but different addresses, this lists all the symbols.
  2500. */
  2501. num_matched_functions = find_probe_functions(map, pp->function, syms);
  2502. if (num_matched_functions <= 0) {
  2503. pr_err("Failed to find symbol %s in %s\n", pp->function,
  2504. pev->target ? : "kernel");
  2505. ret = -ENOENT;
  2506. goto out;
  2507. } else if (num_matched_functions > probe_conf.max_probes) {
  2508. pr_err("Too many functions matched in %s\n",
  2509. pev->target ? : "kernel");
  2510. ret = -E2BIG;
  2511. goto out;
  2512. }
  2513. /* Note that the symbols in the kmodule are not relocated */
  2514. if (!pev->uprobes && !pev->target &&
  2515. (!pp->retprobe || kretprobe_offset_is_supported())) {
  2516. reloc_sym = kernel_get_ref_reloc_sym();
  2517. if (!reloc_sym) {
  2518. pr_warning("Relocated base symbol is not found!\n");
  2519. ret = -EINVAL;
  2520. goto out;
  2521. }
  2522. }
  2523. /* Setup result trace-probe-events */
  2524. *tevs = zalloc(sizeof(*tev) * num_matched_functions);
  2525. if (!*tevs) {
  2526. ret = -ENOMEM;
  2527. goto out;
  2528. }
  2529. ret = 0;
  2530. for (j = 0; j < num_matched_functions; j++) {
  2531. sym = syms[j];
  2532. tev = (*tevs) + ret;
  2533. tp = &tev->point;
  2534. if (ret == num_matched_functions) {
  2535. pr_warning("Too many symbols are listed. Skip it.\n");
  2536. break;
  2537. }
  2538. ret++;
  2539. if (pp->offset > sym->end - sym->start) {
  2540. pr_warning("Offset %ld is bigger than the size of %s\n",
  2541. pp->offset, sym->name);
  2542. ret = -ENOENT;
  2543. goto err_out;
  2544. }
  2545. /* Add one probe point */
  2546. tp->address = map->unmap_ip(map, sym->start) + pp->offset;
  2547. /* Check the kprobe (not in module) is within .text */
  2548. if (!pev->uprobes && !pev->target &&
  2549. kprobe_warn_out_range(sym->name, tp->address)) {
  2550. tp->symbol = NULL; /* Skip it */
  2551. skipped++;
  2552. } else if (reloc_sym) {
  2553. tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
  2554. tp->offset = tp->address - reloc_sym->addr;
  2555. } else {
  2556. tp->symbol = strdup_or_goto(sym->name, nomem_out);
  2557. tp->offset = pp->offset;
  2558. }
  2559. tp->realname = strdup_or_goto(sym->name, nomem_out);
  2560. tp->retprobe = pp->retprobe;
  2561. if (pev->target) {
  2562. if (pev->uprobes) {
  2563. tev->point.module = strdup_or_goto(pev->target,
  2564. nomem_out);
  2565. } else {
  2566. mod_name = find_module_name(pev->target);
  2567. tev->point.module =
  2568. strdup(mod_name ? mod_name : pev->target);
  2569. free(mod_name);
  2570. if (!tev->point.module)
  2571. goto nomem_out;
  2572. }
  2573. }
  2574. tev->uprobes = pev->uprobes;
  2575. tev->nargs = pev->nargs;
  2576. if (tev->nargs) {
  2577. tev->args = zalloc(sizeof(struct probe_trace_arg) *
  2578. tev->nargs);
  2579. if (tev->args == NULL)
  2580. goto nomem_out;
  2581. }
  2582. for (i = 0; i < tev->nargs; i++) {
  2583. if (pev->args[i].name)
  2584. tev->args[i].name =
  2585. strdup_or_goto(pev->args[i].name,
  2586. nomem_out);
  2587. tev->args[i].value = strdup_or_goto(pev->args[i].var,
  2588. nomem_out);
  2589. if (pev->args[i].type)
  2590. tev->args[i].type =
  2591. strdup_or_goto(pev->args[i].type,
  2592. nomem_out);
  2593. }
  2594. arch__fix_tev_from_maps(pev, tev, map, sym);
  2595. }
  2596. if (ret == skipped) {
  2597. ret = -ENOENT;
  2598. goto err_out;
  2599. }
  2600. out:
  2601. map__put(map);
  2602. free(syms);
  2603. return ret;
  2604. nomem_out:
  2605. ret = -ENOMEM;
  2606. err_out:
  2607. clear_probe_trace_events(*tevs, num_matched_functions);
  2608. zfree(tevs);
  2609. goto out;
  2610. }
  2611. static int try_to_find_absolute_address(struct perf_probe_event *pev,
  2612. struct probe_trace_event **tevs)
  2613. {
  2614. struct perf_probe_point *pp = &pev->point;
  2615. struct probe_trace_event *tev;
  2616. struct probe_trace_point *tp;
  2617. int i, err;
  2618. if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
  2619. return -EINVAL;
  2620. if (perf_probe_event_need_dwarf(pev))
  2621. return -EINVAL;
  2622. /*
  2623. * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
  2624. * absolute address.
  2625. *
  2626. * Only one tev can be generated by this.
  2627. */
  2628. *tevs = zalloc(sizeof(*tev));
  2629. if (!*tevs)
  2630. return -ENOMEM;
  2631. tev = *tevs;
  2632. tp = &tev->point;
  2633. /*
  2634. * Don't use tp->offset, use address directly, because
  2635. * in synthesize_probe_trace_command() address cannot be
  2636. * zero.
  2637. */
  2638. tp->address = pev->point.abs_address;
  2639. tp->retprobe = pp->retprobe;
  2640. tev->uprobes = pev->uprobes;
  2641. err = -ENOMEM;
  2642. /*
  2643. * Give it a '0x' leading symbol name.
  2644. * In __add_probe_trace_events, a NULL symbol is interpreted as
  2645. * invalud.
  2646. */
  2647. if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
  2648. goto errout;
  2649. /* For kprobe, check range */
  2650. if ((!tev->uprobes) &&
  2651. (kprobe_warn_out_range(tev->point.symbol,
  2652. tev->point.address))) {
  2653. err = -EACCES;
  2654. goto errout;
  2655. }
  2656. if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
  2657. goto errout;
  2658. if (pev->target) {
  2659. tp->module = strdup(pev->target);
  2660. if (!tp->module)
  2661. goto errout;
  2662. }
  2663. if (tev->group) {
  2664. tev->group = strdup(pev->group);
  2665. if (!tev->group)
  2666. goto errout;
  2667. }
  2668. if (pev->event) {
  2669. tev->event = strdup(pev->event);
  2670. if (!tev->event)
  2671. goto errout;
  2672. }
  2673. tev->nargs = pev->nargs;
  2674. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  2675. if (!tev->args)
  2676. goto errout;
  2677. for (i = 0; i < tev->nargs; i++)
  2678. copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
  2679. return 1;
  2680. errout:
  2681. clear_probe_trace_events(*tevs, 1);
  2682. *tevs = NULL;
  2683. return err;
  2684. }
  2685. /* Concatinate two arrays */
  2686. static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
  2687. {
  2688. void *ret;
  2689. ret = malloc(sz_a + sz_b);
  2690. if (ret) {
  2691. memcpy(ret, a, sz_a);
  2692. memcpy(ret + sz_a, b, sz_b);
  2693. }
  2694. return ret;
  2695. }
  2696. static int
  2697. concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
  2698. struct probe_trace_event **tevs2, int ntevs2)
  2699. {
  2700. struct probe_trace_event *new_tevs;
  2701. int ret = 0;
  2702. if (*ntevs == 0) {
  2703. *tevs = *tevs2;
  2704. *ntevs = ntevs2;
  2705. *tevs2 = NULL;
  2706. return 0;
  2707. }
  2708. if (*ntevs + ntevs2 > probe_conf.max_probes)
  2709. ret = -E2BIG;
  2710. else {
  2711. /* Concatinate the array of probe_trace_event */
  2712. new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
  2713. *tevs2, ntevs2 * sizeof(**tevs2));
  2714. if (!new_tevs)
  2715. ret = -ENOMEM;
  2716. else {
  2717. free(*tevs);
  2718. *tevs = new_tevs;
  2719. *ntevs += ntevs2;
  2720. }
  2721. }
  2722. if (ret < 0)
  2723. clear_probe_trace_events(*tevs2, ntevs2);
  2724. zfree(tevs2);
  2725. return ret;
  2726. }
  2727. /*
  2728. * Try to find probe_trace_event from given probe caches. Return the number
  2729. * of cached events found, if an error occurs return the error.
  2730. */
  2731. static int find_cached_events(struct perf_probe_event *pev,
  2732. struct probe_trace_event **tevs,
  2733. const char *target)
  2734. {
  2735. struct probe_cache *cache;
  2736. struct probe_cache_entry *entry;
  2737. struct probe_trace_event *tmp_tevs = NULL;
  2738. int ntevs = 0;
  2739. int ret = 0;
  2740. cache = probe_cache__new(target, pev->nsi);
  2741. /* Return 0 ("not found") if the target has no probe cache. */
  2742. if (!cache)
  2743. return 0;
  2744. for_each_probe_cache_entry(entry, cache) {
  2745. /* Skip the cache entry which has no name */
  2746. if (!entry->pev.event || !entry->pev.group)
  2747. continue;
  2748. if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
  2749. strglobmatch(entry->pev.event, pev->event)) {
  2750. ret = probe_cache_entry__get_event(entry, &tmp_tevs);
  2751. if (ret > 0)
  2752. ret = concat_probe_trace_events(tevs, &ntevs,
  2753. &tmp_tevs, ret);
  2754. if (ret < 0)
  2755. break;
  2756. }
  2757. }
  2758. probe_cache__delete(cache);
  2759. if (ret < 0) {
  2760. clear_probe_trace_events(*tevs, ntevs);
  2761. zfree(tevs);
  2762. } else {
  2763. ret = ntevs;
  2764. if (ntevs > 0 && target && target[0] == '/')
  2765. pev->uprobes = true;
  2766. }
  2767. return ret;
  2768. }
  2769. /* Try to find probe_trace_event from all probe caches */
  2770. static int find_cached_events_all(struct perf_probe_event *pev,
  2771. struct probe_trace_event **tevs)
  2772. {
  2773. struct probe_trace_event *tmp_tevs = NULL;
  2774. struct strlist *bidlist;
  2775. struct str_node *nd;
  2776. char *pathname;
  2777. int ntevs = 0;
  2778. int ret;
  2779. /* Get the buildid list of all valid caches */
  2780. bidlist = build_id_cache__list_all(true);
  2781. if (!bidlist) {
  2782. ret = -errno;
  2783. pr_debug("Failed to get buildids: %d\n", ret);
  2784. return ret;
  2785. }
  2786. ret = 0;
  2787. strlist__for_each_entry(nd, bidlist) {
  2788. pathname = build_id_cache__origname(nd->s);
  2789. ret = find_cached_events(pev, &tmp_tevs, pathname);
  2790. /* In the case of cnt == 0, we just skip it */
  2791. if (ret > 0)
  2792. ret = concat_probe_trace_events(tevs, &ntevs,
  2793. &tmp_tevs, ret);
  2794. free(pathname);
  2795. if (ret < 0)
  2796. break;
  2797. }
  2798. strlist__delete(bidlist);
  2799. if (ret < 0) {
  2800. clear_probe_trace_events(*tevs, ntevs);
  2801. zfree(tevs);
  2802. } else
  2803. ret = ntevs;
  2804. return ret;
  2805. }
  2806. static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
  2807. struct probe_trace_event **tevs)
  2808. {
  2809. struct probe_cache *cache;
  2810. struct probe_cache_entry *entry;
  2811. struct probe_trace_event *tev;
  2812. struct str_node *node;
  2813. int ret, i;
  2814. if (pev->sdt) {
  2815. /* For SDT/cached events, we use special search functions */
  2816. if (!pev->target)
  2817. return find_cached_events_all(pev, tevs);
  2818. else
  2819. return find_cached_events(pev, tevs, pev->target);
  2820. }
  2821. cache = probe_cache__new(pev->target, pev->nsi);
  2822. if (!cache)
  2823. return 0;
  2824. entry = probe_cache__find(cache, pev);
  2825. if (!entry) {
  2826. /* SDT must be in the cache */
  2827. ret = pev->sdt ? -ENOENT : 0;
  2828. goto out;
  2829. }
  2830. ret = strlist__nr_entries(entry->tevlist);
  2831. if (ret > probe_conf.max_probes) {
  2832. pr_debug("Too many entries matched in the cache of %s\n",
  2833. pev->target ? : "kernel");
  2834. ret = -E2BIG;
  2835. goto out;
  2836. }
  2837. *tevs = zalloc(ret * sizeof(*tev));
  2838. if (!*tevs) {
  2839. ret = -ENOMEM;
  2840. goto out;
  2841. }
  2842. i = 0;
  2843. strlist__for_each_entry(node, entry->tevlist) {
  2844. tev = &(*tevs)[i++];
  2845. ret = parse_probe_trace_command(node->s, tev);
  2846. if (ret < 0)
  2847. goto out;
  2848. /* Set the uprobes attribute as same as original */
  2849. tev->uprobes = pev->uprobes;
  2850. }
  2851. ret = i;
  2852. out:
  2853. probe_cache__delete(cache);
  2854. return ret;
  2855. }
  2856. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  2857. struct probe_trace_event **tevs)
  2858. {
  2859. int ret;
  2860. if (!pev->group && !pev->sdt) {
  2861. /* Set group name if not given */
  2862. if (!pev->uprobes) {
  2863. pev->group = strdup(PERFPROBE_GROUP);
  2864. ret = pev->group ? 0 : -ENOMEM;
  2865. } else
  2866. ret = convert_exec_to_group(pev->target, &pev->group);
  2867. if (ret != 0) {
  2868. pr_warning("Failed to make a group name.\n");
  2869. return ret;
  2870. }
  2871. }
  2872. ret = try_to_find_absolute_address(pev, tevs);
  2873. if (ret > 0)
  2874. return ret;
  2875. /* At first, we need to lookup cache entry */
  2876. ret = find_probe_trace_events_from_cache(pev, tevs);
  2877. if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
  2878. return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
  2879. /* Convert perf_probe_event with debuginfo */
  2880. ret = try_to_find_probe_trace_events(pev, tevs);
  2881. if (ret != 0)
  2882. return ret; /* Found in debuginfo or got an error */
  2883. return find_probe_trace_events_from_map(pev, tevs);
  2884. }
  2885. int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2886. {
  2887. int i, ret;
  2888. /* Loop 1: convert all events */
  2889. for (i = 0; i < npevs; i++) {
  2890. /* Init kprobe blacklist if needed */
  2891. if (!pevs[i].uprobes)
  2892. kprobe_blacklist__init();
  2893. /* Convert with or without debuginfo */
  2894. ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
  2895. if (ret < 0)
  2896. return ret;
  2897. pevs[i].ntevs = ret;
  2898. }
  2899. /* This just release blacklist only if allocated */
  2900. kprobe_blacklist__release();
  2901. return 0;
  2902. }
  2903. static int show_probe_trace_event(struct probe_trace_event *tev)
  2904. {
  2905. char *buf = synthesize_probe_trace_command(tev);
  2906. if (!buf) {
  2907. pr_debug("Failed to synthesize probe trace event.\n");
  2908. return -EINVAL;
  2909. }
  2910. /* Showing definition always go stdout */
  2911. printf("%s\n", buf);
  2912. free(buf);
  2913. return 0;
  2914. }
  2915. int show_probe_trace_events(struct perf_probe_event *pevs, int npevs)
  2916. {
  2917. struct strlist *namelist = strlist__new(NULL, NULL);
  2918. struct probe_trace_event *tev;
  2919. struct perf_probe_event *pev;
  2920. int i, j, ret = 0;
  2921. if (!namelist)
  2922. return -ENOMEM;
  2923. for (j = 0; j < npevs && !ret; j++) {
  2924. pev = &pevs[j];
  2925. for (i = 0; i < pev->ntevs && !ret; i++) {
  2926. tev = &pev->tevs[i];
  2927. /* Skip if the symbol is out of .text or blacklisted */
  2928. if (!tev->point.symbol && !pev->uprobes)
  2929. continue;
  2930. /* Set new name for tev (and update namelist) */
  2931. ret = probe_trace_event__set_name(tev, pev,
  2932. namelist, true);
  2933. if (!ret)
  2934. ret = show_probe_trace_event(tev);
  2935. }
  2936. }
  2937. strlist__delete(namelist);
  2938. return ret;
  2939. }
  2940. int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2941. {
  2942. int i, ret = 0;
  2943. /* Loop 2: add all events */
  2944. for (i = 0; i < npevs; i++) {
  2945. ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
  2946. pevs[i].ntevs,
  2947. probe_conf.force_add);
  2948. if (ret < 0)
  2949. break;
  2950. }
  2951. return ret;
  2952. }
  2953. void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2954. {
  2955. int i, j;
  2956. struct perf_probe_event *pev;
  2957. /* Loop 3: cleanup and free trace events */
  2958. for (i = 0; i < npevs; i++) {
  2959. pev = &pevs[i];
  2960. for (j = 0; j < pevs[i].ntevs; j++)
  2961. clear_probe_trace_event(&pevs[i].tevs[j]);
  2962. zfree(&pevs[i].tevs);
  2963. pevs[i].ntevs = 0;
  2964. nsinfo__zput(pev->nsi);
  2965. clear_perf_probe_event(&pevs[i]);
  2966. }
  2967. }
  2968. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2969. {
  2970. int ret;
  2971. ret = init_probe_symbol_maps(pevs->uprobes);
  2972. if (ret < 0)
  2973. return ret;
  2974. ret = convert_perf_probe_events(pevs, npevs);
  2975. if (ret == 0)
  2976. ret = apply_perf_probe_events(pevs, npevs);
  2977. cleanup_perf_probe_events(pevs, npevs);
  2978. exit_probe_symbol_maps();
  2979. return ret;
  2980. }
  2981. int del_perf_probe_events(struct strfilter *filter)
  2982. {
  2983. int ret, ret2, ufd = -1, kfd = -1;
  2984. char *str = strfilter__string(filter);
  2985. if (!str)
  2986. return -EINVAL;
  2987. /* Get current event names */
  2988. ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
  2989. if (ret < 0)
  2990. goto out;
  2991. ret = probe_file__del_events(kfd, filter);
  2992. if (ret < 0 && ret != -ENOENT)
  2993. goto error;
  2994. ret2 = probe_file__del_events(ufd, filter);
  2995. if (ret2 < 0 && ret2 != -ENOENT) {
  2996. ret = ret2;
  2997. goto error;
  2998. }
  2999. ret = 0;
  3000. error:
  3001. if (kfd >= 0)
  3002. close(kfd);
  3003. if (ufd >= 0)
  3004. close(ufd);
  3005. out:
  3006. free(str);
  3007. return ret;
  3008. }
  3009. int show_available_funcs(const char *target, struct nsinfo *nsi,
  3010. struct strfilter *_filter, bool user)
  3011. {
  3012. struct rb_node *nd;
  3013. struct map *map;
  3014. int ret;
  3015. ret = init_probe_symbol_maps(user);
  3016. if (ret < 0)
  3017. return ret;
  3018. /* Get a symbol map */
  3019. map = get_target_map(target, nsi, user);
  3020. if (!map) {
  3021. pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
  3022. return -EINVAL;
  3023. }
  3024. ret = map__load(map);
  3025. if (ret) {
  3026. if (ret == -2) {
  3027. char *str = strfilter__string(_filter);
  3028. pr_err("Failed to find symbols matched to \"%s\"\n",
  3029. str);
  3030. free(str);
  3031. } else
  3032. pr_err("Failed to load symbols in %s\n",
  3033. (target) ? : "kernel");
  3034. goto end;
  3035. }
  3036. if (!dso__sorted_by_name(map->dso))
  3037. dso__sort_by_name(map->dso);
  3038. /* Show all (filtered) symbols */
  3039. setup_pager();
  3040. for (nd = rb_first(&map->dso->symbol_names); nd; nd = rb_next(nd)) {
  3041. struct symbol_name_rb_node *pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  3042. if (strfilter__compare(_filter, pos->sym.name))
  3043. printf("%s\n", pos->sym.name);
  3044. }
  3045. end:
  3046. map__put(map);
  3047. exit_probe_symbol_maps();
  3048. return ret;
  3049. }
  3050. int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
  3051. struct perf_probe_arg *pvar)
  3052. {
  3053. tvar->value = strdup(pvar->var);
  3054. if (tvar->value == NULL)
  3055. return -ENOMEM;
  3056. if (pvar->type) {
  3057. tvar->type = strdup(pvar->type);
  3058. if (tvar->type == NULL)
  3059. return -ENOMEM;
  3060. }
  3061. if (pvar->name) {
  3062. tvar->name = strdup(pvar->name);
  3063. if (tvar->name == NULL)
  3064. return -ENOMEM;
  3065. } else
  3066. tvar->name = NULL;
  3067. return 0;
  3068. }