Reader.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. namespace CoreDump {
  32. OwnPtr<Reader> Reader::create(const String& path)
  33. {
  34. auto mapped_file = make<MappedFile>(path);
  35. if (!mapped_file->is_valid())
  36. return nullptr;
  37. return make<Reader>(move(mapped_file));
  38. }
  39. Reader::Reader(OwnPtr<MappedFile>&& coredump_file)
  40. : m_coredump_file(move(coredump_file))
  41. , m_coredump_image((u8*)m_coredump_file->data(), m_coredump_file->size())
  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->executable_path + strlen(current->executable_path) + 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 ELF::Core::ProcessInfo& Reader::process_info() const
  117. {
  118. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  119. if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
  120. continue;
  121. return reinterpret_cast<const ELF::Core::ProcessInfo&>(*it.current());
  122. }
  123. ASSERT_NOT_REACHED();
  124. }
  125. const ELF::Core::MemoryRegionInfo* Reader::region_containing(FlatPtr address) const
  126. {
  127. const ELF::Core::MemoryRegionInfo* ret = nullptr;
  128. for_each_memory_region_info([&ret, address](const ELF::Core::MemoryRegionInfo& region_info) {
  129. if (region_info.region_start <= address && region_info.region_end >= address) {
  130. ret = &region_info;
  131. return IterationDecision::Break;
  132. }
  133. return IterationDecision::Continue;
  134. });
  135. return ret;
  136. }
  137. const Backtrace Reader::backtrace() const
  138. {
  139. return Backtrace(*this);
  140. }
  141. const HashMap<String, String> Reader::metadata() const
  142. {
  143. const ELF::Core::Metadata* metadata_notes_entry = nullptr;
  144. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  145. if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
  146. continue;
  147. metadata_notes_entry = reinterpret_cast<const ELF::Core::Metadata*>(it.current());
  148. break;
  149. }
  150. if (!metadata_notes_entry)
  151. return {};
  152. auto metadata_json_value = JsonValue::from_string(metadata_notes_entry->json_data);
  153. if (!metadata_json_value.has_value())
  154. return {};
  155. if (!metadata_json_value.value().is_object())
  156. return {};
  157. HashMap<String, String> metadata;
  158. metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
  159. metadata.set(key, value.as_string_or({}));
  160. });
  161. return metadata;
  162. }
  163. }