Backtrace.cpp 5.4 KB

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