llvm.org GIT mirror llvm / 22884dd
Add documentation on how to use enable debug information in the JIT and use it with GDB. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82425 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 10 years ago
2 changed file(s) with 174 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0
1 "http://www.w3.org/TR/html4/strict.dtd">
2
3
4 Debugging JITed Code With GDB
5
6
7
8
9
Debugging JITed Code With GDB
10
11
  • Introduction
  • 12
  • Quickstart
  • 13
  • Example with clang and lli
  • 14
    15
    Written by Reid Kleckner
    16
    17
    18
    19
    20
    21
    22

    Without special runtime support, debugging dynamically generated code with

    23 GDB (as well as most debuggers) can be quite painful. Debuggers generally read
    24 debug information from the object file of the code, but for JITed code, there is
    25 no such file to look for.
    26

    27
    28

    Depending on the architecture, this can impact the debugging experience in

    29 different ways. For example, on most 32-bit x86 architectures, you can simply
    30 compile with -fno-omit-framepointer for GCC and -fdisable-fp-elim for LLVM.
    31 When GDB creates a backtrace, it can properly unwind the stack, but the stack
    32 frames owned by JITed code have ??'s instead of the appropriate symbol name.
    33 However, on Linux x86_64 in particular, GDB relies on the DWARF CFA debug
    34 information to unwind the stack, so even if you compile your program to leave
    35 the frame pointer untouched, GDB will usually be unable to unwind the stack past
    36 any JITed code stack frames.
    37

    38
    39

    In order to communicate the necessary debug info to GDB, an interface for

    40 registering JITed code with debuggers has been designed and implemented for
    41 GDB and LLVM. At a high level, whenever LLVM generates new machine code, it
    42 also generates an object file in memory containing the debug information. LLVM
    43 then adds the object file to the global list of object files and calls a special
    44 function (__jit_debug_register_code) marked noinline that GDB knows about. When
    45 GDB attaches to a process, it puts a breakpoint in this function and loads all
    46 of the object files in the global list. When LLVM calls the registration
    47 function, GDB catches the breakpoint signal, loads the new object file from
    48 LLVM's memory, and resumes the execution. In this way, GDB can get the
    49 necessary debug information.
    50

    51
    52

    At the time of this writing, LLVM only supports architectures that use ELF

    53 object files and it only generates symbols and DWARF CFA information. However,
    54 it would be easy to add more information to the object file, so we don't need to
    55 coordinate with GDB to get better debug information.
    56

    57
    58
    59
    60
    61
    62
    63
    64

    In order to debug code JITed by LLVM, you need to install a recent version

    65 of GDB. The interface was added on 2009-08-19, so you need a snapshot of GDB
    66 more recent than that. Either download a snapshot of GDB or checkout CVS as
    67 instructed here. Here
    68 are the commands for doing a checkout and building the code:
    69

    70
    71
    
                      
                    
    72 $ cvs -z 3 -d :pserver:anoncvs@sourceware.org:/cvs/src co gdb
    73 $ mv src gdb # You probably don't want this checkout called "src".
    74 $ cd gdb
    75 $ ./configure --prefix="$GDB_INSTALL"
    76 $ make
    77 $ make install
    78
    79
    80

    You can then use -jit-emit-debug in the LLVM command line arguments to enable

    81 the interface.
    82

    83
    84
    85
    86
    87
    88
    89
    90

    For example, consider debugging running lli on the following C code in

    91 foo.c:
    92

    93
    94
    
                      
                    
    95 #include <stdio.h>
    96
    97 void foo() {
    98 printf("%d\n", *(int*)NULL); // Crash here
    99 }
    100
    101 void bar() {
    102 foo();
    103 }
    104
    105 void baz() {
    106 bar();
    107 }
    108
    109 int main(int argc, char **argv) {
    110 baz();
    111 }
    112
    113
    114

    Here are the commands to run that application under GDB and print the stack

    115 trace at the crash:
    116

    117
    118
    
                      
                    
    119 # Compile foo.c to bitcode. You can use either clang or llvm-gcc with this
    120 # command line. Both require -fexceptions, or the calls are all marked
    121 # 'nounwind' which disables DWARF CFA info.
    122 $ clang foo.c -fexceptions -emit-llvm -c -o foo.bc
    123
    124 # Run foo.bc under lli with -jit-emit-debug. If you built lli in debug mode,
    125 # -jit-emit-debug defaults to true.
    126 $ $GDB_INSTALL/gdb --args lli -jit-emit-debug foo.bc
    127 ...
    128
    129 # Run the code.
    130 (gdb) run
    131 Starting program: /tmp/gdb/lli -jit-emit-debug foo.bc
    132 [Thread debugging using libthread_db enabled]
    133
    134 Program received signal SIGSEGV, Segmentation fault.
    135 0x00007ffff7f55164 in foo ()
    136
    137 # Print the backtrace, this time with symbols instead of ??.
    138 (gdb) bt
    139 #0 0x00007ffff7f55164 in foo ()
    140 #1 0x00007ffff7f550f9 in bar ()
    141 #2 0x00007ffff7f55099 in baz ()
    142 #3 0x00007ffff7f5502a in main ()
    143 #4 0x00000000007c0225 in llvm::JIT::runFunction(llvm::Function*,
    144 std::vector<llvm::GenericValue,
    145 std::allocator<llvm::GenericValue> > const&) ()
    146 #5 0x00000000007d6d98 in
    147 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*,
    148 std::vector<std::string,
    149 std::allocator<std::string> > const&, char const* const*) ()
    150 #6 0x00000000004dab76 in main ()
    151
    152
    153
    154

    As you can see, GDB can correctly unwind the stack and has the appropriate

    155 function names.
    156

    157
    158
    159
    160
    161
    162 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS">
    163
    164 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01">
    165 Reid Kleckner
    166 The LLVM Compiler Infrastructure
    167 Last modified: $Date: 2009-01-01 23:10:51 -0800 (Thu, 01 Jan 2009) $
    168
    169
    170
    232232
    233233
  • The LLVM gold plugin - How to build your
  • 234234 programs with link-time optimization on Linux.
    235
    236
  • The GDB JIT interface - How to debug
  • 237 JITed code with GDB.
    235238
    236239
    237240