【发布时间】:2016-07-07 17:58:52
【问题描述】:
我正在使用 Visual Studio 的 /Gh 和 /GH 编译器选项来分析一堆代码。使用的两种方法是 _penter 和 _pexit,它们在被分析的代码中进入或退出函数时调用。由于我需要对特定函数进行分析/调试,因此我使用了一个已定义的数组 FuncTable,其中包含我需要检测的函数的地址,它们的名称为字符串。因此,当输入一个函数时,pStack[0] 基本上包含寄存器内容,其中包含正在执行的代码的当前行的地址。类似地,当函数退出时,pStack[0] 包含代码最后一行的地址。
问题:当输入一个函数(调用_penter)时,我得到了pStack[0]中函数第一行的地址,因此我可以通过减去a来得到函数的地址常量(-5)并将其保存到我的列表中,以便稍后在 _pexit 函数中检索。但是由于在 _pexit 中我得到了函数最后一行的地址,所以我需要找到函数的大小,以便从 pStack[0] 中的地址中减去该大小以到达函数的起始地址,然后进行比较该地址保存在我的列表中。下面贴的是代码。
void _stdcall EnterFunc0(unsigned * pStack)
{
void * pCaller;
pCaller = (void *)(pStack[0] - 5); // pStack[0] is first line, -5 for function address
Signature * funct = FuncTable;
while (funct->function)
{
const BYTE * func = (const BYTE *)funct->function;
if ((func == (const BYTE *)pCaller) || ((*func == 0xE9) && ((func + *(DWORD *)(func + 1) + 5) == (const BYTE *)pCaller)))
{
Stack_Push(funct->name, funct->returnType, true, pCaller);
}
funct++;
}
}
extern "C" __declspec(naked) void __cdecl _penter()
{
_asm
{
pushad // save all general purpose registers
mov eax, esp // current stack pointer
add eax, 32 // stack pointer before pushad
push eax // push pointer to return address as parameter to EnterFunc0
call EnterFunc0
popad // restore general purpose registers
ret // start executing original function
}
}
void _stdcall ExitFunc0(unsigned * pStack)
{
if (startRecording)
{
StackEntry * start = top;
while (start != NULL)
{
//**HERE I NEED TO COMPARE THE ADDRESS OF THE FUNCTION WITH THE ONE ALREADY IN MY STACK**
if ((void *)(pStack[0] - sizeOfTheFunction) == start->Address)
{
OutputDebugString("Function Found\n");
}
start = start->next;
}
}
}
extern "C" __declspec(naked) void __cdecl _pexit()
{
_asm
{
pushad // save all general purpose registers
mov eax, esp // current stack pointer
add eax, 32 // stack pointer before pushad
push eax // push pointer to return address as parameter to EnterFunc0
call ExitFunc0
popad // restore general purpose registers
ret // start executing original function
}
}
【问题讨论】:
-
C 不支持 methods,只支持 _functions。但是
extern "C"不是有效的 C,而是不同的语言 C++。使用正确的标签。
标签: c++ visual-studio profiling compiler-options