probe-event.c 69 KB

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