1

我想知道如何记录函数的堆栈返回地址?

如果您记录这样的参数:

int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);

// 在 DllMain 下面

int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
    fopen_s(&pSendLogFile, "C:\\SendLog.txt", "a+");
    fprintf(pSendLogFile, "%s\n", buf);
    fclose(pSendLogFile);
    return pSend(s, buf, len, flags);
}

那么我将如何记录发送的返回地址?

4

2 回答 2

3

我不确定您要达到什么目标,因此很难知道这是否是正确的答案。我假设您知道自己在做什么,但我会敦促您重新考虑您的设计。

话虽如此,请查看http://msdn.microsoft.com/en-us/library/64ez38eh_ReturnAddress()上的内在函数,它将为您提供将在您从返回调用的函数之后立即执行的指令的地址,以及在http://msdn.microsoft.com/en-us/library/s975zw7这将为您提供存储返回地址的地址。_ReturnAddress()_AddressOfReturnAddress()

务必仔细阅读文档并了解您返回的地址代表什么。如果您操纵退货地址,请务必非常小心,因为如果您做错了事情,事情可能(并且将会)变得繁荣。

于 2012-06-25T04:23:19.407 回答
1

您不能以独立于平台/编译器的方式执行此操作。在 Windows 中,您可以使用 StackWalk/StackWalk64 函数。几年前我用过它们。如果您有堆栈上模块的调试信息,您可以获得函数的名称。

例如看看这个: http: //msdn.microsoft.com/en-us/library/windows/desktop/ms680650 (v=vs.85).aspx

于 2012-06-25T04:03:38.700 回答