[llvm] GSym aggregated output to JSON file (PR #81763)

Kevin Frei via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 14 09:10:17 PST 2024


https://github.com/kevinfrei created https://github.com/llvm/llvm-project/pull/81763

In order to make tooling around dwarf health easier, I've added an `--aggregate-output-file` file to gsymutil that will spit out error summary data with counts to a JSON file.

I've added the same capability to `llvm-dwarfdump --verify` in a [different PR.](https://github.com/llvm/llvm-project/pull/81762)


>From c9cced726fb2fc94ecdf1809fc2b73ebc022042e Mon Sep 17 00:00:00 2001
From: Kevin Frei <freik at meta.com>
Date: Wed, 14 Feb 2024 09:03:47 -0800
Subject: [PATCH] GSym aggregated output to JSON file

---
 llvm/tools/llvm-gsymutil/Opts.td           |  3 +++
 llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp | 23 ++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/llvm/tools/llvm-gsymutil/Opts.td b/llvm/tools/llvm-gsymutil/Opts.td
index 740291479f9323..00e81a9f85aa70 100644
--- a/llvm/tools/llvm-gsymutil/Opts.td
+++ b/llvm/tools/llvm-gsymutil/Opts.td
@@ -35,3 +35,6 @@ defm address : Eq<"address", "Lookup an address in a GSYM file">;
 def addresses_from_stdin :
   FF<"addresses-from-stdin",
      "Lookup addresses in a GSYM file that are read from stdin\nEach input line is expected to be of the following format: <addr> <gsym-path>">;
+defm aggregate_error_file :
+  Eq<"aggregate-error-file",
+     "Output any aggregated errors into the file specified in JSON format.">;
diff --git a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp
index 2de9c76fd68c0c..1068ac4e39b266 100644
--- a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp
+++ b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/DebugInfo/DIContext.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/Object/Archive.h"
@@ -87,6 +88,7 @@ static std::vector<std::string> InputFilenames;
 static std::string ConvertFilename;
 static std::vector<std::string> ArchFilters;
 static std::string OutputFilename;
+static std::string AggregateJsonFile;
 static bool Verify;
 static unsigned NumThreads;
 static uint64_t SegmentSize;
@@ -138,6 +140,9 @@ static void parseArgs(int argc, char **argv) {
   if (const llvm::opt::Arg *A = Args.getLastArg(OPT_out_file_EQ))
     OutputFilename = A->getValue();
 
+  if (const llvm::opt::Arg *A = Args.getLastArg(OPT_aggregate_error_file_EQ))
+    AggregateJsonFile = A->getValue();
+
   Verify = Args.hasArg(OPT_verify);
 
   if (const llvm::opt::Arg *A = Args.getLastArg(OPT_num_threads_EQ)) {
@@ -515,10 +520,28 @@ int llvm_gsymutil_main(int argc, char **argv, const llvm::ToolContext &) {
     // Call error() if we have an error and it will exit with a status of 1
     if (auto Err = convertFileToGSYM(Aggregation))
       error("DWARF conversion failed: ", std::move(Err));
+
     // Report the errors from aggregator:
     Aggregation.EnumerateResults([&](StringRef category, unsigned count) {
       OS << category << " occurred " << count << " time(s)\n";
     });
+    if (!AggregateJsonFile.empty()) {
+      std::error_code EC;
+      raw_fd_ostream JsonStream(AggregateJsonFile, EC,
+                                sys::fs::OF_Text | sys::fs::OF_None);
+      if (EC) {
+        OS << "error opening aggregate error json file '" << AggregateJsonFile
+           << "' for writing: " << EC.message() << '\n';
+        return 1;
+      }
+      JsonStream << "{\"errors\":[\n";
+      Aggregation.EnumerateResults([&](StringRef category, unsigned count) {
+        JsonStream << "\"category\":\"";
+        llvm::printEscapedString(category, JsonStream);
+        JsonStream << "\",\"count\":" << count;
+      });
+      JsonStream << "]}\n";
+    }
     return 0;
   }
 



More information about the llvm-commits mailing list