Inspector.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Inspector.h"
  7. namespace Coredump {
  8. OwnPtr<Inspector> Inspector::create(StringView coredump_path, Function<void(float)> on_progress)
  9. {
  10. auto reader = Reader::create(coredump_path);
  11. if (!reader)
  12. return {};
  13. return AK::adopt_own_if_nonnull(new (nothrow) Inspector(reader.release_nonnull(), move(on_progress)));
  14. }
  15. Inspector::Inspector(NonnullOwnPtr<Reader>&& reader, Function<void(float)> on_progress)
  16. : m_reader(move(reader))
  17. {
  18. parse_loaded_libraries(move(on_progress));
  19. }
  20. size_t Inspector::number_of_libraries_in_coredump() const
  21. {
  22. size_t count = 0;
  23. m_reader->for_each_library([&count](Coredump::Reader::LibraryInfo) {
  24. ++count;
  25. });
  26. return count;
  27. }
  28. void Inspector::parse_loaded_libraries(Function<void(float)> on_progress)
  29. {
  30. size_t number_of_libraries = number_of_libraries_in_coredump();
  31. size_t library_index = 0;
  32. m_reader->for_each_library([this, number_of_libraries, &library_index, &on_progress](Coredump::Reader::LibraryInfo library) {
  33. ++library_index;
  34. if (on_progress)
  35. on_progress(library_index / static_cast<float>(number_of_libraries));
  36. auto file_or_error = Core::MappedFile::map(library.path);
  37. if (file_or_error.is_error())
  38. return;
  39. auto image = make<ELF::Image>(file_or_error.value()->bytes());
  40. auto debug_info = make<Debug::DebugInfo>(*image, ByteString {}, library.base_address);
  41. m_loaded_libraries.append(make<Debug::LoadedLibrary>(library.name, file_or_error.release_value(), move(image), move(debug_info), library.base_address));
  42. });
  43. }
  44. bool Inspector::poke(FlatPtr, FlatPtr) { return false; }
  45. Optional<FlatPtr> Inspector::peek(FlatPtr address) const
  46. {
  47. return m_reader->peek_memory(address);
  48. }
  49. PtraceRegisters Inspector::get_registers() const
  50. {
  51. PtraceRegisters registers {};
  52. m_reader->for_each_thread_info([&](ELF::Core::ThreadInfo const& thread_info) {
  53. registers = thread_info.regs;
  54. return IterationDecision::Break;
  55. });
  56. return registers;
  57. }
  58. void Inspector::set_registers(PtraceRegisters const&) {};
  59. void Inspector::for_each_loaded_library(Function<IterationDecision(Debug::LoadedLibrary const&)> func) const
  60. {
  61. for (auto& library : m_loaded_libraries) {
  62. if (func(*library) == IterationDecision::Break)
  63. break;
  64. }
  65. }
  66. }