Code::Blocks Forums

User forums => Help => Topic started by: MortenMacFly on November 06, 2005, 02:02:01 pm

Title: Debugging into <vector> with GDB?
Post by: MortenMacFly on November 06, 2005, 02:02:01 pm
Dear community,

I cannot figure out how to debug into a STL vector so I am asking for help by this post.
I am using the STL vector in a project and during runtime I'd like to inspect the values inside the vector. Hence the only thing I can see in the debugging window is something like:

Value of v: {<_Vector_base<double,std::allocator<double> >> = {
_M_impl = {<allocator<double>> = {<new_allocator<double>> = {<No data fields>}, <No data fields>}, _M_start = 0x3e2cf8, _M_finish = 0x3e2d10, _M_end_of_storage = 0x3e2d18}}, <No data fields>}

but no values. I tried different things but I don't know how to actually display the values of the vector. Can somebody help? Here is a short code-snippet to give a try:
Code
#include <iostream>
#include <vector>

int main()
{
  std::vector<double> v;

  v.push_back(1.1); v.push_back(2.2); v.push_back(3.3);

  for (unsigned int i=0; i<v.size(); i++)
    std::cout << "| " << v.at(i) << " |" << std::endl;

  return 0;
}

I must admit that until now I have used DDD for debugging my code and I am using the C::B GDB plugin just for a short time so there might be a very simple answer... (I hope so)...?!

Thanks, Morten.
Title: Re: Debugging into <vector> with GDB?
Post by: Urxae on November 06, 2005, 04:09:51 pm
Value of v: {<_Vector_base<double,std::allocator<double> >> = {
_M_impl = {<allocator<double>> = {<new_allocator<double>> = {<No data fields>}, <No data fields>}, _M_start = 0x3e2cf8, _M_finish = 0x3e2d10, _M_end_of_storage = 0x3e2d18}}, <No data fields>}

but no values. I tried different things but I don't know how to actually display the values of the vector. Can somebody help?

The values are at *_M_start up to (but not including) *_M_finish (so you can basically consider _M_start to be v.begin(), and _M_start to be v.end()).
You should be able to add watches to those addresses, but I'm not sure if there's a way to view the entire array of them in a single watch.
Title: Re: Debugging into <vector> with GDB?
Post by: MortenMacFly on November 07, 2005, 02:33:40 pm
The values are at *_M_start up to (but not including) *_M_finish
You should be able to add watches to those addresses [...]
Thanks, that helps (so far). So it seems that DDD recurses into each element and presents the list of collected values afterwards. It would be nice if one could save "GDB scripts" in the GDB plugin to allow something similar...

Morten.
Title: Re: Debugging into <vector> with GDB?
Post by: Urxae on November 07, 2005, 03:12:41 pm
It would be nice if one could save "GDB scripts" in the GDB plugin to allow something similar...

Sounds like a good feature request (http://sourceforge.net/tracker/?func=add&group_id=126998&atid=707419) to me 8).

Though it might be a bit complicated, depending on how advanced you'd like those scripts to be.
Title: Re: Debugging into <vector> with GDB?
Post by: MortenMacFly on November 07, 2005, 03:22:17 pm
Sounds like a good feature request (http://sourceforge.net/tracker/?func=add&group_id=126998&atid=707419) to me 8).
I am going to think about that ones I figured out some principles of using the GDB without DDD... :lol:

I just gave it a try and unfortunately I am still unable to inspect the value. The problem is that it seems one can only add variables (existing names) to the GDB watches. What I want is the content of an address (the vector address) which is of cause type-specific. I also tried to send direct commands to the Debugger to see the content of the memory but I don't know, how. To close the "Send command to GDB" window after an command has been input is also quite annoying because I guess the default is that you want to send multiple commands (sounds like another feature request).

Could someone give me a simply step-by-step instruction how to obtain the values? I downloaded the >300 pages manual for GDB but couldn't find the command I am looking for... Help! :cry:

Morten.
Title: Re: Debugging into <vector> with GDB?
Post by: Urxae on November 07, 2005, 04:54:47 pm
Could someone give me a simply step-by-step instruction how to obtain the values? I downloaded the >300 pages manual for GDB but couldn't find the command I am looking for... Help! :cry:

The best way to watch the contents of the vector is probably not to use memory addresses directly in the watches, but to put a watch on v._M_start, for one or more values of i. That way it'll also work after the vector reallocates to get more memory and the addresses change, plus you get the added benefit of retaining type information.
Title: Re: Debugging into <vector> with GDB?
Post by: MortenMacFly on November 07, 2005, 05:16:18 pm
v._M_start, for one or more values of i.
Yeah! That did it. Not exactly, I had to use v._M_impl._M_start as watch but I missed to make use of _M_(...) in the first place. Thanks a lot!!!

Morten.
Title: Re: Debugging into <vector> with GDB?
Post by: MortenMacFly on November 07, 2005, 05:59:15 pm
Sounds like a good feature request (http://sourceforge.net/tracker/?func=add&group_id=126998&atid=707419) to me 8).
By the way: I have filed a feature request concernig "scripts". I think it could just be implemented as operating on a simple text file that states a GDB command in each line. We'll see...

Morten.
Title: Re: Debugging into <vector> with GDB?
Post by: MortenMacFly on November 07, 2005, 09:06:53 pm
I've done it. I've created a patch (attached) to the debugger plugin that allows to select a "debugger script file" to be run. Maybe if mandrav likes it it will be moved into CVS. Should I file a patch on SourceForge?

But: Although the commands are processed there is still an issue: Where does the output go to? I have enabled the "GDB Debugger (debug)" window as well but it's very strange: If I move the cursor e.g. over a variable I get the output directly into the "GDB Debugger (debug)" and the "Code::Blocks Debug" window. But neighter with "Send command to GDB" nor "Send script to GDB" I see any output... only the input

...mandrav???

Morten.

Edit: I've moved this topic to http://forums.codeblocks.org/index.php/topic,1317.0.html because I think is gets no attention in here due to the subject of the topic.

[attachment deleted by admin]