Skip to content

Commit 6244dfe

Browse files
kevinfreiKevin Frei
and
Kevin Frei
authored
llvm-dwarfdump --verify aggregated output to JSON file (#81762)
In order to make tooling around dwarf health easier, I've added an `--verify-json` option to `llvm-dwarfdump --verify` that will spit out error summary data with counts to a JSON file. I've added the same capability to `llvm-gsymutil` in a [different PR.](#81763) The format of the json is: ``` json { "error-categories": { "<first category description>": {"count": 1234}, "<next category description>": {"count":4321} }, "error-count": 5555 } ``` for a clean run: ``` json { "error-categories": {}, "error-count": 0 } ``` --------- Co-authored-by: Kevin Frei <[email protected]>
1 parent baf6725 commit 6244dfe

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

llvm/include/llvm/DebugInfo/DIContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ struct DIDumpOptions {
206206
bool IsEH = false;
207207
bool DumpNonSkeleton = false;
208208
bool ShowAggregateErrors = false;
209+
std::string JsonErrSummaryFile;
209210
std::function<llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)>
210211
GetNameForDWARFReg;
211212

llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#include "llvm/Support/DJB.h"
3030
#include "llvm/Support/Error.h"
3131
#include "llvm/Support/ErrorHandling.h"
32+
#include "llvm/Support/FileSystem.h"
3233
#include "llvm/Support/FormatVariadic.h"
34+
#include "llvm/Support/JSON.h"
3335
#include "llvm/Support/WithColor.h"
3436
#include "llvm/Support/raw_ostream.h"
3537
#include <map>
@@ -2026,12 +2028,37 @@ void OutputCategoryAggregator::EnumerateResults(
20262028
}
20272029

20282030
void DWARFVerifier::summarize() {
2029-
if (ErrorCategory.GetNumCategories() && DumpOpts.ShowAggregateErrors) {
2031+
if (DumpOpts.ShowAggregateErrors && ErrorCategory.GetNumCategories()) {
20302032
error() << "Aggregated error counts:\n";
20312033
ErrorCategory.EnumerateResults([&](StringRef s, unsigned count) {
20322034
error() << s << " occurred " << count << " time(s).\n";
20332035
});
20342036
}
2037+
if (!DumpOpts.JsonErrSummaryFile.empty()) {
2038+
std::error_code EC;
2039+
raw_fd_ostream JsonStream(DumpOpts.JsonErrSummaryFile, EC,
2040+
sys::fs::OF_Text);
2041+
if (EC) {
2042+
error() << "unable to open json summary file '"
2043+
<< DumpOpts.JsonErrSummaryFile
2044+
<< "' for writing: " << EC.message() << '\n';
2045+
return;
2046+
}
2047+
2048+
llvm::json::Object Categories;
2049+
uint64_t ErrorCount = 0;
2050+
ErrorCategory.EnumerateResults([&](StringRef Category, unsigned Count) {
2051+
llvm::json::Object Val;
2052+
Val.try_emplace("count", Count);
2053+
Categories.try_emplace(Category, std::move(Val));
2054+
ErrorCount += Count;
2055+
});
2056+
llvm::json::Object RootNode;
2057+
RootNode.try_emplace("error-categories", std::move(Categories));
2058+
RootNode.try_emplace("error-count", ErrorCount);
2059+
2060+
JsonStream << llvm::json::Value(std::move(RootNode));
2061+
}
20352062
}
20362063

20372064
raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }

llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ static opt<bool> Verify("verify", desc("Verify the DWARF debug info."),
286286
cat(DwarfDumpCategory));
287287
static opt<ErrorDetailLevel> ErrorDetails(
288288
"error-display", init(Unspecified),
289+
desc("Set the level of detail and summary to display when verifying "
290+
"(implies --verify)"),
289291
values(clEnumValN(NoDetailsOrSummary, "quiet",
290292
"Only display whether errors occurred."),
291293
clEnumValN(NoDetailsOnlySummary, "summary",
@@ -295,6 +297,11 @@ static opt<ErrorDetailLevel> ErrorDetails(
295297
clEnumValN(BothDetailsAndSummary, "full",
296298
"Display each error as well as a summary. [default]")),
297299
cat(DwarfDumpCategory));
300+
static opt<std::string> JsonErrSummaryFile(
301+
"verify-json", init(""),
302+
desc("Output JSON-formatted error summary to the specified file. "
303+
"(Implies --verify)"),
304+
value_desc("filename.json"), cat(DwarfDumpCategory));
298305
static opt<bool> Quiet("quiet", desc("Use with -verify to not emit to STDOUT."),
299306
cat(DwarfDumpCategory));
300307
static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture."),
@@ -349,6 +356,7 @@ static DIDumpOptions getDumpOpts(DWARFContext &C) {
349356
ErrorDetails != NoDetailsOrSummary;
350357
DumpOpts.ShowAggregateErrors = ErrorDetails != OnlyDetailsNoSummary &&
351358
ErrorDetails != NoDetailsOnlySummary;
359+
DumpOpts.JsonErrSummaryFile = JsonErrSummaryFile;
352360
return DumpOpts.noImplicitRecursion();
353361
}
354362
return DumpOpts;
@@ -834,8 +842,10 @@ int main(int argc, char **argv) {
834842
"-verbose is currently not supported";
835843
return 1;
836844
}
837-
if (!Verify && ErrorDetails != Unspecified)
838-
WithColor::warning() << "-error-detail has no affect without -verify";
845+
// -error-detail and -json-summary-file both imply -verify
846+
if (ErrorDetails != Unspecified || !JsonErrSummaryFile.empty()) {
847+
Verify = true;
848+
}
839849

840850
std::error_code EC;
841851
ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_TextWithCRLF);

0 commit comments

Comments
 (0)