This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: GDB pretty printers for iterators
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: libstdc++ <libstdc++ at gcc dot gnu dot org>, Tom Tromey <tromey at redhat dot com>, Phil Muldoon <pmuldoon at redhat dot com>
- Date: Tue, 16 Mar 2010 21:23:35 -0700
- Subject: Re: GDB pretty printers for iterators
- References: <4348dea51003161758o51f7490elc4dc5c1c1140a0cb@mail.gmail.com>
On Tue, Mar 16, 2010 at 05:58:35PM -0700, Jonathan Wakely wrote:
> I've been playing with the GDB python printers again, and noticed that
> printing an iterator just dereferences it and prints the value it
> points to:
>
> Breakpoint 1, main () at prettyprinter.cc:5
> 5 std::vector<int> s;
> (gdb) n
> 6 s.push_back(2);
> (gdb)
> 7 std::vector<int>::iterator j = s.begin();
> (gdb)
> 8 std::vector<int>::iterator k = s.end();
> (gdb)
> 9 return *j;
> (gdb) p j
> $1 = 2
>
> I think I'd rather have iterators print something like "iterator
> pointing to 2" or even "-> 2"
Even that is going to be a problem, if for example you're
debugging code that computes distances between random iterators,
for example.
(gdb) p *j
should print 2, but printing the iterator should reveal the internals,
I think. If gdb has special knowledge of the meaning of the interals,
that can be reported, but if any information is left out, you'll
hamper debugging.
> For the end iterator the value is misleading at best:
>
> (gdb) p k
> $2 = 0
>
> We shouldn't be dereferencing past-the-end iterators. Ideally that
> would print a special value like "past-the-end" but I don't think that
> is possible in general, only in debug mode.
Right, in some cases an iterator will just be a pointer, and the end
iterator just points one past the end of the storage. For cases where you
can tell, the printout could say that it's past the end. For debug
iterators you can tell what sequence it's attached to and whether it's
valid, so maybe the fancy printout should just support debug iterators,
and perhaps some other cases where you have the info. For basic
iterators, as long as gdb can do the right thing for the * and ->
operators, I think it's good.
> Does anyone have any good ideas for improving the output? I can look
> into it, but it probably won't be soon.
Imagine if, when printing a const char* pointer from gdb, you only
get the character it points to and you can't see its address. You
risk building something pretty but unusable. An iterator is a
generalization of a pointer. A C or C++ user isn't expecting automatic
dereference (a Java or C# user might).