probe-finder.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 "string.h"
  34. #include "event.h"
  35. #include "debug.h"
  36. #include "util.h"
  37. #include "probe-finder.h"
  38. /*
  39. * Generic dwarf analysis helpers
  40. */
  41. #define X86_32_MAX_REGS 8
  42. const char *x86_32_regs_table[X86_32_MAX_REGS] = {
  43. "%ax",
  44. "%cx",
  45. "%dx",
  46. "%bx",
  47. "$stack", /* Stack address instead of %sp */
  48. "%bp",
  49. "%si",
  50. "%di",
  51. };
  52. #define X86_64_MAX_REGS 16
  53. const char *x86_64_regs_table[X86_64_MAX_REGS] = {
  54. "%ax",
  55. "%dx",
  56. "%cx",
  57. "%bx",
  58. "%si",
  59. "%di",
  60. "%bp",
  61. "%sp",
  62. "%r8",
  63. "%r9",
  64. "%r10",
  65. "%r11",
  66. "%r12",
  67. "%r13",
  68. "%r14",
  69. "%r15",
  70. };
  71. /* TODO: switching by dwarf address size */
  72. #ifdef __x86_64__
  73. #define ARCH_MAX_REGS X86_64_MAX_REGS
  74. #define arch_regs_table x86_64_regs_table
  75. #else
  76. #define ARCH_MAX_REGS X86_32_MAX_REGS
  77. #define arch_regs_table x86_32_regs_table
  78. #endif
  79. /* Return architecture dependent register string (for kprobe-tracer) */
  80. static const char *get_arch_regstr(unsigned int n)
  81. {
  82. return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
  83. }
  84. /*
  85. * Compare the tail of two strings.
  86. * Return 0 if whole of either string is same as another's tail part.
  87. */
  88. static int strtailcmp(const char *s1, const char *s2)
  89. {
  90. int i1 = strlen(s1);
  91. int i2 = strlen(s2);
  92. while (--i1 >= 0 && --i2 >= 0) {
  93. if (s1[i1] != s2[i2])
  94. return s1[i1] - s2[i2];
  95. }
  96. return 0;
  97. }
  98. /* Line number list operations */
  99. /* Add a line to line number list */
  100. static void line_list__add_line(struct list_head *head, unsigned int line)
  101. {
  102. struct line_node *ln;
  103. struct list_head *p;
  104. /* Reverse search, because new line will be the last one */
  105. list_for_each_entry_reverse(ln, head, list) {
  106. if (ln->line < line) {
  107. p = &ln->list;
  108. goto found;
  109. } else if (ln->line == line) /* Already exist */
  110. return ;
  111. }
  112. /* List is empty, or the smallest entry */
  113. p = head;
  114. found:
  115. pr_debug("line list: add a line %u\n", line);
  116. ln = zalloc(sizeof(struct line_node));
  117. DIE_IF(ln == NULL);
  118. ln->line = line;
  119. INIT_LIST_HEAD(&ln->list);
  120. list_add(&ln->list, p);
  121. }
  122. /* Check if the line in line number list */
  123. static int line_list__has_line(struct list_head *head, unsigned int line)
  124. {
  125. struct line_node *ln;
  126. /* Reverse search, because new line will be the last one */
  127. list_for_each_entry(ln, head, list)
  128. if (ln->line == line)
  129. return 1;
  130. return 0;
  131. }
  132. /* Init line number list */
  133. static void line_list__init(struct list_head *head)
  134. {
  135. INIT_LIST_HEAD(head);
  136. }
  137. /* Free line number list */
  138. static void line_list__free(struct list_head *head)
  139. {
  140. struct line_node *ln;
  141. while (!list_empty(head)) {
  142. ln = list_first_entry(head, struct line_node, list);
  143. list_del(&ln->list);
  144. free(ln);
  145. }
  146. }
  147. /* Dwarf wrappers */
  148. /* Find the realpath of the target file. */
  149. static const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
  150. {
  151. Dwarf_Files *files;
  152. size_t nfiles, i;
  153. const char *src;
  154. int ret;
  155. if (!fname)
  156. return NULL;
  157. ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
  158. if (ret != 0)
  159. return NULL;
  160. for (i = 0; i < nfiles; i++) {
  161. src = dwarf_filesrc(files, i, NULL, NULL);
  162. if (strtailcmp(src, fname) == 0)
  163. break;
  164. }
  165. return src;
  166. }
  167. struct __addr_die_search_param {
  168. Dwarf_Addr addr;
  169. Dwarf_Die *die_mem;
  170. };
  171. static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
  172. {
  173. struct __addr_die_search_param *ad = data;
  174. if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
  175. dwarf_haspc(fn_die, ad->addr)) {
  176. memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
  177. return DWARF_CB_ABORT;
  178. }
  179. return DWARF_CB_OK;
  180. }
  181. /* Search a real subprogram including this line, */
  182. static Dwarf_Die *die_get_real_subprogram(Dwarf_Die *cu_die, Dwarf_Addr addr,
  183. Dwarf_Die *die_mem)
  184. {
  185. struct __addr_die_search_param ad;
  186. ad.addr = addr;
  187. ad.die_mem = die_mem;
  188. /* dwarf_getscopes can't find subprogram. */
  189. if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
  190. return NULL;
  191. else
  192. return die_mem;
  193. }
  194. /* Similar to dwarf_getfuncs, but returns inlined_subroutine if exists. */
  195. static Dwarf_Die *die_get_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
  196. Dwarf_Die *die_mem)
  197. {
  198. Dwarf_Die child_die;
  199. int ret;
  200. ret = dwarf_child(sp_die, die_mem);
  201. if (ret != 0)
  202. return NULL;
  203. do {
  204. if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
  205. dwarf_haspc(die_mem, addr))
  206. return die_mem;
  207. if (die_get_inlinefunc(die_mem, addr, &child_die)) {
  208. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  209. return die_mem;
  210. }
  211. } while (dwarf_siblingof(die_mem, die_mem) == 0);
  212. return NULL;
  213. }
  214. /* Compare diename and tname */
  215. static bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
  216. {
  217. const char *name;
  218. name = dwarf_diename(dw_die);
  219. DIE_IF(name == NULL);
  220. return strcmp(tname, name);
  221. }
  222. /* Get entry pc(or low pc, 1st entry of ranges) of the die */
  223. static Dwarf_Addr die_get_entrypc(Dwarf_Die *dw_die)
  224. {
  225. Dwarf_Addr epc;
  226. int ret;
  227. ret = dwarf_entrypc(dw_die, &epc);
  228. DIE_IF(ret == -1);
  229. return epc;
  230. }
  231. /* Get a variable die */
  232. static Dwarf_Die *die_find_variable(Dwarf_Die *sp_die, const char *name,
  233. Dwarf_Die *die_mem)
  234. {
  235. Dwarf_Die child_die;
  236. int tag;
  237. int ret;
  238. ret = dwarf_child(sp_die, die_mem);
  239. if (ret != 0)
  240. return NULL;
  241. do {
  242. tag = dwarf_tag(die_mem);
  243. if ((tag == DW_TAG_formal_parameter ||
  244. tag == DW_TAG_variable) &&
  245. (die_compare_name(die_mem, name) == 0))
  246. return die_mem;
  247. if (die_find_variable(die_mem, name, &child_die)) {
  248. memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
  249. return die_mem;
  250. }
  251. } while (dwarf_siblingof(die_mem, die_mem) == 0);
  252. return NULL;
  253. }
  254. /*
  255. * Probe finder related functions
  256. */
  257. /* Show a location */
  258. static void show_location(Dwarf_Op *op, struct probe_finder *pf)
  259. {
  260. unsigned int regn;
  261. Dwarf_Word offs = 0;
  262. int deref = 0, ret;
  263. const char *regs;
  264. /* TODO: support CFA */
  265. /* If this is based on frame buffer, set the offset */
  266. if (op->atom == DW_OP_fbreg) {
  267. if (pf->fb_ops == NULL)
  268. die("The attribute of frame base is not supported.\n");
  269. deref = 1;
  270. offs = op->number;
  271. op = &pf->fb_ops[0];
  272. }
  273. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  274. regn = op->atom - DW_OP_breg0;
  275. offs += op->number;
  276. deref = 1;
  277. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  278. regn = op->atom - DW_OP_reg0;
  279. } else if (op->atom == DW_OP_bregx) {
  280. regn = op->number;
  281. offs += op->number2;
  282. deref = 1;
  283. } else if (op->atom == DW_OP_regx) {
  284. regn = op->number;
  285. } else
  286. die("DW_OP %d is not supported.", op->atom);
  287. regs = get_arch_regstr(regn);
  288. if (!regs)
  289. die("%u exceeds max register number.", regn);
  290. if (deref)
  291. ret = snprintf(pf->buf, pf->len, " %s=+%ju(%s)",
  292. pf->var, (uintmax_t)offs, regs);
  293. else
  294. ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
  295. DIE_IF(ret < 0);
  296. DIE_IF(ret >= pf->len);
  297. }
  298. /* Show a variables in kprobe event format */
  299. static void show_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  300. {
  301. Dwarf_Attribute attr;
  302. Dwarf_Op *expr;
  303. size_t nexpr;
  304. int ret;
  305. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  306. goto error;
  307. /* TODO: handle more than 1 exprs */
  308. ret = dwarf_getlocation_addr(&attr, (pf->addr - pf->cu_base),
  309. &expr, &nexpr, 1);
  310. if (ret <= 0 || nexpr == 0)
  311. goto error;
  312. show_location(expr, pf);
  313. /* *expr will be cached in libdw. Don't free it. */
  314. return ;
  315. error:
  316. /* TODO: Support const_value */
  317. die("Failed to find the location of %s at this address.\n"
  318. " Perhaps, it has been optimized out.", pf->var);
  319. }
  320. /* Find a variable in a subprogram die */
  321. static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  322. {
  323. int ret;
  324. Dwarf_Die vr_die;
  325. /* TODO: Support struct members and arrays */
  326. if (!is_c_varname(pf->var)) {
  327. /* Output raw parameters */
  328. ret = snprintf(pf->buf, pf->len, " %s", pf->var);
  329. DIE_IF(ret < 0);
  330. DIE_IF(ret >= pf->len);
  331. return ;
  332. }
  333. pr_debug("Searching '%s' variable in context.\n", pf->var);
  334. /* Search child die for local variables and parameters. */
  335. if (!die_find_variable(sp_die, pf->var, &vr_die))
  336. die("Failed to find '%s' in this function.", pf->var);
  337. show_variable(&vr_die, pf);
  338. }
  339. /* Show a probe point to output buffer */
  340. static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  341. {
  342. struct probe_point *pp = pf->pp;
  343. Dwarf_Addr eaddr;
  344. Dwarf_Die die_mem;
  345. const char *name;
  346. char tmp[MAX_PROBE_BUFFER];
  347. int ret, i, len;
  348. Dwarf_Attribute fb_attr;
  349. size_t nops;
  350. /* If no real subprogram, find a real one */
  351. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  352. sp_die = die_get_real_subprogram(&pf->cu_die,
  353. pf->addr, &die_mem);
  354. if (!sp_die)
  355. die("Probe point is not found in subprograms.");
  356. }
  357. /* Output name of probe point */
  358. name = dwarf_diename(sp_die);
  359. if (name) {
  360. dwarf_entrypc(sp_die, &eaddr);
  361. ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
  362. (unsigned long)(pf->addr - eaddr));
  363. /* Copy the function name if possible */
  364. if (!pp->function) {
  365. pp->function = strdup(name);
  366. pp->offset = (size_t)(pf->addr - eaddr);
  367. }
  368. } else {
  369. /* This function has no name. */
  370. ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
  371. (uintmax_t)pf->addr);
  372. if (!pp->function) {
  373. /* TODO: Use _stext */
  374. pp->function = strdup("");
  375. pp->offset = (size_t)pf->addr;
  376. }
  377. }
  378. DIE_IF(ret < 0);
  379. DIE_IF(ret >= MAX_PROBE_BUFFER);
  380. len = ret;
  381. pr_debug("Probe point found: %s\n", tmp);
  382. /* Get the frame base attribute/ops */
  383. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  384. ret = dwarf_getlocation_addr(&fb_attr, (pf->addr - pf->cu_base),
  385. &pf->fb_ops, &nops, 1);
  386. if (ret <= 0 || nops == 0)
  387. pf->fb_ops = NULL;
  388. /* Find each argument */
  389. /* TODO: use dwarf_cfi_addrframe */
  390. for (i = 0; i < pp->nr_args; i++) {
  391. pf->var = pp->args[i];
  392. pf->buf = &tmp[len];
  393. pf->len = MAX_PROBE_BUFFER - len;
  394. find_variable(sp_die, pf);
  395. len += strlen(pf->buf);
  396. }
  397. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  398. pf->fb_ops = NULL;
  399. pp->probes[pp->found] = strdup(tmp);
  400. pp->found++;
  401. }
  402. /* Find probe point from its line number */
  403. static void find_probe_point_by_line(struct probe_finder *pf)
  404. {
  405. Dwarf_Lines *lines;
  406. Dwarf_Line *line;
  407. size_t nlines, i;
  408. Dwarf_Addr addr;
  409. int lineno;
  410. int ret;
  411. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  412. DIE_IF(ret != 0);
  413. for (i = 0; i < nlines; i++) {
  414. line = dwarf_onesrcline(lines, i);
  415. dwarf_lineno(line, &lineno);
  416. if (lineno != pf->lno)
  417. continue;
  418. /* TODO: Get fileno from line, but how? */
  419. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  420. continue;
  421. ret = dwarf_lineaddr(line, &addr);
  422. DIE_IF(ret != 0);
  423. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  424. (int)i, lineno, (uintmax_t)addr);
  425. pf->addr = addr;
  426. show_probe_point(NULL, pf);
  427. /* Continuing, because target line might be inlined. */
  428. }
  429. }
  430. /* Find lines which match lazy pattern */
  431. static int find_lazy_match_lines(struct list_head *head,
  432. const char *fname, const char *pat)
  433. {
  434. char *fbuf, *p1, *p2;
  435. int fd, line, nlines = 0;
  436. struct stat st;
  437. fd = open(fname, O_RDONLY);
  438. if (fd < 0)
  439. die("failed to open %s", fname);
  440. DIE_IF(fstat(fd, &st) < 0);
  441. fbuf = malloc(st.st_size + 2);
  442. DIE_IF(fbuf == NULL);
  443. DIE_IF(read(fd, fbuf, st.st_size) < 0);
  444. close(fd);
  445. fbuf[st.st_size] = '\n'; /* Dummy line */
  446. fbuf[st.st_size + 1] = '\0';
  447. p1 = fbuf;
  448. line = 1;
  449. while ((p2 = strchr(p1, '\n')) != NULL) {
  450. *p2 = '\0';
  451. if (strlazymatch(p1, pat)) {
  452. line_list__add_line(head, line);
  453. nlines++;
  454. }
  455. line++;
  456. p1 = p2 + 1;
  457. }
  458. free(fbuf);
  459. return nlines;
  460. }
  461. /* Find probe points from lazy pattern */
  462. static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  463. {
  464. Dwarf_Lines *lines;
  465. Dwarf_Line *line;
  466. size_t nlines, i;
  467. Dwarf_Addr addr;
  468. Dwarf_Die die_mem;
  469. int lineno;
  470. int ret;
  471. if (list_empty(&pf->lcache)) {
  472. /* Matching lazy line pattern */
  473. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  474. pf->pp->lazy_line);
  475. if (ret <= 0)
  476. die("No matched lines found in %s.", pf->fname);
  477. }
  478. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  479. DIE_IF(ret != 0);
  480. for (i = 0; i < nlines; i++) {
  481. line = dwarf_onesrcline(lines, i);
  482. dwarf_lineno(line, &lineno);
  483. if (!line_list__has_line(&pf->lcache, lineno))
  484. continue;
  485. /* TODO: Get fileno from line, but how? */
  486. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  487. continue;
  488. ret = dwarf_lineaddr(line, &addr);
  489. DIE_IF(ret != 0);
  490. if (sp_die) {
  491. /* Address filtering 1: does sp_die include addr? */
  492. if (!dwarf_haspc(sp_die, addr))
  493. continue;
  494. /* Address filtering 2: No child include addr? */
  495. if (die_get_inlinefunc(sp_die, addr, &die_mem))
  496. continue;
  497. }
  498. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  499. (int)i, lineno, (unsigned long long)addr);
  500. pf->addr = addr;
  501. show_probe_point(sp_die, pf);
  502. /* Continuing, because target line might be inlined. */
  503. }
  504. /* TODO: deallocate lines, but how? */
  505. }
  506. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  507. {
  508. struct probe_finder *pf = (struct probe_finder *)data;
  509. struct probe_point *pp = pf->pp;
  510. if (pp->lazy_line)
  511. find_probe_point_lazy(in_die, pf);
  512. else {
  513. /* Get probe address */
  514. pf->addr = die_get_entrypc(in_die);
  515. pf->addr += pp->offset;
  516. pr_debug("found inline addr: 0x%jx\n",
  517. (uintmax_t)pf->addr);
  518. show_probe_point(in_die, pf);
  519. }
  520. return DWARF_CB_OK;
  521. }
  522. /* Search function from function name */
  523. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  524. {
  525. struct probe_finder *pf = (struct probe_finder *)data;
  526. struct probe_point *pp = pf->pp;
  527. /* Check tag and diename */
  528. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  529. die_compare_name(sp_die, pp->function) != 0)
  530. return 0;
  531. pf->fname = dwarf_decl_file(sp_die);
  532. if (pp->line) { /* Function relative line */
  533. dwarf_decl_line(sp_die, &pf->lno);
  534. pf->lno += pp->line;
  535. find_probe_point_by_line(pf);
  536. } else if (!dwarf_func_inline(sp_die)) {
  537. /* Real function */
  538. if (pp->lazy_line)
  539. find_probe_point_lazy(sp_die, pf);
  540. else {
  541. pf->addr = die_get_entrypc(sp_die);
  542. pf->addr += pp->offset;
  543. /* TODO: Check the address in this function */
  544. show_probe_point(sp_die, pf);
  545. }
  546. } else
  547. /* Inlined function: search instances */
  548. dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
  549. return 1; /* Exit; no same symbol in this CU. */
  550. }
  551. static void find_probe_point_by_func(struct probe_finder *pf)
  552. {
  553. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
  554. }
  555. /* Find a probe point */
  556. int find_probe_point(int fd, struct probe_point *pp)
  557. {
  558. struct probe_finder pf = {.pp = pp};
  559. int ret;
  560. Dwarf_Off off, noff;
  561. size_t cuhl;
  562. Dwarf_Die *diep;
  563. Dwarf *dbg;
  564. dbg = dwarf_begin(fd, DWARF_C_READ);
  565. if (!dbg)
  566. return -ENOENT;
  567. pp->found = 0;
  568. off = 0;
  569. line_list__init(&pf.lcache);
  570. /* Loop on CUs (Compilation Unit) */
  571. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  572. /* Get the DIE(Debugging Information Entry) of this CU */
  573. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  574. if (!diep)
  575. continue;
  576. /* Check if target file is included. */
  577. if (pp->file)
  578. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  579. else
  580. pf.fname = NULL;
  581. if (!pp->file || pf.fname) {
  582. /* Save CU base address (for frame_base) */
  583. ret = dwarf_lowpc(&pf.cu_die, &pf.cu_base);
  584. if (ret != 0)
  585. pf.cu_base = 0;
  586. if (pp->function)
  587. find_probe_point_by_func(&pf);
  588. else if (pp->lazy_line)
  589. find_probe_point_lazy(NULL, &pf);
  590. else {
  591. pf.lno = pp->line;
  592. find_probe_point_by_line(&pf);
  593. }
  594. }
  595. off = noff;
  596. }
  597. line_list__free(&pf.lcache);
  598. dwarf_end(dbg);
  599. return pp->found;
  600. }
  601. /* Find line range from its line number */
  602. static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  603. {
  604. Dwarf_Lines *lines;
  605. Dwarf_Line *line;
  606. size_t nlines, i;
  607. Dwarf_Addr addr;
  608. int lineno;
  609. int ret;
  610. const char *src;
  611. Dwarf_Die die_mem;
  612. line_list__init(&lf->lr->line_list);
  613. ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
  614. DIE_IF(ret != 0);
  615. for (i = 0; i < nlines; i++) {
  616. line = dwarf_onesrcline(lines, i);
  617. ret = dwarf_lineno(line, &lineno);
  618. DIE_IF(ret != 0);
  619. if (lf->lno_s > lineno || lf->lno_e < lineno)
  620. continue;
  621. if (sp_die) {
  622. /* Address filtering 1: does sp_die include addr? */
  623. ret = dwarf_lineaddr(line, &addr);
  624. DIE_IF(ret != 0);
  625. if (!dwarf_haspc(sp_die, addr))
  626. continue;
  627. /* Address filtering 2: No child include addr? */
  628. if (die_get_inlinefunc(sp_die, addr, &die_mem))
  629. continue;
  630. }
  631. /* TODO: Get fileno from line, but how? */
  632. src = dwarf_linesrc(line, NULL, NULL);
  633. if (strtailcmp(src, lf->fname) != 0)
  634. continue;
  635. /* Copy real path */
  636. if (!lf->lr->path)
  637. lf->lr->path = strdup(src);
  638. line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
  639. }
  640. /* Update status */
  641. if (!list_empty(&lf->lr->line_list))
  642. lf->found = 1;
  643. else {
  644. free(lf->lr->path);
  645. lf->lr->path = NULL;
  646. }
  647. }
  648. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  649. {
  650. find_line_range_by_line(in_die, (struct line_finder *)data);
  651. return DWARF_CB_ABORT; /* No need to find other instances */
  652. }
  653. /* Search function from function name */
  654. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  655. {
  656. struct line_finder *lf = (struct line_finder *)data;
  657. struct line_range *lr = lf->lr;
  658. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  659. die_compare_name(sp_die, lr->function) == 0) {
  660. lf->fname = dwarf_decl_file(sp_die);
  661. dwarf_decl_line(sp_die, &lr->offset);
  662. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  663. lf->lno_s = lr->offset + lr->start;
  664. if (!lr->end)
  665. lf->lno_e = INT_MAX;
  666. else
  667. lf->lno_e = lr->offset + lr->end;
  668. lr->start = lf->lno_s;
  669. lr->end = lf->lno_e;
  670. if (dwarf_func_inline(sp_die))
  671. dwarf_func_inline_instances(sp_die,
  672. line_range_inline_cb, lf);
  673. else
  674. find_line_range_by_line(sp_die, lf);
  675. return 1;
  676. }
  677. return 0;
  678. }
  679. static void find_line_range_by_func(struct line_finder *lf)
  680. {
  681. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
  682. }
  683. int find_line_range(int fd, struct line_range *lr)
  684. {
  685. struct line_finder lf = {.lr = lr, .found = 0};
  686. int ret;
  687. Dwarf_Off off = 0, noff;
  688. size_t cuhl;
  689. Dwarf_Die *diep;
  690. Dwarf *dbg;
  691. dbg = dwarf_begin(fd, DWARF_C_READ);
  692. if (!dbg)
  693. return -ENOENT;
  694. /* Loop on CUs (Compilation Unit) */
  695. while (!lf.found) {
  696. ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
  697. if (ret != 0)
  698. break;
  699. /* Get the DIE(Debugging Information Entry) of this CU */
  700. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  701. if (!diep)
  702. continue;
  703. /* Check if target file is included. */
  704. if (lr->file)
  705. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  706. else
  707. lf.fname = 0;
  708. if (!lr->file || lf.fname) {
  709. if (lr->function)
  710. find_line_range_by_func(&lf);
  711. else {
  712. lf.lno_s = lr->start;
  713. if (!lr->end)
  714. lf.lno_e = INT_MAX;
  715. else
  716. lf.lno_e = lr->end;
  717. find_line_range_by_line(NULL, &lf);
  718. }
  719. }
  720. off = noff;
  721. }
  722. pr_debug("path: %lx\n", (unsigned long)lr->path);
  723. dwarf_end(dbg);
  724. return lf.found;
  725. }