[llvm] [llvm-dwarfdump] Added --output-style option for controlling output format (PR #216559)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 03:57:32 PDT 2026
https://github.com/arrowten updated https://github.com/llvm/llvm-project/pull/216559
>From d9600c2f45eacd7cafcc390407c87d097c79b25d Mon Sep 17 00:00:00 2001
From: Ajay Wakodikar <ajaywakodikarsocial at gmail.com>
Date: Sun, 16 Aug 2026 06:45:03 -0400
Subject: [PATCH 1/4] [llvm-dwarfdump] Added --output-style option for
controlling output format
---
llvm/docs/CommandGuide/llvm-dwarfdump.rst | 7 ++++
.../tools/llvm-dwarfdump/output-style.test | 23 +++++++++++++
llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 33 ++++++++++++++++++-
3 files changed, 62 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/tools/llvm-dwarfdump/output-style.test
diff --git a/llvm/docs/CommandGuide/llvm-dwarfdump.rst b/llvm/docs/CommandGuide/llvm-dwarfdump.rst
index 22f2edfc30f57..fff1bede3dced 100644
--- a/llvm/docs/CommandGuide/llvm-dwarfdump.rst
+++ b/llvm/docs/CommandGuide/llvm-dwarfdump.rst
@@ -95,6 +95,13 @@ OPTIONS
Redirect output to a file specified by <path>, where `-` is the
standard output stream.
+.. option:: --output-style=<value>
+
+ Specify the format of the output. The supported values are:
+
+ `LLVM` - Output in the format used by llvm-dwarfdump. [default]
+ `JSON` - Output in JSON format.
+
.. option:: -p, --show-parents
Show a debug info entry's parents when selectively printing with
diff --git a/llvm/test/tools/llvm-dwarfdump/output-style.test b/llvm/test/tools/llvm-dwarfdump/output-style.test
new file mode 100644
index 0000000000000..b59fc3c392f56
--- /dev/null
+++ b/llvm/test/tools/llvm-dwarfdump/output-style.test
@@ -0,0 +1,23 @@
+## Show that --output-style=LLVM (the default) dumps normally.
+# RUN: yaml2obj %s -o %t.o
+# RUN: llvm-dwarfdump --output-style=LLVM %t.o | FileCheck %s --check-prefix=LLVM
+
+# LLVM: file format elf64-x86-64
+
+## Show that --output-style=JSON emits a warning, since JSON output is not yet implemented.
+# RUN: llvm-dwarfdump --output-style=JSON %t.o 2>&1 | FileCheck %s --check-prefix=JSON
+
+# JSON: file format elf64-x86-64
+# JSON-NEXT: warning: JSON output style is not yet implemented
+
+## Show that an unrecognized --output-style value is rejected.
+# RUN: not llvm-dwarfdump --output-style=YAML 2>&1 | FileCheck %s --check-prefix=INVALID
+
+# INVALID: error: --output-style value should be either 'LLVM' or 'JSON', but was 'YAML'
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_X86_64
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 4d7fc7e3108bf..36d22335d935a 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -16,6 +16,7 @@
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVectorExtras.h"
#include "llvm/ADT/StringSet.h"
+#include "llvm/ADT/StringSwitch.h"
#include "llvm/DebugInfo/DIContext.h"
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
@@ -135,6 +136,8 @@ enum ErrorDetailLevel {
Unspecified
};
+enum OutputStyleTy { LLVM, JSON, UNKNOWN };
+
OptionCategory DwarfDumpCategory("Specific Options");
static list<std::string>
InputFilenames(Positional, desc("<input object files or .dSYM bundles>"),
@@ -334,6 +337,11 @@ static opt<bool> Verbose("verbose",
cat(DwarfDumpCategory));
static alias VerboseAlias("v", desc("Alias for --verbose."), aliasopt(Verbose),
cat(DwarfDumpCategory), cl::NotHidden);
+static opt<std::string>
+ OutputStyleOpt("output-style", init("LLVM"),
+ desc("Specify the format of the output (LLVM or JSON)"),
+ cat(DwarfDumpCategory));
+static OutputStyleTy OutputStyle = LLVM;
static opt<bool>
ShowVariableCoverage("show-variable-coverage",
desc("Show per-variable coverage metrics."),
@@ -775,7 +783,19 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
// Dump the complete DWARF structure.
auto DumpOpts = getDumpOpts(DICtx);
DumpOpts.GetNameForDWARFReg = GetRegName;
- DICtx.dump(OS, DumpOpts, DumpOffsets);
+ switch (OutputStyle) {
+ case LLVM:
+ DICtx.dump(OS, DumpOpts, DumpOffsets);
+ break;
+ case JSON:
+ WithColor::warning() << "JSON output style is not yet implemented\n";
+ break;
+ case UNKNOWN:
+ WithColor::error() << "--output-style value should be either 'LLVM' or "
+ "'JSON', but was '"
+ << OutputStyleOpt << "'\n";
+ return false;
+ }
return true;
}
@@ -889,6 +909,17 @@ int main(int argc, char **argv) {
"pretty-print DWARF debug information in object files"
" and debug info archives.\n");
+ OutputStyle = StringSwitch<OutputStyleTy>(OutputStyleOpt)
+ .Case("LLVM", LLVM)
+ .Case("JSON", JSON)
+ .Default(UNKNOWN);
+ if (OutputStyle == UNKNOWN) {
+ WithColor::error(errs(), sys::path::filename(argv[0]))
+ << "--output-style value should be either 'LLVM' or 'JSON', but was '"
+ << OutputStyleOpt << "'\n";
+ return 1;
+ }
+
// FIXME: Audit interactions between these two options and make them
// compatible.
if (Diff && Verbose) {
>From 08ae6f6eec3d37e418a2a5fd940e7c9b8112d9f8 Mon Sep 17 00:00:00 2001
From: Ajay Wakodikar <ajaywakodikarsocial at gmail.com>
Date: Mon, 24 Aug 2026 12:12:39 -0400
Subject: [PATCH 2/4] [llvm-dwarfdump] Changed the enum format and test file
---
.../tools/llvm-dwarfdump/output-style.test | 5 +++++
llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 22 +++++++++----------
2 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/llvm/test/tools/llvm-dwarfdump/output-style.test b/llvm/test/tools/llvm-dwarfdump/output-style.test
index b59fc3c392f56..27223f33333b0 100644
--- a/llvm/test/tools/llvm-dwarfdump/output-style.test
+++ b/llvm/test/tools/llvm-dwarfdump/output-style.test
@@ -4,6 +4,11 @@
# LLVM: file format elf64-x86-64
+## Show that omitting --output-style is equivalent to passing --output-style=LLVM explicitly.
+# RUN: llvm-dwarfdump %t.o > %t.default.txt
+# RUN: llvm-dwarfdump --output-style=LLVM %t.o > %t.llvm.txt
+# RUN: cmp %t.default.txt %t.llvm.txt
+
## Show that --output-style=JSON emits a warning, since JSON output is not yet implemented.
# RUN: llvm-dwarfdump --output-style=JSON %t.o 2>&1 | FileCheck %s --check-prefix=JSON
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 36d22335d935a..b1260beac4845 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -136,7 +136,7 @@ enum ErrorDetailLevel {
Unspecified
};
-enum OutputStyleTy { LLVM, JSON, UNKNOWN };
+enum class OutputStyleKind { LLVM, JSON, Unknown };
OptionCategory DwarfDumpCategory("Specific Options");
static list<std::string>
@@ -341,7 +341,7 @@ static opt<std::string>
OutputStyleOpt("output-style", init("LLVM"),
desc("Specify the format of the output (LLVM or JSON)"),
cat(DwarfDumpCategory));
-static OutputStyleTy OutputStyle = LLVM;
+static OutputStyleKind Style = OutputStyleKind::LLVM;
static opt<bool>
ShowVariableCoverage("show-variable-coverage",
desc("Show per-variable coverage metrics."),
@@ -783,14 +783,14 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
// Dump the complete DWARF structure.
auto DumpOpts = getDumpOpts(DICtx);
DumpOpts.GetNameForDWARFReg = GetRegName;
- switch (OutputStyle) {
- case LLVM:
+ switch (Style) {
+ case OutputStyleKind::LLVM:
DICtx.dump(OS, DumpOpts, DumpOffsets);
break;
- case JSON:
+ case OutputStyleKind::JSON:
WithColor::warning() << "JSON output style is not yet implemented\n";
break;
- case UNKNOWN:
+ case OutputStyleKind::Unknown:
WithColor::error() << "--output-style value should be either 'LLVM' or "
"'JSON', but was '"
<< OutputStyleOpt << "'\n";
@@ -909,11 +909,11 @@ int main(int argc, char **argv) {
"pretty-print DWARF debug information in object files"
" and debug info archives.\n");
- OutputStyle = StringSwitch<OutputStyleTy>(OutputStyleOpt)
- .Case("LLVM", LLVM)
- .Case("JSON", JSON)
- .Default(UNKNOWN);
- if (OutputStyle == UNKNOWN) {
+ Style = StringSwitch<OutputStyleKind>(OutputStyleOpt)
+ .Case("LLVM", OutputStyleKind::LLVM)
+ .Case("JSON", OutputStyleKind::JSON)
+ .Default(OutputStyleKind::Unknown);
+ if (Style == OutputStyleKind::Unknown) {
WithColor::error(errs(), sys::path::filename(argv[0]))
<< "--output-style value should be either 'LLVM' or 'JSON', but was '"
<< OutputStyleOpt << "'\n";
>From 4fb05c7f016088cf7f71211550e84ba80f1e1ce2 Mon Sep 17 00:00:00 2001
From: Ajay Wakodikar <ajaywakodikarsocial at gmail.com>
Date: Wed, 26 Aug 2026 06:25:07 -0400
Subject: [PATCH 3/4] Revert "[llvm-dwarfdump] Changed the enum format and test
file"
This reverts commit 08ae6f6eec3d37e418a2a5fd940e7c9b8112d9f8.
---
.../tools/llvm-dwarfdump/output-style.test | 5 -----
llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 22 +++++++++----------
2 files changed, 11 insertions(+), 16 deletions(-)
diff --git a/llvm/test/tools/llvm-dwarfdump/output-style.test b/llvm/test/tools/llvm-dwarfdump/output-style.test
index 27223f33333b0..b59fc3c392f56 100644
--- a/llvm/test/tools/llvm-dwarfdump/output-style.test
+++ b/llvm/test/tools/llvm-dwarfdump/output-style.test
@@ -4,11 +4,6 @@
# LLVM: file format elf64-x86-64
-## Show that omitting --output-style is equivalent to passing --output-style=LLVM explicitly.
-# RUN: llvm-dwarfdump %t.o > %t.default.txt
-# RUN: llvm-dwarfdump --output-style=LLVM %t.o > %t.llvm.txt
-# RUN: cmp %t.default.txt %t.llvm.txt
-
## Show that --output-style=JSON emits a warning, since JSON output is not yet implemented.
# RUN: llvm-dwarfdump --output-style=JSON %t.o 2>&1 | FileCheck %s --check-prefix=JSON
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index b1260beac4845..36d22335d935a 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -136,7 +136,7 @@ enum ErrorDetailLevel {
Unspecified
};
-enum class OutputStyleKind { LLVM, JSON, Unknown };
+enum OutputStyleTy { LLVM, JSON, UNKNOWN };
OptionCategory DwarfDumpCategory("Specific Options");
static list<std::string>
@@ -341,7 +341,7 @@ static opt<std::string>
OutputStyleOpt("output-style", init("LLVM"),
desc("Specify the format of the output (LLVM or JSON)"),
cat(DwarfDumpCategory));
-static OutputStyleKind Style = OutputStyleKind::LLVM;
+static OutputStyleTy OutputStyle = LLVM;
static opt<bool>
ShowVariableCoverage("show-variable-coverage",
desc("Show per-variable coverage metrics."),
@@ -783,14 +783,14 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
// Dump the complete DWARF structure.
auto DumpOpts = getDumpOpts(DICtx);
DumpOpts.GetNameForDWARFReg = GetRegName;
- switch (Style) {
- case OutputStyleKind::LLVM:
+ switch (OutputStyle) {
+ case LLVM:
DICtx.dump(OS, DumpOpts, DumpOffsets);
break;
- case OutputStyleKind::JSON:
+ case JSON:
WithColor::warning() << "JSON output style is not yet implemented\n";
break;
- case OutputStyleKind::Unknown:
+ case UNKNOWN:
WithColor::error() << "--output-style value should be either 'LLVM' or "
"'JSON', but was '"
<< OutputStyleOpt << "'\n";
@@ -909,11 +909,11 @@ int main(int argc, char **argv) {
"pretty-print DWARF debug information in object files"
" and debug info archives.\n");
- Style = StringSwitch<OutputStyleKind>(OutputStyleOpt)
- .Case("LLVM", OutputStyleKind::LLVM)
- .Case("JSON", OutputStyleKind::JSON)
- .Default(OutputStyleKind::Unknown);
- if (Style == OutputStyleKind::Unknown) {
+ OutputStyle = StringSwitch<OutputStyleTy>(OutputStyleOpt)
+ .Case("LLVM", LLVM)
+ .Case("JSON", JSON)
+ .Default(UNKNOWN);
+ if (OutputStyle == UNKNOWN) {
WithColor::error(errs(), sys::path::filename(argv[0]))
<< "--output-style value should be either 'LLVM' or 'JSON', but was '"
<< OutputStyleOpt << "'\n";
>From 17acb790d67d59ea4ae6aee4134b8f7d65d718a8 Mon Sep 17 00:00:00 2001
From: Ajay Wakodikar <ajaywakodikarsocial at gmail.com>
Date: Wed, 26 Aug 2026 06:55:39 -0400
Subject: [PATCH 4/4] [llvm-dwarfdump] Chnaged the enum format
---
.../tools/llvm-dwarfdump/output-style.test | 5 +++++
llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 22 +++++++++----------
2 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/llvm/test/tools/llvm-dwarfdump/output-style.test b/llvm/test/tools/llvm-dwarfdump/output-style.test
index b59fc3c392f56..27223f33333b0 100644
--- a/llvm/test/tools/llvm-dwarfdump/output-style.test
+++ b/llvm/test/tools/llvm-dwarfdump/output-style.test
@@ -4,6 +4,11 @@
# LLVM: file format elf64-x86-64
+## Show that omitting --output-style is equivalent to passing --output-style=LLVM explicitly.
+# RUN: llvm-dwarfdump %t.o > %t.default.txt
+# RUN: llvm-dwarfdump --output-style=LLVM %t.o > %t.llvm.txt
+# RUN: cmp %t.default.txt %t.llvm.txt
+
## Show that --output-style=JSON emits a warning, since JSON output is not yet implemented.
# RUN: llvm-dwarfdump --output-style=JSON %t.o 2>&1 | FileCheck %s --check-prefix=JSON
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index 36d22335d935a..b1260beac4845 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -136,7 +136,7 @@ enum ErrorDetailLevel {
Unspecified
};
-enum OutputStyleTy { LLVM, JSON, UNKNOWN };
+enum class OutputStyleKind { LLVM, JSON, Unknown };
OptionCategory DwarfDumpCategory("Specific Options");
static list<std::string>
@@ -341,7 +341,7 @@ static opt<std::string>
OutputStyleOpt("output-style", init("LLVM"),
desc("Specify the format of the output (LLVM or JSON)"),
cat(DwarfDumpCategory));
-static OutputStyleTy OutputStyle = LLVM;
+static OutputStyleKind Style = OutputStyleKind::LLVM;
static opt<bool>
ShowVariableCoverage("show-variable-coverage",
desc("Show per-variable coverage metrics."),
@@ -783,14 +783,14 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx,
// Dump the complete DWARF structure.
auto DumpOpts = getDumpOpts(DICtx);
DumpOpts.GetNameForDWARFReg = GetRegName;
- switch (OutputStyle) {
- case LLVM:
+ switch (Style) {
+ case OutputStyleKind::LLVM:
DICtx.dump(OS, DumpOpts, DumpOffsets);
break;
- case JSON:
+ case OutputStyleKind::JSON:
WithColor::warning() << "JSON output style is not yet implemented\n";
break;
- case UNKNOWN:
+ case OutputStyleKind::Unknown:
WithColor::error() << "--output-style value should be either 'LLVM' or "
"'JSON', but was '"
<< OutputStyleOpt << "'\n";
@@ -909,11 +909,11 @@ int main(int argc, char **argv) {
"pretty-print DWARF debug information in object files"
" and debug info archives.\n");
- OutputStyle = StringSwitch<OutputStyleTy>(OutputStyleOpt)
- .Case("LLVM", LLVM)
- .Case("JSON", JSON)
- .Default(UNKNOWN);
- if (OutputStyle == UNKNOWN) {
+ Style = StringSwitch<OutputStyleKind>(OutputStyleOpt)
+ .Case("LLVM", OutputStyleKind::LLVM)
+ .Case("JSON", OutputStyleKind::JSON)
+ .Default(OutputStyleKind::Unknown);
+ if (Style == OutputStyleKind::Unknown) {
WithColor::error(errs(), sys::path::filename(argv[0]))
<< "--output-style value should be either 'LLVM' or 'JSON', but was '"
<< OutputStyleOpt << "'\n";
More information about the llvm-commits
mailing list