Is it possible to set a conditional breakpoint at the end of a function based on the return of the function?

I have a more complicated version:

unsigned int foo ();
unsigned int bar ();

unsigned int myFunc () {
  return foo()+bar();
}

In my case, it myFuncis called from a large number of places. Something is wrong in one context. I know that after debugging it’s even worse that it returns the value of this function when something is bad, but, unfortunately, I don’t know which path led to this value.

I can add a temporary variable that saved the result of the expression "foo () + bar ()" and then add a conditional breakpoint to this value, but I was wondering if it could be done in another way.

I am working on x86 architecture.

From this and this answer, I thought I could set a breakpoint at the exact location of the return from the function:

gdb> break *$eip

$eax, .

?

+3
2

, , , , , $eax ( $rax, 64- x86) .

unsigned int foo(void) { return 1; }
unsigned int bar(void) { return 4; }
unsigned int myFunc(void) { return foo()+bar(); }

gdb..

(gdb) disass myFunc
Dump of assembler code for function myFunc:
0x080483d8 <myFunc+0>:  push   %ebp
0x080483d9 <myFunc+1>:  mov    %esp,%ebp
0x080483db <myFunc+3>:  push   %ebx
0x080483dc <myFunc+4>:  call   0x80483c4 <foo>
0x080483e1 <myFunc+9>:  mov    %eax,%ebx
0x080483e3 <myFunc+11>: call   0x80483ce <bar>
0x080483e8 <myFunc+16>: lea    (%ebx,%eax,1),%eax
0x080483eb <myFunc+19>: pop    %ebx
0x080483ec <myFunc+20>: pop    %ebp
0x080483ed <myFunc+21>: ret    
End of assembler dump.
(gdb) b *0x080483ed if $eax==5
Breakpoint 1 at 0x80483ed
(gdb) run
Starting program: /tmp/x 
Breakpoint 1, 0x080483ed in myFunc ()
(gdb)
+4

, , Visual Studio, , "..." , .

, !: -)

0

Source: https://habr.com/ru/post/1716891/


All Articles