Reader.cpp 4.3 KB

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