DisassemblyModel.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/Loader.h>
  30. #include <LibGUI/Painter.h>
  31. #include <LibX86/Disassembler.h>
  32. #include <ctype.h>
  33. #include <stdio.h>
  34. static const Gfx::Bitmap& heat_gradient()
  35. {
  36. static RefPtr<Gfx::Bitmap> bitmap;
  37. if (!bitmap) {
  38. bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 100, 1 });
  39. GUI::Painter painter(*bitmap);
  40. painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000));
  41. }
  42. return *bitmap;
  43. }
  44. static Color color_for_percent(int percent)
  45. {
  46. ASSERT(percent >= 0 && percent <= 100);
  47. return heat_gradient().get_pixel(percent, 0);
  48. }
  49. DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
  50. : m_profile(profile)
  51. , m_node(node)
  52. {
  53. m_file = make<MappedFile>(profile.executable_path());
  54. if (!m_file->is_valid())
  55. return;
  56. auto elf_loader = make<ELF::Loader>((const u8*)m_file->data(), m_file->size());
  57. auto symbol = elf_loader->find_symbol(node.address());
  58. if (!symbol.has_value())
  59. return;
  60. ASSERT(symbol.has_value());
  61. auto view = symbol.value().raw_data();
  62. X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
  63. X86::Disassembler disassembler(stream);
  64. size_t offset_into_symbol = 0;
  65. for (;;) {
  66. auto insn = disassembler.next();
  67. if (!insn.has_value())
  68. break;
  69. FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
  70. auto disassembly = insn.value().to_string(address_in_profiled_program);
  71. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  72. size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
  73. float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f;
  74. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent });
  75. offset_into_symbol += insn.value().length();
  76. }
  77. }
  78. DisassemblyModel::~DisassemblyModel()
  79. {
  80. }
  81. int DisassemblyModel::row_count(const GUI::ModelIndex&) const
  82. {
  83. return m_instructions.size();
  84. }
  85. String DisassemblyModel::column_name(int column) const
  86. {
  87. switch (column) {
  88. case Column::SampleCount:
  89. return m_profile.show_percentages() ? "% Samples" : "# Samples";
  90. case Column::Address:
  91. return "Address";
  92. case Column::InstructionBytes:
  93. return "Insn Bytes";
  94. case Column::Disassembly:
  95. return "Disassembly";
  96. default:
  97. ASSERT_NOT_REACHED();
  98. return {};
  99. }
  100. }
  101. GUI::Model::ColumnMetadata DisassemblyModel::column_metadata(int column) const
  102. {
  103. if (column == Column::SampleCount)
  104. return ColumnMetadata { 0, Gfx::TextAlignment::CenterRight };
  105. return {};
  106. }
  107. struct ColorPair {
  108. Color background;
  109. Color foreground;
  110. };
  111. static Optional<ColorPair> color_pair_for(const InstructionData& insn)
  112. {
  113. if (insn.percent == 0)
  114. return {};
  115. Color background = color_for_percent(insn.percent);
  116. Color foreground;
  117. if (insn.percent > 50)
  118. foreground = Color::White;
  119. else
  120. foreground = Color::Black;
  121. return ColorPair { background, foreground };
  122. }
  123. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const
  124. {
  125. auto& insn = m_instructions[index.row()];
  126. if (role == Role::BackgroundColor) {
  127. auto colors = color_pair_for(insn);
  128. if (!colors.has_value())
  129. return {};
  130. return colors.value().background;
  131. }
  132. if (role == Role::ForegroundColor) {
  133. auto colors = color_pair_for(insn);
  134. if (!colors.has_value())
  135. return {};
  136. return colors.value().foreground;
  137. }
  138. if (role == Role::Display) {
  139. if (index.column() == Column::SampleCount) {
  140. if (m_profile.show_percentages())
  141. return ((float)insn.event_count / (float)m_node.event_count()) * 100.0f;
  142. return insn.event_count;
  143. }
  144. if (index.column() == Column::Address)
  145. return String::format("%#08x", insn.address);
  146. if (index.column() == Column::InstructionBytes) {
  147. StringBuilder builder;
  148. for (auto ch : insn.bytes) {
  149. builder.appendf("%02x ", (u8)ch);
  150. }
  151. return builder.to_string();
  152. }
  153. if (index.column() == Column::Disassembly)
  154. return insn.disassembly;
  155. return {};
  156. }
  157. return {};
  158. }
  159. void DisassemblyModel::update()
  160. {
  161. did_update();
  162. }