Can gdb conditionally break non-interactively on function only if the function's return value is equal to "Value"? Can gdb conditionally break non-interactively on function only if the function's return value is equal to "Value"? unix unix

Can gdb conditionally break non-interactively on function only if the function's return value is equal to "Value"?


Suppose I want to break on SomeSourceFile:123 only if the return of foo(...) is not equal to "Alice".

This fairly easy to do if you go to the lower (assembly level), and if the code is built without optimization.

It is also trivial to do if you can modify the source like so:

if (foo(...) == "Alice") { ...} else {  int x = 0; if (x) abort(); // break here}

Now you can simply set breakpoint on line 125, and you are done.

So how can you do this without modifying the source?

You must understand that the compiler invokes some bool operator==(), compares the return to true or false, and conditionally jumps around the body of if when the condition is false.

The (gdb) info line 123 will give you range of instructions that were produced for this source line.

Disassembling that range of instructions will allow you to locate the call to operator==, and show you the instruction that compares EAX (JE or JNE) with 0 or 1.

You can then set the breakpoint on the conditional jump instruction, and use $EAX (or $RAX for 64-bit code) is the condition on that breakpoint.