probe-finder.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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 "string.h"
  35. #include "event.h"
  36. #include "debug.h"
  37. #include "util.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 wrappers */
  107. /* Find the realpath of the target file. */
  108. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  109. {
  110. Dwarf_Files *files;
  111. size_t nfiles, i;
  112. const char *src = NULL;
  113. int ret;
  114. if (!fname)
  115. return NULL;
  116. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  117. if (ret != 0)
  118. return NULL;
  119. for (i = 0; i < nfiles; i++) {
  120. src = dwarf_filesrc(files, i, NULL, NULL);
  121. if (strtailcmp(src, fname) == 0)
  122. break;
  123. }
  124. if (i == nfiles)
  125. return NULL;
  126. return src;
  127. }
  128. /* Compare diename and tname */
  129. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  130. {
  131. const char *name;
  132. name = dwarf_diename(dw_die);
  133. return name ? strcmp(tname, name) : -1;
  134. }
  135. /* Get type die, but skip qualifiers and typedef */
  136. static Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
  137. {
  138. Dwarf_Attribute attr;
  139. int tag;
  140. do {
  141. if (dwarf_attr(vr_die, DW_AT_type, &attr) == NULL ||
  142. dwarf_formref_die(&attr, die_mem) == NULL)
  143. return NULL;
  144. tag = dwarf_tag(die_mem);
  145. vr_die = die_mem;
  146. } while (tag == DW_TAG_const_type ||
  147. tag == DW_TAG_restrict_type ||
  148. tag == DW_TAG_volatile_type ||
  149. tag == DW_TAG_shared_type ||
  150. tag == DW_TAG_typedef);
  151. return die_mem;
  152. }
  153. static bool die_is_signed_type(Dwarf_Die *tp_die)
  154. {
  155. Dwarf_Attribute attr;
  156. Dwarf_Word ret;
  157. if (dwarf_attr(tp_die, DW_AT_encoding, &attr) == NULL ||
  158. dwarf_formudata(&attr, &ret) != 0)
  159. return false;
  160. return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
  161. ret == DW_ATE_signed_fixed);
  162. }
  163. static int die_get_byte_size(Dwarf_Die *tp_die)
  164. {
  165. Dwarf_Attribute attr;
  166. Dwarf_Word ret;
  167. if (dwarf_attr(tp_die, DW_AT_byte_size, &attr) == NULL ||
  168. dwarf_formudata(&attr, &ret) != 0)
  169. return 0;
  170. return (int)ret;
  171. }
  172. /* Get data_member_location offset */
  173. static int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
  174. {
  175. Dwarf_Attribute attr;
  176. Dwarf_Op *expr;
  177. size_t nexpr;
  178. int ret;
  179. if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
  180. return -ENOENT;
  181. if (dwarf_formudata(&attr, offs) != 0) {
  182. /* DW_AT_data_member_location should be DW_OP_plus_uconst */
  183. ret = dwarf_getlocation(&attr, &expr, &nexpr);
  184. if (ret < 0 || nexpr == 0)
  185. return -ENOENT;
  186. if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
  187. pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
  188. expr[0].atom, nexpr);
  189. return -ENOTSUP;
  190. }
  191. *offs = (Dwarf_Word)expr[0].number;
  192. }
  193. return 0;
  194. }
  195. /* Return values for die_find callbacks */
  196. enum {
  197. DIE_FIND_CB_FOUND = 0, /* End of Search */
  198. DIE_FIND_CB_CHILD = 1, /* Search only children */
  199. DIE_FIND_CB_SIBLING = 2, /* Search only siblings */
  200. DIE_FIND_CB_CONTINUE = 3, /* Search children and siblings */
  201. };
  202. /* Search a child die */
  203. static Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
  204. int (*callback)(Dwarf_Die *, void *),
  205. void *data, Dwarf_Die *die_mem)
  206. {
  207. Dwarf_Die child_die;
  208. int ret;
  209. ret = dwarf_child(rt_die, die_mem);
  210. if (ret != 0)
  211. return NULL;
  212. do {
  213. ret = callback(die_mem, data);
  214. if (ret == DIE_FIND_CB_FOUND)
  215. return die_mem;
  216. if ((ret & DIE_FIND_CB_CHILD) &&
  217. die_find_child(die_mem, callback, data, &child_die)) {
  218. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  219. return die_mem;
  220. }
  221. } while ((ret & DIE_FIND_CB_SIBLING) &&
  222. dwarf_siblingof(die_mem, die_mem) == 0);
  223. return NULL;
  224. }
  225. struct __addr_die_search_param {
  226. Dwarf_Addr addr;
  227. Dwarf_Die *die_mem;
  228. };
  229. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  230. {
  231. struct __addr_die_search_param *ad = data;
  232. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  233. dwarf_haspc(fn_die, ad->addr)) {
  234. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  235. return DWARF_CB_ABORT;
  236. }
  237. return DWARF_CB_OK;
  238. }
  239. /* Search a real subprogram including this line, */
  240. static Dwarf_Die *die_find_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  241. Dwarf_Die *die_mem)
  242. {
  243. struct __addr_die_search_param ad;
  244. ad.addr = addr;
  245. ad.die_mem = die_mem;
  246. /* dwarf_getscopes can't find subprogram. */
  247. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  248. return NULL;
  249. else
  250. return die_mem;
  251. }
  252. /* die_find callback for inline function search */
  253. static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
  254. {
  255. Dwarf_Addr *addr = data;
  256. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  257. dwarf_haspc(die_mem, *addr))
  258. return DIE_FIND_CB_FOUND;
  259. return DIE_FIND_CB_CONTINUE;
  260. }
  261. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  262. static Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  263. Dwarf_Die *die_mem)
  264. {
  265. return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
  266. }
  267. static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
  268. {
  269. const char *name = data;
  270. int tag;
  271. tag = dwarf_tag(die_mem);
  272. if ((tag == DW_TAG_formal_parameter ||
  273. tag == DW_TAG_variable) &&
  274. (die_compare_name(die_mem, name) == 0))
  275. return DIE_FIND_CB_FOUND;
  276. return DIE_FIND_CB_CONTINUE;
  277. }
  278. /* Find a variable called 'name' */
  279. static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
  280. Dwarf_Die *die_mem)
  281. {
  282. return die_find_child(sp_die, __die_find_variable_cb, (void *)name,
  283. die_mem);
  284. }
  285. static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
  286. {
  287. const char *name = data;
  288. if ((dwarf_tag(die_mem) == DW_TAG_member) &&
  289. (die_compare_name(die_mem, name) == 0))
  290. return DIE_FIND_CB_FOUND;
  291. return DIE_FIND_CB_SIBLING;
  292. }
  293. /* Find a member called 'name' */
  294. static Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
  295. Dwarf_Die *die_mem)
  296. {
  297. return die_find_child(st_die, __die_find_member_cb, (void *)name,
  298. die_mem);
  299. }
  300. /*
  301. * Probe finder related functions
  302. */
  303. /* Show a location */
  304. static int convert_location(Dwarf_Op *op, struct probe_finder *pf)
  305. {
  306. unsigned int regn;
  307. Dwarf_Word offs = 0;
  308. bool ref = false;
  309. const char *regs;
  310. struct kprobe_trace_arg *tvar = pf->tvar;
  311. /* If this is based on frame buffer, set the offset */
  312. if (op->atom == DW_OP_fbreg) {
  313. if (pf->fb_ops == NULL) {
  314. pr_warning("The attribute of frame base is not "
  315. "supported.\n");
  316. return -ENOTSUP;
  317. }
  318. ref = true;
  319. offs = op->number;
  320. op = &pf->fb_ops[0];
  321. }
  322. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  323. regn = op->atom - DW_OP_breg0;
  324. offs += op->number;
  325. ref = true;
  326. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  327. regn = op->atom - DW_OP_reg0;
  328. } else if (op->atom == DW_OP_bregx) {
  329. regn = op->number;
  330. offs += op->number2;
  331. ref = true;
  332. } else if (op->atom == DW_OP_regx) {
  333. regn = op->number;
  334. } else {
  335. pr_warning("DW_OP %x is not supported.\n", op->atom);
  336. return -ENOTSUP;
  337. }
  338. regs = get_arch_regstr(regn);
  339. if (!regs) {
  340. pr_warning("Mapping for DWARF register number %u missing on this architecture.", regn);
  341. return -ERANGE;
  342. }
  343. tvar->value = strdup(regs);
  344. if (tvar->value == NULL)
  345. return -ENOMEM;
  346. if (ref) {
  347. tvar->ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
  348. if (tvar->ref == NULL)
  349. return -ENOMEM;
  350. tvar->ref->offset = (long)offs;
  351. }
  352. return 0;
  353. }
  354. static int convert_variable_type(Dwarf_Die *vr_die,
  355. struct kprobe_trace_arg *targ)
  356. {
  357. Dwarf_Die type;
  358. char buf[16];
  359. int ret;
  360. if (die_get_real_type(vr_die, &type) == NULL) {
  361. pr_warning("Failed to get a type information of %s.\n",
  362. dwarf_diename(vr_die));
  363. return -ENOENT;
  364. }
  365. ret = die_get_byte_size(&type) * 8;
  366. if (ret) {
  367. /* Check the bitwidth */
  368. if (ret > MAX_BASIC_TYPE_BITS) {
  369. pr_info("%s exceeds max-bitwidth."
  370. " Cut down to %d bits.\n",
  371. dwarf_diename(&type), MAX_BASIC_TYPE_BITS);
  372. ret = MAX_BASIC_TYPE_BITS;
  373. }
  374. ret = snprintf(buf, 16, "%c%d",
  375. die_is_signed_type(&type) ? 's' : 'u', ret);
  376. if (ret < 0 || ret >= 16) {
  377. if (ret >= 16)
  378. ret = -E2BIG;
  379. pr_warning("Failed to convert variable type: %s\n",
  380. strerror(-ret));
  381. return ret;
  382. }
  383. targ->type = strdup(buf);
  384. if (targ->type == NULL)
  385. return -ENOMEM;
  386. }
  387. return 0;
  388. }
  389. static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname,
  390. struct perf_probe_arg_field *field,
  391. struct kprobe_trace_arg_ref **ref_ptr,
  392. Dwarf_Die *die_mem)
  393. {
  394. struct kprobe_trace_arg_ref *ref = *ref_ptr;
  395. Dwarf_Die type;
  396. Dwarf_Word offs;
  397. int ret;
  398. pr_debug("converting %s in %s\n", field->name, varname);
  399. if (die_get_real_type(vr_die, &type) == NULL) {
  400. pr_warning("Failed to get the type of %s.\n", varname);
  401. return -ENOENT;
  402. }
  403. /* Check the pointer and dereference */
  404. if (dwarf_tag(&type) == DW_TAG_pointer_type) {
  405. if (!field->ref) {
  406. pr_err("Semantic error: %s must be referred by '->'\n",
  407. field->name);
  408. return -EINVAL;
  409. }
  410. /* Get the type pointed by this pointer */
  411. if (die_get_real_type(&type, &type) == NULL) {
  412. pr_warning("Failed to get the type of %s.\n", varname);
  413. return -ENOENT;
  414. }
  415. /* Verify it is a data structure */
  416. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  417. pr_warning("%s is not a data structure.\n", varname);
  418. return -EINVAL;
  419. }
  420. ref = zalloc(sizeof(struct kprobe_trace_arg_ref));
  421. if (ref == NULL)
  422. return -ENOMEM;
  423. if (*ref_ptr)
  424. (*ref_ptr)->next = ref;
  425. else
  426. *ref_ptr = ref;
  427. } else {
  428. /* Verify it is a data structure */
  429. if (dwarf_tag(&type) != DW_TAG_structure_type) {
  430. pr_warning("%s is not a data structure.\n", varname);
  431. return -EINVAL;
  432. }
  433. if (field->ref) {
  434. pr_err("Semantic error: %s must be referred by '.'\n",
  435. field->name);
  436. return -EINVAL;
  437. }
  438. if (!ref) {
  439. pr_warning("Structure on a register is not "
  440. "supported yet.\n");
  441. return -ENOTSUP;
  442. }
  443. }
  444. if (die_find_member(&type, field->name, die_mem) == NULL) {
  445. pr_warning("%s(tyep:%s) has no member %s.\n", varname,
  446. dwarf_diename(&type), field->name);
  447. return -EINVAL;
  448. }
  449. /* Get the offset of the field */
  450. ret = die_get_data_member_location(die_mem, &offs);
  451. if (ret < 0) {
  452. pr_warning("Failed to get the offset of %s.\n", field->name);
  453. return ret;
  454. }
  455. ref->offset += (long)offs;
  456. /* Converting next field */
  457. if (field->next)
  458. return convert_variable_fields(die_mem, field->name,
  459. field->next, &ref, die_mem);
  460. else
  461. return 0;
  462. }
  463. /* Show a variables in kprobe event format */
  464. static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  465. {
  466. Dwarf_Attribute attr;
  467. Dwarf_Die die_mem;
  468. Dwarf_Op *expr;
  469. size_t nexpr;
  470. int ret;
  471. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  472. goto error;
  473. /* TODO: handle more than 1 exprs */
  474. ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
  475. if (ret <= 0 || nexpr == 0)
  476. goto error;
  477. ret = convert_location(expr, pf);
  478. if (ret == 0 && pf->pvar->field) {
  479. ret = convert_variable_fields(vr_die, pf->pvar->var,
  480. pf->pvar->field, &pf->tvar->ref,
  481. &die_mem);
  482. vr_die = &die_mem;
  483. }
  484. if (ret == 0) {
  485. if (pf->pvar->type) {
  486. pf->tvar->type = strdup(pf->pvar->type);
  487. if (pf->tvar->type == NULL)
  488. ret = -ENOMEM;
  489. } else
  490. ret = convert_variable_type(vr_die, pf->tvar);
  491. }
  492. /* *expr will be cached in libdw. Don't free it. */
  493. return ret;
  494. error:
  495. /* TODO: Support const_value */
  496. pr_err("Failed to find the location of %s at this address.\n"
  497. " Perhaps, it has been optimized out.\n", pf->pvar->var);
  498. return -ENOENT;
  499. }
  500. /* Find a variable in a subprogram die */
  501. static int find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  502. {
  503. Dwarf_Die vr_die;
  504. char buf[32], *ptr;
  505. int ret;
  506. /* TODO: Support arrays */
  507. if (pf->pvar->name)
  508. pf->tvar->name = strdup(pf->pvar->name);
  509. else {
  510. ret = synthesize_perf_probe_arg(pf->pvar, buf, 32);
  511. if (ret < 0)
  512. return ret;
  513. ptr = strchr(buf, ':'); /* Change type separator to _ */
  514. if (ptr)
  515. *ptr = '_';
  516. pf->tvar->name = strdup(buf);
  517. }
  518. if (pf->tvar->name == NULL)
  519. return -ENOMEM;
  520. if (!is_c_varname(pf->pvar->var)) {
  521. /* Copy raw parameters */
  522. pf->tvar->value = strdup(pf->pvar->var);
  523. if (pf->tvar->value == NULL)
  524. return -ENOMEM;
  525. else
  526. return 0;
  527. }
  528. pr_debug("Searching '%s' variable in context.\n",
  529. pf->pvar->var);
  530. /* Search child die for local variables and parameters. */
  531. if (!die_find_variable(sp_die, pf->pvar->var, &vr_die)) {
  532. pr_warning("Failed to find '%s' in this function.\n",
  533. pf->pvar->var);
  534. return -ENOENT;
  535. }
  536. return convert_variable(&vr_die, pf);
  537. }
  538. /* Show a probe point to output buffer */
  539. static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  540. {
  541. struct kprobe_trace_event *tev;
  542. Dwarf_Addr eaddr;
  543. Dwarf_Die die_mem;
  544. const char *name;
  545. int ret, i;
  546. Dwarf_Attribute fb_attr;
  547. size_t nops;
  548. if (pf->ntevs == pf->max_tevs) {
  549. pr_warning("Too many( > %d) probe point found.\n",
  550. pf->max_tevs);
  551. return -ERANGE;
  552. }
  553. tev = &pf->tevs[pf->ntevs++];
  554. /* If no real subprogram, find a real one */
  555. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  556. sp_die = die_find_real_subprogram(&pf->cu_die,
  557. pf->addr, &die_mem);
  558. if (!sp_die) {
  559. pr_warning("Failed to find probe point in any "
  560. "functions.\n");
  561. return -ENOENT;
  562. }
  563. }
  564. /* Copy the name of probe point */
  565. name = dwarf_diename(sp_die);
  566. if (name) {
  567. if (dwarf_entrypc(sp_die, &eaddr) != 0) {
  568. pr_warning("Failed to get entry pc of %s\n",
  569. dwarf_diename(sp_die));
  570. return -ENOENT;
  571. }
  572. tev->point.symbol = strdup(name);
  573. if (tev->point.symbol == NULL)
  574. return -ENOMEM;
  575. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  576. } else
  577. /* This function has no name. */
  578. tev->point.offset = (unsigned long)pf->addr;
  579. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  580. tev->point.offset);
  581. /* Get the frame base attribute/ops */
  582. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  583. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  584. if (ret <= 0 || nops == 0) {
  585. pf->fb_ops = NULL;
  586. #if _ELFUTILS_PREREQ(0, 142)
  587. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  588. pf->cfi != NULL) {
  589. Dwarf_Frame *frame;
  590. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  591. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  592. pr_warning("Failed to get CFA on 0x%jx\n",
  593. (uintmax_t)pf->addr);
  594. return -ENOENT;
  595. }
  596. #endif
  597. }
  598. /* Find each argument */
  599. tev->nargs = pf->pev->nargs;
  600. tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  601. if (tev->args == NULL)
  602. return -ENOMEM;
  603. for (i = 0; i < pf->pev->nargs; i++) {
  604. pf->pvar = &pf->pev->args[i];
  605. pf->tvar = &tev->args[i];
  606. ret = find_variable(sp_die, pf);
  607. if (ret != 0)
  608. return ret;
  609. }
  610. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  611. pf->fb_ops = NULL;
  612. return 0;
  613. }
  614. /* Find probe point from its line number */
  615. static int find_probe_point_by_line(struct probe_finder *pf)
  616. {
  617. Dwarf_Lines *lines;
  618. Dwarf_Line *line;
  619. size_t nlines, i;
  620. Dwarf_Addr addr;
  621. int lineno;
  622. int ret = 0;
  623. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  624. pr_warning("No source lines found in this CU.\n");
  625. return -ENOENT;
  626. }
  627. for (i = 0; i < nlines && ret == 0; i++) {
  628. line = dwarf_onesrcline(lines, i);
  629. if (dwarf_lineno(line, &lineno) != 0 ||
  630. lineno != pf->lno)
  631. continue;
  632. /* TODO: Get fileno from line, but how? */
  633. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  634. continue;
  635. if (dwarf_lineaddr(line, &addr) != 0) {
  636. pr_warning("Failed to get the address of the line.\n");
  637. return -ENOENT;
  638. }
  639. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  640. (int)i, lineno, (uintmax_t)addr);
  641. pf->addr = addr;
  642. ret = convert_probe_point(NULL, pf);
  643. /* Continuing, because target line might be inlined. */
  644. }
  645. return ret;
  646. }
  647. /* Find lines which match lazy pattern */
  648. static int find_lazy_match_lines(struct list_head *head,
  649. const char *fname, const char *pat)
  650. {
  651. char *fbuf, *p1, *p2;
  652. int fd, line, nlines = -1;
  653. struct stat st;
  654. fd = open(fname, O_RDONLY);
  655. if (fd < 0) {
  656. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  657. return -errno;
  658. }
  659. if (fstat(fd, &st) < 0) {
  660. pr_warning("Failed to get the size of %s: %s\n",
  661. fname, strerror(errno));
  662. nlines = -errno;
  663. goto out_close;
  664. }
  665. nlines = -ENOMEM;
  666. fbuf = malloc(st.st_size + 2);
  667. if (fbuf == NULL)
  668. goto out_close;
  669. if (read(fd, fbuf, st.st_size) < 0) {
  670. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  671. nlines = -errno;
  672. goto out_free_fbuf;
  673. }
  674. fbuf[st.st_size] = '\n'; /* Dummy line */
  675. fbuf[st.st_size + 1] = '\0';
  676. p1 = fbuf;
  677. line = 1;
  678. nlines = 0;
  679. while ((p2 = strchr(p1, '\n')) != NULL) {
  680. *p2 = '\0';
  681. if (strlazymatch(p1, pat)) {
  682. line_list__add_line(head, line);
  683. nlines++;
  684. }
  685. line++;
  686. p1 = p2 + 1;
  687. }
  688. out_free_fbuf:
  689. free(fbuf);
  690. out_close:
  691. close(fd);
  692. return nlines;
  693. }
  694. /* Find probe points from lazy pattern */
  695. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  696. {
  697. Dwarf_Lines *lines;
  698. Dwarf_Line *line;
  699. size_t nlines, i;
  700. Dwarf_Addr addr;
  701. Dwarf_Die die_mem;
  702. int lineno;
  703. int ret = 0;
  704. if (list_empty(&pf->lcache)) {
  705. /* Matching lazy line pattern */
  706. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  707. pf->pev->point.lazy_line);
  708. if (ret == 0) {
  709. pr_debug("No matched lines found in %s.\n", pf->fname);
  710. return 0;
  711. } else if (ret < 0)
  712. return ret;
  713. }
  714. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  715. pr_warning("No source lines found in this CU.\n");
  716. return -ENOENT;
  717. }
  718. for (i = 0; i < nlines && ret >= 0; i++) {
  719. line = dwarf_onesrcline(lines, i);
  720. if (dwarf_lineno(line, &lineno) != 0 ||
  721. !line_list__has_line(&pf->lcache, lineno))
  722. continue;
  723. /* TODO: Get fileno from line, but how? */
  724. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  725. continue;
  726. if (dwarf_lineaddr(line, &addr) != 0) {
  727. pr_debug("Failed to get the address of line %d.\n",
  728. lineno);
  729. continue;
  730. }
  731. if (sp_die) {
  732. /* Address filtering 1: does sp_die include addr? */
  733. if (!dwarf_haspc(sp_die, addr))
  734. continue;
  735. /* Address filtering 2: No child include addr? */
  736. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  737. continue;
  738. }
  739. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  740. (int)i, lineno, (unsigned long long)addr);
  741. pf->addr = addr;
  742. ret = convert_probe_point(sp_die, pf);
  743. /* Continuing, because target line might be inlined. */
  744. }
  745. /* TODO: deallocate lines, but how? */
  746. return ret;
  747. }
  748. /* Callback parameter with return value */
  749. struct dwarf_callback_param {
  750. void *data;
  751. int retval;
  752. };
  753. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  754. {
  755. struct dwarf_callback_param *param = data;
  756. struct probe_finder *pf = param->data;
  757. struct perf_probe_point *pp = &pf->pev->point;
  758. Dwarf_Addr addr;
  759. if (pp->lazy_line)
  760. param->retval = find_probe_point_lazy(in_die, pf);
  761. else {
  762. /* Get probe address */
  763. if (dwarf_entrypc(in_die, &addr) != 0) {
  764. pr_warning("Failed to get entry pc of %s.\n",
  765. dwarf_diename(in_die));
  766. param->retval = -ENOENT;
  767. return DWARF_CB_ABORT;
  768. }
  769. pf->addr = addr;
  770. pf->addr += pp->offset;
  771. pr_debug("found inline addr: 0x%jx\n",
  772. (uintmax_t)pf->addr);
  773. param->retval = convert_probe_point(in_die, pf);
  774. if (param->retval < 0)
  775. return DWARF_CB_ABORT;
  776. }
  777. return DWARF_CB_OK;
  778. }
  779. /* Search function from function name */
  780. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  781. {
  782. struct dwarf_callback_param *param = data;
  783. struct probe_finder *pf = param->data;
  784. struct perf_probe_point *pp = &pf->pev->point;
  785. /* Check tag and diename */
  786. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  787. die_compare_name(sp_die, pp->function) != 0)
  788. return DWARF_CB_OK;
  789. pf->fname = dwarf_decl_file(sp_die);
  790. if (pp->line) { /* Function relative line */
  791. dwarf_decl_line(sp_die, &pf->lno);
  792. pf->lno += pp->line;
  793. param->retval = find_probe_point_by_line(pf);
  794. } else if (!dwarf_func_inline(sp_die)) {
  795. /* Real function */
  796. if (pp->lazy_line)
  797. param->retval = find_probe_point_lazy(sp_die, pf);
  798. else {
  799. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  800. pr_warning("Failed to get entry pc of %s.\n",
  801. dwarf_diename(sp_die));
  802. param->retval = -ENOENT;
  803. return DWARF_CB_ABORT;
  804. }
  805. pf->addr += pp->offset;
  806. /* TODO: Check the address in this function */
  807. param->retval = convert_probe_point(sp_die, pf);
  808. }
  809. } else {
  810. struct dwarf_callback_param _param = {.data = (void *)pf,
  811. .retval = 0};
  812. /* Inlined function: search instances */
  813. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  814. &_param);
  815. param->retval = _param.retval;
  816. }
  817. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  818. }
  819. static int find_probe_point_by_func(struct probe_finder *pf)
  820. {
  821. struct dwarf_callback_param _param = {.data = (void *)pf,
  822. .retval = 0};
  823. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  824. return _param.retval;
  825. }
  826. /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
  827. int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
  828. struct kprobe_trace_event **tevs, int max_tevs)
  829. {
  830. struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
  831. struct perf_probe_point *pp = &pev->point;
  832. Dwarf_Off off, noff;
  833. size_t cuhl;
  834. Dwarf_Die *diep;
  835. Dwarf *dbg;
  836. int ret = 0;
  837. pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
  838. if (pf.tevs == NULL)
  839. return -ENOMEM;
  840. *tevs = pf.tevs;
  841. pf.ntevs = 0;
  842. dbg = dwarf_begin(fd, DWARF_C_READ);
  843. if (!dbg) {
  844. pr_warning("No dwarf info found in the vmlinux - "
  845. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  846. free(pf.tevs);
  847. *tevs = NULL;
  848. return -EBADF;
  849. }
  850. #if _ELFUTILS_PREREQ(0, 142)
  851. /* Get the call frame information from this dwarf */
  852. pf.cfi = dwarf_getcfi(dbg);
  853. #endif
  854. off = 0;
  855. line_list__init(&pf.lcache);
  856. /* Loop on CUs (Compilation Unit) */
  857. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  858. ret >= 0) {
  859. /* Get the DIE(Debugging Information Entry) of this CU */
  860. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  861. if (!diep)
  862. continue;
  863. /* Check if target file is included. */
  864. if (pp->file)
  865. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  866. else
  867. pf.fname = NULL;
  868. if (!pp->file || pf.fname) {
  869. if (pp->function)
  870. ret = find_probe_point_by_func(&pf);
  871. else if (pp->lazy_line)
  872. ret = find_probe_point_lazy(NULL, &pf);
  873. else {
  874. pf.lno = pp->line;
  875. ret = find_probe_point_by_line(&pf);
  876. }
  877. }
  878. off = noff;
  879. }
  880. line_list__free(&pf.lcache);
  881. dwarf_end(dbg);
  882. return (ret < 0) ? ret : pf.ntevs;
  883. }
  884. /* Reverse search */
  885. int find_perf_probe_point(int fd, unsigned long addr,
  886. struct perf_probe_point *ppt)
  887. {
  888. Dwarf_Die cudie, spdie, indie;
  889. Dwarf *dbg;
  890. Dwarf_Line *line;
  891. Dwarf_Addr laddr, eaddr;
  892. const char *tmp;
  893. int lineno, ret = 0;
  894. bool found = false;
  895. dbg = dwarf_begin(fd, DWARF_C_READ);
  896. if (!dbg)
  897. return -EBADF;
  898. /* Find cu die */
  899. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
  900. ret = -EINVAL;
  901. goto end;
  902. }
  903. /* Find a corresponding line */
  904. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  905. if (line) {
  906. if (dwarf_lineaddr(line, &laddr) == 0 &&
  907. (Dwarf_Addr)addr == laddr &&
  908. dwarf_lineno(line, &lineno) == 0) {
  909. tmp = dwarf_linesrc(line, NULL, NULL);
  910. if (tmp) {
  911. ppt->line = lineno;
  912. ppt->file = strdup(tmp);
  913. if (ppt->file == NULL) {
  914. ret = -ENOMEM;
  915. goto end;
  916. }
  917. found = true;
  918. }
  919. }
  920. }
  921. /* Find a corresponding function */
  922. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  923. tmp = dwarf_diename(&spdie);
  924. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  925. goto end;
  926. if (ppt->line) {
  927. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  928. &indie)) {
  929. /* addr in an inline function */
  930. tmp = dwarf_diename(&indie);
  931. if (!tmp)
  932. goto end;
  933. ret = dwarf_decl_line(&indie, &lineno);
  934. } else {
  935. if (eaddr == addr) { /* Function entry */
  936. lineno = ppt->line;
  937. ret = 0;
  938. } else
  939. ret = dwarf_decl_line(&spdie, &lineno);
  940. }
  941. if (ret == 0) {
  942. /* Make a relative line number */
  943. ppt->line -= lineno;
  944. goto found;
  945. }
  946. }
  947. /* We don't have a line number, let's use offset */
  948. ppt->offset = addr - (unsigned long)eaddr;
  949. found:
  950. ppt->function = strdup(tmp);
  951. if (ppt->function == NULL) {
  952. ret = -ENOMEM;
  953. goto end;
  954. }
  955. found = true;
  956. }
  957. end:
  958. dwarf_end(dbg);
  959. if (ret >= 0)
  960. ret = found ? 1 : 0;
  961. return ret;
  962. }
  963. /* Add a line and store the src path */
  964. static int line_range_add_line(const char *src, unsigned int lineno,
  965. struct line_range *lr)
  966. {
  967. /* Copy real path */
  968. if (!lr->path) {
  969. lr->path = strdup(src);
  970. if (lr->path == NULL)
  971. return -ENOMEM;
  972. }
  973. return line_list__add_line(&lr->line_list, lineno);
  974. }
  975. /* Search function declaration lines */
  976. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  977. {
  978. struct dwarf_callback_param *param = data;
  979. struct line_finder *lf = param->data;
  980. const char *src;
  981. int lineno;
  982. src = dwarf_decl_file(sp_die);
  983. if (src && strtailcmp(src, lf->fname) != 0)
  984. return DWARF_CB_OK;
  985. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  986. (lf->lno_s > lineno || lf->lno_e < lineno))
  987. return DWARF_CB_OK;
  988. param->retval = line_range_add_line(src, lineno, lf->lr);
  989. if (param->retval < 0)
  990. return DWARF_CB_ABORT;
  991. return DWARF_CB_OK;
  992. }
  993. static int find_line_range_func_decl_lines(struct line_finder *lf)
  994. {
  995. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  996. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  997. return param.retval;
  998. }
  999. /* Find line range from its line number */
  1000. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  1001. {
  1002. Dwarf_Lines *lines;
  1003. Dwarf_Line *line;
  1004. size_t nlines, i;
  1005. Dwarf_Addr addr;
  1006. int lineno, ret = 0;
  1007. const char *src;
  1008. Dwarf_Die die_mem;
  1009. line_list__init(&lf->lr->line_list);
  1010. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  1011. pr_warning("No source lines found in this CU.\n");
  1012. return -ENOENT;
  1013. }
  1014. /* Search probable lines on lines list */
  1015. for (i = 0; i < nlines; i++) {
  1016. line = dwarf_onesrcline(lines, i);
  1017. if (dwarf_lineno(line, &lineno) != 0 ||
  1018. (lf->lno_s > lineno || lf->lno_e < lineno))
  1019. continue;
  1020. if (sp_die) {
  1021. /* Address filtering 1: does sp_die include addr? */
  1022. if (dwarf_lineaddr(line, &addr) != 0 ||
  1023. !dwarf_haspc(sp_die, addr))
  1024. continue;
  1025. /* Address filtering 2: No child include addr? */
  1026. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1027. continue;
  1028. }
  1029. /* TODO: Get fileno from line, but how? */
  1030. src = dwarf_linesrc(line, NULL, NULL);
  1031. if (strtailcmp(src, lf->fname) != 0)
  1032. continue;
  1033. ret = line_range_add_line(src, lineno, lf->lr);
  1034. if (ret < 0)
  1035. return ret;
  1036. }
  1037. /*
  1038. * Dwarf lines doesn't include function declarations. We have to
  1039. * check functions list or given function.
  1040. */
  1041. if (sp_die) {
  1042. src = dwarf_decl_file(sp_die);
  1043. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1044. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1045. ret = line_range_add_line(src, lineno, lf->lr);
  1046. } else
  1047. ret = find_line_range_func_decl_lines(lf);
  1048. /* Update status */
  1049. if (ret >= 0)
  1050. if (!list_empty(&lf->lr->line_list))
  1051. ret = lf->found = 1;
  1052. else
  1053. ret = 0; /* Lines are not found */
  1054. else {
  1055. free(lf->lr->path);
  1056. lf->lr->path = NULL;
  1057. }
  1058. return ret;
  1059. }
  1060. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1061. {
  1062. struct dwarf_callback_param *param = data;
  1063. param->retval = find_line_range_by_line(in_die, param->data);
  1064. return DWARF_CB_ABORT; /* No need to find other instances */
  1065. }
  1066. /* Search function from function name */
  1067. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1068. {
  1069. struct dwarf_callback_param *param = data;
  1070. struct line_finder *lf = param->data;
  1071. struct line_range *lr = lf->lr;
  1072. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1073. die_compare_name(sp_die, lr->function) == 0) {
  1074. lf->fname = dwarf_decl_file(sp_die);
  1075. dwarf_decl_line(sp_die, &lr->offset);
  1076. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1077. lf->lno_s = lr->offset + lr->start;
  1078. if (lf->lno_s < 0) /* Overflow */
  1079. lf->lno_s = INT_MAX;
  1080. lf->lno_e = lr->offset + lr->end;
  1081. if (lf->lno_e < 0) /* Overflow */
  1082. lf->lno_e = INT_MAX;
  1083. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1084. lr->start = lf->lno_s;
  1085. lr->end = lf->lno_e;
  1086. if (dwarf_func_inline(sp_die)) {
  1087. struct dwarf_callback_param _param;
  1088. _param.data = (void *)lf;
  1089. _param.retval = 0;
  1090. dwarf_func_inline_instances(sp_die,
  1091. line_range_inline_cb,
  1092. &_param);
  1093. param->retval = _param.retval;
  1094. } else
  1095. param->retval = find_line_range_by_line(sp_die, lf);
  1096. return DWARF_CB_ABORT;
  1097. }
  1098. return DWARF_CB_OK;
  1099. }
  1100. static int find_line_range_by_func(struct line_finder *lf)
  1101. {
  1102. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1103. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1104. return param.retval;
  1105. }
  1106. int find_line_range(int fd, struct line_range *lr)
  1107. {
  1108. struct line_finder lf = {.lr = lr, .found = 0};
  1109. int ret = 0;
  1110. Dwarf_Off off = 0, noff;
  1111. size_t cuhl;
  1112. Dwarf_Die *diep;
  1113. Dwarf *dbg;
  1114. dbg = dwarf_begin(fd, DWARF_C_READ);
  1115. if (!dbg) {
  1116. pr_warning("No dwarf info found in the vmlinux - "
  1117. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1118. return -EBADF;
  1119. }
  1120. /* Loop on CUs (Compilation Unit) */
  1121. while (!lf.found && ret >= 0) {
  1122. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1123. break;
  1124. /* Get the DIE(Debugging Information Entry) of this CU */
  1125. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1126. if (!diep)
  1127. continue;
  1128. /* Check if target file is included. */
  1129. if (lr->file)
  1130. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1131. else
  1132. lf.fname = 0;
  1133. if (!lr->file || lf.fname) {
  1134. if (lr->function)
  1135. ret = find_line_range_by_func(&lf);
  1136. else {
  1137. lf.lno_s = lr->start;
  1138. lf.lno_e = lr->end;
  1139. ret = find_line_range_by_line(NULL, &lf);
  1140. }
  1141. }
  1142. off = noff;
  1143. }
  1144. pr_debug("path: %lx\n", (unsigned long)lr->path);
  1145. dwarf_end(dbg);
  1146. return (ret < 0) ? ret : lf.found;
  1147. }