conditional breakpoints in gdb (c++)

Discussion in 'C++' started by digz, Mar 24, 2007.

  1. digz

    digz Guest

    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 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.

    (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;
    and set a breakpoint at that line!!!
    There has to be a better way to do this...what am i missing here..

    Thx
    Digz
     
    digz, Mar 24, 2007
    #1
    1. Advertising

  2. On 2007-03-24 15:47, 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 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.
    >
    > (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;
    > and set a breakpoint at that line!!!
    > There has to be a better way to do this...what am i missing here..


    This is off-topic here since it concerns 1) debugging, which is not
    defined in the C++ standard and 2) a specific implementation, next time
    try a group for your debugger, gnu.gdb comes to mind.

    I've no personal experience with this kind of stuff under gdb, but in
    VS2005 I notices a severe performance degradation when trying to set a
    condition on a breakpoint, my guess is that the debugger braked on the
    specific line each time it was executed, performed the check and if it
    was false resumed execution. So I would insert a bit of code that
    performed the check if I were you.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 24, 2007
    #2
    1. Advertising

  3. 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 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/]______\/
     
    Adrian Hawryluk, Mar 24, 2007
    #3
  4. Adrian Hawryluk wrote:
    > 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 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.


    Er, now that I think about that last comment RE inline some more,
    inlineing from within a source file will probably only affect things
    referring to brk() within the source file after the definition. So the
    comment is moot.

    > 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.


    I forgot to mention, you must define BREAK_NO_CHECK in your project to
    strip out the excess code in the compiled binary. Make sure that the
    condition you pass to BREAK has no side-effects, or you _will_ have trouble.


    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/]______\/
     
    Adrian Hawryluk, Mar 24, 2007
    #4
  5. digz

    digz Guest

    On Mar 24, 12:18 pm, Adrian Hawryluk <adrian.hawryluk-at-
    > wrote:
    > digz wrote:
    > > Hi,
    > > I am having a lot of trouble settingconditionalbreakpointsingdb,
    > > 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 am trying to break at the cout (line # 6)
    > > in func(string&) only if s == "C", butgdbdisregards 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 thegdbparser 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 ifgdbwill 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;


    say this is line# 7 in the sample code
    >
    > 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.


    I meant set a breakpoint in the line number where such ifs
    would be used ,it could as well be :
    if ( s== "C" ) cout << "found it" ;
    as in the example say the "if statement" is in line no 7 in the source
    so...
    (gdb) break 7
    would only break if the condition s==C is satisfied ,the debug macros
    you suggest seem to be an overkill..
    i was essentially trying to do non intrusive debugging ,without
    changing the source, but that seems not likely.
    even if we had to introduce operator== somewhere so its in the symbol
    table it would mean changing source recompiling etc..
    Apologies for Off Topic Posting :(
    Thx
    Digz




    Digz
     
    digz, Mar 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surendra
    Replies:
    0
    Views:
    521
    Surendra
    Mar 23, 2006
  2. seba
    Replies:
    1
    Views:
    1,464
    mlimber
    Mar 22, 2007
  3. Daneel Yaitskov

    using conditional breakpoints for sub

    Daneel Yaitskov, Dec 3, 2008, in forum: Perl
    Replies:
    0
    Views:
    2,467
    Daneel Yaitskov
    Dec 3, 2008
  4. Timothy Madden
    Replies:
    1
    Views:
    1,664
    Timothy Madden
    Sep 17, 2009
  5. Daneel Yaitskov

    using conditional breakpoints for sub

    Daneel Yaitskov, Dec 3, 2008, in forum: Perl Misc
    Replies:
    0
    Views:
    126
    Daneel Yaitskov
    Dec 3, 2008
Loading...

Share This Page