Backtrace.cpp 4.2 KB

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