probe-event.c 77 KB

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