Backtrace.cpp 4.5 KB

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