srcline.c 15 KB

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