Reader.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <LibCoreDump/Reader.h>
  29. #include <signal_numbers.h>
  30. #include <string.h>
  31. #include <sys/stat.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.release_value()));
  39. }
  40. Reader::Reader(NonnullRefPtr<MappedFile> coredump_file)
  41. : m_coredump_file(move(coredump_file))
  42. , m_coredump_image(m_coredump_file->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. ASSERT(m_notes_segment_index != -1);
  54. }
  55. Reader::~Reader()
  56. {
  57. }
  58. Reader::NotesEntryIterator::NotesEntryIterator(const u8* notes_data)
  59. : m_current((const ELF::Core::NotesEntry*)notes_data)
  60. , start(notes_data)
  61. {
  62. }
  63. ELF::Core::NotesEntryHeader::Type Reader::NotesEntryIterator::type() const
  64. {
  65. ASSERT(m_current->header.type == ELF::Core::NotesEntryHeader::Type::ProcessInfo
  66. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
  67. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
  68. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Metadata
  69. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
  70. return m_current->header.type;
  71. }
  72. const ELF::Core::NotesEntry* Reader::NotesEntryIterator::current() const
  73. {
  74. return m_current;
  75. }
  76. void Reader::NotesEntryIterator::next()
  77. {
  78. ASSERT(!at_end());
  79. switch (type()) {
  80. case ELF::Core::NotesEntryHeader::Type::ProcessInfo: {
  81. const auto* current = reinterpret_cast<const ELF::Core::ProcessInfo*>(m_current);
  82. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
  83. break;
  84. }
  85. case ELF::Core::NotesEntryHeader::Type::ThreadInfo: {
  86. const auto* current = reinterpret_cast<const ELF::Core::ThreadInfo*>(m_current);
  87. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current + 1);
  88. break;
  89. }
  90. case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
  91. const auto* current = reinterpret_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
  92. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
  93. break;
  94. }
  95. case ELF::Core::NotesEntryHeader::Type::Metadata: {
  96. const auto* current = reinterpret_cast<const ELF::Core::Metadata*>(m_current);
  97. m_current = reinterpret_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
  98. break;
  99. }
  100. default:
  101. ASSERT_NOT_REACHED();
  102. }
  103. }
  104. bool Reader::NotesEntryIterator::at_end() const
  105. {
  106. return type() == ELF::Core::NotesEntryHeader::Type::Null;
  107. }
  108. Optional<uint32_t> Reader::peek_memory(FlatPtr address) const
  109. {
  110. const auto* region = region_containing(address);
  111. if (!region)
  112. return {};
  113. FlatPtr offset_in_region = address - region->region_start;
  114. const char* region_data = image().program_header(region->program_header_index).raw_data();
  115. return *(const uint32_t*)(&region_data[offset_in_region]);
  116. }
  117. const JsonObject Reader::process_info() const
  118. {
  119. const ELF::Core::ProcessInfo* process_info_notes_entry = nullptr;
  120. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  121. if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
  122. continue;
  123. process_info_notes_entry = reinterpret_cast<const ELF::Core::ProcessInfo*>(it.current());
  124. break;
  125. }
  126. if (!process_info_notes_entry)
  127. return {};
  128. auto process_info_json_value = JsonValue::from_string(process_info_notes_entry->json_data);
  129. if (!process_info_json_value.has_value())
  130. return {};
  131. if (!process_info_json_value.value().is_object())
  132. return {};
  133. return process_info_json_value.value().as_object();
  134. // FIXME: Maybe just cache this on the Reader instance after first access.
  135. }
  136. const ELF::Core::MemoryRegionInfo* Reader::region_containing(FlatPtr address) const
  137. {
  138. const ELF::Core::MemoryRegionInfo* ret = nullptr;
  139. for_each_memory_region_info([&ret, address](const ELF::Core::MemoryRegionInfo& region_info) {
  140. if (region_info.region_start <= address && region_info.region_end >= address) {
  141. ret = &region_info;
  142. return IterationDecision::Break;
  143. }
  144. return IterationDecision::Continue;
  145. });
  146. return ret;
  147. }
  148. int Reader::process_pid() const
  149. {
  150. auto process_info = this->process_info();
  151. auto pid = process_info.get("pid");
  152. return pid.to_number<int>();
  153. }
  154. u8 Reader::process_termination_signal() const
  155. {
  156. auto process_info = this->process_info();
  157. auto termination_signal = process_info.get("termination_signal");
  158. auto signal_number = termination_signal.to_number<int>();
  159. if (signal_number <= SIGINVAL || signal_number >= NSIG)
  160. return SIGINVAL;
  161. return (u8)signal_number;
  162. }
  163. String Reader::process_executable_path() const
  164. {
  165. auto process_info = this->process_info();
  166. auto executable_path = process_info.get("executable_path");
  167. return executable_path.as_string_or({});
  168. }
  169. const HashMap<String, String> Reader::metadata() const
  170. {
  171. const ELF::Core::Metadata* metadata_notes_entry = nullptr;
  172. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  173. if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
  174. continue;
  175. metadata_notes_entry = reinterpret_cast<const ELF::Core::Metadata*>(it.current());
  176. break;
  177. }
  178. if (!metadata_notes_entry)
  179. return {};
  180. auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data);
  181. if (!metadata_json_value.has_value())
  182. return {};
  183. if (!metadata_json_value.value().is_object())
  184. return {};
  185. HashMap<String, String> metadata;
  186. metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
  187. metadata.set(key, value.as_string_or({}));
  188. });
  189. return metadata;
  190. }
  191. struct LibraryData {
  192. String name;
  193. OwnPtr<MappedFile> file;
  194. ELF::Image lib_elf;
  195. };
  196. const Reader::LibraryData* Reader::library_containing(FlatPtr address) const
  197. {
  198. static HashMap<String, OwnPtr<LibraryData>> cached_libs;
  199. auto* region = region_containing(address);
  200. if (!region)
  201. return {};
  202. auto name = region->object_name();
  203. String path;
  204. if (name.contains(".so"))
  205. path = String::format("/usr/lib/%s", name.characters());
  206. else {
  207. path = name;
  208. }
  209. if (!cached_libs.contains(path)) {
  210. auto file_or_error = MappedFile::map(path);
  211. if (file_or_error.is_error())
  212. return {};
  213. auto image = ELF::Image(file_or_error.value()->bytes());
  214. cached_libs.set(path, make<LibraryData>(name, region->region_start, file_or_error.release_value(), move(image)));
  215. }
  216. auto lib_data = cached_libs.get(path).value();
  217. return lib_data;
  218. }
  219. }