DisassemblyModel.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DisassemblyModel.h"
  7. #include <AK/MappedFile.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibDebug/DebugSession.h>
  10. #include <LibELF/Image.h>
  11. #include <LibSymbolication/Symbolication.h>
  12. #include <LibX86/Disassembler.h>
  13. #include <LibX86/ELFSymbolProvider.h>
  14. #include <stdio.h>
  15. namespace HackStudio {
  16. DisassemblyModel::DisassemblyModel(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  17. {
  18. auto lib = debug_session.library_at(regs.ip());
  19. if (!lib)
  20. return;
  21. auto containing_function = lib->debug_info->get_containing_function(regs.ip() - lib->base_address);
  22. if (!containing_function.has_value()) {
  23. dbgln("Cannot disassemble as the containing function was not found.");
  24. return;
  25. }
  26. OwnPtr<ELF::Image> kernel_elf;
  27. const ELF::Image* elf = nullptr;
  28. auto maybe_kernel_base = Symbolication::kernel_base();
  29. if (maybe_kernel_base.has_value() && containing_function.value().address_low >= maybe_kernel_base.value()) {
  30. auto file_or_error = MappedFile::map("/boot/Kernel.debug");
  31. if (file_or_error.is_error())
  32. return;
  33. kernel_elf = make<ELF::Image>(file_or_error.value()->bytes());
  34. elf = kernel_elf.ptr();
  35. } else {
  36. elf = &lib->debug_info->elf();
  37. }
  38. auto symbol = elf->find_symbol(containing_function.value().address_low);
  39. if (!symbol.has_value())
  40. return;
  41. VERIFY(symbol.has_value());
  42. auto view = symbol.value().raw_data();
  43. X86::ELFSymbolProvider symbol_provider(*elf);
  44. X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
  45. X86::Disassembler disassembler(stream);
  46. size_t offset_into_symbol = 0;
  47. for (;;) {
  48. auto insn = disassembler.next();
  49. if (!insn.has_value())
  50. break;
  51. FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
  52. auto disassembly = insn.value().to_string(address_in_profiled_program, &symbol_provider);
  53. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  54. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program });
  55. offset_into_symbol += insn.value().length();
  56. }
  57. }
  58. DisassemblyModel::~DisassemblyModel()
  59. {
  60. }
  61. int DisassemblyModel::row_count(const GUI::ModelIndex&) const
  62. {
  63. return m_instructions.size();
  64. }
  65. String DisassemblyModel::column_name(int column) const
  66. {
  67. switch (column) {
  68. case Column::Address:
  69. return "Address";
  70. case Column::InstructionBytes:
  71. return "Insn Bytes";
  72. case Column::Disassembly:
  73. return "Disassembly";
  74. default:
  75. VERIFY_NOT_REACHED();
  76. return {};
  77. }
  78. }
  79. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  80. {
  81. auto& insn = m_instructions[index.row()];
  82. if (role == GUI::ModelRole::Display) {
  83. if (index.column() == Column::Address)
  84. return String::formatted("{:p}", insn.address);
  85. if (index.column() == Column::InstructionBytes) {
  86. StringBuilder builder;
  87. for (auto ch : insn.bytes)
  88. builder.appendff("{:02x} ", static_cast<unsigned char>(ch));
  89. return builder.to_string();
  90. }
  91. if (index.column() == Column::Disassembly)
  92. return insn.disassembly;
  93. return {};
  94. }
  95. return {};
  96. }
  97. }