jameszhao00
jameszhao00

Reputation: 7301

Visual C++ versions of GCC functions

Are there Visual C++ versions of the following (in GCC)?

  1. __builtin_return_address
  2. __builtin_frame_address

Reference - http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html

If not, is there a way to emulate them?

Thanks.

Upvotes: 2

Views: 1829

Answers (4)

czz
czz

Reputation: 474

You can use _AddressOfReturnAddress to determine frame address.

Upvotes: 0

Drew Hoskins
Drew Hoskins

Reputation: 4186

The corresponding function to __builtin_frame_address, if it exists, would likely not work in optimized code, since VC does an optimization called Frame Pointer Omission. However, you can turn that optimization off, as described here: http://msdn.microsoft.com/en-us/library/2kxx5t2c(VS.71).aspx

Note that, for x86 you can write inline assembly code http://msdn.microsoft.com/en-us/library/4ks26t93(VS.71).aspx unfortunately, it doesn't work for 64-bit architectures so that probably isn't helpful to you.

Upvotes: 1

Sam Harwell
Sam Harwell

Reputation: 99889

Here is a full list of the available Visual Studio 2008 Compiler Intrinsics. One of the ones you are specifically looking for here is _ReturnAddress... still looking for the other.

For walking the stack (and getting frame pointers), read the details on the Visual Leak Detector stack walking mechanism, which uses StackWalk64 internally.

Upvotes: 5

greyfade
greyfade

Reputation: 25657

For functions declared __cdecl, the frame address is the top of the function's stack (pointed to by esp, and adjusted by the sizeof the function's parameters). I believe GCC typically stores that pointer for the current function in ebp (not sure about VS). That memory location is a pointer, and holds the return address.

For functions declared __fastcall, the adjustment to esp is much smaller, as some of the function's arguments may have been passed in registers.

I'm not sure about __stdcall, but I think it's the same as __cdecl.

Upvotes: 1

Related Questions