Skip to content

[lldb] Print a warning on checksum mismatch #107968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lldb/include/lldb/Core/SourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class SourceManager {

const Checksum &GetChecksum() const { return m_checksum; }

llvm::once_flag &GetChecksumWarningOnceFlag() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This getter seems rather pointless, isn't the only user in SourceManager itself?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this isn't SourceManager.

return m_checksum_warning_once_flag;
}

protected:
/// Set file and update modification time.
void SetSupportFile(lldb::SupportFileSP support_file_sp);
Expand All @@ -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;

Expand Down
24 changes: 18 additions & 6 deletions lldb/source/Core/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions lldb/test/Shell/SymbolFile/Inputs/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main(int argc, char **argv) {
// Break on main.
return 1;
}
7 changes: 7 additions & 0 deletions lldb/test/Shell/SymbolFile/checksum-mismatch.test
Original file line number Diff line number Diff line change
@@ -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 ({{.*}})
Loading