|
| 1 | +Debugging in JIT Compiled Code |
| 2 | +------------------------------ |
| 3 | + |
| 4 | +C++ Language Interoperability Layer - Debugging Guide |
| 5 | +====================================================== |
| 6 | + |
| 7 | +Overview |
| 8 | +======== |
| 9 | + |
| 10 | +This guide provides comprehensive instructions for debugging CppInterOp applications using LLDB. |
| 11 | + |
| 12 | +Prerequisites |
| 13 | +============= |
| 14 | + |
| 15 | +Before proceeding with debugging, ensure you have the following tools installed: |
| 16 | + |
| 17 | +- **LLDB**: The LLVM debugger |
| 18 | +- **LLVM/Clang**: The C++ compiler and toolchain |
| 19 | +- **CppInterOp**: The C++ language interoperability library |
| 20 | + |
| 21 | +The debugging tools should be available in your LLVM toolchain. On most systems, these are installed alongside your LLVM/Clang installation. |
| 22 | + |
| 23 | +Setting Up Debug Environment |
| 24 | +============================ |
| 25 | + |
| 26 | +To effectively debug CppInterOp applications, you need to compile your code with debugging symbols and disable optimizations. This ensures that the debugger can accurately map between source code and machine instructions. |
| 27 | + |
| 28 | +**Compilation Flags** |
| 29 | + |
| 30 | +When compiling your CppInterOp application, use the following essential flags: |
| 31 | + |
| 32 | +.. code-block:: bash |
| 33 | +
|
| 34 | + $CXX -I$CPPINTEROP_DIR/include -g -O0 -lclangCppInterOp -Wl,-rpath,$CPPINTEROP_DIR/build/lib |
| 35 | +
|
| 36 | +**Flag Explanation:** |
| 37 | + |
| 38 | +- ``-g``: Includes debugging information in the executable |
| 39 | +- ``-O0``: Disables compiler optimizations for clearer debugging |
| 40 | +- ``-I$CPPINTEROP_DIR/include``: Includes CppInterOp headers |
| 41 | +- ``-lclangCppInterOp``: Links against the CppInterOp library |
| 42 | +- ``-Wl,-rpath,$CPPINTEROP_DIR/build/lib``: Sets runtime library path |
| 43 | + |
| 44 | +Creating a Debug-Ready Test Program |
| 45 | +=================================== |
| 46 | + |
| 47 | +Here's a comprehensive example that demonstrates common CppInterOp usage patterns suitable for debugging: |
| 48 | + |
| 49 | +.. code-block:: cpp |
| 50 | +
|
| 51 | + #include "clang/Interpreter/CppInterOp.h" |
| 52 | + #include <iostream> |
| 53 | +
|
| 54 | + void run_code(std::string code) { |
| 55 | + Cpp::Declare(code.c_str()); |
| 56 | + } |
| 57 | +
|
| 58 | + int main(int argc, char *argv[]) { |
| 59 | + Cpp::CreateInterpreter({"-gdwarf-4", "-O0"}); |
| 60 | + std::vector<Cpp::TCppScope_t> Decls; |
| 61 | + std::string code = R"( |
| 62 | + #include <iostream> |
| 63 | + void f1() { |
| 64 | + std::cout << "in f1 function" << std::endl; |
| 65 | + } |
| 66 | + std::cout << "In codeblock 1" << std::endl; |
| 67 | + int a = 100; |
| 68 | + int b = 1000; |
| 69 | + )"; |
| 70 | + run_code(code); |
| 71 | + code = R"( |
| 72 | + f1(); |
| 73 | + )"; |
| 74 | + run_code(code); |
| 75 | + return 0; |
| 76 | + } |
| 77 | +
|
| 78 | +
|
| 79 | +**Program Structure Explanation:** |
| 80 | + |
| 81 | +This example demonstrates key debugging scenarios: |
| 82 | + |
| 83 | +1. **Interpreter Initialization**: The ``Cpp::CreateInterpreter`` call with debug flags |
| 84 | +2. **Code Declaration**: Dynamic C++ code execution through ``Cpp::Declare`` |
| 85 | +3. **Mixed Execution**: Combination of compiled and interpreted code paths |
| 86 | +4. **Variable Scoping**: Local variables in both compiled and interpreted contexts |
| 87 | + |
| 88 | +Debugging Strategies |
| 89 | +==================== |
| 90 | + |
| 91 | +**Debugging Compiled Code** |
| 92 | + |
| 93 | +For debugging the main executable and compiled portions of your CppInterOp application: |
| 94 | + |
| 95 | +.. code-block:: bash |
| 96 | +
|
| 97 | + lldb /path/to/executable |
| 98 | + (lldb) settings set plugin.jit-loader.gdb.enable on |
| 99 | + (lldb) breakpoint set --name f1 |
| 100 | + (lldb) r |
| 101 | + 1 location added to breakpoint 1 |
| 102 | + In codeblock 1 |
| 103 | + Process 49132 stopped |
| 104 | + * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 |
| 105 | + frame #0: 0x000000010217c008 JIT(0x10215c218) f1() at input_line_1:4:13 |
| 106 | +
|
| 107 | +**Note** |
| 108 | + |
| 109 | +1. Ensure the JIT loader is enabled to allow LLDB to debug dynamically generated code. |
| 110 | +2. Use ``settings set plugin.jit-loader.gdb.enable on`` to enable JIT debugging. |
| 111 | +3. Set breakpoints in both compiled and interpreted code using ``breakpoint set --name function_name``. |
| 112 | + |
| 113 | + |
| 114 | +**Some Caveats** |
| 115 | + |
| 116 | +1. For each block of code, there is a file named ``input_line_<execution_number>`` that contains the code block. This file is in-memory and thus cannot be directly accessed. |
| 117 | +2. However, generating actual input_line_<number> files on disk will let LLDB pick them up and render the source content correctly during debugging. This can be achieved by modifying run_code as follows: |
| 118 | + |
| 119 | +.. code-block:: cpp |
| 120 | +
|
| 121 | + void run_code(std::string code) { |
| 122 | + static size_t i = 0; |
| 123 | + i++; |
| 124 | + std::string filename = "input_line_" + std::to_string(i); |
| 125 | + std::ofstream file(filename); |
| 126 | + file << code; |
| 127 | + file.close(); |
| 128 | + Cpp::Declare(code.c_str()); |
| 129 | + } |
| 130 | +
|
| 131 | +.. note:: |
| 132 | + |
| 133 | + You'll need to manually delete these files later to avoid cluttering the filesystem. |
| 134 | + |
| 135 | +3. If a function is called from different cell, then it may take multiple step-ins to reach the function definition due to the way CppInterOp handles code blocks. |
| 136 | + |
| 137 | +Advanced Debugging Techniques |
| 138 | +============================= |
| 139 | + |
| 140 | +**Using LLDB with VS Code** |
| 141 | + |
| 142 | +For IDE-based debugging: |
| 143 | + |
| 144 | +1. Install the LLDB extension in VS Code |
| 145 | +2. Create a ``launch.json`` configuration: |
| 146 | + |
| 147 | +.. code-block:: json |
| 148 | +
|
| 149 | + { |
| 150 | + "version": "0.2.0", |
| 151 | + "configurations": [ |
| 152 | + { |
| 153 | + "type": "lldb-dap", |
| 154 | + "request": "launch", |
| 155 | + "name": "Debug", |
| 156 | + "program": "/path/to/executable", |
| 157 | + "sourcePath" : ["${workspaceFolder}"], |
| 158 | + "cwd": "${workspaceFolder}", |
| 159 | + "initCommands": [ |
| 160 | + "settings set plugin.jit-loader.gdb.enable on", // This is crucial |
| 161 | + ] |
| 162 | + }, |
| 163 | + ] |
| 164 | + } |
| 165 | +
|
| 166 | +
|
| 167 | +
|
| 168 | +Further Reading |
| 169 | +=============== |
| 170 | + |
| 171 | +- **LLDB Documentation**: `LLDB Debugger <https://lldb.llvm.org/>`_ |
| 172 | +- **CppInterOp Source**: `CppInterOp Repository <https://github.com/compiler-research/CppInterOp>`_ |
| 173 | +- **Clang Documentation**: `Clang Compiler <https://clang.llvm.org/docs/>`_ |
| 174 | +- **LLVM Debugging Guide**: `LLVM Debug Info <https://llvm.org/docs/SourceLevelDebugging.html>`_ |
| 175 | + |
| 176 | + |
0 commit comments