probe-finder.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 "event.h"
  34. #include "debug.h"
  35. #include "util.h"
  36. #include "probe-finder.h"
  37. /* Dwarf_Die Linkage to parent Die */
  38. struct die_link {
  39. struct die_link *parent; /* Parent die */
  40. Dwarf_Die die; /* Current die */
  41. };
  42. static Dwarf_Debug __dw_debug;
  43. static Dwarf_Error __dw_error;
  44. /*
  45. * Generic dwarf analysis helpers
  46. */
  47. #define X86_32_MAX_REGS 8
  48. const char *x86_32_regs_table[X86_32_MAX_REGS] = {
  49. "%ax",
  50. "%cx",
  51. "%dx",
  52. "%bx",
  53. "$stack", /* Stack address instead of %sp */
  54. "%bp",
  55. "%si",
  56. "%di",
  57. };
  58. #define X86_64_MAX_REGS 16
  59. const char *x86_64_regs_table[X86_64_MAX_REGS] = {
  60. "%ax",
  61. "%dx",
  62. "%cx",
  63. "%bx",
  64. "%si",
  65. "%di",
  66. "%bp",
  67. "%sp",
  68. "%r8",
  69. "%r9",
  70. "%r10",
  71. "%r11",
  72. "%r12",
  73. "%r13",
  74. "%r14",
  75. "%r15",
  76. };
  77. /* TODO: switching by dwarf address size */
  78. #ifdef __x86_64__
  79. #define ARCH_MAX_REGS X86_64_MAX_REGS
  80. #define arch_regs_table x86_64_regs_table
  81. #else
  82. #define ARCH_MAX_REGS X86_32_MAX_REGS
  83. #define arch_regs_table x86_32_regs_table
  84. #endif
  85. /* Return architecture dependent register string (for kprobe-tracer) */
  86. static const char *get_arch_regstr(unsigned int n)
  87. {
  88. return (n <= ARCH_MAX_REGS) ? arch_regs_table[n] : NULL;
  89. }
  90. /*
  91. * Compare the tail of two strings.
  92. * Return 0 if whole of either string is same as another's tail part.
  93. */
  94. static int strtailcmp(const char *s1, const char *s2)
  95. {
  96. int i1 = strlen(s1);
  97. int i2 = strlen(s2);
  98. while (--i1 >= 0 && --i2 >= 0) {
  99. if (s1[i1] != s2[i2])
  100. return s1[i1] - s2[i2];
  101. }
  102. return 0;
  103. }
  104. /* Find the fileno of the target file. */
  105. static Dwarf_Unsigned cu_find_fileno(Dwarf_Die cu_die, const char *fname)
  106. {
  107. Dwarf_Signed cnt, i;
  108. Dwarf_Unsigned found = 0;
  109. char **srcs;
  110. int ret;
  111. if (!fname)
  112. return 0;
  113. ret = dwarf_srcfiles(cu_die, &srcs, &cnt, &__dw_error);
  114. if (ret == DW_DLV_OK) {
  115. for (i = 0; i < cnt && !found; i++) {
  116. if (strtailcmp(srcs[i], fname) == 0)
  117. found = i + 1;
  118. dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING);
  119. }
  120. for (; i < cnt; i++)
  121. dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING);
  122. dwarf_dealloc(__dw_debug, srcs, DW_DLA_LIST);
  123. }
  124. if (found)
  125. pr_debug("found fno: %d\n", (int)found);
  126. return found;
  127. }
  128. static int cu_get_filename(Dwarf_Die cu_die, Dwarf_Unsigned fno, char **buf)
  129. {
  130. Dwarf_Signed cnt, i;
  131. char **srcs;
  132. int ret = 0;
  133. if (!buf || !fno)
  134. return -EINVAL;
  135. ret = dwarf_srcfiles(cu_die, &srcs, &cnt, &__dw_error);
  136. if (ret == DW_DLV_OK) {
  137. if ((Dwarf_Unsigned)cnt > fno - 1) {
  138. *buf = strdup(srcs[fno - 1]);
  139. ret = 0;
  140. pr_debug("found filename: %s\n", *buf);
  141. } else
  142. ret = -ENOENT;
  143. for (i = 0; i < cnt; i++)
  144. dwarf_dealloc(__dw_debug, srcs[i], DW_DLA_STRING);
  145. dwarf_dealloc(__dw_debug, srcs, DW_DLA_LIST);
  146. } else
  147. ret = -EINVAL;
  148. return ret;
  149. }
  150. /* Compare diename and tname */
  151. static int die_compare_name(Dwarf_Die dw_die, const char *tname)
  152. {
  153. char *name;
  154. int ret;
  155. ret = dwarf_diename(dw_die, &name, &__dw_error);
  156. DIE_IF(ret == DW_DLV_ERROR);
  157. if (ret == DW_DLV_OK) {
  158. ret = strcmp(tname, name);
  159. dwarf_dealloc(__dw_debug, name, DW_DLA_STRING);
  160. } else
  161. ret = -1;
  162. return ret;
  163. }
  164. /* Check the address is in the subprogram(function). */
  165. static int die_within_subprogram(Dwarf_Die sp_die, Dwarf_Addr addr,
  166. Dwarf_Signed *offs)
  167. {
  168. Dwarf_Addr lopc, hipc;
  169. int ret;
  170. /* TODO: check ranges */
  171. ret = dwarf_lowpc(sp_die, &lopc, &__dw_error);
  172. DIE_IF(ret == DW_DLV_ERROR);
  173. if (ret == DW_DLV_NO_ENTRY)
  174. return 0;
  175. ret = dwarf_highpc(sp_die, &hipc, &__dw_error);
  176. DIE_IF(ret != DW_DLV_OK);
  177. if (lopc <= addr && addr < hipc) {
  178. *offs = addr - lopc;
  179. return 1;
  180. } else
  181. return 0;
  182. }
  183. /* Check the die is inlined function */
  184. static Dwarf_Bool die_inlined_subprogram(Dwarf_Die dw_die)
  185. {
  186. /* TODO: check strictly */
  187. Dwarf_Bool inl;
  188. int ret;
  189. ret = dwarf_hasattr(dw_die, DW_AT_inline, &inl, &__dw_error);
  190. DIE_IF(ret == DW_DLV_ERROR);
  191. return inl;
  192. }
  193. /* Get the offset of abstruct_origin */
  194. static Dwarf_Off die_get_abstract_origin(Dwarf_Die dw_die)
  195. {
  196. Dwarf_Attribute attr;
  197. Dwarf_Off cu_offs;
  198. int ret;
  199. ret = dwarf_attr(dw_die, DW_AT_abstract_origin, &attr, &__dw_error);
  200. DIE_IF(ret != DW_DLV_OK);
  201. ret = dwarf_formref(attr, &cu_offs, &__dw_error);
  202. DIE_IF(ret != DW_DLV_OK);
  203. dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR);
  204. return cu_offs;
  205. }
  206. /* Get entry pc(or low pc, 1st entry of ranges) of the die */
  207. static Dwarf_Addr die_get_entrypc(Dwarf_Die dw_die)
  208. {
  209. Dwarf_Attribute attr;
  210. Dwarf_Addr addr;
  211. Dwarf_Off offs;
  212. Dwarf_Ranges *ranges;
  213. Dwarf_Signed cnt;
  214. int ret;
  215. /* Try to get entry pc */
  216. ret = dwarf_attr(dw_die, DW_AT_entry_pc, &attr, &__dw_error);
  217. DIE_IF(ret == DW_DLV_ERROR);
  218. if (ret == DW_DLV_OK) {
  219. ret = dwarf_formaddr(attr, &addr, &__dw_error);
  220. DIE_IF(ret != DW_DLV_OK);
  221. dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR);
  222. return addr;
  223. }
  224. /* Try to get low pc */
  225. ret = dwarf_lowpc(dw_die, &addr, &__dw_error);
  226. DIE_IF(ret == DW_DLV_ERROR);
  227. if (ret == DW_DLV_OK)
  228. return addr;
  229. /* Try to get ranges */
  230. ret = dwarf_attr(dw_die, DW_AT_ranges, &attr, &__dw_error);
  231. DIE_IF(ret != DW_DLV_OK);
  232. ret = dwarf_formref(attr, &offs, &__dw_error);
  233. DIE_IF(ret != DW_DLV_OK);
  234. ret = dwarf_get_ranges(__dw_debug, offs, &ranges, &cnt, NULL,
  235. &__dw_error);
  236. DIE_IF(ret != DW_DLV_OK);
  237. addr = ranges[0].dwr_addr1;
  238. dwarf_ranges_dealloc(__dw_debug, ranges, cnt);
  239. return addr;
  240. }
  241. /*
  242. * Search a Die from Die tree.
  243. * Note: cur_link->die should be deallocated in this function.
  244. */
  245. static int __search_die_tree(struct die_link *cur_link,
  246. int (*die_cb)(struct die_link *, void *),
  247. void *data)
  248. {
  249. Dwarf_Die new_die;
  250. struct die_link new_link;
  251. int ret;
  252. if (!die_cb)
  253. return 0;
  254. /* Check current die */
  255. while (!(ret = die_cb(cur_link, data))) {
  256. /* Check child die */
  257. ret = dwarf_child(cur_link->die, &new_die, &__dw_error);
  258. DIE_IF(ret == DW_DLV_ERROR);
  259. if (ret == DW_DLV_OK) {
  260. new_link.parent = cur_link;
  261. new_link.die = new_die;
  262. ret = __search_die_tree(&new_link, die_cb, data);
  263. if (ret)
  264. break;
  265. }
  266. /* Move to next sibling */
  267. ret = dwarf_siblingof(__dw_debug, cur_link->die, &new_die,
  268. &__dw_error);
  269. DIE_IF(ret == DW_DLV_ERROR);
  270. dwarf_dealloc(__dw_debug, cur_link->die, DW_DLA_DIE);
  271. cur_link->die = new_die;
  272. if (ret == DW_DLV_NO_ENTRY)
  273. return 0;
  274. }
  275. dwarf_dealloc(__dw_debug, cur_link->die, DW_DLA_DIE);
  276. return ret;
  277. }
  278. /* Search a die in its children's die tree */
  279. static int search_die_from_children(Dwarf_Die parent_die,
  280. int (*die_cb)(struct die_link *, void *),
  281. void *data)
  282. {
  283. struct die_link new_link;
  284. int ret;
  285. new_link.parent = NULL;
  286. ret = dwarf_child(parent_die, &new_link.die, &__dw_error);
  287. DIE_IF(ret == DW_DLV_ERROR);
  288. if (ret == DW_DLV_OK)
  289. return __search_die_tree(&new_link, die_cb, data);
  290. else
  291. return 0;
  292. }
  293. /* Find a locdesc corresponding to the address */
  294. static int attr_get_locdesc(Dwarf_Attribute attr, Dwarf_Locdesc *desc,
  295. Dwarf_Addr addr)
  296. {
  297. Dwarf_Signed lcnt;
  298. Dwarf_Locdesc **llbuf;
  299. int ret, i;
  300. ret = dwarf_loclist_n(attr, &llbuf, &lcnt, &__dw_error);
  301. DIE_IF(ret != DW_DLV_OK);
  302. ret = DW_DLV_NO_ENTRY;
  303. for (i = 0; i < lcnt; ++i) {
  304. if (llbuf[i]->ld_lopc <= addr &&
  305. llbuf[i]->ld_hipc > addr) {
  306. memcpy(desc, llbuf[i], sizeof(Dwarf_Locdesc));
  307. desc->ld_s =
  308. malloc(sizeof(Dwarf_Loc) * llbuf[i]->ld_cents);
  309. DIE_IF(desc->ld_s == NULL);
  310. memcpy(desc->ld_s, llbuf[i]->ld_s,
  311. sizeof(Dwarf_Loc) * llbuf[i]->ld_cents);
  312. ret = DW_DLV_OK;
  313. break;
  314. }
  315. dwarf_dealloc(__dw_debug, llbuf[i]->ld_s, DW_DLA_LOC_BLOCK);
  316. dwarf_dealloc(__dw_debug, llbuf[i], DW_DLA_LOCDESC);
  317. }
  318. /* Releasing loop */
  319. for (; i < lcnt; ++i) {
  320. dwarf_dealloc(__dw_debug, llbuf[i]->ld_s, DW_DLA_LOC_BLOCK);
  321. dwarf_dealloc(__dw_debug, llbuf[i], DW_DLA_LOCDESC);
  322. }
  323. dwarf_dealloc(__dw_debug, llbuf, DW_DLA_LIST);
  324. return ret;
  325. }
  326. /* Get decl_file attribute value (file number) */
  327. static Dwarf_Unsigned die_get_decl_file(Dwarf_Die sp_die)
  328. {
  329. Dwarf_Attribute attr;
  330. Dwarf_Unsigned fno;
  331. int ret;
  332. ret = dwarf_attr(sp_die, DW_AT_decl_file, &attr, &__dw_error);
  333. DIE_IF(ret != DW_DLV_OK);
  334. dwarf_formudata(attr, &fno, &__dw_error);
  335. DIE_IF(ret != DW_DLV_OK);
  336. dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR);
  337. return fno;
  338. }
  339. /* Get decl_line attribute value (line number) */
  340. static Dwarf_Unsigned die_get_decl_line(Dwarf_Die sp_die)
  341. {
  342. Dwarf_Attribute attr;
  343. Dwarf_Unsigned lno;
  344. int ret;
  345. ret = dwarf_attr(sp_die, DW_AT_decl_line, &attr, &__dw_error);
  346. DIE_IF(ret != DW_DLV_OK);
  347. dwarf_formudata(attr, &lno, &__dw_error);
  348. DIE_IF(ret != DW_DLV_OK);
  349. dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR);
  350. return lno;
  351. }
  352. /*
  353. * Probe finder related functions
  354. */
  355. /* Show a location */
  356. static void show_location(Dwarf_Loc *loc, struct probe_finder *pf)
  357. {
  358. Dwarf_Small op;
  359. Dwarf_Unsigned regn;
  360. Dwarf_Signed offs;
  361. int deref = 0, ret;
  362. const char *regs;
  363. op = loc->lr_atom;
  364. /* If this is based on frame buffer, set the offset */
  365. if (op == DW_OP_fbreg) {
  366. deref = 1;
  367. offs = (Dwarf_Signed)loc->lr_number;
  368. op = pf->fbloc.ld_s[0].lr_atom;
  369. loc = &pf->fbloc.ld_s[0];
  370. } else
  371. offs = 0;
  372. if (op >= DW_OP_breg0 && op <= DW_OP_breg31) {
  373. regn = op - DW_OP_breg0;
  374. offs += (Dwarf_Signed)loc->lr_number;
  375. deref = 1;
  376. } else if (op >= DW_OP_reg0 && op <= DW_OP_reg31) {
  377. regn = op - DW_OP_reg0;
  378. } else if (op == DW_OP_bregx) {
  379. regn = loc->lr_number;
  380. offs += (Dwarf_Signed)loc->lr_number2;
  381. deref = 1;
  382. } else if (op == DW_OP_regx) {
  383. regn = loc->lr_number;
  384. } else
  385. die("Dwarf_OP %d is not supported.", op);
  386. regs = get_arch_regstr(regn);
  387. if (!regs)
  388. die("%lld exceeds max register number.", regn);
  389. if (deref)
  390. ret = snprintf(pf->buf, pf->len,
  391. " %s=%+lld(%s)", pf->var, offs, regs);
  392. else
  393. ret = snprintf(pf->buf, pf->len, " %s=%s", pf->var, regs);
  394. DIE_IF(ret < 0);
  395. DIE_IF(ret >= pf->len);
  396. }
  397. /* Show a variables in kprobe event format */
  398. static void show_variable(Dwarf_Die vr_die, struct probe_finder *pf)
  399. {
  400. Dwarf_Attribute attr;
  401. Dwarf_Locdesc ld;
  402. int ret;
  403. ret = dwarf_attr(vr_die, DW_AT_location, &attr, &__dw_error);
  404. if (ret != DW_DLV_OK)
  405. goto error;
  406. ret = attr_get_locdesc(attr, &ld, (pf->addr - pf->cu_base));
  407. if (ret != DW_DLV_OK)
  408. goto error;
  409. /* TODO? */
  410. DIE_IF(ld.ld_cents != 1);
  411. show_location(&ld.ld_s[0], pf);
  412. free(ld.ld_s);
  413. dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR);
  414. return ;
  415. error:
  416. die("Failed to find the location of %s at this address.\n"
  417. " Perhaps, it has been optimized out.", pf->var);
  418. }
  419. static int variable_callback(struct die_link *dlink, void *data)
  420. {
  421. struct probe_finder *pf = (struct probe_finder *)data;
  422. Dwarf_Half tag;
  423. int ret;
  424. ret = dwarf_tag(dlink->die, &tag, &__dw_error);
  425. DIE_IF(ret == DW_DLV_ERROR);
  426. if ((tag == DW_TAG_formal_parameter ||
  427. tag == DW_TAG_variable) &&
  428. (die_compare_name(dlink->die, pf->var) == 0)) {
  429. show_variable(dlink->die, pf);
  430. return 1;
  431. }
  432. /* TODO: Support struct members and arrays */
  433. return 0;
  434. }
  435. /* Find a variable in a subprogram die */
  436. static void find_variable(Dwarf_Die sp_die, struct probe_finder *pf)
  437. {
  438. int ret;
  439. if (!is_c_varname(pf->var)) {
  440. /* Output raw parameters */
  441. ret = snprintf(pf->buf, pf->len, " %s", pf->var);
  442. DIE_IF(ret < 0);
  443. DIE_IF(ret >= pf->len);
  444. return ;
  445. }
  446. pr_debug("Searching '%s' variable in context.\n", pf->var);
  447. /* Search child die for local variables and parameters. */
  448. ret = search_die_from_children(sp_die, variable_callback, pf);
  449. if (!ret)
  450. die("Failed to find '%s' in this function.", pf->var);
  451. }
  452. /* Get a frame base on the address */
  453. static void get_current_frame_base(Dwarf_Die sp_die, struct probe_finder *pf)
  454. {
  455. Dwarf_Attribute attr;
  456. int ret;
  457. ret = dwarf_attr(sp_die, DW_AT_frame_base, &attr, &__dw_error);
  458. DIE_IF(ret != DW_DLV_OK);
  459. ret = attr_get_locdesc(attr, &pf->fbloc, (pf->addr - pf->cu_base));
  460. DIE_IF(ret != DW_DLV_OK);
  461. dwarf_dealloc(__dw_debug, attr, DW_DLA_ATTR);
  462. }
  463. static void free_current_frame_base(struct probe_finder *pf)
  464. {
  465. free(pf->fbloc.ld_s);
  466. memset(&pf->fbloc, 0, sizeof(Dwarf_Locdesc));
  467. }
  468. /* Show a probe point to output buffer */
  469. static void show_probepoint(Dwarf_Die sp_die, Dwarf_Signed offs,
  470. struct probe_finder *pf)
  471. {
  472. struct probe_point *pp = pf->pp;
  473. char *name;
  474. char tmp[MAX_PROBE_BUFFER];
  475. int ret, i, len;
  476. /* Output name of probe point */
  477. ret = dwarf_diename(sp_die, &name, &__dw_error);
  478. DIE_IF(ret == DW_DLV_ERROR);
  479. if (ret == DW_DLV_OK) {
  480. ret = snprintf(tmp, MAX_PROBE_BUFFER, "%s+%u", name,
  481. (unsigned int)offs);
  482. /* Copy the function name if possible */
  483. if (!pp->function) {
  484. pp->function = strdup(name);
  485. pp->offset = offs;
  486. }
  487. dwarf_dealloc(__dw_debug, name, DW_DLA_STRING);
  488. } else {
  489. /* This function has no name. */
  490. ret = snprintf(tmp, MAX_PROBE_BUFFER, "0x%llx", pf->addr);
  491. if (!pp->function) {
  492. /* TODO: Use _stext */
  493. pp->function = strdup("");
  494. pp->offset = (int)pf->addr;
  495. }
  496. }
  497. DIE_IF(ret < 0);
  498. DIE_IF(ret >= MAX_PROBE_BUFFER);
  499. len = ret;
  500. pr_debug("Probe point found: %s\n", tmp);
  501. /* Find each argument */
  502. get_current_frame_base(sp_die, pf);
  503. for (i = 0; i < pp->nr_args; i++) {
  504. pf->var = pp->args[i];
  505. pf->buf = &tmp[len];
  506. pf->len = MAX_PROBE_BUFFER - len;
  507. find_variable(sp_die, pf);
  508. len += strlen(pf->buf);
  509. }
  510. free_current_frame_base(pf);
  511. pp->probes[pp->found] = strdup(tmp);
  512. pp->found++;
  513. }
  514. static int probeaddr_callback(struct die_link *dlink, void *data)
  515. {
  516. struct probe_finder *pf = (struct probe_finder *)data;
  517. Dwarf_Half tag;
  518. Dwarf_Signed offs;
  519. int ret;
  520. ret = dwarf_tag(dlink->die, &tag, &__dw_error);
  521. DIE_IF(ret == DW_DLV_ERROR);
  522. /* Check the address is in this subprogram */
  523. if (tag == DW_TAG_subprogram &&
  524. die_within_subprogram(dlink->die, pf->addr, &offs)) {
  525. show_probepoint(dlink->die, offs, pf);
  526. return 1;
  527. }
  528. return 0;
  529. }
  530. /* Find probe point from its line number */
  531. static void find_probe_point_by_line(struct probe_finder *pf)
  532. {
  533. Dwarf_Signed cnt, i, clm;
  534. Dwarf_Line *lines;
  535. Dwarf_Unsigned lineno = 0;
  536. Dwarf_Addr addr;
  537. Dwarf_Unsigned fno;
  538. int ret;
  539. ret = dwarf_srclines(pf->cu_die, &lines, &cnt, &__dw_error);
  540. DIE_IF(ret != DW_DLV_OK);
  541. for (i = 0; i < cnt; i++) {
  542. ret = dwarf_line_srcfileno(lines[i], &fno, &__dw_error);
  543. DIE_IF(ret != DW_DLV_OK);
  544. if (fno != pf->fno)
  545. continue;
  546. ret = dwarf_lineno(lines[i], &lineno, &__dw_error);
  547. DIE_IF(ret != DW_DLV_OK);
  548. if (lineno != pf->lno)
  549. continue;
  550. ret = dwarf_lineoff(lines[i], &clm, &__dw_error);
  551. DIE_IF(ret != DW_DLV_OK);
  552. ret = dwarf_lineaddr(lines[i], &addr, &__dw_error);
  553. DIE_IF(ret != DW_DLV_OK);
  554. pr_debug("Probe line found: line[%d]:%u,%d addr:0x%llx\n",
  555. (int)i, (unsigned)lineno, (int)clm, addr);
  556. pf->addr = addr;
  557. /* Search a real subprogram including this line, */
  558. ret = search_die_from_children(pf->cu_die,
  559. probeaddr_callback, pf);
  560. if (ret == 0)
  561. die("Probe point is not found in subprograms.");
  562. /* Continuing, because target line might be inlined. */
  563. }
  564. dwarf_srclines_dealloc(__dw_debug, lines, cnt);
  565. }
  566. /* Search function from function name */
  567. static int probefunc_callback(struct die_link *dlink, void *data)
  568. {
  569. struct probe_finder *pf = (struct probe_finder *)data;
  570. struct probe_point *pp = pf->pp;
  571. struct die_link *lk;
  572. Dwarf_Signed offs;
  573. Dwarf_Half tag;
  574. int ret;
  575. ret = dwarf_tag(dlink->die, &tag, &__dw_error);
  576. DIE_IF(ret == DW_DLV_ERROR);
  577. if (tag == DW_TAG_subprogram) {
  578. if (die_compare_name(dlink->die, pp->function) == 0) {
  579. if (pp->line) { /* Function relative line */
  580. pf->fno = die_get_decl_file(dlink->die);
  581. pf->lno = die_get_decl_line(dlink->die)
  582. + pp->line;
  583. find_probe_point_by_line(pf);
  584. return 1;
  585. }
  586. if (die_inlined_subprogram(dlink->die)) {
  587. /* Inlined function, save it. */
  588. ret = dwarf_die_CU_offset(dlink->die,
  589. &pf->inl_offs,
  590. &__dw_error);
  591. DIE_IF(ret != DW_DLV_OK);
  592. pr_debug("inline definition offset %lld\n",
  593. pf->inl_offs);
  594. return 0; /* Continue to search */
  595. }
  596. /* Get probe address */
  597. pf->addr = die_get_entrypc(dlink->die);
  598. pf->addr += pp->offset;
  599. /* TODO: Check the address in this function */
  600. show_probepoint(dlink->die, pp->offset, pf);
  601. return 1; /* Exit; no same symbol in this CU. */
  602. }
  603. } else if (tag == DW_TAG_inlined_subroutine && pf->inl_offs) {
  604. if (die_get_abstract_origin(dlink->die) == pf->inl_offs) {
  605. /* Get probe address */
  606. pf->addr = die_get_entrypc(dlink->die);
  607. pf->addr += pp->offset;
  608. pr_debug("found inline addr: 0x%llx\n", pf->addr);
  609. /* Inlined function. Get a real subprogram */
  610. for (lk = dlink->parent; lk != NULL; lk = lk->parent) {
  611. tag = 0;
  612. dwarf_tag(lk->die, &tag, &__dw_error);
  613. DIE_IF(ret == DW_DLV_ERROR);
  614. if (tag == DW_TAG_subprogram &&
  615. !die_inlined_subprogram(lk->die))
  616. goto found;
  617. }
  618. die("Failed to find real subprogram.");
  619. found:
  620. /* Get offset from subprogram */
  621. ret = die_within_subprogram(lk->die, pf->addr, &offs);
  622. DIE_IF(!ret);
  623. show_probepoint(lk->die, offs, pf);
  624. /* Continue to search */
  625. }
  626. }
  627. return 0;
  628. }
  629. static void find_probe_point_by_func(struct probe_finder *pf)
  630. {
  631. search_die_from_children(pf->cu_die, probefunc_callback, pf);
  632. }
  633. /* Find a probe point */
  634. int find_probepoint(int fd, struct probe_point *pp)
  635. {
  636. Dwarf_Half addr_size = 0;
  637. Dwarf_Unsigned next_cuh = 0;
  638. int cu_number = 0, ret;
  639. struct probe_finder pf = {.pp = pp};
  640. ret = dwarf_init(fd, DW_DLC_READ, 0, 0, &__dw_debug, &__dw_error);
  641. if (ret != DW_DLV_OK)
  642. return -ENOENT;
  643. pp->found = 0;
  644. while (++cu_number) {
  645. /* Search CU (Compilation Unit) */
  646. ret = dwarf_next_cu_header(__dw_debug, NULL, NULL, NULL,
  647. &addr_size, &next_cuh, &__dw_error);
  648. DIE_IF(ret == DW_DLV_ERROR);
  649. if (ret == DW_DLV_NO_ENTRY)
  650. break;
  651. /* Get the DIE(Debugging Information Entry) of this CU */
  652. ret = dwarf_siblingof(__dw_debug, 0, &pf.cu_die, &__dw_error);
  653. DIE_IF(ret != DW_DLV_OK);
  654. /* Check if target file is included. */
  655. if (pp->file)
  656. pf.fno = cu_find_fileno(pf.cu_die, pp->file);
  657. if (!pp->file || pf.fno) {
  658. /* Save CU base address (for frame_base) */
  659. ret = dwarf_lowpc(pf.cu_die, &pf.cu_base, &__dw_error);
  660. DIE_IF(ret == DW_DLV_ERROR);
  661. if (ret == DW_DLV_NO_ENTRY)
  662. pf.cu_base = 0;
  663. if (pp->function)
  664. find_probe_point_by_func(&pf);
  665. else {
  666. pf.lno = pp->line;
  667. find_probe_point_by_line(&pf);
  668. }
  669. }
  670. dwarf_dealloc(__dw_debug, pf.cu_die, DW_DLA_DIE);
  671. }
  672. ret = dwarf_finish(__dw_debug, &__dw_error);
  673. DIE_IF(ret != DW_DLV_OK);
  674. return pp->found;
  675. }
  676. static void line_range_add_line(struct line_range *lr, unsigned int line)
  677. {
  678. struct line_node *ln;
  679. struct list_head *p;
  680. /* Reverse search, because new line will be the last one */
  681. list_for_each_entry_reverse(ln, &lr->line_list, list) {
  682. if (ln->line < line) {
  683. p = &ln->list;
  684. goto found;
  685. } else if (ln->line == line) /* Already exist */
  686. return ;
  687. }
  688. /* List is empty, or the smallest entry */
  689. p = &lr->line_list;
  690. found:
  691. pr_debug("Debug: add a line %u\n", line);
  692. ln = zalloc(sizeof(struct line_node));
  693. DIE_IF(ln == NULL);
  694. ln->line = line;
  695. INIT_LIST_HEAD(&ln->list);
  696. list_add(&ln->list, p);
  697. }
  698. /* Find line range from its line number */
  699. static void find_line_range_by_line(struct line_finder *lf)
  700. {
  701. Dwarf_Signed cnt, i;
  702. Dwarf_Line *lines;
  703. Dwarf_Unsigned lineno = 0;
  704. Dwarf_Unsigned fno;
  705. Dwarf_Addr addr;
  706. int ret;
  707. ret = dwarf_srclines(lf->cu_die, &lines, &cnt, &__dw_error);
  708. DIE_IF(ret != DW_DLV_OK);
  709. for (i = 0; i < cnt; i++) {
  710. ret = dwarf_line_srcfileno(lines[i], &fno, &__dw_error);
  711. DIE_IF(ret != DW_DLV_OK);
  712. if (fno != lf->fno)
  713. continue;
  714. ret = dwarf_lineno(lines[i], &lineno, &__dw_error);
  715. DIE_IF(ret != DW_DLV_OK);
  716. if (lf->lno_s > lineno || lf->lno_e < lineno)
  717. continue;
  718. /* Filter line in the function address range */
  719. if (lf->addr_s && lf->addr_e) {
  720. ret = dwarf_lineaddr(lines[i], &addr, &__dw_error);
  721. DIE_IF(ret != DW_DLV_OK);
  722. if (lf->addr_s > addr || lf->addr_e <= addr)
  723. continue;
  724. }
  725. line_range_add_line(lf->lr, (unsigned int)lineno);
  726. }
  727. dwarf_srclines_dealloc(__dw_debug, lines, cnt);
  728. if (!list_empty(&lf->lr->line_list))
  729. lf->found = 1;
  730. }
  731. /* Search function from function name */
  732. static int linefunc_callback(struct die_link *dlink, void *data)
  733. {
  734. struct line_finder *lf = (struct line_finder *)data;
  735. struct line_range *lr = lf->lr;
  736. Dwarf_Half tag;
  737. int ret;
  738. ret = dwarf_tag(dlink->die, &tag, &__dw_error);
  739. DIE_IF(ret == DW_DLV_ERROR);
  740. if (tag == DW_TAG_subprogram &&
  741. die_compare_name(dlink->die, lr->function) == 0) {
  742. /* Get the address range of this function */
  743. ret = dwarf_highpc(dlink->die, &lf->addr_e, &__dw_error);
  744. if (ret == DW_DLV_OK)
  745. ret = dwarf_lowpc(dlink->die, &lf->addr_s, &__dw_error);
  746. DIE_IF(ret == DW_DLV_ERROR);
  747. if (ret == DW_DLV_NO_ENTRY) {
  748. lf->addr_s = 0;
  749. lf->addr_e = 0;
  750. }
  751. lf->fno = die_get_decl_file(dlink->die);
  752. lr->offset = die_get_decl_line(dlink->die);;
  753. lf->lno_s = lr->offset + lr->start;
  754. if (!lr->end)
  755. lf->lno_e = (Dwarf_Unsigned)-1;
  756. else
  757. lf->lno_e = lr->offset + lr->end;
  758. lr->start = lf->lno_s;
  759. lr->end = lf->lno_e;
  760. find_line_range_by_line(lf);
  761. /* If we find a target function, this should be end. */
  762. lf->found = 1;
  763. return 1;
  764. }
  765. return 0;
  766. }
  767. static void find_line_range_by_func(struct line_finder *lf)
  768. {
  769. search_die_from_children(lf->cu_die, linefunc_callback, lf);
  770. }
  771. int find_line_range(int fd, struct line_range *lr)
  772. {
  773. Dwarf_Half addr_size = 0;
  774. Dwarf_Unsigned next_cuh = 0;
  775. int ret;
  776. struct line_finder lf = {.lr = lr};
  777. ret = dwarf_init(fd, DW_DLC_READ, 0, 0, &__dw_debug, &__dw_error);
  778. if (ret != DW_DLV_OK)
  779. return -ENOENT;
  780. while (!lf.found) {
  781. /* Search CU (Compilation Unit) */
  782. ret = dwarf_next_cu_header(__dw_debug, NULL, NULL, NULL,
  783. &addr_size, &next_cuh, &__dw_error);
  784. DIE_IF(ret == DW_DLV_ERROR);
  785. if (ret == DW_DLV_NO_ENTRY)
  786. break;
  787. /* Get the DIE(Debugging Information Entry) of this CU */
  788. ret = dwarf_siblingof(__dw_debug, 0, &lf.cu_die, &__dw_error);
  789. DIE_IF(ret != DW_DLV_OK);
  790. /* Check if target file is included. */
  791. if (lr->file)
  792. lf.fno = cu_find_fileno(lf.cu_die, lr->file);
  793. if (!lr->file || lf.fno) {
  794. if (lr->function)
  795. find_line_range_by_func(&lf);
  796. else {
  797. lf.lno_s = lr->start;
  798. if (!lr->end)
  799. lf.lno_e = (Dwarf_Unsigned)-1;
  800. else
  801. lf.lno_e = lr->end;
  802. find_line_range_by_line(&lf);
  803. }
  804. /* Get the real file path */
  805. if (lf.found)
  806. cu_get_filename(lf.cu_die, lf.fno, &lr->path);
  807. }
  808. dwarf_dealloc(__dw_debug, lf.cu_die, DW_DLA_DIE);
  809. }
  810. ret = dwarf_finish(__dw_debug, &__dw_error);
  811. DIE_IF(ret != DW_DLV_OK);
  812. return lf.found;
  813. }