* Compiling and injecting code in GDB: C++ in addition to C?
@ 2023-02-08 16:34 Robin Whittle
2023-02-10 17:01 ` Tom Tromey
0 siblings, 1 reply; 3+ messages in thread
From: Robin Whittle @ 2023-02-08 16:34 UTC (permalink / raw)
To: gdb
[-- Attachment #1: Type: text/plain, Size: 1593 bytes --]
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Compiling and injecting code in GDB: C++ in addition to C?
2023-02-08 16:34 Compiling and injecting code in GDB: C++ in addition to C? Robin Whittle
@ 2023-02-10 17:01 ` Tom Tromey
2023-02-11 9:03 ` Robin Whittle
0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2023-02-10 17:01 UTC (permalink / raw)
To: Robin Whittle; +Cc: gdb
>>>>> "Robin" == Robin Whittle <rw@firstpr.com.au> writes:
Robin> Is it possible to inject C++ code, with any version of gdb and gcc/g++?
My experience is that the C++ plugin isn't very functional. It seems to
have some bugs and crash pretty frequently as well. Also, it seems like
nobody is actively working on it.
Robin> I want this capability so I can easily dump the contents of large,
Robin> complex, vectors of class objects to a text file, likely multiple
Robin> times the middle of a debug session. Also I would like to be able to
Robin> modify such data programmatically.
I wonder if you could do some trick like have a library, then use
'compile' in C mode to dlopen it. Not great of course.
Tom
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Compiling and injecting code in GDB: C++ in addition to C?
2023-02-10 17:01 ` Tom Tromey
@ 2023-02-11 9:03 ` Robin Whittle
0 siblings, 0 replies; 3+ messages in thread
From: Robin Whittle @ 2023-02-11 9:03 UTC (permalink / raw)
To: gdb, Tom Tromey
Hi Tom,
Thanks very much for your reply. Here is what I learned, as I wrote as
an answer to my question at:
https://stackoverflow.com/questions/75387541/compiling-and-injecting-code-in-gdb-c-rather-than-c/75419090#75419090.
Best regards
Robin
Summary: GDB's C++ compilation and injection system does not work for
me, and it is reported to be buggy or non-functional. I only tested
this "compile" command with single lines of C code, but it worked fine
for these. I was able to run my function, which writes a text dump file
containing the valeus of a vector of class objects' data members, while
the inferior was paused after a breakpoint, by including it in a source
file and calling it with GDB's "print" or "call" commands, giving it the
required input argument of the name of the vector to dump.
I haven't tried using GDB on its own, but at present I have no reason to
believe that Codelite is interfering with my ability to communicate with
GDB via Codelite's "Debugger > Output" console.
I wrote to the GDB Developer list and received what I assume is a well
informed reply. I was told that in my respondent's experience "the C++
plugin isn't very functional. It seems to have some bugs and crash
pretty frequently as well. Also, it seems like nobody is actively
working on it." This is consistent with my experience to date.
Perhaps later versions of GDB would work better. However, there has
been minimal change to the library
https://github.com/gcc-mirror/gcc/commits/master/libcc1/libcp1.cc since
2017, and I could find no indication of new work on C++ compilation and
injection in https://www.sourceware.org/gdb/news/ since the announcement
of its introduction:
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)."
It is possible that my problem is related to not having this library. I
do have the C equivalent: /usr/lib/x86_64-linux-gnu/libcc1.so.o. I
could find no such library in a Debian package. It is possible I could
get this going by compiling the latest GCC/G++ and GDB, but that is
pretty daunting, involving installing later standard libraries than my
current Debian installation has, while making those and the newer G++
and GDB available for Codelite sessions, but not interfering with other
software on machine which needs the older compilation system.
The respondent suggested that instead of compiling and injecting fresh
code in the middle of a debugging session, I could write it as a
library, and run it by using the compile command, on a purely C piece of
code which would call it. However, for my purposes, the function needs
to be passed one or more C++ constructs, and any code which called a C++
function would be C++ code and so, presumably, not be compiled by GDB.
For my immediate purpose of doing text dumps of large vectors of class
objects' data members, I was able to succeed by writing such a function
in an existing source file, which also contains the functions I am
debugging. Then I could call it with GDB's "print" or "call" commands.
I imagine this would work if the debugging function was in any other
linked source file, or in a linked library. (Perhaps there is a way of
writing such a library, compiling and linking it while the inferior is
halted.)
I was able to run this, from a debugging session in which the inferior
was halted somewhere, with either the "print" or "call" command followed
by the name of the function, with the name of the vector in brackets.
There must be no semicolon. I found the "print" command better, since
GDB would report the return value, which was "void". My function used
C++ constructs such as "<<" to write to the text file.
A call within my dump function of the C function "system("pwd")"
resulted in the current directory being output on the inferior's
terminal window. Likewise: "print system("pwd")". "print system("mc")"
dutifully ran Midnight Commander in the inferior's terminal window.
I altered the function to return a string, which GDB reported in the GDB
"Debugger > Output" console. I added a line: std::cout << "xyz"; - but
nothing appeared on either the inferior's terminal window or in the GDB
console.
My attempts to use the compile command to call my dump function in
exactly the same way produced no results, with or without a trailing
semicolon.
I found that"print" and "call" work with C and C++ code while "compile"
only works with C code.
I tried print vvv[6].mmm for data members which were integers, floats,
and strings and GDB returned their values correctly. Adding a ++ at the
end caused it to return the original integer value, but the value itself
was incremented.
When I tried "print" with a compound line of code, with a semicolon
between the two logical lines, with and without one at the end, there
was no response.
The GDB Developers list is not the place for support requests, but I did
not know a better way to find out about the status of this facility.
On 2023-02-11 4:01 am, Tom Tromey wrote:
> Robin> Is it possible to inject C++ code, with any version of gdb and gcc/g++?
>
> My experience is that the C++ plugin isn't very functional. It seems to
> have some bugs and crash pretty frequently as well. Also, it seems like
> nobody is actively working on it.
>
> Robin> I want this capability so I can easily dump the contents of large,
> Robin> complex, vectors of class objects to a text file, likely multiple
> Robin> times the middle of a debug session. Also I would like to be able to
> Robin> modify such data programmatically.
>
> I wonder if you could do some trick like have a library, then use
> 'compile' in C mode to dlopen it. Not great of course.
>
> Tom
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-02-11 9:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08 16:34 Compiling and injecting code in GDB: C++ in addition to C? Robin Whittle
2023-02-10 17:01 ` Tom Tromey
2023-02-11 9:03 ` Robin Whittle
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).