SubscribeMainTagsEditHistoryDiscussion

I wanted to debug a small C++ program that uses the STL. I didn't know how to look into STL vectors and I really wanted to do it. I tried with Eclipse and I didn't like to use a GUI. Not just because I stay away from GUIs but because it was not helpful for inspecting STL objects. It seems there is an extension for that. If you know about it please let me know.

Eclipse + C++, no thanks.

Almost every time I try a GUI to debug a program I end up using vim and GDB again. I guess I'll use a GUI when I need it, but today is not that day.

I wanted to know how to debug using GDB so I searched the WEB for tips and I found two of them to be interesting.

First, there is a nice collection of GDB scripts updated for GCC 3.3 and GCC 3.4. I couldn't get them to work and I would get this error:

me@host:~/gdb_stl_utils$ gdb
GNU gdb 6.8-debian
(cut...)
This GDB was configured as "i486-linux-gnu".
/home/me/.gdbinit:1: Error in sourced command file:
/home/me/.gdb/gdb_stl_utils:21: Error in sourced command file:
The address where /home/me/.gdb/StlStdContainers.o has been loaded is missing.

So, I didn't use them today. Anyway, I am starting to discover how powerful GDB is. I guess I will need to use the scripting facilities of GDB someday.

Fortunately, this message saved my day. It is nice to be able to call functions from GDB. The idea is to code a custom function that allows you to print what you want to inspect.

So, I just used the following functions.

void
dump (const vector <string> &v)
{
  int c = 0;
  for(vector<string>::const_iterator i(v.begin()); i != v.end(); ++i)
    cerr << '[' << (c++) << ']' << " => \"" << *i << "\"" << endl;
}

void
dump (const string &s)
{
  cerr << "\"" << s << "\"" << endl;
}

And this is a sample session:

   me@host:~work$ gdb ./a.out
   GNU gdb 6.8-debian
   Copyright (C) 2008 Free Software Foundation, Inc.
   License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
   This is free software: you are free to change and redistribute it.
   There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
   and "show warranty" for details.
   This GDB was configured as "i486-linux-gnu"...
   (gdb) list 118
   113       {
   114         cerr << "space:" << i << " ";
   115         loop(j,space[i].size())
   116           cerr << " " << space[i][j];
   117         cerr << endl;
   118       }
   119
   120       int m = 51;
   121
   122       loop(i, space[0].size())
   (gdb) break 120
   Breakpoint 1 at 0x80492ff: file MatchString.cpp, line 120.
   (gdb) run
   Starting program: ~/work/a.out
   0 => TIK
   1 => PPPO
   2 => OP
   space:0  0
   space:1  3
   space:2  1

Now comes the interesting part:

   Breakpoint 1, MatchString::placeWords (this=0xbfe49633, matchString=@0xbfe495f0,
       matchWords=@0xbfe495e4) at MatchString.cpp:120
   120       int m = 51;
   (gdb) call dump(MatchString)
   A syntax error in expression, near `)'.
   (gdb) call dump(matchString)
   "TOP"
   (gdb) call dump(matchWords)
   [0] => "TIK"
   [1] => "PPPO"
   [2] => "OP"
   (gdb)

I liked this simple method. I think I will use it for simple programs. It is nice to know that you can modify the dump function and have it print data structures using ranges. It also works with nested structures.

I'd like to ask the following question:

Loading... Vote up! Vote down! Discussion

Last update: 2008-05-02 (Rev 14082)

svnwiki $Rev: 14721 $