The problem is to find a fool proof way of establishing that the code is back to the original frame.
The one thing that comes to mind is that after the stack allocation, EBP/RBP _must_ have the value they had before the call. IOW, ESP/RSP may change due to stack memory being allocated but, EBP/RBP _must_ stay the same.
**IF** the basepointer is used at all... (Some code does not use it, and also not set it / That is why trying to get the stack sometimes fails, when only relying on BP)
Well, to be exact, the value of BP will be the same after the call, even if it is not used. But that is meaningless because in that case BP will also be the same inside a recursion. And that would mean that, if the call was recursive, the "step over" would stop inside the recursive call.
Of course maybe there is a way to find out if the calling function (the function in which the step-over starts) is using the BP. Then it could be used based on the result of such a check. Not sure, it may be avail in DWARF CFI info.... but that will need some (quite some) read up. Or parsing the callers asm at function start.
The allocation behaves like a local variable.
I know the general concept. Though not the different ways it may be implemented.
IOW, it is valid _only_ in the function/procedure in which it was allocated. My code does it the simplest way which is, restore ESP/RSP to its original value before exiting the function.
Which function? the function that did the alloc? or the function that called the alloc?
The latter shouldn't need any work when exiting? the "leave" instruction (or restore from BP) should do it without any extra code needed?
That is of course if the function has a leave statement.
If you compile code with (IIRC)
{$Optimization FORCENOSTACKFRAME}
then that may not happen (though, I am not sure, if FPC will honour that for procs that call other procs...)
That way, ESP/RSP has the value originally set by the compiler which means that when the stack frame is taken down, the stack pointer points to the right place because the stack allocation has been "freed" (by adding the number of bytes allocated back to ESP/RSP.) That way when EBP/RBP is popped, RSP is pointing to the original EBP/RBP value (which is what the compiler expects.)
Did you swap sp <> bp in this statement?
Or is that a reference to when the function in which you tried to "step over" will exit, rather than when the function over which you tried to step returns?
SP is restored based on BP, BP holds the original SP. (after it itself was pushed)
But SP then points to the end of allocation, so allocation more must change SP.
Fixing this will probably take some time (before I get to it).
Also, this will require me to test with c compilers.
As this will add a cost (slight slow down) to step over, this is not going to be done based on your personal code only (and then affecting everyone).
This should be done, so it works with code everyone may encounter while stepping through c compiled code. (which fpdebug should eventually be able to do, as fpdebug is a generic debugger).
If at that point I have your code available it can probably be part of the recognized set of conditions for this.
If your code can be published, I would recommend to create a bug report with a full self contained example (including the alloc code).
But as I said: someday later...