Backtrace.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/MappedFile.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/Types.h>
  30. #include <LibCore/File.h>
  31. #include <LibCoreDump/Backtrace.h>
  32. #include <LibCoreDump/Reader.h>
  33. #include <LibELF/CoreDump.h>
  34. #include <LibELF/Image.h>
  35. namespace CoreDump {
  36. // FIXME: This cache has to be invalidated when libraries/programs are re-compiled.
  37. // We can store the last-modified timestamp of the elf files in ELFObjectInfo to invalidate cache entries.
  38. static HashMap<String, NonnullOwnPtr<ELFObjectInfo>> s_debug_info_cache;
  39. static const ELFObjectInfo* object_info_for_region(const ELF::Core::MemoryRegionInfo& region)
  40. {
  41. auto name = region.object_name();
  42. String path;
  43. if (name.contains(".so"))
  44. path = String::formatted("/usr/lib/{}", name);
  45. else {
  46. path = name;
  47. }
  48. if (auto it = s_debug_info_cache.find(path); it != s_debug_info_cache.end())
  49. return it->value.ptr();
  50. if (!Core::File::exists(path.characters()))
  51. return nullptr;
  52. auto file_or_error = MappedFile::map(path);
  53. if (file_or_error.is_error())
  54. return nullptr;
  55. auto image = make<ELF::Image>(file_or_error.value()->bytes());
  56. auto info = make<ELFObjectInfo>(file_or_error.release_value(), Debug::DebugInfo { move(image) });
  57. auto* info_ptr = info.ptr();
  58. s_debug_info_cache.set(path, move(info));
  59. return info_ptr;
  60. }
  61. Backtrace::Backtrace(const Reader& coredump)
  62. {
  63. coredump.for_each_thread_info([this, &coredump](const ELF::Core::ThreadInfo& thread_info) {
  64. uint32_t* ebp = (uint32_t*)thread_info.regs.ebp;
  65. uint32_t* eip = (uint32_t*)thread_info.regs.eip;
  66. while (ebp && eip) {
  67. add_backtrace_entry(coredump, (FlatPtr)eip);
  68. auto next_eip = coredump.peek_memory((FlatPtr)(ebp + 1));
  69. auto next_ebp = coredump.peek_memory((FlatPtr)(ebp));
  70. if (!next_eip.has_value() || !next_ebp.has_value())
  71. break;
  72. eip = (uint32_t*)next_eip.value();
  73. ebp = (uint32_t*)next_ebp.value();
  74. }
  75. return IterationDecision::Continue;
  76. });
  77. }
  78. Backtrace::~Backtrace()
  79. {
  80. }
  81. void Backtrace::add_backtrace_entry(const Reader& coredump, FlatPtr eip)
  82. {
  83. auto* region = coredump.region_containing((FlatPtr)eip);
  84. if (!region) {
  85. m_entries.append({ eip, {}, {}, {} });
  86. return;
  87. }
  88. auto object_name = region->object_name();
  89. if (object_name == "Loader.so")
  90. return;
  91. auto* object_info = object_info_for_region(*region);
  92. if (!object_info)
  93. return;
  94. auto function_name = object_info->debug_info.elf().symbolicate(eip - region->region_start);
  95. auto source_position = object_info->debug_info.get_source_position(eip - region->region_start);
  96. m_entries.append({ eip, object_name, function_name, source_position });
  97. }
  98. String Backtrace::Entry::to_string(bool color) const
  99. {
  100. StringBuilder builder;
  101. builder.appendff("{:p}: ", eip);
  102. if (object_name.is_empty()) {
  103. builder.append("???");
  104. return builder.build();
  105. }
  106. builder.appendff("[{}] {}", object_name, function_name.is_empty() ? "???" : function_name);
  107. if (source_position.has_value()) {
  108. auto& source_position = this->source_position.value();
  109. auto fmt = color ? " (\033[34;1m{}\033[0m:{})" : " ({}:{})";
  110. builder.appendff(fmt, LexicalPath(source_position.file_path).basename(), source_position.line_number);
  111. }
  112. return builder.build();
  113. }
  114. }