probe-event.c 66 KB

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