From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx1.firstpr.com.au (mx1.firstpr.com.au [149.28.173.46]) by sourceware.org (Postfix) with ESMTP id 11E343858D32 for ; Sat, 11 Feb 2023 09:03:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 11E343858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=firstpr.com.au Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=firstpr.com.au Received: from [127.0.0.1] (localhost [IPv6:::1]) by mx1.firstpr.com.au (Postfix) with ESMTP id A87BD1F5AF; Sat, 11 Feb 2023 09:03:17 +0000 (UTC) Message-ID: Date: Sat, 11 Feb 2023 20:03:10 +1100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: Compiling and injecting code in GDB: C++ in addition to C? Content-Language: en-US To: "gdb@sourceware.org" , Tom Tromey References: <1575f29a-498f-5c65-41cc-3ee134108d48@firstpr.com.au> <87k00pxxmx.fsf@tromey.com> From: Robin Whittle In-Reply-To: <87k00pxxmx.fsf@tromey.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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