Reader.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/MappedFile.h>
  29. #include <AK/Noncopyable.h>
  30. #include <AK/OwnPtr.h>
  31. #include <LibCoreDump/Forward.h>
  32. #include <LibELF/CoreDump.h>
  33. #include <LibELF/Image.h>
  34. namespace CoreDump {
  35. class Reader {
  36. AK_MAKE_NONCOPYABLE(Reader);
  37. public:
  38. static OwnPtr<Reader> create(const String&);
  39. ~Reader();
  40. Reader(OwnPtr<MappedFile>&&);
  41. const ELF::Core::ProcessInfo& process_info() const;
  42. template<typename Func>
  43. void for_each_memory_region_info(Func func) const;
  44. template<typename Func>
  45. void for_each_thread_info(Func func) const;
  46. const ELF::Image& image() const { return m_coredump_image; }
  47. Optional<uint32_t> peek_memory(FlatPtr address) const;
  48. const ELF::Core::MemoryRegionInfo* region_containing(FlatPtr address) const;
  49. const Backtrace backtrace() const;
  50. const HashMap<String, String> metadata() const;
  51. private:
  52. class NotesEntryIterator {
  53. public:
  54. NotesEntryIterator(const u8* notes_data);
  55. ELF::Core::NotesEntryHeader::Type type() const;
  56. const ELF::Core::NotesEntry* current() const;
  57. void next();
  58. bool at_end() const;
  59. private:
  60. const ELF::Core::NotesEntry* m_current { nullptr };
  61. const u8* start { nullptr };
  62. };
  63. OwnPtr<MappedFile> m_coredump_file;
  64. ELF::Image m_coredump_image;
  65. ssize_t m_notes_segment_index { -1 };
  66. };
  67. template<typename Func>
  68. void Reader::for_each_memory_region_info(Func func) const
  69. {
  70. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  71. if (it.type() != ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo)
  72. continue;
  73. auto& memory_region_info = reinterpret_cast<const ELF::Core::MemoryRegionInfo&>(*it.current());
  74. IterationDecision decision = func(memory_region_info);
  75. if (decision == IterationDecision::Break)
  76. return;
  77. }
  78. }
  79. template<typename Func>
  80. void Reader::for_each_thread_info(Func func) const
  81. {
  82. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  83. if (it.type() != ELF::Core::NotesEntryHeader::Type::ThreadInfo)
  84. continue;
  85. auto& thread_info = reinterpret_cast<const ELF::Core::ThreadInfo&>(*it.current());
  86. IterationDecision decision = func(thread_info);
  87. if (decision == IterationDecision::Break)
  88. return;
  89. }
  90. }
  91. }