probe-finder.c 47 KB

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