Backtrace.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. DeprecatedString 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. #else
  46. # error Unknown architecture
  47. #endif
  48. // In order to provide progress updates, we first have to walk the
  49. // call stack to determine how many frames it has.
  50. size_t frame_count = 0;
  51. {
  52. auto bp = start_bp;
  53. auto ip = start_ip;
  54. while (bp && ip) {
  55. ++frame_count;
  56. auto next_ip = coredump.peek_memory(bp + sizeof(FlatPtr));
  57. auto next_bp = coredump.peek_memory(bp);
  58. if (!next_ip.has_value() || !next_bp.has_value())
  59. break;
  60. ip = next_ip.value();
  61. bp = next_bp.value();
  62. }
  63. }
  64. auto bp = start_bp;
  65. auto ip = start_ip;
  66. size_t frame_index = 0;
  67. while (bp && ip) {
  68. // We use eip - 1 because the return address from a function frame
  69. // is the instruction that comes after the 'call' instruction.
  70. // However, because the first frame represents the faulting
  71. // instruction rather than the return address we don't subtract
  72. // 1 there.
  73. VERIFY(ip > 0);
  74. add_entry(coredump, ip - ((frame_index == 0) ? 0 : 1));
  75. if (on_progress)
  76. on_progress(frame_index, frame_count);
  77. ++frame_index;
  78. auto next_ip = coredump.peek_memory(bp + sizeof(FlatPtr));
  79. auto next_bp = coredump.peek_memory(bp);
  80. if (!next_ip.has_value() || !next_bp.has_value())
  81. break;
  82. ip = next_ip.value();
  83. bp = next_bp.value();
  84. }
  85. }
  86. void Backtrace::add_entry(Reader const& coredump, FlatPtr ip)
  87. {
  88. auto ip_region = coredump.region_containing(ip);
  89. if (!ip_region.has_value()) {
  90. m_entries.append({ ip, {}, {}, {} });
  91. return;
  92. }
  93. auto object_name = ip_region->object_name();
  94. // Only skip addresses coming from Loader.so if the faulting instruction is not in Loader.so
  95. if (object_name == "Loader.so") {
  96. if (m_skip_loader_so)
  97. return;
  98. } else {
  99. m_skip_loader_so = true;
  100. }
  101. // We need to find the first region for the object, just in case
  102. // the PT_LOAD header for the .text segment isn't the first one
  103. // in the object file.
  104. auto region = coredump.first_region_for_object(object_name);
  105. auto object_info = object_info_for_region(coredump, *region);
  106. if (!object_info) {
  107. m_entries.append({ ip, object_name, {}, {} });
  108. return;
  109. }
  110. auto function_name = object_info->debug_info->elf().symbolicate(ip - region->region_start);
  111. auto source_position = object_info->debug_info->get_source_position_with_inlines(ip - region->region_start).release_value_but_fixme_should_propagate_errors();
  112. m_entries.append({ ip, object_name, function_name, source_position });
  113. }
  114. DeprecatedString Backtrace::Entry::to_deprecated_string(bool color) const
  115. {
  116. StringBuilder builder;
  117. builder.appendff("{:p}: ", eip);
  118. if (object_name.is_empty()) {
  119. builder.append("???"sv);
  120. return builder.to_deprecated_string();
  121. }
  122. builder.appendff("[{}] {}", object_name, function_name.is_empty() ? "???" : function_name);
  123. builder.append(" ("sv);
  124. Vector<Debug::DebugInfo::SourcePosition> source_positions;
  125. for (auto& position : source_position_with_inlines.inline_chain) {
  126. if (!source_positions.contains_slow(position))
  127. source_positions.append(position);
  128. }
  129. if (source_position_with_inlines.source_position.has_value() && !source_positions.contains_slow(source_position_with_inlines.source_position.value())) {
  130. source_positions.insert(0, source_position_with_inlines.source_position.value());
  131. }
  132. for (size_t i = 0; i < source_positions.size(); ++i) {
  133. auto& position = source_positions[i];
  134. auto fmt = color ? "\033[34;1m{}\033[0m:{}"sv : "{}:{}"sv;
  135. builder.appendff(fmt, LexicalPath::basename(position.file_path), position.line_number);
  136. if (i != source_positions.size() - 1) {
  137. builder.append(" => "sv);
  138. }
  139. }
  140. builder.append(')');
  141. return builder.to_deprecated_string();
  142. }
  143. }