probe-finder.c 19 KB

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