probe-event.c 66 KB

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