When ever we put a break point it becomes a pain if it gets hit many many many times. In such case we can try couple of things without having to modify source recompile and run. one option is conditional break point. when that is not possible, other option is logging values and later search them in the file. we can do this by adding a command to the break point. when ever the break point gets hit given command will execute. That is cool. its cool because it allows us to log information (and do whatever ...). Here goes the example.
Example
then we add a break point at line number 23 and lets say its break point 1
the we do following in gdb prompt to add the commnad to log debug information
(gdb) command 1
>>set logging on
>>set logging file tfile
>>p val
>>set logging off
>>c
>>end
(gdb)
now we just have to check the tfile after execution is done and then figure out the issue if possible :D
Example
list <string> ls; ls.push_back("jp"); ls.push_back("mom"); ls.push_back("dad"); ls.push_back("grany"); ls.push_back("pip"); list<string>::iterator lb = ls.begin(); list<string>::iterator le = ls.end(); for (; lb != le; ++lb) { string val = (*lb); cout << val<< endl; }
then we add a break point at line number 23 and lets say its break point 1
the we do following in gdb prompt to add the commnad to log debug information
(gdb) command 1
>>set logging on
>>set logging file tfile
>>p val
>>set logging off
>>c
>>end
(gdb)
- commad 1 - we are going to execute the command that we are about to define when the break point 1 is hit
- set logging on - this will enable gdb logging
- set logging file tfile - this will set the log file to be tfile
- set logging off - this will disable logging - otherwise unwanted info will get added to log file
- c - continue after logging is done - we dont need keep pressing buttons to continue
- end - this says that we are don defining the command
now we just have to check the tfile after execution is done and then figure out the issue if possible :D