Skip to content

llvm-dwarfdump --verify aggregated output to JSON file #81762

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 9 commits into from
Feb 28, 2024
1 change: 1 addition & 0 deletions llvm/include/llvm/DebugInfo/DIContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ struct DIDumpOptions {
bool IsEH = false;
bool DumpNonSkeleton = false;
bool ShowAggregateErrors = false;
std::string JsonErrSummaryFile;
std::function<llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)>
GetNameForDWARFReg;

Expand Down
29 changes: 28 additions & 1 deletion llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
#include "llvm/Support/DJB.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <map>
Expand Down Expand Up @@ -2026,12 +2028,37 @@ void OutputCategoryAggregator::EnumerateResults(
}

void DWARFVerifier::summarize() {
if (ErrorCategory.GetNumCategories() && DumpOpts.ShowAggregateErrors) {
if (DumpOpts.ShowAggregateErrors && ErrorCategory.GetNumCategories()) {
error() << "Aggregated error counts:\n";
ErrorCategory.EnumerateResults([&](StringRef s, unsigned count) {
error() << s << " occurred " << count << " time(s).\n";
});
}
if (!DumpOpts.JsonErrSummaryFile.empty()) {
std::error_code EC;
raw_fd_ostream JsonStream(DumpOpts.JsonErrSummaryFile, EC,
sys::fs::OF_Text);
if (EC) {
error() << "unable to open json summary file '"
<< DumpOpts.JsonErrSummaryFile
<< "' for writing: " << EC.message() << '\n';
return;
}

llvm::json::Object Categories;
uint64_t ErrorCount = 0;
ErrorCategory.EnumerateResults([&](StringRef Category, unsigned Count) {
llvm::json::Object Val;
Val.try_emplace("count", Count);
Categories.try_emplace(Category, std::move(Val));
ErrorCount += Count;
});
llvm::json::Object RootNode;
RootNode.try_emplace("error-categories", std::move(Categories));
RootNode.try_emplace("error-count", ErrorCount);

JsonStream << llvm::json::Value(std::move(RootNode));
}
}

raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
Expand Down
14 changes: 12 additions & 2 deletions llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ static opt<bool> Verify("verify", desc("Verify the DWARF debug info."),
cat(DwarfDumpCategory));
static opt<ErrorDetailLevel> ErrorDetails(
"error-display", init(Unspecified),
desc("Set the level of detail and summary to display when verifying "
"(implies --verify)"),
values(clEnumValN(NoDetailsOrSummary, "quiet",
"Only display whether errors occurred."),
clEnumValN(NoDetailsOnlySummary, "summary",
Expand All @@ -295,6 +297,11 @@ static opt<ErrorDetailLevel> ErrorDetails(
clEnumValN(BothDetailsAndSummary, "full",
"Display each error as well as a summary. [default]")),
cat(DwarfDumpCategory));
static opt<std::string> JsonErrSummaryFile(
"verify-json", init(""),
desc("Output JSON-formatted error summary to the specified file. "
"(Implies --verify)"),
value_desc("filename.json"), cat(DwarfDumpCategory));
static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."),
cat(DwarfDumpCategory));
static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture."),
Expand Down Expand Up @@ -349,6 +356,7 @@ static DIDumpOptions getDumpOpts(DWARFContext &C) {
ErrorDetails != NoDetailsOrSummary;
DumpOpts.ShowAggregateErrors = ErrorDetails != OnlyDetailsNoSummary &&
ErrorDetails != NoDetailsOnlySummary;
DumpOpts.JsonErrSummaryFile = JsonErrSummaryFile;
return DumpOpts.noImplicitRecursion();
}
return DumpOpts;
Expand Down Expand Up @@ -834,8 +842,10 @@ int main(int argc, char **argv) {
"-verbose is currently not supported";
return 1;
}
if (!Verify && ErrorDetails != Unspecified)
WithColor::warning() << "-error-detail has no affect without -verify";
// -error-detail and -json-summary-file both imply -verify
if (ErrorDetails != Unspecified || !JsonErrSummaryFile.empty()) {
Verify = true;
}

std::error_code EC;
ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_TextWithCRLF);
Expand Down