brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.9 KiB · 5719c71 Raw
183 lines · plain
1=====================2Debugging JIT-ed Code3=====================4 5Background6==========7 8Without special runtime support, debugging dynamically generated code can be9quite painful.  Debuggers generally read debug information from object files on10disk, but for JITed code there is no such file to look for.11 12In order to hand over the necessary debug info, `GDB established an13interface <https://sourceware.org/gdb/onlinedocs/gdb/JIT-Interface.html>`_14for registering JITed code with debuggers. LLDB implements it in the15JITLoaderGDB plugin.  On the JIT side, LLVM MCJIT does implement the interface16for ELF object files.17 18At a high level, whenever MCJIT generates new machine code, it does so in an19in-memory object file that contains the debug information in DWARF format.20MCJIT then adds this in-memory object file to a global list of dynamically21generated object files and calls a special function22``__jit_debug_register_code`` that the debugger knows about. When the debugger23attaches to a process, it puts a breakpoint in this function and associates a24special handler with it.  Once MCJIT calls the registration function, the25debugger catches the breakpoint signal, loads the new object file from the26inferior's memory and resumes execution.  This way it can obtain debug27information for pure in-memory object files.28 29 30GDB Version31===========32 33In order to debug code JIT-ed by LLVM, you need GDB 7.0 or newer, which is34available on most modern distributions of Linux.  The version of GDB that35Apple ships with Xcode has been frozen at 6.3 for a while.36 37 38LLDB Version39============40 41Due to a regression in release 6.0, LLDB didn't support JITed code debugging for42a while.  The bug was fixed in mainline recently, so that debugging JITed ELF43objects should be possible again from the upcoming release 12.0 on. On macOS the44feature must be enabled explicitly using the ``plugin.jit-loader.gdb.enable``45setting.46 47 48Debugging MCJIT-ed code49=======================50 51The emerging MCJIT component of LLVM allows full debugging of JIT-ed code with52GDB.  This is due to MCJIT's ability to use the MC emitter to provide full53DWARF debugging information to GDB.54 55Note that lli has to be passed the ``--jit-kind=mcjit`` flag to JIT the code56with MCJIT instead of the newer ORC JIT.57 58Example59-------60 61Consider the following C code (with line numbers added to make the example62easier to follow):63 64..65   FIXME:66   Sphinx has the ability to automatically number these lines by adding67   :linenos: on the line immediately following the `.. code-block:: c`, but68   it looks like garbage; the line numbers don't even line up with the69   lines. Is this a Sphinx bug, or is it a CSS problem?70 71.. code-block:: c72 73   1   int compute_factorial(int n)74   2   {75   3       if (n <= 1)76   4           return 1;77   578   6       int f = n;79   7       while (--n > 1)80   8           f *= n;81   9       return f;82   10  }83   1184   1285   13  int main(int argc, char** argv)86   14  {87   15      if (argc < 2)88   16          return -1;89   17      char firstletter = argv[1][0];90   18      int result = compute_factorial(firstletter - '0');91   1992   20      // Returned result is clipped at 255...93   21      return result;94   22  }95 96Here is a sample command line session that shows how to build and run this97code via ``lli`` inside LLDB:98 99.. code-block:: bash100 101   > export BINPATH=/workspaces/llvm-project/build/bin102   > $BINPATH/clang -g -S -emit-llvm --target=x86_64-unknown-unknown-elf showdebug.c103   > lldb $BINPATH/lli104   (lldb) target create "/workspaces/llvm-project/build/bin/lli"105   Current executable set to '/workspaces/llvm-project/build/bin/lli' (x86_64).106   (lldb) settings set plugin.jit-loader.gdb.enable on107   (lldb) b compute_factorial108   Breakpoint 1: no locations (pending).109   WARNING:  Unable to resolve breakpoint to any actual locations.110   (lldb) run --jit-kind=mcjit showdebug.ll 5111   1 location added to breakpoint 1112   Process 21340 stopped113   * thread #1, name = 'lli', stop reason = breakpoint 1.1114      frame #0: 0x00007ffff7fd0007 JIT(0x45c2cb0)`compute_factorial(n=5) at showdebug.c:3:11115      1    int compute_factorial(int n)116      2    {117   -> 3        if (n <= 1)118      4            return 1;119      5        int f = n;120      6        while (--n > 1)121      7            f *= n;122   (lldb) p n123   (int) $0 = 5124   (lldb) b showdebug.c:9125   Breakpoint 2: where = JIT(0x45c2cb0)`compute_factorial + 60 at showdebug.c:9:1, address = 0x00007ffff7fd003c126   (lldb) c127   Process 21340 resuming128   Process 21340 stopped129   * thread #1, name = 'lli', stop reason = breakpoint 2.1130      frame #0: 0x00007ffff7fd003c JIT(0x45c2cb0)`compute_factorial(n=1) at showdebug.c:9:1131      6        while (--n > 1)132      7            f *= n;133      8        return f;134   -> 9    }135      10136      11   int main(int argc, char** argv)137      12   {138   (lldb) p f139   (int) $1 = 120140   (lldb) bt141   * thread #1, name = 'lli', stop reason = breakpoint 2.1142   * frame #0: 0x00007ffff7fd003c JIT(0x45c2cb0)`compute_factorial(n=1) at showdebug.c:9:1143      frame #1: 0x00007ffff7fd0095 JIT(0x45c2cb0)`main(argc=2, argv=0x00000000046122f0) at showdebug.c:16:18144      frame #2: 0x0000000002a8306e lli`llvm::MCJIT::runFunction(this=0x000000000458ed10, F=0x0000000004589ff8, ArgValues=ArrayRef<llvm::GenericValue> @ 0x00007fffffffc798) at MCJIT.cpp:554:31145      frame #3: 0x00000000029bdb45 lli`llvm::ExecutionEngine::runFunctionAsMain(this=0x000000000458ed10, Fn=0x0000000004589ff8, argv=size=0, envp=0x00007fffffffe140) at ExecutionEngine.cpp:467:10146      frame #4: 0x0000000001f2fc2f lli`main(argc=4, argv=0x00007fffffffe118, envp=0x00007fffffffe140) at lli.cpp:643:18147      frame #5: 0x00007ffff788c09b libc.so.6`__libc_start_main(main=(lli`main at lli.cpp:387), argc=4, argv=0x00007fffffffe118, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffe108) at libc-start.c:308:16148      frame #6: 0x0000000001f2dc7a lli`_start + 42149   (lldb) finish150   Process 21340 stopped151   * thread #1, name = 'lli', stop reason = step out152   Return value: (int) $2 = 120153 154      frame #0: 0x00007ffff7fd0095 JIT(0x45c2cb0)`main(argc=2, argv=0x00000000046122f0) at showdebug.c:16:9155      13       if (argc < 2)156      14           return -1;157      15       char firstletter = argv[1][0];158   -> 16       int result = compute_factorial(firstletter - '0');159      17160      18       // Returned result is clipped at 255...161      19       return result;162   (lldb) p result163   (int) $3 = 73670648164   (lldb) n165   Process 21340 stopped166   * thread #1, name = 'lli', stop reason = step over167      frame #0: 0x00007ffff7fd0098 JIT(0x45c2cb0)`main(argc=2, argv=0x00000000046122f0) at showdebug.c:19:12168      16       int result = compute_factorial(firstletter - '0');169      17170      18       // Returned result is clipped at 255...171   -> 19       return result;172      20   }173   (lldb) p result174   (int) $4 = 120175   (lldb) expr result=42176   (int) $5 = 42177   (lldb) p result178   (int) $6 = 42179   (lldb) c180   Process 21340 resuming181   Process 21340 exited with status = 42 (0x0000002a)182   (lldb) exit183