Skip to content

Commit 21d9d08

Browse files
New symbolizer option to print files relative to the compilation directory.
Summary: New "--relative" option to allow printing files relative to the compilation directory. Reviewers: jhenderson Subscribers: MaskRay, rupprecht, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D76733
1 parent da7b6fe commit 21d9d08

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

llvm/docs/CommandGuide/llvm-symbolizer.rst

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,27 @@ Example 4 - CODE and DATA prefixes:
147147
bar
148148
6295592 4
149149
150+
Example 5 - path-style options:
151+
152+
This example uses the same source file as above, but the source file's
153+
full path is /tmp/foo/test.cpp and is compiled as follows. The first case
154+
shows the default absolute path, the second --basenames, and the third
155+
shows --relativenames.
156+
157+
.. code-block:: console
158+
$ pwd
159+
/tmp
160+
$ clang -g foo/test.cpp -o test.elf
161+
$ llvm-symbolizer --obj=test.elf 0x4004a0
162+
main
163+
/tmp/foo/test.cpp:15:0
164+
$ llvm-symbolizer --obj=test.elf 0x4004a0 --basenames
165+
main
166+
test.cpp:15:0
167+
$ llvm-symbolizer --obj=test.elf 0x4004a0 --relativenames
168+
main
169+
foo/test.cpp:15:0
170+
150171
OPTIONS
151172
-------
152173

@@ -158,8 +179,15 @@ OPTIONS
158179

159180
.. option:: --basenames, -s
160181

161-
Strip directories when printing the file path.
182+
Print just the file's name without any directories, instead of the
183+
absolute path.
162184

185+
.. option:: --relativenames
186+
187+
Print the file's path relative to the compilation directory, instead
188+
of the absolute path. If the command-line to the compiler included
189+
the full path, this will be the same as the default.
190+
163191
.. _llvm-symbolizer-opt-C:
164192

165193
.. option:: --demangle, -C
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
8+
# RUN: llvm-symbolizer 0 --relativenames --obj=%t.o \
9+
# RUN: | FileCheck %s -DDIR=%p --check-prefix=RELATIVENAMES
10+
11+
## Ensure last option wins.
12+
# RUN: llvm-symbolizer 0 --basenames --relativenames --obj=%t.o \
13+
# RUN: | FileCheck %s -DDIR=%p --check-prefix=RELATIVENAMES
14+
# RUN: llvm-symbolizer 0 --relativenames --basenames --obj=%t.o \
15+
# RUN: | FileCheck %s --check-prefix=BASENAMES
16+
17+
# RELATIVENAMES: [[DIR]]{{\\|/}}relativenames.s:4
18+
# BASENAMES: {{^}}relativenames.s:4

llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ static cl::opt<bool> ClBasenames("basenames", cl::init(false),
7777
static cl::alias ClBasenamesShort("s", cl::desc("Alias for -basenames"),
7878
cl::NotHidden, cl::aliasopt(ClBasenames));
7979

80+
// -relativenames
81+
static cl::opt<bool>
82+
ClRelativenames("relativenames", cl::init(false),
83+
cl::desc("Strip the compilation directory from paths"));
84+
8085
// -demangle, -C, -no-demangle
8186
static cl::opt<bool>
8287
ClDemangle("demangle", cl::init(true), cl::desc("Demangle function names"));
@@ -310,8 +315,12 @@ int main(int argc, char **argv) {
310315
Opts.DWPName = ClDwpName;
311316
Opts.DebugFileDirectory = ClDebugFileDirectory;
312317
Opts.PathStyle = DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath;
313-
if (ClBasenames)
318+
// If both --basenames and --relativenames are specified then pick the last
319+
// one.
320+
if (ClBasenames.getPosition() > ClRelativenames.getPosition())
314321
Opts.PathStyle = DILineInfoSpecifier::FileLineInfoKind::BaseNameOnly;
322+
else if (ClRelativenames)
323+
Opts.PathStyle = DILineInfoSpecifier::FileLineInfoKind::RelativeFilePath;
315324

316325
for (const auto &hint : ClDsymHint) {
317326
if (sys::path::extension(hint) == ".dSYM") {

0 commit comments

Comments
 (0)