Backtrace.cpp 5.5 KB

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