[llvm] Added --debugger-view option to llvm-debuginfo-analyzer (PR #159853)
Adam Yang via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 22 00:00:53 PDT 2025
https://github.com/adam-yang updated https://github.com/llvm/llvm-project/pull/159853
>From 85fc61b158a9d8ca93d633079a2bc9332b10ae3c Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Fri, 29 Aug 2025 19:55:34 -0700
Subject: [PATCH 1/3] Initial changes.
---
.../CodeGen/AMDGPU/amdgpu-debuginfo-check.ll | 115 ++++++++
.../llvm-debuginfo-analyzer/CMakeLists.txt | 1 +
.../llvm-debuginfo-analyzer/DebuggerView.cpp | 276 ++++++++++++++++++
.../llvm-debuginfo-analyzer/DebuggerView.h | 36 +++
.../llvm-debuginfo-analyzer.cpp | 6 +-
5 files changed, 433 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll
create mode 100644 llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
create mode 100644 llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll
new file mode 100644
index 0000000000000..e4fae7f1f8026
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll
@@ -0,0 +1,115 @@
+; RUN: llc %s -o %t.o -mcpu=gfx1030 -filetype=obj -O0
+; RUN: llvm-debuginfo-analyzer --check --check-vars %t.o | FileCheck %s
+
+; This test compiles this module with AMDGPU backend under -O0,
+; and makes sure llvm-debuginfo-check works for it.
+
+; CHECK: FUNCTION: main
+; CHECK: LINE: {{.+}}basic_var.hlsl:7
+; CHECK: LINE: {{.+}}basic_var.hlsl:11
+; CHECK: VAR: dtid: uint3 : reg{{.+}}, piece 4
+; CHECK: LINE: {{.+}}basic_var.hlsl:17
+; CHECK: VAR: dtid: uint3 : reg{{.+}}, piece 4
+; CHECK: LINE: {{.+}}basic_var.hlsl:11
+; CHECK: VAR: dtid: uint3 : reg{{.+}}, piece 4
+; CHECK: LINE: {{.+}}basic_var.hlsl:14
+; CHECK-DAG: VAR: dtid: uint3 : reg{{.+}}, piece 4
+; CHECK: LINE: {{.+}}basic_var.hlsl:17
+; CHECK-DAG: VAR: dtid: uint3 : reg{{.+}}, piece 4
+; CHECK-DAG: VAR: my_var2: float : reg{{.+}}
+; CHECK: LINE: {{.+}}basic_var.hlsl:19
+; CHECK-DAG: VAR: dtid: uint3 : reg{{.+}}, piece 4
+; CHECK-DAG: VAR: my_var2: float : reg{{.+}}
+
+source_filename = "module"
+target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-p10:32:32-p11:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9-p32:32:32-v8:8-v16:16-v32:32-v48:32-v64:32-v80:32-v96:32-v112:32-v128:32-v144:32-v160:32-v176:32-v192:32-v208:32-v224:32-v240:32-v256:32-i1:32-i8:8-i16:16-i32:32-i64:32-f16:16-f32:32-f64:32"
+target triple = "amdgcn-amd-amdpal"
+
+%dx.types.ResRet.f32 = type { float, float, float, float, i32 }
+
+; Function Attrs: memory(readwrite)
+define dllexport amdgpu_cs void @_amdgpu_cs_main(i32 inreg noundef %globalTable, i32 inreg noundef %userdata4, <3 x i32> inreg noundef %WorkgroupId, i32 inreg noundef %MultiDispatchInfo, <3 x i32> noundef %LocalInvocationId) #0 !dbg !14 {
+ %LocalInvocationId.i0 = extractelement <3 x i32> %LocalInvocationId, i64 0, !dbg !28
+ %WorkgroupId.i0 = extractelement <3 x i32> %WorkgroupId, i64 0, !dbg !28
+ %1 = call i64 @llvm.amdgcn.s.getpc(), !dbg !28
+ %2 = shl i32 %WorkgroupId.i0, 6, !dbg !28
+ %3 = add i32 %LocalInvocationId.i0, %2, !dbg !28
+ #dbg_value(i32 %3, !29, !DIExpression(DW_OP_LLVM_fragment, 0, 32), !28)
+ %4 = and i64 %1, -4294967296, !dbg !30
+ %5 = zext i32 %userdata4 to i64, !dbg !30
+ %6 = or disjoint i64 %4, %5, !dbg !30
+ %7 = inttoptr i64 %6 to ptr addrspace(4), !dbg !30
+ call void @llvm.assume(i1 true) [ "align"(ptr addrspace(4) %7, i32 4), "dereferenceable"(ptr addrspace(4) %7, i32 -1) ], !dbg !30
+ %8 = load <4 x i32>, ptr addrspace(4) %7, align 4, !dbg !30, !invariant.load !2
+ %9 = call float @llvm.amdgcn.struct.buffer.load.format.f32(<4 x i32> %8, i32 %3, i32 0, i32 0, i32 0), !dbg !30
+ #dbg_value(%dx.types.ResRet.f32 poison, !31, !DIExpression(), !32)
+ %10 = fmul reassoc arcp contract afn float %9, 2.000000e+00, !dbg !33
+ #dbg_value(float %10, !34, !DIExpression(), !35)
+ call void @llvm.assume(i1 true) [ "align"(ptr addrspace(4) %7, i32 4), "dereferenceable"(ptr addrspace(4) %7, i32 -1) ], !dbg !36
+ %11 = getelementptr i8, ptr addrspace(4) %7, i64 32, !dbg !36
+ %.upto01 = insertelement <4 x float> poison, float %10, i64 0, !dbg !36
+ %12 = shufflevector <4 x float> %.upto01, <4 x float> poison, <4 x i32> zeroinitializer, !dbg !36
+ %13 = load <4 x i32>, ptr addrspace(4) %11, align 4, !dbg !36, !invariant.load !2
+ call void @llvm.amdgcn.struct.buffer.store.format.v4f32(<4 x float> %12, <4 x i32> %13, i32 %3, i32 0, i32 0, i32 0), !dbg !36
+ ret void, !dbg !37
+}
+
+; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
+declare noundef i64 @llvm.amdgcn.s.getpc() #1
+
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
+declare void @llvm.assume(i1 noundef) #2
+
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(write)
+declare void @llvm.amdgcn.struct.buffer.store.format.v4f32(<4 x float>, <4 x i32>, i32, i32, i32, i32 immarg) #3
+
+; Function Attrs: nocallback nofree nosync nounwind willreturn memory(read)
+declare float @llvm.amdgcn.struct.buffer.load.format.f32(<4 x i32>, i32, i32, i32, i32 immarg) #4
+
+attributes #0 = { memory(readwrite) "amdgpu-flat-work-group-size"="64,64" "amdgpu-memory-bound"="false" "amdgpu-num-sgpr"="4294967295" "amdgpu-num-vgpr"="4294967295" "amdgpu-prealloc-sgpr-spill-vgprs" "amdgpu-unroll-threshold"="1200" "amdgpu-wave-limiter"="false" "amdgpu-work-group-info-arg-no"="3" "denormal-fp-math"="ieee" "denormal-fp-math-f32"="preserve-sign" "target-features"=",+wavefrontsize64,+cumode,+enable-flat-scratch" }
+attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
+attributes #2 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
+attributes #3 = { nocallback nofree nosync nounwind willreturn memory(write) }
+attributes #4 = { nocallback nofree nosync nounwind willreturn memory(read) }
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!12, !13}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "dxcoob 1.7.2308.16 (52da17e29)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3)
+!1 = !DIFile(filename: "tests\\basic_var.hlsl", directory: "")
+!2 = !{}
+!3 = !{!4, !10}
+!4 = distinct !DIGlobalVariableExpression(var: !5, expr: !DIExpression())
+!5 = !DIGlobalVariable(name: "u0", linkageName: "\01?u0@@3V?$RWBuffer at M@@A", scope: !0, file: !1, line: 2, type: !6, isLocal: false, isDefinition: true)
+!6 = !DICompositeType(tag: DW_TAG_class_type, name: "RWBuffer<float>", file: !1, line: 2, size: 32, align: 32, elements: !2, templateParams: !7)
+!7 = !{!8}
+!8 = !DITemplateTypeParameter(name: "element", type: !9)
+!9 = !DIBasicType(name: "float", size: 32, align: 32, encoding: DW_ATE_float)
+!10 = distinct !DIGlobalVariableExpression(var: !11, expr: !DIExpression())
+!11 = !DIGlobalVariable(name: "u1", linkageName: "\01?u1@@3V?$RWBuffer at M@@A", scope: !0, file: !1, line: 3, type: !6, isLocal: false, isDefinition: true)
+!12 = !{i32 2, !"Dwarf Version", i32 5}
+!13 = !{i32 2, !"Debug Info Version", i32 3}
+!14 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !15, scopeLine: 7, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
+!15 = !DISubroutineType(types: !16)
+!16 = !{null, !17}
+!17 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint3", file: !1, baseType: !18)
+!18 = !DICompositeType(tag: DW_TAG_class_type, name: "vector<unsigned int, 3>", file: !1, size: 96, align: 32, elements: !19, templateParams: !24)
+!19 = !{!20, !22, !23}
+!20 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !18, file: !1, baseType: !21, size: 32, align: 32, flags: DIFlagPublic)
+!21 = !DIBasicType(name: "unsigned int", size: 32, align: 32, encoding: DW_ATE_unsigned)
+!22 = !DIDerivedType(tag: DW_TAG_member, name: "y", scope: !18, file: !1, baseType: !21, size: 32, align: 32, offset: 32, flags: DIFlagPublic)
+!23 = !DIDerivedType(tag: DW_TAG_member, name: "z", scope: !18, file: !1, baseType: !21, size: 32, align: 32, offset: 64, flags: DIFlagPublic)
+!24 = !{!25, !26}
+!25 = !DITemplateTypeParameter(name: "element", type: !21)
+!26 = !DITemplateValueParameter(name: "element_count", type: !27, value: i32 3)
+!27 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
+!28 = !DILocation(line: 7, column: 17, scope: !14)
+!29 = !DILocalVariable(name: "dtid", arg: 1, scope: !14, file: !1, line: 7, type: !17)
+!30 = !DILocation(line: 11, column: 18, scope: !14)
+!31 = !DILocalVariable(name: "my_var", scope: !14, file: !1, line: 11, type: !9)
+!32 = !DILocation(line: 11, column: 9, scope: !14)
+!33 = !DILocation(line: 14, column: 26, scope: !14)
+!34 = !DILocalVariable(name: "my_var2", scope: !14, file: !1, line: 14, type: !9)
+!35 = !DILocation(line: 14, column: 9, scope: !14)
+!36 = !DILocation(line: 17, column: 14, scope: !14)
+!37 = !DILocation(line: 19, column: 1, scope: !14)
\ No newline at end of file
diff --git a/llvm/tools/llvm-debuginfo-analyzer/CMakeLists.txt b/llvm/tools/llvm-debuginfo-analyzer/CMakeLists.txt
index 3e16d81abe35c..9de3078ffee55 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/CMakeLists.txt
+++ b/llvm/tools/llvm-debuginfo-analyzer/CMakeLists.txt
@@ -15,4 +15,5 @@ set(LLVM_LINK_COMPONENTS
add_llvm_tool(llvm-debuginfo-analyzer
llvm-debuginfo-analyzer.cpp
Options.cpp
+ DebuggerView.cpp
)
diff --git a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
new file mode 100644
index 0000000000000..0cfb7086d8a24
--- /dev/null
+++ b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
@@ -0,0 +1,276 @@
+#include "Options.h"
+#include "llvm/ADT/SetVector.h"
+#include "llvm/DebugInfo/LogicalView/LVReaderHandler.h"
+
+#include <string>
+#include <unordered_map>
+#include <unordered_set>
+
+using namespace llvm;
+
+cl::opt<bool> EnableDebuggerView("debugger-view");
+static cl::opt<bool> DebuggerViewVars("debugger-view-vars");
+static cl::opt<bool> DebuggerViewCode("debugger-view-code");
+static cl::opt<bool> DebuggerViewHelp("debugger-view-help");
+
+constexpr const char *HelpText =
+ R"(Prints debug info in a way that is easy to verify correctness of debug info.
+FUNCTION: main
+ LINE: my_source_file.c:1 [main] <---- New statement lines, inlined callstack
+ VAR: argc : int : {expression} <---- Variables live at this point
+ VAR: argv : char ** : {expression}
+ LINE: my_source_file.c:2 [main]
+ VAR: argc : int
+ VAR: argv : char **
+ LINE: my_source_file.c:3 [main]
+ VAR: argc : int
+ VAR: argv : char **
+ LINE: my_source_file.c:4 [main]
+)";
+
+using namespace llvm;
+using namespace logicalview;
+
+static cl::opt<std::string>
+ InputFilename(cl::Positional, "<input-file>",
+ cl::desc("Input file, an object file with DWARF."),
+ cl::Required);
+
+static cl::opt<bool> IncludeCode("code", cl::desc("Include asm"));
+static cl::opt<bool>
+ IncludeRanges("ranges", cl::desc("Include variable ranges"), cl::Hidden);
+static cl::opt<bool> IncludeVars("vars", cl::desc("Include live variables"));
+
+template <typename T> T Take(Expected<T> ExpectedResult, const Twine &Msg) {
+ if (!ExpectedResult) {
+ auto Err = ExpectedResult.takeError();
+ errs() << Msg << " " << toStringWithoutConsuming(Err) << '\n';
+ exit(2);
+ }
+ T ret = std::move(*ExpectedResult);
+ return ret;
+}
+
+struct ScopePrinter {
+ std::vector<const LVLine *> Lines;
+ std::unordered_map<LVAddress, std::vector<const LVLocation *>> LivetimeBegins;
+ std::unordered_map<LVAddress, std::vector<const LVLocation *>>
+ LivetimeEndsExclusive;
+ raw_ostream &OS;
+
+ void Walk(raw_ostream &OS, const LVScope *Scope) {
+ if (Scope->scopeCount()) {
+ for (const LVScope *ChildScope : *Scope->getScopes())
+ Walk(OS, ChildScope);
+ }
+ if (Scope->lineCount()) {
+ for (const LVLine *Line : *Scope->getLines()) {
+ Lines.push_back(Line);
+ }
+ }
+ if (Scope->symbolCount()) {
+ for (const LVSymbol *Symbol : *Scope->getSymbols()) {
+ LVLocations SymbolLocations;
+ Symbol->getLocations(SymbolLocations);
+ if (SymbolLocations.empty())
+ continue;
+
+ if (IncludeRanges) {
+ OS << "RANGES: " << Symbol->getName() << " (line "
+ << Symbol->getLineNumber() << ")" << ": ";
+ }
+
+ for (const LVLocation *Loc : SymbolLocations) {
+ if (Loc->getIsGapEntry())
+ continue;
+
+ LVAddress Begin = Loc->getLowerAddress();
+ LVAddress End = Loc->getUpperAddress();
+ LivetimeBegins[Begin].push_back(Loc);
+ LivetimeEndsExclusive[End].push_back(Loc);
+ if (IncludeRanges) {
+ OS << "[" << hexValue(Begin) << ":" << hexValue(End) << "] ";
+ }
+ }
+
+ if (IncludeRanges)
+ OS << "\n";
+ }
+ }
+ }
+
+ ScopePrinter(raw_ostream &OS, const LVScopeFunction *Fn) : OS(OS) {
+ Walk(OS, Fn);
+ std::sort(Lines.begin(), Lines.end(),
+ [](const LVLine *a, const LVLine *b) -> bool {
+ if (a->getAddress() != b->getAddress())
+ return a->getAddress() < b->getAddress();
+ if (a->getIsLineDebug() != b->getIsLineDebug())
+ return a->getIsLineDebug();
+ return a->getID() < b->getID();
+ });
+ }
+
+ static void PrintIndent(raw_ostream &OS, int Indent) {
+ for (int i = 0; i < Indent; i++)
+ OS << " ";
+ }
+
+ static void PrintCallstack(raw_ostream &OS, const LVScope *Scope) {
+ bool First = true;
+ const LVScope *PrevScope = nullptr;
+ while (Scope) {
+ if (Scope->getIsFunction() || Scope->getIsInlinedFunction()) {
+ OS << "[" << Scope->getName();
+ if (PrevScope && PrevScope->getIsInlinedFunction()) {
+ OS << ":"
+ << cast<LVScopeFunctionInlined>(PrevScope)->getCallLineNumber();
+ }
+ OS << "]";
+ First = false;
+ PrevScope = Scope;
+ }
+ Scope = Scope->getParentScope();
+ }
+ }
+
+ static bool IsChildScopeOf(const LVScope *A, const LVScope *B) {
+ while (A) {
+ A = A->getParentScope();
+ if (A == B)
+ return true;
+ }
+ return false;
+ }
+
+ void Print() {
+ SetVector<const LVLocation *>
+ LiveSymbols; // This needs to be ordered since we're iterating over it.
+ for (const LVLine *Line : Lines) {
+
+ const LVScope *Scope = Line->getParentScope();
+
+ // Update live list: Add lives
+ for (auto Loc : LivetimeBegins[Line->getAddress()])
+ LiveSymbols.insert(Loc);
+ // Update live list: remove dead
+ for (auto Loc : LivetimeEndsExclusive[Line->getAddress()])
+ LiveSymbols.remove(Loc);
+
+ if (Line->getIsNewStatement() && Line->getIsLineDebug() &&
+ Line->getLineNumber() != 0) {
+ auto LineDebug = cast<LVLineDebug>(Line);
+
+ OS << "LINE: " << " [" << hexValue(LineDebug->getAddress()) << "] "
+ << LineDebug->getPathname() << ":" << LineDebug->getLineNumber()
+ << " ";
+ PrintCallstack(OS, Scope);
+ OS << "\n";
+ if (IncludeVars) {
+ for (auto SymLoc : LiveSymbols) {
+ const LVSymbol *Sym = SymLoc->getParentSymbol();
+ auto SymScope = Sym->getParentScope();
+ auto LineScope = LineDebug->getParentScope();
+ if (SymScope != LineScope && !IsChildScopeOf(LineScope, SymScope))
+ continue;
+ PrintIndent(OS, 1);
+ OS << "VAR: " << Sym->getName() << ": " << Sym->getType()->getName()
+ << " : ";
+ SymLoc->printLocations(OS);
+ OS << " (line " << Sym->getLineNumber() << ")";
+ OS << "\n";
+ }
+ }
+
+ } else if (IncludeCode && Line->getIsLineAssembler()) {
+ OS << " CODE: " << " [" << hexValue(Line->getAddress()) << "] "
+ << Line->getName() << "\n";
+ }
+ }
+ }
+};
+#if 0
+int main(int argc, char *argv[]) {
+ InitLLVM X(argc, argv);
+
+ // Initialize targets and assembly printers/parsers.
+ llvm::InitializeAllTargetInfos();
+ llvm::InitializeAllTargetMCs();
+ InitializeAllDisassemblers();
+
+ cl::ParseCommandLineOptions(argc, argv, HelpText);
+
+ ScopedPrinter W(llvm::outs());
+ LVOptions Options;
+ Options.setAttributeAll();
+ Options.setAttributeAnyLocation();
+ Options.setPrintAll();
+ Options.setPrintAnyLine();
+ Options.resolveDependencies();
+ std::vector<std::string> Objects;
+ LVReaderHandler Handler(Objects, W, Options);
+ auto Readers = Take(Handler.createReader(InputFilename),
+ Twine("Failed to create LV reader from '") +
+ Twine(InputFilename) + Twine("'"));
+
+ auto *CU = Readers->getCompileUnit();
+ if (!CU) {
+ errs() << "No compute unit found.\n";
+ return 2;
+ }
+
+ for (LVElement *Child : *CU->getChildren()) {
+ auto *Fn = dyn_cast<LVScopeFunction>(Child);
+ if (Fn) {
+ const LVLines *Lines = Fn->getLines();
+ // If there's no lines, this function has no body.
+ if (!Lines)
+ continue;
+ outs() << "FUNCTION: " << Child->getName() << "\n";
+
+ ScopePrinter P(outs(), Fn);
+ P.Print();
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
+#endif
+
+int llvm::debuggerview::printDebuggerView(std::vector<std::string> &Objects, raw_ostream &OS) {
+
+ LVOptions Options;
+ Options.setAttributeAll();
+ Options.setAttributeAnyLocation();
+ Options.setPrintAll();
+ Options.setPrintAnyLine();
+ Options.resolveDependencies();
+
+ ScopedPrinter W(nulls());
+ LVReaderHandler Handler(Objects, W, Options);
+ auto Readers = Take(Handler.createReader(InputFilename),
+ Twine("Failed to create LV reader from '") +
+ Twine(InputFilename) + Twine("'"));
+
+ auto *CU = Readers->getCompileUnit();
+ if (!CU) {
+ errs() << "No compute unit found.\n";
+ return 2;
+ }
+
+ for (LVElement *Child : *CU->getChildren()) {
+ auto *Fn = dyn_cast<LVScopeFunction>(Child);
+ if (Fn) {
+ const LVLines *Lines = Fn->getLines();
+ // If there's no lines, this function has no body.
+ if (!Lines)
+ continue;
+ outs() << "FUNCTION: " << Child->getName() << "\n";
+
+ ScopePrinter P(OS, Fn);
+ P.Print();
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
new file mode 100644
index 0000000000000..9b54122ce22dd
--- /dev/null
+++ b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
@@ -0,0 +1,36 @@
+//===-- DebuggerView.h ------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef DEBUGGER_VIEW_H
+#define DEBUGGER_VIEW_H
+
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/CommandLine.h"
+#include <vector>
+
+namespace llvm {
+class ScopedPrinter;
+class raw_ostream;
+
+namespace logicalview {
+class LVReaderHandler;
+class LVOptions;
+} // namespace logicalview
+
+namespace debuggerview {
+
+extern cl::opt<bool> EnableDebuggerView;
+int printDebuggerView(std::vector<std::string> &Objects, raw_ostream &OS);
+
+} // namespace debuggerview
+} // namespace llvm
+#endif // DEBUGGER_VIEW_H
diff --git a/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp b/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
index a5e2d39f67d7f..47157796eb910 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
+++ b/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "Options.h"
+#include "DebuggerView.h"
#include "llvm/DebugInfo/LogicalView/Core/LVOptions.h"
#include "llvm/DebugInfo/LogicalView/LVReaderHandler.h"
#include "llvm/Support/COM.h"
@@ -120,8 +121,11 @@ int main(int argc, char **argv) {
llvm::append_range(Objects, Objs);
}
- propagateOptions();
+ if (debuggerview::EnableDebuggerView)
+ return debuggerview::printDebuggerView(Objects, OutputFile.os());
+
ScopedPrinter W(OutputFile.os());
+ propagateOptions();
LVReaderHandler ReaderHandler(Objects, W, ReaderOptions);
// Print the command line.
>From c9c8ba5a511c402331c50cf87158d9ee29e9e5c5 Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Fri, 19 Sep 2025 14:44:47 -0700
Subject: [PATCH 2/3] Cleaned up and hooked up the options
---
.../DebugInfo/LogicalView/Core/LVLocation.h | 2 +
.../DebugInfo/LogicalView/LVReaderHandler.h | 2 +
.../DebugInfo/LogicalView/Core/LVLocation.cpp | 25 +--
.../CodeGen/AMDGPU/amdgpu-debuginfo-check.ll | 4 +-
.../llvm-debuginfo-analyzer/DebuggerView.cpp | 146 ++++++++----------
.../llvm-debuginfo-analyzer/DebuggerView.h | 3 +-
.../llvm-debuginfo-analyzer.cpp | 9 +-
7 files changed, 95 insertions(+), 96 deletions(-)
diff --git a/llvm/include/llvm/DebugInfo/LogicalView/Core/LVLocation.h b/llvm/include/llvm/DebugInfo/LogicalView/Core/LVLocation.h
index 0718e33f5645b..c55b8259b4ee8 100644
--- a/llvm/include/llvm/DebugInfo/LogicalView/Core/LVLocation.h
+++ b/llvm/include/llvm/DebugInfo/LogicalView/Core/LVLocation.h
@@ -157,6 +157,7 @@ class LLVM_ABI LVLocation : public LVObject {
void printRaw(raw_ostream &OS, bool Full = true) const;
virtual void printRawExtra(raw_ostream &OS, bool Full = true) const {}
+ virtual void printLocations(raw_ostream &OS) const {}
void print(raw_ostream &OS, bool Full = true) const override;
void printExtra(raw_ostream &OS, bool Full = true) const override;
};
@@ -177,6 +178,7 @@ class LLVM_ABI LVLocationSymbol final : public LVLocation {
uint64_t LocDescOffset) override;
void addObject(LVSmall Opcode, ArrayRef<LVUnsigned> Operands) override;
+ void printLocations(raw_ostream &OS) const override;
void printRawExtra(raw_ostream &OS, bool Full = true) const override;
void printExtra(raw_ostream &OS, bool Full = true) const override;
};
diff --git a/llvm/include/llvm/DebugInfo/LogicalView/LVReaderHandler.h b/llvm/include/llvm/DebugInfo/LogicalView/LVReaderHandler.h
index f6dc65f39c7ac..aac1f21338eac 100644
--- a/llvm/include/llvm/DebugInfo/LogicalView/LVReaderHandler.h
+++ b/llvm/include/llvm/DebugInfo/LogicalView/LVReaderHandler.h
@@ -87,6 +87,8 @@ class LVReaderHandler {
return std::move(Readers[0]);
}
+ const LVReaders &getReaders() const { return TheReaders; }
+
LLVM_ABI void print(raw_ostream &OS) const;
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
diff --git a/llvm/lib/DebugInfo/LogicalView/Core/LVLocation.cpp b/llvm/lib/DebugInfo/LogicalView/Core/LVLocation.cpp
index 3c078d8ee74b8..4724f4f007163 100644
--- a/llvm/lib/DebugInfo/LogicalView/Core/LVLocation.cpp
+++ b/llvm/lib/DebugInfo/LogicalView/Core/LVLocation.cpp
@@ -648,6 +648,19 @@ void LVLocation::print(LVLocations *Locations, raw_ostream &OS, bool Full) {
Location->print(OS, Full);
}
+void LVLocationSymbol::printLocations(raw_ostream &OS) const {
+ if (Entries) {
+ bool CodeViewLocation = getParentSymbol()->getHasCodeViewLocation();
+ std::string Leading;
+ for (LVOperation *Operation : *Entries) {
+ OS << Leading
+ << (CodeViewLocation ? Operation->getOperandsCodeViewInfo()
+ : Operation->getOperandsDWARFInfo());
+ Leading = ", ";
+ }
+ }
+}
+
void LVLocationSymbol::printExtra(raw_ostream &OS, bool Full) const {
OS << "{Location}";
if (getIsCallSite())
@@ -657,15 +670,9 @@ void LVLocationSymbol::printExtra(raw_ostream &OS, bool Full) const {
// Print location entries.
if (Full && Entries) {
- bool CodeViewLocation = getParentSymbol()->getHasCodeViewLocation();
- std::stringstream Stream;
- std::string Leading;
- for (LVOperation *Operation : *Entries) {
- Stream << Leading
- << (CodeViewLocation ? Operation->getOperandsCodeViewInfo()
- : Operation->getOperandsDWARFInfo());
- Leading = ", ";
- }
+ std::string Str;
+ raw_string_ostream Stream(Str);
+ printLocations(Stream);
printAttributes(OS, Full, "{Entry} ", const_cast<LVLocationSymbol *>(this),
StringRef(Stream.str()),
/*UseQuotes=*/false,
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll
index e4fae7f1f8026..51d347fc5d7d2 100644
--- a/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-debuginfo-check.ll
@@ -1,5 +1,5 @@
; RUN: llc %s -o %t.o -mcpu=gfx1030 -filetype=obj -O0
-; RUN: llvm-debuginfo-analyzer --check --check-vars %t.o | FileCheck %s
+; RUN: llvm-debuginfo-analyzer --debugger-view --debugger-view-vars %t.o | FileCheck %s
; This test compiles this module with AMDGPU backend under -O0,
; and makes sure llvm-debuginfo-check works for it.
@@ -112,4 +112,4 @@ attributes #4 = { nocallback nofree nosync nounwind willreturn memory(read) }
!34 = !DILocalVariable(name: "my_var2", scope: !14, file: !1, line: 14, type: !9)
!35 = !DILocation(line: 14, column: 9, scope: !14)
!36 = !DILocation(line: 17, column: 14, scope: !14)
-!37 = !DILocation(line: 19, column: 1, scope: !14)
\ No newline at end of file
+!37 = !DILocation(line: 19, column: 1, scope: !14)
diff --git a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
index 0cfb7086d8a24..69d50c37033d6 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
+++ b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
@@ -1,3 +1,4 @@
+#include "DebuggerView.h"
#include "Options.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/DebugInfo/LogicalView/LVReaderHandler.h"
@@ -7,11 +8,33 @@
#include <unordered_set>
using namespace llvm;
+using namespace debuggerview;
-cl::opt<bool> EnableDebuggerView("debugger-view");
-static cl::opt<bool> DebuggerViewVars("debugger-view-vars");
-static cl::opt<bool> DebuggerViewCode("debugger-view-code");
-static cl::opt<bool> DebuggerViewHelp("debugger-view-help");
+cl::OptionCategory llvm::debuggerview::Category(
+ "Debugger View",
+ "Special printing mode that emulate how debugger uses debug info.");
+
+cl::opt<bool> llvm::debuggerview::Enable(
+ "debugger-view",
+ cl::desc("Enables debugger view. Normal debug-info printing is disabled "
+ "and options are ignored."),
+ cl::init(false), cl::cat(Category));
+static cl::opt<bool>
+ IncludeVars("debugger-view-vars",
+ cl::desc("Include live variables at each statement line."),
+ cl::init(false), cl::cat(Category));
+static cl::opt<bool>
+ IncludeCode("debugger-view-code",
+ cl::desc("Include disassembly at each statement line"),
+ cl::init(false), cl::cat(Category));
+static cl::opt<bool> IncludeRanges("debugger-view-ranges",
+ cl::desc("Include variable ranges"),
+ cl::init(false), cl::cat(Category));
+static cl::opt<bool> Help(
+ "debugger-view-help",
+ cl::desc(
+ "Print a detailed help screen about what kind of output to expect"),
+ cl::init(false), cl::cat(Category));
constexpr const char *HelpText =
R"(Prints debug info in a way that is easy to verify correctness of debug info.
@@ -31,17 +54,8 @@ FUNCTION: main
using namespace llvm;
using namespace logicalview;
-static cl::opt<std::string>
- InputFilename(cl::Positional, "<input-file>",
- cl::desc("Input file, an object file with DWARF."),
- cl::Required);
-
-static cl::opt<bool> IncludeCode("code", cl::desc("Include asm"));
-static cl::opt<bool>
- IncludeRanges("ranges", cl::desc("Include variable ranges"), cl::Hidden);
-static cl::opt<bool> IncludeVars("vars", cl::desc("Include live variables"));
-
-template <typename T> T Take(Expected<T> ExpectedResult, const Twine &Msg) {
+template <typename T>
+static T Take(Expected<T> ExpectedResult, const Twine &Msg) {
if (!ExpectedResult) {
auto Err = ExpectedResult.takeError();
errs() << Msg << " " << toStringWithoutConsuming(Err) << '\n';
@@ -51,6 +65,8 @@ template <typename T> T Take(Expected<T> ExpectedResult, const Twine &Msg) {
return ret;
}
+namespace {
+
struct ScopePrinter {
std::vector<const LVLine *> Lines;
std::unordered_map<LVAddress, std::vector<const LVLocation *>> LivetimeBegins;
@@ -189,56 +205,16 @@ struct ScopePrinter {
}
}
};
-#if 0
-int main(int argc, char *argv[]) {
- InitLLVM X(argc, argv);
- // Initialize targets and assembly printers/parsers.
- llvm::InitializeAllTargetInfos();
- llvm::InitializeAllTargetMCs();
- InitializeAllDisassemblers();
-
- cl::ParseCommandLineOptions(argc, argv, HelpText);
-
- ScopedPrinter W(llvm::outs());
- LVOptions Options;
- Options.setAttributeAll();
- Options.setAttributeAnyLocation();
- Options.setPrintAll();
- Options.setPrintAnyLine();
- Options.resolveDependencies();
- std::vector<std::string> Objects;
- LVReaderHandler Handler(Objects, W, Options);
- auto Readers = Take(Handler.createReader(InputFilename),
- Twine("Failed to create LV reader from '") +
- Twine(InputFilename) + Twine("'"));
-
- auto *CU = Readers->getCompileUnit();
- if (!CU) {
- errs() << "No compute unit found.\n";
- return 2;
- }
+} // namespace
- for (LVElement *Child : *CU->getChildren()) {
- auto *Fn = dyn_cast<LVScopeFunction>(Child);
- if (Fn) {
- const LVLines *Lines = Fn->getLines();
- // If there's no lines, this function has no body.
- if (!Lines)
- continue;
- outs() << "FUNCTION: " << Child->getName() << "\n";
-
- ScopePrinter P(outs(), Fn);
- P.Print();
- }
+int llvm::debuggerview::printDebuggerView(std::vector<std::string> &Objects,
+ raw_ostream &OS) {
+ if (Help) {
+ OS << HelpText;
+ return EXIT_SUCCESS;
}
- return EXIT_SUCCESS;
-}
-#endif
-
-int llvm::debuggerview::printDebuggerView(std::vector<std::string> &Objects, raw_ostream &OS) {
-
LVOptions Options;
Options.setAttributeAll();
Options.setAttributeAnyLocation();
@@ -248,27 +224,37 @@ int llvm::debuggerview::printDebuggerView(std::vector<std::string> &Objects, raw
ScopedPrinter W(nulls());
LVReaderHandler Handler(Objects, W, Options);
- auto Readers = Take(Handler.createReader(InputFilename),
- Twine("Failed to create LV reader from '") +
- Twine(InputFilename) + Twine("'"));
-
- auto *CU = Readers->getCompileUnit();
- if (!CU) {
- errs() << "No compute unit found.\n";
- return 2;
+ std::vector<std::unique_ptr<LVReader>> Readers;
+ for (auto &Object : Objects) {
+ auto ExpectedReader = Handler.createReader(Object);
+ if (!ExpectedReader) {
+ auto Err = ExpectedReader.takeError();
+ errs() << "Failed to create reader: " << toStringWithoutConsuming(Err)
+ << '\n';
+ return 2;
+ }
+ Readers.emplace_back(std::move(*ExpectedReader));
}
- for (LVElement *Child : *CU->getChildren()) {
- auto *Fn = dyn_cast<LVScopeFunction>(Child);
- if (Fn) {
- const LVLines *Lines = Fn->getLines();
- // If there's no lines, this function has no body.
- if (!Lines)
- continue;
- outs() << "FUNCTION: " << Child->getName() << "\n";
-
- ScopePrinter P(OS, Fn);
- P.Print();
+ for (auto &Reader : Readers) {
+ auto *CU = Reader->getCompileUnit();
+ if (!CU) {
+ errs() << "No compute unit found.\n";
+ return 2;
+ }
+
+ for (LVElement *Child : *CU->getChildren()) {
+ auto *Fn = dyn_cast<LVScopeFunction>(Child);
+ if (Fn) {
+ const LVLines *Lines = Fn->getLines();
+ // If there's no lines, this function has no body.
+ if (!Lines)
+ continue;
+ outs() << "FUNCTION: " << Child->getName() << "\n";
+
+ ScopePrinter P(OS, Fn);
+ P.Print();
+ }
}
}
diff --git a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
index 9b54122ce22dd..bf358c314ed2a 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
+++ b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
@@ -28,7 +28,8 @@ class LVOptions;
namespace debuggerview {
-extern cl::opt<bool> EnableDebuggerView;
+extern cl::OptionCategory Category;
+extern cl::opt<bool> Enable;
int printDebuggerView(std::vector<std::string> &Objects, raw_ostream &OS);
} // namespace debuggerview
diff --git a/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp b/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
index 47157796eb910..31d8774b46702 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
+++ b/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
@@ -96,9 +96,10 @@ int main(int argc, char **argv) {
cl::extrahelp HelpResponse(
"\nPass @FILE as argument to read options from FILE.\n");
- cl::HideUnrelatedOptions(
- {&AttributeCategory, &CompareCategory, &InternalCategory, &OutputCategory,
- &PrintCategory, &ReportCategory, &SelectCategory, &WarningCategory});
+ cl::HideUnrelatedOptions({&AttributeCategory, &CompareCategory,
+ &InternalCategory, &OutputCategory, &PrintCategory,
+ &ReportCategory, &SelectCategory, &WarningCategory,
+ &debuggerview::Category});
cl::ParseCommandLineOptions(argc, argv,
"Printing a logical representation of low-level "
"debug information.\n");
@@ -121,7 +122,7 @@ int main(int argc, char **argv) {
llvm::append_range(Objects, Objs);
}
- if (debuggerview::EnableDebuggerView)
+ if (debuggerview::Enable)
return debuggerview::printDebuggerView(Objects, OutputFile.os());
ScopedPrinter W(OutputFile.os());
>From b7b8a42565dadc5085b34bc709bc732aca21f6dd Mon Sep 17 00:00:00 2001
From: Adam Yang <hanbyang at microsoft.com>
Date: Mon, 22 Sep 2025 00:00:38 -0700
Subject: [PATCH 3/3] formatting
---
llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp | 12 ++++++++++++
llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h | 2 +-
.../llvm-debuginfo-analyzer.cpp | 2 +-
3 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
index 69d50c37033d6..3408751496e44 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
+++ b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.cpp
@@ -1,3 +1,15 @@
+//===-- DebuggerView.cpp ---------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Options and functions related to --debugger-view for llvm-debuginfo-analyzer
+//
+//===----------------------------------------------------------------------===//
+
#include "DebuggerView.h"
#include "Options.h"
#include "llvm/ADT/SetVector.h"
diff --git a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
index bf358c314ed2a..d775fa0c80d2d 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
+++ b/llvm/tools/llvm-debuginfo-analyzer/DebuggerView.h
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
//
-//
+// Options and functions related to --debugger-view for llvm-debuginfo-analyzer
//
//===----------------------------------------------------------------------===//
diff --git a/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp b/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
index 31d8774b46702..36b289411c044 100644
--- a/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
+++ b/llvm/tools/llvm-debuginfo-analyzer/llvm-debuginfo-analyzer.cpp
@@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//
-#include "Options.h"
#include "DebuggerView.h"
+#include "Options.h"
#include "llvm/DebugInfo/LogicalView/Core/LVOptions.h"
#include "llvm/DebugInfo/LogicalView/LVReaderHandler.h"
#include "llvm/Support/COM.h"
More information about the llvm-commits
mailing list