[Converted from Gnats 2294] Having a virtual const function in a class that is inherited from a base class using virtual inheritance causes a "Cannot access memory at address 0x0" error when doing a 'print' on that function from the gdb console. Taking out the virtual keyword from the inheritance of the base class solves the problem, or just removing the const from the function also fixes it. The problem seems to be from the call_function_by_hand() function in gdb/infcall.c, near the start of the function. You'll see the following two lines that were added in 6.6: if (TYPE_CODE (ftype) == TYPE_CODE_PTR) ftype = check_typedef (TYPE_TARGET_TYPE (ftype)); This replaces ftype with the target type if the function is a pointer, and eventually this leads to a xfer_partial memory read that has a NULL obj->beneath, and the memory read returns a -1. I wasn't able to track down why the virtual inheritance causes this bad memory read. As a temp work around, I have commented out the two lines of code and this fixes the problem. Please see the test case below to reproduce the error. I haven't tested this test case in 64bit mode, but my early tests showed the problem wasn't there if you compiled with 64bit and used a 64bit gdb. Release: gdb 6.6 Environment: 32bit ee3_0 using gnu g++ 3.2.3 and 4.1.1 gdb 6.6 How-To-Repeat: Compile with the following testcase and run the following commands at the gdb prompt: (gdb) break main (gdb) start (gdb) next (gdb) print o.do_print() -------- test case ------------------------ class interface { }; class Obj : virtual // comment out this line and it works public interface { public: virtual const char* do_print() const { return "Obj3"; } }; int main(int argc, char** argv) { Obj o; return 0; }
Fix: See description
Subject: Bug 9399 CVSROOT: /cvs/src Module name: src Changes by: cmoller@sourceware.org 2009-12-10 20:57:11 Modified files: gdb : ChangeLog MAINTAINERS valops.c gdb/testsuite : ChangeLog gdb/testsuite/gdb.cp: Makefile.in Added files: gdb/testsuite/gdb.cp: virtfunc2.cc virtfunc2.exp Log message: Fix for PR 9399 gdb can't call or print a const function that uses virtual inheritance Patches: http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.11141&r2=1.11142 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/MAINTAINERS.diff?cvsroot=src&r1=1.431&r2=1.432 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/valops.c.diff?cvsroot=src&r1=1.229&r2=1.230 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.2047&r2=1.2048 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.cp/virtfunc2.cc.diff?cvsroot=src&r1=NONE&r2=1.1 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.cp/virtfunc2.exp.diff?cvsroot=src&r1=NONE&r2=1.1 http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.cp/Makefile.in.diff?cvsroot=src&r1=1.6&r2=1.7
Problem was unnecessary casting in valops.c:value_cast_struct resulting in an attempt to dereference a null resulting from a value_zero. Fix was to return NULL if the type names are non-null and match.