DisassemblyModel.cpp 6.6 KB

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