Bug 31084

Summary: -break-condition compare strings
Product: gdb Reporter: Ernie Pasveer <epasveer>
Component: miAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: ssbssa, tromey
Priority: P2    
Version: unknown   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description Ernie Pasveer 2023-11-24 19:05:49 UTC
What is the proper syntax to add a condition to a breakpoint to break if a string matches a text.  Same question to -break-insert.


gdb) 
-break-condition 1 name.c_str() == "ernie"
^error,msg="No symbol \"ernie\" in current context."
(gdb) 
-break-condition 1 $_streq(name.c_str(), "ernie")
^error,msg="Problem parsing arguments: break-condition 1 $_streq(name.c_str(), \"ernie\")"
(gdb) 






$ more gdbmi_condition.cpp 
#include <iostream>
#include <string>

int main (int argc, char* argv[]) {

    std::string name = "";

    name = "ernie";

    // Create the breakpoint with a string condition.
    //
    // -break-insert -c 'name.c_str() == "ernie"' gdbmi_condition.cpp:16             // Failed parsing.
    // -break-insert -c '$_streq(name.c_str(), "ernie")' gdbmi_condition.cpp:16      // Failed parsing.
    // -break-insert -c 'strcmp("xxxxx", name.c_str() == 0' gdbmi_condition.cpp:16   // Is accepted but breaks eventhough strings are different.

    std::cout << "Name is: " << name << std::endl;

    return 0;
}
Comment 1 Hannes Domani 2023-11-24 20:10:57 UTC
I think it should work if you quote the whole condition, like this:

-break-condition 1 "$_streq(name.c_str(), \"ernie\")"
Comment 2 Ernie Pasveer 2023-11-25 15:36:22 UTC
Sweet!  Yes, indeed.  It does work after all the escape'isms.

I can implement this in my Seergdb debugger.

Thanks for your quick help.
Comment 3 Tom Tromey 2023-11-25 16:22:51 UTC
Answered so closing.  Thank you Hannes.