DisassemblyModel.cpp 5.5 KB

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