Author Topic: see content of a vector in debugging  (Read 91442 times)

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
see content of a vector in debugging
« on: June 07, 2009, 08:02:35 am »
hi,

I use c::b (svn 5616)  and in my c::b project I have defined a typdef like this:

typedef vector<string> vstr;

And use it like following:

--------------------------
vstr v;

v.push_back("bla bla");
---------------------------

Everything works fine.

But when I debug, I can not see in the  "Local variabel"  an "understandable" content of the string vector.

When I open the "local watches" I only see "_M_start"  and "_M_finish" etc. with a hex number.




The only thing which is working when I watch a concrete cell of the vector is:


Add watch -->v[0]


then I can see the string of the first element of the string vector.

Is there a way to see all strings of the string vector in the debugger? That would be great.

The "v[0]" (element view) works fine but it would be very convenient to see all strings in the string vector "en block".


Thanks for your help

Martin




« Last Edit: June 07, 2009, 08:10:26 am by taram »
O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: see content of a vector in debugging
« Reply #1 on: June 07, 2009, 08:45:44 am »
I test your code, found it is even bad in my system(MinGW, WindowsXP), I can't view "v[0]".
See my screenshot


[attachment deleted by admin]
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: see content of a vector in debugging
« Reply #2 on: June 07, 2009, 10:02:31 am »
If v[0] works for you, you can try to watch v as array (add it to watch window, edit it and check watch as array).

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: see content of a vector in debugging
« Reply #3 on: June 07, 2009, 10:12:13 am »
I test your code, found it is even bad in my system(MinGW, WindowsXP), I can't view "v[0]".
See my screenshot

You have to use the []-operator in your program, otherwise it gets not compiled in and therefore cannot be used by gdb.

@taram:
watching the variable as array does not work here.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: see content of a vector in debugging
« Reply #4 on: June 07, 2009, 10:43:26 am »
Thanks Jens for your hints.

Here is a workaround.
Code
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<string> vstr;

int main()
{
    vstr v;
    v.push_back("bla bla");
    string v0 = v[0];
    cout << "Hello world!" << endl;
    return 0;
}

I can only watch "v0" (see the screen shot)



[attachment deleted by admin]
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
Re: see content of a vector in debugging
« Reply #5 on: June 07, 2009, 10:49:22 pm »
Hi,

first of all, thank all for your help and replies to my question. Sorry for my delayed answer, but I am here in Calgary and -8h behind European time :)

However, here the code which I was using for testing:
Code
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<string> vstr;

int main()
{
    vstr v;
    v.push_back("1. bla bla");
    v.push_back("2. bla bla");
    v.push_back("3. bla bla");

    int a[3] ={11, 22, 33};



    for (unsigned int f = 0; f<3; f++)
    {
        cout <<" a[" << f <<"] = " << a[f];
        cout << " and v[" << f << "] = " << v[f] << endl;
    }

    return 0;
}

I played around with "Add watches" and I was not able to get a similar debugger output of a string vector compared to the output of a normal array (see screen-shot-1.png).

The only thing which was working for the string vector was the "element view" i.e. "Add wachtes --> v[0] (see screen-shot-1.png).

