DisassemblyModel.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2020, Luke Wilde <luke.wilde@live.co.uk>
  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 "DisassemblyModel.h"
  27. #include <AK/MappedFile.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibDebug/DebugSession.h>
  30. #include <LibELF/Loader.h>
  31. #include <LibX86/Disassembler.h>
  32. #include <LibX86/ELFSymbolProvider.h>
  33. #include <ctype.h>
  34. #include <stdio.h>
  35. namespace HackStudio {
  36. DisassemblyModel::DisassemblyModel(const Debug::DebugSession& debug_session, const PtraceRegisters& regs)
  37. {
  38. auto containing_function = debug_session.debug_info().get_containing_function(regs.eip);
  39. if (!containing_function.has_value()) {
  40. dbgln("Cannot disassemble as the containing function was not found.");
  41. return;
  42. }
  43. RefPtr<ELF::Loader> elf_loader;
  44. if (containing_function.value().address_low >= 0xc0000000) {
  45. auto kernel_file = make<MappedFile>("/boot/Kernel");
  46. if (!kernel_file->is_valid())
  47. return;
  48. elf_loader = ELF::Loader::create((const u8*)kernel_file->data(), kernel_file->size());
  49. } else {
  50. elf_loader = debug_session.elf();
  51. }
  52. auto symbol = elf_loader->find_symbol(containing_function.value().address_low);
  53. if (!symbol.has_value())
  54. return;
  55. ASSERT(symbol.has_value());
  56. auto view = symbol.value().raw_data();
  57. X86::ELFSymbolProvider symbol_provider(*elf_loader);
  58. X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
  59. X86::Disassembler disassembler(stream);
  60. size_t offset_into_symbol = 0;
  61. for (;;) {
  62. auto insn = disassembler.next();
  63. if (!insn.has_value())
  64. break;
  65. FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
  66. auto disassembly = insn.value().to_string(address_in_profiled_program, &symbol_provider);
  67. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  68. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program });
  69. offset_into_symbol += insn.value().length();
  70. }
  71. }
  72. DisassemblyModel::~DisassemblyModel()
  73. {
  74. }
  75. int DisassemblyModel::row_count(const GUI::ModelIndex&) const
  76. {
  77. return m_instructions.size();
  78. }
  79. String DisassemblyModel::column_name(int column) const
  80. {
  81. switch (column) {
  82. case Column::Address:
  83. return "Address";
  84. case Column::InstructionBytes:
  85. return "Insn Bytes";
  86. case Column::Disassembly:
  87. return "Disassembly";
  88. default:
  89. ASSERT_NOT_REACHED();
  90. return {};
  91. }
  92. }
  93. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  94. {
  95. auto& insn = m_instructions[index.row()];
  96. if (role == GUI::ModelRole::Display) {
  97. if (index.column() == Column::Address)
  98. return String::formatted("{:p}", insn.address);
  99. if (index.column() == Column::InstructionBytes) {
  100. StringBuilder builder;
  101. for (auto ch : insn.bytes)
  102. builder.appendff("{:02x} ", static_cast<unsigned char>(ch));
  103. return builder.to_string();
  104. }
  105. if (index.column() == Column::Disassembly)
  106. return insn.disassembly;
  107. return {};
  108. }
  109. return {};
  110. }
  111. void DisassemblyModel::update()
  112. {
  113. did_update();
  114. }
  115. }