1*9880d681SAndroid Build Coastguard Worker============================== 2*9880d681SAndroid Build Coastguard WorkerDebugging JIT-ed Code With GDB 3*9880d681SAndroid Build Coastguard Worker============================== 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard WorkerBackground 6*9880d681SAndroid Build Coastguard Worker========== 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard WorkerWithout special runtime support, debugging dynamically generated code with 9*9880d681SAndroid Build Coastguard WorkerGDB (as well as most debuggers) can be quite painful. Debuggers generally 10*9880d681SAndroid Build Coastguard Workerread debug information from the object file of the code, but for JITed 11*9880d681SAndroid Build Coastguard Workercode, there is no such file to look for. 12*9880d681SAndroid Build Coastguard Worker 13*9880d681SAndroid Build Coastguard WorkerIn order to communicate the necessary debug info to GDB, an interface for 14*9880d681SAndroid Build Coastguard Workerregistering JITed code with debuggers has been designed and implemented for 15*9880d681SAndroid Build Coastguard WorkerGDB and LLVM MCJIT. At a high level, whenever MCJIT generates new machine code, 16*9880d681SAndroid Build Coastguard Workerit does so in an in-memory object file that contains the debug information in 17*9880d681SAndroid Build Coastguard WorkerDWARF format. MCJIT then adds this in-memory object file to a global list of 18*9880d681SAndroid Build Coastguard Workerdynamically generated object files and calls a special function 19*9880d681SAndroid Build Coastguard Worker(``__jit_debug_register_code``) marked noinline that GDB knows about. When 20*9880d681SAndroid Build Coastguard WorkerGDB attaches to a process, it puts a breakpoint in this function and loads all 21*9880d681SAndroid Build Coastguard Workerof the object files in the global list. When MCJIT calls the registration 22*9880d681SAndroid Build Coastguard Workerfunction, GDB catches the breakpoint signal, loads the new object file from 23*9880d681SAndroid Build Coastguard Workerthe inferior's memory, and resumes the execution. In this way, GDB can get the 24*9880d681SAndroid Build Coastguard Workernecessary debug information. 25*9880d681SAndroid Build Coastguard Worker 26*9880d681SAndroid Build Coastguard WorkerGDB Version 27*9880d681SAndroid Build Coastguard Worker=========== 28*9880d681SAndroid Build Coastguard Worker 29*9880d681SAndroid Build Coastguard WorkerIn order to debug code JIT-ed by LLVM, you need GDB 7.0 or newer, which is 30*9880d681SAndroid Build Coastguard Workeravailable on most modern distributions of Linux. The version of GDB that 31*9880d681SAndroid Build Coastguard WorkerApple ships with Xcode has been frozen at 6.3 for a while. LLDB may be a 32*9880d681SAndroid Build Coastguard Workerbetter option for debugging JIT-ed code on Mac OS X. 33*9880d681SAndroid Build Coastguard Worker 34*9880d681SAndroid Build Coastguard Worker 35*9880d681SAndroid Build Coastguard WorkerDebugging MCJIT-ed code 36*9880d681SAndroid Build Coastguard Worker======================= 37*9880d681SAndroid Build Coastguard Worker 38*9880d681SAndroid Build Coastguard WorkerThe emerging MCJIT component of LLVM allows full debugging of JIT-ed code with 39*9880d681SAndroid Build Coastguard WorkerGDB. This is due to MCJIT's ability to use the MC emitter to provide full 40*9880d681SAndroid Build Coastguard WorkerDWARF debugging information to GDB. 41*9880d681SAndroid Build Coastguard Worker 42*9880d681SAndroid Build Coastguard WorkerNote that lli has to be passed the ``-use-mcjit`` flag to JIT the code with 43*9880d681SAndroid Build Coastguard WorkerMCJIT instead of the old JIT. 44*9880d681SAndroid Build Coastguard Worker 45*9880d681SAndroid Build Coastguard WorkerExample 46*9880d681SAndroid Build Coastguard Worker------- 47*9880d681SAndroid Build Coastguard Worker 48*9880d681SAndroid Build Coastguard WorkerConsider the following C code (with line numbers added to make the example 49*9880d681SAndroid Build Coastguard Workereasier to follow): 50*9880d681SAndroid Build Coastguard Worker 51*9880d681SAndroid Build Coastguard Worker.. 52*9880d681SAndroid Build Coastguard Worker FIXME: 53*9880d681SAndroid Build Coastguard Worker Sphinx has the ability to automatically number these lines by adding 54*9880d681SAndroid Build Coastguard Worker :linenos: on the line immediately following the `.. code-block:: c`, but 55*9880d681SAndroid Build Coastguard Worker it looks like garbage; the line numbers don't even line up with the 56*9880d681SAndroid Build Coastguard Worker lines. Is this a Sphinx bug, or is it a CSS problem? 57*9880d681SAndroid Build Coastguard Worker 58*9880d681SAndroid Build Coastguard Worker.. code-block:: c 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard Worker 1 int compute_factorial(int n) 61*9880d681SAndroid Build Coastguard Worker 2 { 62*9880d681SAndroid Build Coastguard Worker 3 if (n <= 1) 63*9880d681SAndroid Build Coastguard Worker 4 return 1; 64*9880d681SAndroid Build Coastguard Worker 5 65*9880d681SAndroid Build Coastguard Worker 6 int f = n; 66*9880d681SAndroid Build Coastguard Worker 7 while (--n > 1) 67*9880d681SAndroid Build Coastguard Worker 8 f *= n; 68*9880d681SAndroid Build Coastguard Worker 9 return f; 69*9880d681SAndroid Build Coastguard Worker 10 } 70*9880d681SAndroid Build Coastguard Worker 11 71*9880d681SAndroid Build Coastguard Worker 12 72*9880d681SAndroid Build Coastguard Worker 13 int main(int argc, char** argv) 73*9880d681SAndroid Build Coastguard Worker 14 { 74*9880d681SAndroid Build Coastguard Worker 15 if (argc < 2) 75*9880d681SAndroid Build Coastguard Worker 16 return -1; 76*9880d681SAndroid Build Coastguard Worker 17 char firstletter = argv[1][0]; 77*9880d681SAndroid Build Coastguard Worker 18 int result = compute_factorial(firstletter - '0'); 78*9880d681SAndroid Build Coastguard Worker 19 79*9880d681SAndroid Build Coastguard Worker 20 // Returned result is clipped at 255... 80*9880d681SAndroid Build Coastguard Worker 21 return result; 81*9880d681SAndroid Build Coastguard Worker 22 } 82*9880d681SAndroid Build Coastguard Worker 83*9880d681SAndroid Build Coastguard WorkerHere is a sample command line session that shows how to build and run this 84*9880d681SAndroid Build Coastguard Workercode via ``lli`` inside GDB: 85*9880d681SAndroid Build Coastguard Worker 86*9880d681SAndroid Build Coastguard Worker.. code-block:: bash 87*9880d681SAndroid Build Coastguard Worker 88*9880d681SAndroid Build Coastguard Worker $ $BINPATH/clang -cc1 -O0 -g -emit-llvm showdebug.c 89*9880d681SAndroid Build Coastguard Worker $ gdb --quiet --args $BINPATH/lli -use-mcjit showdebug.ll 5 90*9880d681SAndroid Build Coastguard Worker Reading symbols from $BINPATH/lli...done. 91*9880d681SAndroid Build Coastguard Worker (gdb) b showdebug.c:6 92*9880d681SAndroid Build Coastguard Worker No source file named showdebug.c. 93*9880d681SAndroid Build Coastguard Worker Make breakpoint pending on future shared library load? (y or [n]) y 94*9880d681SAndroid Build Coastguard Worker Breakpoint 1 (showdebug.c:6) pending. 95*9880d681SAndroid Build Coastguard Worker (gdb) r 96*9880d681SAndroid Build Coastguard Worker Starting program: $BINPATH/lli -use-mcjit showdebug.ll 5 97*9880d681SAndroid Build Coastguard Worker [Thread debugging using libthread_db enabled] 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Worker Breakpoint 1, compute_factorial (n=5) at showdebug.c:6 100*9880d681SAndroid Build Coastguard Worker 6 int f = n; 101*9880d681SAndroid Build Coastguard Worker (gdb) p n 102*9880d681SAndroid Build Coastguard Worker $1 = 5 103*9880d681SAndroid Build Coastguard Worker (gdb) p f 104*9880d681SAndroid Build Coastguard Worker $2 = 0 105*9880d681SAndroid Build Coastguard Worker (gdb) n 106*9880d681SAndroid Build Coastguard Worker 7 while (--n > 1) 107*9880d681SAndroid Build Coastguard Worker (gdb) p f 108*9880d681SAndroid Build Coastguard Worker $3 = 5 109*9880d681SAndroid Build Coastguard Worker (gdb) b showdebug.c:9 110*9880d681SAndroid Build Coastguard Worker Breakpoint 2 at 0x7ffff7ed404c: file showdebug.c, line 9. 111*9880d681SAndroid Build Coastguard Worker (gdb) c 112*9880d681SAndroid Build Coastguard Worker Continuing. 113*9880d681SAndroid Build Coastguard Worker 114*9880d681SAndroid Build Coastguard Worker Breakpoint 2, compute_factorial (n=1) at showdebug.c:9 115*9880d681SAndroid Build Coastguard Worker 9 return f; 116*9880d681SAndroid Build Coastguard Worker (gdb) p f 117*9880d681SAndroid Build Coastguard Worker $4 = 120 118*9880d681SAndroid Build Coastguard Worker (gdb) bt 119*9880d681SAndroid Build Coastguard Worker #0 compute_factorial (n=1) at showdebug.c:9 120*9880d681SAndroid Build Coastguard Worker #1 0x00007ffff7ed40a9 in main (argc=2, argv=0x16677e0) at showdebug.c:18 121*9880d681SAndroid Build Coastguard Worker #2 0x3500000001652748 in ?? () 122*9880d681SAndroid Build Coastguard Worker #3 0x00000000016677e0 in ?? () 123*9880d681SAndroid Build Coastguard Worker #4 0x0000000000000002 in ?? () 124*9880d681SAndroid Build Coastguard Worker #5 0x0000000000d953b3 in llvm::MCJIT::runFunction (this=0x16151f0, F=0x1603020, ArgValues=...) at /home/ebenders_test/llvm_svn_rw/lib/ExecutionEngine/MCJIT/MCJIT.cpp:161 125*9880d681SAndroid Build Coastguard Worker #6 0x0000000000dc8872 in llvm::ExecutionEngine::runFunctionAsMain (this=0x16151f0, Fn=0x1603020, argv=..., envp=0x7fffffffe040) 126*9880d681SAndroid Build Coastguard Worker at /home/ebenders_test/llvm_svn_rw/lib/ExecutionEngine/ExecutionEngine.cpp:397 127*9880d681SAndroid Build Coastguard Worker #7 0x000000000059c583 in main (argc=4, argv=0x7fffffffe018, envp=0x7fffffffe040) at /home/ebenders_test/llvm_svn_rw/tools/lli/lli.cpp:324 128*9880d681SAndroid Build Coastguard Worker (gdb) finish 129*9880d681SAndroid Build Coastguard Worker Run till exit from #0 compute_factorial (n=1) at showdebug.c:9 130*9880d681SAndroid Build Coastguard Worker 0x00007ffff7ed40a9 in main (argc=2, argv=0x16677e0) at showdebug.c:18 131*9880d681SAndroid Build Coastguard Worker 18 int result = compute_factorial(firstletter - '0'); 132*9880d681SAndroid Build Coastguard Worker Value returned is $5 = 120 133*9880d681SAndroid Build Coastguard Worker (gdb) p result 134*9880d681SAndroid Build Coastguard Worker $6 = 23406408 135*9880d681SAndroid Build Coastguard Worker (gdb) n 136*9880d681SAndroid Build Coastguard Worker 21 return result; 137*9880d681SAndroid Build Coastguard Worker (gdb) p result 138*9880d681SAndroid Build Coastguard Worker $7 = 120 139*9880d681SAndroid Build Coastguard Worker (gdb) c 140*9880d681SAndroid Build Coastguard Worker Continuing. 141*9880d681SAndroid Build Coastguard Worker 142*9880d681SAndroid Build Coastguard Worker Program exited with code 0170. 143*9880d681SAndroid Build Coastguard Worker (gdb) 144