public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug python/29079] New: Python pretty printer does not work for class members
@ 2022-04-20 23:24 asmwarrior at gmail dot com
  2022-04-21  3:02 ` [Bug python/29079] " asmwarrior at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: asmwarrior at gmail dot com @ 2022-04-20 23:24 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=29079

            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 = (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 = 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 = 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) == 'reco': return recoPrinter(val)
   return None

gdb.pretty_printers.append(CustomPrettyPrinters) 
--------------------------------------

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 breakpoint,
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 <http://gnu.org/licenses/gpl.html>
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:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

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 = <incomplete type>
(gdb) p ccc
$2 = {r = <incomplete type>, v = 18, f = 65.6999969}
(gdb) source test.py
(gdb) p sss
$3 = 5
(gdb) p ccc
$4 = {r = <incomplete type>, v = 18, f = 65.6999969}
(gdb)

--------------------------------------


You see, the "p sss" works OK after "source test.py", but the "p ccc" is still
not good.

I think the pretty printer is simple, because I don't even use pretty printer
for std library.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-09  9:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-20 23:24 [Bug python/29079] New: Python pretty printer does not work for class members asmwarrior at gmail dot com
2022-04-21  3:02 ` [Bug python/29079] " asmwarrior at gmail dot com
2023-12-08 17:22 ` cvs-commit at gcc dot gnu.org
2023-12-08 17:23 ` ssbssa at sourceware dot org
2023-12-09  9:03 ` asmwarrior at gmail dot com

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).