DisassemblyModel.cpp 5.6 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. static const Gfx::Bitmap& heat_gradient()
  16. {
  17. static RefPtr<Gfx::Bitmap> bitmap;
  18. if (!bitmap) {
  19. bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { 101, 1 });
  20. GUI::Painter painter(*bitmap);
  21. painter.fill_rect_with_gradient(Orientation::Horizontal, bitmap->rect(), Color::from_rgb(0xffc080), Color::from_rgb(0xff3000));
  22. }
  23. return *bitmap;
  24. }
  25. static Color color_for_percent(int percent)
  26. {
  27. VERIFY(percent >= 0 && percent <= 100);
  28. return heat_gradient().get_pixel(percent, 0);
  29. }
  30. DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
  31. : m_profile(profile)
  32. , m_node(node)
  33. {
  34. OwnPtr<ELF::Image> kernel_elf;
  35. const ELF::Image* elf;
  36. FlatPtr base_address = 0;
  37. if (m_node.address() >= 0xc0000000) {
  38. if (!m_kernel_file) {
  39. auto file_or_error = MappedFile::map("/boot/Kernel");
  40. if (file_or_error.is_error())
  41. return;
  42. m_kernel_file = file_or_error.release_value();
  43. }
  44. kernel_elf = make<ELF::Image>((const u8*)m_kernel_file->data(), m_kernel_file->size());
  45. elf = kernel_elf.ptr();
  46. } else {
  47. auto process = node.process(profile, node.timestamp());
  48. if (!process) {
  49. dbgln("no process for address {}", node.address());
  50. return;
  51. }
  52. auto library_data = process->library_metadata.library_containing(node.address());
  53. if (!library_data) {
  54. dbgln("no library data for address {}", node.address());
  55. return;
  56. }
  57. elf = &library_data->object->elf;
  58. base_address = library_data->base;
  59. }
  60. VERIFY(elf != nullptr);
  61. auto symbol = elf->find_symbol(node.address() - base_address);
  62. if (!symbol.has_value()) {
  63. dbgln("DisassemblyModel: symbol not found");
  64. return;
  65. }
  66. VERIFY(symbol.has_value());
  67. auto view = symbol.value().raw_data();
  68. X86::ELFSymbolProvider symbol_provider(*elf);
  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 = 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. 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. VERIFY_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. }