libdw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "dso.h"
  3. #include "libdw.h"
  4. #include "srcline.h"
  5. #include "symbol.h"
  6. #include "dwarf-aux.h"
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <elfutils/libdwfl.h>
  10. static const Dwfl_Callbacks offline_callbacks = {
  11. .find_debuginfo = dwfl_standard_find_debuginfo,
  12. .section_address = dwfl_offline_section_address,
  13. .find_elf = dwfl_build_id_find_elf,
  14. };
  15. void dso__free_libdw(struct dso *dso)
  16. {
  17. Dwfl *dwfl = dso__libdw(dso);
  18. if (dwfl) {
  19. dwfl_end(dwfl);
  20. dso__set_libdw(dso, NULL);
  21. }
  22. }
  23. struct Dwfl *dso__libdw_dwfl(struct dso *dso)
  24. {
  25. Dwfl *dwfl = dso__libdw(dso);
  26. const char *dso_name;
  27. Dwfl_Module *mod;
  28. int fd;
  29. if (dwfl)
  30. return dwfl;
  31. dso_name = dso__long_name(dso);
  32. /*
  33. * Initialize Dwfl session.
  34. * We need to open the DSO file to report it to libdw.
  35. */
  36. fd = open(dso_name, O_RDONLY);
  37. if (fd < 0)
  38. return NULL;
  39. dwfl = dwfl_begin(&offline_callbacks);
  40. if (!dwfl) {
  41. close(fd);
  42. return NULL;
  43. }
  44. /*
  45. * If the report is successful, the file descriptor fd is consumed
  46. * and closed by the Dwfl. If not, it is not closed.
  47. */
  48. mod = dwfl_report_offline(dwfl, dso_name, dso_name, fd);
  49. if (!mod) {
  50. dwfl_end(dwfl);
  51. close(fd);
  52. return NULL;
  53. }
  54. dwfl_report_end(dwfl, /*removed=*/NULL, /*arg=*/NULL);
  55. dso__set_libdw(dso, dwfl);
  56. return dwfl;
  57. }
  58. struct libdw_a2l_cb_args {
  59. struct dso *dso;
  60. struct symbol *sym;
  61. struct inline_node *node;
  62. char *leaf_srcline;
  63. bool leaf_srcline_used;
  64. };
  65. static int libdw_a2l_cb(Dwarf_Die *die, void *_args)
  66. {
  67. struct libdw_a2l_cb_args *args = _args;
  68. struct symbol *inline_sym = new_inline_sym(args->dso, args->sym, dwarf_diename(die));
  69. const char *call_fname = die_get_call_file(die);
  70. char *call_srcline = srcline__unknown;
  71. struct inline_list *ilist;
  72. if (!inline_sym)
  73. return -ENOMEM;
  74. /* Assign caller information to the parent. */
  75. if (call_fname)
  76. call_srcline = srcline_from_fileline(call_fname, die_get_call_lineno(die));
  77. list_for_each_entry(ilist, &args->node->val, list) {
  78. if (args->leaf_srcline == ilist->srcline)
  79. args->leaf_srcline_used = false;
  80. else if (ilist->srcline != srcline__unknown)
  81. free(ilist->srcline);
  82. ilist->srcline = call_srcline;
  83. call_srcline = NULL;
  84. break;
  85. }
  86. if (call_srcline && call_srcline != srcline__unknown)
  87. free(call_srcline);
  88. /* Add this symbol to the chain as the leaf. */
  89. if (!args->leaf_srcline_used) {
  90. inline_list__append_tail(inline_sym, args->leaf_srcline, args->node);
  91. args->leaf_srcline_used = true;
  92. } else {
  93. inline_list__append_tail(inline_sym, strdup(args->leaf_srcline), args->node);
  94. }
  95. return 0;
  96. }
  97. int libdw__addr2line(u64 addr, char **file, unsigned int *line_nr,
  98. struct dso *dso, bool unwind_inlines,
  99. struct inline_node *node, struct symbol *sym)
  100. {
  101. Dwfl *dwfl = dso__libdw_dwfl(dso);
  102. Dwfl_Module *mod;
  103. Dwfl_Line *dwline;
  104. Dwarf_Addr bias;
  105. const char *src;
  106. int lineno = 0;
  107. if (!dwfl)
  108. return 0;
  109. mod = dwfl_addrmodule(dwfl, addr);
  110. if (!mod)
  111. return 0;
  112. /*
  113. * Get/ignore the dwarf information. Determine the bias, difference
  114. * between the regular ELF addr2line addresses and those to use with
  115. * libdw.
  116. */
  117. if (!dwfl_module_getdwarf(mod, &bias))
  118. return 0;
  119. /* Find source line information for the address. */
  120. dwline = dwfl_module_getsrc(mod, addr + bias);
  121. if (!dwline)
  122. return 0;
  123. /* Get line information. */
  124. src = dwfl_lineinfo(dwline, /*addr=*/NULL, &lineno, /*col=*/NULL, /*mtime=*/NULL,
  125. /*length=*/NULL);
  126. if (file)
  127. *file = src ? strdup(src) : NULL;
  128. if (line_nr)
  129. *line_nr = lineno;
  130. /* Optionally unwind inline function call chain. */
  131. if (unwind_inlines && node) {
  132. Dwarf_Addr unused_bias;
  133. Dwarf_Die *cudie = dwfl_module_addrdie(mod, addr + bias, &unused_bias);
  134. struct libdw_a2l_cb_args args = {
  135. .dso = dso,
  136. .sym = sym,
  137. .node = node,
  138. .leaf_srcline = srcline_from_fileline(src ?: "<unknown>", lineno),
  139. };
  140. /* Walk from the parent down to the leaf. */
  141. cu_walk_functions_at(cudie, addr, libdw_a2l_cb, &args);
  142. if (!args.leaf_srcline_used)
  143. free(args.leaf_srcline);
  144. }
  145. return 1;
  146. }