Backtrace.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/CoreDump.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('/') && path.ends_with(".so"sv))
  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. #if !ARCH(I386)
  32. // FIXME: Fix LibDebug
  33. return nullptr;
  34. #endif
  35. auto info = make<ELFObjectInfo>(file_or_error.release_value(), make<Debug::DebugInfo>(move(image)));
  36. auto* info_ptr = info.ptr();
  37. m_debug_info_cache.set(path, move(info));
  38. return info_ptr;
  39. }
  40. Backtrace::Backtrace(const Reader& coredump, const ELF::Core::ThreadInfo& thread_info)
  41. : m_thread_info(move(thread_info))
  42. {
  43. FlatPtr* bp;
  44. FlatPtr* ip;
  45. #if ARCH(I386)
  46. bp = (FlatPtr*)m_thread_info.regs.ebp;
  47. ip = (FlatPtr*)m_thread_info.regs.eip;
  48. #else
  49. bp = (FlatPtr*)m_thread_info.regs.rbp;
  50. ip = (FlatPtr*)m_thread_info.regs.rip;
  51. #endif
  52. bool first_frame = true;
  53. while (bp && ip) {
  54. // We use eip - 1 because the return address from a function frame
  55. // is the instruction that comes after the 'call' instruction.
  56. // However, because the first frame represents the faulting
  57. // instruction rather than the return address we don't subtract
  58. // 1 there.
  59. VERIFY((FlatPtr)ip > 0);
  60. add_entry(coredump, (FlatPtr)ip - (first_frame ? 0 : 1));
  61. first_frame = false;
  62. auto next_ip = coredump.peek_memory((FlatPtr)(bp + 1));
  63. auto next_bp = coredump.peek_memory((FlatPtr)(bp));
  64. if (!next_ip.has_value() || !next_bp.has_value())
  65. break;
  66. ip = (FlatPtr*)next_ip.value();
  67. bp = (FlatPtr*)next_bp.value();
  68. }
  69. }
  70. Backtrace::~Backtrace()
  71. {
  72. }
  73. void Backtrace::add_entry(const Reader& coredump, FlatPtr eip)
  74. {
  75. auto* region = coredump.region_containing((FlatPtr)eip);
  76. if (!region) {
  77. m_entries.append({ eip, {}, {}, {} });
  78. return;
  79. }
  80. auto object_name = region->object_name();
  81. if (object_name == "Loader.so")
  82. return;
  83. auto* object_info = object_info_for_region(*region);
  84. if (!object_info)
  85. return;
  86. #if ARCH(I386)
  87. auto function_name = object_info->debug_info->elf().symbolicate(eip - region->region_start);
  88. auto source_position = object_info->debug_info->get_source_position_with_inlines(eip - region->region_start);
  89. #else
  90. // FIXME: Fix symbolication.
  91. auto function_name = "";
  92. Debug::DebugInfo::SourcePositionWithInlines source_position;
  93. #endif
  94. m_entries.append({ eip, object_name, function_name, source_position });
  95. }
  96. String Backtrace::Entry::to_string(bool color) const
  97. {
  98. StringBuilder builder;
  99. builder.appendff("{:p}: ", eip);
  100. if (object_name.is_empty()) {
  101. builder.append("???");
  102. return builder.build();
  103. }
  104. builder.appendff("[{}] {}", object_name, function_name.is_empty() ? "???" : function_name);
  105. builder.append(" (");
  106. Vector<Debug::DebugInfo::SourcePosition> source_positions;
  107. for (auto& position : source_position_with_inlines.inline_chain) {
  108. if (!source_positions.contains_slow(position))
  109. source_positions.append(position);
  110. }
  111. if (source_position_with_inlines.source_position.has_value() && !source_positions.contains_slow(source_position_with_inlines.source_position.value())) {
  112. source_positions.insert(0, source_position_with_inlines.source_position.value());
  113. }
  114. for (size_t i = 0; i < source_positions.size(); ++i) {
  115. auto& position = source_positions[i];
  116. auto fmt = color ? "\033[34;1m{}\033[0m:{}" : "{}:{}";
  117. builder.appendff(fmt, LexicalPath::basename(position.file_path), position.line_number);
  118. if (i != source_positions.size() - 1) {
  119. builder.append(" => ");
  120. }
  121. }
  122. builder.append(")");
  123. return builder.build();
  124. }
  125. }