Bug 30057 - Pretty printing doesn't seem to happen for static members
Summary: Pretty printing doesn't seem to happen for static members
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: python (show other bugs)
Version: 12.1
: P2 normal
Target Milestone: 14.1
Assignee: Tom Tromey
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-01-27 20:41 UTC by Michael Welsh Duggan
Modified: 2023-05-06 16:51 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2023-04-22 00:00:00
Project(s) to access:
ssh public key:


Attachments
test program (98 bytes, text/x-c++src)
2023-01-27 20:41 UTC, Michael Welsh Duggan
Details
test program (previous was wrong file) (171 bytes, text/x-c++src)
2023-01-27 20:42 UTC, Michael Welsh Duggan
Details
The python pretty-printer (525 bytes, text/x-python)
2023-01-27 20:43 UTC, Michael Welsh Duggan
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Welsh Duggan 2023-01-27 20:41:05 UTC
Created attachment 14635 [details]
test program

For some reason, I'm having difficulty getting Python pretty printers to work properly on static members.  For example:

I compile the included program, foo.cpp:

  g++ -std=c++20 -ggdb3 -o foo foo.cpp

Then I set run gdb on the executable:

  gdb --quiet foo

Here is the session without the pretty printer:

  Reading symbols from foo...
  (gdb) start
  Temporary breakpoint 1 at 0x112d: file foo.cpp, line 22.
  Starting program: /home/md5i/tmp/gdbtest/foo 
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

  Temporary breakpoint 1, main () at foo.cpp:22
  22        X x;
  (gdb) n
  23      }
  (gdb) p x
  $1 = {a = {x = 0}, static b = {x = 1}, c = {x = 2}, 
    static d = <same as static member of an already seen type>}
  (gdb) p x.b
  $2 = {x = 1}
  (gdb) p x.d
  $3 = {x = 3}

Pretty much what you would expect, though what is that "<same as ... >" thing?

Then, loading the included python pretty printer:

  (gdb) source foo.py
  (gdb) p x
  $4 = {a = {Foo_A_B_C = 0}, static b = {x = 1}, c = {Foo_B_C_A = 2}, 
    static d = <same as static member of an already seen type>}
  (gdb) p x.b
  $5 = {Foo_A_B_C = 1}
  (gdb) p x.d
  $6 = {Foo_B_C_A = 3}

As you can see, b as part of x does not use the pretty printer.  And d has the same "<same as ... >" problem as before.
Comment 1 Michael Welsh Duggan 2023-01-27 20:42:31 UTC
Created attachment 14636 [details]
test program (previous was wrong file)

I attached the wrong file before.
Comment 2 Michael Welsh Duggan 2023-01-27 20:43:16 UTC
Created attachment 14637 [details]
The python pretty-printer

Attached the pretty printer
Comment 3 Hannes Domani 2023-01-28 20:00:26 UTC
With gdb-8.1.1 everything is output as expected (including member variable d).
And with gdb-8.2.1 onward I can reproduce the behavior of this bug report.

So both the <same as static member of an already seen type> output, and the missing pretty printing of static members, start with 8.2.1.
Comment 4 Tom Tromey 2023-04-22 01:15:07 UTC
I think the issue is the code to check for a repeated static member
is local to cp-valprint, so cp_print_static_field short-circuits
value printing to handle this case.

This also seems to miss that a union can have a static member.

This seems like something that should instead be handled globally
by the top-level value printer.
Comment 5 Tom Tromey 2023-04-27 02:39:50 UTC
I have a patch.
Comment 7 Sourceware Commits 2023-05-06 16:49:02 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3c0e312054aa6c46f0efba0ab111f83f0ea4caed

commit 3c0e312054aa6c46f0efba0ab111f83f0ea4caed
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Apr 21 18:53:48 2023 -0600

    Allow pretty-print of static members
    
    Python pretty-printers haven't applied to static members for quite
    some time.  I tracked this down to the call to cp_print_value_fields
    in cp_print_static_field -- it doesn't let pretty-printers have a
    chance to print the value.  This patch fixes the problem.
    
    The way that static members are handled is very weird to me.  I tend
    to think this should be done more globally, like in value_print.
    However, I haven't made any big change.
    
    Reviewed-by:  Keith Seitz <keiths@redhat.com>
    Tested-by:  Keith Seitz <keiths@redhat.com>
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30057
Comment 8 Tom Tromey 2023-05-06 16:51:05 UTC
Fixed.