Backtrace.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/Platform.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/Types.h>
  11. #include <LibCore/MappedFile.h>
  12. #include <LibCoredump/Backtrace.h>
  13. #include <LibCoredump/Reader.h>
  14. #include <LibELF/Core.h>
  15. #include <LibELF/Image.h>
  16. #include <LibFileSystem/FileSystem.h>
  17. namespace Coredump {
  18. ELFObjectInfo const* Backtrace::object_info_for_region(Reader const& coredump, MemoryRegionInfo const& region)
  19. {
  20. ByteString path = coredump.resolve_object_path(region.object_name());
  21. auto maybe_ptr = m_debug_info_cache.get(path);
  22. if (maybe_ptr.has_value())
  23. return *maybe_ptr;
  24. if (!FileSystem::exists(path))
  25. return nullptr;
  26. auto file_or_error = Core::MappedFile::map(path);
  27. if (file_or_error.is_error())
  28. return nullptr;
  29. auto image = make<ELF::Image>(file_or_error.value()->bytes());
  30. auto& image_reference = *image;
  31. auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(image_reference), move(image));
  32. auto* info_ptr = info.ptr();
  33. m_debug_info_cache.set(path, move(info));
  34. return info_ptr;
  35. }
  36. Backtrace::Backtrace(Reader const& coredump, const ELF::Core::ThreadInfo& thread_info, Function<void(size_t, size_t)> on_progress)
  37. : m_thread_info(move(thread_info))
  38. {
  39. #if ARCH(X86_64)
  40. auto start_bp = m_thread_info.regs.rbp;
  41. auto start_ip = m_thread_info.regs.rip;
  42. #elif ARCH(AARCH64)
  43. auto start_bp = m_thread_info.regs.x[29];
  44. auto start_ip = m_thread_info.regs.pc;
  45. #elif ARCH(RISCV64)
  46. auto start_bp = m_thread_info.regs.x[7];
  47. auto start_ip = m_thread_info.regs.pc;
  48. #else
  49. # error Unknown architecture
  50. #endif
  51. // In order to provide progress updates, we first have to walk the
  52. // call stack to determine how many frames it has.
  53. size_t frame_count = 0;
  54. {
  55. auto bp = start_bp;
  56. auto ip = start_ip;
  57. while (bp && ip) {
  58. ++frame_count;
  59. auto next_ip = coredump.peek_memory(bp + sizeof(FlatPtr));
  60. auto next_bp = coredump.peek_memory(bp);
  61. if (!next_ip.has_value() || !next_bp.has_value())
  62. break;
  63. ip = next_ip.value();
  64. bp = next_bp.value();
  65. }
  66. }
  67. auto bp = start_bp;
  68. auto ip = start_ip;
  69. size_t frame_index = 0;
  70. while (bp && ip) {
  71. // We use eip - 1 because the return address from a function frame
  72. // is the instruction that comes after the 'call' instruction.
  73. // However, because the first frame represents the faulting
  74. // instruction rather than the return address we don't subtract
  75. // 1 there.
  76. VERIFY(ip > 0);
  77. add_entry(coredump, ip - ((frame_index == 0) ? 0 : 1));
  78. if (on_progress)
  79. on_progress(frame_index, frame_count);
  80. ++frame_index;
  81. auto next_ip = coredump.peek_memory(bp + sizeof(FlatPtr));
  82. auto next_bp = coredump.peek_memory(bp);
  83. if (!next_ip.has_value() || !next_bp.has_value())
  84. break;
  85. ip = next_ip.value();
  86. bp = next_bp.value();
  87. }
  88. }
  89. void Backtrace::add_entry(Reader const& coredump, FlatPtr ip)
  90. {
  91. auto ip_region = coredump.region_containing(ip);
  92. if (!ip_region.has_value()) {
  93. m_entries.append({ ip, {}, {}, {} });
  94. return;
  95. }
  96. auto object_name = ip_region->object_name();
  97. // Only skip addresses coming from Loader.so if the faulting instruction is not in Loader.so
  98. if (object_name == "Loader.so") {
  99. if (m_skip_loader_so)
  100. return;
  101. } else {
  102. m_skip_loader_so = true;
  103. }
  104. // We need to find the first region for the object, just in case
  105. // the PT_LOAD header for the .text segment isn't the first one
  106. // in the object file.
  107. auto region = coredump.first_region_for_object(object_name);
  108. auto object_info = object_info_for_region(coredump, *region);
  109. if (!object_info) {
  110. m_entries.append({ ip, object_name, {}, {} });
  111. return;
  112. }
  113. auto function_name = object_info->debug_info->elf().symbolicate(ip - region->region_start);
  114. auto source_position = object_info->debug_info->get_source_position_with_inlines(ip - region->region_start).release_value_but_fixme_should_propagate_errors();
  115. m_entries.append({ ip, object_name, function_name, source_position });
  116. }
  117. ByteString Backtrace::Entry::to_byte_string(bool color) const
  118. {
  119. StringBuilder builder;
  120. builder.appendff("{:p}: ", eip);
  121. if (object_name.is_empty()) {
  122. builder.append("???"sv);
  123. return builder.to_byte_string();
  124. }
  125. builder.appendff("[{}] {}", object_name, function_name.is_empty() ? "???" : function_name);
  126. builder.append(" ("sv);
  127. Vector<Debug::DebugInfo::SourcePosition> source_positions;
  128. for (auto& position : source_position_with_inlines.inline_chain) {
  129. if (!source_positions.contains_slow(position))
  130. source_positions.append(position);
  131. }
  132. if (source_position_with_inlines.source_position.has_value() && !source_positions.contains_slow(source_position_with_inlines.source_position.value())) {
  133. source_positions.insert(0, source_position_with_inlines.source_position.value());
  134. }
  135. for (size_t i = 0; i < source_positions.size(); ++i) {
  136. auto& position = source_positions[i];
  137. auto fmt = color ? "\033[34;1m{}\033[0m:{}"sv : "{}:{}"sv;
  138. builder.appendff(fmt, LexicalPath::basename(position.file_path), position.line_number);
  139. if (i != source_positions.size() - 1) {
  140. builder.append(" => "sv);
  141. }
  142. }
  143. builder.append(')');
  144. return builder.to_byte_string();
  145. }
  146. }