]> granicus.if.org Git - llvm/commitdiff
[llvm-symbolizer] Add `--output-style` switch.
authorIgor Kudrin <ikudrin@accesssoftek.com>
Thu, 4 Apr 2019 08:39:40 +0000 (08:39 +0000)
committerIgor Kudrin <ikudrin@accesssoftek.com>
Thu, 4 Apr 2019 08:39:40 +0000 (08:39 +0000)
In general, llvm-symbolizer follows the output style of GNU's addr2line.
However, there are still some differences; in particular, for a requested
address, llvm-symbolizer prints line and column, while addr2line prints
only the line number.

This patch adds a new switch to select the preferred style.

Differential Revision: https://reviews.llvm.org/D60190

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357675 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/DebugInfo/Symbolize/DIPrinter.h
lib/DebugInfo/Symbolize/DIPrinter.cpp
test/tools/llvm-symbolizer/output-style.test [new file with mode: 0644]
tools/llvm-symbolizer/llvm-symbolizer.cpp

index 71663f30a59ff6314e277f9537ed9dfd4b3fd03a..8bc8dd2ef7e4b5df540ec1b92fe439112bebadf6 100644 (file)
@@ -24,12 +24,17 @@ struct DIGlobal;
 namespace symbolize {
 
 class DIPrinter {
+public:
+  enum class OutputStyle { LLVM, GNU };
+
+private:
   raw_ostream &OS;
   bool PrintFunctionNames;
   bool PrintPretty;
   int PrintSourceContext;
   bool Verbose;
   bool Basenames;
+  OutputStyle Style;
 
   void print(const DILineInfo &Info, bool Inlined);
   void printContext(const std::string &FileName, int64_t Line);
@@ -37,10 +42,11 @@ class DIPrinter {
 public:
   DIPrinter(raw_ostream &OS, bool PrintFunctionNames = true,
             bool PrintPretty = false, int PrintSourceContext = 0,
-            bool Verbose = false, bool Basenames = false)
+            bool Verbose = false, bool Basenames = false,
+            OutputStyle Style = OutputStyle::LLVM)
       : OS(OS), PrintFunctionNames(PrintFunctionNames),
         PrintPretty(PrintPretty), PrintSourceContext(PrintSourceContext),
-        Verbose(Verbose), Basenames(Basenames) {}
+        Verbose(Verbose), Basenames(Basenames), Style(Style) {}
 
   DIPrinter &operator<<(const DILineInfo &Info);
   DIPrinter &operator<<(const DIInliningInfo &Info);
index 18fe9bc610b765561f21a55a0874c47a7f51b474..f6c7ef84958bcf7ad774d6c0db9391f7188a8ed0 100644 (file)
@@ -81,7 +81,10 @@ void DIPrinter::print(const DILineInfo &Info, bool Inlined) {
   else if (Basenames)
     Filename = llvm::sys::path::filename(Filename);
   if (!Verbose) {
-    OS << Filename << ":" << Info.Line << ":" << Info.Column << "\n";
+    OS << Filename << ":" << Info.Line;
+    if (Style == OutputStyle::LLVM)
+      OS << ":" << Info.Column;
+    OS << "\n";
     printContext(Filename, Info.Line);
     return;
   }
diff --git a/test/tools/llvm-symbolizer/output-style.test b/test/tools/llvm-symbolizer/output-style.test
new file mode 100644 (file)
index 0000000..bb143c1
--- /dev/null
@@ -0,0 +1,11 @@
+RUN: llvm-symbolizer -e %p/Inputs/addr.exe 0x40054d \
+RUN:   | FileCheck %s --check-prefix=LLVM
+
+RUN: llvm-symbolizer --output-style=GNU -e %p/Inputs/addr.exe 0x40054d \
+RUN:   | FileCheck %s --check-prefix=GNU
+
+RUN: llvm-symbolizer --output-style=LLVM -e %p/Inputs/addr.exe 0x40054d \
+RUN:   | FileCheck %s --check-prefix=LLVM
+
+LLVM: {{^}}/tmp{{\\|/}}x.c:3:3{{$}}
+GNU: {{^}}/tmp{{\\|/}}x.c:3{{$}}
index bac7fc5f8e77882457c658ee4300a3d82438aa7d..e410cfe14930c421df4efaf2f2542f24b00a84fe 100644 (file)
@@ -147,6 +147,14 @@ static cl::opt<std::string>
     ClFallbackDebugPath("fallback-debug-path", cl::init(""),
                         cl::desc("Fallback path for debug binaries."));
 
+static cl::opt<DIPrinter::OutputStyle>
+    ClOutputStyle("output-style", cl::init(DIPrinter::OutputStyle::LLVM),
+                  cl::desc("Specify print style"), cl::Hidden,
+                  cl::values(clEnumValN(DIPrinter::OutputStyle::LLVM, "LLVM",
+                                        "LLVM default style"),
+                             clEnumValN(DIPrinter::OutputStyle::GNU, "GNU",
+                                        "GNU addr2line style")));
+
 template<typename T>
 static bool error(Expected<T> &ResOrErr) {
   if (ResOrErr)
@@ -256,7 +264,7 @@ int main(int argc, char **argv) {
 
   DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
                     ClPrettyPrint, ClPrintSourceContextLines, ClVerbose,
-                    ClBasenames);
+                    ClBasenames, ClOutputStyle);
 
   if (ClInputAddresses.empty()) {
     const int kMaxInputStringLength = 1024;