public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug python/17728] New: gdb.LazyString is confused by typedefs
@ 2014-12-18 13:55 jwakely.gcc at gmail dot com
  2014-12-18 14:00 ` [Bug python/17728] " jwakely.gcc at gmail dot com
  0 siblings, 1 reply; 2+ messages in thread
From: jwakely.gcc at gmail dot com @ 2014-12-18 13:55 UTC (permalink / raw)
  To: gdb-prs

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

            Bug ID: 17728
           Summary: gdb.LazyString is confused by typedefs
           Product: gdb
           Version: 7.8
            Status: NEW
          Severity: normal
          Priority: P2
         Component: python
          Assignee: unassigned at sourceware dot org
          Reporter: jwakely.gcc at gmail dot com

tmp$ cat > str.cc
struct Foo {
    Foo() : str("foo"), len(3) { }
    const char* str;
    int len;
};

struct Bar {
    Bar() : str("bar"), len(3) { }
    typedef const char* pointer;
    pointer str;
    int len;
};

int main()
{
  Foo foo;
  Bar bar;
  return 0;
}
tmp$ cat > a.out-gdb.py
import gdb

class StrPrinter:
    "Print a stringy thingie"

    def __init__ (self, val):
        self.val = val

    def to_string (self):
        s = self.val['str']
        l = self.val['len']
        return s.lazy_string(length = l)

    def display_hint(self):
        return 'string'

def str_lookup_function(val):
    lookup_tag = val.type.tag
    if lookup_tag == "Foo" or lookup_tag == "Bar":
        return StrPrinter(val)
    return None

gdb.current_objfile().pretty_printers.append(str_lookup_function)
tmp$ g++ -g str.cc
tmp$ ~/gcc/gdb/7.8.50/bin/gdb 
GNU gdb (GDB) 7.8.50.20141216-cvs
Copyright (C) 2014 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-unknown-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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".
(gdb) set auto-load safe-path /
(gdb) file a.out
Reading symbols from a.out...done.
(gdb) start
Temporary breakpoint 1 at 0x4005b8: file str.cc, line 16.
Starting program: /tmp/a.out 

Temporary breakpoint 1, main () at str.cc:16
16        Foo foo;
(gdb) n
17        Bar bar;
(gdb) p foo
$1 = "foo"
(gdb) n
18        return 0;
(gdb) p bar
$2 = "\x4006b4\003\000\000\000\000\000\000\000\x4006b0"
(gdb) 

The LazyString seems to be confused by the fact that Bar::str is declared with
a typedef, so it prints the contents of &bar.str instead of bar.str

The only solution I've found is to strip the typedef first:

    def to_string (self):
        s = self.val['str']
        s = s.cast(s.type.strip_typedefs())
        l = self.val['len']
        return s.lazy_string(length = l)

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


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

* [Bug python/17728] gdb.LazyString is confused by typedefs
  2014-12-18 13:55 [Bug python/17728] New: gdb.LazyString is confused by typedefs jwakely.gcc at gmail dot com
@ 2014-12-18 14:00 ` jwakely.gcc at gmail dot com
  0 siblings, 0 replies; 2+ messages in thread
From: jwakely.gcc at gmail dot com @ 2014-12-18 14:00 UTC (permalink / raw)
  To: gdb-prs

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

--- Comment #1 from Jonathan Wakely <jwakely.gcc at gmail dot com> ---
This seems odd:

(gdb) python print StrPrinter(gdb.parse_and_eval('foo')).to_string().type
const char
(gdb) python print StrPrinter(gdb.parse_and_eval('bar')).to_string().type
Bar::pointer

https://sourceware.org/gdb/onlinedocs/gdb/Lazy-Strings-In-Python.html says type
"will always be a pointer type" but the lazy string for foo is not a pointer.
The lazy string for bar is a typedef for a pointer, but doesn't work correctly.

Also odd:

(gdb) python print StrPrinter(gdb.parse_and_eval('foo')).to_string().value()
102 'f'
(gdb) python print StrPrinter(gdb.parse_and_eval('bar')).to_string().value()
0x4006b4 "bar"

The LazyString.value() method shows the right thing for bar, but not for foo
(which is at least consistent with the .type attribute which says that foo's
type is just a char not a char*)

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


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

end of thread, other threads:[~2014-12-18 14:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-18 13:55 [Bug python/17728] New: gdb.LazyString is confused by typedefs jwakely.gcc at gmail dot com
2014-12-18 14:00 ` [Bug python/17728] " jwakely.gcc 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).