probe-event.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  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 (!str_error_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. str_error_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. str_error_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 [GRP:][EVENT=]SRC[:LN|;PTN]
  1042. * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  1043. */
  1044. if (!arg)
  1045. return -EINVAL;
  1046. ptr = strpbrk(arg, ";=@+%");
  1047. if (ptr && *ptr == '=') { /* Event name */
  1048. *ptr = '\0';
  1049. tmp = ptr + 1;
  1050. ptr = strchr(arg, ':');
  1051. if (ptr) {
  1052. *ptr = '\0';
  1053. if (!is_c_func_name(arg))
  1054. goto not_fname;
  1055. pev->group = strdup(arg);
  1056. if (!pev->group)
  1057. return -ENOMEM;
  1058. arg = ptr + 1;
  1059. } else
  1060. pev->group = NULL;
  1061. if (!is_c_func_name(arg)) {
  1062. not_fname:
  1063. semantic_error("%s is bad for event name -it must "
  1064. "follow C symbol-naming rule.\n", arg);
  1065. return -EINVAL;
  1066. }
  1067. pev->event = strdup(arg);
  1068. if (pev->event == NULL)
  1069. return -ENOMEM;
  1070. arg = tmp;
  1071. }
  1072. /*
  1073. * Check arg is function or file name and copy it.
  1074. *
  1075. * We consider arg to be a file spec if and only if it satisfies
  1076. * all of the below criteria::
  1077. * - it does not include any of "+@%",
  1078. * - it includes one of ":;", and
  1079. * - it has a period '.' in the name.
  1080. *
  1081. * Otherwise, we consider arg to be a function specification.
  1082. */
  1083. if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
  1084. /* This is a file spec if it includes a '.' before ; or : */
  1085. if (memchr(arg, '.', ptr - arg))
  1086. file_spec = true;
  1087. }
  1088. ptr = strpbrk(arg, ";:+@%");
  1089. if (ptr) {
  1090. nc = *ptr;
  1091. *ptr++ = '\0';
  1092. }
  1093. if (arg[0] == '\0')
  1094. tmp = NULL;
  1095. else {
  1096. tmp = strdup(arg);
  1097. if (tmp == NULL)
  1098. return -ENOMEM;
  1099. }
  1100. if (file_spec)
  1101. pp->file = tmp;
  1102. else {
  1103. pp->function = tmp;
  1104. /*
  1105. * Keep pp->function even if this is absolute address,
  1106. * so it can mark whether abs_address is valid.
  1107. * Which make 'perf probe lib.bin 0x0' possible.
  1108. *
  1109. * Note that checking length of tmp is not needed
  1110. * because when we access tmp[1] we know tmp[0] is '0',
  1111. * so tmp[1] should always valid (but could be '\0').
  1112. */
  1113. if (tmp && !strncmp(tmp, "0x", 2)) {
  1114. pp->abs_address = strtoul(pp->function, &tmp, 0);
  1115. if (*tmp != '\0') {
  1116. semantic_error("Invalid absolute address.\n");
  1117. return -EINVAL;
  1118. }
  1119. }
  1120. }
  1121. /* Parse other options */
  1122. while (ptr) {
  1123. arg = ptr;
  1124. c = nc;
  1125. if (c == ';') { /* Lazy pattern must be the last part */
  1126. pp->lazy_line = strdup(arg);
  1127. if (pp->lazy_line == NULL)
  1128. return -ENOMEM;
  1129. break;
  1130. }
  1131. ptr = strpbrk(arg, ";:+@%");
  1132. if (ptr) {
  1133. nc = *ptr;
  1134. *ptr++ = '\0';
  1135. }
  1136. switch (c) {
  1137. case ':': /* Line number */
  1138. pp->line = strtoul(arg, &tmp, 0);
  1139. if (*tmp != '\0') {
  1140. semantic_error("There is non-digit char"
  1141. " in line number.\n");
  1142. return -EINVAL;
  1143. }
  1144. break;
  1145. case '+': /* Byte offset from a symbol */
  1146. pp->offset = strtoul(arg, &tmp, 0);
  1147. if (*tmp != '\0') {
  1148. semantic_error("There is non-digit character"
  1149. " in offset.\n");
  1150. return -EINVAL;
  1151. }
  1152. break;
  1153. case '@': /* File name */
  1154. if (pp->file) {
  1155. semantic_error("SRC@SRC is not allowed.\n");
  1156. return -EINVAL;
  1157. }
  1158. pp->file = strdup(arg);
  1159. if (pp->file == NULL)
  1160. return -ENOMEM;
  1161. break;
  1162. case '%': /* Probe places */
  1163. if (strcmp(arg, "return") == 0) {
  1164. pp->retprobe = 1;
  1165. } else { /* Others not supported yet */
  1166. semantic_error("%%%s is not supported.\n", arg);
  1167. return -ENOTSUP;
  1168. }
  1169. break;
  1170. default: /* Buggy case */
  1171. pr_err("This program has a bug at %s:%d.\n",
  1172. __FILE__, __LINE__);
  1173. return -ENOTSUP;
  1174. break;
  1175. }
  1176. }
  1177. /* Exclusion check */
  1178. if (pp->lazy_line && pp->line) {
  1179. semantic_error("Lazy pattern can't be used with"
  1180. " line number.\n");
  1181. return -EINVAL;
  1182. }
  1183. if (pp->lazy_line && pp->offset) {
  1184. semantic_error("Lazy pattern can't be used with offset.\n");
  1185. return -EINVAL;
  1186. }
  1187. if (pp->line && pp->offset) {
  1188. semantic_error("Offset can't be used with line number.\n");
  1189. return -EINVAL;
  1190. }
  1191. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  1192. semantic_error("File always requires line number or "
  1193. "lazy pattern.\n");
  1194. return -EINVAL;
  1195. }
  1196. if (pp->offset && !pp->function) {
  1197. semantic_error("Offset requires an entry function.\n");
  1198. return -EINVAL;
  1199. }
  1200. if (pp->retprobe && !pp->function) {
  1201. semantic_error("Return probe requires an entry function.\n");
  1202. return -EINVAL;
  1203. }
  1204. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  1205. semantic_error("Offset/Line/Lazy pattern can't be used with "
  1206. "return probe.\n");
  1207. return -EINVAL;
  1208. }
  1209. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  1210. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  1211. pp->lazy_line);
  1212. return 0;
  1213. }
  1214. /* Parse perf-probe event argument */
  1215. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  1216. {
  1217. char *tmp, *goodname;
  1218. struct perf_probe_arg_field **fieldp;
  1219. pr_debug("parsing arg: %s into ", str);
  1220. tmp = strchr(str, '=');
  1221. if (tmp) {
  1222. arg->name = strndup(str, tmp - str);
  1223. if (arg->name == NULL)
  1224. return -ENOMEM;
  1225. pr_debug("name:%s ", arg->name);
  1226. str = tmp + 1;
  1227. }
  1228. tmp = strchr(str, ':');
  1229. if (tmp) { /* Type setting */
  1230. *tmp = '\0';
  1231. arg->type = strdup(tmp + 1);
  1232. if (arg->type == NULL)
  1233. return -ENOMEM;
  1234. pr_debug("type:%s ", arg->type);
  1235. }
  1236. tmp = strpbrk(str, "-.[");
  1237. if (!is_c_varname(str) || !tmp) {
  1238. /* A variable, register, symbol or special value */
  1239. arg->var = strdup(str);
  1240. if (arg->var == NULL)
  1241. return -ENOMEM;
  1242. pr_debug("%s\n", arg->var);
  1243. return 0;
  1244. }
  1245. /* Structure fields or array element */
  1246. arg->var = strndup(str, tmp - str);
  1247. if (arg->var == NULL)
  1248. return -ENOMEM;
  1249. goodname = arg->var;
  1250. pr_debug("%s, ", arg->var);
  1251. fieldp = &arg->field;
  1252. do {
  1253. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  1254. if (*fieldp == NULL)
  1255. return -ENOMEM;
  1256. if (*tmp == '[') { /* Array */
  1257. str = tmp;
  1258. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  1259. (*fieldp)->ref = true;
  1260. if (*tmp != ']' || tmp == str + 1) {
  1261. semantic_error("Array index must be a"
  1262. " number.\n");
  1263. return -EINVAL;
  1264. }
  1265. tmp++;
  1266. if (*tmp == '\0')
  1267. tmp = NULL;
  1268. } else { /* Structure */
  1269. if (*tmp == '.') {
  1270. str = tmp + 1;
  1271. (*fieldp)->ref = false;
  1272. } else if (tmp[1] == '>') {
  1273. str = tmp + 2;
  1274. (*fieldp)->ref = true;
  1275. } else {
  1276. semantic_error("Argument parse error: %s\n",
  1277. str);
  1278. return -EINVAL;
  1279. }
  1280. tmp = strpbrk(str, "-.[");
  1281. }
  1282. if (tmp) {
  1283. (*fieldp)->name = strndup(str, tmp - str);
  1284. if ((*fieldp)->name == NULL)
  1285. return -ENOMEM;
  1286. if (*str != '[')
  1287. goodname = (*fieldp)->name;
  1288. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  1289. fieldp = &(*fieldp)->next;
  1290. }
  1291. } while (tmp);
  1292. (*fieldp)->name = strdup(str);
  1293. if ((*fieldp)->name == NULL)
  1294. return -ENOMEM;
  1295. if (*str != '[')
  1296. goodname = (*fieldp)->name;
  1297. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  1298. /* If no name is specified, set the last field name (not array index)*/
  1299. if (!arg->name) {
  1300. arg->name = strdup(goodname);
  1301. if (arg->name == NULL)
  1302. return -ENOMEM;
  1303. }
  1304. return 0;
  1305. }
  1306. /* Parse perf-probe event command */
  1307. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  1308. {
  1309. char **argv;
  1310. int argc, i, ret = 0;
  1311. argv = argv_split(cmd, &argc);
  1312. if (!argv) {
  1313. pr_debug("Failed to split arguments.\n");
  1314. return -ENOMEM;
  1315. }
  1316. if (argc - 1 > MAX_PROBE_ARGS) {
  1317. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  1318. ret = -ERANGE;
  1319. goto out;
  1320. }
  1321. /* Parse probe point */
  1322. ret = parse_perf_probe_point(argv[0], pev);
  1323. if (ret < 0)
  1324. goto out;
  1325. /* Copy arguments and ensure return probe has no C argument */
  1326. pev->nargs = argc - 1;
  1327. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1328. if (pev->args == NULL) {
  1329. ret = -ENOMEM;
  1330. goto out;
  1331. }
  1332. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  1333. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  1334. if (ret >= 0 &&
  1335. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  1336. semantic_error("You can't specify local variable for"
  1337. " kretprobe.\n");
  1338. ret = -EINVAL;
  1339. }
  1340. }
  1341. out:
  1342. argv_free(argv);
  1343. return ret;
  1344. }
  1345. /* Return true if this perf_probe_event requires debuginfo */
  1346. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  1347. {
  1348. int i;
  1349. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  1350. return true;
  1351. for (i = 0; i < pev->nargs; i++)
  1352. if (is_c_varname(pev->args[i].var))
  1353. return true;
  1354. return false;
  1355. }
  1356. /* Parse probe_events event into struct probe_point */
  1357. int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
  1358. {
  1359. struct probe_trace_point *tp = &tev->point;
  1360. char pr;
  1361. char *p;
  1362. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  1363. int ret, i, argc;
  1364. char **argv;
  1365. pr_debug("Parsing probe_events: %s\n", cmd);
  1366. argv = argv_split(cmd, &argc);
  1367. if (!argv) {
  1368. pr_debug("Failed to split arguments.\n");
  1369. return -ENOMEM;
  1370. }
  1371. if (argc < 2) {
  1372. semantic_error("Too few probe arguments.\n");
  1373. ret = -ERANGE;
  1374. goto out;
  1375. }
  1376. /* Scan event and group name. */
  1377. argv0_str = strdup(argv[0]);
  1378. if (argv0_str == NULL) {
  1379. ret = -ENOMEM;
  1380. goto out;
  1381. }
  1382. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  1383. fmt2_str = strtok_r(NULL, "/", &fmt);
  1384. fmt3_str = strtok_r(NULL, " \t", &fmt);
  1385. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  1386. || fmt3_str == NULL) {
  1387. semantic_error("Failed to parse event name: %s\n", argv[0]);
  1388. ret = -EINVAL;
  1389. goto out;
  1390. }
  1391. pr = fmt1_str[0];
  1392. tev->group = strdup(fmt2_str);
  1393. tev->event = strdup(fmt3_str);
  1394. if (tev->group == NULL || tev->event == NULL) {
  1395. ret = -ENOMEM;
  1396. goto out;
  1397. }
  1398. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1399. tp->retprobe = (pr == 'r');
  1400. /* Scan module name(if there), function name and offset */
  1401. p = strchr(argv[1], ':');
  1402. if (p) {
  1403. tp->module = strndup(argv[1], p - argv[1]);
  1404. if (!tp->module) {
  1405. ret = -ENOMEM;
  1406. goto out;
  1407. }
  1408. p++;
  1409. } else
  1410. p = argv[1];
  1411. fmt1_str = strtok_r(p, "+", &fmt);
  1412. /* only the address started with 0x */
  1413. if (fmt1_str[0] == '0') {
  1414. /*
  1415. * Fix a special case:
  1416. * if address == 0, kernel reports something like:
  1417. * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
  1418. * Newer kernel may fix that, but we want to
  1419. * support old kernel also.
  1420. */
  1421. if (strcmp(fmt1_str, "0x") == 0) {
  1422. if (!argv[2] || strcmp(argv[2], "(null)")) {
  1423. ret = -EINVAL;
  1424. goto out;
  1425. }
  1426. tp->address = 0;
  1427. free(argv[2]);
  1428. for (i = 2; argv[i + 1] != NULL; i++)
  1429. argv[i] = argv[i + 1];
  1430. argv[i] = NULL;
  1431. argc -= 1;
  1432. } else
  1433. tp->address = strtoul(fmt1_str, NULL, 0);
  1434. } else {
  1435. /* Only the symbol-based probe has offset */
  1436. tp->symbol = strdup(fmt1_str);
  1437. if (tp->symbol == NULL) {
  1438. ret = -ENOMEM;
  1439. goto out;
  1440. }
  1441. fmt2_str = strtok_r(NULL, "", &fmt);
  1442. if (fmt2_str == NULL)
  1443. tp->offset = 0;
  1444. else
  1445. tp->offset = strtoul(fmt2_str, NULL, 10);
  1446. }
  1447. tev->nargs = argc - 2;
  1448. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1449. if (tev->args == NULL) {
  1450. ret = -ENOMEM;
  1451. goto out;
  1452. }
  1453. for (i = 0; i < tev->nargs; i++) {
  1454. p = strchr(argv[i + 2], '=');
  1455. if (p) /* We don't need which register is assigned. */
  1456. *p++ = '\0';
  1457. else
  1458. p = argv[i + 2];
  1459. tev->args[i].name = strdup(argv[i + 2]);
  1460. /* TODO: parse regs and offset */
  1461. tev->args[i].value = strdup(p);
  1462. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1463. ret = -ENOMEM;
  1464. goto out;
  1465. }
  1466. }
  1467. ret = 0;
  1468. out:
  1469. free(argv0_str);
  1470. argv_free(argv);
  1471. return ret;
  1472. }
  1473. /* Compose only probe arg */
  1474. char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
  1475. {
  1476. struct perf_probe_arg_field *field = pa->field;
  1477. struct strbuf buf;
  1478. char *ret = NULL;
  1479. int err;
  1480. if (strbuf_init(&buf, 64) < 0)
  1481. return NULL;
  1482. if (pa->name && pa->var)
  1483. err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
  1484. else
  1485. err = strbuf_addstr(&buf, pa->name ?: pa->var);
  1486. if (err)
  1487. goto out;
  1488. while (field) {
  1489. if (field->name[0] == '[')
  1490. err = strbuf_addstr(&buf, field->name);
  1491. else
  1492. err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
  1493. field->name);
  1494. field = field->next;
  1495. if (err)
  1496. goto out;
  1497. }
  1498. if (pa->type)
  1499. if (strbuf_addf(&buf, ":%s", pa->type) < 0)
  1500. goto out;
  1501. ret = strbuf_detach(&buf, NULL);
  1502. out:
  1503. strbuf_release(&buf);
  1504. return ret;
  1505. }
  1506. /* Compose only probe point (not argument) */
  1507. char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1508. {
  1509. struct strbuf buf;
  1510. char *tmp, *ret = NULL;
  1511. int len, err = 0;
  1512. if (strbuf_init(&buf, 64) < 0)
  1513. return NULL;
  1514. if (pp->function) {
  1515. if (strbuf_addstr(&buf, pp->function) < 0)
  1516. goto out;
  1517. if (pp->offset)
  1518. err = strbuf_addf(&buf, "+%lu", pp->offset);
  1519. else if (pp->line)
  1520. err = strbuf_addf(&buf, ":%d", pp->line);
  1521. else if (pp->retprobe)
  1522. err = strbuf_addstr(&buf, "%return");
  1523. if (err)
  1524. goto out;
  1525. }
  1526. if (pp->file) {
  1527. tmp = pp->file;
  1528. len = strlen(tmp);
  1529. if (len > 30) {
  1530. tmp = strchr(pp->file + len - 30, '/');
  1531. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1532. }
  1533. err = strbuf_addf(&buf, "@%s", tmp);
  1534. if (!err && !pp->function && pp->line)
  1535. err = strbuf_addf(&buf, ":%d", pp->line);
  1536. }
  1537. if (!err)
  1538. ret = strbuf_detach(&buf, NULL);
  1539. out:
  1540. strbuf_release(&buf);
  1541. return ret;
  1542. }
  1543. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1544. {
  1545. struct strbuf buf;
  1546. char *tmp, *ret = NULL;
  1547. int i;
  1548. if (strbuf_init(&buf, 64))
  1549. return NULL;
  1550. if (pev->event)
  1551. if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
  1552. pev->event) < 0)
  1553. goto out;
  1554. tmp = synthesize_perf_probe_point(&pev->point);
  1555. if (!tmp || strbuf_addstr(&buf, tmp) < 0)
  1556. goto out;
  1557. free(tmp);
  1558. for (i = 0; i < pev->nargs; i++) {
  1559. tmp = synthesize_perf_probe_arg(pev->args + i);
  1560. if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
  1561. goto out;
  1562. free(tmp);
  1563. }
  1564. ret = strbuf_detach(&buf, NULL);
  1565. out:
  1566. strbuf_release(&buf);
  1567. return ret;
  1568. }
  1569. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1570. struct strbuf *buf, int depth)
  1571. {
  1572. int err;
  1573. if (ref->next) {
  1574. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1575. depth + 1);
  1576. if (depth < 0)
  1577. return depth;
  1578. }
  1579. err = strbuf_addf(buf, "%+ld(", ref->offset);
  1580. return (err < 0) ? err : depth;
  1581. }
  1582. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1583. struct strbuf *buf)
  1584. {
  1585. struct probe_trace_arg_ref *ref = arg->ref;
  1586. int depth = 0, err;
  1587. /* Argument name or separator */
  1588. if (arg->name)
  1589. err = strbuf_addf(buf, " %s=", arg->name);
  1590. else
  1591. err = strbuf_addch(buf, ' ');
  1592. if (err)
  1593. return err;
  1594. /* Special case: @XXX */
  1595. if (arg->value[0] == '@' && arg->ref)
  1596. ref = ref->next;
  1597. /* Dereferencing arguments */
  1598. if (ref) {
  1599. depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
  1600. if (depth < 0)
  1601. return depth;
  1602. }
  1603. /* Print argument value */
  1604. if (arg->value[0] == '@' && arg->ref)
  1605. err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
  1606. else
  1607. err = strbuf_addstr(buf, arg->value);
  1608. /* Closing */
  1609. while (!err && depth--)
  1610. err = strbuf_addch(buf, ')');
  1611. /* Print argument type */
  1612. if (!err && arg->type)
  1613. err = strbuf_addf(buf, ":%s", arg->type);
  1614. return err;
  1615. }
  1616. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1617. {
  1618. struct probe_trace_point *tp = &tev->point;
  1619. struct strbuf buf;
  1620. char *ret = NULL;
  1621. int i, err;
  1622. /* Uprobes must have tp->module */
  1623. if (tev->uprobes && !tp->module)
  1624. return NULL;
  1625. if (strbuf_init(&buf, 32) < 0)
  1626. return NULL;
  1627. if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
  1628. tev->group, tev->event) < 0)
  1629. goto error;
  1630. /*
  1631. * If tp->address == 0, then this point must be a
  1632. * absolute address uprobe.
  1633. * try_to_find_absolute_address() should have made
  1634. * tp->symbol to "0x0".
  1635. */
  1636. if (tev->uprobes && !tp->address) {
  1637. if (!tp->symbol || strcmp(tp->symbol, "0x0"))
  1638. goto error;
  1639. }
  1640. /* Use the tp->address for uprobes */
  1641. if (tev->uprobes)
  1642. err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
  1643. else if (!strncmp(tp->symbol, "0x", 2))
  1644. /* Absolute address. See try_to_find_absolute_address() */
  1645. err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
  1646. tp->module ? ":" : "", tp->address);
  1647. else
  1648. err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
  1649. tp->module ? ":" : "", tp->symbol, tp->offset);
  1650. if (err)
  1651. goto error;
  1652. for (i = 0; i < tev->nargs; i++)
  1653. if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
  1654. goto error;
  1655. ret = strbuf_detach(&buf, NULL);
  1656. error:
  1657. strbuf_release(&buf);
  1658. return ret;
  1659. }
  1660. static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
  1661. struct perf_probe_point *pp,
  1662. bool is_kprobe)
  1663. {
  1664. struct symbol *sym = NULL;
  1665. struct map *map;
  1666. u64 addr = tp->address;
  1667. int ret = -ENOENT;
  1668. if (!is_kprobe) {
  1669. map = dso__new_map(tp->module);
  1670. if (!map)
  1671. goto out;
  1672. sym = map__find_symbol(map, addr, NULL);
  1673. } else {
  1674. if (tp->symbol && !addr) {
  1675. if (kernel_get_symbol_address_by_name(tp->symbol,
  1676. &addr, true, false) < 0)
  1677. goto out;
  1678. }
  1679. if (addr) {
  1680. addr += tp->offset;
  1681. sym = __find_kernel_function(addr, &map);
  1682. }
  1683. }
  1684. if (!sym)
  1685. goto out;
  1686. pp->retprobe = tp->retprobe;
  1687. pp->offset = addr - map->unmap_ip(map, sym->start);
  1688. pp->function = strdup(sym->name);
  1689. ret = pp->function ? 0 : -ENOMEM;
  1690. out:
  1691. if (map && !is_kprobe) {
  1692. map__put(map);
  1693. }
  1694. return ret;
  1695. }
  1696. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  1697. struct perf_probe_point *pp,
  1698. bool is_kprobe)
  1699. {
  1700. char buf[128];
  1701. int ret;
  1702. ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
  1703. if (!ret)
  1704. return 0;
  1705. ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
  1706. if (!ret)
  1707. return 0;
  1708. pr_debug("Failed to find probe point from both of dwarf and map.\n");
  1709. if (tp->symbol) {
  1710. pp->function = strdup(tp->symbol);
  1711. pp->offset = tp->offset;
  1712. } else {
  1713. ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
  1714. if (ret < 0)
  1715. return ret;
  1716. pp->function = strdup(buf);
  1717. pp->offset = 0;
  1718. }
  1719. if (pp->function == NULL)
  1720. return -ENOMEM;
  1721. pp->retprobe = tp->retprobe;
  1722. return 0;
  1723. }
  1724. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1725. struct perf_probe_event *pev, bool is_kprobe)
  1726. {
  1727. struct strbuf buf = STRBUF_INIT;
  1728. int i, ret;
  1729. /* Convert event/group name */
  1730. pev->event = strdup(tev->event);
  1731. pev->group = strdup(tev->group);
  1732. if (pev->event == NULL || pev->group == NULL)
  1733. return -ENOMEM;
  1734. /* Convert trace_point to probe_point */
  1735. ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
  1736. if (ret < 0)
  1737. return ret;
  1738. /* Convert trace_arg to probe_arg */
  1739. pev->nargs = tev->nargs;
  1740. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1741. if (pev->args == NULL)
  1742. return -ENOMEM;
  1743. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1744. if (tev->args[i].name)
  1745. pev->args[i].name = strdup(tev->args[i].name);
  1746. else {
  1747. if ((ret = strbuf_init(&buf, 32)) < 0)
  1748. goto error;
  1749. ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
  1750. pev->args[i].name = strbuf_detach(&buf, NULL);
  1751. }
  1752. if (pev->args[i].name == NULL && ret >= 0)
  1753. ret = -ENOMEM;
  1754. }
  1755. error:
  1756. if (ret < 0)
  1757. clear_perf_probe_event(pev);
  1758. return ret;
  1759. }
  1760. void clear_perf_probe_event(struct perf_probe_event *pev)
  1761. {
  1762. struct perf_probe_arg_field *field, *next;
  1763. int i;
  1764. free(pev->event);
  1765. free(pev->group);
  1766. free(pev->target);
  1767. clear_perf_probe_point(&pev->point);
  1768. for (i = 0; i < pev->nargs; i++) {
  1769. free(pev->args[i].name);
  1770. free(pev->args[i].var);
  1771. free(pev->args[i].type);
  1772. field = pev->args[i].field;
  1773. while (field) {
  1774. next = field->next;
  1775. zfree(&field->name);
  1776. free(field);
  1777. field = next;
  1778. }
  1779. }
  1780. free(pev->args);
  1781. memset(pev, 0, sizeof(*pev));
  1782. }
  1783. #define strdup_or_goto(str, label) \
  1784. ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
  1785. static int perf_probe_point__copy(struct perf_probe_point *dst,
  1786. struct perf_probe_point *src)
  1787. {
  1788. dst->file = strdup_or_goto(src->file, out_err);
  1789. dst->function = strdup_or_goto(src->function, out_err);
  1790. dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
  1791. dst->line = src->line;
  1792. dst->retprobe = src->retprobe;
  1793. dst->offset = src->offset;
  1794. return 0;
  1795. out_err:
  1796. clear_perf_probe_point(dst);
  1797. return -ENOMEM;
  1798. }
  1799. static int perf_probe_arg__copy(struct perf_probe_arg *dst,
  1800. struct perf_probe_arg *src)
  1801. {
  1802. struct perf_probe_arg_field *field, **ppfield;
  1803. dst->name = strdup_or_goto(src->name, out_err);
  1804. dst->var = strdup_or_goto(src->var, out_err);
  1805. dst->type = strdup_or_goto(src->type, out_err);
  1806. field = src->field;
  1807. ppfield = &(dst->field);
  1808. while (field) {
  1809. *ppfield = zalloc(sizeof(*field));
  1810. if (!*ppfield)
  1811. goto out_err;
  1812. (*ppfield)->name = strdup_or_goto(field->name, out_err);
  1813. (*ppfield)->index = field->index;
  1814. (*ppfield)->ref = field->ref;
  1815. field = field->next;
  1816. ppfield = &((*ppfield)->next);
  1817. }
  1818. return 0;
  1819. out_err:
  1820. return -ENOMEM;
  1821. }
  1822. int perf_probe_event__copy(struct perf_probe_event *dst,
  1823. struct perf_probe_event *src)
  1824. {
  1825. int i;
  1826. dst->event = strdup_or_goto(src->event, out_err);
  1827. dst->group = strdup_or_goto(src->group, out_err);
  1828. dst->target = strdup_or_goto(src->target, out_err);
  1829. dst->uprobes = src->uprobes;
  1830. if (perf_probe_point__copy(&dst->point, &src->point) < 0)
  1831. goto out_err;
  1832. dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
  1833. if (!dst->args)
  1834. goto out_err;
  1835. dst->nargs = src->nargs;
  1836. for (i = 0; i < src->nargs; i++)
  1837. if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
  1838. goto out_err;
  1839. return 0;
  1840. out_err:
  1841. clear_perf_probe_event(dst);
  1842. return -ENOMEM;
  1843. }
  1844. void clear_probe_trace_event(struct probe_trace_event *tev)
  1845. {
  1846. struct probe_trace_arg_ref *ref, *next;
  1847. int i;
  1848. free(tev->event);
  1849. free(tev->group);
  1850. free(tev->point.symbol);
  1851. free(tev->point.realname);
  1852. free(tev->point.module);
  1853. for (i = 0; i < tev->nargs; i++) {
  1854. free(tev->args[i].name);
  1855. free(tev->args[i].value);
  1856. free(tev->args[i].type);
  1857. ref = tev->args[i].ref;
  1858. while (ref) {
  1859. next = ref->next;
  1860. free(ref);
  1861. ref = next;
  1862. }
  1863. }
  1864. free(tev->args);
  1865. memset(tev, 0, sizeof(*tev));
  1866. }
  1867. struct kprobe_blacklist_node {
  1868. struct list_head list;
  1869. unsigned long start;
  1870. unsigned long end;
  1871. char *symbol;
  1872. };
  1873. static void kprobe_blacklist__delete(struct list_head *blacklist)
  1874. {
  1875. struct kprobe_blacklist_node *node;
  1876. while (!list_empty(blacklist)) {
  1877. node = list_first_entry(blacklist,
  1878. struct kprobe_blacklist_node, list);
  1879. list_del(&node->list);
  1880. free(node->symbol);
  1881. free(node);
  1882. }
  1883. }
  1884. static int kprobe_blacklist__load(struct list_head *blacklist)
  1885. {
  1886. struct kprobe_blacklist_node *node;
  1887. const char *__debugfs = debugfs__mountpoint();
  1888. char buf[PATH_MAX], *p;
  1889. FILE *fp;
  1890. int ret;
  1891. if (__debugfs == NULL)
  1892. return -ENOTSUP;
  1893. ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
  1894. if (ret < 0)
  1895. return ret;
  1896. fp = fopen(buf, "r");
  1897. if (!fp)
  1898. return -errno;
  1899. ret = 0;
  1900. while (fgets(buf, PATH_MAX, fp)) {
  1901. node = zalloc(sizeof(*node));
  1902. if (!node) {
  1903. ret = -ENOMEM;
  1904. break;
  1905. }
  1906. INIT_LIST_HEAD(&node->list);
  1907. list_add_tail(&node->list, blacklist);
  1908. if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
  1909. ret = -EINVAL;
  1910. break;
  1911. }
  1912. p = strchr(buf, '\t');
  1913. if (p) {
  1914. p++;
  1915. if (p[strlen(p) - 1] == '\n')
  1916. p[strlen(p) - 1] = '\0';
  1917. } else
  1918. p = (char *)"unknown";
  1919. node->symbol = strdup(p);
  1920. if (!node->symbol) {
  1921. ret = -ENOMEM;
  1922. break;
  1923. }
  1924. pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
  1925. node->start, node->end, node->symbol);
  1926. ret++;
  1927. }
  1928. if (ret < 0)
  1929. kprobe_blacklist__delete(blacklist);
  1930. fclose(fp);
  1931. return ret;
  1932. }
  1933. static struct kprobe_blacklist_node *
  1934. kprobe_blacklist__find_by_address(struct list_head *blacklist,
  1935. unsigned long address)
  1936. {
  1937. struct kprobe_blacklist_node *node;
  1938. list_for_each_entry(node, blacklist, list) {
  1939. if (node->start <= address && address <= node->end)
  1940. return node;
  1941. }
  1942. return NULL;
  1943. }
  1944. static LIST_HEAD(kprobe_blacklist);
  1945. static void kprobe_blacklist__init(void)
  1946. {
  1947. if (!list_empty(&kprobe_blacklist))
  1948. return;
  1949. if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
  1950. pr_debug("No kprobe blacklist support, ignored\n");
  1951. }
  1952. static void kprobe_blacklist__release(void)
  1953. {
  1954. kprobe_blacklist__delete(&kprobe_blacklist);
  1955. }
  1956. static bool kprobe_blacklist__listed(unsigned long address)
  1957. {
  1958. return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
  1959. }
  1960. static int perf_probe_event__sprintf(const char *group, const char *event,
  1961. struct perf_probe_event *pev,
  1962. const char *module,
  1963. struct strbuf *result)
  1964. {
  1965. int i, ret;
  1966. char *buf;
  1967. if (asprintf(&buf, "%s:%s", group, event) < 0)
  1968. return -errno;
  1969. ret = strbuf_addf(result, " %-20s (on ", buf);
  1970. free(buf);
  1971. if (ret)
  1972. return ret;
  1973. /* Synthesize only event probe point */
  1974. buf = synthesize_perf_probe_point(&pev->point);
  1975. if (!buf)
  1976. return -ENOMEM;
  1977. ret = strbuf_addstr(result, buf);
  1978. free(buf);
  1979. if (!ret && module)
  1980. ret = strbuf_addf(result, " in %s", module);
  1981. if (!ret && pev->nargs > 0) {
  1982. ret = strbuf_add(result, " with", 5);
  1983. for (i = 0; !ret && i < pev->nargs; i++) {
  1984. buf = synthesize_perf_probe_arg(&pev->args[i]);
  1985. if (!buf)
  1986. return -ENOMEM;
  1987. ret = strbuf_addf(result, " %s", buf);
  1988. free(buf);
  1989. }
  1990. }
  1991. if (!ret)
  1992. ret = strbuf_addch(result, ')');
  1993. return ret;
  1994. }
  1995. /* Show an event */
  1996. int show_perf_probe_event(const char *group, const char *event,
  1997. struct perf_probe_event *pev,
  1998. const char *module, bool use_stdout)
  1999. {
  2000. struct strbuf buf = STRBUF_INIT;
  2001. int ret;
  2002. ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
  2003. if (ret >= 0) {
  2004. if (use_stdout)
  2005. printf("%s\n", buf.buf);
  2006. else
  2007. pr_info("%s\n", buf.buf);
  2008. }
  2009. strbuf_release(&buf);
  2010. return ret;
  2011. }
  2012. static bool filter_probe_trace_event(struct probe_trace_event *tev,
  2013. struct strfilter *filter)
  2014. {
  2015. char tmp[128];
  2016. /* At first, check the event name itself */
  2017. if (strfilter__compare(filter, tev->event))
  2018. return true;
  2019. /* Next, check the combination of name and group */
  2020. if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
  2021. return false;
  2022. return strfilter__compare(filter, tmp);
  2023. }
  2024. static int __show_perf_probe_events(int fd, bool is_kprobe,
  2025. struct strfilter *filter)
  2026. {
  2027. int ret = 0;
  2028. struct probe_trace_event tev;
  2029. struct perf_probe_event pev;
  2030. struct strlist *rawlist;
  2031. struct str_node *ent;
  2032. memset(&tev, 0, sizeof(tev));
  2033. memset(&pev, 0, sizeof(pev));
  2034. rawlist = probe_file__get_rawlist(fd);
  2035. if (!rawlist)
  2036. return -ENOMEM;
  2037. strlist__for_each_entry(ent, rawlist) {
  2038. ret = parse_probe_trace_command(ent->s, &tev);
  2039. if (ret >= 0) {
  2040. if (!filter_probe_trace_event(&tev, filter))
  2041. goto next;
  2042. ret = convert_to_perf_probe_event(&tev, &pev,
  2043. is_kprobe);
  2044. if (ret < 0)
  2045. goto next;
  2046. ret = show_perf_probe_event(pev.group, pev.event,
  2047. &pev, tev.point.module,
  2048. true);
  2049. }
  2050. next:
  2051. clear_perf_probe_event(&pev);
  2052. clear_probe_trace_event(&tev);
  2053. if (ret < 0)
  2054. break;
  2055. }
  2056. strlist__delete(rawlist);
  2057. /* Cleanup cached debuginfo if needed */
  2058. debuginfo_cache__exit();
  2059. return ret;
  2060. }
  2061. /* List up current perf-probe events */
  2062. int show_perf_probe_events(struct strfilter *filter)
  2063. {
  2064. int kp_fd, up_fd, ret;
  2065. setup_pager();
  2066. if (probe_conf.cache)
  2067. return probe_cache__show_all_caches(filter);
  2068. ret = init_probe_symbol_maps(false);
  2069. if (ret < 0)
  2070. return ret;
  2071. ret = probe_file__open_both(&kp_fd, &up_fd, 0);
  2072. if (ret < 0)
  2073. return ret;
  2074. if (kp_fd >= 0)
  2075. ret = __show_perf_probe_events(kp_fd, true, filter);
  2076. if (up_fd >= 0 && ret >= 0)
  2077. ret = __show_perf_probe_events(up_fd, false, filter);
  2078. if (kp_fd > 0)
  2079. close(kp_fd);
  2080. if (up_fd > 0)
  2081. close(up_fd);
  2082. exit_probe_symbol_maps();
  2083. return ret;
  2084. }
  2085. static int get_new_event_name(char *buf, size_t len, const char *base,
  2086. struct strlist *namelist, bool allow_suffix)
  2087. {
  2088. int i, ret;
  2089. char *p, *nbase;
  2090. if (*base == '.')
  2091. base++;
  2092. nbase = strdup(base);
  2093. if (!nbase)
  2094. return -ENOMEM;
  2095. /* Cut off the dot suffixes (e.g. .const, .isra)*/
  2096. p = strchr(nbase, '.');
  2097. if (p && p != nbase)
  2098. *p = '\0';
  2099. /* Try no suffix number */
  2100. ret = e_snprintf(buf, len, "%s", nbase);
  2101. if (ret < 0) {
  2102. pr_debug("snprintf() failed: %d\n", ret);
  2103. goto out;
  2104. }
  2105. if (!strlist__has_entry(namelist, buf))
  2106. goto out;
  2107. if (!allow_suffix) {
  2108. pr_warning("Error: event \"%s\" already exists.\n"
  2109. " Hint: Remove existing event by 'perf probe -d'\n"
  2110. " or force duplicates by 'perf probe -f'\n"
  2111. " or set 'force=yes' in BPF source.\n",
  2112. buf);
  2113. ret = -EEXIST;
  2114. goto out;
  2115. }
  2116. /* Try to add suffix */
  2117. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  2118. ret = e_snprintf(buf, len, "%s_%d", nbase, i);
  2119. if (ret < 0) {
  2120. pr_debug("snprintf() failed: %d\n", ret);
  2121. goto out;
  2122. }
  2123. if (!strlist__has_entry(namelist, buf))
  2124. break;
  2125. }
  2126. if (i == MAX_EVENT_INDEX) {
  2127. pr_warning("Too many events are on the same function.\n");
  2128. ret = -ERANGE;
  2129. }
  2130. out:
  2131. free(nbase);
  2132. return ret;
  2133. }
  2134. /* Warn if the current kernel's uprobe implementation is old */
  2135. static void warn_uprobe_event_compat(struct probe_trace_event *tev)
  2136. {
  2137. int i;
  2138. char *buf = synthesize_probe_trace_command(tev);
  2139. /* Old uprobe event doesn't support memory dereference */
  2140. if (!tev->uprobes || tev->nargs == 0 || !buf)
  2141. goto out;
  2142. for (i = 0; i < tev->nargs; i++)
  2143. if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
  2144. pr_warning("Please upgrade your kernel to at least "
  2145. "3.14 to have access to feature %s\n",
  2146. tev->args[i].value);
  2147. break;
  2148. }
  2149. out:
  2150. free(buf);
  2151. }
  2152. /* Set new name from original perf_probe_event and namelist */
  2153. static int probe_trace_event__set_name(struct probe_trace_event *tev,
  2154. struct perf_probe_event *pev,
  2155. struct strlist *namelist,
  2156. bool allow_suffix)
  2157. {
  2158. const char *event, *group;
  2159. char buf[64];
  2160. int ret;
  2161. /* If probe_event or trace_event already have the name, reuse it */
  2162. if (pev->event)
  2163. event = pev->event;
  2164. else if (tev->event)
  2165. event = tev->event;
  2166. else {
  2167. /* Or generate new one from probe point */
  2168. if (pev->point.function &&
  2169. (strncmp(pev->point.function, "0x", 2) != 0) &&
  2170. !strisglob(pev->point.function))
  2171. event = pev->point.function;
  2172. else
  2173. event = tev->point.realname;
  2174. }
  2175. if (pev->group)
  2176. group = pev->group;
  2177. else if (tev->group)
  2178. group = tev->group;
  2179. else
  2180. group = PERFPROBE_GROUP;
  2181. /* Get an unused new event name */
  2182. ret = get_new_event_name(buf, 64, event,
  2183. namelist, allow_suffix);
  2184. if (ret < 0)
  2185. return ret;
  2186. event = buf;
  2187. tev->event = strdup(event);
  2188. tev->group = strdup(group);
  2189. if (tev->event == NULL || tev->group == NULL)
  2190. return -ENOMEM;
  2191. /* Add added event name to namelist */
  2192. strlist__add(namelist, event);
  2193. return 0;
  2194. }
  2195. static int __add_probe_trace_events(struct perf_probe_event *pev,
  2196. struct probe_trace_event *tevs,
  2197. int ntevs, bool allow_suffix)
  2198. {
  2199. int i, fd, ret;
  2200. struct probe_trace_event *tev = NULL;
  2201. struct probe_cache *cache = NULL;
  2202. struct strlist *namelist;
  2203. fd = probe_file__open(PF_FL_RW | (pev->uprobes ? PF_FL_UPROBE : 0));
  2204. if (fd < 0)
  2205. return fd;
  2206. /* Get current event names */
  2207. namelist = probe_file__get_namelist(fd);
  2208. if (!namelist) {
  2209. pr_debug("Failed to get current event list.\n");
  2210. ret = -ENOMEM;
  2211. goto close_out;
  2212. }
  2213. ret = 0;
  2214. for (i = 0; i < ntevs; i++) {
  2215. tev = &tevs[i];
  2216. /* Skip if the symbol is out of .text or blacklisted */
  2217. if (!tev->point.symbol && !pev->uprobes)
  2218. continue;
  2219. /* Set new name for tev (and update namelist) */
  2220. ret = probe_trace_event__set_name(tev, pev, namelist,
  2221. allow_suffix);
  2222. if (ret < 0)
  2223. break;
  2224. ret = probe_file__add_event(fd, tev);
  2225. if (ret < 0)
  2226. break;
  2227. /*
  2228. * Probes after the first probe which comes from same
  2229. * user input are always allowed to add suffix, because
  2230. * there might be several addresses corresponding to
  2231. * one code line.
  2232. */
  2233. allow_suffix = true;
  2234. }
  2235. if (ret == -EINVAL && pev->uprobes)
  2236. warn_uprobe_event_compat(tev);
  2237. if (ret == 0 && probe_conf.cache) {
  2238. cache = probe_cache__new(pev->target);
  2239. if (!cache ||
  2240. probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
  2241. probe_cache__commit(cache) < 0)
  2242. pr_warning("Failed to add event to probe cache\n");
  2243. probe_cache__delete(cache);
  2244. }
  2245. strlist__delete(namelist);
  2246. close_out:
  2247. close(fd);
  2248. return ret;
  2249. }
  2250. static int find_probe_functions(struct map *map, char *name,
  2251. struct symbol **syms)
  2252. {
  2253. int found = 0;
  2254. struct symbol *sym;
  2255. struct rb_node *tmp;
  2256. if (map__load(map, NULL) < 0)
  2257. return 0;
  2258. map__for_each_symbol(map, sym, tmp) {
  2259. if (strglobmatch(sym->name, name)) {
  2260. found++;
  2261. if (syms && found < probe_conf.max_probes)
  2262. syms[found - 1] = sym;
  2263. }
  2264. }
  2265. return found;
  2266. }
  2267. void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
  2268. struct probe_trace_event *tev __maybe_unused,
  2269. struct map *map __maybe_unused,
  2270. struct symbol *sym __maybe_unused) { }
  2271. /*
  2272. * Find probe function addresses from map.
  2273. * Return an error or the number of found probe_trace_event
  2274. */
  2275. static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
  2276. struct probe_trace_event **tevs)
  2277. {
  2278. struct map *map = NULL;
  2279. struct ref_reloc_sym *reloc_sym = NULL;
  2280. struct symbol *sym;
  2281. struct symbol **syms = NULL;
  2282. struct probe_trace_event *tev;
  2283. struct perf_probe_point *pp = &pev->point;
  2284. struct probe_trace_point *tp;
  2285. int num_matched_functions;
  2286. int ret, i, j, skipped = 0;
  2287. char *mod_name;
  2288. map = get_target_map(pev->target, pev->uprobes);
  2289. if (!map) {
  2290. ret = -EINVAL;
  2291. goto out;
  2292. }
  2293. syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
  2294. if (!syms) {
  2295. ret = -ENOMEM;
  2296. goto out;
  2297. }
  2298. /*
  2299. * Load matched symbols: Since the different local symbols may have
  2300. * same name but different addresses, this lists all the symbols.
  2301. */
  2302. num_matched_functions = find_probe_functions(map, pp->function, syms);
  2303. if (num_matched_functions == 0) {
  2304. pr_err("Failed to find symbol %s in %s\n", pp->function,
  2305. pev->target ? : "kernel");
  2306. ret = -ENOENT;
  2307. goto out;
  2308. } else if (num_matched_functions > probe_conf.max_probes) {
  2309. pr_err("Too many functions matched in %s\n",
  2310. pev->target ? : "kernel");
  2311. ret = -E2BIG;
  2312. goto out;
  2313. }
  2314. /* Note that the symbols in the kmodule are not relocated */
  2315. if (!pev->uprobes && !pp->retprobe && !pev->target) {
  2316. reloc_sym = kernel_get_ref_reloc_sym();
  2317. if (!reloc_sym) {
  2318. pr_warning("Relocated base symbol is not found!\n");
  2319. ret = -EINVAL;
  2320. goto out;
  2321. }
  2322. }
  2323. /* Setup result trace-probe-events */
  2324. *tevs = zalloc(sizeof(*tev) * num_matched_functions);
  2325. if (!*tevs) {
  2326. ret = -ENOMEM;
  2327. goto out;
  2328. }
  2329. ret = 0;
  2330. for (j = 0; j < num_matched_functions; j++) {
  2331. sym = syms[j];
  2332. tev = (*tevs) + ret;
  2333. tp = &tev->point;
  2334. if (ret == num_matched_functions) {
  2335. pr_warning("Too many symbols are listed. Skip it.\n");
  2336. break;
  2337. }
  2338. ret++;
  2339. if (pp->offset > sym->end - sym->start) {
  2340. pr_warning("Offset %ld is bigger than the size of %s\n",
  2341. pp->offset, sym->name);
  2342. ret = -ENOENT;
  2343. goto err_out;
  2344. }
  2345. /* Add one probe point */
  2346. tp->address = map->unmap_ip(map, sym->start) + pp->offset;
  2347. /* Check the kprobe (not in module) is within .text */
  2348. if (!pev->uprobes && !pev->target &&
  2349. kprobe_warn_out_range(sym->name, tp->address)) {
  2350. tp->symbol = NULL; /* Skip it */
  2351. skipped++;
  2352. } else if (reloc_sym) {
  2353. tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
  2354. tp->offset = tp->address - reloc_sym->addr;
  2355. } else {
  2356. tp->symbol = strdup_or_goto(sym->name, nomem_out);
  2357. tp->offset = pp->offset;
  2358. }
  2359. tp->realname = strdup_or_goto(sym->name, nomem_out);
  2360. tp->retprobe = pp->retprobe;
  2361. if (pev->target) {
  2362. if (pev->uprobes) {
  2363. tev->point.module = strdup_or_goto(pev->target,
  2364. nomem_out);
  2365. } else {
  2366. mod_name = find_module_name(pev->target);
  2367. tev->point.module =
  2368. strdup(mod_name ? mod_name : pev->target);
  2369. free(mod_name);
  2370. if (!tev->point.module)
  2371. goto nomem_out;
  2372. }
  2373. }
  2374. tev->uprobes = pev->uprobes;
  2375. tev->nargs = pev->nargs;
  2376. if (tev->nargs) {
  2377. tev->args = zalloc(sizeof(struct probe_trace_arg) *
  2378. tev->nargs);
  2379. if (tev->args == NULL)
  2380. goto nomem_out;
  2381. }
  2382. for (i = 0; i < tev->nargs; i++) {
  2383. if (pev->args[i].name)
  2384. tev->args[i].name =
  2385. strdup_or_goto(pev->args[i].name,
  2386. nomem_out);
  2387. tev->args[i].value = strdup_or_goto(pev->args[i].var,
  2388. nomem_out);
  2389. if (pev->args[i].type)
  2390. tev->args[i].type =
  2391. strdup_or_goto(pev->args[i].type,
  2392. nomem_out);
  2393. }
  2394. arch__fix_tev_from_maps(pev, tev, map, sym);
  2395. }
  2396. if (ret == skipped) {
  2397. ret = -ENOENT;
  2398. goto err_out;
  2399. }
  2400. out:
  2401. put_target_map(map, pev->uprobes);
  2402. free(syms);
  2403. return ret;
  2404. nomem_out:
  2405. ret = -ENOMEM;
  2406. err_out:
  2407. clear_probe_trace_events(*tevs, num_matched_functions);
  2408. zfree(tevs);
  2409. goto out;
  2410. }
  2411. static int try_to_find_absolute_address(struct perf_probe_event *pev,
  2412. struct probe_trace_event **tevs)
  2413. {
  2414. struct perf_probe_point *pp = &pev->point;
  2415. struct probe_trace_event *tev;
  2416. struct probe_trace_point *tp;
  2417. int i, err;
  2418. if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
  2419. return -EINVAL;
  2420. if (perf_probe_event_need_dwarf(pev))
  2421. return -EINVAL;
  2422. /*
  2423. * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
  2424. * absolute address.
  2425. *
  2426. * Only one tev can be generated by this.
  2427. */
  2428. *tevs = zalloc(sizeof(*tev));
  2429. if (!*tevs)
  2430. return -ENOMEM;
  2431. tev = *tevs;
  2432. tp = &tev->point;
  2433. /*
  2434. * Don't use tp->offset, use address directly, because
  2435. * in synthesize_probe_trace_command() address cannot be
  2436. * zero.
  2437. */
  2438. tp->address = pev->point.abs_address;
  2439. tp->retprobe = pp->retprobe;
  2440. tev->uprobes = pev->uprobes;
  2441. err = -ENOMEM;
  2442. /*
  2443. * Give it a '0x' leading symbol name.
  2444. * In __add_probe_trace_events, a NULL symbol is interpreted as
  2445. * invalud.
  2446. */
  2447. if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
  2448. goto errout;
  2449. /* For kprobe, check range */
  2450. if ((!tev->uprobes) &&
  2451. (kprobe_warn_out_range(tev->point.symbol,
  2452. tev->point.address))) {
  2453. err = -EACCES;
  2454. goto errout;
  2455. }
  2456. if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
  2457. goto errout;
  2458. if (pev->target) {
  2459. tp->module = strdup(pev->target);
  2460. if (!tp->module)
  2461. goto errout;
  2462. }
  2463. if (tev->group) {
  2464. tev->group = strdup(pev->group);
  2465. if (!tev->group)
  2466. goto errout;
  2467. }
  2468. if (pev->event) {
  2469. tev->event = strdup(pev->event);
  2470. if (!tev->event)
  2471. goto errout;
  2472. }
  2473. tev->nargs = pev->nargs;
  2474. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  2475. if (!tev->args) {
  2476. err = -ENOMEM;
  2477. goto errout;
  2478. }
  2479. for (i = 0; i < tev->nargs; i++)
  2480. copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
  2481. return 1;
  2482. errout:
  2483. if (*tevs) {
  2484. clear_probe_trace_events(*tevs, 1);
  2485. *tevs = NULL;
  2486. }
  2487. return err;
  2488. }
  2489. bool __weak arch__prefers_symtab(void) { return false; }
  2490. static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
  2491. struct probe_trace_event **tevs)
  2492. {
  2493. struct probe_cache *cache;
  2494. struct probe_cache_entry *entry;
  2495. struct probe_trace_event *tev;
  2496. struct str_node *node;
  2497. int ret, i;
  2498. cache = probe_cache__new(pev->target);
  2499. if (!cache)
  2500. return 0;
  2501. entry = probe_cache__find(cache, pev);
  2502. if (!entry) {
  2503. ret = 0;
  2504. goto out;
  2505. }
  2506. ret = strlist__nr_entries(entry->tevlist);
  2507. if (ret > probe_conf.max_probes) {
  2508. pr_debug("Too many entries matched in the cache of %s\n",
  2509. pev->target ? : "kernel");
  2510. ret = -E2BIG;
  2511. goto out;
  2512. }
  2513. *tevs = zalloc(ret * sizeof(*tev));
  2514. if (!*tevs) {
  2515. ret = -ENOMEM;
  2516. goto out;
  2517. }
  2518. i = 0;
  2519. strlist__for_each_entry(node, entry->tevlist) {
  2520. tev = &(*tevs)[i++];
  2521. ret = parse_probe_trace_command(node->s, tev);
  2522. if (ret < 0)
  2523. goto out;
  2524. /* Set the uprobes attribute as same as original */
  2525. tev->uprobes = pev->uprobes;
  2526. }
  2527. ret = i;
  2528. out:
  2529. probe_cache__delete(cache);
  2530. return ret;
  2531. }
  2532. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  2533. struct probe_trace_event **tevs)
  2534. {
  2535. int ret;
  2536. if (!pev->group) {
  2537. /* Set group name if not given */
  2538. if (!pev->uprobes) {
  2539. pev->group = strdup(PERFPROBE_GROUP);
  2540. ret = pev->group ? 0 : -ENOMEM;
  2541. } else
  2542. ret = convert_exec_to_group(pev->target, &pev->group);
  2543. if (ret != 0) {
  2544. pr_warning("Failed to make a group name.\n");
  2545. return ret;
  2546. }
  2547. }
  2548. ret = try_to_find_absolute_address(pev, tevs);
  2549. if (ret > 0)
  2550. return ret;
  2551. /* At first, we need to lookup cache entry */
  2552. ret = find_probe_trace_events_from_cache(pev, tevs);
  2553. if (ret > 0)
  2554. return ret; /* Found in probe cache */
  2555. if (arch__prefers_symtab() && !perf_probe_event_need_dwarf(pev)) {
  2556. ret = find_probe_trace_events_from_map(pev, tevs);
  2557. if (ret > 0)
  2558. return ret; /* Found in symbol table */
  2559. }
  2560. /* Convert perf_probe_event with debuginfo */
  2561. ret = try_to_find_probe_trace_events(pev, tevs);
  2562. if (ret != 0)
  2563. return ret; /* Found in debuginfo or got an error */
  2564. return find_probe_trace_events_from_map(pev, tevs);
  2565. }
  2566. int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2567. {
  2568. int i, ret;
  2569. /* Loop 1: convert all events */
  2570. for (i = 0; i < npevs; i++) {
  2571. /* Init kprobe blacklist if needed */
  2572. if (!pevs[i].uprobes)
  2573. kprobe_blacklist__init();
  2574. /* Convert with or without debuginfo */
  2575. ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
  2576. if (ret < 0)
  2577. return ret;
  2578. pevs[i].ntevs = ret;
  2579. }
  2580. /* This just release blacklist only if allocated */
  2581. kprobe_blacklist__release();
  2582. return 0;
  2583. }
  2584. int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2585. {
  2586. int i, ret = 0;
  2587. /* Loop 2: add all events */
  2588. for (i = 0; i < npevs; i++) {
  2589. ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
  2590. pevs[i].ntevs,
  2591. probe_conf.force_add);
  2592. if (ret < 0)
  2593. break;
  2594. }
  2595. return ret;
  2596. }
  2597. void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2598. {
  2599. int i, j;
  2600. /* Loop 3: cleanup and free trace events */
  2601. for (i = 0; i < npevs; i++) {
  2602. for (j = 0; j < pevs[i].ntevs; j++)
  2603. clear_probe_trace_event(&pevs[i].tevs[j]);
  2604. zfree(&pevs[i].tevs);
  2605. pevs[i].ntevs = 0;
  2606. clear_perf_probe_event(&pevs[i]);
  2607. }
  2608. }
  2609. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2610. {
  2611. int ret;
  2612. ret = init_probe_symbol_maps(pevs->uprobes);
  2613. if (ret < 0)
  2614. return ret;
  2615. ret = convert_perf_probe_events(pevs, npevs);
  2616. if (ret == 0)
  2617. ret = apply_perf_probe_events(pevs, npevs);
  2618. cleanup_perf_probe_events(pevs, npevs);
  2619. exit_probe_symbol_maps();
  2620. return ret;
  2621. }
  2622. int del_perf_probe_events(struct strfilter *filter)
  2623. {
  2624. int ret, ret2, ufd = -1, kfd = -1;
  2625. char *str = strfilter__string(filter);
  2626. if (!str)
  2627. return -EINVAL;
  2628. /* Get current event names */
  2629. ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
  2630. if (ret < 0)
  2631. goto out;
  2632. ret = probe_file__del_events(kfd, filter);
  2633. if (ret < 0 && ret != -ENOENT)
  2634. goto error;
  2635. ret2 = probe_file__del_events(ufd, filter);
  2636. if (ret2 < 0 && ret2 != -ENOENT) {
  2637. ret = ret2;
  2638. goto error;
  2639. }
  2640. ret = 0;
  2641. error:
  2642. if (kfd >= 0)
  2643. close(kfd);
  2644. if (ufd >= 0)
  2645. close(ufd);
  2646. out:
  2647. free(str);
  2648. return ret;
  2649. }
  2650. /* TODO: don't use a global variable for filter ... */
  2651. static struct strfilter *available_func_filter;
  2652. /*
  2653. * If a symbol corresponds to a function with global binding and
  2654. * matches filter return 0. For all others return 1.
  2655. */
  2656. static int filter_available_functions(struct map *map __maybe_unused,
  2657. struct symbol *sym)
  2658. {
  2659. if (strfilter__compare(available_func_filter, sym->name))
  2660. return 0;
  2661. return 1;
  2662. }
  2663. int show_available_funcs(const char *target, struct strfilter *_filter,
  2664. bool user)
  2665. {
  2666. struct map *map;
  2667. int ret;
  2668. ret = init_probe_symbol_maps(user);
  2669. if (ret < 0)
  2670. return ret;
  2671. /* Get a symbol map */
  2672. if (user)
  2673. map = dso__new_map(target);
  2674. else
  2675. map = kernel_get_module_map(target);
  2676. if (!map) {
  2677. pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
  2678. return -EINVAL;
  2679. }
  2680. /* Load symbols with given filter */
  2681. available_func_filter = _filter;
  2682. if (map__load(map, filter_available_functions)) {
  2683. pr_err("Failed to load symbols in %s\n", (target) ? : "kernel");
  2684. goto end;
  2685. }
  2686. if (!dso__sorted_by_name(map->dso, map->type))
  2687. dso__sort_by_name(map->dso, map->type);
  2688. /* Show all (filtered) symbols */
  2689. setup_pager();
  2690. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  2691. end:
  2692. if (user) {
  2693. map__put(map);
  2694. }
  2695. exit_probe_symbol_maps();
  2696. return ret;
  2697. }
  2698. int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
  2699. struct perf_probe_arg *pvar)
  2700. {
  2701. tvar->value = strdup(pvar->var);
  2702. if (tvar->value == NULL)
  2703. return -ENOMEM;
  2704. if (pvar->type) {
  2705. tvar->type = strdup(pvar->type);
  2706. if (tvar->type == NULL)
  2707. return -ENOMEM;
  2708. }
  2709. if (pvar->name) {
  2710. tvar->name = strdup(pvar->name);
  2711. if (tvar->name == NULL)
  2712. return -ENOMEM;
  2713. } else
  2714. tvar->name = NULL;
  2715. return 0;
  2716. }