DataBreakPoint

本文介绍了一种使用程序控制数据断点的方法,该方法可以灵活地设置内存读写断点的位置和格式,相较于VC提供的功能更为强大。文章提供了具体的实现代码,包括如何查找、清除和设置数据断点。

http://msinilo.pl/blog/?p=571

http://www.codeproject.com/KB/debug/hardwarebreakpoint.aspx

查冲内存的 错误时候,可以通过程序来控制哪里有data breakpoint,那里没有,什么时间有,和write,read格式,比vc里面设给力10倍。

每个thread只能有4个data break point这个还比较郁闷(vc2005里面,vs2010里没试过)

把下载的代码copy一份这里吧,方便快速查找和索引

#include "core/Debug.h" #include "core/Config.h" #include "core/win32/Windows.h" namespace { int FindBreakpointIndexForAddress(const void* address, CONTEXT& ctx, DWORD** retDebugReg) { int dataIndex(-1); *retDebugReg = 0; if (ctx.Dr0 == (size_t)address) { dataIndex = 0; *retDebugReg = &ctx.Dr0; } else if (ctx.Dr1 == (size_t)address) { dataIndex = 1; *retDebugReg = &ctx.Dr1; } else if (ctx.Dr2 == (size_t)address) { dataIndex = 2; *retDebugReg = &ctx.Dr2; } else if (ctx.Dr3 == (size_t)address) { dataIndex = 3; *retDebugReg = &ctx.Dr3; } return dataIndex; } int FindFreeBreakpointIndex(CONTEXT& ctx, DWORD** retDebugReg) { return FindBreakpointIndexForAddress(NULL, ctx, retDebugReg); } // Indexed with DataBreakpoint::Enum const rde::uint32 s_triggerMasks[] = { 0xFFFFFFFF, // NONE 1, // WRITE 3, // READWRITE 0, // EXECUTE }; // Indexed with DataBreakpoint::SizeEnum const rde::uint32 s_sizeMasks[] = { 0, 1, 3, 2, }; } // <anonymous> namespace namespace rde { bool SetDataBreakpoint(const void* address, DataBreakpoint::Enum accessType, DataBreakpoint::SizeEnum dataSize) { CONTEXT threadContext; threadContext.ContextFlags = CONTEXT_DEBUG_REGISTERS; const HANDLE currentThread = ::GetCurrentThread(); if (!::GetThreadContext(currentThread, &threadContext)) return false; DWORD* dataDebugReg(0); int dataIndex = FindBreakpointIndexForAddress(address, threadContext, &dataDebugReg); bool success(false); // Clear data breakpoint if (accessType == DataBreakpoint::NONE) { if (dataIndex >= 0) { threadContext.Dr7 &= ~(3 << (dataIndex * 2)); *dataDebugReg = 0; // clear address success = true; } } else // set/change data breakpoint { if (dataIndex < 0) dataIndex = FindFreeBreakpointIndex(threadContext, &dataDebugReg); if (dataIndex >= 0) { // Enable breakpoint. threadContext.Dr7 &= ~(3 << (dataIndex * 2)); threadContext.Dr7 |= 3 << (dataIndex * 2); // local & global // Set trigger mode. // (starts at bit 16, 2 bits per entry). const uint32 triggerMask = s_triggerMasks[accessType]; threadContext.Dr7 &= ~(3 << ((dataIndex * 2) + 16)); // clear old mask threadContext.Dr7 |= triggerMask << ((dataIndex * 2) + 16); // Set data size // (starts at bit 24, 2 bits per entry). threadContext.Dr7 &= ~(3 << ((dataIndex * 2) + 24)); // clear old mask const uint32 sizeMask = s_sizeMasks[dataSize]; threadContext.Dr7 |= sizeMask << ((dataIndex * 2) + 24); *dataDebugReg = (DWORD)((size_t)address); success = true; } } if (success) { success = (::SetThreadContext(currentThread, &threadContext) != 0); } return success; } } // rde

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值