probe-finder.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. /*
  2. * probe-finder.c : C expression to kprobe event 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 <getopt.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <ctype.h>
  33. #include <dwarf-regs.h>
  34. #include <linux/bitops.h>
  35. #include "event.h"
  36. #include "debug.h"
  37. #include "util.h"
  38. #include "symbol.h"
  39. #include "probe-finder.h"
  40. /* Kprobe tracer basic type is up to u64 */
  41. #define MAX_BASIC_TYPE_BITS 64
  42. /* Line number list operations */
  43. /* Add a line to line number list */
  44. static int line_list__add_line(struct list_head *head, int line)
  45. {
  46. struct line_node *ln;
  47. struct list_head *p;
  48. /* Reverse search, because new line will be the last one */
  49. list_for_each_entry_reverse(ln, head, list) {
  50. if (ln->line < line) {
  51. p = &ln->list;
  52. goto found;
  53. } else if (ln->line == line) /* Already exist */
  54. return 1;
  55. }
  56. /* List is empty, or the smallest entry */
  57. p = head;
  58. found:
  59. pr_debug("line list: add a line %u\n", line);
  60. ln = zalloc(sizeof(struct line_node));
  61. if (ln == NULL)
  62. return -ENOMEM;
  63. ln->line = line;
  64. INIT_LIST_HEAD(&ln->list);
  65. list_add(&ln->list, p);
  66. return 0;
  67. }
  68. /* Check if the line in line number list */
  69. static int line_list__has_line(struct list_head *head, int line)
  70. {
  71. struct line_node *ln;
  72. /* Reverse search, because new line will be the last one */
  73. list_for_each_entry(ln, head, list)
  74. if (ln->line == line)
  75. return 1;
  76. return 0;
  77. }
  78. /* Init line number list */
  79. static void line_list__init(struct list_head *head)
  80. {
  81. INIT_LIST_HEAD(head);
  82. }
  83. /* Free line number list */
  84. static void line_list__free(struct list_head *head)
  85. {
  86. struct line_node *ln;
  87. while (!list_empty(head)) {
  88. ln = list_first_entry(head, struct line_node, list);
  89. list_del(&ln->list);
  90. free(ln);
  91. }
  92. }
  93. /* Dwarf FL wrappers */
  94. static char *debuginfo_path; /* Currently dummy */
  95. static const Dwfl_Callbacks offline_callbacks = {
  96. .find_debuginfo = dwfl_standard_find_debuginfo,
  97. .debuginfo_path = &debuginfo_path,
  98. .section_address = dwfl_offline_section_address,
  99. /* We use this table for core files too. */
  100. .find_elf = dwfl_build_id_find_elf,
  101. };
  102. /* Get a Dwarf from offline image */
  103. static Dwarf *dwfl_init_offline_dwarf(int fd, Dwfl **dwflp, Dwarf_Addr *bias)
  104. {
  105. Dwfl_Module *mod;
  106. Dwarf *dbg = NULL;
  107. if (!dwflp)
  108. return NULL;
  109. *dwflp = dwfl_begin(&offline_callbacks);
  110. if (!*dwflp)
  111. return NULL;
  112. mod = dwfl_report_offline(*dwflp, "", "", fd);
  113. if (!mod)
  114. goto error;
  115. dbg = dwfl_module_getdwarf(mod, bias);
  116. if (!dbg) {
  117. error:
  118. dwfl_end(*dwflp);
  119. *dwflp = NULL;
  120. }
  121. return dbg;
  122. }
  123. #if _ELFUTILS_PREREQ(0, 148)
  124. /* This method is buggy if elfutils is older than 0.148 */
  125. static int __linux_kernel_find_elf(Dwfl_Module *mod,
  126. void **userdata,
  127. const char *module_name,
  128. Dwarf_Addr base,
  129. char **file_name, Elf **elfp)
  130. {
  131. int fd;
  132. const char *path = kernel_get_module_path(module_name);
  133. pr_debug2("Use file %s for %s\n", path, module_name);
  134. if (path) {
  135. fd = open(path, O_RDONLY);
  136. if (fd >= 0) {
  137. *file_name = strdup(path);
  138. return fd;
  139. }
  140. }
  141. /* If failed, try to call standard method */
  142. return dwfl_linux_kernel_find_elf(mod, userdata, module_name, base,
  143. file_name, elfp);
  144. }
  145. static const Dwfl_Callbacks kernel_callbacks = {
  146. .find_debuginfo = dwfl_standard_find_debuginfo,
  147. .debuginfo_path = &debuginfo_path,
  148. .find_elf = __linux_kernel_find_elf,
  149. .section_address = dwfl_linux_kernel_module_section_address,
  150. };
  151. /* Get a Dwarf from live kernel image */
  152. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr, Dwfl **dwflp,
  153. Dwarf_Addr *bias)
  154. {
  155. Dwarf *dbg;
  156. if (!dwflp)
  157. return NULL;
  158. *dwflp = dwfl_begin(&kernel_callbacks);
  159. if (!*dwflp)
  160. return NULL;
  161. /* Load the kernel dwarves: Don't care the result here */
  162. dwfl_linux_kernel_report_kernel(*dwflp);
  163. dwfl_linux_kernel_report_modules(*dwflp);
  164. dbg = dwfl_addrdwarf(*dwflp, addr, bias);
  165. /* Here, check whether we could get a real dwarf */
  166. if (!dbg) {
  167. pr_debug("Failed to find kernel dwarf at %lx\n",
  168. (unsigned long)addr);
  169. dwfl_end(*dwflp);
  170. *dwflp = NULL;
  171. }
  172. return dbg;
  173. }
  174. #else
  175. /* With older elfutils, this just support kernel module... */
  176. static Dwarf *dwfl_init_live_kernel_dwarf(Dwarf_Addr addr __used, Dwfl **dwflp,
  177. Dwarf_Addr *bias)
  178. {
  179. int fd;
  180. const char *path = kernel_get_module_path("kernel");
  181. if (!path) {
  182. pr_err("Failed to find vmlinux path\n");
  183. return NULL;
  184. }
  185. pr_debug2("Use file %s for debuginfo\n", path);
  186. fd = open(path, O_RDONLY);
  187. if (fd < 0)
  188. return NULL;
  189. return dwfl_init_offline_dwarf(fd, dwflp, bias);
  190. }
  191. #endif
  192. /* Dwarf wrappers */
  193. /* Find the realpath of the target file. */
  194. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  195. {
  196. Dwarf_Files *files;
  197. size_t nfiles, i;
  198. const char *src = NULL;
  199. int ret;
  200. if (!fname)
  201. return NULL;
  202. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  203. if (ret != 0)
  204. return NULL;
  205. for (i = 0; i < nfiles; i++) {
  206. src = dwarf_filesrc(files, i, NULL, NULL);
  207. if (strtailcmp(src, fname) == 0)
  208. break;
  209. }
  210. if (i == nfiles)
  211. return NULL;
  212. return src;
  213. }
  214. /* Get DW_AT_comp_dir (should be NULL with older gcc) */
  215. static const char *cu_get_comp_dir(Dwarf_Die *cu_die)
  216. {
  217. Dwarf_Attribute attr;
  218. if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
  219. return NULL;
  220. return dwarf_formstring(&attr);
  221. }
  222. /* Get a line number and file name for given address */
  223. static int cu_find_lineinfo(Dwarf_Die *cudie, unsigned long addr,
  224. const char **fname, int *lineno)
  225. {
  226. Dwarf_Line *line;
  227. Dwarf_Addr laddr;
  228. line = dwarf_getsrc_die(cudie, (Dwarf_Addr)addr);
  229. if (line && dwarf_lineaddr(line, &laddr) == 0 &&
  230. addr == (unsigned long)laddr && dwarf_lineno(line, lineno) == 0) {
  231. *fname = dwarf_linesrc(line, NULL, NULL);
  232. if (!*fname)
  233. /* line number is useless without filename */
  234. *lineno = 0;
  235. }
  236. return *lineno ?: -ENOENT;
  237. }
  238. /* Compare diename and tname */
  239. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  240. {
  241. const char *name;
  242. name = dwarf_diename(dw_die);
  243. return name ? (strcmp(tname, name) == 0) : false;
  244. }
  245. /* Get callsite line number of inline-function instance */
  246. static int die_get_call_lineno(Dwarf_Die *in_die)
  247. {
  248. Dwarf_Attribute attr;
  249. Dwarf_Word ret;
  250. if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
  251. return -ENOENT;
  252. dwarf_formudata(&attr, &ret);
  253. return (int)ret;
  254. }
  255. /* Get type die */
  256. static Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  257. {
  258. Dwarf_Attribute attr;
  259. if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
  260. dwarf_formref_die(&attr, die_mem))
  261. return die_mem;
  262. else
  263. return NULL;
  264. }
  265. /* Get a type die, but skip qualifiers */
  266. static Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  267. {
  268. int tag;
  269. do {
  270. vr_die = die_get_type(vr_die, die_mem);
  271. if (!vr_die)
  272. break;
  273. tag = dwarf_tag(vr_die);
  274. } while (tag == DW_TAG_const_type ||
  275. tag == DW_TAG_restrict_type ||
  276. tag == DW_TAG_volatile_type ||
  277. tag == DW_TAG_shared_type);
  278. return vr_die;
  279. }
  280. /* Get a type die, but skip qualifiers and typedef */
  281. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  282. {
  283. do {
  284. vr_die = __die_get_real_type(vr_die, die_mem);
  285. } while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
  286. return vr_die;
  287. }
  288. static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
  289. Dwarf_Word *result)
  290. {
  291. Dwarf_Attribute attr;
  292. if (dwarf_attr(tp_die, attr_name, &attr) == NULL ||
  293. dwarf_formudata(&attr, result) != 0)
  294. return -ENOENT;
  295. return 0;
  296. }
  297. static bool die_is_signed_type(Dwarf_Die *tp_die)
  298. {
  299. Dwarf_Word ret;
  300. if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
  301. return false;
  302. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  303. ret == DW_ATE_signed_fixed);
  304. }
  305. /* Get data_member_location offset */
  306. static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  307. {
  308. Dwarf_Attribute attr;
  309. Dwarf_Op *expr;
  310. size_t nexpr;
  311. int ret;
  312. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  313. return -ENOENT;
  314. if (dwarf_formudata(&attr, offs) != 0) {
  315. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  316. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  317. if (ret < 0 || nexpr == 0)
  318. return -ENOENT;
  319. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  320. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  321. expr[0].atom, nexpr);
  322. return -ENOTSUP;
  323. }
  324. *offs = (Dwarf_Word)expr[0].number;
  325. }
  326. return 0;
  327. }
  328. /* Return values for die_find callbacks */
  329. enum {
  330. DIE_FIND_CB_END = 0, /* End of Search */
  331. DIE_FIND_CB_CHILD = 1, /* Search only children */
  332. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  333. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  334. };
  335. /* Search a child die */
  336. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  337. int (*callback)(Dwarf_Die *, void *),
  338. void *data, Dwarf_Die *die_mem)
  339. {
  340. Dwarf_Die child_die;
  341. int ret;
  342. ret = dwarf_child(rt_die, die_mem);
  343. if (ret != 0)
  344. return NULL;
  345. do {
  346. ret = callback(die_mem, data);
  347. if (ret == DIE_FIND_CB_END)
  348. return die_mem;
  349. if ((ret & DIE_FIND_CB_CHILD) &&
  350. die_find_child(die_mem, callback, data, &child_die)) {
  351. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  352. return die_mem;
  353. }
  354. } while ((ret & DIE_FIND_CB_SIBLING) &&
  355. dwarf_siblingof(die_mem, die_mem) == 0);
  356. return NULL;
  357. }
  358. struct __addr_die_search_param {
  359. Dwarf_Addr addr;
  360. Dwarf_Die *die_mem;
  361. };
  362. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  363. {
  364. struct __addr_die_search_param *ad = data;
  365. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  366. dwarf_haspc(fn_die, ad->addr)) {
  367. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  368. return DWARF_CB_ABORT;
  369. }
  370. return DWARF_CB_OK;
  371. }
  372. /* Search a real subprogram including this line, */
  373. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  374. Dwarf_Die *die_mem)
  375. {
  376. struct __addr_die_search_param ad;
  377. ad.addr = addr;
  378. ad.die_mem = die_mem;
  379. /* dwarf_getscopes can't find subprogram. */
  380. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  381. return NULL;
  382. else
  383. return die_mem;
  384. }
  385. /* die_find callback for inline function search */
  386. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  387. {
  388. Dwarf_Addr *addr = data;
  389. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  390. dwarf_haspc(die_mem, *addr))
  391. return DIE_FIND_CB_END;
  392. return DIE_FIND_CB_CONTINUE;
  393. }
  394. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  395. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  396. Dwarf_Die *die_mem)
  397. {
  398. Dwarf_Die tmp_die;
  399. sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
  400. if (!sp_die)
  401. return NULL;
  402. /* Inlined function could be recursive. Trace it until fail */
  403. while (sp_die) {
  404. memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
  405. sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
  406. &tmp_die);
  407. }
  408. return die_mem;
  409. }
  410. /* Walker on lines (Note: line number will not be sorted) */
  411. typedef int (* line_walk_handler_t) (const char *fname, int lineno,
  412. Dwarf_Addr addr, void *data);
  413. struct __line_walk_param {
  414. const char *fname;
  415. line_walk_handler_t handler;
  416. void *data;
  417. int retval;
  418. };
  419. static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
  420. {
  421. struct __line_walk_param *lw = data;
  422. Dwarf_Addr addr;
  423. int lineno;
  424. if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
  425. lineno = die_get_call_lineno(in_die);
  426. if (lineno > 0 && dwarf_entrypc(in_die, &addr) == 0) {
  427. lw->retval = lw->handler(lw->fname, lineno, addr,
  428. lw->data);
  429. if (lw->retval != 0)
  430. return DIE_FIND_CB_END;
  431. }
  432. }
  433. return DIE_FIND_CB_SIBLING;
  434. }
  435. /* Walk on lines of blocks included in given DIE */
  436. static int __die_walk_funclines(Dwarf_Die *sp_die,
  437. line_walk_handler_t handler, void *data)
  438. {
  439. struct __line_walk_param lw = {
  440. .handler = handler,
  441. .data = data,
  442. .retval = 0,
  443. };
  444. Dwarf_Die die_mem;
  445. Dwarf_Addr addr;
  446. int lineno;
  447. /* Handle function declaration line */
  448. lw.fname = dwarf_decl_file(sp_die);
  449. if (lw.fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
  450. dwarf_entrypc(sp_die, &addr) == 0) {
  451. lw.retval = handler(lw.fname, lineno, addr, data);
  452. if (lw.retval != 0)
  453. goto done;
  454. }
  455. die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
  456. done:
  457. return lw.retval;
  458. }
  459. static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
  460. {
  461. struct __line_walk_param *lw = data;
  462. lw->retval = __die_walk_funclines(sp_die, lw->handler, lw->data);
  463. if (lw->retval != 0)
  464. return DWARF_CB_ABORT;
  465. return DWARF_CB_OK;
  466. }
  467. /*
  468. * Walk on lines inside given PDIE. If the PDIE is subprogram, walk only on
  469. * the lines inside the subprogram, otherwise PDIE must be a CU DIE.
  470. */
  471. static int die_walk_lines(Dwarf_Die *pdie, line_walk_handler_t handler,
  472. void *data)
  473. {
  474. Dwarf_Lines *lines;
  475. Dwarf_Line *line;
  476. Dwarf_Addr addr;
  477. const char *fname;
  478. int lineno, ret = 0;
  479. Dwarf_Die die_mem, *cu_die;
  480. size_t nlines, i;
  481. /* Get the CU die */
  482. if (dwarf_tag(pdie) == DW_TAG_subprogram)
  483. cu_die = dwarf_diecu(pdie, &die_mem, NULL, NULL);
  484. else
  485. cu_die = pdie;
  486. if (!cu_die) {
  487. pr_debug2("Failed to get CU from subprogram\n");
  488. return -EINVAL;
  489. }
  490. /* Get lines list in the CU */
  491. if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
  492. pr_debug2("Failed to get source lines on this CU.\n");
  493. return -ENOENT;
  494. }
  495. pr_debug2("Get %zd lines from this CU\n", nlines);
  496. /* Walk on the lines on lines list */
  497. for (i = 0; i < nlines; i++) {
  498. line = dwarf_onesrcline(lines, i);
  499. if (line == NULL ||
  500. dwarf_lineno(line, &lineno) != 0 ||
  501. dwarf_lineaddr(line, &addr) != 0) {
  502. pr_debug2("Failed to get line info. "
  503. "Possible error in debuginfo.\n");
  504. continue;
  505. }
  506. /* Filter lines based on address */
  507. if (pdie != cu_die)
  508. /*
  509. * Address filtering
  510. * The line is included in given function, and
  511. * no inline block includes it.
  512. */
  513. if (!dwarf_haspc(pdie, addr) ||
  514. die_find_inlinefunc(pdie, addr, &die_mem))
  515. continue;
  516. /* Get source line */
  517. fname = dwarf_linesrc(line, NULL, NULL);
  518. ret = handler(fname, lineno, addr, data);
  519. if (ret != 0)
  520. return ret;
  521. }
  522. /*
  523. * Dwarf lines doesn't include function declarations and inlined
  524. * subroutines. We have to check functions list or given function.
  525. */
  526. if (pdie != cu_die)
  527. ret = __die_walk_funclines(pdie, handler, data);
  528. else {
  529. struct __line_walk_param param = {
  530. .handler = handler,
  531. .data = data,
  532. .retval = 0,
  533. };
  534. dwarf_getfuncs(cu_die, __die_walk_culines_cb, &param, 0);
  535. ret = param.retval;
  536. }
  537. return ret;
  538. }
  539. struct __find_variable_param {
  540. const char *name;
  541. Dwarf_Addr addr;
  542. };
  543. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  544. {
  545. struct __find_variable_param *fvp = data;
  546. int tag;
  547. tag = dwarf_tag(die_mem);
  548. if ((tag == DW_TAG_formal_parameter ||
  549. tag == DW_TAG_variable) &&
  550. die_compare_name(die_mem, fvp->name))
  551. return DIE_FIND_CB_END;
  552. if (dwarf_haspc(die_mem, fvp->addr))
  553. return DIE_FIND_CB_CONTINUE;
  554. else
  555. return DIE_FIND_CB_SIBLING;
  556. }
  557. /* Find a variable called 'name' at given address */
  558. static Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
  559. Dwarf_Addr addr, Dwarf_Die *die_mem)
  560. {
  561. struct __find_variable_param fvp = { .name = name, .addr = addr};
  562. return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
  563. die_mem);
  564. }
  565. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  566. {
  567. const char *name = data;
  568. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  569. die_compare_name(die_mem, name))
  570. return DIE_FIND_CB_END;
  571. return DIE_FIND_CB_SIBLING;
  572. }
  573. /* Find a member called 'name' */
  574. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  575. Dwarf_Die *die_mem)
  576. {
  577. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  578. die_mem);
  579. }
  580. /* Get the name of given variable DIE */
  581. static int die_get_typename(Dwarf_Die *vr_die, char *buf, int len)
  582. {
  583. Dwarf_Die type;
  584. int tag, ret, ret2;
  585. const char *tmp = "";
  586. if (__die_get_real_type(vr_die, &type) == NULL)
  587. return -ENOENT;
  588. tag = dwarf_tag(&type);
  589. if (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)
  590. tmp = "*";
  591. else if (tag == DW_TAG_subroutine_type) {
  592. /* Function pointer */
  593. ret = snprintf(buf, len, "(function_type)");
  594. return (ret >= len) ? -E2BIG : ret;
  595. } else {
  596. if (!dwarf_diename(&type))
  597. return -ENOENT;
  598. if (tag == DW_TAG_union_type)
  599. tmp = "union ";
  600. else if (tag == DW_TAG_structure_type)
  601. tmp = "struct ";
  602. /* Write a base name */
  603. ret = snprintf(buf, len, "%s%s", tmp, dwarf_diename(&type));
  604. return (ret >= len) ? -E2BIG : ret;
  605. }
  606. ret = die_get_typename(&type, buf, len);
  607. if (ret > 0) {
  608. ret2 = snprintf(buf + ret, len - ret, "%s", tmp);
  609. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  610. }
  611. return ret;
  612. }
  613. /* Get the name and type of given variable DIE, stored as "type\tname" */
  614. static int die_get_varname(Dwarf_Die *vr_die, char *buf, int len)
  615. {
  616. int ret, ret2;
  617. ret = die_get_typename(vr_die, buf, len);
  618. if (ret < 0) {
  619. pr_debug("Failed to get type, make it unknown.\n");
  620. ret = snprintf(buf, len, "(unknown_type)");
  621. }
  622. if (ret > 0) {
  623. ret2 = snprintf(buf + ret, len - ret, "\t%s",
  624. dwarf_diename(vr_die));
  625. ret = (ret2 >= len - ret) ? -E2BIG : ret2 + ret;
  626. }
  627. return ret;
  628. }
  629. /*
  630. * Probe finder related functions
  631. */
  632. static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs)
  633. {
  634. struct probe_trace_arg_ref *ref;
  635. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  636. if (ref != NULL)
  637. ref->offset = offs;
  638. return ref;
  639. }
  640. /*
  641. * Convert a location into trace_arg.
  642. * If tvar == NULL, this just checks variable can be converted.
  643. */
  644. static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr,
  645. Dwarf_Op *fb_ops,
  646. struct probe_trace_arg *tvar)
  647. {
  648. Dwarf_Attribute attr;
  649. Dwarf_Op *op;
  650. size_t nops;
  651. unsigned int regn;
  652. Dwarf_Word offs = 0;
  653. bool ref = false;
  654. const char *regs;
  655. int ret;
  656. if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL)
  657. goto static_var;
  658. /* TODO: handle more than 1 exprs */
  659. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL ||
  660. dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0 ||
  661. nops == 0) {
  662. /* TODO: Support const_value */
  663. return -ENOENT;
  664. }
  665. if (op->atom == DW_OP_addr) {
  666. static_var:
  667. if (!tvar)
  668. return 0;
  669. /* Static variables on memory (not stack), make @varname */
  670. ret = strlen(dwarf_diename(vr_die));
  671. tvar->value = zalloc(ret + 2);
  672. if (tvar->value == NULL)
  673. return -ENOMEM;
  674. snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die));
  675. tvar->ref = alloc_trace_arg_ref((long)offs);
  676. if (tvar->ref == NULL)
  677. return -ENOMEM;
  678. return 0;
  679. }
  680. /* If this is based on frame buffer, set the offset */
  681. if (op->atom == DW_OP_fbreg) {
  682. if (fb_ops == NULL)
  683. return -ENOTSUP;
  684. ref = true;
  685. offs = op->number;
  686. op = &fb_ops[0];
  687. }
  688. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  689. regn = op->atom - DW_OP_breg0;
  690. offs += op->number;
  691. ref = true;
  692. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  693. regn = op->atom - DW_OP_reg0;
  694. } else if (op->atom == DW_OP_bregx) {
  695. regn = op->number;
  696. offs += op->number2;
  697. ref = true;
  698. } else if (op->atom == DW_OP_regx) {
  699. regn = op->number;
  700. } else {
  701. pr_debug("DW_OP %x is not supported.\n", op->atom);
  702. return -ENOTSUP;
  703. }
  704. if (!tvar)
  705. return 0;
  706. regs = get_arch_regstr(regn);
  707. if (!regs) {
  708. /* This should be a bug in DWARF or this tool */
  709. pr_warning("Mapping for the register number %u "
  710. "missing on this architecture.\n", regn);
  711. return -ERANGE;
  712. }
  713. tvar->value = strdup(regs);
  714. if (tvar->value == NULL)
  715. return -ENOMEM;
  716. if (ref) {
  717. tvar->ref = alloc_trace_arg_ref((long)offs);
  718. if (tvar->ref == NULL)
  719. return -ENOMEM;
  720. }
  721. return 0;
  722. }
  723. #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long))
  724. static int convert_variable_type(Dwarf_Die *vr_die,
  725. struct probe_trace_arg *tvar,
  726. const char *cast)
  727. {
  728. struct probe_trace_arg_ref **ref_ptr = &tvar->ref;
  729. Dwarf_Die type;
  730. char buf[16];
  731. int bsize, boffs, total;
  732. int ret;
  733. /* TODO: check all types */
  734. if (cast && strcmp(cast, "string") != 0) {
  735. /* Non string type is OK */
  736. tvar->type = strdup(cast);
  737. return (tvar->type == NULL) ? -ENOMEM : 0;
  738. }
  739. bsize = dwarf_bitsize(vr_die);
  740. if (bsize > 0) {
  741. /* This is a bitfield */
  742. boffs = dwarf_bitoffset(vr_die);
  743. total = dwarf_bytesize(vr_die);
  744. if (boffs < 0 || total < 0)
  745. return -ENOENT;
  746. ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs,
  747. BYTES_TO_BITS(total));
  748. goto formatted;
  749. }
  750. if (die_get_real_type(vr_die, &type) == NULL) {
  751. pr_warning("Failed to get a type information of %s.\n",
  752. dwarf_diename(vr_die));
  753. return -ENOENT;
  754. }
  755. pr_debug("%s type is %s.\n",
  756. dwarf_diename(vr_die), dwarf_diename(&type));
  757. if (cast && strcmp(cast, "string") == 0) { /* String type */
  758. ret = dwarf_tag(&type);
  759. if (ret != DW_TAG_pointer_type &&
  760. ret != DW_TAG_array_type) {
  761. pr_warning("Failed to cast into string: "
  762. "%s(%s) is not a pointer nor array.\n",
  763. dwarf_diename(vr_die), dwarf_diename(&type));
  764. return -EINVAL;
  765. }
  766. if (ret == DW_TAG_pointer_type) {
  767. if (die_get_real_type(&type, &type) == NULL) {
  768. pr_warning("Failed to get a type"
  769. " information.\n");
  770. return -ENOENT;
  771. }
  772. while (*ref_ptr)
  773. ref_ptr = &(*ref_ptr)->next;
  774. /* Add new reference with offset +0 */
  775. *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref));
  776. if (*ref_ptr == NULL) {
  777. pr_warning("Out of memory error\n");
  778. return -ENOMEM;
  779. }
  780. }
  781. if (!die_compare_name(&type, "char") &&
  782. !die_compare_name(&type, "unsigned char")) {
  783. pr_warning("Failed to cast into string: "
  784. "%s is not (unsigned) char *.\n",
  785. dwarf_diename(vr_die));
  786. return -EINVAL;
  787. }
  788. tvar->type = strdup(cast);
  789. return (tvar->type == NULL) ? -ENOMEM : 0;
  790. }
  791. ret = dwarf_bytesize(&type);
  792. if (ret <= 0)
  793. /* No size ... try to use default type */
  794. return 0;
  795. ret = BYTES_TO_BITS(ret);
  796. /* Check the bitwidth */
  797. if (ret > MAX_BASIC_TYPE_BITS) {
  798. pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n",
  799. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  800. ret = MAX_BASIC_TYPE_BITS;
  801. }
  802. ret = snprintf(buf, 16, "%c%d",
  803. die_is_signed_type(&type) ? 's' : 'u', ret);
  804. formatted:
  805. if (ret < 0 || ret >= 16) {
  806. if (ret >= 16)
  807. ret = -E2BIG;
  808. pr_warning("Failed to convert variable type: %s\n",
  809. strerror(-ret));
  810. return ret;
  811. }
  812. tvar->type = strdup(buf);
  813. if (tvar->type == NULL)
  814. return -ENOMEM;
  815. return 0;
  816. }
  817. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  818. struct perf_probe_arg_field *field,
  819. struct probe_trace_arg_ref **ref_ptr,
  820. Dwarf_Die *die_mem)
  821. {
  822. struct probe_trace_arg_ref *ref = *ref_ptr;
  823. Dwarf_Die type;
  824. Dwarf_Word offs;
  825. int ret, tag;
  826. pr_debug("converting %s in %s\n", field->name, varname);
  827. if (die_get_real_type(vr_die, &type) == NULL) {
  828. pr_warning("Failed to get the type of %s.\n", varname);
  829. return -ENOENT;
  830. }
  831. pr_debug2("Var real type: (%x)\n", (unsigned)dwarf_dieoffset(&type));
  832. tag = dwarf_tag(&type);
  833. if (field->name[0] == '[' &&
  834. (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) {
  835. if (field->next)
  836. /* Save original type for next field */
  837. memcpy(die_mem, &type, sizeof(*die_mem));
  838. /* Get the type of this array */
  839. if (die_get_real_type(&type, &type) == NULL) {
  840. pr_warning("Failed to get the type of %s.\n", varname);
  841. return -ENOENT;
  842. }
  843. pr_debug2("Array real type: (%x)\n",
  844. (unsigned)dwarf_dieoffset(&type));
  845. if (tag == DW_TAG_pointer_type) {
  846. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  847. if (ref == NULL)
  848. return -ENOMEM;
  849. if (*ref_ptr)
  850. (*ref_ptr)->next = ref;
  851. else
  852. *ref_ptr = ref;
  853. }
  854. ref->offset += dwarf_bytesize(&type) * field->index;
  855. if (!field->next)
  856. /* Save vr_die for converting types */
  857. memcpy(die_mem, vr_die, sizeof(*die_mem));
  858. goto next;
  859. } else if (tag == DW_TAG_pointer_type) {
  860. /* Check the pointer and dereference */
  861. if (!field->ref) {
  862. pr_err("Semantic error: %s must be referred by '->'\n",
  863. field->name);
  864. return -EINVAL;
  865. }
  866. /* Get the type pointed by this pointer */
  867. if (die_get_real_type(&type, &type) == NULL) {
  868. pr_warning("Failed to get the type of %s.\n", varname);
  869. return -ENOENT;
  870. }
  871. /* Verify it is a data structure */
  872. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  873. pr_warning("%s is not a data structure.\n", varname);
  874. return -EINVAL;
  875. }
  876. ref = zalloc(sizeof(struct probe_trace_arg_ref));
  877. if (ref == NULL)
  878. return -ENOMEM;
  879. if (*ref_ptr)
  880. (*ref_ptr)->next = ref;
  881. else
  882. *ref_ptr = ref;
  883. } else {
  884. /* Verify it is a data structure */
  885. if (tag != DW_TAG_structure_type) {
  886. pr_warning("%s is not a data structure.\n", varname);
  887. return -EINVAL;
  888. }
  889. if (field->name[0] == '[') {
  890. pr_err("Semantic error: %s is not a pointor"
  891. " nor array.\n", varname);
  892. return -EINVAL;
  893. }
  894. if (field->ref) {
  895. pr_err("Semantic error: %s must be referred by '.'\n",
  896. field->name);
  897. return -EINVAL;
  898. }
  899. if (!ref) {
  900. pr_warning("Structure on a register is not "
  901. "supported yet.\n");
  902. return -ENOTSUP;
  903. }
  904. }
  905. if (die_find_member(&type, field->name, die_mem) == NULL) {
  906. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  907. dwarf_diename(&type), field->name);
  908. return -EINVAL;
  909. }
  910. /* Get the offset of the field */
  911. ret = die_get_data_member_location(die_mem, &offs);
  912. if (ret < 0) {
  913. pr_warning("Failed to get the offset of %s.\n", field->name);
  914. return ret;
  915. }
  916. ref->offset += (long)offs;
  917. next:
  918. /* Converting next field */
  919. if (field->next)
  920. return convert_variable_fields(die_mem, field->name,
  921. field->next, &ref, die_mem);
  922. else
  923. return 0;
  924. }
  925. /* Show a variables in kprobe event format */
  926. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  927. {
  928. Dwarf_Die die_mem;
  929. int ret;
  930. pr_debug("Converting variable %s into trace event.\n",
  931. dwarf_diename(vr_die));
  932. ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops,
  933. pf->tvar);
  934. if (ret == -ENOENT)
  935. pr_err("Failed to find the location of %s at this address.\n"
  936. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  937. else if (ret == -ENOTSUP)
  938. pr_err("Sorry, we don't support this variable location yet.\n");
  939. else if (pf->pvar->field) {
  940. ret = convert_variable_fields(vr_die, pf->pvar->var,
  941. pf->pvar->field, &pf->tvar->ref,
  942. &die_mem);
  943. vr_die = &die_mem;
  944. }
  945. if (ret == 0)
  946. ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type);
  947. /* *expr will be cached in libdw. Don't free it. */
  948. return ret;
  949. }
  950. /* Find a variable in a subprogram die */
  951. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  952. {
  953. Dwarf_Die vr_die, *scopes;
  954. char buf[32], *ptr;
  955. int ret, nscopes;
  956. if (!is_c_varname(pf->pvar->var)) {
  957. /* Copy raw parameters */
  958. pf->tvar->value = strdup(pf->pvar->var);
  959. if (pf->tvar->value == NULL)
  960. return -ENOMEM;
  961. if (pf->pvar->type) {
  962. pf->tvar->type = strdup(pf->pvar->type);
  963. if (pf->tvar->type == NULL)
  964. return -ENOMEM;
  965. }
  966. if (pf->pvar->name) {
  967. pf->tvar->name = strdup(pf->pvar->name);
  968. if (pf->tvar->name == NULL)
  969. return -ENOMEM;
  970. } else
  971. pf->tvar->name = NULL;
  972. return 0;
  973. }
  974. if (pf->pvar->name)
  975. pf->tvar->name = strdup(pf->pvar->name);
  976. else {
  977. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  978. if (ret < 0)
  979. return ret;
  980. ptr = strchr(buf, ':'); /* Change type separator to _ */
  981. if (ptr)
  982. *ptr = '_';
  983. pf->tvar->name = strdup(buf);
  984. }
  985. if (pf->tvar->name == NULL)
  986. return -ENOMEM;
  987. pr_debug("Searching '%s' variable in context.\n",
  988. pf->pvar->var);
  989. /* Search child die for local variables and parameters. */
  990. if (die_find_variable_at(sp_die, pf->pvar->var, pf->addr, &vr_die))
  991. ret = convert_variable(&vr_die, pf);
  992. else {
  993. /* Search upper class */
  994. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  995. while (nscopes-- > 1) {
  996. pr_debug("Searching variables in %s\n",
  997. dwarf_diename(&scopes[nscopes]));
  998. /* We should check this scope, so give dummy address */
  999. if (die_find_variable_at(&scopes[nscopes],
  1000. pf->pvar->var, 0,
  1001. &vr_die)) {
  1002. ret = convert_variable(&vr_die, pf);
  1003. goto found;
  1004. }
  1005. }
  1006. if (scopes)
  1007. free(scopes);
  1008. ret = -ENOENT;
  1009. }
  1010. found:
  1011. if (ret < 0)
  1012. pr_warning("Failed to find '%s' in this function.\n",
  1013. pf->pvar->var);
  1014. return ret;
  1015. }
  1016. /* Convert subprogram DIE to trace point */
  1017. static int convert_to_trace_point(Dwarf_Die *sp_die, Dwarf_Addr paddr,
  1018. bool retprobe, struct probe_trace_point *tp)
  1019. {
  1020. Dwarf_Addr eaddr;
  1021. const char *name;
  1022. /* Copy the name of probe point */
  1023. name = dwarf_diename(sp_die);
  1024. if (name) {
  1025. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  1026. pr_warning("Failed to get entry address of %s\n",
  1027. dwarf_diename(sp_die));
  1028. return -ENOENT;
  1029. }
  1030. tp->symbol = strdup(name);
  1031. if (tp->symbol == NULL)
  1032. return -ENOMEM;
  1033. tp->offset = (unsigned long)(paddr - eaddr);
  1034. } else
  1035. /* This function has no name. */
  1036. tp->offset = (unsigned long)paddr;
  1037. /* Return probe must be on the head of a subprogram */
  1038. if (retprobe) {
  1039. if (eaddr != paddr) {
  1040. pr_warning("Return probe must be on the head of"
  1041. " a real function.\n");
  1042. return -EINVAL;
  1043. }
  1044. tp->retprobe = true;
  1045. }
  1046. return 0;
  1047. }
  1048. /* Call probe_finder callback with real subprogram DIE */
  1049. static int call_probe_finder(Dwarf_Die *sp_die, struct probe_finder *pf)
  1050. {
  1051. Dwarf_Die die_mem;
  1052. Dwarf_Attribute fb_attr;
  1053. size_t nops;
  1054. int ret;
  1055. /* If no real subprogram, find a real one */
  1056. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  1057. sp_die = die_find_real_subprogram(&pf->cu_die,
  1058. pf->addr, &die_mem);
  1059. if (!sp_die) {
  1060. pr_warning("Failed to find probe point in any "
  1061. "functions.\n");
  1062. return -ENOENT;
  1063. }
  1064. }
  1065. /* Get the frame base attribute/ops */
  1066. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  1067. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  1068. if (ret <= 0 || nops == 0) {
  1069. pf->fb_ops = NULL;
  1070. #if _ELFUTILS_PREREQ(0, 142)
  1071. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  1072. pf->cfi != NULL) {
  1073. Dwarf_Frame *frame;
  1074. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  1075. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  1076. pr_warning("Failed to get call frame on 0x%jx\n",
  1077. (uintmax_t)pf->addr);
  1078. return -ENOENT;
  1079. }
  1080. #endif
  1081. }
  1082. /* Call finder's callback handler */
  1083. ret = pf->callback(sp_die, pf);
  1084. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  1085. pf->fb_ops = NULL;
  1086. return ret;
  1087. }
  1088. static int probe_point_line_walker(const char *fname, int lineno,
  1089. Dwarf_Addr addr, void *data)
  1090. {
  1091. struct probe_finder *pf = data;
  1092. int ret;
  1093. if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0)
  1094. return 0;
  1095. pf->addr = addr;
  1096. ret = call_probe_finder(NULL, pf);
  1097. /* Continue if no error, because the line will be in inline function */
  1098. return ret < 0 ? ret : 0;
  1099. }
  1100. /* Find probe point from its line number */
  1101. static int find_probe_point_by_line(struct probe_finder *pf)
  1102. {
  1103. return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf);
  1104. }
  1105. /* Find lines which match lazy pattern */
  1106. static int find_lazy_match_lines(struct list_head *head,
  1107. const char *fname, const char *pat)
  1108. {
  1109. FILE *fp;
  1110. char *line = NULL;
  1111. size_t line_len;
  1112. ssize_t len;
  1113. int count = 0, linenum = 1;
  1114. fp = fopen(fname, "r");
  1115. if (!fp) {
  1116. pr_warning("Failed to open %s: %s\n", fname, strerror(errno));
  1117. return -errno;
  1118. }
  1119. while ((len = getline(&line, &line_len, fp)) > 0) {
  1120. if (line[len - 1] == '\n')
  1121. line[len - 1] = '\0';
  1122. if (strlazymatch(line, pat)) {
  1123. line_list__add_line(head, linenum);
  1124. count++;
  1125. }
  1126. linenum++;
  1127. }
  1128. if (ferror(fp))
  1129. count = -errno;
  1130. free(line);
  1131. fclose(fp);
  1132. if (count == 0)
  1133. pr_debug("No matched lines found in %s.\n", fname);
  1134. return count;
  1135. }
  1136. static int probe_point_lazy_walker(const char *fname, int lineno,
  1137. Dwarf_Addr addr, void *data)
  1138. {
  1139. struct probe_finder *pf = data;
  1140. int ret;
  1141. if (!line_list__has_line(&pf->lcache, lineno) ||
  1142. strtailcmp(fname, pf->fname) != 0)
  1143. return 0;
  1144. pr_debug("Probe line found: line:%d addr:0x%llx\n",
  1145. lineno, (unsigned long long)addr);
  1146. pf->addr = addr;
  1147. ret = call_probe_finder(NULL, pf);
  1148. /*
  1149. * Continue if no error, because the lazy pattern will match
  1150. * to other lines
  1151. */
  1152. return ret < 0 ? ret : 0;
  1153. }
  1154. /* Find probe points from lazy pattern */
  1155. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  1156. {
  1157. int ret = 0;
  1158. if (list_empty(&pf->lcache)) {
  1159. /* Matching lazy line pattern */
  1160. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  1161. pf->pev->point.lazy_line);
  1162. if (ret <= 0)
  1163. return ret;
  1164. }
  1165. return die_walk_lines(sp_die, probe_point_lazy_walker, pf);
  1166. }
  1167. /* Callback parameter with return value */
  1168. struct dwarf_callback_param {
  1169. void *data;
  1170. int retval;
  1171. };
  1172. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  1173. {
  1174. struct dwarf_callback_param *param = data;
  1175. struct probe_finder *pf = param->data;
  1176. struct perf_probe_point *pp = &pf->pev->point;
  1177. Dwarf_Addr addr;
  1178. if (pp->lazy_line)
  1179. param->retval = find_probe_point_lazy(in_die, pf);
  1180. else {
  1181. /* Get probe address */
  1182. if (dwarf_entrypc(in_die, &addr) != 0) {
  1183. pr_warning("Failed to get entry address of %s.\n",
  1184. dwarf_diename(in_die));
  1185. param->retval = -ENOENT;
  1186. return DWARF_CB_ABORT;
  1187. }
  1188. pf->addr = addr;
  1189. pf->addr += pp->offset;
  1190. pr_debug("found inline addr: 0x%jx\n",
  1191. (uintmax_t)pf->addr);
  1192. param->retval = call_probe_finder(in_die, pf);
  1193. if (param->retval < 0)
  1194. return DWARF_CB_ABORT;
  1195. }
  1196. return DWARF_CB_OK;
  1197. }
  1198. /* Search function from function name */
  1199. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  1200. {
  1201. struct dwarf_callback_param *param = data;
  1202. struct probe_finder *pf = param->data;
  1203. struct perf_probe_point *pp = &pf->pev->point;
  1204. /* Check tag and diename */
  1205. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  1206. !die_compare_name(sp_die, pp->function))
  1207. return DWARF_CB_OK;
  1208. /* Check declared file */
  1209. if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die)))
  1210. return DWARF_CB_OK;
  1211. pf->fname = dwarf_decl_file(sp_die);
  1212. if (pp->line) { /* Function relative line */
  1213. dwarf_decl_line(sp_die, &pf->lno);
  1214. pf->lno += pp->line;
  1215. param->retval = find_probe_point_by_line(pf);
  1216. } else if (!dwarf_func_inline(sp_die)) {
  1217. /* Real function */
  1218. if (pp->lazy_line)
  1219. param->retval = find_probe_point_lazy(sp_die, pf);
  1220. else {
  1221. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  1222. pr_warning("Failed to get entry address of "
  1223. "%s.\n", dwarf_diename(sp_die));
  1224. param->retval = -ENOENT;
  1225. return DWARF_CB_ABORT;
  1226. }
  1227. pf->addr += pp->offset;
  1228. /* TODO: Check the address in this function */
  1229. param->retval = call_probe_finder(sp_die, pf);
  1230. }
  1231. } else {
  1232. struct dwarf_callback_param _param = {.data = (void *)pf,
  1233. .retval = 0};
  1234. /* Inlined function: search instances */
  1235. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  1236. &_param);
  1237. param->retval = _param.retval;
  1238. }
  1239. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  1240. }
  1241. static int find_probe_point_by_func(struct probe_finder *pf)
  1242. {
  1243. struct dwarf_callback_param _param = {.data = (void *)pf,
  1244. .retval = 0};
  1245. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  1246. return _param.retval;
  1247. }
  1248. struct pubname_callback_param {
  1249. char *function;
  1250. char *file;
  1251. Dwarf_Die *cu_die;
  1252. Dwarf_Die *sp_die;
  1253. int found;
  1254. };
  1255. static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data)
  1256. {
  1257. struct pubname_callback_param *param = data;
  1258. if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) {
  1259. if (dwarf_tag(param->sp_die) != DW_TAG_subprogram)
  1260. return DWARF_CB_OK;
  1261. if (die_compare_name(param->sp_die, param->function)) {
  1262. if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die))
  1263. return DWARF_CB_OK;
  1264. if (param->file &&
  1265. strtailcmp(param->file, dwarf_decl_file(param->sp_die)))
  1266. return DWARF_CB_OK;
  1267. param->found = 1;
  1268. return DWARF_CB_ABORT;
  1269. }
  1270. }
  1271. return DWARF_CB_OK;
  1272. }
  1273. /* Find probe points from debuginfo */
  1274. static int find_probes(int fd, struct probe_finder *pf)
  1275. {
  1276. struct perf_probe_point *pp = &pf->pev->point;
  1277. Dwarf_Off off, noff;
  1278. size_t cuhl;
  1279. Dwarf_Die *diep;
  1280. Dwarf *dbg = NULL;
  1281. Dwfl *dwfl;
  1282. Dwarf_Addr bias; /* Currently ignored */
  1283. int ret = 0;
  1284. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1285. if (!dbg) {
  1286. pr_warning("No debug information found in the vmlinux - "
  1287. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1288. close(fd); /* Without dwfl_end(), fd isn't closed. */
  1289. return -EBADF;
  1290. }
  1291. #if _ELFUTILS_PREREQ(0, 142)
  1292. /* Get the call frame information from this dwarf */
  1293. pf->cfi = dwarf_getcfi(dbg);
  1294. #endif
  1295. off = 0;
  1296. line_list__init(&pf->lcache);
  1297. /* Fastpath: lookup by function name from .debug_pubnames section */
  1298. if (pp->function) {
  1299. struct pubname_callback_param pubname_param = {
  1300. .function = pp->function,
  1301. .file = pp->file,
  1302. .cu_die = &pf->cu_die,
  1303. .sp_die = &pf->sp_die,
  1304. .found = 0,
  1305. };
  1306. struct dwarf_callback_param probe_param = {
  1307. .data = pf,
  1308. };
  1309. dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
  1310. if (pubname_param.found) {
  1311. ret = probe_point_search_cb(&pf->sp_die, &probe_param);
  1312. if (ret)
  1313. goto found;
  1314. }
  1315. }
  1316. /* Loop on CUs (Compilation Unit) */
  1317. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  1318. /* Get the DIE(Debugging Information Entry) of this CU */
  1319. diep = dwarf_offdie(dbg, off + cuhl, &pf->cu_die);
  1320. if (!diep)
  1321. continue;
  1322. /* Check if target file is included. */
  1323. if (pp->file)
  1324. pf->fname = cu_find_realpath(&pf->cu_die, pp->file);
  1325. else
  1326. pf->fname = NULL;
  1327. if (!pp->file || pf->fname) {
  1328. if (pp->function)
  1329. ret = find_probe_point_by_func(pf);
  1330. else if (pp->lazy_line)
  1331. ret = find_probe_point_lazy(NULL, pf);
  1332. else {
  1333. pf->lno = pp->line;
  1334. ret = find_probe_point_by_line(pf);
  1335. }
  1336. if (ret < 0)
  1337. break;
  1338. }
  1339. off = noff;
  1340. }
  1341. found:
  1342. line_list__free(&pf->lcache);
  1343. if (dwfl)
  1344. dwfl_end(dwfl);
  1345. return ret;
  1346. }
  1347. /* Add a found probe point into trace event list */
  1348. static int add_probe_trace_event(Dwarf_Die *sp_die, struct probe_finder *pf)
  1349. {
  1350. struct trace_event_finder *tf =
  1351. container_of(pf, struct trace_event_finder, pf);
  1352. struct probe_trace_event *tev;
  1353. int ret, i;
  1354. /* Check number of tevs */
  1355. if (tf->ntevs == tf->max_tevs) {
  1356. pr_warning("Too many( > %d) probe point found.\n",
  1357. tf->max_tevs);
  1358. return -ERANGE;
  1359. }
  1360. tev = &tf->tevs[tf->ntevs++];
  1361. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1362. &tev->point);
  1363. if (ret < 0)
  1364. return ret;
  1365. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  1366. tev->point.offset);
  1367. /* Find each argument */
  1368. tev->nargs = pf->pev->nargs;
  1369. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1370. if (tev->args == NULL)
  1371. return -ENOMEM;
  1372. for (i = 0; i < pf->pev->nargs; i++) {
  1373. pf->pvar = &pf->pev->args[i];
  1374. pf->tvar = &tev->args[i];
  1375. ret = find_variable(sp_die, pf);
  1376. if (ret != 0)
  1377. return ret;
  1378. }
  1379. return 0;
  1380. }
  1381. /* Find probe_trace_events specified by perf_probe_event from debuginfo */
  1382. int find_probe_trace_events(int fd, struct perf_probe_event *pev,
  1383. struct probe_trace_event **tevs, int max_tevs)
  1384. {
  1385. struct trace_event_finder tf = {
  1386. .pf = {.pev = pev, .callback = add_probe_trace_event},
  1387. .max_tevs = max_tevs};
  1388. int ret;
  1389. /* Allocate result tevs array */
  1390. *tevs = zalloc(sizeof(struct probe_trace_event) * max_tevs);
  1391. if (*tevs == NULL)
  1392. return -ENOMEM;
  1393. tf.tevs = *tevs;
  1394. tf.ntevs = 0;
  1395. ret = find_probes(fd, &tf.pf);
  1396. if (ret < 0) {
  1397. free(*tevs);
  1398. *tevs = NULL;
  1399. return ret;
  1400. }
  1401. return (ret < 0) ? ret : tf.ntevs;
  1402. }
  1403. #define MAX_VAR_LEN 64
  1404. /* Collect available variables in this scope */
  1405. static int collect_variables_cb(Dwarf_Die *die_mem, void *data)
  1406. {
  1407. struct available_var_finder *af = data;
  1408. struct variable_list *vl;
  1409. char buf[MAX_VAR_LEN];
  1410. int tag, ret;
  1411. vl = &af->vls[af->nvls - 1];
  1412. tag = dwarf_tag(die_mem);
  1413. if (tag == DW_TAG_formal_parameter ||
  1414. tag == DW_TAG_variable) {
  1415. ret = convert_variable_location(die_mem, af->pf.addr,
  1416. af->pf.fb_ops, NULL);
  1417. if (ret == 0) {
  1418. ret = die_get_varname(die_mem, buf, MAX_VAR_LEN);
  1419. pr_debug2("Add new var: %s\n", buf);
  1420. if (ret > 0)
  1421. strlist__add(vl->vars, buf);
  1422. }
  1423. }
  1424. if (af->child && dwarf_haspc(die_mem, af->pf.addr))
  1425. return DIE_FIND_CB_CONTINUE;
  1426. else
  1427. return DIE_FIND_CB_SIBLING;
  1428. }
  1429. /* Add a found vars into available variables list */
  1430. static int add_available_vars(Dwarf_Die *sp_die, struct probe_finder *pf)
  1431. {
  1432. struct available_var_finder *af =
  1433. container_of(pf, struct available_var_finder, pf);
  1434. struct variable_list *vl;
  1435. Dwarf_Die die_mem, *scopes = NULL;
  1436. int ret, nscopes;
  1437. /* Check number of tevs */
  1438. if (af->nvls == af->max_vls) {
  1439. pr_warning("Too many( > %d) probe point found.\n", af->max_vls);
  1440. return -ERANGE;
  1441. }
  1442. vl = &af->vls[af->nvls++];
  1443. ret = convert_to_trace_point(sp_die, pf->addr, pf->pev->point.retprobe,
  1444. &vl->point);
  1445. if (ret < 0)
  1446. return ret;
  1447. pr_debug("Probe point found: %s+%lu\n", vl->point.symbol,
  1448. vl->point.offset);
  1449. /* Find local variables */
  1450. vl->vars = strlist__new(true, NULL);
  1451. if (vl->vars == NULL)
  1452. return -ENOMEM;
  1453. af->child = true;
  1454. die_find_child(sp_die, collect_variables_cb, (void *)af, &die_mem);
  1455. /* Find external variables */
  1456. if (!af->externs)
  1457. goto out;
  1458. /* Don't need to search child DIE for externs. */
  1459. af->child = false;
  1460. nscopes = dwarf_getscopes_die(sp_die, &scopes);
  1461. while (nscopes-- > 1)
  1462. die_find_child(&scopes[nscopes], collect_variables_cb,
  1463. (void *)af, &die_mem);
  1464. if (scopes)
  1465. free(scopes);
  1466. out:
  1467. if (strlist__empty(vl->vars)) {
  1468. strlist__delete(vl->vars);
  1469. vl->vars = NULL;
  1470. }
  1471. return ret;
  1472. }
  1473. /* Find available variables at given probe point */
  1474. int find_available_vars_at(int fd, struct perf_probe_event *pev,
  1475. struct variable_list **vls, int max_vls,
  1476. bool externs)
  1477. {
  1478. struct available_var_finder af = {
  1479. .pf = {.pev = pev, .callback = add_available_vars},
  1480. .max_vls = max_vls, .externs = externs};
  1481. int ret;
  1482. /* Allocate result vls array */
  1483. *vls = zalloc(sizeof(struct variable_list) * max_vls);
  1484. if (*vls == NULL)
  1485. return -ENOMEM;
  1486. af.vls = *vls;
  1487. af.nvls = 0;
  1488. ret = find_probes(fd, &af.pf);
  1489. if (ret < 0) {
  1490. /* Free vlist for error */
  1491. while (af.nvls--) {
  1492. if (af.vls[af.nvls].point.symbol)
  1493. free(af.vls[af.nvls].point.symbol);
  1494. if (af.vls[af.nvls].vars)
  1495. strlist__delete(af.vls[af.nvls].vars);
  1496. }
  1497. free(af.vls);
  1498. *vls = NULL;
  1499. return ret;
  1500. }
  1501. return (ret < 0) ? ret : af.nvls;
  1502. }
  1503. /* Reverse search */
  1504. int find_perf_probe_point(unsigned long addr, struct perf_probe_point *ppt)
  1505. {
  1506. Dwarf_Die cudie, spdie, indie;
  1507. Dwarf *dbg = NULL;
  1508. Dwfl *dwfl = NULL;
  1509. Dwarf_Addr _addr, baseaddr, bias = 0;
  1510. const char *fname = NULL, *func = NULL, *tmp;
  1511. int baseline = 0, lineno = 0, ret = 0;
  1512. /* Open the live linux kernel */
  1513. dbg = dwfl_init_live_kernel_dwarf(addr, &dwfl, &bias);
  1514. if (!dbg) {
  1515. pr_warning("No debug information found in the vmlinux - "
  1516. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1517. ret = -EINVAL;
  1518. goto end;
  1519. }
  1520. /* Adjust address with bias */
  1521. addr += bias;
  1522. /* Find cu die */
  1523. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr - bias, &cudie)) {
  1524. pr_warning("Failed to find debug information for address %lx\n",
  1525. addr);
  1526. ret = -EINVAL;
  1527. goto end;
  1528. }
  1529. /* Find a corresponding line (filename and lineno) */
  1530. cu_find_lineinfo(&cudie, addr, &fname, &lineno);
  1531. /* Don't care whether it failed or not */
  1532. /* Find a corresponding function (name, baseline and baseaddr) */
  1533. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  1534. /* Get function entry information */
  1535. tmp = dwarf_diename(&spdie);
  1536. if (!tmp ||
  1537. dwarf_entrypc(&spdie, &baseaddr) != 0 ||
  1538. dwarf_decl_line(&spdie, &baseline) != 0)
  1539. goto post;
  1540. func = tmp;
  1541. if (addr == (unsigned long)baseaddr)
  1542. /* Function entry - Relative line number is 0 */
  1543. lineno = baseline;
  1544. else if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  1545. &indie)) {
  1546. if (dwarf_entrypc(&indie, &_addr) == 0 &&
  1547. _addr == addr)
  1548. /*
  1549. * addr is at an inline function entry.
  1550. * In this case, lineno should be the call-site
  1551. * line number.
  1552. */
  1553. lineno = die_get_call_lineno(&indie);
  1554. else {
  1555. /*
  1556. * addr is in an inline function body.
  1557. * Since lineno points one of the lines
  1558. * of the inline function, baseline should
  1559. * be the entry line of the inline function.
  1560. */
  1561. tmp = dwarf_diename(&indie);
  1562. if (tmp &&
  1563. dwarf_decl_line(&spdie, &baseline) == 0)
  1564. func = tmp;
  1565. }
  1566. }
  1567. }
  1568. post:
  1569. /* Make a relative line number or an offset */
  1570. if (lineno)
  1571. ppt->line = lineno - baseline;
  1572. else if (func)
  1573. ppt->offset = addr - (unsigned long)baseaddr;
  1574. /* Duplicate strings */
  1575. if (func) {
  1576. ppt->function = strdup(func);
  1577. if (ppt->function == NULL) {
  1578. ret = -ENOMEM;
  1579. goto end;
  1580. }
  1581. }
  1582. if (fname) {
  1583. ppt->file = strdup(fname);
  1584. if (ppt->file == NULL) {
  1585. if (ppt->function) {
  1586. free(ppt->function);
  1587. ppt->function = NULL;
  1588. }
  1589. ret = -ENOMEM;
  1590. goto end;
  1591. }
  1592. }
  1593. end:
  1594. if (dwfl)
  1595. dwfl_end(dwfl);
  1596. if (ret == 0 && (fname || func))
  1597. ret = 1; /* Found a point */
  1598. return ret;
  1599. }
  1600. /* Add a line and store the src path */
  1601. static int line_range_add_line(const char *src, unsigned int lineno,
  1602. struct line_range *lr)
  1603. {
  1604. /* Copy source path */
  1605. if (!lr->path) {
  1606. lr->path = strdup(src);
  1607. if (lr->path == NULL)
  1608. return -ENOMEM;
  1609. }
  1610. return line_list__add_line(&lr->line_list, lineno);
  1611. }
  1612. static int line_range_walk_cb(const char *fname, int lineno,
  1613. Dwarf_Addr addr __used,
  1614. void *data)
  1615. {
  1616. struct line_finder *lf = data;
  1617. if ((strtailcmp(fname, lf->fname) != 0) ||
  1618. (lf->lno_s > lineno || lf->lno_e < lineno))
  1619. return 0;
  1620. if (line_range_add_line(fname, lineno, lf->lr) < 0)
  1621. return -EINVAL;
  1622. return 0;
  1623. }
  1624. /* Find line range from its line number */
  1625. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1626. {
  1627. int ret;
  1628. ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf);
  1629. /* Update status */
  1630. if (ret >= 0)
  1631. if (!list_empty(&lf->lr->line_list))
  1632. ret = lf->found = 1;
  1633. else
  1634. ret = 0; /* Lines are not found */
  1635. else {
  1636. free(lf->lr->path);
  1637. lf->lr->path = NULL;
  1638. }
  1639. return ret;
  1640. }
  1641. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1642. {
  1643. struct dwarf_callback_param *param = data;
  1644. param->retval = find_line_range_by_line(in_die, param->data);
  1645. return DWARF_CB_ABORT; /* No need to find other instances */
  1646. }
  1647. /* Search function from function name */
  1648. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1649. {
  1650. struct dwarf_callback_param *param = data;
  1651. struct line_finder *lf = param->data;
  1652. struct line_range *lr = lf->lr;
  1653. /* Check declared file */
  1654. if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die)))
  1655. return DWARF_CB_OK;
  1656. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1657. die_compare_name(sp_die, lr->function)) {
  1658. lf->fname = dwarf_decl_file(sp_die);
  1659. dwarf_decl_line(sp_die, &lr->offset);
  1660. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1661. lf->lno_s = lr->offset + lr->start;
  1662. if (lf->lno_s < 0) /* Overflow */
  1663. lf->lno_s = INT_MAX;
  1664. lf->lno_e = lr->offset + lr->end;
  1665. if (lf->lno_e < 0) /* Overflow */
  1666. lf->lno_e = INT_MAX;
  1667. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1668. lr->start = lf->lno_s;
  1669. lr->end = lf->lno_e;
  1670. if (dwarf_func_inline(sp_die)) {
  1671. struct dwarf_callback_param _param;
  1672. _param.data = (void *)lf;
  1673. _param.retval = 0;
  1674. dwarf_func_inline_instances(sp_die,
  1675. line_range_inline_cb,
  1676. &_param);
  1677. param->retval = _param.retval;
  1678. } else
  1679. param->retval = find_line_range_by_line(sp_die, lf);
  1680. return DWARF_CB_ABORT;
  1681. }
  1682. return DWARF_CB_OK;
  1683. }
  1684. static int find_line_range_by_func(struct line_finder *lf)
  1685. {
  1686. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1687. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1688. return param.retval;
  1689. }
  1690. int find_line_range(int fd, struct line_range *lr)
  1691. {
  1692. struct line_finder lf = {.lr = lr, .found = 0};
  1693. int ret = 0;
  1694. Dwarf_Off off = 0, noff;
  1695. size_t cuhl;
  1696. Dwarf_Die *diep;
  1697. Dwarf *dbg = NULL;
  1698. Dwfl *dwfl;
  1699. Dwarf_Addr bias; /* Currently ignored */
  1700. const char *comp_dir;
  1701. dbg = dwfl_init_offline_dwarf(fd, &dwfl, &bias);
  1702. if (!dbg) {
  1703. pr_warning("No debug information found in the vmlinux - "
  1704. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1705. close(fd); /* Without dwfl_end(), fd isn't closed. */
  1706. return -EBADF;
  1707. }
  1708. /* Fastpath: lookup by function name from .debug_pubnames section */
  1709. if (lr->function) {
  1710. struct pubname_callback_param pubname_param = {
  1711. .function = lr->function, .file = lr->file,
  1712. .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0};
  1713. struct dwarf_callback_param line_range_param = {
  1714. .data = (void *)&lf, .retval = 0};
  1715. dwarf_getpubnames(dbg, pubname_search_cb, &pubname_param, 0);
  1716. if (pubname_param.found) {
  1717. line_range_search_cb(&lf.sp_die, &line_range_param);
  1718. if (lf.found)
  1719. goto found;
  1720. }
  1721. }
  1722. /* Loop on CUs (Compilation Unit) */
  1723. while (!lf.found && ret >= 0) {
  1724. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1725. break;
  1726. /* Get the DIE(Debugging Information Entry) of this CU */
  1727. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1728. if (!diep)
  1729. continue;
  1730. /* Check if target file is included. */
  1731. if (lr->file)
  1732. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1733. else
  1734. lf.fname = 0;
  1735. if (!lr->file || lf.fname) {
  1736. if (lr->function)
  1737. ret = find_line_range_by_func(&lf);
  1738. else {
  1739. lf.lno_s = lr->start;
  1740. lf.lno_e = lr->end;
  1741. ret = find_line_range_by_line(NULL, &lf);
  1742. }
  1743. }
  1744. off = noff;
  1745. }
  1746. found:
  1747. /* Store comp_dir */
  1748. if (lf.found) {
  1749. comp_dir = cu_get_comp_dir(&lf.cu_die);
  1750. if (comp_dir) {
  1751. lr->comp_dir = strdup(comp_dir);
  1752. if (!lr->comp_dir)
  1753. ret = -ENOMEM;
  1754. }
  1755. }
  1756. pr_debug("path: %s\n", lr->path);
  1757. dwfl_end(dwfl);
  1758. return (ret < 0) ? ret : lf.found;
  1759. }