Backtrace.cpp 5.5 KB

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