DisassemblyModel.cpp 6.1 KB

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