Symbolication.cpp 8.8 KB

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