DisassemblyModel.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 "Profile.h"
  28. #include <AK/MappedFile.h>
  29. #include <LibELF/ELFLoader.h>
  30. #include <LibX86/Disassembler.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
  34. : m_profile(profile)
  35. , m_node(node)
  36. {
  37. m_file = make<MappedFile>(profile.executable_path());
  38. auto elf_loader = make<ELFLoader>((const u8*)m_file->data(), m_file->size());
  39. auto symbol = elf_loader->find_symbol(node.address());
  40. ASSERT(symbol.has_value());
  41. auto view = symbol.value().raw_data();
  42. X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
  43. X86::Disassembler disassembler(stream);
  44. dbg() << "Disassembly for " << node.symbol() << " @ " << (const void*)node.address();
  45. size_t offset_into_symbol = 0;
  46. for (;;) {
  47. auto insn = disassembler.next();
  48. if (!insn.has_value())
  49. break;
  50. FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
  51. auto disassembly = insn.value().to_string(address_in_profiled_program);
  52. dbg() << disassembly;
  53. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  54. size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
  55. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction });
  56. offset_into_symbol += insn.value().length();
  57. }
  58. }
  59. DisassemblyModel::~DisassemblyModel()
  60. {
  61. }
  62. int DisassemblyModel::row_count(const GUI::ModelIndex&) const
  63. {
  64. return m_instructions.size();
  65. }
  66. String DisassemblyModel::column_name(int column) const
  67. {
  68. switch (column) {
  69. case Column::SampleCount:
  70. return m_profile.show_percentages() ? "% Samples" : "# Samples";
  71. case Column::Address:
  72. return "Address";
  73. case Column::InstructionBytes:
  74. return "Insn Bytes";
  75. case Column::Disassembly:
  76. return "Disassembly";
  77. default:
  78. ASSERT_NOT_REACHED();
  79. return {};
  80. }
  81. }
  82. GUI::Model::ColumnMetadata DisassemblyModel::column_metadata(int column) const
  83. {
  84. if (column == Column::SampleCount)
  85. return ColumnMetadata { 0, Gfx::TextAlignment::CenterRight };
  86. return {};
  87. }
  88. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const
  89. {
  90. auto& insn = m_instructions[index.row()];
  91. if (role == Role::BackgroundColor) {
  92. if (insn.event_count > 0)
  93. return Color(Color::Yellow);
  94. return {};
  95. }
  96. if (role == Role::Display) {
  97. if (index.column() == Column::SampleCount) {
  98. if (m_profile.show_percentages())
  99. return ((float)insn.event_count / (float)m_profile.filtered_event_count()) * 100.0f;
  100. return insn.event_count;
  101. }
  102. if (index.column() == Column::Address)
  103. return String::format("%#08x", insn.address);
  104. if (index.column() == Column::InstructionBytes) {
  105. StringBuilder builder;
  106. for (auto ch : insn.bytes) {
  107. builder.appendf("%02x ", (u8)ch);
  108. }
  109. return builder.to_string();
  110. }
  111. if (index.column() == Column::Disassembly)
  112. return insn.disassembly;
  113. return {};
  114. }
  115. return {};
  116. }
  117. void DisassemblyModel::update()
  118. {
  119. did_update();
  120. }