2

Is it possible, in any debugger for C/C++ to see where the last return statement happened? Or even a "return stack", like the call stack but the other way around?

bool foo(int bar)
{
    if(bar == 1) return false;
    if(bar == 2) return false;
    return true;
}

void baz()
{
    bool result = foo(4);
}

So after the call to foo in baz, you can see which of the three return statements in foo that was hit.

edvinHolm
  • 314
  • 1
  • 11
  • If you'd split `if` statements and `return` statements to separate lines, you could see it quite easily when going line by line in debugger. Or, if your real problem is as simple as this one, you could just inspect value of `bar` inside your function. – Yksisarvinen Dec 19 '19 at 07:39
  • If you put the return statements on separate lines, you can set breakpoints on those lines. – user3386109 Dec 19 '19 at 07:43
  • Yes, that is how I do it now. But it would be convenient if you didn't have to edit the code and put breakpoints on every one of the return statements. Often I call the same function multiple times in a loop and I don't want to step through all of it. – edvinHolm Dec 19 '19 at 07:47
  • 2
    Off-topic because you use visual studio, but there are reverse debuggers, which allow you to step back in time, which would then show you which return statement was hit. For windows you can look at WinGDB time-travel debugging, for linux you can look at rr on github, at undo.io (commercial) or use gdb, it has small reverse features. – RoQuOTriX Dec 19 '19 at 08:07
  • 1
    Disabling all the optimization you can [set breakpoint on assembler](https://stackoverflow.com/questions/5459581/how-to-break-on-assembly-instruction-at-a-given-address-in-gdb) – LPs Dec 19 '19 at 08:09
  • @RoQuOTriX Thanks! I don't mind looking at other debuggers. – edvinHolm Dec 19 '19 at 08:31
  • Ok, because you tagged visual-studio – RoQuOTriX Dec 19 '19 at 08:32
  • Depending on the situation, you may be able to use MIcrosoft's "Time Travel Debugging" feature (but, if memory serves, it's only included in the rather expensive Enterprise Edition of VS). – Jerry Coffin Dec 19 '19 at 08:55
  • This is known as _trace_ or "back trace". – Lundin Dec 19 '19 at 11:58

1 Answers1

2

Here are some reverse debugger for C/C++ which can step back in time in your debug session:

Only Linux:
rr from mozilla
open source, really fast, small record traces, has to record the whole application

undo reverse debugger
commercial, not so fast as rr, small record traces, recording of application can start anywhere

GDB reverse debugging
standard gdb from GNU, can record small functions (limited), start debugging, set breakpoint, then start recording until next breakpoint, you can reverse debug between those points

Not tested by me:
RogueWave TotalView Debugger
commercial, comes with full IDE

Windows 10 (7 not supported)
WinGDB Reverse Debugging
not tested, cannot say much about it. It seems to be the only one working with windows

What to add, multithreading can be handled by these debuggers, but only if the execution of the threads is serialized to one core (pseudo-parallalization). You can't reverse debug multicore-multithreaded applications. The reason is simple, the reverse execution has to synchronize the executed threads, which can't be done if two or more threads are executed at the "same time".

RoQuOTriX
  • 2,871
  • 14
  • 25