Reader.cpp 9.4 KB

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