probe-finder.c 47 KB

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