]> granicus.if.org Git - llvm/commitdiff
llvm-dwarfdump: implement the --name lookup option.
authorAdrian Prantl <aprantl@apple.com>
Sat, 30 Sep 2017 00:22:25 +0000 (00:22 +0000)
committerAdrian Prantl <aprantl@apple.com>
Sat, 30 Sep 2017 00:22:25 +0000 (00:22 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314576 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-dwarfdump/X86/find.test
test/tools/llvm-dwarfdump/X86/name.test [new file with mode: 0644]
test/tools/llvm-dwarfdump/cmdline.test
tools/llvm-dwarfdump/llvm-dwarfdump.cpp

index 2d308b7973deaf5247449a96d4fe998d28c99240..0606c674f9142e55907dc2c77b6071516b26db78 100644 (file)
@@ -12,7 +12,7 @@ CHECK-NOT: {{:}}
 CHECK:     DW_AT_name ("main")
 CHECK-NOT: {{:}}
 
-RUN: llvm-dwarfdump --debug-info %S/../../dsymutil/Inputs/libfat-test.a \
+RUN: llvm-dwarfdump %S/../../dsymutil/Inputs/libfat-test.a \
 RUN:   -find=x86_64h_var -find=i386_var \
 RUN:   | FileCheck %s --check-prefix=MULTI
 MULTI: .debug_info contents:
diff --git a/test/tools/llvm-dwarfdump/X86/name.test b/test/tools/llvm-dwarfdump/X86/name.test
new file mode 100644 (file)
index 0000000..d1cdc40
--- /dev/null
@@ -0,0 +1,39 @@
+RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \
+RUN:   | llvm-dwarfdump -name=not_there_at_all - | \
+RUN: FileCheck %s --check-prefix=EMPTY --allow-empty
+EMPTY: {{^$}}
+
+RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \
+RUN:   | llvm-dwarfdump -name=main - | FileCheck %s
+CHECK: : DW_TAG_subprogram
+CHECK-NOT: {{:}}
+CHECK:     DW_AT_name ("main")
+CHECK-NOT: {{:}}
+
+RUN: llvm-dwarfdump %S/../../dsymutil/Inputs/libfat-test.a \
+RUN:   -name=x86_64h_var -name=i386_var \
+RUN:   | FileCheck %s --check-prefix=MULTI
+MULTI: Mach-O 32-bit i386
+MULTI-NOT: {{: DW}}
+MULTI: : DW_TAG_variable
+MULTI-NOT: {{: DW}}
+MULTI:    DW_AT_name ("i386_var")
+MULTI-NOT: {{: DW}}
+MULTI: Mach-O 64-bit x86-64
+MULTI: : DW_TAG_variable
+MULTI-NOT: {{: DW}}
+MULTI:    DW_AT_name ("x86_64h_var")
+MULTI-NOT: {{: DW}}
+
+RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \
+RUN:   | llvm-dwarfdump -name=int - | FileCheck %s --check-prefix=TYPES
+TYPES: : DW_TAG_base_type
+TYPES-NOT: {{:}}
+TYPES:     DW_AT_name ("int")
+TYPES-NOT: {{:}}
+
+This is one where --name observably behaves different from --find.
+RUN: llvm-dwarfdump %S/../../dsymutil/Inputs/odr-anon-namespace/1.o \
+RUN:    -name="(anonymous namespace)" \
+RUN:   | FileCheck %s --check-prefix=EMPTY
+
index 467282457d02c7c98e6ee98deb64d81a87922a75..fd06f77dda3e4310305f10ff0cf9f30f6ee59666 100644 (file)
@@ -7,6 +7,7 @@ HELP: -debug-info            - Dump the .debug_info section
 HELP: -eh-frame
 HELP: Specific Options
 HELP: -find
+HELP: -name
 HELP: -recurse-depth=<N> 
 HELP: -show-children
 HELP: -show-parents
index c61169626c574cf00e1e0f7b153869314d861dae..28abe05e88962b1f755eca389a3b79d0d2983dff 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/Triple.h"
 #include "llvm/DebugInfo/DIContext.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
@@ -137,13 +138,17 @@ static list<std::string>
 static list<std::string>
     Find("find",
          desc("Search for the exact match for <name> in the accelerator tables "
-              "and print the matching debug information entries."),
+              "and print the matching debug information entries. When there no "
+              "accelerator tables avilable, the slower but more complete -name "
+              "option can be used."),
          value_desc("name"), cat(DwarfDumpCategory));
 static alias FindAlias("f", desc("Alias for -find"), aliasopt(Find));
-
-static opt<bool> DumpUUID("uuid", desc("Show the UUID for each architecture"),
-                          cat(DwarfDumpCategory));
-static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID));
+static list<std::string>
+    Name("name",
+         desc("Find and print all debug info entries whose name (DW_AT_name "
+              "attribute) matches the exact text in <name>."),
+         value_desc("name"), cat(DwarfDumpCategory));
+static alias NameAlias("n", desc("Alias for -name"), aliasopt(Name));
 static opt<std::string>
     OutputFilename("out-file", cl::init(""),
                    cl::desc("Redirect output to the specified file"),
@@ -180,6 +185,9 @@ static opt<bool> Verify("verify", desc("Verify the DWARF debug info"),
                         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"),
+                          cat(DwarfDumpCategory));
+static alias DumpUUIDAlias("u", desc("Alias for -uuid"), aliasopt(DumpUUID));
 static opt<bool> Verbose("verbose",
                          desc("Print more low-level encoding details"),
                          cat(DwarfDumpCategory));
@@ -244,11 +252,37 @@ static bool filterArch(ObjectFile &Obj) {
 using HandlerFn = std::function<bool(ObjectFile &, DWARFContext &DICtx, Twine,
                                      raw_ostream &)>;
 
+/// Print only DIEs that have a certain name.
+static void filterByName(const StringSet<> &Names,
+                         DWARFContext::cu_iterator_range CUs, raw_ostream &OS) {
+  for (const auto &CU : CUs)
+    for (const auto &Entry : CU->dies()) {
+      DWARFDie Die = {CU.get(), &Entry};
+      if (Names.count(Die.getName(DINameKind::ShortName)))
+        Die.dump(OS, 0, getDumpOpts());
+    }
+}
+
 static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename,
                            raw_ostream &OS) {
   logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(),
                         Filename.str() + ": ");
 
+  // The UUID dump already contains all the same information.
+  if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
+    OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
+
+  // Handle the --name option.
+  if (!Name.empty()) {
+    StringSet<> Names;
+    for (auto name : Name)
+      Names.insert(name);
+      
+    filterByName(Names, DICtx.compile_units(), OS);
+    filterByName(Names, DICtx.dwo_compile_units(), OS);
+    return true;
+  }
+
   // Handle the --find option and lower it to --debug-info=<offset>.
   if (!Find.empty()) {
     DumpOffsets[DIDT_ID_DebugInfo] = [&]() -> llvm::Optional<uint64_t> {
@@ -275,10 +309,6 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename,
       return true;
   }
   
-  // The UUID dump already contains all the same information.
-  if (!(DumpType & DIDT_UUID) || DumpType == DIDT_All)
-    OS << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
-
   // Dump the complete DWARF structure.
   DICtx.dump(OS, getDumpOpts(), DumpOffsets);
   return true;