Skeleton code:
Let us say you have something like this(x.cpp):
int main() {
char* str = <some_function_which_returns_char*>; // Such as hello, hell, hellow and it could be anything.
// Do some work here.
}
How do you put a breakpoint in gdb if str contains "hell". This substr 'hell' can come at any location of str. Say ahell, hello etc. I have written:
b x.cpp:3 if $_regex(str, "hell") // At line number 3 of above snapshot. Right after getting the char*
Is it the right way? OR Are there any other ways to deal with it?
Let us not worry about the leaks and anything else for now.
You can use the cond command to make the breakpoint conditional:
cond x.cpp:3 strcmp(str,"hell") == 0 - for hell exactly.cond x.cpp:3 strncmp (str,"hell",4) - for all strings that start with hell.cond x.cpp:3 strstr(str, "hell") != NULL - for all strings containing hell as a substring.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With