srcline.c 14 KB

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