Issue 168799
Summary [lldb][RFC] hot reload C++ functions from lldb
Labels new issue
Assignees
Reporter cs01
    I created a Python scripting bridge script to hot reload functions while in an lldb session. I wanted to see what folks thought of it, and depending on feedback integrating the SB script here as example, or building it into the C++ and exposing it as a built in command. You can try it as described here https://github.com/cs01/lldbhotreload. 

You can use it like so:
```
$ curl -O  https://raw.githubusercontent.com/cs01/lldbhotreload/main/src/hotreload.py
$ lldb ./your_program
(lldb) command script import hotreload.py
(lldb) b your_file.cpp:50
(lldb) run

# Edit your_file.cpp in your editor and save
(lldb) hotreload your_file.cpp
(lldb) continue
```
This is what the output looks like. It's kind of verbose but it does describe what it's doing. What it does is generate a temporary file with the modified code, build a .so, dlload the .so, get its address, patch the original function to jump to the new function in the new .so. More details are available in the github repo.

```
hotreload example.cpp
Hot reloading: example.cpp
  Found 1 functions: ['addOne(int)']
  Compiling .so...
  → (int)dlclose((void*)0x417300)
  → dlclose() succeeded
  → g++ -std=c++17 -g -O0 -fPIC -shared -o /tmp/lldb_hotreload/hotreload_example_de7d20e4.so /tmp/lldb_hotreload/hotreload_example_de7d20e4.cpp
  Compiled to /tmp/lldb_hotreload/hotreload_example_de7d20e4.so
  → (void*)dlopen("/tmp/lldb_hotreload/hotreload_example_de7d20e4.so", 2 | 256)
  → dlopen() returned handle 0x417300
  → LLDB auto-detected module: hotreload_example_de7d20e4.so
  Processing addOne(int) @ 0x401176
  → ((void*(*)())dlsym((void*)0x417300, "__addOne_hotreload_de7d20e4_ptr"))())
  → Resolved addOne(int) to 0x7ffff7fb5169
  Re-patching addOne(int): 0x401176 → 0x7ffff7fb5169
  → WriteMemory(0x401176, 21 bytes)
     [48 b8 69 51 fb f7 ff 7f 00 00 ff e0] + 9 NOPs
     Disassembly: movabsq $0x7ffff7fb5169, %rax; jmp *%rax; nop×9
  Deleted 1 old breakpoint(s) from previous hot reloads
✓ Patched 1/1 functions
  Auto-breakpoint: addOne(int) at hotreload_example_de7d20e4.cpp:23 (addr 0x7ffff7fb5169)
============================================================
  ✓ Created 1 auto-breakpoint(s) in hot-reloaded code
  Next 'continue' will hit breakpoints in new code!
  Manual breakpoints: b /tmp/lldb_hotreload/hotreload_example_de7d20e4.cpp:<line>
============================================================
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to