DisassemblyModel.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "DisassemblyModel.h"
  9. #include "Gradient.h"
  10. #include "PercentageFormatting.h"
  11. #include "Profile.h"
  12. #include <LibCore/MappedFile.h>
  13. #include <LibDebug/DebugInfo.h>
  14. #include <LibELF/Image.h>
  15. #include <LibSymbolication/Symbolication.h>
  16. #include <LibX86/Disassembler.h>
  17. #include <LibX86/ELFSymbolProvider.h>
  18. #include <stdio.h>
  19. namespace Profiler {
  20. static Optional<MappedObject> s_kernel_binary;
  21. static ELF::Image* try_load_kernel_binary()
  22. {
  23. if (s_kernel_binary.has_value())
  24. return &s_kernel_binary->elf;
  25. auto kernel_binary_or_error = Core::MappedFile::map("/boot/Kernel"sv);
  26. if (!kernel_binary_or_error.is_error()) {
  27. auto kernel_binary = kernel_binary_or_error.release_value();
  28. auto image = ELF::Image(kernel_binary->bytes());
  29. s_kernel_binary = { { move(kernel_binary), image } };
  30. return &s_kernel_binary->elf;
  31. }
  32. return nullptr;
  33. }
  34. DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
  35. : m_profile(profile)
  36. , m_node(node)
  37. {
  38. FlatPtr base_address = 0;
  39. Debug::DebugInfo const* debug_info;
  40. ELF::Image const* elf;
  41. if (auto maybe_kernel_base = Symbolication::kernel_base(); maybe_kernel_base.has_value() && m_node.address() >= *maybe_kernel_base) {
  42. if (!g_kernel_debuginfo_object.has_value())
  43. return;
  44. base_address = maybe_kernel_base.release_value();
  45. elf = try_load_kernel_binary();
  46. if (elf == nullptr)
  47. return;
  48. if (g_kernel_debug_info == nullptr)
  49. g_kernel_debug_info = make<Debug::DebugInfo>(g_kernel_debuginfo_object->elf, DeprecatedString::empty(), base_address);
  50. debug_info = g_kernel_debug_info.ptr();
  51. } else {
  52. auto const& process = node.process();
  53. auto const* library_data = process.library_metadata.library_containing(node.address());
  54. if (!library_data) {
  55. dbgln("no library data for address {:p}", node.address());
  56. return;
  57. }
  58. base_address = library_data->base;
  59. elf = &library_data->object->elf;
  60. debug_info = &library_data->load_debug_info(base_address);
  61. }
  62. VERIFY(elf != nullptr);
  63. VERIFY(debug_info != nullptr);
  64. FlatPtr function_address = node.address() - base_address;
  65. auto is_function_address = false;
  66. auto function = debug_info->get_containing_function(function_address);
  67. if (function.has_value()) {
  68. if (function_address == function->address_low)
  69. is_function_address = true;
  70. function_address = function->address_low;
  71. } else {
  72. dbgln("DisassemblyModel: Function containing {:p} ({}) not found", node.address() - base_address, node.symbol());
  73. }
  74. auto symbol = elf->find_symbol(function_address);
  75. if (!symbol.has_value()) {
  76. dbgln("DisassemblyModel: symbol not found");
  77. return;
  78. }
  79. if (!symbol.value().raw_data().length()) {
  80. dbgln("DisassemblyModel: Found symbol without code");
  81. return;
  82. }
  83. VERIFY(symbol.has_value());
  84. auto symbol_offset_from_function_start = node.address() - base_address - symbol->value();
  85. auto view = symbol.value().raw_data().substring_view(symbol_offset_from_function_start);
  86. X86::ELFSymbolProvider symbol_provider(*elf, base_address);
  87. X86::SimpleInstructionStream stream((u8 const*)view.characters_without_null_termination(), view.length());
  88. X86::Disassembler disassembler(stream);
  89. size_t offset_into_symbol = 0;
  90. FlatPtr last_instruction_offset = 0;
  91. if (!is_function_address) {
  92. FlatPtr last_instruction_address = 0;
  93. for (auto const& event : node.events_per_address())
  94. last_instruction_address = max(event.key, last_instruction_address);
  95. last_instruction_offset = last_instruction_address - node.address();
  96. }
  97. for (;;) {
  98. if (!is_function_address && offset_into_symbol > last_instruction_offset)
  99. break;
  100. auto insn = disassembler.next();
  101. if (!insn.has_value())
  102. break;
  103. FlatPtr address_in_profiled_program = node.address() + offset_into_symbol;
  104. auto disassembly = insn.value().to_deprecated_string(address_in_profiled_program, &symbol_provider);
  105. StringView instruction_bytes = view.substring_view(offset_into_symbol, insn.value().length());
  106. u32 samples_at_this_instruction = m_node.events_per_address().get(address_in_profiled_program).value_or(0);
  107. float percent = ((float)samples_at_this_instruction / (float)m_node.event_count()) * 100.0f;
  108. auto source_position = debug_info->get_source_position_with_inlines(address_in_profiled_program - base_address).release_value_but_fixme_should_propagate_errors();
  109. m_instructions.append({ insn.value(), disassembly, instruction_bytes, address_in_profiled_program, samples_at_this_instruction, percent, source_position });
  110. offset_into_symbol += insn.value().length();
  111. }
  112. }
  113. int DisassemblyModel::row_count(GUI::ModelIndex const&) const
  114. {
  115. return m_instructions.size();
  116. }
  117. ErrorOr<String> DisassemblyModel::column_name(int column) const
  118. {
  119. switch (column) {
  120. case Column::SampleCount:
  121. return m_profile.show_percentages() ? "% Samples"_string : "# Samples"_string;
  122. case Column::Address:
  123. return "Address"_string;
  124. case Column::InstructionBytes:
  125. return "Insn Bytes"_string;
  126. case Column::Disassembly:
  127. return "Disassembly"_string;
  128. case Column::SourceLocation:
  129. return "Source Location"_string;
  130. default:
  131. VERIFY_NOT_REACHED();
  132. }
  133. }
  134. struct ColorPair {
  135. Color background;
  136. Color foreground;
  137. };
  138. static Optional<ColorPair> color_pair_for(InstructionData const& insn)
  139. {
  140. if (insn.percent == 0)
  141. return {};
  142. Color background = color_for_percent(insn.percent);
  143. Color foreground;
  144. if (insn.percent > 50)
  145. foreground = Color::White;
  146. else
  147. foreground = Color::Black;
  148. return ColorPair { background, foreground };
  149. }
  150. GUI::Variant DisassemblyModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  151. {
  152. auto const& insn = m_instructions[index.row()];
  153. if (role == GUI::ModelRole::BackgroundColor) {
  154. auto colors = color_pair_for(insn);
  155. if (!colors.has_value())
  156. return {};
  157. return colors.value().background;
  158. }
  159. if (role == GUI::ModelRole::ForegroundColor) {
  160. auto colors = color_pair_for(insn);
  161. if (!colors.has_value())
  162. return {};
  163. return colors.value().foreground;
  164. }
  165. if (role == GUI::ModelRole::TextAlignment) {
  166. if (index.column() == Column::SampleCount)
  167. return Gfx::TextAlignment::CenterRight;
  168. }
  169. if (role == GUI::ModelRole::Display) {
  170. if (index.column() == Column::SampleCount) {
  171. if (m_profile.show_percentages())
  172. return format_percentage(insn.event_count, m_node.event_count());
  173. return insn.event_count;
  174. }
  175. if (index.column() == Column::Address)
  176. return DeprecatedString::formatted("{:p}", insn.address);
  177. if (index.column() == Column::InstructionBytes) {
  178. StringBuilder builder;
  179. for (auto ch : insn.bytes) {
  180. builder.appendff("{:02x} ", (u8)ch);
  181. }
  182. return builder.to_deprecated_string();
  183. }
  184. if (index.column() == Column::Disassembly)
  185. return insn.disassembly;
  186. if (index.column() == Column::SourceLocation) {
  187. StringBuilder builder;
  188. auto first = true;
  189. for (auto const& entry : insn.source_position_with_inlines.inline_chain) {
  190. if (first)
  191. first = false;
  192. else
  193. builder.append(" => "sv);
  194. builder.appendff("{}:{}", entry.file_path, entry.line_number);
  195. }
  196. if (insn.source_position_with_inlines.source_position.has_value()) {
  197. if (!first)
  198. builder.append(" => "sv);
  199. auto const& entry = insn.source_position_with_inlines.source_position.value();
  200. builder.appendff("{}:{}", entry.file_path, entry.line_number);
  201. }
  202. return builder.to_deprecated_string();
  203. }
  204. return {};
  205. }
  206. return {};
  207. }
  208. }