digz wrote:
Hi,
I am having a lot of trouble setting conditional breakpoints in gdb,
here is a simple example...
#include<string>
#include<iostream>
using namespace std;
void func(string& s){
cout << s << endl;
}
int main()
{
string a[] ={ "A", "B", "C" };
for (int i=0; i < 3 ; i++)
func(a[i]);
}
~
I am trying to break at the cout (line # 6)
in func(string&) only if s == "C", but gdb disregards it no matter
what i try to do, here it goes
(gdb) b 6 if s == "C" //does not work breaks for "A", "B", "C"
(gdb) b 6 if ( (s== "C") 0) //breaks for all "A", "B", "C"
(gdb) b 6 if s.operator==("C") 0
(gdb) b 6 if s.operator==(s,"C") 0
Error in testing breakpoint condition:
There is no member or method named operator.
IIRC, the function needs to be put in quotes, or the gdb parser will not
parse it in the way you expect:
b 6 if s."operator=="("C")
but you must have used that operator or it would not have been put in
the resulting executable. Also, I'm not sure if gdb will generate "C"
(an array of two bytes) on the fly.
(gdb) b 6 if (strcmp(s.c_str() , "C") == 0 )
No symbol "strcmp" in current context.
The only way i can possibly think of is to add source lines and clean
them up later like
if ( s == "C" ) break;
The break keyword doesn't cause the gdb to break. Actually, I'm not
sure what it would do in this context. Maybe go out one scope level?
Dunno.
If you want to do something like you are suggesting, IMHO, it would
'easier' to do like this:
BREAK(s == "C");
then have in a header file somewhere:
#if !defined BREAK_NO_CHECK
# define BREAK(x) ((void)((x)?(brk()):0))
#else
# define BREAK(x) ((void)0)
#endif
and in a source file somewhere:
#if !defined BREAK_NO_CHECK
// Need something so that it doesn't get optimised out if optimisation
// are on.
// Not sure if you can make inline as I don't know what gdb will be
// able to break on the call then.
int brk() { int a=0; ++a; return a; }
#endif
and then use the gdb command:
b brk
Once the break has been tripped, use gdb 'finish' command to let the
brk() function finish and return to the caller.
NOTE: the way I defined BREAK() allows for you to use it pretty much
anywhere, though these would be a bit obscure, such as:
int a=3, b=(BREAK(a=3), 3);
// NOTE: BREAK() always returns a non-zero (i.e. TRUE) value.
for (int i=3; i<9, BREAK(i==4); ++i) {
//...
}
fn(3, (BREAK(b==3),b), 3);
Though usually, you would use it in a more normal statement syntax as I
initially described.
Hope this helps.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/