List Info

Thread: how to view content in stl vector




how to view content in stl vector
user name
2007-06-12 10:05:21
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

Re: how to view content in stl vector
country flaguser name
Hungary
2007-06-18 03:52:00
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: kdsfingergmail.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



Re: how to view content in stl vector
country flaguser name
Germany
2007-06-18 06:14:44
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: KDSFINGERGMAIL.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

Re: how to view content in stl vector
country flaguser name
United States
2007-06-18 06:27:21
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

Re: how to view content in stl vector
country flaguser name
Germany
2007-06-18 07:26:36
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


Re: how to view content in stl vector
country flaguser name
United States
2007-06-18 07:36:50
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

Re: how to view content in stl vector
country flaguser name
Germany
2007-06-18 07:53:21
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


Re: how to view content in stl vector
country flaguser name
United States
2007-06-18 08:06:35
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

Re: how to view content in stl vector
country flaguser name
Germany
2007-06-18 09:34:31
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



Re: how to view content in stl vector
country flaguser name
United States
2007-06-18 09:44:38
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

[1-10] [11-14]

about | contact  Other archives ( Real Estate discussion Medical topics )