Reader.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/Backtrace.h>
  29. #include <LibCoreDump/Reader.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 nullptr;
  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->executable_path + strlen(current->executable_path) + 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 ELF::Core::ProcessInfo& Reader::process_info() const
  118. {
  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. return reinterpret_cast<const ELF::Core::ProcessInfo&>(*it.current());
  123. }
  124. ASSERT_NOT_REACHED();
  125. }
  126. const ELF::Core::MemoryRegionInfo* Reader::region_containing(FlatPtr address) const
  127. {
  128. const ELF::Core::MemoryRegionInfo* ret = nullptr;
  129. for_each_memory_region_info([&ret, address](const ELF::Core::MemoryRegionInfo& region_info) {
  130. if (region_info.region_start <= address && region_info.region_end >= address) {
  131. ret = &region_info;
  132. return IterationDecision::Break;
  133. }
  134. return IterationDecision::Continue;
  135. });
  136. return ret;
  137. }
  138. const Backtrace Reader::backtrace() const
  139. {
  140. return Backtrace(*this);
  141. }
  142. const HashMap<String, String> Reader::metadata() const
  143. {
  144. const ELF::Core::Metadata* metadata_notes_entry = nullptr;
  145. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  146. if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
  147. continue;
  148. metadata_notes_entry = reinterpret_cast<const ELF::Core::Metadata*>(it.current());
  149. break;
  150. }
  151. if (!metadata_notes_entry)
  152. return {};
  153. auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data);
  154. if (!metadata_json_value.has_value())
  155. return {};
  156. if (!metadata_json_value.value().is_object())
  157. return {};
  158. HashMap<String, String> metadata;
  159. metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
  160. metadata.set(key, value.as_string_or({}));
  161. });
  162. return metadata;
  163. }
  164. struct LibraryData {
  165. String name;
  166. OwnPtr<MappedFile> file;
  167. ELF::Image lib_elf;
  168. };
  169. const Reader::LibraryData* Reader::library_containing(FlatPtr address) const
  170. {
  171. static HashMap<String, OwnPtr<LibraryData>> cached_libs;
  172. auto* region = region_containing(address);
  173. if (!region)
  174. return {};
  175. auto name = region->object_name();
  176. String path;
  177. if (name.contains(".so"))
  178. path = String::format("/usr/lib/%s", name.characters());
  179. else {
  180. path = name;
  181. }
  182. if (!cached_libs.contains(path)) {
  183. auto file_or_error = MappedFile::map(path);
  184. if (file_or_error.is_error())
  185. return {};
  186. auto image = ELF::Image(file_or_error.value()->bytes());
  187. cached_libs.set(path, make<LibraryData>(name, region->region_start, file_or_error.release_value(), move(image)));
  188. }
  189. auto lib_data = cached_libs.get(path).value();
  190. return lib_data;
  191. }
  192. }