GDB recently introduced the compile
command to inject code at runtime, see this answer for requirements and a minimal example.
But I noticed that a few
The compile
command works by emitting a new function, compiling it with gcc, and then invoking the function from gdb (an "inferior function call" in gdb lingo).
The code generator does have some special features to make it possible to access local variables. In particular it translates DWARF location expressions to C. References to registers are translated into references to fields in a special struct
. gdb arranges to copy the relevant registers into an instance of this struct when performing the inferior call. After the call has completed, it copies the registers back out -- this allows writes to local variables.
This description should, I think, make it clearer what will work and what will not. I would expect return
and other flow-of-control operations (break
, continue
, goto
) not to work.
Writing to a register should work, but only for registers which are needed by some location expression. This could perhaps be fixed; though I believe right now only the necessary registers are passed in for performance reasons.
I don't know what would happen if your compiled code calls longjmp
or throw
(well, when C++ is implemented). Probably madness.
One thing worth knowing is that this code was designed so that a future patch could add compiled breakpoint conditions, perhaps in conjunction with something like dyninst.