srcline.c 14 KB

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