1

I have put a break point on a function by attaching to a running program. The function is long, and returns same error value from multiple places. Is there any way to find out where the function has exited without stepping through each line or putting breakpoints on all return values?

There is finish command which gives me the return value, but it doesn't say where it exited.

Thanks in advance

  • 2
    By putting breakpoints on each return statement. – Eugene Sh. May 28 '18 at 14:09
  • 4
    Create a dummy class with a destructor. Declare this variable in automatic scope, at the beginning of the function, and set a breakpoint in its destructor. When the breakpoint hits, you can `up` the backtrace to figure out where the destructor is getting invoked from. – Sam Varshavchik May 28 '18 at 14:13
  • "returns same error value from multiple places" - If not already in use, consider using the errno. i.e. The function can return the 'same error' in multiple places, and the function can also set "errno" to something convenient, perhaps "errno = _ LINE _; Note that "Several standard library functions indicate errors by writing positive integers to errno." I have seen several where the function returns -1, and errno describes the error. (a common idiom) – 2785528 May 28 '18 at 15:15
  • @DOUGLAS O. MOEN don't abuse `errno` like that. – Jesper Juhl May 28 '18 at 19:04
  • @JesperJuhl - Not abuse. This use is very common. And in this case, it makes trivial how "to find out where the function has exited without stepping through each line or putting breakpoints on all return values", per the OPs question. – 2785528 May 28 '18 at 21:51
  • See https://stackoverflow.com/a/9856885/2785528 and many other places. Also, "errno is used by nearly all functions in POSIX (except ones which return the error code and a few weird exceptions), not just low-level stuff" And "Not only can you set errno, in many cases you should set errno. When calling some library functions you can only reliably detect an error if you first set errno to zero." From all this, I conclude that it is not possible to abuse errno. – 2785528 May 28 '18 at 21:59

1 Answers1

4

Enable reverse debugging and then put a breakpoint after the function call. Once the breakpoint is hit, do reverse-next/reverse-step to go backwards to the return statement that terminated the function.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70