probe-finder.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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. } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
  587. pf->cfi != NULL) {
  588. Dwarf_Frame *frame;
  589. if (dwarf_cfi_addrframe(pf->cfi, pf->addr, &frame) != 0 ||
  590. dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) {
  591. pr_warning("Failed to get CFA on 0x%jx\n",
  592. (uintmax_t)pf->addr);
  593. return -ENOENT;
  594. }
  595. }
  596. /* Find each argument */
  597. tev->nargs = pf->pev->nargs;
  598. tev->args = zalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  599. if (tev->args == NULL)
  600. return -ENOMEM;
  601. for (i = 0; i < pf->pev->nargs; i++) {
  602. pf->pvar = &pf->pev->args[i];
  603. pf->tvar = &tev->args[i];
  604. ret = find_variable(sp_die, pf);
  605. if (ret != 0)
  606. return ret;
  607. }
  608. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  609. pf->fb_ops = NULL;
  610. return 0;
  611. }
  612. /* Find probe point from its line number */
  613. static int find_probe_point_by_line(struct probe_finder *pf)
  614. {
  615. Dwarf_Lines *lines;
  616. Dwarf_Line *line;
  617. size_t nlines, i;
  618. Dwarf_Addr addr;
  619. int lineno;
  620. int ret = 0;
  621. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  622. pr_warning("No source lines found in this CU.\n");
  623. return -ENOENT;
  624. }
  625. for (i = 0; i < nlines && ret == 0; i++) {
  626. line = dwarf_onesrcline(lines, i);
  627. if (dwarf_lineno(line, &lineno) != 0 ||
  628. lineno != pf->lno)
  629. continue;
  630. /* TODO: Get fileno from line, but how? */
  631. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  632. continue;
  633. if (dwarf_lineaddr(line, &addr) != 0) {
  634. pr_warning("Failed to get the address of the line.\n");
  635. return -ENOENT;
  636. }
  637. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  638. (int)i, lineno, (uintmax_t)addr);
  639. pf->addr = addr;
  640. ret = convert_probe_point(NULL, pf);
  641. /* Continuing, because target line might be inlined. */
  642. }
  643. return ret;
  644. }
  645. /* Find lines which match lazy pattern */
  646. static int find_lazy_match_lines(struct list_head *head,
  647. const char *fname, const char *pat)
  648. {
  649. char *fbuf, *p1, *p2;
  650. int fd, ret, line, nlines = 0;
  651. struct stat st;
  652. fd = open(fname, O_RDONLY);
  653. if (fd < 0) {
  654. pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
  655. return fd;
  656. }
  657. ret = fstat(fd, &st);
  658. if (ret < 0) {
  659. pr_warning("Failed to get the size of %s: %s\n",
  660. fname, strerror(errno));
  661. return ret;
  662. }
  663. fbuf = xmalloc(st.st_size + 2);
  664. ret = read(fd, fbuf, st.st_size);
  665. if (ret < 0) {
  666. pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
  667. return ret;
  668. }
  669. close(fd);
  670. fbuf[st.st_size] = '\n'; /* Dummy line */
  671. fbuf[st.st_size + 1] = '\0';
  672. p1 = fbuf;
  673. line = 1;
  674. while ((p2 = strchr(p1, '\n')) != NULL) {
  675. *p2 = '\0';
  676. if (strlazymatch(p1, pat)) {
  677. line_list__add_line(head, line);
  678. nlines++;
  679. }
  680. line++;
  681. p1 = p2 + 1;
  682. }
  683. free(fbuf);
  684. return nlines;
  685. }
  686. /* Find probe points from lazy pattern */
  687. static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  688. {
  689. Dwarf_Lines *lines;
  690. Dwarf_Line *line;
  691. size_t nlines, i;
  692. Dwarf_Addr addr;
  693. Dwarf_Die die_mem;
  694. int lineno;
  695. int ret = 0;
  696. if (list_empty(&pf->lcache)) {
  697. /* Matching lazy line pattern */
  698. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  699. pf->pev->point.lazy_line);
  700. if (ret == 0) {
  701. pr_debug("No matched lines found in %s.\n", pf->fname);
  702. return 0;
  703. } else if (ret < 0)
  704. return ret;
  705. }
  706. if (dwarf_getsrclines(&pf->cu_die, &lines, &nlines) != 0) {
  707. pr_warning("No source lines found in this CU.\n");
  708. return -ENOENT;
  709. }
  710. for (i = 0; i < nlines && ret >= 0; i++) {
  711. line = dwarf_onesrcline(lines, i);
  712. if (dwarf_lineno(line, &lineno) != 0 ||
  713. !line_list__has_line(&pf->lcache, lineno))
  714. continue;
  715. /* TODO: Get fileno from line, but how? */
  716. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  717. continue;
  718. if (dwarf_lineaddr(line, &addr) != 0) {
  719. pr_debug("Failed to get the address of line %d.\n",
  720. lineno);
  721. continue;
  722. }
  723. if (sp_die) {
  724. /* Address filtering 1: does sp_die include addr? */
  725. if (!dwarf_haspc(sp_die, addr))
  726. continue;
  727. /* Address filtering 2: No child include addr? */
  728. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  729. continue;
  730. }
  731. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  732. (int)i, lineno, (unsigned long long)addr);
  733. pf->addr = addr;
  734. ret = convert_probe_point(sp_die, pf);
  735. /* Continuing, because target line might be inlined. */
  736. }
  737. /* TODO: deallocate lines, but how? */
  738. return ret;
  739. }
  740. /* Callback parameter with return value */
  741. struct dwarf_callback_param {
  742. void *data;
  743. int retval;
  744. };
  745. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  746. {
  747. struct dwarf_callback_param *param = data;
  748. struct probe_finder *pf = param->data;
  749. struct perf_probe_point *pp = &pf->pev->point;
  750. Dwarf_Addr addr;
  751. if (pp->lazy_line)
  752. param->retval = find_probe_point_lazy(in_die, pf);
  753. else {
  754. /* Get probe address */
  755. if (dwarf_entrypc(in_die, &addr) != 0) {
  756. pr_warning("Failed to get entry pc of %s.\n",
  757. dwarf_diename(in_die));
  758. param->retval = -ENOENT;
  759. return DWARF_CB_ABORT;
  760. }
  761. pf->addr = addr;
  762. pf->addr += pp->offset;
  763. pr_debug("found inline addr: 0x%jx\n",
  764. (uintmax_t)pf->addr);
  765. param->retval = convert_probe_point(in_die, pf);
  766. if (param->retval < 0)
  767. return DWARF_CB_ABORT;
  768. }
  769. return DWARF_CB_OK;
  770. }
  771. /* Search function from function name */
  772. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  773. {
  774. struct dwarf_callback_param *param = data;
  775. struct probe_finder *pf = param->data;
  776. struct perf_probe_point *pp = &pf->pev->point;
  777. /* Check tag and diename */
  778. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  779. die_compare_name(sp_die, pp->function) != 0)
  780. return DWARF_CB_OK;
  781. pf->fname = dwarf_decl_file(sp_die);
  782. if (pp->line) { /* Function relative line */
  783. dwarf_decl_line(sp_die, &pf->lno);
  784. pf->lno += pp->line;
  785. param->retval = find_probe_point_by_line(pf);
  786. } else if (!dwarf_func_inline(sp_die)) {
  787. /* Real function */
  788. if (pp->lazy_line)
  789. param->retval = find_probe_point_lazy(sp_die, pf);
  790. else {
  791. if (dwarf_entrypc(sp_die, &pf->addr) != 0) {
  792. pr_warning("Failed to get entry pc of %s.\n",
  793. dwarf_diename(sp_die));
  794. param->retval = -ENOENT;
  795. return DWARF_CB_ABORT;
  796. }
  797. pf->addr += pp->offset;
  798. /* TODO: Check the address in this function */
  799. param->retval = convert_probe_point(sp_die, pf);
  800. }
  801. } else {
  802. struct dwarf_callback_param _param = {.data = (void *)pf,
  803. .retval = 0};
  804. /* Inlined function: search instances */
  805. dwarf_func_inline_instances(sp_die, probe_point_inline_cb,
  806. &_param);
  807. param->retval = _param.retval;
  808. }
  809. return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */
  810. }
  811. static int find_probe_point_by_func(struct probe_finder *pf)
  812. {
  813. struct dwarf_callback_param _param = {.data = (void *)pf,
  814. .retval = 0};
  815. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0);
  816. return _param.retval;
  817. }
  818. /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
  819. int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
  820. struct kprobe_trace_event **tevs, int max_tevs)
  821. {
  822. struct probe_finder pf = {.pev = pev, .max_tevs = max_tevs};
  823. struct perf_probe_point *pp = &pev->point;
  824. Dwarf_Off off, noff;
  825. size_t cuhl;
  826. Dwarf_Die *diep;
  827. Dwarf *dbg;
  828. int ret = 0;
  829. pf.tevs = zalloc(sizeof(struct kprobe_trace_event) * max_tevs);
  830. if (pf.tevs == NULL)
  831. return -ENOMEM;
  832. *tevs = pf.tevs;
  833. pf.ntevs = 0;
  834. dbg = dwarf_begin(fd, DWARF_C_READ);
  835. if (!dbg) {
  836. pr_warning("No dwarf info found in the vmlinux - "
  837. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  838. return -EBADF;
  839. }
  840. /* Get the call frame information from this dwarf */
  841. pf.cfi = dwarf_getcfi(dbg);
  842. off = 0;
  843. line_list__init(&pf.lcache);
  844. /* Loop on CUs (Compilation Unit) */
  845. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) &&
  846. ret >= 0) {
  847. /* Get the DIE(Debugging Information Entry) of this CU */
  848. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  849. if (!diep)
  850. continue;
  851. /* Check if target file is included. */
  852. if (pp->file)
  853. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  854. else
  855. pf.fname = NULL;
  856. if (!pp->file || pf.fname) {
  857. if (pp->function)
  858. ret = find_probe_point_by_func(&pf);
  859. else if (pp->lazy_line)
  860. ret = find_probe_point_lazy(NULL, &pf);
  861. else {
  862. pf.lno = pp->line;
  863. ret = find_probe_point_by_line(&pf);
  864. }
  865. }
  866. off = noff;
  867. }
  868. line_list__free(&pf.lcache);
  869. dwarf_end(dbg);
  870. return (ret < 0) ? ret : pf.ntevs;
  871. }
  872. /* Reverse search */
  873. int find_perf_probe_point(int fd, unsigned long addr,
  874. struct perf_probe_point *ppt)
  875. {
  876. Dwarf_Die cudie, spdie, indie;
  877. Dwarf *dbg;
  878. Dwarf_Line *line;
  879. Dwarf_Addr laddr, eaddr;
  880. const char *tmp;
  881. int lineno, ret = 0;
  882. bool found = false;
  883. dbg = dwarf_begin(fd, DWARF_C_READ);
  884. if (!dbg)
  885. return -EBADF;
  886. /* Find cu die */
  887. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie)) {
  888. ret = -EINVAL;
  889. goto end;
  890. }
  891. /* Find a corresponding line */
  892. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  893. if (line) {
  894. if (dwarf_lineaddr(line, &laddr) == 0 &&
  895. (Dwarf_Addr)addr == laddr &&
  896. dwarf_lineno(line, &lineno) == 0) {
  897. tmp = dwarf_linesrc(line, NULL, NULL);
  898. if (tmp) {
  899. ppt->line = lineno;
  900. ppt->file = strdup(tmp);
  901. if (ppt->file == NULL) {
  902. ret = -ENOMEM;
  903. goto end;
  904. }
  905. found = true;
  906. }
  907. }
  908. }
  909. /* Find a corresponding function */
  910. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  911. tmp = dwarf_diename(&spdie);
  912. if (!tmp || dwarf_entrypc(&spdie, &eaddr) != 0)
  913. goto end;
  914. if (ppt->line) {
  915. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr,
  916. &indie)) {
  917. /* addr in an inline function */
  918. tmp = dwarf_diename(&indie);
  919. if (!tmp)
  920. goto end;
  921. ret = dwarf_decl_line(&indie, &lineno);
  922. } else {
  923. if (eaddr == addr) { /* Function entry */
  924. lineno = ppt->line;
  925. ret = 0;
  926. } else
  927. ret = dwarf_decl_line(&spdie, &lineno);
  928. }
  929. if (ret == 0) {
  930. /* Make a relative line number */
  931. ppt->line -= lineno;
  932. goto found;
  933. }
  934. }
  935. /* We don't have a line number, let's use offset */
  936. ppt->offset = addr - (unsigned long)eaddr;
  937. found:
  938. ppt->function = strdup(tmp);
  939. if (ppt->function == NULL) {
  940. ret = -ENOMEM;
  941. goto end;
  942. }
  943. found = true;
  944. }
  945. end:
  946. dwarf_end(dbg);
  947. if (ret >= 0)
  948. ret = found ? 1 : 0;
  949. return ret;
  950. }
  951. /* Add a line and store the src path */
  952. static int line_range_add_line(const char *src, unsigned int lineno,
  953. struct line_range *lr)
  954. {
  955. /* Copy real path */
  956. if (!lr->path) {
  957. lr->path = strdup(src);
  958. if (lr->path == NULL)
  959. return -ENOMEM;
  960. }
  961. return line_list__add_line(&lr->line_list, lineno);
  962. }
  963. /* Search function declaration lines */
  964. static int line_range_funcdecl_cb(Dwarf_Die *sp_die, void *data)
  965. {
  966. struct dwarf_callback_param *param = data;
  967. struct line_finder *lf = param->data;
  968. const char *src;
  969. int lineno;
  970. src = dwarf_decl_file(sp_die);
  971. if (src && strtailcmp(src, lf->fname) != 0)
  972. return DWARF_CB_OK;
  973. if (dwarf_decl_line(sp_die, &lineno) != 0 ||
  974. (lf->lno_s > lineno || lf->lno_e < lineno))
  975. return DWARF_CB_OK;
  976. param->retval = line_range_add_line(src, lineno, lf->lr);
  977. if (param->retval < 0)
  978. return DWARF_CB_ABORT;
  979. return DWARF_CB_OK;
  980. }
  981. static int find_line_range_func_decl_lines(struct line_finder *lf)
  982. {
  983. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  984. dwarf_getfuncs(&lf->cu_die, line_range_funcdecl_cb, &param, 0);
  985. return param.retval;
  986. }
  987. /* Find line range from its line number */
  988. static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  989. {
  990. Dwarf_Lines *lines;
  991. Dwarf_Line *line;
  992. size_t nlines, i;
  993. Dwarf_Addr addr;
  994. int lineno, ret = 0;
  995. const char *src;
  996. Dwarf_Die die_mem;
  997. line_list__init(&lf->lr->line_list);
  998. if (dwarf_getsrclines(&lf->cu_die, &lines, &nlines) != 0) {
  999. pr_warning("No source lines found in this CU.\n");
  1000. return -ENOENT;
  1001. }
  1002. /* Search probable lines on lines list */
  1003. for (i = 0; i < nlines; i++) {
  1004. line = dwarf_onesrcline(lines, i);
  1005. if (dwarf_lineno(line, &lineno) != 0 ||
  1006. (lf->lno_s > lineno || lf->lno_e < lineno))
  1007. continue;
  1008. if (sp_die) {
  1009. /* Address filtering 1: does sp_die include addr? */
  1010. if (dwarf_lineaddr(line, &addr) != 0 ||
  1011. !dwarf_haspc(sp_die, addr))
  1012. continue;
  1013. /* Address filtering 2: No child include addr? */
  1014. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  1015. continue;
  1016. }
  1017. /* TODO: Get fileno from line, but how? */
  1018. src = dwarf_linesrc(line, NULL, NULL);
  1019. if (strtailcmp(src, lf->fname) != 0)
  1020. continue;
  1021. ret = line_range_add_line(src, lineno, lf->lr);
  1022. if (ret < 0)
  1023. return ret;
  1024. }
  1025. /*
  1026. * Dwarf lines doesn't include function declarations. We have to
  1027. * check functions list or given function.
  1028. */
  1029. if (sp_die) {
  1030. src = dwarf_decl_file(sp_die);
  1031. if (src && dwarf_decl_line(sp_die, &lineno) == 0 &&
  1032. (lf->lno_s <= lineno && lf->lno_e >= lineno))
  1033. ret = line_range_add_line(src, lineno, lf->lr);
  1034. } else
  1035. ret = find_line_range_func_decl_lines(lf);
  1036. /* Update status */
  1037. if (ret >= 0)
  1038. if (!list_empty(&lf->lr->line_list))
  1039. ret = lf->found = 1;
  1040. else
  1041. ret = 0; /* Lines are not found */
  1042. else {
  1043. free(lf->lr->path);
  1044. lf->lr->path = NULL;
  1045. }
  1046. return ret;
  1047. }
  1048. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  1049. {
  1050. struct dwarf_callback_param *param = data;
  1051. param->retval = find_line_range_by_line(in_die, param->data);
  1052. return DWARF_CB_ABORT; /* No need to find other instances */
  1053. }
  1054. /* Search function from function name */
  1055. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  1056. {
  1057. struct dwarf_callback_param *param = data;
  1058. struct line_finder *lf = param->data;
  1059. struct line_range *lr = lf->lr;
  1060. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  1061. die_compare_name(sp_die, lr->function) == 0) {
  1062. lf->fname = dwarf_decl_file(sp_die);
  1063. dwarf_decl_line(sp_die, &lr->offset);
  1064. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  1065. lf->lno_s = lr->offset + lr->start;
  1066. if (lf->lno_s < 0) /* Overflow */
  1067. lf->lno_s = INT_MAX;
  1068. lf->lno_e = lr->offset + lr->end;
  1069. if (lf->lno_e < 0) /* Overflow */
  1070. lf->lno_e = INT_MAX;
  1071. pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e);
  1072. lr->start = lf->lno_s;
  1073. lr->end = lf->lno_e;
  1074. if (dwarf_func_inline(sp_die)) {
  1075. struct dwarf_callback_param _param;
  1076. _param.data = (void *)lf;
  1077. _param.retval = 0;
  1078. dwarf_func_inline_instances(sp_die,
  1079. line_range_inline_cb,
  1080. &_param);
  1081. param->retval = _param.retval;
  1082. } else
  1083. param->retval = find_line_range_by_line(sp_die, lf);
  1084. return DWARF_CB_ABORT;
  1085. }
  1086. return DWARF_CB_OK;
  1087. }
  1088. static int find_line_range_by_func(struct line_finder *lf)
  1089. {
  1090. struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0};
  1091. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, &param, 0);
  1092. return param.retval;
  1093. }
  1094. int find_line_range(int fd, struct line_range *lr)
  1095. {
  1096. struct line_finder lf = {.lr = lr, .found = 0};
  1097. int ret = 0;
  1098. Dwarf_Off off = 0, noff;
  1099. size_t cuhl;
  1100. Dwarf_Die *diep;
  1101. Dwarf *dbg;
  1102. dbg = dwarf_begin(fd, DWARF_C_READ);
  1103. if (!dbg) {
  1104. pr_warning("No dwarf info found in the vmlinux - "
  1105. "please rebuild with CONFIG_DEBUG_INFO=y.\n");
  1106. return -EBADF;
  1107. }
  1108. /* Loop on CUs (Compilation Unit) */
  1109. while (!lf.found && ret >= 0) {
  1110. if (dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL) != 0)
  1111. break;
  1112. /* Get the DIE(Debugging Information Entry) of this CU */
  1113. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  1114. if (!diep)
  1115. continue;
  1116. /* Check if target file is included. */
  1117. if (lr->file)
  1118. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  1119. else
  1120. lf.fname = 0;
  1121. if (!lr->file || lf.fname) {
  1122. if (lr->function)
  1123. ret = find_line_range_by_func(&lf);
  1124. else {
  1125. lf.lno_s = lr->start;
  1126. lf.lno_e = lr->end;
  1127. ret = find_line_range_by_line(NULL, &lf);
  1128. }
  1129. }
  1130. off = noff;
  1131. }
  1132. pr_debug("path: %lx\n", (unsigned long)lr->path);
  1133. dwarf_end(dbg);
  1134. return (ret < 0) ? ret : lf.found;
  1135. }