Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional gdb breakpoint after getting the char* from an function

Tags:

c++

gdb

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.

like image 581
Hemant Bhargava Avatar asked May 25 '26 14:05

Hemant Bhargava


1 Answers

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.
like image 165
alon Avatar answered May 27 '26 03:05

alon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!