probe-finder.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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 convert_location(Dwarf_Op *op, struct probe_finder *pf)
  278. {
  279. unsigned int regn;
  280. Dwarf_Word offs = 0;
  281. bool ref = false;
  282. const char *regs;
  283. struct kprobe_trace_arg *tvar = pf->tvar;
  284. /* TODO: support CFA */
  285. /* If this is based on frame buffer, set the offset */
  286. if (op->atom == DW_OP_fbreg) {
  287. if (pf->fb_ops == NULL)
  288. die("The attribute of frame base is not supported.\n");
  289. ref = true;
  290. offs = op->number;
  291. op = &pf->fb_ops[0];
  292. }
  293. if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) {
  294. regn = op->atom - DW_OP_breg0;
  295. offs += op->number;
  296. ref = true;
  297. } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) {
  298. regn = op->atom - DW_OP_reg0;
  299. } else if (op->atom == DW_OP_bregx) {
  300. regn = op->number;
  301. offs += op->number2;
  302. ref = true;
  303. } else if (op->atom == DW_OP_regx) {
  304. regn = op->number;
  305. } else
  306. die("DW_OP %d is not supported.", op->atom);
  307. regs = get_arch_regstr(regn);
  308. if (!regs)
  309. die("%u exceeds max register number.", regn);
  310. tvar->value = xstrdup(regs);
  311. if (ref) {
  312. tvar->ref = xzalloc(sizeof(struct kprobe_trace_arg_ref));
  313. tvar->ref->offset = (long)offs;
  314. }
  315. }
  316. /* Show a variables in kprobe event format */
  317. static void convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf)
  318. {
  319. Dwarf_Attribute attr;
  320. Dwarf_Op *expr;
  321. size_t nexpr;
  322. int ret;
  323. if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
  324. goto error;
  325. /* TODO: handle more than 1 exprs */
  326. ret = dwarf_getlocation_addr(&attr, pf->addr, &expr, &nexpr, 1);
  327. if (ret <= 0 || nexpr == 0)
  328. goto error;
  329. convert_location(expr, pf);
  330. /* *expr will be cached in libdw. Don't free it. */
  331. return ;
  332. error:
  333. /* TODO: Support const_value */
  334. die("Failed to find the location of %s at this address.\n"
  335. " Perhaps, it has been optimized out.", pf->pvar->name);
  336. }
  337. /* Find a variable in a subprogram die */
  338. static void find_variable(Dwarf_Die *sp_die, struct probe_finder *pf)
  339. {
  340. Dwarf_Die vr_die;
  341. /* TODO: Support struct members and arrays */
  342. if (!is_c_varname(pf->pvar->name)) {
  343. /* Copy raw parameters */
  344. pf->tvar->value = xstrdup(pf->pvar->name);
  345. } else {
  346. pf->tvar->name = xstrdup(pf->pvar->name);
  347. pr_debug("Searching '%s' variable in context.\n",
  348. pf->pvar->name);
  349. /* Search child die for local variables and parameters. */
  350. if (!die_find_variable(sp_die, pf->pvar->name, &vr_die))
  351. die("Failed to find '%s' in this function.",
  352. pf->pvar->name);
  353. convert_variable(&vr_die, pf);
  354. }
  355. }
  356. /* Show a probe point to output buffer */
  357. static void convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
  358. {
  359. struct kprobe_trace_event *tev;
  360. Dwarf_Addr eaddr;
  361. Dwarf_Die die_mem;
  362. const char *name;
  363. int ret, i;
  364. Dwarf_Attribute fb_attr;
  365. size_t nops;
  366. if (pf->ntevs == MAX_PROBES)
  367. die("Too many( > %d) probe point found.\n", MAX_PROBES);
  368. tev = &pf->tevs[pf->ntevs++];
  369. /* If no real subprogram, find a real one */
  370. if (!sp_die || dwarf_tag(sp_die) != DW_TAG_subprogram) {
  371. sp_die = die_find_real_subprogram(&pf->cu_die,
  372. pf->addr, &die_mem);
  373. if (!sp_die)
  374. die("Probe point is not found in subprograms.");
  375. }
  376. /* Copy the name of probe point */
  377. name = dwarf_diename(sp_die);
  378. if (name) {
  379. dwarf_entrypc(sp_die, &eaddr);
  380. tev->point.symbol = xstrdup(name);
  381. tev->point.offset = (unsigned long)(pf->addr - eaddr);
  382. } else
  383. /* This function has no name. */
  384. tev->point.offset = (unsigned long)pf->addr;
  385. pr_debug("Probe point found: %s+%lu\n", tev->point.symbol,
  386. tev->point.offset);
  387. /* Get the frame base attribute/ops */
  388. dwarf_attr(sp_die, DW_AT_frame_base, &fb_attr);
  389. ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
  390. if (ret <= 0 || nops == 0)
  391. pf->fb_ops = NULL;
  392. /* Find each argument */
  393. /* TODO: use dwarf_cfi_addrframe */
  394. tev->nargs = pf->pev->nargs;
  395. tev->args = xzalloc(sizeof(struct kprobe_trace_arg) * tev->nargs);
  396. for (i = 0; i < pf->pev->nargs; i++) {
  397. pf->pvar = &pf->pev->args[i];
  398. pf->tvar = &tev->args[i];
  399. find_variable(sp_die, pf);
  400. }
  401. /* *pf->fb_ops will be cached in libdw. Don't free it. */
  402. pf->fb_ops = NULL;
  403. }
  404. /* Find probe point from its line number */
  405. static void find_probe_point_by_line(struct probe_finder *pf)
  406. {
  407. Dwarf_Lines *lines;
  408. Dwarf_Line *line;
  409. size_t nlines, i;
  410. Dwarf_Addr addr;
  411. int lineno;
  412. int ret;
  413. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  414. DIE_IF(ret != 0);
  415. for (i = 0; i < nlines; i++) {
  416. line = dwarf_onesrcline(lines, i);
  417. dwarf_lineno(line, &lineno);
  418. if (lineno != pf->lno)
  419. continue;
  420. /* TODO: Get fileno from line, but how? */
  421. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  422. continue;
  423. ret = dwarf_lineaddr(line, &addr);
  424. DIE_IF(ret != 0);
  425. pr_debug("Probe line found: line[%d]:%d addr:0x%jx\n",
  426. (int)i, lineno, (uintmax_t)addr);
  427. pf->addr = addr;
  428. convert_probe_point(NULL, pf);
  429. /* Continuing, because target line might be inlined. */
  430. }
  431. }
  432. /* Find lines which match lazy pattern */
  433. static int find_lazy_match_lines(struct list_head *head,
  434. const char *fname, const char *pat)
  435. {
  436. char *fbuf, *p1, *p2;
  437. int fd, line, nlines = 0;
  438. struct stat st;
  439. fd = open(fname, O_RDONLY);
  440. if (fd < 0)
  441. die("failed to open %s", fname);
  442. DIE_IF(fstat(fd, &st) < 0);
  443. fbuf = xmalloc(st.st_size + 2);
  444. DIE_IF(read(fd, fbuf, st.st_size) < 0);
  445. close(fd);
  446. fbuf[st.st_size] = '\n'; /* Dummy line */
  447. fbuf[st.st_size + 1] = '\0';
  448. p1 = fbuf;
  449. line = 1;
  450. while ((p2 = strchr(p1, '\n')) != NULL) {
  451. *p2 = '\0';
  452. if (strlazymatch(p1, pat)) {
  453. line_list__add_line(head, line);
  454. nlines++;
  455. }
  456. line++;
  457. p1 = p2 + 1;
  458. }
  459. free(fbuf);
  460. return nlines;
  461. }
  462. /* Find probe points from lazy pattern */
  463. static void find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf)
  464. {
  465. Dwarf_Lines *lines;
  466. Dwarf_Line *line;
  467. size_t nlines, i;
  468. Dwarf_Addr addr;
  469. Dwarf_Die die_mem;
  470. int lineno;
  471. int ret;
  472. if (list_empty(&pf->lcache)) {
  473. /* Matching lazy line pattern */
  474. ret = find_lazy_match_lines(&pf->lcache, pf->fname,
  475. pf->pev->point.lazy_line);
  476. if (ret <= 0)
  477. die("No matched lines found in %s.", pf->fname);
  478. }
  479. ret = dwarf_getsrclines(&pf->cu_die, &lines, &nlines);
  480. DIE_IF(ret != 0);
  481. for (i = 0; i < nlines; i++) {
  482. line = dwarf_onesrcline(lines, i);
  483. dwarf_lineno(line, &lineno);
  484. if (!line_list__has_line(&pf->lcache, lineno))
  485. continue;
  486. /* TODO: Get fileno from line, but how? */
  487. if (strtailcmp(dwarf_linesrc(line, NULL, NULL), pf->fname) != 0)
  488. continue;
  489. ret = dwarf_lineaddr(line, &addr);
  490. DIE_IF(ret != 0);
  491. if (sp_die) {
  492. /* Address filtering 1: does sp_die include addr? */
  493. if (!dwarf_haspc(sp_die, addr))
  494. continue;
  495. /* Address filtering 2: No child include addr? */
  496. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  497. continue;
  498. }
  499. pr_debug("Probe line found: line[%d]:%d addr:0x%llx\n",
  500. (int)i, lineno, (unsigned long long)addr);
  501. pf->addr = addr;
  502. convert_probe_point(sp_die, pf);
  503. /* Continuing, because target line might be inlined. */
  504. }
  505. /* TODO: deallocate lines, but how? */
  506. }
  507. static int probe_point_inline_cb(Dwarf_Die *in_die, void *data)
  508. {
  509. struct probe_finder *pf = (struct probe_finder *)data;
  510. struct perf_probe_point *pp = &pf->pev->point;
  511. if (pp->lazy_line)
  512. find_probe_point_lazy(in_die, pf);
  513. else {
  514. /* Get probe address */
  515. pf->addr = die_get_entrypc(in_die);
  516. pf->addr += pp->offset;
  517. pr_debug("found inline addr: 0x%jx\n",
  518. (uintmax_t)pf->addr);
  519. convert_probe_point(in_die, pf);
  520. }
  521. return DWARF_CB_OK;
  522. }
  523. /* Search function from function name */
  524. static int probe_point_search_cb(Dwarf_Die *sp_die, void *data)
  525. {
  526. struct probe_finder *pf = (struct probe_finder *)data;
  527. struct perf_probe_point *pp = &pf->pev->point;
  528. /* Check tag and diename */
  529. if (dwarf_tag(sp_die) != DW_TAG_subprogram ||
  530. die_compare_name(sp_die, pp->function) != 0)
  531. return 0;
  532. pf->fname = dwarf_decl_file(sp_die);
  533. if (pp->line) { /* Function relative line */
  534. dwarf_decl_line(sp_die, &pf->lno);
  535. pf->lno += pp->line;
  536. find_probe_point_by_line(pf);
  537. } else if (!dwarf_func_inline(sp_die)) {
  538. /* Real function */
  539. if (pp->lazy_line)
  540. find_probe_point_lazy(sp_die, pf);
  541. else {
  542. pf->addr = die_get_entrypc(sp_die);
  543. pf->addr += pp->offset;
  544. /* TODO: Check the address in this function */
  545. convert_probe_point(sp_die, pf);
  546. }
  547. } else
  548. /* Inlined function: search instances */
  549. dwarf_func_inline_instances(sp_die, probe_point_inline_cb, pf);
  550. return 1; /* Exit; no same symbol in this CU. */
  551. }
  552. static void find_probe_point_by_func(struct probe_finder *pf)
  553. {
  554. dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, pf, 0);
  555. }
  556. /* Find kprobe_trace_events specified by perf_probe_event from debuginfo */
  557. int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
  558. struct kprobe_trace_event **tevs)
  559. {
  560. struct probe_finder pf = {.pev = pev};
  561. struct perf_probe_point *pp = &pev->point;
  562. Dwarf_Off off, noff;
  563. size_t cuhl;
  564. Dwarf_Die *diep;
  565. Dwarf *dbg;
  566. pf.tevs = xzalloc(sizeof(struct kprobe_trace_event) * MAX_PROBES);
  567. *tevs = pf.tevs;
  568. pf.ntevs = 0;
  569. dbg = dwarf_begin(fd, DWARF_C_READ);
  570. if (!dbg)
  571. return -ENOENT;
  572. off = 0;
  573. line_list__init(&pf.lcache);
  574. /* Loop on CUs (Compilation Unit) */
  575. while (!dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL)) {
  576. /* Get the DIE(Debugging Information Entry) of this CU */
  577. diep = dwarf_offdie(dbg, off + cuhl, &pf.cu_die);
  578. if (!diep)
  579. continue;
  580. /* Check if target file is included. */
  581. if (pp->file)
  582. pf.fname = cu_find_realpath(&pf.cu_die, pp->file);
  583. else
  584. pf.fname = NULL;
  585. if (!pp->file || pf.fname) {
  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 pf.ntevs;
  600. }
  601. /* Reverse search */
  602. int find_perf_probe_point(int fd, unsigned long addr,
  603. struct perf_probe_point *ppt)
  604. {
  605. Dwarf_Die cudie, spdie, indie;
  606. Dwarf *dbg;
  607. Dwarf_Line *line;
  608. Dwarf_Addr laddr, eaddr;
  609. const char *tmp;
  610. int lineno, ret = 0;
  611. dbg = dwarf_begin(fd, DWARF_C_READ);
  612. if (!dbg)
  613. return -ENOENT;
  614. /* Find cu die */
  615. if (!dwarf_addrdie(dbg, (Dwarf_Addr)addr, &cudie))
  616. return -EINVAL;
  617. /* Find a corresponding line */
  618. line = dwarf_getsrc_die(&cudie, (Dwarf_Addr)addr);
  619. if (line) {
  620. dwarf_lineaddr(line, &laddr);
  621. if ((Dwarf_Addr)addr == laddr) {
  622. dwarf_lineno(line, &lineno);
  623. ppt->line = lineno;
  624. tmp = dwarf_linesrc(line, NULL, NULL);
  625. DIE_IF(!tmp);
  626. ppt->file = xstrdup(tmp);
  627. ret = 1;
  628. }
  629. }
  630. /* Find a corresponding function */
  631. if (die_find_real_subprogram(&cudie, (Dwarf_Addr)addr, &spdie)) {
  632. tmp = dwarf_diename(&spdie);
  633. if (!tmp)
  634. goto end;
  635. dwarf_entrypc(&spdie, &eaddr);
  636. if (!lineno) {
  637. /* We don't have a line number, let's use offset */
  638. ppt->function = xstrdup(tmp);
  639. ppt->offset = addr - (unsigned long)eaddr;
  640. ret = 1;
  641. goto end;
  642. }
  643. if (die_find_inlinefunc(&spdie, (Dwarf_Addr)addr, &indie)) {
  644. /* addr in an inline function */
  645. tmp = dwarf_diename(&indie);
  646. if (!tmp)
  647. goto end;
  648. dwarf_decl_line(&indie, &lineno);
  649. } else {
  650. if (eaddr == addr) /* No offset: function entry */
  651. lineno = ppt->line;
  652. else
  653. dwarf_decl_line(&spdie, &lineno);
  654. }
  655. ppt->function = xstrdup(tmp);
  656. ppt->line -= lineno; /* Make a relative line number */
  657. }
  658. end:
  659. dwarf_end(dbg);
  660. return ret;
  661. }
  662. /* Find line range from its line number */
  663. static void find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
  664. {
  665. Dwarf_Lines *lines;
  666. Dwarf_Line *line;
  667. size_t nlines, i;
  668. Dwarf_Addr addr;
  669. int lineno;
  670. int ret;
  671. const char *src;
  672. Dwarf_Die die_mem;
  673. line_list__init(&lf->lr->line_list);
  674. ret = dwarf_getsrclines(&lf->cu_die, &lines, &nlines);
  675. DIE_IF(ret != 0);
  676. for (i = 0; i < nlines; i++) {
  677. line = dwarf_onesrcline(lines, i);
  678. ret = dwarf_lineno(line, &lineno);
  679. DIE_IF(ret != 0);
  680. if (lf->lno_s > lineno || lf->lno_e < lineno)
  681. continue;
  682. if (sp_die) {
  683. /* Address filtering 1: does sp_die include addr? */
  684. ret = dwarf_lineaddr(line, &addr);
  685. DIE_IF(ret != 0);
  686. if (!dwarf_haspc(sp_die, addr))
  687. continue;
  688. /* Address filtering 2: No child include addr? */
  689. if (die_find_inlinefunc(sp_die, addr, &die_mem))
  690. continue;
  691. }
  692. /* TODO: Get fileno from line, but how? */
  693. src = dwarf_linesrc(line, NULL, NULL);
  694. if (strtailcmp(src, lf->fname) != 0)
  695. continue;
  696. /* Copy real path */
  697. if (!lf->lr->path)
  698. lf->lr->path = xstrdup(src);
  699. line_list__add_line(&lf->lr->line_list, (unsigned int)lineno);
  700. }
  701. /* Update status */
  702. if (!list_empty(&lf->lr->line_list))
  703. lf->found = 1;
  704. else {
  705. free(lf->lr->path);
  706. lf->lr->path = NULL;
  707. }
  708. }
  709. static int line_range_inline_cb(Dwarf_Die *in_die, void *data)
  710. {
  711. find_line_range_by_line(in_die, (struct line_finder *)data);
  712. return DWARF_CB_ABORT; /* No need to find other instances */
  713. }
  714. /* Search function from function name */
  715. static int line_range_search_cb(Dwarf_Die *sp_die, void *data)
  716. {
  717. struct line_finder *lf = (struct line_finder *)data;
  718. struct line_range *lr = lf->lr;
  719. if (dwarf_tag(sp_die) == DW_TAG_subprogram &&
  720. die_compare_name(sp_die, lr->function) == 0) {
  721. lf->fname = dwarf_decl_file(sp_die);
  722. dwarf_decl_line(sp_die, &lr->offset);
  723. pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset);
  724. lf->lno_s = lr->offset + lr->start;
  725. if (!lr->end)
  726. lf->lno_e = INT_MAX;
  727. else
  728. lf->lno_e = lr->offset + lr->end;
  729. lr->start = lf->lno_s;
  730. lr->end = lf->lno_e;
  731. if (dwarf_func_inline(sp_die))
  732. dwarf_func_inline_instances(sp_die,
  733. line_range_inline_cb, lf);
  734. else
  735. find_line_range_by_line(sp_die, lf);
  736. return 1;
  737. }
  738. return 0;
  739. }
  740. static void find_line_range_by_func(struct line_finder *lf)
  741. {
  742. dwarf_getfuncs(&lf->cu_die, line_range_search_cb, lf, 0);
  743. }
  744. int find_line_range(int fd, struct line_range *lr)
  745. {
  746. struct line_finder lf = {.lr = lr, .found = 0};
  747. int ret;
  748. Dwarf_Off off = 0, noff;
  749. size_t cuhl;
  750. Dwarf_Die *diep;
  751. Dwarf *dbg;
  752. dbg = dwarf_begin(fd, DWARF_C_READ);
  753. if (!dbg)
  754. return -ENOENT;
  755. /* Loop on CUs (Compilation Unit) */
  756. while (!lf.found) {
  757. ret = dwarf_nextcu(dbg, off, &noff, &cuhl, NULL, NULL, NULL);
  758. if (ret != 0)
  759. break;
  760. /* Get the DIE(Debugging Information Entry) of this CU */
  761. diep = dwarf_offdie(dbg, off + cuhl, &lf.cu_die);
  762. if (!diep)
  763. continue;
  764. /* Check if target file is included. */
  765. if (lr->file)
  766. lf.fname = cu_find_realpath(&lf.cu_die, lr->file);
  767. else
  768. lf.fname = 0;
  769. if (!lr->file || lf.fname) {
  770. if (lr->function)
  771. find_line_range_by_func(&lf);
  772. else {
  773. lf.lno_s = lr->start;
  774. if (!lr->end)
  775. lf.lno_e = INT_MAX;
  776. else
  777. lf.lno_e = lr->end;
  778. find_line_range_by_line(NULL, &lf);
  779. }
  780. }
  781. off = noff;
  782. }
  783. pr_debug("path: %lx\n", (unsigned long)lr->path);
  784. dwarf_end(dbg);
  785. return lf.found;
  786. }