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

Conversation

kevinfrei
Copy link
Contributor

@kevinfrei kevinfrei commented Feb 14, 2024

In order to make tooling around dwarf health easier, I've added an --aggregate-output-file 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.

The format of the json is:

{ 
  "error-categories": { 
    "<first category description>": {"count": 1234},
    "<next category description>": {"count":4321}
  },
  "error-count": 5555
}

for a clean run:

{ 
  "error-categories": {},
  "error-count": 0
}

@llvmbot
Copy link
Member

llvmbot commented Feb 14, 2024

@llvm/pr-subscribers-debuginfo

Author: Kevin Frei (kevinfrei)

Changes

In order to make tooling around dwarf health easier, I've added an --aggregate-output-file file to 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: [todo: add link when PR is up]


Full diff: https://github.com/llvm/llvm-project/pull/81762.diff

3 Files Affected:

  • (modified) llvm/include/llvm/DebugInfo/DIContext.h (+1)
  • (modified) llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp (+23-1)
  • (modified) llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp (+6)
diff --git a/llvm/include/llvm/DebugInfo/DIContext.h b/llvm/include/llvm/DebugInfo/DIContext.h
index 288ddf77bdfda7..a6407389b9dcc0 100644
--- a/llvm/include/llvm/DebugInfo/DIContext.h
+++ b/llvm/include/llvm/DebugInfo/DIContext.h
@@ -206,6 +206,7 @@ struct DIDumpOptions {
   bool IsEH = false;
   bool DumpNonSkeleton = false;
   bool ShowAggregateErrors = false;
+  std::string AggregateErrJsonFile = "";
   std::function<llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)>
       GetNameForDWARFReg;
 
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
index 20ef59e7b4422e..5c8b5ba48f4193 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFVerifier.cpp
@@ -9,6 +9,7 @@
 #include "llvm/ADT/IntervalMap.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/Dwarf.h"
 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
 #include "llvm/DebugInfo/DWARF/DWARFAttribute.h"
@@ -29,6 +30,7 @@
 #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/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
@@ -2026,12 +2028,32 @@ void OutputCategoryAggregator::EnumerateResults(
 }
 
 void DWARFVerifier::summarize() {
-  if (ErrorCategory.GetNumCategories() && DumpOpts.ShowAggregateErrors) {
+  if (!ErrorCategory.GetNumCategories())
+    return;
+  if (DumpOpts.ShowAggregateErrors) {
     error() << "Aggregated error counts:\n";
     ErrorCategory.EnumerateResults([&](StringRef s, unsigned count) {
       error() << s << " occurred " << count << " time(s).\n";
     });
   }
+  if (!DumpOpts.AggregateErrJsonFile.empty()) {
+    std::error_code EC;
+    raw_fd_ostream JsonStream(DumpOpts.AggregateErrJsonFile, EC,
+                              sys::fs::OF_Text | sys::fs::OF_None);
+    if (EC) {
+      error() << "error opening aggregate error json file '"
+              << DumpOpts.AggregateErrJsonFile << "' for writing: "
+              << EC.message() << '\n';
+      return;
+    }
+    JsonStream << "{\"errors\":[\n";
+    ErrorCategory.EnumerateResults([&](StringRef category, unsigned count) {
+      JsonStream << "\"category\":\"";
+      llvm::printEscapedString(category, JsonStream);
+      JsonStream << "\",\"count\":" << count;
+    });
+    JsonStream << "]}\n";
+  }
 }
 
 raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 8cdd84bcc867cb..40b1b5db5fe3cd 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -295,6 +295,10 @@ static opt<ErrorDetailLevel> ErrorDetails(
            clEnumValN(BothDetailsAndSummary, "full",
                       "Display each error as well as a summary. [default]")),
     cat(DwarfDumpCategory));
+static opt<std::string> AggregationJsonFile(
+    "aggregate-output-file", cl::init(""),
+    cl::desc("Output JSON-formatted error summary to the specified file."),
+    cl::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."),
@@ -836,6 +840,8 @@ int main(int argc, char **argv) {
   }
   if (!Verify && ErrorDetails != Unspecified)
     WithColor::warning() << "-error-detail has no affect without -verify";
+  if (!Verify && !AggregationJsonFile.empty())
+    WithColor::warning() << "-aggregation-json has no affect without -verify";
 
   std::error_code EC;
   ToolOutputFile OutputFile(OutputFilename, EC, sys::fs::OF_TextWithCRLF);

Copy link

github-actions bot commented Feb 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@kevinfrei kevinfrei marked this pull request as draft February 14, 2024 17:15
@kevinfrei kevinfrei marked this pull request as ready for review February 14, 2024 18:52
@kevinfrei
Copy link
Contributor Author

@dwblaikie Any concerns with this?

@kevinfrei
Copy link
Contributor Author

@clayborg had some excellent feedback on the GsymUtil PR that I'm going to put in here, too.

Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

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

LGTM. I will let the llvm owners give the final OK.

@kevinfrei
Copy link
Contributor Author

@JDevlieghere any thoughts on this one? It's pretty focused & straightforward (outside of the controversy over the CLI switch name (naming is hard...)

clayborg pushed a commit that referenced this pull request Feb 22, 2024
In order to make tooling around dwarf health easier, I've added an
`--json-summary-file` option to `llvm-gsymutil` that will spit out error
summary data with counts to a JSON file.

I've added the same capability to `llvm-dwarfdump` in a [different
PR.](#81762)

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]>
@clayborg clayborg merged commit 6244dfe into llvm:main Feb 28, 2024
@kevinfrei kevinfrei deleted the json-agg-dwarfdump branch February 28, 2024 19:16
mylai-mtk pushed a commit to mylai-mtk/llvm-project that referenced this pull request Jul 12, 2024
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.](llvm#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]>
bd1976bris added a commit that referenced this pull request Oct 4, 2024
This adds documentation for --verify-json, see:
#81762
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants