Skip to content

[lldb][RFC] hot reload C/C++ functions from lldb #168799

@cs01

Description

@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 potentially integrating it into lldb. You can try it as described here https://github.com/cs01/lldbhotreload. To be clear it is basically a proof of concept in its current form.

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 C++ file with the modified code
  • call clang to build an .so out of it
  • dlload the .so
  • get the address the .so was loaded at
  • 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>
============================================================

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions