Reader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteReader.h>
  8. #include <AK/Function.h>
  9. #include <AK/HashTable.h>
  10. #include <AK/JsonObject.h>
  11. #include <AK/JsonValue.h>
  12. #include <AK/LexicalPath.h>
  13. #include <LibCompress/Gzip.h>
  14. #include <LibCoredump/Reader.h>
  15. #include <LibFileSystem/FileSystem.h>
  16. #include <signal.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. namespace Coredump {
  20. OwnPtr<Reader> Reader::create(StringView path)
  21. {
  22. auto file_or_error = Core::MappedFile::map(path);
  23. if (file_or_error.is_error())
  24. return {};
  25. if (!Compress::GzipDecompressor::is_likely_compressed(file_or_error.value()->bytes())) {
  26. // It's an uncompressed coredump.
  27. return AK::adopt_own_if_nonnull(new (nothrow) Reader(file_or_error.release_value()));
  28. }
  29. auto decompressed_data = decompress_coredump(file_or_error.value()->bytes());
  30. if (!decompressed_data.has_value())
  31. return {};
  32. return adopt_own_if_nonnull(new (nothrow) Reader(decompressed_data.release_value()));
  33. }
  34. Reader::Reader(ByteBuffer buffer)
  35. : Reader(buffer.bytes())
  36. {
  37. m_coredump_buffer = move(buffer);
  38. }
  39. Reader::Reader(NonnullOwnPtr<Core::MappedFile> file)
  40. : Reader(file->bytes())
  41. {
  42. m_mapped_file = move(file);
  43. }
  44. Reader::Reader(ReadonlyBytes coredump_bytes)
  45. : m_coredump_bytes(coredump_bytes)
  46. , m_coredump_image(m_coredump_bytes)
  47. {
  48. size_t index = 0;
  49. m_coredump_image.for_each_program_header([this, &index](auto pheader) {
  50. if (pheader.type() == PT_NOTE) {
  51. m_notes_segment_index = index;
  52. return IterationDecision::Break;
  53. }
  54. ++index;
  55. return IterationDecision::Continue;
  56. });
  57. VERIFY(m_notes_segment_index != -1);
  58. }
  59. Optional<ByteBuffer> Reader::decompress_coredump(ReadonlyBytes raw_coredump)
  60. {
  61. auto decompressed_coredump = Compress::GzipDecompressor::decompress_all(raw_coredump);
  62. if (!decompressed_coredump.is_error())
  63. return decompressed_coredump.release_value();
  64. // If we didn't manage to decompress it, try and parse it as decompressed coredump
  65. auto bytebuffer = ByteBuffer::copy(raw_coredump);
  66. if (bytebuffer.is_error())
  67. return {};
  68. return bytebuffer.release_value();
  69. }
  70. Reader::NotesEntryIterator::NotesEntryIterator(u8 const* notes_data)
  71. : m_current(bit_cast<const ELF::Core::NotesEntry*>(notes_data))
  72. , start(notes_data)
  73. {
  74. }
  75. ELF::Core::NotesEntryHeader::Type Reader::NotesEntryIterator::type() const
  76. {
  77. VERIFY(m_current->header.type == ELF::Core::NotesEntryHeader::Type::ProcessInfo
  78. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
  79. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
  80. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Metadata
  81. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
  82. return m_current->header.type;
  83. }
  84. const ELF::Core::NotesEntry* Reader::NotesEntryIterator::current() const
  85. {
  86. return m_current;
  87. }
  88. void Reader::NotesEntryIterator::next()
  89. {
  90. VERIFY(!at_end());
  91. switch (type()) {
  92. case ELF::Core::NotesEntryHeader::Type::ProcessInfo: {
  93. auto const* current = bit_cast<const ELF::Core::ProcessInfo*>(m_current);
  94. m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
  95. break;
  96. }
  97. case ELF::Core::NotesEntryHeader::Type::ThreadInfo: {
  98. auto const* current = bit_cast<const ELF::Core::ThreadInfo*>(m_current);
  99. m_current = bit_cast<const ELF::Core::NotesEntry*>(current + 1);
  100. break;
  101. }
  102. case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
  103. auto const* current = bit_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
  104. m_current = bit_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
  105. break;
  106. }
  107. case ELF::Core::NotesEntryHeader::Type::Metadata: {
  108. auto const* current = bit_cast<const ELF::Core::Metadata*>(m_current);
  109. m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
  110. break;
  111. }
  112. default:
  113. VERIFY_NOT_REACHED();
  114. }
  115. }
  116. bool Reader::NotesEntryIterator::at_end() const
  117. {
  118. return type() == ELF::Core::NotesEntryHeader::Type::Null;
  119. }
  120. Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
  121. {
  122. auto region = region_containing(address);
  123. if (!region.has_value())
  124. return {};
  125. FlatPtr offset_in_region = address - region->region_start;
  126. auto* region_data = bit_cast<u8 const*>(image().program_header(region->program_header_index).raw_data());
  127. FlatPtr value { 0 };
  128. ByteReader::load(region_data + offset_in_region, value);
  129. return value;
  130. }
  131. const JsonObject Reader::process_info() const
  132. {
  133. const ELF::Core::ProcessInfo* process_info_notes_entry = nullptr;
  134. NotesEntryIterator it(bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data()));
  135. for (; !it.at_end(); it.next()) {
  136. if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
  137. continue;
  138. process_info_notes_entry = bit_cast<const ELF::Core::ProcessInfo*>(it.current());
  139. break;
  140. }
  141. if (!process_info_notes_entry)
  142. return {};
  143. auto const* json_data_ptr = process_info_notes_entry->json_data;
  144. auto process_info_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) });
  145. if (process_info_json_value.is_error())
  146. return {};
  147. if (!process_info_json_value.value().is_object())
  148. return {};
  149. return process_info_json_value.value().as_object();
  150. // FIXME: Maybe just cache this on the Reader instance after first access.
  151. }
  152. Optional<MemoryRegionInfo> Reader::first_region_for_object(StringView object_name) const
  153. {
  154. Optional<MemoryRegionInfo> ret;
  155. for_each_memory_region_info([&ret, &object_name](auto& region_info) {
  156. if (region_info.object_name() == object_name) {
  157. ret = region_info;
  158. return IterationDecision::Break;
  159. }
  160. return IterationDecision::Continue;
  161. });
  162. return ret;
  163. }
  164. Optional<MemoryRegionInfo> Reader::region_containing(FlatPtr address) const
  165. {
  166. Optional<MemoryRegionInfo> ret;
  167. for_each_memory_region_info([&ret, address](auto const& region_info) {
  168. if (region_info.region_start <= address && region_info.region_end >= address) {
  169. ret = region_info;
  170. return IterationDecision::Break;
  171. }
  172. return IterationDecision::Continue;
  173. });
  174. return ret;
  175. }
  176. int Reader::process_pid() const
  177. {
  178. auto process_info = this->process_info();
  179. auto pid = process_info.get_integer<int>("pid"sv).value_or(0);
  180. return pid;
  181. }
  182. u8 Reader::process_termination_signal() const
  183. {
  184. auto process_info = this->process_info();
  185. auto termination_signal = process_info.get_u8("termination_signal"sv);
  186. if (!termination_signal.has_value() || *termination_signal <= SIGINVAL || *termination_signal >= NSIG)
  187. return SIGINVAL;
  188. return *termination_signal;
  189. }
  190. DeprecatedString Reader::process_executable_path() const
  191. {
  192. auto process_info = this->process_info();
  193. auto executable_path = process_info.get_deprecated_string("executable_path"sv);
  194. return executable_path.value_or({});
  195. }
  196. Vector<DeprecatedString> Reader::process_arguments() const
  197. {
  198. auto process_info = this->process_info();
  199. auto arguments = process_info.get_array("arguments"sv);
  200. if (!arguments.has_value())
  201. return {};
  202. Vector<DeprecatedString> vector;
  203. arguments->for_each([&](auto& value) {
  204. if (value.is_string())
  205. vector.append(value.as_string());
  206. });
  207. return vector;
  208. }
  209. Vector<DeprecatedString> Reader::process_environment() const
  210. {
  211. auto process_info = this->process_info();
  212. auto environment = process_info.get_array("environment"sv);
  213. if (!environment.has_value())
  214. return {};
  215. Vector<DeprecatedString> vector;
  216. environment->for_each([&](auto& value) {
  217. if (value.is_string())
  218. vector.append(value.as_string());
  219. });
  220. return vector;
  221. }
  222. HashMap<DeprecatedString, DeprecatedString> Reader::metadata() const
  223. {
  224. const ELF::Core::Metadata* metadata_notes_entry = nullptr;
  225. NotesEntryIterator it(bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data()));
  226. for (; !it.at_end(); it.next()) {
  227. if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
  228. continue;
  229. metadata_notes_entry = bit_cast<const ELF::Core::Metadata*>(it.current());
  230. break;
  231. }
  232. if (!metadata_notes_entry)
  233. return {};
  234. auto const* json_data_ptr = metadata_notes_entry->json_data;
  235. auto metadata_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) });
  236. if (metadata_json_value.is_error())
  237. return {};
  238. if (!metadata_json_value.value().is_object())
  239. return {};
  240. HashMap<DeprecatedString, DeprecatedString> metadata;
  241. metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
  242. metadata.set(key, value.as_string_or({}));
  243. });
  244. return metadata;
  245. }
  246. Reader::LibraryData const* Reader::library_containing(FlatPtr address) const
  247. {
  248. static HashMap<DeprecatedString, OwnPtr<LibraryData>> cached_libs;
  249. auto region = region_containing(address);
  250. if (!region.has_value())
  251. return {};
  252. auto name = region->object_name();
  253. DeprecatedString path = resolve_object_path(name);
  254. if (!cached_libs.contains(path)) {
  255. auto file_or_error = Core::MappedFile::map(path);
  256. if (file_or_error.is_error())
  257. return {};
  258. auto image = ELF::Image(file_or_error.value()->bytes());
  259. cached_libs.set(path, make<LibraryData>(name, static_cast<FlatPtr>(region->region_start), file_or_error.release_value(), move(image)));
  260. }
  261. auto lib_data = cached_libs.get(path).value();
  262. return lib_data;
  263. }
  264. DeprecatedString Reader::resolve_object_path(StringView name) const
  265. {
  266. // TODO: There are other places where similar method is implemented or would be useful.
  267. // (e.g. UserspaceEmulator, LibSymbolication, Profiler, and DynamicLinker itself)
  268. // We should consider creating unified implementation in the future.
  269. if (name.starts_with('/') || !FileSystem::looks_like_shared_library(name)) {
  270. return name;
  271. }
  272. Vector<DeprecatedString> library_search_directories;
  273. // If LD_LIBRARY_PATH is present, check its folders first
  274. for (auto& environment_variable : process_environment()) {
  275. auto prefix = "LD_LIBRARY_PATH="sv;
  276. if (environment_variable.starts_with(prefix)) {
  277. auto ld_library_path = environment_variable.substring_view(prefix.length());
  278. // FIXME: This code won't handle folders with ":" in the name correctly.
  279. for (auto directory : ld_library_path.split_view(':')) {
  280. library_search_directories.append(directory);
  281. }
  282. }
  283. }
  284. // Add default paths that DynamicLinker uses
  285. library_search_directories.append("/usr/lib/"sv);
  286. library_search_directories.append("/usr/local/lib/"sv);
  287. // Search for the first readable library file
  288. for (auto& directory : library_search_directories) {
  289. auto full_path = LexicalPath::join(directory, name).string();
  290. if (access(full_path.characters(), R_OK) != 0)
  291. continue;
  292. return full_path;
  293. }
  294. return name;
  295. }
  296. void Reader::for_each_library(Function<void(LibraryInfo)> func) const
  297. {
  298. HashTable<DeprecatedString> libraries;
  299. for_each_memory_region_info([&](auto const& region) {
  300. auto name = region.object_name();
  301. if (name.is_null() || libraries.contains(name))
  302. return IterationDecision::Continue;
  303. libraries.set(name);
  304. DeprecatedString path = resolve_object_path(name);
  305. func(LibraryInfo { name, path, static_cast<FlatPtr>(region.region_start) });
  306. return IterationDecision::Continue;
  307. });
  308. }
  309. }