But  with this method you have to be very carefully, since if you enter a element which does not exist i.e. out of array range, you will get a "segmentation signal" from Code::Blocks. (see v[3] in "wachtes in screen-shot-1.png and signal in screen-shot-2.png). So for me this "element view" method is not a 100% good way, it is more a 80% provisional solution :)

However, it would be very convenient if the debugger would show the string vector similar like the array (see "a" in watches --> screen-shot-1.png).

Thanks

Martin

PS: @ Jens
special thanks to you for providing debian-repository for Code::Blocks.
I use Debain Lenny, and this is a very convenient way to be up to date with Code::Blocks nightly builds.
Small suggestion for your page http://apt.jenslody.de/ :

instead of this line:

--------------------------------------------------------------------------------------------

...The best and easiest way to add my public-key to apt's trustdb is to install the package jens-lody-debian-keyring with your preferred package-manager or apt-get.  ....

--------------------------------------------------------------------------------------------

write it more explicitly like:

--------------------------------------------------------------------------------------------

... The best and easiest way to add my public-key to apt's trustdb is to install the package jens-lody-debian-keyring with

#apt-get-get install jens-lody-debian-keyring

or

#aptitude  install jens-lody-debian-keyring

--------------------------------------------------------------------------------------------

Since most people do not read a web page they skim it.


[attachment deleted by admin]
« Last Edit: June 08, 2009, 06:38:55 pm by taram »
O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
Re: see content of a vector in debugging
« Reply #6 on: June 07, 2009, 10:50:24 pm »
Here screen-shot 2, since max. attachment size is 128kb

Martin

[attachment deleted by admin]
« Last Edit: June 07, 2009, 10:52:52 pm by taram »
O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: see content of a vector in debugging
« Reply #7 on: June 08, 2009, 01:22:09 am »
However, it would be very convenient if the debugger would show the string vector similar like the array (see "a" in watches --> screen-shot-1.png).
You are right, but that seems to be a gdb issue and a workaround might not be so easy.

PS: @ Jens
special thanks to you for providing debian-repository for Code::Blocks.
I use Debain Lenny, and this is a very convenient way to be up to date with Code::Blocks nightly builds.
Small suggestion for your page http://apt.jenslody.de/ :

(Vielen Dank)
Thank you for the suggestion, I just changed my site.

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
Re: see content of a vector in debugging
« Reply #8 on: June 08, 2009, 02:59:59 am »
Yes, it may be an GDB issue, but I have less knowledge about it.

However may be in a future nightly build this issue will be solved... may be this issue should be forwarded to a "GDB geek" of the Code::Blocks developer crew.

Cheers

Martin
« Last Edit: June 08, 2009, 06:38:04 pm by taram »
O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
Re: see content of a vector in debugging
« Reply #9 on: June 08, 2009, 06:45:19 am »
may be these links help....

http://www.stanford.edu/~afn/gdb_stl_utils/

http://help.lockergnome.com/linux/GDB-capabilities-exploring-STL-classes--ftopict279673.html

but this is beyond my "doable", since I am a newbe to GBD. hope some of the C::B developers can use this

cheers

Martin
« Last Edit: June 08, 2009, 06:51:26 am by taram »
O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: see content of a vector in debugging
« Reply #10 on: June 08, 2009, 06:57:51 am »
Also frisch ans Werk - lets start fresh....
[...]
@ Jens: Schön, daß Du meinen Vorschlag aufgegriffen hast bzgl. Deiner HP. 
Although we do have German admins please do *not* post in German anymore if there is no particular reason. This is and remains an english-only forum. Thanks.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
Re: see content of a vector in debugging
« Reply #11 on: June 08, 2009, 06:33:31 pm »
?? I do not understand your concerns?? I know this fact, but there was no additional information in the German sentence and furthermore the translations was given directly behind it.

The other sentence was a thankful personal note to jens.

Further the complete discussion about the problem was/is done in English. If it would be done in German you would be right.

However, it was not my intention to disregard the forum rules, so I modified my entries above.

Cheers

Martin  
« Last Edit: June 08, 2009, 06:41:35 pm by taram »
O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: see content of a vector in debugging
« Reply #12 on: June 08, 2009, 07:39:23 pm »
Yes, it may be an GDB issue, but I have less knowledge about it.

However may be in a future nightly build this issue will be solved... may be this issue should be forwarded to a "GDB geek" of the Code::Blocks developer crew.

Cheers

Martin


No, I doubt it.  The GDB geeks are another group of geeks entirely.  The geeks who develope C::B only use GCC, they don't develope it.  In short, your problem is strictly a GDB problem, not a C::B problem.

I noticed that you never mentioned which version of GCC (and therefore GDB) you are using.  It may be that a newer version has the features you need.

Ringo

Offline taram

  • Multiple posting newcomer
  • *
  • Posts: 10
  • PhD = permanent head damage :)
Re: see content of a vector in debugging
« Reply #13 on: June 09, 2009, 02:48:08 am »
thx ringo for your input. I use

- gcc version 4.3.2 (Debian 4.3.2-1.1,Target: i486-linux-gnu)   -- by $gcc -v
 
- GNU gdb 6.8-debian (This GDB was configured as "i486-linux-gnu".) -- by $gdb -v

I will try to find a deb package for debian lenny and then  update to GCC 4.3.3
   
http://gcc.gnu.org/gcc-4.3/

may be this solve the problem

cheers

Martin




O tempora o mores! - Edite, bibite, collegiales, post multa saecula, pocula nulla!

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: see content of a vector in debugging
« Reply #14 on: June 09, 2009, 04:14:22 am »
You have to use the []-operator in your program, otherwise it gets not compiled in and therefore cannot be used by gdb.
Hi, Jens. Thanks for the reply.
I understand now. (Oh, My poor English, I need to read twice of these sentence to understand it's meaning :()
If I define v[0], then gdb can also understand v[1].  :D

Code
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<string> vstr;

int main()
{
    vstr v;
    v.push_back("bla bla");
    v.push_back("abcdef");

    string v0 = v[0];
    cout << "Hello world!" << endl;
    return 0;
}

See my screen shot.

Edit:
Oh, taram has already mentioned that.

Edit2
If I just add "v" to "watch", then check on the "Watch as array" opiotion. I still can't view any contents. Seems GDB didn't regard "v" as an array beginning address.
We can only use "v[0], v[1]" instead. :D





[attachment deleted by admin]
« Last Edit: June 09, 2009, 04:38:58 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.