Compiling and injecting code in GDB: C++ in addition to C?

Robin Whittle rw@firstpr.com.au
Wed Feb 8 16:34:43 GMT 2023


A more detailed description of my experiences is at: 
https://stackoverflow.com/questions/75387541/compiling-and-injecting-code-in-gdb-c-rather-than-c 
.  Briefly:

    I am running gdb 10.1.90.20210103-git and gcc/g++ 10.2.1 20210110 on
    x86-64 Debian 11.

    I can compile C code and have it run in the context of the paused
    inferior, but as soon as I introduce one or more lines of C++ code,
    no compilation or at least no run of the code occurs.  I get no
    error messages.

    https://www.sourceware.org/gdb/news/ mentions that GDB 8.3.1 was
    released on 2019-09-20, with: "Experimental support for compilation
    and injection of C++ source code into the inferior (requires GCC 7.1
    or higher, built with libcp1.so)."

    I have /usr/lib/x86_64-linux-gnu/libcc1.so.o but no libcp1.so
    there.  Little seems to have changed in libcp1.so in recent years:
    https://github.com/gcc-mirror/gcc/commits/master/libcc1/libcp1.cc .

Is it possible to inject C++ code, with any version of gdb and gcc/g++?

Is there any other documentation I should be looking at?  The main gdb 
documentation of code compilation and injection 
https://sourceware.org/gdb/onlinedocs/gdb/Compiling-and-Injecting-Code.html#set-debug-compile 
only gives C examples.

I want this capability so I can easily dump the contents of large, 
complex, vectors of class objects to a text file, likely multiple times 
the middle of a debug session.  Also I would like to be able to modify 
such data programmatically.

Thanks very much for gdb and gcc!


   Best regards

     Robin Whittle



More information about the Gdb mailing list