probe-finder.c 20 KB

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