probe-finder.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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 = NULL;
  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=%+jd(%s)",
  292. pf->var, (intmax_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, &expr, &nexpr, 1);
  309. if (ret <= 0 || nexpr == 0)
  310. goto error;
  311. show_location(expr, pf);
  312. /* *expr will be cached in libdw. Don't free it. */
  313. return ;
  314. error:
  315. /* TODO: Support const_value */
  316. die("Failed to find the location of %s at this address.\n"
  317. " Perhaps, it has been optimized out.", pf->var);
  318. }
  319. /* Find a variable in a subprogram die */
  320. static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  321. {
  322. int ret;
  323. Dwarf_Die vr_die;
  324. /* TODO: Support struct members and arrays */
  325. if (!is_c_varname(pf->var)) {
  326. /* Output raw parameters */
  327. ret = snprintf(pf->buf, pf->len, " %s", pf->var);
  328. DIE_IF(ret < 0);
  329. DIE_IF(ret >= pf->len);
  330. return ;
  331. }
  332. pr_debug("Searching '%s' variable in context.\n", pf->var);
  333. /* Search child die for local variables and parameters. */
  334. if (!die_find_variable(sp_die, pf->var, &vr_die))
  335. die("Failed to find '%s' in this function.", pf->var);
  336. show_variable(&vr_die, pf);
  337. }
  338. /* Show a probe point to output buffer */
  339. static void show_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  340. {
  341. struct probe_point *pp = pf->pp;
  342. Dwarf_Addr eaddr;
  343. Dwarf_Die die_mem;
  344. const char *name;
  345. char tmp[MAX_PROBE_BUFFER];
  346. int ret, i, len;
  347. Dwarf_Attribute fb_attr;
  348. size_t nops;
  349. /* If no real subprogram, find a real one */
  350. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  351. sp_die = die_get_real_subprogram(&pf->cu_die,
  352. pf->addr, &die_mem);
  353. if (!sp_die)
  354. die("Probe point is not found in subprograms.");
  355. }
  356. /* Output name of probe point */
  357. name = dwarf_diename(sp_die);
  358. if (name) {
  359. dwarf_entrypc(sp_die, &eaddr);
  360. ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%lu", name,
  361. (unsigned long)(pf->addr - eaddr));
  362. /* Copy the function name if possible */
  363. if (!pp->function) {
  364. pp->function = strdup(name);
  365. pp->offset = (size_t)(pf->addr - eaddr);
  366. }
  367. } else {
  368. /* This function has no name. */
  369. ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%jx",
  370. (uintmax_t)pf->addr);
  371. if (!pp->function) {
  372. /* TODO: Use _stext */
  373. pp->function = strdup("");
  374. pp->offset = (size_t)pf->addr;
  375. }
  376. }
  377. DIE_IF(ret < 0);
  378. DIE_IF(ret >= MAX_PROBE_BUFFER);
  379. len = ret;
  380. pr_debug("Probe point found: %s\n", tmp);
  381. /* Get the frame base attribute/ops */
  382. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  383. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  384. if (ret <= 0 || nops == 0)
  385. pf->fb_ops = NULL;
  386. /* Find each argument */
  387. /* TODO: use dwarf_cfi_addrframe */
  388. for (i = 0; i < pp->nr_args; i++) {
  389. pf->var = pp->args[i];
  390. pf->buf = &tmp[len];
  391. pf->len = MAX_PROBE_BUFFER - len;
  392. find_variable(sp_die, pf);
  393. len += strlen(pf->buf);
  394. }
  395. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  396. pf->fb_ops = NULL;
  397. if (pp->found == MAX_PROBES)
  398. die("Too many( > %d) probe point found.\n", MAX_PROBES);
  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. Dwarf_Off off, noff;
  560. size_t cuhl;
  561. Dwarf_Die *diep;
  562. Dwarf *dbg;
  563. dbg = dwarf_begin(fd, DWARF_C_READ);
  564. if (!dbg)
  565. return -ENOENT;
  566. pp->found = 0;
  567. off = 0;
  568. line_list__init(&pf.lcache);
  569. /* Loop on CUs (Compilation Unit) */
  570. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  571. /* Get the DIE(Debugging Information Entry) of this CU */
  572. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  573. if (!diep)
  574. continue;
  575. /* Check if target file is included. */
  576. if (pp->file)
  577. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  578. else
  579. pf.fname = NULL;
  580. if (!pp->file || pf.fname) {
  581. if (pp->function)
  582. find_probe_point_by_func(&pf);
  583. else if (pp->lazy_line)
  584. find_probe_point_lazy(NULL, &pf);
  585. else {
  586. pf.lno = pp->line;
  587. find_probe_point_by_line(&pf);
  588. }
  589. }
  590. off = noff;
  591. }
  592. line_list__free(&pf.lcache);
  593. dwarf_end(dbg);
  594. return pp->found;
  595. }
  596. /* Find line range from its line number */
  597. static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  598. {
  599. Dwarf_Lines *lines;
  600. Dwarf_Line *line;
  601. size_t nlines, i;
  602. Dwarf_Addr addr;
  603. int lineno;
  604. int ret;
  605. const char *src;
  606. Dwarf_Die die_mem;
  607. line_list__init(&lf->lr->line_list);
  608. ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
  609. DIE_IF(ret != 0);
  610. for (i = 0; i < nlines; i++) {
  611. line = dwarf_onesrcline(lines, i);
  612. ret = dwarf_lineno(line, &lineno);
  613. DIE_IF(ret != 0);
  614. if (lf->lno_s > lineno || lf->lno_e < lineno)
  615. continue;
  616. if (sp_die) {
  617. /* Address filtering 1: does sp_die include addr? */
  618. ret = dwarf_lineaddr(line, &addr);
  619. DIE_IF(ret != 0);
  620. if (!dwarf_haspc(sp_die, addr))
  621. continue;
  622. /* Address filtering 2: No child include addr? */
  623. if (die_get_inlinefunc(sp_die, addr, &die_mem))
  624. continue;
  625. }
  626. /* TODO: Get fileno from line, but how? */
  627. src = dwarf_linesrc(line, NULL, NULL);
  628. if (strtailcmp(src, lf->fname) != 0)
  629. continue;
  630. /* Copy real path */
  631. if (!lf->lr->path)
  632. lf->lr->path = strdup(src);
  633. line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
  634. }
  635. /* Update status */
  636. if (!list_empty(&lf->lr->line_list))
  637. lf->found = 1;
  638. else {
  639. free(lf->lr->path);
  640. lf->lr->path = NULL;
  641. }
  642. }
  643. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  644. {
  645. find_line_range_by_line(in_die, (struct line_finder *)data);
  646. return DWARF_CB_ABORT; /* No need to find other instances */
  647. }
  648. /* Search function from function name */
  649. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  650. {
  651. struct line_finder *lf = (struct line_finder *)data;
  652. struct line_range *lr = lf->lr;
  653. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  654. die_compare_name(sp_die, lr->function) == 0) {
  655. lf->fname = dwarf_decl_file(sp_die);
  656. dwarf_decl_line(sp_die, &lr->offset);
  657. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  658. lf->lno_s = lr->offset + lr->start;
  659. if (!lr->end)
  660. lf->lno_e = INT_MAX;
  661. else
  662. lf->lno_e = lr->offset + lr->end;
  663. lr->start = lf->lno_s;
  664. lr->end = lf->lno_e;
  665. if (dwarf_func_inline(sp_die))
  666. dwarf_func_inline_instances(sp_die,
  667. line_range_inline_cb, lf);
  668. else
  669. find_line_range_by_line(sp_die, lf);
  670. return 1;
  671. }
  672. return 0;
  673. }
  674. static void find_line_range_by_func(struct line_finder *lf)
  675. {
  676. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
  677. }
  678. int find_line_range(int fd, struct line_range *lr)
  679. {
  680. struct line_finder lf = {.lr = lr, .found = 0};
  681. int ret;
  682. Dwarf_Off off = 0, noff;
  683. size_t cuhl;
  684. Dwarf_Die *diep;
  685. Dwarf *dbg;
  686. dbg = dwarf_begin(fd, DWARF_C_READ);
  687. if (!dbg)
  688. return -ENOENT;
  689. /* Loop on CUs (Compilation Unit) */
  690. while (!lf.found) {
  691. ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
  692. if (ret != 0)
  693. break;
  694. /* Get the DIE(Debugging Information Entry) of this CU */
  695. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  696. if (!diep)
  697. continue;
  698. /* Check if target file is included. */
  699. if (lr->file)
  700. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  701. else
  702. lf.fname = 0;
  703. if (!lr->file || lf.fname) {
  704. if (lr->function)
  705. find_line_range_by_func(&lf);
  706. else {
  707. lf.lno_s = lr->start;
  708. if (!lr->end)
  709. lf.lno_e = INT_MAX;
  710. else
  711. lf.lno_e = lr->end;
  712. find_line_range_by_line(NULL, &lf);
  713. }
  714. }
  715. off = noff;
  716. }
  717. pr_debug("path: %lx\n", (unsigned long)lr->path);
  718. dwarf_end(dbg);
  719. return lf.found;
  720. }