Skip to content

Commit 6d3367a

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6-beta.1
1 parent 43c85af commit 6d3367a

File tree

4 files changed

+117
-10
lines changed

4 files changed

+117
-10
lines changed

llvm/docs/DebuggingLLVM.rst

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
==============
2+
Debugging LLVM
3+
==============
4+
5+
This document is a collection of tips and tricks for debugging LLVM
6+
using a source-level debugger. The assumption is that you are trying to
7+
figure out the root cause of a miscompilation in the program that you
8+
are compiling.
9+
10+
Extract and rerun the compile command
11+
=====================================
12+
13+
Extract the Clang command that produces the buggy code. The way to do
14+
this depends on the build system used by your program.
15+
16+
- For Ninja-based build systems, you can pass ``-t commands`` to Ninja
17+
and filter the output by the targeted source file name. For example:
18+
``ninja -t commands myprogram | grep path/to/file.cpp``.
19+
20+
- For Bazel-based build systems using Bazel 9 or newer (not released yet
21+
as of this writing), you can pass ``--output=commands`` to the ``bazel
22+
aquery`` subcommand for a similar result. For example: ``bazel aquery
23+
--output=commands 'deps(//myprogram)' | grep path/to/file.cpp``. Build
24+
commands must generally be run from a subdirectory of the source
25+
directory named ``bazel-$PROJECTNAME``. Bazel typically makes the target
26+
paths of ``-o`` and ``-MF`` read-only when running commands outside
27+
of a build, so it may be necessary to change or remove these flags.
28+
29+
- A method that should work with any build system is to build your program
30+
under `Bear <https://github.com/rizsotto/Bear>`_ and look for the
31+
compile command in the resulting ``compile_commands.json`` file.
32+
33+
Once you have the command you can use the following steps to debug
34+
it. Note that any flags mentioned later in this document are LLVM flags
35+
so they must be prefixed with ``-mllvm`` when passed to the Clang driver,
36+
e.g. ``-mllvm -print-after-all``.
37+
38+
Understanding the source of the issue
39+
=====================================
40+
41+
If you have a miscompilation introduced by a pass, it is frequently
42+
possible to identify the pass where things go wrong by searching a
43+
pass-by-pass printout, which is enabled using the ``-print-after-all``
44+
flag. Pipe stderr into ``less`` (append ``2>&1 | less`` to command
45+
line) and use text search to move between passes (e.g. type ``/Dump
46+
After<Enter>``, ``n`` to move to next pass, ``N`` to move to previous
47+
pass). If the name of the function containing the buggy IR is known, you
48+
can filter the output by passing ``-filter-print-funcs=functionname``. You
49+
can sometimes pass ``-debug`` to get useful details about what passes
50+
are doing.
51+
52+
Creating a debug build of LLVM
53+
==============================
54+
55+
The subsequent debugging steps require a debug build of LLVM. Pass the
56+
``-DCMAKE_BUILD_TYPE=Debug`` to CMake in a separate build tree to create
57+
a debug build.
58+
59+
Understanding where an instruction came from
60+
============================================
61+
62+
A common debugging task involves understanding which part of the code
63+
introduced a buggy instruction. The pass-by-pass dump is sometimes enough,
64+
but for complex or unfamiliar passes, more information is often required.
65+
66+
The first step is to record a run of the debug build of Clang under `rr
67+
<https://rr-project.org>`_ passing the LLVM flag ``-print-inst-addrs``
68+
together with ``-print-after-all`` and any desired filters. This will
69+
cause each instruction printed by LLVM to be suffixed with a comment
70+
showing the address of the ``Instruction`` object. You can then replay
71+
the run of Clang with ``rr replay``. Because ``rr`` is deterministic,
72+
the instruction will receive the same address during the replay, so
73+
you can break on the instruction's construction using a conditional
74+
breakpoint that checks for the address printed by LLVM, with commands
75+
such as the following:
76+
77+
.. code-block:: text
78+
79+
b Instruction::Instruction
80+
cond 1 this == 0x12345678
81+
82+
When the breakpoint is hit, you will likely be at the location where
83+
the instruction was created, so you can unwind the stack with ``bt``
84+
to see the stack trace. It is also possible that an instruction was
85+
created multiple times at the same address, so you may need to continue
86+
until reaching the desired location, but in the author's experience this
87+
is unlikely to occur.
88+
89+
Identifying the source locations of instructions
90+
================================================
91+
92+
To identify the source location that caused a particular instruction
93+
to be created, you can pass the LLVM flag ``-print-inst-debug-locs``
94+
and each instruction printed by LLVM is suffixed with the file and line
95+
number of the instruction according to the debug information. Note that
96+
this requires debug information to be enabled (e.g. pass ``-g`` to Clang).
97+
98+
GDB pretty printers
99+
===================
100+
101+
A handful of `GDB pretty printers
102+
<https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html>`__ are
103+
provided for some of the core LLVM libraries. To use them, execute the
104+
following (or add it to your ``~/.gdbinit``)::
105+
106+
source /path/to/llvm/src/utils/gdb-scripts/prettyprinters.py
107+
108+
It also might be handy to enable the `print pretty
109+
<http://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_57.html>`__ option to
110+
avoid data structures being printed as a big block of text.

llvm/docs/GettingStartedTutorials.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ For those new to the LLVM system.
1111
GettingStarted
1212
GettingStartedVS
1313
ProgrammersManual
14+
DebuggingLLVM
1415
tutorial/index
1516
MyFirstTypoFix
1617

@@ -27,6 +28,9 @@ For those new to the LLVM system.
2728
Introduction to the general layout of the LLVM sourcebase, important classes
2829
and APIs, and some tips & tricks.
2930

31+
:doc:`DebuggingLLVM`
32+
Provides information about how to debug LLVM.
33+
3034
:doc:`Frontend/PerformanceTips`
3135
A collection of tips for frontend authors on how to generate IR
3236
which LLVM is able to effectively optimize.

llvm/docs/ProgrammersManual.rst

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,16 +2641,7 @@ with an ``assert``).
26412641
Debugging
26422642
=========
26432643

2644-
A handful of `GDB pretty printers
2645-
<https://sourceware.org/gdb/onlinedocs/gdb/Pretty-Printing.html>`__ are
2646-
provided for some of the core LLVM libraries. To use them, execute the
2647-
following (or add it to your ``~/.gdbinit``)::
2648-
2649-
source /path/to/llvm/src/utils/gdb-scripts/prettyprinters.py
2650-
2651-
It also might be handy to enable the `print pretty
2652-
<http://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_57.html>`__ option to
2653-
avoid data structures being printed as a big block of text.
2644+
See :doc:`Debugging LLVM <DebuggingLLVM>`.
26542645

26552646
.. _common:
26562647

llvm/lib/IR/AsmWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
using namespace llvm;
9090

91+
// See https://llvm.org/docs/DebuggingLLVM.html for why these flags are useful.
92+
9193
static cl::opt<bool>
9294
PrintInstAddrs("print-inst-addrs", cl::Hidden,
9395
cl::desc("Print addresses of instructions when dumping"));

0 commit comments

Comments
 (0)