llvm-dwarfdump: add a --show-parents options when selectively dumping DIEs.

llvm-svn: 313567
diff --git a/llvm/include/llvm/DebugInfo/DIContext.h b/llvm/include/llvm/DebugInfo/DIContext.h
index 0d9ea00..12093fe 100644
--- a/llvm/include/llvm/DebugInfo/DIContext.h
+++ b/llvm/include/llvm/DebugInfo/DIContext.h
@@ -139,6 +139,7 @@
 struct DIDumpOptions {
   unsigned DumpType = DIDT_All;
   bool ShowChildren = false;
+  bool ShowParents = false;
   bool SummarizeTypes = false;
   bool Verbose = false;
 };
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
index 5773498..f588f25 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
@@ -367,6 +367,16 @@
   CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
 }
 
+/// Helper to dump a DIE with all of its parents, but no siblings.
+static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
+                                DIDumpOptions DumpOpts) {
+  if (!Die)
+    return Indent;
+  Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
+  Die.dump(OS, 0, Indent, DumpOpts);
+  return Indent + 2;
+}
+
 void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth, unsigned Indent,
                     DIDumpOptions DumpOpts) const {
   if (!isValid())
@@ -374,7 +384,12 @@
   DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
   const uint32_t Offset = getOffset();
   uint32_t offset = Offset;
-  RecurseDepth += DumpOpts.ShowChildren ? 1 : 0;
+  if (DumpOpts.ShowChildren)
+    RecurseDepth++;
+  if (DumpOpts.ShowParents) {
+    DumpOpts.ShowParents = false;
+    Indent = dumpParentChain(getParent(), OS, Indent, DumpOpts);
+  }
 
   if (debug_info_data.isValidOffset(offset)) {
     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
diff --git a/llvm/test/tools/llvm-dwarfdump/X86/debug_info_offset.test b/llvm/test/tools/llvm-dwarfdump/X86/debug_info_offset.test
index caae816..59c8c7c 100644
--- a/llvm/test/tools/llvm-dwarfdump/X86/debug_info_offset.test
+++ b/llvm/test/tools/llvm-dwarfdump/X86/debug_info_offset.test
@@ -20,3 +20,18 @@
 CHILDREN: .debug_info contents:
 CHILDREN: 0x0000000b: DW_TAG_compile_unit
 CHILDREN: DW_TAG_subprogram
+
+RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \
+RUN:   | llvm-dwarfdump -debug-info=0x00000043 --show-parents - \
+RUN:   | FileCheck %s --check-prefix=PARENTS
+
+RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \
+RUN:   | llvm-dwarfdump -debug-info=0x00000043 -p - \
+RUN:   | FileCheck %s --check-prefix=PARENTS
+PARENTS: .debug_info contents:
+PARENTS: 0x0000000b:{{ }}DW_TAG_compile_unit
+PARENTS:                   DW_AT_name
+PARENTS-NOT: {{:}}
+PARENTS: 0x00000043:{{   }}DW_TAG_base_type
+PARENTS:                     DW_AT_name
+PARENTS-NOT: {{:}}
diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
index c2bcd38..b8d0f07 100644
--- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
+++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
@@ -131,10 +131,16 @@
 static opt<bool>
     ShowChildren("show-children",
                  desc("Show a debug info entry's children when selectively "
-                      "printing with the =<Offset> option"));
+                      "printing with the =<offset> option"));
 static alias ShowChildrenAlias("c", desc("Alias for -show-children"),
                                aliasopt(ShowChildren));
 static opt<bool>
+    ShowParents("show-parents",
+                desc("Show a debug info entry's parents when selectively "
+                     "printing with the =<offset> option"));
+static alias ShowParentsAlias("p", desc("Alias for -show-parents"),
+                              aliasopt(ShowParents));
+static opt<bool>
     SummarizeTypes("summarize-types",
                    desc("Abbreviate the description of type unit entries"));
 static opt<bool> Verify("verify", desc("Verify the DWARF debug info"),
@@ -162,6 +168,7 @@
   DIDumpOptions DumpOpts;
   DumpOpts.DumpType = DumpType;
   DumpOpts.ShowChildren = ShowChildren;
+  DumpOpts.ShowParents = ShowParents;
   DumpOpts.SummarizeTypes = SummarizeTypes;
   DumpOpts.Verbose = Verbose;
   return DumpOpts;