Reader.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonObject.h>
  27. #include <AK/JsonValue.h>
  28. #include <LibCompress/Gzip.h>
  29. #include <LibCoreDump/Reader.h>
  30. #include <signal_numbers.h>
  31. #include <string.h>
  32. namespace CoreDump {
  33. OwnPtr<Reader> Reader::create(const String& path)
  34. {
  35. auto file_or_error = MappedFile::map(path);
  36. if (file_or_error.is_error())
  37. return {};
  38. return adopt_own(*new Reader(file_or_error.value()->bytes()));
  39. }
  40. Reader::Reader(ReadonlyBytes coredump_bytes)
  41. : m_coredump_buffer(decompress_coredump(coredump_bytes))
  42. , m_coredump_image(m_coredump_buffer.bytes())
  43. {
  44. size_t index = 0;
  45. m_coredump_image.for_each_program_header([this, &index](auto pheader) {
  46. if (pheader.type() == PT_NOTE) {
  47. m_notes_segment_index = index;
  48. return IterationDecision::Break;
  49. }
  50. ++index;
  51. return IterationDecision::Continue;
  52. });
  53. VERIFY(m_notes_segment_index != -1);
  54. }
  55. ByteBuffer Reader::decompress_coredump(const ReadonlyBytes& raw_coredump)
  56. {
  57. if (!Compress::GzipDecompressor::is_likely_compressed(raw_coredump))
  58. return ByteBuffer::copy(raw_coredump); // handle old format core dumps (uncompressed)
  59. auto decompressed_coredump = Compress::GzipDecompressor::decompress_all(raw_coredump);
  60. if (!decompressed_coredump.has_value())
  61. return ByteBuffer::copy(raw_coredump); // if we didn't manage to decompress it, try and parse it as decompressed core dump
  62. return decompressed_coredump.value();
  63. }
  64. Reader::~Reader()
  65. {
  66. }
  67. Reader::NotesEntryIterator::NotesEntryIterator(const u8* notes_data)
  68. : m_current((const ELF::Core::NotesEntry*)notes_data)
  69. , start(notes_data)
  70. {
  71. }
  72. ELF::Core::NotesEntryHeader::Type Reader::NotesEntryIterator::type() const
  73. {
  74. VERIFY(m_current->header.type == ELF::Core::NotesEntryHeader::Type::ProcessInfo
  75. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
  76. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
  77. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Metadata
  78. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
  79. return m_current->header.type;
  80. }
  81. const ELF::Core::NotesEntry* Reader::NotesEntryIterator::current() const
  82. {
  83. return m_current;
  84. }
  85. void Reader::NotesEntryIterator::next()
  86. {
  87. VERIFY(!at_end());
  88. switch (type()) {
  89. case ELF::Core::NotesEntryHeader::Type::ProcessInfo: {
  90. const auto* current = reinterpret_cast<const ELF::Core::ProcessInfo*>(m_current);
  91. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
  92. break;
  93. }
  94. case ELF::Core::NotesEntryHeader::Type::ThreadInfo: {
  95. const auto* current = reinterpret_cast<const ELF::Core::ThreadInfo*>(m_current);
  96. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current + 1);
  97. break;
  98. }
  99. case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
  100. const auto* current = reinterpret_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
  101. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
  102. break;
  103. }
  104. case ELF::Core::NotesEntryHeader::Type::Metadata: {
  105. const auto* current = reinterpret_cast<const ELF::Core::Metadata*>(m_current);
  106. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
  107. break;
  108. }
  109. default:
  110. VERIFY_NOT_REACHED();
  111. }
  112. }
  113. bool Reader::NotesEntryIterator::at_end() const
  114. {
  115. return type() == ELF::Core::NotesEntryHeader::Type::Null;
  116. }
  117. Optional<uint32_t> Reader::peek_memory(FlatPtr address) const
  118. {
  119. const auto* region = region_containing(address);
  120. if (!region)
  121. return {};
  122. FlatPtr offset_in_region = address - region->region_start;
  123. const char* region_data = image().program_header(region->program_header_index).raw_data();
  124. return *(const uint32_t*)(&region_data[offset_in_region]);
  125. }
  126. const JsonObject Reader::process_info() const
  127. {
  128. const ELF::Core::ProcessInfo* process_info_notes_entry = nullptr;
  129. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  130. if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
  131. continue;
  132. process_info_notes_entry = reinterpret_cast<const ELF::Core::ProcessInfo*>(it.current());
  133. break;
  134. }
  135. if (!process_info_notes_entry)
  136. return {};
  137. auto process_info_json_value = JsonValue::from_string(process_info_notes_entry->json_data);
  138. if (!process_info_json_value.has_value())
  139. return {};
  140. if (!process_info_json_value.value().is_object())
  141. return {};
  142. return process_info_json_value.value().as_object();
  143. // FIXME: Maybe just cache this on the Reader instance after first access.
  144. }
  145. const ELF::Core::MemoryRegionInfo* Reader::region_containing(FlatPtr address) const
  146. {
  147. const ELF::Core::MemoryRegionInfo* ret = nullptr;
  148. for_each_memory_region_info([&ret, address](const ELF::Core::MemoryRegionInfo& region_info) {
  149. if (region_info.region_start <= address && region_info.region_end >= address) {
  150. ret = &region_info;
  151. return IterationDecision::Break;
  152. }
  153. return IterationDecision::Continue;
  154. });
  155. return ret;
  156. }
  157. int Reader::process_pid() const
  158. {
  159. auto process_info = this->process_info();
  160. auto pid = process_info.get("pid");
  161. return pid.to_number<int>();
  162. }
  163. u8 Reader::process_termination_signal() const
  164. {
  165. auto process_info = this->process_info();
  166. auto termination_signal = process_info.get("termination_signal");
  167. auto signal_number = termination_signal.to_number<int>();
  168. if (signal_number <= SIGINVAL || signal_number >= NSIG)
  169. return SIGINVAL;
  170. return (u8)signal_number;
  171. }
  172. String Reader::process_executable_path() const
  173. {
  174. auto process_info = this->process_info();
  175. auto executable_path = process_info.get("executable_path");
  176. return executable_path.as_string_or({});
  177. }
  178. Vector<String> Reader::process_arguments() const
  179. {
  180. auto process_info = this->process_info();
  181. auto arguments = process_info.get("arguments");
  182. if (!arguments.is_array())
  183. return {};
  184. Vector<String> vector;
  185. arguments.as_array().for_each([&](auto& value) {
  186. if (value.is_string())
  187. vector.append(value.as_string());
  188. });
  189. return vector;
  190. }
  191. Vector<String> Reader::process_environment() const
  192. {
  193. auto process_info = this->process_info();
  194. auto environment = process_info.get("environment");
  195. if (!environment.is_array())
  196. return {};
  197. Vector<String> vector;
  198. environment.as_array().for_each([&](auto& value) {
  199. if (value.is_string())
  200. vector.append(value.as_string());
  201. });
  202. return vector;
  203. }
  204. HashMap<String, String> Reader::metadata() const
  205. {
  206. const ELF::Core::Metadata* metadata_notes_entry = nullptr;
  207. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  208. if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
  209. continue;
  210. metadata_notes_entry = reinterpret_cast<const ELF::Core::Metadata*>(it.current());
  211. break;
  212. }
  213. if (!metadata_notes_entry)
  214. return {};
  215. auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data);
  216. if (!metadata_json_value.has_value())
  217. return {};
  218. if (!metadata_json_value.value().is_object())
  219. return {};
  220. HashMap<String, String> metadata;
  221. metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
  222. metadata.set(key, value.as_string_or({}));
  223. });
  224. return metadata;
  225. }
  226. struct LibraryData {
  227. String name;
  228. OwnPtr<MappedFile> file;
  229. ELF::Image lib_elf;
  230. };
  231. const Reader::LibraryData* Reader::library_containing(FlatPtr address) const
  232. {
  233. static HashMap<String, OwnPtr<LibraryData>> cached_libs;
  234. auto* region = region_containing(address);
  235. if (!region)
  236. return {};
  237. auto name = region->object_name();
  238. String path;
  239. if (name.contains(".so"))
  240. path = String::formatted("/usr/lib/{}", name);
  241. else {
  242. path = name;
  243. }
  244. if (!cached_libs.contains(path)) {
  245. auto file_or_error = MappedFile::map(path);
  246. if (file_or_error.is_error())
  247. return {};
  248. auto image = ELF::Image(file_or_error.value()->bytes());
  249. cached_libs.set(path, make<LibraryData>(name, region->region_start, file_or_error.release_value(), move(image)));
  250. }
  251. auto lib_data = cached_libs.get(path).value();
  252. return lib_data;
  253. }
  254. }