https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/107968
Print a warning when the debugger detects a mismatch between the MD5 checksum in the DWARF 5 line table and the file on disk. The warning is printed only once per file. >From 397e41bfb8516659ec6af7cf98c2d3bb33d3df98 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere <jo...@devlieghere.com> Date: Mon, 9 Sep 2024 21:05:25 -0700 Subject: [PATCH] [lldb] Print a warning on checksum mismatch Print a warning when the debugger detects a mismatch between the MD5 checksum in the DWARF 5 line table and the file on disk. The warning is printed only once per file. --- lldb/include/lldb/Core/SourceManager.h | 7 ++++++ lldb/source/Core/SourceManager.cpp | 24 ++++++++++++++----- lldb/test/Shell/SymbolFile/Inputs/main.c | 4 ++++ .../Shell/SymbolFile/checksum-mismatch.test | 7 ++++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 lldb/test/Shell/SymbolFile/Inputs/main.c create mode 100644 lldb/test/Shell/SymbolFile/checksum-mismatch.test diff --git a/lldb/include/lldb/Core/SourceManager.h b/lldb/include/lldb/Core/SourceManager.h index 172824dc78a6bc..e38627182a9690 100644 --- a/lldb/include/lldb/Core/SourceManager.h +++ b/lldb/include/lldb/Core/SourceManager.h @@ -74,6 +74,10 @@ class SourceManager { const Checksum &GetChecksum() const { return m_checksum; } + llvm::once_flag &GetChecksumWarningOnceFlag() { + return m_checksum_warning_once_flag; + } + protected: /// Set file and update modification time. void SetSupportFile(lldb::SupportFileSP support_file_sp); @@ -87,6 +91,9 @@ class SourceManager { /// Keep track of the on-disk checksum. Checksum m_checksum; + /// Once flag for emitting a checksum mismatch warning. + llvm::once_flag m_checksum_warning_once_flag; + // Keep the modification time that this file data is valid for llvm::sys::TimePoint<> m_mod_time; diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp index f97d86ad79f6ab..fd5b49946c6a92 100644 --- a/lldb/source/Core/SourceManager.cpp +++ b/lldb/source/Core/SourceManager.cpp @@ -61,6 +61,12 @@ static void resolve_tilde(FileSpec &file_spec) { } } +static std::string toString(const Checksum &checksum) { + if (!checksum) + return ""; + return std::string(llvm::formatv("{0}", checksum.digest())); +} + // SourceManager constructor SourceManager::SourceManager(const TargetSP &target_sp) : m_last_support_file_sp(std::make_shared<SupportFile>()), m_last_line(0), @@ -302,6 +308,18 @@ size_t SourceManager::DisplaySourceLinesWithLineNumbersUsingLastFile( break; } } + + Checksum line_table_checksum = + last_file_sp->GetSupportFile()->GetChecksum(); + Checksum on_disk_checksum = last_file_sp->GetChecksum(); + if (line_table_checksum && line_table_checksum != on_disk_checksum) + Debugger::ReportWarning( + llvm::formatv( + "{0}: source file checksum mismatch between line table " + "({1}) and file on disk ({2})", + last_file_sp->GetSupportFile()->GetSpecOnly().GetFilename(), + toString(line_table_checksum), toString(on_disk_checksum)), + std::nullopt, &last_file_sp->GetChecksumWarningOnceFlag()); } return *delta; } @@ -837,12 +855,6 @@ SourceManager::FileSP SourceManager::SourceFileCache::FindSourceFile( return {}; } -static std::string toString(const Checksum &checksum) { - if (!checksum) - return ""; - return std::string(llvm::formatv("{0}", checksum.digest())); -} - void SourceManager::SourceFileCache::Dump(Stream &stream) const { // clang-format off stream << "Modification time MD5 Checksum (on-disk) MD5 Checksum (line table) Lines Path\n"; diff --git a/lldb/test/Shell/SymbolFile/Inputs/main.c b/lldb/test/Shell/SymbolFile/Inputs/main.c new file mode 100644 index 00000000000000..341417f970d7fe --- /dev/null +++ b/lldb/test/Shell/SymbolFile/Inputs/main.c @@ -0,0 +1,4 @@ +int main(int argc, char **argv) { + // Break on main. + return 1; +} diff --git a/lldb/test/Shell/SymbolFile/checksum-mismatch.test b/lldb/test/Shell/SymbolFile/checksum-mismatch.test new file mode 100644 index 00000000000000..5db97647c9aa02 --- /dev/null +++ b/lldb/test/Shell/SymbolFile/checksum-mismatch.test @@ -0,0 +1,7 @@ +RUN: mkdir -p %t +RUN: cp %S/Inputs/main.c %t/main.c +RUN: %clang_host %t/main.c -std=c99 -gdwarf-5 -o %t/main.out +RUN: echo "// Modify source file hash" >> %t/main.c +RUN: %lldb -b %t/main.out -o 'b main' -o 'r' 2>&1 | FileCheck %s + +CHECK: warning: main.c: source file checksum mismatch between line table ({{.*}}) and file on disk ({{.*}}) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits