Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* how to view content in stl vector
@ 2007-06-12 15:05 kdsfinger
  2007-06-15 19:52 ` Mark Kettenis
  0 siblings, 1 reply; 15+ messages in thread
From: kdsfinger @ 2007-06-12 15:05 UTC (permalink / raw)
  To: gdb

hi, all
How may I view the content in a stl vector in ddd? (or the command to
print it in gdb?). It seems I can only view the first element. I
searched google but did not find answer. Thanks for help.
zl2k


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

* Re: how to view content in stl vector
  2007-06-12 15:05 how to view content in stl vector kdsfinger
@ 2007-06-15 19:52 ` Mark Kettenis
  2007-06-18  8:53   ` Alpár Jüttner
  0 siblings, 1 reply; 15+ messages in thread
From: Mark Kettenis @ 2007-06-15 19:52 UTC (permalink / raw)
  To: kdsfinger; +Cc: gdb

> Date: Tue, 12 Jun 2007 11:05:21 -0400
> From: kdsfinger@gmail.com
> 
> hi, all
> How may I view the content in a stl vector in ddd? (or the command to
> print it in gdb?). It seems I can only view the first element. I
> searched google but did not find answer. Thanks for help.

There isn't a simple way.  You'll have to dig through the data
structure yourself, which requires knowledge about the particulat stl
implementation.  In a running program you might be able to call the
appropriate member functions (like operator[]), but doing so in the
right way isn't easy for heavily templated code used in most stl
implementations.  And that doesn't work in core dumps.

I generally consider C++ code to be undebuggable.

Mark


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

* Re: how to view content in stl vector
  2007-06-15 19:52 ` Mark Kettenis
@ 2007-06-18  8:53   ` Alpár Jüttner
  2007-06-18 11:15     ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Alpár Jüttner @ 2007-06-18  8:53 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: kdsfinger, gdb

In a running On Fri, 2007-06-15 at 21:52 +0200, Mark Kettenis wrote:
> > Date: Tue, 12 Jun 2007 11:05:21 -0400
> > From: kdsfinger@gmail.com
> > 
> > hi, all
> > How may I view the content in a stl vector in ddd? (or the command to
> > print it in gdb?). It seems I can only view the first element. I
> > searched google but did not find answer. Thanks for help.

You can simply use the member functions and operators of std::vector<>
in the p[rint] gdb command. For example:

(gdb) p v.size()
$1 = 8
(gdb) p v[2]
$2 = (double &) @0x804c048: 12

This works perfectly provided that
      * optimization is turned off and debugging (-ggdb) is turned on,
      * operator[] (or any other query function you would like to use)
        is used at least once in the code, thus its code is put in the
        executable,
      * you debug a running process, not a core dump.

> I generally consider C++ code to be undebuggable.

I wouldn't say that. It is at least as well debuggable as a C code.
(Debugging any non-trivial data structure in C is also very cumbersome).
The only major difficulty I frequently encounter is that debugging of
the STL header files cannot be turned off.

Regards,
Alpar



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

* Re: how to view content in stl vector
  2007-06-18  8:53   ` Alpár Jüttner
@ 2007-06-18 11:15     ` Maik Beckmann
  2007-06-18 11:27       ` Daniel Jacobowitz
  0 siblings, 1 reply; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 11:15 UTC (permalink / raw)
  To: gdb

Am Montag, 18. Juni 2007 10:52:00 schrieb Alpár Jüttner:
> In a running On Fri, 2007-06-15 at 21:52 +0200, Mark Kettenis wrote:
> > > Date: Tue, 12 Jun 2007 11:05:21 -0400
> > > From: kdsfinger@gmail.com
> > >
> > > hi, all
> > > How may I view the content in a stl vector in ddd? (or the command to
> > > print it in gdb?). It seems I can only view the first element. I
> > > searched google but did not find answer. Thanks for help.

Hello

If you add this to your .gdbinit 

<.gdbinit>
define dump_vector_simple

    set $it = $arg0.data()
    set $size = $arg0.size()
    set $end = $it + $size

    set $i = 0
    while ($it != $end)

        printf "[%u] == " , $i
        output *($it)
        printf "\n"

        set $it++
        set $i++
    end

end

define dump_vector_as_virtual_array

    set $it = $arg0.data()
    set $size = $arg0.size()

    output *$it@$size
    printf "\n"

end
</.gdbinit>

the gdb output  for this program
<test.cpp>
#include <vector>

int main(int argc, char **argv)
{

    std::vector<double> vec;

    for(int i = 0; i < 10; i++)
        vec.push_back(i+1);

    vec.data(); // dummy for gdb

    return 0; // breakpoint here
}
</test.cpp>

will be

<gdb>
dump_vector_simple vec
[0] == 1
[1] == 2
[2] == 3
[3] == 4
[4] == 5
[5] == 6
[6] == 7
[7] == 8
[8] == 9
[9] == 10
</gdb>

or  respectively

<gdb>
dump_vector_as_virtual_array vec
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
</gdb>


HTH, Maik Beckmann

PS: I'm using g++-4.1.2 and gdb-6.6


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

* Re: how to view content in stl vector
  2007-06-18 11:15     ` Maik Beckmann
@ 2007-06-18 11:27       ` Daniel Jacobowitz
  2007-06-18 12:27         ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel Jacobowitz @ 2007-06-18 11:27 UTC (permalink / raw)
  To: Maik Beckmann; +Cc: gdb

On Mon, Jun 18, 2007 at 01:14:44PM +0200, Maik Beckmann wrote:
> Hello
> 
> If you add this to your .gdbinit 
> 
> <.gdbinit>
> define dump_vector_simple

[snip]

FYI: I'm planning for a future version of GDB to be able to do this
automatically in the "print" command.  No promises on when it will be
ready, though.  Might be another two years.

-- 
Daniel Jacobowitz
CodeSourcery


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

* Re: how to view content in stl vector
  2007-06-18 11:27       ` Daniel Jacobowitz
@ 2007-06-18 12:27         ` Maik Beckmann
  2007-06-18 12:36           ` Daniel Jacobowitz
  0 siblings, 1 reply; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 12:27 UTC (permalink / raw)
  To: gdb

Am Montag, 18. Juni 2007 13:27:21 schrieb Daniel Jacobowitz:
> > define dump_vector_simple
>
> FYI: I'm planning for a future version of GDB to be able to do this
> automatically in the "print" command.  No promises on when it will be
> ready, though.  Might be another two years.

Hello Daniel,

First of all, I'm new to gdb (don't ask me why I didn't used a debugger until 
last weok, there is no rational reason).

Native c++/stl support for gdb would be nice. Until then using user defined
gdb-functions is a good workarround.
 
Regarding user defined functions, doing this for std::vector is easy. 
It took only one day to write the scripts I posted (enforced by google and 
gmane).
But there are others like std::map where one has to cast to get the content of 
a node

i.e. for std::map<int, char> you have to do
<gdb>
set $h = $map_instance_name._M_t._M_impl._M_header
p (('std::_Rb_tree_node<std::pair<const int, char> >' *) $h)->_M_value_field 
</gdb>

I tried to do this more generic:

<gdb>
set $h = $map_instance_name._M_t._M_impl._M_header

set $key_type = 'int'
set $mapped_type = 'char'
set $node_type = 'std::_Rb_tree_node<std::pair<const $key_type, $mapped_type> 
>'

p (( $node_type *) $h)->_M_value_field 
</gdb>

which would make things like

<gdb>
dump_std_map mymap int char
</gdb>

possible. But it didn't work :(

Is storing of typenames possible at all?
i.e.
<gdb>
set $type = int // syntax error!
set $type = 'int' // syntax error!
</gdb> 

A workaround for this would be a native c++ map-dump function, which naturally 
would be implemented as function template, but I didn't find out how to call 
an instantiated function template  :(


It would be nice if you could give me a hint towards function templates and 
the typename storing issue.

MfG, Maik Beckmann


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

* Re: how to view content in stl vector
  2007-06-18 12:27         ` Maik Beckmann
@ 2007-06-18 12:36           ` Daniel Jacobowitz
  2007-06-18 12:54             ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel Jacobowitz @ 2007-06-18 12:36 UTC (permalink / raw)
  To: Maik Beckmann; +Cc: gdb

On Mon, Jun 18, 2007 at 02:26:36PM +0200, Maik Beckmann wrote:
> Is storing of typenames possible at all?

No.  GDB's command line scripting language does not support this sort
of thing.  My plan is a more sophisticated Python integration.

> A workaround for this would be a native c++ map-dump function, which naturally 
> would be implemented as function template, but I didn't find out how to call 
> an instantiated function template  :(

It should work just fine.... I don't know for sure though.

-- 
Daniel Jacobowitz
CodeSourcery


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

* Re: how to view content in stl vector
  2007-06-18 12:36           ` Daniel Jacobowitz
@ 2007-06-18 12:54             ` Maik Beckmann
  2007-06-18 13:06               ` Daniel Jacobowitz
  0 siblings, 1 reply; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 12:54 UTC (permalink / raw)
  To: gdb

Am Montag, 18. Juni 2007 14:36:50 schrieb Daniel Jacobowitz:
> On Mon, Jun 18, 2007 at 02:26:36PM +0200, Maik Beckmann wrote:
> > Is storing of typenames possible at all?
>
> No.  GDB's command line scripting language does not support this sort
> of thing.  My plan is a more sophisticated Python integration.
>
> > A workaround for this would be a native c++ map-dump function, which
> > naturally would be implemented as function template, but I didn't find
> > out how to call an instantiated function template  :(
>
> It should work just fine.... I don't know for sure though.

This is an example:

source:
 - main.cpp 
 - Foo.h 
 - Foo.cpp

<main.cpp>
call foo(i)
No symbol "foo" in current context.
call bar(i)
$1 = true
</main.cpp>

<Foo.h>
#ifndef FOO_H_
#define FOO_H_

template<typename T>
bool foo(T val);

bool bar(int val);

#endif /*FOO_H_*/

</Foo.h>

<Foo.cpp>
#include "Foo.h"
#include <iostream>


template<typename T>
bool foo(T val)
{
    std::cout << "Hello from foo" << std::endl;

    return true;
}

template bool foo(int); // instantiates foo<int>(int)

bool bar(int val)
{
    std::cout << "Hello from bar" << std::endl;

    return true;
}
</Foo.cpp>

gdb says
<gdb>
call foo(i) # the template function
No symbol "foo" in current context.
call bar(i)
$1 = true
<gdb>


MfG, Maik


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

* Re: how to view content in stl vector
  2007-06-18 12:54             ` Maik Beckmann
@ 2007-06-18 13:06               ` Daniel Jacobowitz
  2007-06-18 14:35                 ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel Jacobowitz @ 2007-06-18 13:06 UTC (permalink / raw)
  To: Maik Beckmann; +Cc: gdb

On Mon, Jun 18, 2007 at 02:53:21PM +0200, Maik Beckmann wrote:
> <main.cpp>
> call foo(i)
> No symbol "foo" in current context.

You have to specify the instantiation manually, sorry.  Figuring out
which teplated and/or overloaded function to call is one of the
hardest parts of a C++ compiler front end; when I spoke to Mark
Mitchell about this, he estimated it at about 10,000 lines of code.
In light of that it's not surprising that GDB can't do it.  You have
to give it some help.

-- 
Daniel Jacobowitz
CodeSourcery


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

* Re: how to view content in stl vector
  2007-06-18 13:06               ` Daniel Jacobowitz
@ 2007-06-18 14:35                 ` Maik Beckmann
  2007-06-18 14:44                   ` Daniel Jacobowitz
  0 siblings, 1 reply; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 14:35 UTC (permalink / raw)
  To: gdb

Am Montag, 18. Juni 2007 15:06:35 schrieb Daniel Jacobowitz:

> You have to specify the instantiation manually

hm, how is this be done? 
I tried this
<gdb> call foo<int>(i) </gdb>
but gdb responds 
<gdb>No symbol "foo<int>" in current context.</gdb>

or is it about the mangled symbol from the disassembly?
    0x08048972 <main+30>: call  0x8048b66 <_Z3fooIiEbT_>

Doing
<gdb>p _Z3fooIiEbT_(i) </gdb> 
crahes gdb.
( btw. 
<gdb>p p _Z3fooIiEbT_ </gdb> 
gives "{bool (int)} 0x8048b66 <bool foo<int>(int)>" 
)

How looks the line for calling foo correctly?


thanks in advance, 
Maik Beckmann



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

* Re: how to view content in stl vector
  2007-06-18 14:35                 ` Maik Beckmann
@ 2007-06-18 14:44                   ` Daniel Jacobowitz
  2007-06-18 14:57                     ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel Jacobowitz @ 2007-06-18 14:44 UTC (permalink / raw)
  To: Maik Beckmann; +Cc: gdb

On Mon, Jun 18, 2007 at 04:34:31PM +0200, Maik Beckmann wrote:
> Am Montag, 18. Juni 2007 15:06:35 schrieb Daniel Jacobowitz:
> 
> > You have to specify the instantiation manually
> 
> hm, how is this be done? 
> I tried this
> <gdb> call foo<int>(i) </gdb>
> but gdb responds 
> <gdb>No symbol "foo<int>" in current context.</gdb>
> 
> or is it about the mangled symbol from the disassembly?
>     0x08048972 <main+30>: call  0x8048b66 <_Z3fooIiEbT_>
> 
> Doing
> <gdb>p _Z3fooIiEbT_(i) </gdb> 
> crahes gdb.
> ( btw. 
> <gdb>p p _Z3fooIiEbT_ </gdb> 
> gives "{bool (int)} 0x8048b66 <bool foo<int>(int)>" 
> )
> 
> How looks the line for calling foo correctly?

I don't know.  It may just be broken.  I did provoke an amusing
internal error trying to get it to work...

(gdb) p 'int f<int>'()
/space/debian/gdb/build-area/gdb-6.6.dfsg/gdb/valops.c:2112:
internal-error: find_oload_champ_namespace_loop: Assertion
`new_oload_champ != -1' failed.
A problem internal to GDB has been detected,


-- 
Daniel Jacobowitz
CodeSourcery


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

* Re: how to view content in stl vector
  2007-06-18 14:44                   ` Daniel Jacobowitz
@ 2007-06-18 14:57                     ` Maik Beckmann
  2007-06-18 15:03                       ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 14:57 UTC (permalink / raw)
  To: gdb

Am Montag, 18. Juni 2007 16:44:38 schrieb Daniel Jacobowitz:
> I don't know.  It may just be broken.  I did provoke an amusing
> internal error trying to get it to work...
>
> (gdb) p 'int f<int>'()
> /space/debian/gdb/build-area/gdb-6.6.dfsg/gdb/valops.c:2112:
> internal-error: find_oload_champ_namespace_loop: Assertion
> `new_oload_champ != -1' failed.
> A problem internal to GDB has been detected,

This works:
1. start and  run gdb:
 
2.look for the matching disassembly line
    0x08048972 <main+30>: call  0x8048b66 <_Z3fooIiEbT_>

3. let gdb say what it has to say: 
    output _Z3fooIiEbT_
    {bool (int)} 0x8048b66 <bool foo<int>(int)>

4. call it like this:
    p ({bool (int)} 0x8048b66)(i)
    $1 = true

The gdb-bug causes one extra step. 
However, as far as I understand  it should be possible to wrap this and the 
typename-storing stuff into convenience functions,  if your plan to get 
python into gdb-business succeeds.

many thanks to you Daniel,
Maik Beckmann


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

* Re: how to view content in stl vector
  2007-06-18 14:57                     ` Maik Beckmann
@ 2007-06-18 15:03                       ` Maik Beckmann
  2007-06-18 15:28                         ` Andreas Schwab
  0 siblings, 1 reply; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 15:03 UTC (permalink / raw)
  To: gdb

even better.... after I read you last mail entirely


1. start and  run gdb:

3. let gdb say what it has to say:
    output 'bool foo<int>'
    {bool (int)} 0x8048b66 <bool foo<int>(int)>

4. call it like this:
    p ({bool (int)} 0x8048b66)(i)
    $1 = true



Maik




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

* Re: how to view content in stl vector
  2007-06-18 15:03                       ` Maik Beckmann
@ 2007-06-18 15:28                         ` Andreas Schwab
  2007-06-18 15:49                           ` Maik Beckmann
  0 siblings, 1 reply; 15+ messages in thread
From: Andreas Schwab @ 2007-06-18 15:28 UTC (permalink / raw)
  To: Maik Beckmann; +Cc: gdb

Maik Beckmann <maikbeckmann@gmx.de> writes:

> even better.... after I read you last mail entirely
>
>
> 1. start and  run gdb:
>
> 3. let gdb say what it has to say:
>     output 'bool foo<int>'
>     {bool (int)} 0x8048b66 <bool foo<int>(int)>
>
> 4. call it like this:
>     p ({bool (int)} 0x8048b66)(i)
>     $1 = true

(gdb) p ({bool(int)}'bool foo<int>(int)')(i)
Hello from foo
$3 = true

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
PGP key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."


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

* Re: how to view content in stl vector
  2007-06-18 15:28                         ` Andreas Schwab
@ 2007-06-18 15:49                           ` Maik Beckmann
  0 siblings, 0 replies; 15+ messages in thread
From: Maik Beckmann @ 2007-06-18 15:49 UTC (permalink / raw)
  To: gdb

Am Montag, 18. Juni 2007 17:28:03 schrieb Andreas Schwab:
> (gdb) p ({bool(int)}'bool foo<int>(int)')(i)
> Hello from foo
> $3 = true


thx for this!

Btw.
<gdb>p ({bool(int)}'bool foo<int>')(i)</gdb> 
works too.

Maik


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

end of thread, other threads:[~2007-06-18 15:49 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-12 15:05 how to view content in stl vector kdsfinger
2007-06-15 19:52 ` Mark Kettenis
2007-06-18  8:53   ` Alpár Jüttner
2007-06-18 11:15     ` Maik Beckmann
2007-06-18 11:27       ` Daniel Jacobowitz
2007-06-18 12:27         ` Maik Beckmann
2007-06-18 12:36           ` Daniel Jacobowitz
2007-06-18 12:54             ` Maik Beckmann
2007-06-18 13:06               ` Daniel Jacobowitz
2007-06-18 14:35                 ` Maik Beckmann
2007-06-18 14:44                   ` Daniel Jacobowitz
2007-06-18 14:57                     ` Maik Beckmann
2007-06-18 15:03                       ` Maik Beckmann
2007-06-18 15:28                         ` Andreas Schwab
2007-06-18 15:49                           ` Maik Beckmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox