DisassemblyModel.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DisassemblyModel.h"
  7. #include "Profile.h"
  8. #include <AK/MappedFile.h>
  9. #include <LibDebug/DebugInfo.h>
  10. #include <LibELF/Image.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibSymbolication/Symbolication.h>
  13. #include <LibX86/Disassembler.h>
  14. #include <LibX86/ELFSymbolProvider.h>
  15. #include <stdio.h>
  16. namespace Profiler {
  17. static const Gfx::Bitmap& heat_gradient()
  18. {
  19. static RefPtr<Gfx::Bitmap> bitmap;
  20. if (!bitmap) {
  21. bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 101, 1 });
  22. GUI::Painter painter(*bitmap);
  23. painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000));
  24. }
  25. return *bitmap;
  26. }
  27. static Color color_for_percent(int percent)
  28. {
  29. VERIFY(percent >= 0 && percent <= 100);
  30. return heat_gradient().get_pixel(percent, 0);
  31. }
  32. DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
  33. : m_profile(profile)
  34. , m_node(node)
  35. {
  36. OwnPtr<ELF::Image> kernel_elf;
  37. const ELF::Image* elf;
  38. FlatPtr base_address = 0;
  39. auto maybe_kernel_base = Symbolication::kernel_base();
  40. if (maybe_kernel_base.has_value() && m_node.address() >= maybe_kernel_base.value()) {
  41. if (!m_kernel_file) {
  42. auto file_or_error = MappedFile::map("/boot/Kernel.debug");
  43. if (file_or_error.is_error())
  44. return;
  45. m_kernel_file = file_or_error.release_value();
  46. }
  47. kernel_elf = make<ELF::Image>((const u8*)m_kernel_file->data(), m_kernel_file->size());
  48. elf = kernel_elf.ptr();
  49. } else {
  50. auto& process = node.process();
  51. auto library_data = process.library_metadata.library_containing(node.address());
  52. if (!library_data) {
  53. dbgln("no library data for address {:p}", node.address());
  54. return;
  55. }
  56. elf = &library_data->object->elf;
  57. base_address = library_data->base;
  58. }
  59. VERIFY(elf != nullptr);
  60. auto symbol = elf->find_symbol(node.address() - base_address);
  61. if (!symbol.has_value()) {
  62. dbgln("DisassemblyModel: symbol not found");
  63. return;
  64. }
  65. VERIFY(symbol.has_value());
  66. auto view = symbol.value().raw_data();
  67. X86::ELFSymbolProvider symbol_provider(*elf);
  68. X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
  69. X86::Disassembler disassembler(stream);
  70. Debug::DebugInfo debug_info { *elf, {}, base_address };
  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 = base_address + 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. u32 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, debug_info.get_source_position_with_inlines(address_in_profiled_program - base_address) });
  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. case Column::SourceLocation:
  104. return "Source Location";
  105. default:
  106. VERIFY_NOT_REACHED();
  107. return {};
  108. }
  109. }
  110. struct ColorPair {
  111. Color background;
  112. Color foreground;
  113. };
  114. static Optional<ColorPair> color_pair_for(const InstructionData& insn)
  115. {
  116. if (insn.percent == 0)
  117. return {};
  118. Color background = color_for_percent(insn.percent);
  119. Color foreground;
  120. if (insn.percent > 50)
  121. foreground = Color::White;
  122. else
  123. foreground = Color::Black;
  124. return ColorPair { background, foreground };
  125. }
  126. GUI::Variant DisassemblyModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  127. {
  128. auto& insn = m_instructions[index.row()];
  129. if (role == GUI::ModelRole::BackgroundColor) {
  130. auto colors = color_pair_for(insn);
  131. if (!colors.has_value())
  132. return {};
  133. return colors.value().background;
  134. }
  135. if (role == GUI::ModelRole::ForegroundColor) {
  136. auto colors = color_pair_for(insn);
  137. if (!colors.has_value())
  138. return {};
  139. return colors.value().foreground;
  140. }
  141. if (role == GUI::ModelRole::Display) {
  142. if (index.column() == Column::SampleCount) {
  143. if (m_profile.show_percentages())
  144. return ((float)insn.event_count / (float)m_node.event_count()) * 100.0f;
  145. return insn.event_count;
  146. }
  147. if (index.column() == Column::Address)
  148. return String::formatted("{:p}", insn.address);
  149. if (index.column() == Column::InstructionBytes) {
  150. StringBuilder builder;
  151. for (auto ch : insn.bytes) {
  152. builder.appendff("{:02x} ", (u8)ch);
  153. }
  154. return builder.to_string();
  155. }
  156. if (index.column() == Column::Disassembly)
  157. return insn.disassembly;
  158. if (index.column() == Column::SourceLocation) {
  159. StringBuilder builder;
  160. auto first = true;
  161. for (auto& entry : insn.source_position_with_inlines.inline_chain) {
  162. if (first)
  163. first = false;
  164. else
  165. builder.append(" => ");
  166. builder.appendff("{}:{}", entry.file_path, entry.line_number);
  167. }
  168. if (insn.source_position_with_inlines.source_position.has_value()) {
  169. if (!first)
  170. builder.append(" => ");
  171. auto& entry = insn.source_position_with_inlines.source_position.value();
  172. builder.appendff("{}:{}", entry.file_path, entry.line_number);
  173. }
  174. return builder.build();
  175. }
  176. return {};
  177. }
  178. return {};
  179. }
  180. void DisassemblyModel::update()
  181. {
  182. did_update();
  183. }
  184. }