Symbolication.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/Checked.h>
  8. #include <AK/JsonArray.h>
  9. #include <AK/JsonObject.h>
  10. #include <AK/JsonValue.h>
  11. #include <AK/LexicalPath.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/MappedFile.h>
  14. #include <LibDebug/DebugInfo.h>
  15. #include <LibSymbolication/Symbolication.h>
  16. namespace Symbolication {
  17. struct CachedELF {
  18. NonnullRefPtr<Core::MappedFile> mapped_file;
  19. NonnullOwnPtr<Debug::DebugInfo> debug_info;
  20. NonnullOwnPtr<ELF::Image> image;
  21. };
  22. static HashMap<String, OwnPtr<CachedELF>> s_cache;
  23. enum class KernelBaseState {
  24. Uninitialized,
  25. Valid,
  26. Invalid,
  27. };
  28. static FlatPtr s_kernel_base;
  29. static KernelBaseState s_kernel_base_state = KernelBaseState::Uninitialized;
  30. Optional<FlatPtr> kernel_base()
  31. {
  32. if (s_kernel_base_state == KernelBaseState::Uninitialized) {
  33. auto file = Core::File::open("/proc/kernel_base", Core::OpenMode::ReadOnly);
  34. if (file.is_error()) {
  35. s_kernel_base_state = KernelBaseState::Invalid;
  36. return {};
  37. }
  38. auto kernel_base_str = String { file.value()->read_all(), NoChomp };
  39. #if ARCH(I386)
  40. using AddressType = u32;
  41. #else
  42. using AddressType = u64;
  43. #endif
  44. auto maybe_kernel_base = kernel_base_str.to_uint<AddressType>();
  45. if (!maybe_kernel_base.has_value()) {
  46. s_kernel_base_state = KernelBaseState::Invalid;
  47. return {};
  48. }
  49. s_kernel_base = maybe_kernel_base.value();
  50. s_kernel_base_state = KernelBaseState::Valid;
  51. }
  52. if (s_kernel_base_state == KernelBaseState::Invalid)
  53. return {};
  54. return s_kernel_base;
  55. }
  56. Optional<Symbol> symbolicate(String const& path, FlatPtr address, IncludeSourcePosition include_source_positions)
  57. {
  58. String full_path = path;
  59. if (!path.starts_with('/')) {
  60. Array<StringView, 2> search_paths { "/usr/lib"sv, "/usr/local/lib"sv };
  61. bool found = false;
  62. for (auto& search_path : search_paths) {
  63. full_path = LexicalPath::join(search_path, path).string();
  64. if (Core::File::exists(full_path)) {
  65. found = true;
  66. break;
  67. }
  68. }
  69. if (!found) {
  70. dbgln("Failed to find candidate for {}", path);
  71. s_cache.set(path, {});
  72. return {};
  73. }
  74. }
  75. if (!s_cache.contains(full_path)) {
  76. auto mapped_file = Core::MappedFile::map(full_path);
  77. if (mapped_file.is_error()) {
  78. dbgln("Failed to map {}: {}", full_path, mapped_file.error());
  79. s_cache.set(full_path, {});
  80. return {};
  81. }
  82. auto elf = make<ELF::Image>(mapped_file.value()->bytes());
  83. if (!elf->is_valid()) {
  84. dbgln("ELF not valid: {}", full_path);
  85. s_cache.set(full_path, {});
  86. return {};
  87. }
  88. auto cached_elf = make<CachedELF>(mapped_file.release_value(), make<Debug::DebugInfo>(*elf), move(elf));
  89. s_cache.set(full_path, move(cached_elf));
  90. }
  91. auto it = s_cache.find(full_path);
  92. VERIFY(it != s_cache.end());
  93. auto& cached_elf = it->value;
  94. if (!cached_elf)
  95. return {};
  96. u32 offset = 0;
  97. auto symbol = cached_elf->debug_info->elf().symbolicate(address, &offset);
  98. Vector<Debug::DebugInfo::SourcePosition> positions;
  99. if (include_source_positions == IncludeSourcePosition::Yes) {
  100. auto source_position_with_inlines = cached_elf->debug_info->get_source_position_with_inlines(address);
  101. for (auto& position : source_position_with_inlines.inline_chain) {
  102. if (!positions.contains_slow(position))
  103. positions.append(position);
  104. }
  105. if (source_position_with_inlines.source_position.has_value() && !positions.contains_slow(source_position_with_inlines.source_position.value())) {
  106. positions.insert(0, source_position_with_inlines.source_position.value());
  107. }
  108. }
  109. return Symbol {
  110. .address = address,
  111. .name = move(symbol),
  112. .object = LexicalPath::basename(path),
  113. .offset = offset,
  114. .source_positions = move(positions),
  115. };
  116. }
  117. Vector<Symbol> symbolicate_thread(pid_t pid, pid_t tid, IncludeSourcePosition include_source_positions)
  118. {
  119. struct RegionWithSymbols {
  120. FlatPtr base { 0 };
  121. size_t size { 0 };
  122. String path;
  123. };
  124. Vector<FlatPtr> stack;
  125. Vector<RegionWithSymbols> regions;
  126. if (auto maybe_kernel_base = kernel_base(); maybe_kernel_base.has_value()) {
  127. regions.append(RegionWithSymbols {
  128. .base = maybe_kernel_base.value(),
  129. .size = 0x3fffffff,
  130. .path = "/boot/Kernel.debug",
  131. });
  132. }
  133. {
  134. auto stack_path = String::formatted("/proc/{}/stacks/{}", pid, tid);
  135. auto file_or_error = Core::File::open(stack_path, Core::OpenMode::ReadOnly);
  136. if (file_or_error.is_error()) {
  137. warnln("Could not open {}: {}", stack_path, file_or_error.error());
  138. return {};
  139. }
  140. auto json = JsonValue::from_string(file_or_error.value()->read_all());
  141. if (json.is_error() || !json.value().is_array()) {
  142. warnln("Invalid contents in {}", stack_path);
  143. return {};
  144. }
  145. stack.ensure_capacity(json.value().as_array().size());
  146. for (auto& value : json.value().as_array().values()) {
  147. stack.append(value.to_addr());
  148. }
  149. }
  150. {
  151. auto vm_path = String::formatted("/proc/{}/vm", pid);
  152. auto file_or_error = Core::File::open(vm_path, Core::OpenMode::ReadOnly);
  153. if (file_or_error.is_error()) {
  154. warnln("Could not open {}: {}", vm_path, file_or_error.error());
  155. return {};
  156. }
  157. auto json = JsonValue::from_string(file_or_error.value()->read_all());
  158. if (json.is_error() || !json.value().is_array()) {
  159. warnln("Invalid contents in {}", vm_path);
  160. return {};
  161. }
  162. for (auto& region_value : json.value().as_array().values()) {
  163. auto& region = region_value.as_object();
  164. auto name = region.get("name").to_string();
  165. auto address = region.get("address").to_addr();
  166. auto size = region.get("size").to_addr();
  167. String path;
  168. if (name == "/usr/lib/Loader.so") {
  169. path = name;
  170. } else if (name.ends_with(": .text") || name.ends_with(": .rodata")) {
  171. auto parts = name.split_view(':');
  172. path = parts[0];
  173. } else {
  174. continue;
  175. }
  176. RegionWithSymbols r;
  177. r.base = address;
  178. r.size = size;
  179. r.path = path;
  180. regions.append(move(r));
  181. }
  182. }
  183. Vector<Symbol> symbols;
  184. bool first_frame = true;
  185. for (auto address : stack) {
  186. RegionWithSymbols const* found_region = nullptr;
  187. for (auto& region : regions) {
  188. FlatPtr region_end;
  189. if (Checked<FlatPtr>::addition_would_overflow(region.base, region.size))
  190. region_end = NumericLimits<FlatPtr>::max();
  191. else
  192. region_end = region.base + region.size;
  193. if (address >= region.base && address < region_end) {
  194. found_region = &region;
  195. break;
  196. }
  197. }
  198. if (!found_region) {
  199. outln("{:p} ??", address);
  200. continue;
  201. }
  202. // We found an address inside of a region, but the base of that region
  203. // may not be the base of the ELF image. For example, there could be an
  204. // .rodata mapping at a lower address than the first .text mapping from
  205. // the same image. look for the lowest address region with the same path.
  206. RegionWithSymbols const* base_region = nullptr;
  207. for (auto& region : regions) {
  208. if (region.path != found_region->path)
  209. continue;
  210. if (!base_region || region.base <= base_region->base)
  211. base_region = &region;
  212. }
  213. FlatPtr adjusted_address = address - base_region->base;
  214. // We're subtracting 1 from the address because this is the return address,
  215. // i.e. it is one instruction past the call instruction.
  216. // However, because the first frame represents the current
  217. // instruction pointer rather than the return address we don't
  218. // subtract 1 for that.
  219. auto result = symbolicate(found_region->path, adjusted_address - (first_frame ? 0 : 1), include_source_positions);
  220. first_frame = false;
  221. if (!result.has_value()) {
  222. symbols.append(Symbol {
  223. .address = address,
  224. .source_positions = {},
  225. });
  226. continue;
  227. }
  228. symbols.append(result.value());
  229. }
  230. return symbols;
  231. }
  232. }