From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 787B23858D1E; Wed, 20 Apr 2022 23:24:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 787B23858D1E From: "asmwarrior at gmail dot com" To: gdb-prs@sourceware.org Subject: [Bug python/29079] New: Python pretty printer does not work for class members Date: Wed, 20 Apr 2022 23:24:57 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gdb X-Bugzilla-Component: python X-Bugzilla-Version: HEAD X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: asmwarrior at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gdb-prs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-prs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Apr 2022 23:24:57 -0000 https://sourceware.org/bugzilla/show_bug.cgi?id=3D29079 Bug ID: 29079 Summary: Python pretty printer does not work for class members Product: gdb Version: HEAD Status: UNCONFIRMED Severity: normal Priority: P2 Component: python Assignee: unassigned at sourceware dot org Reporter: asmwarrior at gmail dot com Target Milestone: --- When I define a Python pretty printer, I see it only works for a normal variable, but not class member variable. Here is a simple test case, it contains 3 files: main.cpp, reco.h and reco.= cpp. main.cpp -------------------------------------- #include "reco.h" class CTest { public: CTest(float input) :r(input), v(18), f(65.7) { } reco r; int v; float f; }; double DummyFunctionForwxreco(void *dummy) { reco* p =3D (reco*)dummy; return p->getWeight(); } int main() { reco sss(5.0); CTest ccc(7.8); return 0; } -------------------------------------- reco.h -------------------------------------- #ifndef RECO_H #define RECO_H class reco { public: reco(float input); ~reco(void); virtual float getWeight(); private: float weight; }; #endif -------------------------------------- reco.cpp -------------------------------------- #include "reco.h" reco::reco(float input) { weight =3D input; } reco::~reco(void) { } float reco::getWeight() { return weight; } -------------------------------------- To build the main.exe, you have to type the following command, not the "-g0" option means you don't have debug information in the reco.o. -------------------------------------- gcc -c -g0 reco.cpp gcc -c -g main.cpp g++ -o main.exe main.o reco.o -------------------------------------- The python pretty printer test.py -------------------------------------- import gdb import sys class recoPrinter: def __init__(self, val): self.val =3D val def to_string(self): return gdb.parse_and_eval('DummyFunctionForwxreco((void*)%s)' % self.val.address ) # call the dummy function here! def CustomPrettyPrinters(val): if str(val.type) =3D=3D 'reco': return recoPrinter(val) return None gdb.pretty_printers.append(CustomPrettyPrinters)=20 -------------------------------------- Here is the debug session: you set the breakpoint by this command "b main.cpp:27", and run the exe file by command "r", when it hit the breakpoi= nt, you can try to print some variables. -------------------------------------- # gdb main.exe GNU gdb (GDB) 11.2 Copyright (C) 2022 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-w64-mingw32". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from main.exe... (gdb) b main.cpp:27 Breakpoint 1 at 0x1400015a3: file main.cpp, line 27. (gdb) r Starting program: E:\code\gdb-incomplete-type\main.exe Breakpoint 1, main () at main.cpp:27 27 return 0; (gdb) p sss $1 =3D (gdb) p ccc $2 =3D {r =3D , v =3D 18, f =3D 65.6999969} (gdb) source test.py (gdb) p sss $3 =3D 5 (gdb) p ccc $4 =3D {r =3D , v =3D 18, f =3D 65.6999969} (gdb) -------------------------------------- You see, the "p sss" works OK after "source test.py", but the "p ccc" is st= ill not good. I think the pretty printer is simple, because I don't even use pretty print= er for std library. --=20 You are receiving this mail because: You are on the CC list for the bug.=