DisassemblyModel.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. class ELFSymbolProvider final : public X86::SymbolProvider {
  50. public:
  51. ELFSymbolProvider(ELF::Loader& loader)
  52. : m_loader(loader)
  53. {
  54. }
  55. virtual String symbolicate(FlatPtr address, u32* offset = nullptr) const
  56. {
  57. return m_loader.symbolicate(address, offset);
  58. }
  59. private:
  60. ELF::Loader& m_loader;
  61. };
  62. DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
  63. : m_profile(profile)
  64. , m_node(node)
  65. {
  66. String path;
  67. if (m_node.address() >= 0xc0000000)
  68. path = "/boot/Kernel";
  69. else
  70. path = profile.executable_path();
  71. m_file = make<MappedFile>(path);
  72. if (!m_file->is_valid())
  73. return;
  74. auto elf_loader = ELF::Loader::create((const u8*)m_file->data(), m_file->size());
  75. auto symbol = elf_loader->find_symbol(node.address());
  76. if (!symbol.has_value())
  77. return;
  78. ASSERT(symbol.has_value());
  79. auto view = symbol.value().raw_data();
  80. ELFSymbolProvider symbol_provider(*elf_loader);
  81. X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
  82. X86::Disassembler disassembler(stream);
  83. size_t offset_into_symbol = 0;
  84. for (;;) {
  85. auto insn = disassembler.next();
  86. if (!insn.has_value())
  87. break;
  88. FlatPtr address_in_profiled_program = symbol.value().value() + offset_into_symbol;
  89. auto disassembly = insn.value().to_string(address_in_profiled_program, &symbol_provider);
  90. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  91. size_t samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
  92. float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f;
  93. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent });
  94. offset_into_symbol += insn.value().length();
  95. }
  96. }
  97. DisassemblyModel::~DisassemblyModel()
  98. {
  99. }
  100. int DisassemblyModel::row_count(const GUI::ModelIndex&) const
  101. {
  102. return m_instructions.size();
  103. }
  104. String DisassemblyModel::column_name(int column) const
  105. {
  106. switch (column) {
  107. case Column::SampleCount:
  108. return m_profile.show_percentages() ? "% Samples" : "# Samples";
  109. case Column::Address:
  110. return "Address";
  111. case Column::InstructionBytes:
  112. return "Insn Bytes";
  113. case Column::Disassembly:
  114. return "Disassembly";
  115. default:
  116. ASSERT_NOT_REACHED();
  117. return {};
  118. }
  119. }
  120. GUI::Model::ColumnMetadata DisassemblyModel::column_metadata(int column) const
  121. {
  122. if (column == Column::SampleCount)
  123. return ColumnMetadata { 0, Gfx::TextAlignment::CenterRight };
  124. return {};
  125. }
  126. struct ColorPair {
  127. Color background;
  128. Color foreground;
  129. };
  130. static Optional<ColorPair> color_pair_for(const InstructionData& insn)
  131. {
  132. if (insn.percent == 0)
  133. return {};
  134. Color background = color_for_percent(insn.percent);
  135. Color foreground;
  136. if (insn.percent > 50)
  137. foreground = Color::White;
  138. else
  139. foreground = Color::Black;
  140. return ColorPair { background, foreground };
  141. }
  142. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, Role role) const
  143. {
  144. auto& insn = m_instructions[index.row()];
  145. if (role == Role::BackgroundColor) {
  146. auto colors = color_pair_for(insn);
  147. if (!colors.has_value())
  148. return {};
  149. return colors.value().background;
  150. }
  151. if (role == Role::ForegroundColor) {
  152. auto colors = color_pair_for(insn);
  153. if (!colors.has_value())
  154. return {};
  155. return colors.value().foreground;
  156. }
  157. if (role == Role::Display) {
  158. if (index.column() == Column::SampleCount) {
  159. if (m_profile.show_percentages())
  160. return ((float)insn.event_count / (float)m_node.event_count()) * 100.0f;
  161. return insn.event_count;
  162. }
  163. if (index.column() == Column::Address)
  164. return String::format("%#08x", insn.address);
  165. if (index.column() == Column::InstructionBytes) {
  166. StringBuilder builder;
  167. for (auto ch : insn.bytes) {
  168. builder.appendf("%02x ", (u8)ch);
  169. }
  170. return builder.to_string();
  171. }
  172. if (index.column() == Column::Disassembly)
  173. return insn.disassembly;
  174. return {};
  175. }
  176. return {};
  177. }
  178. void DisassemblyModel::update()
  179. {
  180. did_update();
  181. }