DisassemblyModel.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. size_t offset_into_symbol = 0;
  45. for (;;) {
  46. auto insn = disassembler.next();
  47. if (!insn.has_value())
  48. break;
  49. FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
  50. auto disassembly = insn.value().to_string(address_in_profiled_program);
  51. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  52. size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
  53. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction });
  54. offset_into_symbol += insn.value().length();
  55. }
  56. }
  57. DisassemblyModel::~DisassemblyModel()
  58. {
  59. }
  60. int DisassemblyModel::row_count(const GUI::ModelIndex&) const
  61. {
  62. return m_instructions.size();
  63. }
  64. String DisassemblyModel::column_name(int column) const
  65. {
  66. switch (column) {
  67. case Column::SampleCount:
  68. return m_profile.show_percentages() ? "% Samples" : "# Samples";
  69. case Column::Address:
  70. return "Address";
  71. case Column::InstructionBytes:
  72. return "Insn Bytes";
  73. case Column::Disassembly:
  74. return "Disassembly";
  75. default:
  76. ASSERT_NOT_REACHED();
  77. return {};
  78. }
  79. }
  80. GUI::Model::ColumnMetadata DisassemblyModel::column_metadata(int column) const
  81. {
  82. if (column == Column::SampleCount)
  83. return ColumnMetadata { 0, Gfx::TextAlignment::CenterRight };
  84. return {};
  85. }
  86. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const
  87. {
  88. auto& insn = m_instructions[index.row()];
  89. if (role == Role::BackgroundColor) {
  90. if (insn.event_count > 0)
  91. return Color(Color::Yellow);
  92. return {};
  93. }
  94. if (role == Role::Display) {
  95. if (index.column() == Column::SampleCount) {
  96. if (m_profile.show_percentages())
  97. return ((float)insn.event_count / (float)m_profile.filtered_event_count()) * 100.0f;
  98. return insn.event_count;
  99. }
  100. if (index.column() == Column::Address)
  101. return String::format("%#08x", insn.address);
  102. if (index.column() == Column::InstructionBytes) {
  103. StringBuilder builder;
  104. for (auto ch : insn.bytes) {
  105. builder.appendf("%02x ", (u8)ch);
  106. }
  107. return builder.to_string();
  108. }
  109. if (index.column() == Column::Disassembly)
  110. return insn.disassembly;
  111. return {};
  112. }
  113. return {};
  114. }
  115. void DisassemblyModel::update()
  116. {
  117. did_update();
  118. }