Reader.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/MappedFile.h>
  9. #include <AK/Noncopyable.h>
  10. #include <AK/OwnPtr.h>
  11. #include <LibELF/CoreDump.h>
  12. #include <LibELF/Image.h>
  13. namespace CoreDump {
  14. class Reader {
  15. AK_MAKE_NONCOPYABLE(Reader);
  16. AK_MAKE_NONMOVABLE(Reader);
  17. public:
  18. static OwnPtr<Reader> create(const String&);
  19. ~Reader();
  20. template<typename Func>
  21. void for_each_memory_region_info(Func func) const;
  22. template<typename Func>
  23. void for_each_thread_info(Func func) const;
  24. const ELF::Image& image() const { return m_coredump_image; }
  25. Optional<FlatPtr> peek_memory(FlatPtr address) const;
  26. const ELF::Core::MemoryRegionInfo* region_containing(FlatPtr address) const;
  27. struct LibraryData {
  28. String name;
  29. FlatPtr base_address { 0 };
  30. NonnullRefPtr<MappedFile> file;
  31. ELF::Image lib_elf;
  32. };
  33. const LibraryData* library_containing(FlatPtr address) const;
  34. int process_pid() const;
  35. u8 process_termination_signal() const;
  36. String process_executable_path() const;
  37. Vector<String> process_arguments() const;
  38. Vector<String> process_environment() const;
  39. HashMap<String, String> metadata() const;
  40. private:
  41. Reader(ReadonlyBytes);
  42. static ByteBuffer decompress_coredump(const ReadonlyBytes&);
  43. class NotesEntryIterator {
  44. public:
  45. NotesEntryIterator(const u8* notes_data);
  46. ELF::Core::NotesEntryHeader::Type type() const;
  47. const ELF::Core::NotesEntry* current() const;
  48. void next();
  49. bool at_end() const;
  50. private:
  51. const ELF::Core::NotesEntry* m_current { nullptr };
  52. const u8* start { nullptr };
  53. };
  54. // Private as we don't need anyone poking around in this JsonObject
  55. // manually - we know very well what should be included and expose that
  56. // as getters with the appropriate (non-JsonValue) types.
  57. const JsonObject process_info() const;
  58. ByteBuffer m_coredump_buffer;
  59. ELF::Image m_coredump_image;
  60. ssize_t m_notes_segment_index { -1 };
  61. };
  62. template<typename Func>
  63. void Reader::for_each_memory_region_info(Func func) const
  64. {
  65. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  66. if (it.type() != ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo)
  67. continue;
  68. auto& memory_region_info = reinterpret_cast<const ELF::Core::MemoryRegionInfo&>(*it.current());
  69. IterationDecision decision = func(memory_region_info);
  70. if (decision == IterationDecision::Break)
  71. return;
  72. }
  73. }
  74. template<typename Func>
  75. void Reader::for_each_thread_info(Func func) const
  76. {
  77. for (NotesEntryIterator it((const u8*)m_coredump_image.program_header(m_notes_segment_index).raw_data()); !it.at_end(); it.next()) {
  78. if (it.type() != ELF::Core::NotesEntryHeader::Type::ThreadInfo)
  79. continue;
  80. auto& thread_info = reinterpret_cast<const ELF::Core::ThreadInfo&>(*it.current());
  81. IterationDecision decision = func(thread_info);
  82. if (decision == IterationDecision::Break)
  83. return;
  84. }
  85. }
  86. }