srcline.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 : 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. if (a2l->found)
  153. return;
  154. if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
  155. return;
  156. pc = a2l->addr;
  157. vma = bfd_get_section_vma(abfd, section);
  158. size = bfd_get_section_size(section);
  159. if (pc < vma || pc >= vma + size)
  160. return;
  161. a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
  162. &a2l->filename, &a2l->funcname,
  163. &a2l->line);
  164. if (a2l->filename && !strlen(a2l->filename))
  165. a2l->filename = NULL;
  166. }
  167. static struct a2l_data *addr2line_init(const char *path)
  168. {
  169. bfd *abfd;
  170. struct a2l_data *a2l = NULL;
  171. abfd = bfd_openr(path, NULL);
  172. if (abfd == NULL)
  173. return NULL;
  174. if (!bfd_check_format(abfd, bfd_object))
  175. goto out;
  176. a2l = zalloc(sizeof(*a2l));
  177. if (a2l == NULL)
  178. goto out;
  179. a2l->abfd = abfd;
  180. a2l->input = strdup(path);
  181. if (a2l->input == NULL)
  182. goto out;
  183. if (slurp_symtab(abfd, a2l))
  184. goto out;
  185. return a2l;
  186. out:
  187. if (a2l) {
  188. zfree((char **)&a2l->input);
  189. free(a2l);
  190. }
  191. bfd_close(abfd);
  192. return NULL;
  193. }
  194. static void addr2line_cleanup(struct a2l_data *a2l)
  195. {
  196. if (a2l->abfd)
  197. bfd_close(a2l->abfd);
  198. zfree((char **)&a2l->input);
  199. zfree(&a2l->syms);
  200. free(a2l);
  201. }
  202. #define MAX_INLINE_NEST 1024
  203. static int inline_list__append_dso_a2l(struct dso *dso,
  204. struct inline_node *node,
  205. struct symbol *sym)
  206. {
  207. struct a2l_data *a2l = dso->a2l;
  208. struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
  209. char *srcline = NULL;
  210. if (a2l->filename)
  211. srcline = srcline_from_fileline(a2l->filename, a2l->line);
  212. return inline_list__append(inline_sym, srcline, node);
  213. }
  214. static int addr2line(const char *dso_name, u64 addr,
  215. char **file, unsigned int *line, struct dso *dso,
  216. bool unwind_inlines, struct inline_node *node,
  217. struct symbol *sym)
  218. {
  219. int ret = 0;
  220. struct a2l_data *a2l = dso->a2l;
  221. if (!a2l) {
  222. dso->a2l = addr2line_init(dso_name);
  223. a2l = dso->a2l;
  224. }
  225. if (a2l == NULL) {
  226. pr_warning("addr2line_init failed for %s\n", dso_name);
  227. return 0;
  228. }
  229. a2l->addr = addr;
  230. a2l->found = false;
  231. bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
  232. if (!a2l->found)
  233. return 0;
  234. if (unwind_inlines) {
  235. int cnt = 0;
  236. if (node && inline_list__append_dso_a2l(dso, node, sym))
  237. return 0;
  238. while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
  239. &a2l->funcname, &a2l->line) &&
  240. cnt++ < MAX_INLINE_NEST) {
  241. if (a2l->filename && !strlen(a2l->filename))
  242. a2l->filename = NULL;
  243. if (node != NULL) {
  244. if (inline_list__append_dso_a2l(dso, node, sym))
  245. return 0;
  246. // found at least one inline frame
  247. ret = 1;
  248. }
  249. }
  250. }
  251. if (file) {
  252. *file = a2l->filename ? strdup(a2l->filename) : NULL;
  253. ret = *file ? 1 : 0;
  254. }
  255. if (line)
  256. *line = a2l->line;
  257. return ret;
  258. }
  259. void dso__free_a2l(struct dso *dso)
  260. {
  261. struct a2l_data *a2l = dso->a2l;
  262. if (!a2l)
  263. return;
  264. addr2line_cleanup(a2l);
  265. dso->a2l = NULL;
  266. }
  267. static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
  268. struct dso *dso, struct symbol *sym)
  269. {
  270. struct inline_node *node;
  271. node = zalloc(sizeof(*node));
  272. if (node == NULL) {
  273. perror("not enough memory for the inline node");
  274. return NULL;
  275. }
  276. INIT_LIST_HEAD(&node->val);
  277. node->addr = addr;
  278. addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
  279. return node;
  280. }
  281. #else /* HAVE_LIBBFD_SUPPORT */
  282. static int filename_split(char *filename, unsigned int *line_nr)
  283. {
  284. char *sep;
  285. sep = strchr(filename, '\n');
  286. if (sep)
  287. *sep = '\0';
  288. if (!strcmp(filename, "??:0"))
  289. return 0;
  290. sep = strchr(filename, ':');
  291. if (sep) {
  292. *sep++ = '\0';
  293. *line_nr = strtoul(sep, NULL, 0);
  294. return 1;
  295. }
  296. return 0;
  297. }
  298. static int addr2line(const char *dso_name, u64 addr,
  299. char **file, unsigned int *line_nr,
  300. struct dso *dso __maybe_unused,
  301. bool unwind_inlines __maybe_unused,
  302. struct inline_node *node __maybe_unused,
  303. struct symbol *sym __maybe_unused)
  304. {
  305. FILE *fp;
  306. char cmd[PATH_MAX];
  307. char *filename = NULL;
  308. size_t len;
  309. int ret = 0;
  310. scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
  311. dso_name, addr);
  312. fp = popen(cmd, "r");
  313. if (fp == NULL) {
  314. pr_warning("popen failed for %s\n", dso_name);
  315. return 0;
  316. }
  317. if (getline(&filename, &len, fp) < 0 || !len) {
  318. pr_warning("addr2line has no output for %s\n", dso_name);
  319. goto out;
  320. }
  321. ret = filename_split(filename, line_nr);
  322. if (ret != 1) {
  323. free(filename);
  324. goto out;
  325. }
  326. *file = filename;
  327. out:
  328. pclose(fp);
  329. return ret;
  330. }
  331. void dso__free_a2l(struct dso *dso __maybe_unused)
  332. {
  333. }
  334. static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
  335. struct dso *dso __maybe_unused,
  336. struct symbol *sym)
  337. {
  338. FILE *fp;
  339. char cmd[PATH_MAX];
  340. struct inline_node *node;
  341. char *filename = NULL;
  342. char *funcname = NULL;
  343. size_t filelen, funclen;
  344. unsigned int line_nr = 0;
  345. scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
  346. dso_name, addr);
  347. fp = popen(cmd, "r");
  348. if (fp == NULL) {
  349. pr_err("popen failed for %s\n", dso_name);
  350. return NULL;
  351. }
  352. node = zalloc(sizeof(*node));
  353. if (node == NULL) {
  354. perror("not enough memory for the inline node");
  355. goto out;
  356. }
  357. INIT_LIST_HEAD(&node->val);
  358. node->addr = addr;
  359. /* addr2line -f generates two lines for each inlined functions */
  360. while (getline(&funcname, &funclen, fp) != -1) {
  361. char *srcline;
  362. struct symbol *inline_sym;
  363. rtrim(funcname);
  364. if (getline(&filename, &filelen, fp) == -1)
  365. goto out;
  366. if (filename_split(filename, &line_nr) != 1)
  367. goto out;
  368. srcline = srcline_from_fileline(filename, line_nr);
  369. inline_sym = new_inline_sym(dso, sym, funcname);
  370. if (inline_list__append(inline_sym, srcline, node) != 0) {
  371. free(srcline);
  372. if (inline_sym && inline_sym->inlined)
  373. symbol__delete(inline_sym);
  374. goto out;
  375. }
  376. }
  377. out:
  378. pclose(fp);
  379. free(filename);
  380. free(funcname);
  381. return node;
  382. }
  383. #endif /* HAVE_LIBBFD_SUPPORT */
  384. /*
  385. * Number of addr2line failures (without success) before disabling it for that
  386. * dso.
  387. */
  388. #define A2L_FAIL_LIMIT 123
  389. char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
  390. bool show_sym, bool show_addr, bool unwind_inlines,
  391. u64 ip)
  392. {
  393. char *file = NULL;
  394. unsigned line = 0;
  395. char *srcline;
  396. const char *dso_name;
  397. if (!dso->has_srcline)
  398. goto out;
  399. dso_name = dso__name(dso);
  400. if (dso_name == NULL)
  401. goto out;
  402. if (!addr2line(dso_name, addr, &file, &line, dso,
  403. unwind_inlines, NULL, sym))
  404. goto out;
  405. srcline = srcline_from_fileline(file, line);
  406. free(file);
  407. if (!srcline)
  408. goto out;
  409. dso->a2l_fails = 0;
  410. return srcline;
  411. out:
  412. if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
  413. dso->has_srcline = 0;
  414. dso__free_a2l(dso);
  415. }
  416. if (!show_addr)
  417. return (show_sym && sym) ?
  418. strndup(sym->name, sym->namelen) : NULL;
  419. if (sym) {
  420. if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
  421. ip - sym->start) < 0)
  422. return SRCLINE_UNKNOWN;
  423. } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
  424. return SRCLINE_UNKNOWN;
  425. return srcline;
  426. }
  427. void free_srcline(char *srcline)
  428. {
  429. if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
  430. free(srcline);
  431. }
  432. char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
  433. bool show_sym, bool show_addr, u64 ip)
  434. {
  435. return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
  436. }
  437. struct srcline_node {
  438. u64 addr;
  439. char *srcline;
  440. struct rb_node rb_node;
  441. };
  442. void srcline__tree_insert(struct rb_root *tree, u64 addr, char *srcline)
  443. {
  444. struct rb_node **p = &tree->rb_node;
  445. struct rb_node *parent = NULL;
  446. struct srcline_node *i, *node;
  447. node = zalloc(sizeof(struct srcline_node));
  448. if (!node) {
  449. perror("not enough memory for the srcline node");
  450. return;
  451. }
  452. node->addr = addr;
  453. node->srcline = srcline;
  454. while (*p != NULL) {
  455. parent = *p;
  456. i = rb_entry(parent, struct srcline_node, rb_node);
  457. if (addr < i->addr)
  458. p = &(*p)->rb_left;
  459. else
  460. p = &(*p)->rb_right;
  461. }
  462. rb_link_node(&node->rb_node, parent, p);
  463. rb_insert_color(&node->rb_node, tree);
  464. }
  465. char *srcline__tree_find(struct rb_root *tree, u64 addr)
  466. {
  467. struct rb_node *n = tree->rb_node;
  468. while (n) {
  469. struct srcline_node *i = rb_entry(n, struct srcline_node,
  470. rb_node);
  471. if (addr < i->addr)
  472. n = n->rb_left;
  473. else if (addr > i->addr)
  474. n = n->rb_right;
  475. else
  476. return i->srcline;
  477. }
  478. return NULL;
  479. }
  480. void srcline__tree_delete(struct rb_root *tree)
  481. {
  482. struct srcline_node *pos;
  483. struct rb_node *next = rb_first(tree);
  484. while (next) {
  485. pos = rb_entry(next, struct srcline_node, rb_node);
  486. next = rb_next(&pos->rb_node);
  487. rb_erase(&pos->rb_node, tree);
  488. free_srcline(pos->srcline);
  489. zfree(&pos);
  490. }
  491. }
  492. struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
  493. struct symbol *sym)
  494. {
  495. const char *dso_name;
  496. dso_name = dso__name(dso);
  497. if (dso_name == NULL)
  498. return NULL;
  499. return addr2inlines(dso_name, addr, dso, sym);
  500. }
  501. void inline_node__delete(struct inline_node *node)
  502. {
  503. struct inline_list *ilist, *tmp;
  504. list_for_each_entry_safe(ilist, tmp, &node->val, list) {
  505. list_del_init(&ilist->list);
  506. free_srcline(ilist->srcline);
  507. /* only the inlined symbols are owned by the list */
  508. if (ilist->symbol && ilist->symbol->inlined)
  509. symbol__delete(ilist->symbol);
  510. free(ilist);
  511. }
  512. free(node);
  513. }
  514. void inlines__tree_insert(struct rb_root *tree, struct inline_node *inlines)
  515. {
  516. struct rb_node **p = &tree->rb_node;
  517. struct rb_node *parent = NULL;
  518. const u64 addr = inlines->addr;
  519. struct inline_node *i;
  520. while (*p != NULL) {
  521. parent = *p;
  522. i = rb_entry(parent, struct inline_node, rb_node);
  523. if (addr < i->addr)
  524. p = &(*p)->rb_left;
  525. else
  526. p = &(*p)->rb_right;
  527. }
  528. rb_link_node(&inlines->rb_node, parent, p);
  529. rb_insert_color(&inlines->rb_node, tree);
  530. }
  531. struct inline_node *inlines__tree_find(struct rb_root *tree, u64 addr)
  532. {
  533. struct rb_node *n = tree->rb_node;
  534. while (n) {
  535. struct inline_node *i = rb_entry(n, struct inline_node,
  536. rb_node);
  537. if (addr < i->addr)
  538. n = n->rb_left;
  539. else if (addr > i->addr)
  540. n = n->rb_right;
  541. else
  542. return i;
  543. }
  544. return NULL;
  545. }
  546. void inlines__tree_delete(struct rb_root *tree)
  547. {
  548. struct inline_node *pos;
  549. struct rb_node *next = rb_first(tree);
  550. while (next) {
  551. pos = rb_entry(next, struct inline_node, rb_node);
  552. next = rb_next(&pos->rb_node);
  553. rb_erase(&pos->rb_node, tree);
  554. inline_node__delete(pos);
  555. }
  556. }