CoreDumpReader.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "CoreDumpReader.h"
  27. #include <string.h>
  28. OwnPtr<CoreDumpReader> CoreDumpReader::create(const String& path)
  29. {
  30. auto mapped_file = make<MappedFile>(path);
  31. if (!mapped_file->is_valid())
  32. return nullptr;
  33. return make<CoreDumpReader>(move(mapped_file));
  34. }
  35. CoreDumpReader::CoreDumpReader(OwnPtr<MappedFile>&& coredump_file)
  36. : m_coredump_file(move(coredump_file))
  37. , m_coredump_image((u8*)m_coredump_file->data(), m_coredump_file->size())
  38. {
  39. size_t index = 0;
  40. m_coredump_image.for_each_program_header([this, &index](auto pheader) {
  41. if (pheader.type() == PT_NOTE) {
  42. m_notes_segment_index = index;
  43. return IterationDecision::Break;
  44. }
  45. ++index;
  46. return IterationDecision::Continue;
  47. });
  48. ASSERT(m_notes_segment_index != -1);
  49. }
  50. CoreDumpReader::~CoreDumpReader()
  51. {
  52. }
  53. CoreDumpReader::NotesEntryIterator::NotesEntryIterator(const u8* notes_data)
  54. : m_current((const ELF::Core::NotesEntry*)notes_data)
  55. , start(notes_data)
  56. {
  57. }
  58. ELF::Core::NotesEntryHeader::Type CoreDumpReader::NotesEntryIterator::type() const
  59. {
  60. ASSERT(m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
  61. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
  62. || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
  63. return m_current->header.type;
  64. }
  65. const ELF::Core::NotesEntry* CoreDumpReader::NotesEntryIterator::current() const
  66. {
  67. return m_current;
  68. }
  69. void CoreDumpReader::NotesEntryIterator::next()
  70. {
  71. ASSERT(!at_end());
  72. if (type() == ELF::Core::NotesEntryHeader::Type::ThreadInfo) {
  73. const ELF::Core::ThreadInfo* current = (const ELF::Core::ThreadInfo*)m_current;
  74. m_current = (const ELF::Core::NotesEntry*)(current + 1);
  75. return;
  76. }
  77. if (type() == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo) {
  78. const ELF::Core::MemoryRegionInfo* current = (const ELF::Core::MemoryRegionInfo*)m_current;
  79. m_current = (const ELF::Core::NotesEntry*)(current->region_name + strlen(current->region_name) + 1);
  80. return;
  81. }
  82. }
  83. bool CoreDumpReader::NotesEntryIterator::at_end() const
  84. {
  85. return type() == ELF::Core::NotesEntryHeader::Type::Null;
  86. }
  87. Optional<uint32_t> CoreDumpReader::peek_memory(FlatPtr address) const
  88. {
  89. const auto* region = region_containing(address);
  90. if (!region)
  91. return {};
  92. FlatPtr offset_in_region = address - region->region_start;
  93. const char* region_data = image().program_header(region->program_header_index).raw_data();
  94. return *(const uint32_t*)(&region_data[offset_in_region]);
  95. }
  96. const ELF::Core::MemoryRegionInfo* CoreDumpReader::region_containing(FlatPtr address) const
  97. {
  98. const ELF::Core::MemoryRegionInfo* ret = nullptr;
  99. for_each_memory_region_info([&ret, address](const ELF::Core::MemoryRegionInfo* region_info) {
  100. if (region_info->region_start <= address && region_info->region_end >= address) {
  101. ret = region_info;
  102. return IterationDecision::Break;
  103. }
  104. return IterationDecision::Continue;
  105. });
  106. return ret;
  107. }