srcline.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <linux/kernel.h>
  7. #include "util/dso.h"
  8. #include "util/util.h"
  9. #include "util/debug.h"
  10. #include "util/callchain.h"
  11. #include "srcline.h"
  12. #include "string2.h"
  13. #include "symbol.h"
  14. bool srcline_full_filename;
  15. static const char *dso__name(struct dso *dso)
  16. {
  17. const char *dso_name;
  18. if (dso->symsrc_filename)
  19. dso_name = dso->symsrc_filename;
  20. else
  21. dso_name = dso->long_name;
  22. if (dso_name[0] == '[')
  23. return NULL;
  24. if (!strncmp(dso_name, "/tmp/perf-", 10))
  25. return NULL;
  26. return dso_name;
  27. }
  28. static int inline_list__append(struct symbol *symbol, char *srcline,
  29. struct inline_node *node)
  30. {
  31. struct inline_list *ilist;
  32. ilist = zalloc(sizeof(*ilist));
  33. if (ilist == NULL)
  34. return -1;
  35. ilist->symbol = symbol;
  36. ilist->srcline = srcline;
  37. if (callchain_param.order == ORDER_CALLEE)
  38. list_add_tail(&ilist->list, &node->val);
  39. else
  40. list_add(&ilist->list, &node->val);
  41. return 0;
  42. }
  43. /* basename version that takes a const input string */
  44. static const char *gnu_basename(const char *path)
  45. {
  46. const char *base = strrchr(path, '/');
  47. return base ? base + 1 : path;
  48. }
  49. static char *srcline_from_fileline(const char *file, unsigned int line)
  50. {
  51. char *srcline;
  52. if (!file)
  53. return NULL;
  54. if (!srcline_full_filename)
  55. file = gnu_basename(file);
  56. if (asprintf(&srcline, "%s:%u", file, line) < 0)
  57. return NULL;
  58. return srcline;
  59. }
  60. static struct symbol *new_inline_sym(struct dso *dso,
  61. struct symbol *base_sym,
  62. const char *funcname)
  63. {
  64. struct symbol *inline_sym;
  65. char *demangled = NULL;
  66. if (!funcname)
  67. funcname = "??";
  68. if (dso) {
  69. demangled = dso__demangle_sym(dso, 0, funcname);
  70. if (demangled)
  71. funcname = demangled;
  72. }
  73. if (base_sym && strcmp(funcname, base_sym->name) == 0) {
  74. /* reuse the real, existing symbol */
  75. inline_sym = base_sym;
  76. /* ensure that we don't alias an inlined symbol, which could
  77. * lead to double frees in inline_node__delete
  78. */
  79. assert(!base_sym->inlined);
  80. } else {
  81. /* create a fake symbol for the inline frame */
  82. inline_sym = symbol__new(base_sym ? base_sym->start : 0,
  83. base_sym ? (base_sym->end - base_sym->start) : 0,
  84. base_sym ? base_sym->binding : 0,
  85. base_sym ? base_sym->type : 0,
  86. funcname);
  87. if (inline_sym)
  88. inline_sym->inlined = 1;
  89. }
  90. free(demangled);
  91. return inline_sym;
  92. }
  93. #ifdef HAVE_LIBBFD_SUPPORT
  94. /*
  95. * Implement addr2line using libbfd.
  96. */
  97. #define PACKAGE "perf"
  98. #include <bfd.h>
  99. struct a2l_data {
  100. const char *input;
  101. u64 addr;
  102. bool found;
  103. const char *filename;
  104. const char *funcname;
  105. unsigned line;
  106. bfd *abfd;
  107. asymbol **syms;
  108. };
  109. static int bfd_error(const char *string)
  110. {
  111. const char *errmsg;
  112. errmsg = bfd_errmsg(bfd_get_error());
  113. fflush(stdout);
  114. if (string)
  115. pr_debug("%s: %s\n", string, errmsg);
  116. else
  117. pr_debug("%s\n", errmsg);
  118. return -1;
  119. }
  120. static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
  121. {
  122. long storage;
  123. long symcount;
  124. asymbol **syms;
  125. bfd_boolean dynamic = FALSE;
  126. if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
  127. return bfd_error(bfd_get_filename(abfd));
  128. storage = bfd_get_symtab_upper_bound(abfd);
  129. if (storage == 0L) {
  130. storage = bfd_get_dynamic_symtab_upper_bound(abfd);
  131. dynamic = TRUE;
  132. }
  133. if (storage < 0L)
  134. return bfd_error(bfd_get_filename(abfd));
  135. syms = malloc(storage);
  136. if (dynamic)
  137. symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
  138. else
  139. symcount = bfd_canonicalize_symtab(abfd, syms);
  140. if (symcount < 0) {
  141. free(syms);
  142. return bfd_error(bfd_get_filename(abfd));
  143. }
  144. a2l->syms = syms;
  145. return 0;
  146. }
  147. static void find_address_in_section(bfd *abfd, asection *section, void *data)
  148. {
  149. bfd_vma pc, vma;
  150. bfd_size_type size;
  151. struct a2l_data *a2l = data;
  152. flagword flags;
  153. if (a2l->found)
  154. return;
  155. #ifdef bfd_get_section_flags
  156. flags = bfd_get_section_flags(abfd, section);
  157. #else
  158. flags = bfd_section_flags(section);
  159. #endif
  160. if ((flags & SEC_ALLOC) == 0)
  161. return;
  162. pc = a2l->addr;
  163. #ifdef bfd_get_section_vma
  164. vma = bfd_get_section_vma(abfd, section);
  165. #else
  166. vma = bfd_section_vma(section);
  167. #endif
  168. #ifdef bfd_get_section_size
  169. size = bfd_get_section_size(section);
  170. #else
  171. size = bfd_section_size(section);
  172. #endif
  173. if (pc < vma || pc >= vma + size)
  174. return;
  175. a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
  176. &a2l->filename, &a2l->funcname,
  177. &a2l->line);
  178. if (a2l->filename && !strlen(a2l->filename))
  179. a2l->filename = NULL;
  180. }
  181. static struct a2l_data *addr2line_init(const char *path)
  182. {
  183. bfd *abfd;
  184. struct a2l_data *a2l = NULL;
  185. abfd = bfd_openr(path, NULL);
  186. if (abfd == NULL)
  187. return NULL;
  188. if (!bfd_check_format(abfd, bfd_object))
  189. goto out;
  190. a2l = zalloc(sizeof(*a2l));
  191. if (a2l == NULL)
  192. goto out;
  193. a2l->abfd = abfd;
  194. a2l->input = strdup(path);
  195. if (a2l->input == NULL)
  196. goto out;
  197. if (slurp_symtab(abfd, a2l))
  198. goto out;
  199. return a2l;
  200. out:
  201. if (a2l) {
  202. zfree((char **)&a2l->input);
  203. free(a2l);
  204. }
  205. bfd_close(abfd);
  206. return NULL;
  207. }
  208. static void addr2line_cleanup(struct a2l_data *a2l)
  209. {
  210. if (a2l->abfd)
  211. bfd_close(a2l->abfd);
  212. zfree((char **)&a2l->input);
  213. zfree(&a2l->syms);
  214. free(a2l);
  215. }
  216. #define MAX_INLINE_NEST 1024
  217. static int inline_list__append_dso_a2l(struct dso *dso,
  218. struct inline_node *node,
  219. struct symbol *sym)
  220. {
  221. struct a2l_data *a2l = dso->a2l;
  222. struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
  223. char *srcline = NULL;
  224. if (a2l->filename)
  225. srcline = srcline_from_fileline(a2l->filename, a2l->line);
  226. return inline_list__append(inline_sym, srcline, node);
  227. }
  228. static int addr2line(const char *dso_name, u64 addr,
  229. char **file, unsigned int *line, struct dso *dso,
  230. bool unwind_inlines, struct inline_node *node,
  231. struct symbol *sym)
  232. {
  233. int ret = 0;
  234. struct a2l_data *a2l = dso->a2l;
  235. if (!a2l) {
  236. dso->a2l = addr2line_init(dso_name);
  237. a2l = dso->a2l;
  238. }
  239. if (a2l == NULL) {
  240. pr_warning("addr2line_init failed for %s\n", dso_name);
  241. return 0;
  242. }
  243. a2l->addr = addr;
  244. a2l->found = false;
  245. bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
  246. if (!a2l->found)
  247. return 0;
  248. if (unwind_inlines) {
  249. int cnt = 0;
  250. if (node && inline_list__append_dso_a2l(dso, node, sym))
  251. return 0;
  252. while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
  253. &a2l->funcname, &a2l->line) &&
  254. cnt++ < MAX_INLINE_NEST) {
  255. if (a2l->filename && !strlen(a2l->filename))
  256. a2l->filename = NULL;
  257. if (node != NULL) {
  258. if (inline_list__append_dso_a2l(dso, node, sym))
  259. return 0;
  260. // found at least one inline frame
  261. ret = 1;
  262. }
  263. }
  264. }
  265. if (file) {
  266. *file = a2l->filename ? strdup(a2l->filename) : NULL;
  267. ret = *file ? 1 : 0;
  268. }
  269. if (line)
  270. *line = a2l->line;
  271. return ret;
  272. }
  273. void dso__free_a2l(struct dso *dso)
  274. {
  275. struct a2l_data *a2l = dso->a2l;
  276. if (!a2l)
  277. return;
  278. addr2line_cleanup(a2l);
  279. dso->a2l = NULL;
  280. }
  281. static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
  282. struct dso *dso, struct symbol *sym)
  283. {
  284. struct inline_node *node;
  285. node = zalloc(sizeof(*node));
  286. if (node == NULL) {
  287. perror("not enough memory for the inline node");
  288. return NULL;
  289. }
  290. INIT_LIST_HEAD(&node->val);
  291. node->addr = addr;
  292. addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
  293. return node;
  294. }
  295. #else /* HAVE_LIBBFD_SUPPORT */
  296. static int filename_split(char *filename, unsigned int *line_nr)
  297. {
  298. char *sep;
  299. sep = strchr(filename, '\n');
  300. if (sep)
  301. *sep = '\0';
  302. if (!strcmp(filename, "??:0"))
  303. return 0;
  304. sep = strchr(filename, ':');
  305. if (sep) {
  306. *sep++ = '\0';
  307. *line_nr = strtoul(sep, NULL, 0);
  308. return 1;
  309. }
  310. return 0;
  311. }
  312. static int addr2line(const char *dso_name, u64 addr,
  313. char **file, unsigned int *line_nr,
  314. struct dso *dso __maybe_unused,
  315. bool unwind_inlines __maybe_unused,
  316. struct inline_node *node __maybe_unused,
  317. struct symbol *sym __maybe_unused)
  318. {
  319. FILE *fp;
  320. char cmd[PATH_MAX];
  321. char *filename = NULL;
  322. size_t len;
  323. int ret = 0;
  324. scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
  325. dso_name, addr);
  326. fp = popen(cmd, "r");
  327. if (fp == NULL) {
  328. pr_warning("popen failed for %s\n", dso_name);
  329. return 0;
  330. }
  331. if (getline(&filename, &len, fp) < 0 || !len) {
  332. pr_warning("addr2line has no output for %s\n", dso_name);
  333. goto out;
  334. }
  335. ret = filename_split(filename, line_nr);
  336. if (ret != 1) {
  337. free(filename);
  338. goto out;
  339. }
  340. *file = filename;
  341. out:
  342. pclose(fp);
  343. return ret;
  344. }
  345. void dso__free_a2l(struct dso *dso __maybe_unused)
  346. {
  347. }
  348. static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
  349. struct dso *dso __maybe_unused,
  350. struct symbol *sym)
  351. {
  352. FILE *fp;
  353. char cmd[PATH_MAX];
  354. struct inline_node *node;
  355. char *filename = NULL;
  356. char *funcname = NULL;
  357. size_t filelen, funclen;
  358. unsigned int line_nr = 0;
  359. scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
  360. dso_name, addr);
  361. fp = popen(cmd, "r");
  362. if (fp == NULL) {
  363. pr_err("popen failed for %s\n", dso_name);
  364. return NULL;
  365. }
  366. node = zalloc(sizeof(*node));
  367. if (node == NULL) {
  368. perror("not enough memory for the inline node");
  369. goto out;
  370. }
  371. INIT_LIST_HEAD(&node->val);
  372. node->addr = addr;
  373. /* addr2line -f generates two lines for each inlined functions */
  374. while (getline(&funcname, &funclen, fp) != -1) {
  375. char *srcline;
  376. struct symbol *inline_sym;
  377. rtrim(funcname);
  378. if (getline(&filename, &filelen, fp) == -1)
  379. goto out;
  380. if (filename_split(filename, &line_nr) != 1)
  381. goto out;
  382. srcline = srcline_from_fileline(filename, line_nr);
  383. inline_sym = new_inline_sym(dso, sym, funcname);
  384. if (inline_list__append(inline_sym, srcline, node) != 0) {
  385. free(srcline);
  386. if (inline_sym && inline_sym->inlined)
  387. symbol__delete(inline_sym);
  388. goto out;
  389. }
  390. }
  391. out:
  392. pclose(fp);
  393. free(filename);
  394. free(funcname);
  395. return node;
  396. }
  397. #endif /* HAVE_LIBBFD_SUPPORT */
  398. /*
  399. * Number of addr2line failures (without success) before disabling it for that
  400. * dso.
  401. */
  402. #define A2L_FAIL_LIMIT 123
  403. char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
  404. bool show_sym, bool show_addr, bool unwind_inlines,
  405. u64 ip)
  406. {
  407. char *file = NULL;
  408. unsigned line = 0;
  409. char *srcline;
  410. const char *dso_name;
  411. if (!dso->has_srcline)
  412. goto out;
  413. dso_name = dso__name(dso);
  414. if (dso_name == NULL)
  415. goto out;
  416. if (!addr2line(dso_name, addr, &file, &line, dso,
  417. unwind_inlines, NULL, sym))
  418. goto out;
  419. srcline = srcline_from_fileline(file, line);
  420. free(file);
  421. if (!srcline)
  422. goto out;
  423. dso->a2l_fails = 0;
  424. return srcline;
  425. out:
  426. if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
  427. dso->has_srcline = 0;
  428. dso__free_a2l(dso);
  429. }
  430. if (!show_addr)
  431. return (show_sym && sym) ?
  432. strndup(sym->name, sym->namelen) : NULL;
  433. if (sym) {
  434. if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
  435. ip - sym->start) < 0)
  436. return SRCLINE_UNKNOWN;
  437. } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
  438. return SRCLINE_UNKNOWN;
  439. return srcline;
  440. }
  441. void free_srcline(char *srcline)
  442. {
  443. if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
  444. free(srcline);
  445. }
  446. char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
  447. bool show_sym, bool show_addr, u64 ip)
  448. {
  449. return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
  450. }
  451. struct srcline_node {
  452. u64 addr;
  453. char *srcline;
  454. struct rb_node rb_node;
  455. };
  456. void srcline__tree_insert(struct rb_root *tree, u64 addr, char *srcline)
  457. {
  458. struct rb_node **p = &tree->rb_node;
  459. struct rb_node *parent = NULL;
  460. struct srcline_node *i, *node;
  461. node = zalloc(sizeof(struct srcline_node));
  462. if (!node) {
  463. perror("not enough memory for the srcline node");
  464. return;
  465. }
  466. node->addr = addr;
  467. node->srcline = srcline;
  468. while (*p != NULL) {
  469. parent = *p;
  470. i = rb_entry(parent, struct srcline_node, rb_node);
  471. if (addr < i->addr)
  472. p = &(*p)->rb_left;
  473. else
  474. p = &(*p)->rb_right;
  475. }
  476. rb_link_node(&node->rb_node, parent, p);
  477. rb_insert_color(&node->rb_node, tree);
  478. }
  479. char *srcline__tree_find(struct rb_root *tree, u64 addr)
  480. {
  481. struct rb_node *n = tree->rb_node;
  482. while (n) {
  483. struct srcline_node *i = rb_entry(n, struct srcline_node,
  484. rb_node);
  485. if (addr < i->addr)
  486. n = n->rb_left;
  487. else if (addr > i->addr)
  488. n = n->rb_right;
  489. else
  490. return i->srcline;
  491. }
  492. return NULL;
  493. }
  494. void srcline__tree_delete(struct rb_root *tree)
  495. {
  496. struct srcline_node *pos;
  497. struct rb_node *next = rb_first(tree);
  498. while (next) {
  499. pos = rb_entry(next, struct srcline_node, rb_node);
  500. next = rb_next(&pos->rb_node);
  501. rb_erase(&pos->rb_node, tree);
  502. free_srcline(pos->srcline);
  503. zfree(&pos);
  504. }
  505. }
  506. struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
  507. struct symbol *sym)
  508. {
  509. const char *dso_name;
  510. dso_name = dso__name(dso);
  511. if (dso_name == NULL)
  512. return NULL;
  513. return addr2inlines(dso_name, addr, dso, sym);
  514. }
  515. void inline_node__delete(struct inline_node *node)
  516. {
  517. struct inline_list *ilist, *tmp;
  518. list_for_each_entry_safe(ilist, tmp, &node->val, list) {
  519. list_del_init(&ilist->list);
  520. free_srcline(ilist->srcline);
  521. /* only the inlined symbols are owned by the list */
  522. if (ilist->symbol && ilist->symbol->inlined)
  523. symbol__delete(ilist->symbol);
  524. free(ilist);
  525. }
  526. free(node);
  527. }
  528. void inlines__tree_insert(struct rb_root *tree, struct inline_node *inlines)
  529. {
  530. struct rb_node **p = &tree->rb_node;
  531. struct rb_node *parent = NULL;
  532. const u64 addr = inlines->addr;
  533. struct inline_node *i;
  534. while (*p != NULL) {
  535. parent = *p;
  536. i = rb_entry(parent, struct inline_node, rb_node);
  537. if (addr < i->addr)
  538. p = &(*p)->rb_left;
  539. else
  540. p = &(*p)->rb_right;
  541. }
  542. rb_link_node(&inlines->rb_node, parent, p);
  543. rb_insert_color(&inlines->rb_node, tree);
  544. }
  545. struct inline_node *inlines__tree_find(struct rb_root *tree, u64 addr)
  546. {
  547. struct rb_node *n = tree->rb_node;
  548. while (n) {
  549. struct inline_node *i = rb_entry(n, struct inline_node,
  550. rb_node);
  551. if (addr < i->addr)
  552. n = n->rb_left;
  553. else if (addr > i->addr)
  554. n = n->rb_right;
  555. else
  556. return i;
  557. }
  558. return NULL;
  559. }
  560. void inlines__tree_delete(struct rb_root *tree)
  561. {
  562. struct inline_node *pos;
  563. struct rb_node *next = rb_first(tree);
  564. while (next) {
  565. pos = rb_entry(next, struct inline_node, rb_node);
  566. next = rb_next(&pos->rb_node);
  567. rb_erase(&pos->rb_node, tree);
  568. inline_node__delete(pos);
  569. }
  570. }