blob: f23fdf6f391896ca326b47877714aaa0589db996 [file] [log] [blame]
Reid Kleckner22884dd2009-09-21 02:34:59 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3<html>
4<head>
5 <title>Debugging JITed Code With GDB</title>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8<body>
9
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000010<h1>Debugging JITed Code With GDB</h1>
Reid Kleckner22884dd2009-09-21 02:34:59 +000011<ol>
Reid Kleckner89472822010-07-07 20:16:45 +000012 <li><a href="#example">Example usage</a></li>
13 <li><a href="#background">Background</a></li>
Reid Kleckner22884dd2009-09-21 02:34:59 +000014</ol>
15<div class="doc_author">Written by Reid Kleckner</div>
16
17<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000018<h2><a name="example">Example usage</a></h2>
Reid Kleckner22884dd2009-09-21 02:34:59 +000019<!--=========================================================================-->
20<div class="doc_text">
21
Reid Kleckner89472822010-07-07 20:16:45 +000022<p>In order to debug code JITed by LLVM, you need GDB 7.0 or newer, which is
23available on most modern distributions of Linux. The version of GDB that Apple
24ships with XCode has been frozen at 6.3 for a while. LLDB may be a better
25option for debugging JITed code on Mac OS X.
Reid Kleckner22884dd2009-09-21 02:34:59 +000026</p>
27
Reid Kleckner89472822010-07-07 20:16:45 +000028<p>Consider debugging the following code compiled with clang and run through
29lli:
Reid Kleckner22884dd2009-09-21 02:34:59 +000030</p>
31
32<pre class="doc_code">
33#include &lt;stdio.h&gt;
34
35void foo() {
36 printf("%d\n", *(int*)NULL); // Crash here
37}
38
39void bar() {
40 foo();
41}
42
43void baz() {
44 bar();
45}
46
47int main(int argc, char **argv) {
48 baz();
49}
50</pre>
51
52<p>Here are the commands to run that application under GDB and print the stack
53trace at the crash:
54</p>
55
56<pre class="doc_code">
57# Compile foo.c to bitcode. You can use either clang or llvm-gcc with this
58# command line. Both require -fexceptions, or the calls are all marked
Reid Kleckner89472822010-07-07 20:16:45 +000059# 'nounwind' which disables DWARF exception handling info. Custom frontends
60# should avoid adding this attribute to JITed code, since it interferes with
61# DWARF CFA generation at the moment.
Reid Kleckner22884dd2009-09-21 02:34:59 +000062$ clang foo.c -fexceptions -emit-llvm -c -o foo.bc
63
64# Run foo.bc under lli with -jit-emit-debug. If you built lli in debug mode,
65# -jit-emit-debug defaults to true.
66$ $GDB_INSTALL/gdb --args lli -jit-emit-debug foo.bc
67...
68
69# Run the code.
70(gdb) run
71Starting program: /tmp/gdb/lli -jit-emit-debug foo.bc
72[Thread debugging using libthread_db enabled]
73
74Program received signal SIGSEGV, Segmentation fault.
750x00007ffff7f55164 in foo ()
76
77# Print the backtrace, this time with symbols instead of ??.
78(gdb) bt
79#0 0x00007ffff7f55164 in foo ()
80#1 0x00007ffff7f550f9 in bar ()
81#2 0x00007ffff7f55099 in baz ()
82#3 0x00007ffff7f5502a in main ()
83#4 0x00000000007c0225 in llvm::JIT::runFunction(llvm::Function*,
84 std::vector&lt;llvm::GenericValue,
Reid Kleckner89472822010-07-07 20:16:45 +000085 std::allocator&lt;llvm::GenericValue&gt; &gt; const&amp;) ()
Reid Kleckner22884dd2009-09-21 02:34:59 +000086#5 0x00000000007d6d98 in
87 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*,
88 std::vector&lt;std::string,
Reid Kleckner89472822010-07-07 20:16:45 +000089 std::allocator&lt;std::string&gt; &gt; const&amp;, char const* const*) ()
Reid Kleckner22884dd2009-09-21 02:34:59 +000090#6 0x00000000004dab76 in main ()
91</pre>
Reid Kleckner22884dd2009-09-21 02:34:59 +000092
93<p>As you can see, GDB can correctly unwind the stack and has the appropriate
94function names.
95</p>
Reid Kleckner89472822010-07-07 20:16:45 +000096</div>
97
98<!--=========================================================================-->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000099<h2><a name="background">Background</a></h2>
Reid Kleckner89472822010-07-07 20:16:45 +0000100<!--=========================================================================-->
101<div class="doc_text">
102
103<p>Without special runtime support, debugging dynamically generated code with
104GDB (as well as most debuggers) can be quite painful. Debuggers generally read
105debug information from the object file of the code, but for JITed code, there is
106no such file to look for.
107</p>
108
109<p>Depending on the architecture, this can impact the debugging experience in
110different ways. For example, on most 32-bit x86 architectures, you can simply
111compile with -fno-omit-frame-pointer for GCC and -disable-fp-elim for LLVM.
112When GDB creates a backtrace, it can properly unwind the stack, but the stack
113frames owned by JITed code have ??'s instead of the appropriate symbol name.
114However, on Linux x86_64 in particular, GDB relies on the DWARF call frame
115address (CFA) debug information to unwind the stack, so even if you compile
116your program to leave the frame pointer untouched, GDB will usually be unable
117to unwind the stack past any JITed code stack frames.
118</p>
119
120<p>In order to communicate the necessary debug info to GDB, an interface for
121registering JITed code with debuggers has been designed and implemented for
122GDB and LLVM. At a high level, whenever LLVM generates new machine code, it
123also generates an object file in memory containing the debug information. LLVM
124then adds the object file to the global list of object files and calls a special
125function (__jit_debug_register_code) marked noinline that GDB knows about. When
126GDB attaches to a process, it puts a breakpoint in this function and loads all
127of the object files in the global list. When LLVM calls the registration
128function, GDB catches the breakpoint signal, loads the new object file from
129LLVM's memory, and resumes the execution. In this way, GDB can get the
130necessary debug information.
131</p>
132
133<p>At the time of this writing, LLVM only supports architectures that use ELF
134object files and it only generates symbols and DWARF CFA information. However,
135it would be easy to add more information to the object file, so we don't need to
136coordinate with GDB to get better debug information.
137</p>
138</div>
Reid Kleckner22884dd2009-09-21 02:34:59 +0000139
140<!-- *********************************************************************** -->
141<hr>
142<address>
143 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
144 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
145 <a href="http://validator.w3.org/check/referer"><img
146 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
147 <a href="mailto:reid.kleckner@gmail.com">Reid Kleckner</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000148 <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
Reid Kleckner89472822010-07-07 20:16:45 +0000149 Last modified: $Date$
Reid Kleckner22884dd2009-09-21 02:34:59 +0000150</address>
151</body>
152</html>