Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 2447081

Browse files
committed
[llvm-symbolizer] Add support for --basenames/-s
This fixes https://bugs.llvm.org/show_bug.cgi?id=40068. --basenames is a GNU addr2line switch which strips the directory names from the file path in the output. Reviewed by: ruiu Differential Revision: https://reviews.llvm.org/D56919 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351795 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 99ddd77 commit 2447081

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

docs/CommandGuide/llvm-symbolizer.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ OPTIONS
119119
Print human readable output. If ``-inlining`` is specified, enclosing scope is
120120
prefixed by (inlined by). Refer to listed examples.
121121

122+
.. option:: -basenames, -s
123+
124+
Strip directories when printing the file path.
125+
122126
EXIT STATUS
123127
-----------
124128

include/llvm/DebugInfo/Symbolize/DIPrinter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ class DIPrinter {
2929
bool PrintPretty;
3030
int PrintSourceContext;
3131
bool Verbose;
32+
bool Basenames;
3233

3334
void print(const DILineInfo &Info, bool Inlined);
3435
void printContext(const std::string &FileName, int64_t Line);
3536

3637
public:
3738
DIPrinter(raw_ostream &OS, bool PrintFunctionNames = true,
3839
bool PrintPretty = false, int PrintSourceContext = 0,
39-
bool Verbose = false)
40+
bool Verbose = false, bool Basenames = false)
4041
: OS(OS), PrintFunctionNames(PrintFunctionNames),
4142
PrintPretty(PrintPretty), PrintSourceContext(PrintSourceContext),
42-
Verbose(Verbose) {}
43+
Verbose(Verbose), Basenames(Basenames) {}
4344

4445
DIPrinter &operator<<(const DILineInfo &Info);
4546
DIPrinter &operator<<(const DIInliningInfo &Info);

lib/DebugInfo/Symbolize/DIPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/Format.h"
1919
#include "llvm/Support/LineIterator.h"
2020
#include "llvm/Support/MemoryBuffer.h"
21+
#include "llvm/Support/Path.h"
2122
#include "llvm/Support/raw_ostream.h"
2223
#include <algorithm>
2324
#include <cmath>
@@ -77,6 +78,8 @@ void DIPrinter::print(const DILineInfo &Info, bool Inlined) {
7778
std::string Filename = Info.FileName;
7879
if (Filename == kDILineInfoBadString)
7980
Filename = kBadString;
81+
else if (Basenames)
82+
Filename = llvm::sys::path::filename(Filename);
8083
if (!Verbose) {
8184
OS << Filename << ":" << Info.Line << ":" << Info.Column << "\n";
8285
printContext(Filename, Info.Line);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# REQUIRES: x86-registered-target
2+
3+
foo:
4+
nop
5+
6+
# RUN: llvm-mc --filetype=obj --triple=x86_64-pc-linux %s -o %t.o -g
7+
# RUN: llvm-symbolizer 0 --basenames --obj=%t.o | FileCheck %s
8+
# RUN: llvm-symbolizer 0 -s --obj=%t.o | FileCheck %s
9+
# RUN: llvm-symbolizer 0 --obj=%t.o | FileCheck %s -DDIR=%p --check-prefix=DEFAULT
10+
11+
# CHECK: {{^}}basenames.s:4
12+
# DEFAULT: [[DIR]]{{\\|/}}basenames.s:4

tools/llvm-symbolizer/llvm-symbolizer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ static cl::opt<bool>
5454
ClPrintInlining("inlining", cl::init(true),
5555
cl::desc("Print all inlined frames for a given address"));
5656

57+
// -basenames, -s
58+
static cl::opt<bool> ClBasenames("basenames", cl::init(false),
59+
cl::desc("Strip directory names from paths"));
60+
static cl::alias ClBasenamesShort("s", cl::desc("Alias for -basenames"),
61+
cl::NotHidden, cl::aliasopt(ClBasenames));
62+
5763
// -demangle, -C, -no-demangle
5864
static cl::opt<bool>
5965
ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
@@ -223,7 +229,8 @@ int main(int argc, char **argv) {
223229
LLVMSymbolizer Symbolizer(Opts);
224230

225231
DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
226-
ClPrettyPrint, ClPrintSourceContextLines, ClVerbose);
232+
ClPrettyPrint, ClPrintSourceContextLines, ClVerbose,
233+
ClBasenames);
227234

228235
if (ClInputAddresses.empty()) {
229236
const int kMaxInputStringLength = 1024;

0 commit comments

Comments
 (0)