資源簡介
C語言UT單元測試中需要實現動態,本代碼實現了該功能,原理和網絡上的描述一樣,通過修改機器指令實現。
平臺是Linux
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“stub.h“
static?long?pagesize?=?-1;
static?inline?void?*pageof(const?void*?p)
{?
return?(void?*)((unsigned?long)p?&?~(pagesize?-?1));
}
void?stub_set(struct?func_stub?*pstub?void?*fn?void?*fn_stub)
{
????pstub->fn?=?fn;
????memcpy(pstub->code_buf?fn?CODESIZE);
????
????if?(-1?==?mprotect(pageof(fn)?pagesize?*?2?PROT_READ?|?PROT_WRITE?|?PROT_EXEC))
????{
????????perror(“mprotect?to?w+r+x?faild“);
????????exit(errno);
????}
*(unsigned?char?*)fn?=?(unsigned?char)0xE9;
????*(unsigned?int?*)((unsigned?char?*)fn?+?1)?=?(unsigned?char?*)fn_stub?-?(unsigned?char?*)fn?-?CODESIZE;
????
????if?(-1?==?mprotect(pageof(fn)?pagesize?*?2?PROT_
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1643??2014-07-27?17:29??stub\stub.c
?????文件?????????319??2014-07-27?17:22??stub\stub.h
?????文件?????????670??2014-07-27?17:30??stub\test_stub.c
?????目錄???????????0??2014-07-28?00:46??stub\
- 上一篇:基于C++語言開發的股票交易系統代碼
- 下一篇:MFC制作的屏幕右下角彈出窗口
評論
共有 條評論