我用 C++ 编写了一个仪器,通过挂钩进入和退出调用来记录进入和退出函数。它按照预期与遗留代码库一起工作。然而,在挂钩我从 git 下载的项目时,我保存在主题代码的 extern 变量中的函数地址,它们在探查器库中的结果是不同的。这扰乱了挂钩函数和保存函数之间的函数指针比较。
Function address in subject code main file, breakpoint is inside the _penter hook function in the profiler code currently
The same entry is showing a different address with a "_" preceding the function name, in the profiler code
我不知道它是如何更改地址的,想知道我是否做错了什么。
我这样做的方式是,我有一个函数指针的外部数组(及其名称),它是使用主题主文件(其中所有函数都可用)中的主题代码函数引用进行初始化的。在库的钩子函数(_penter)中,我得到了刚刚输入的函数的地址。因此,我将其与外部数组中的地址进行比较,如果匹配,则记录输入的函数。
PROFILE.H 的片段(分析器)
extern Signature FuncTable[3000];
PROFILE.CPP 的片段(分析器)
void _stdcall EnterFunc0(unsigned * pStack)
{
void * pCaller;
pCaller = (void *)(pStack[0] - 5); // the instruction for calling _penter is 5 bytes long
Signature * funct = FuncTable; //the table that has references to functions and their names
funct = FuncTable;
while (funct->function)
{
//const BYTE * func = (const BYTE *)funct->function;
if ((void *)(pStack[0] - 5) == (void *)(funct->function))
{
int a = 0;
linesBuffer = linesBuffer + "Entering " + funct->signature + ";";
linesBuffer = linesBuffer + "\n";
WriteToFile(false); //function buffers 100kb before writing
break;
}
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
}
}
main.c 中的片段(主题代码主文件)
#include "../Profile/Profile.h"
Signature FuncTable[] = {
{ (int)TetrisView_ProcessPauseMenu, "TetrisView_ProcessPauseMenu" },
{ NULL }
};