[0.2.x] added : setBp routine

git-svn-id: https://pykd.svn.codeplex.com/svn@78898 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2012-08-17 07:45:07 +00:00 committed by Mikhail I. Izmestev
parent baa4531964
commit 17b0cf7447
6 changed files with 290 additions and 181 deletions

View File

@ -4,170 +4,192 @@
#include "stdafx.h"
#include "bpoint.h"
#include "dbgengine.h"
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
namespace pykd {
static IDebugBreakpoint *setBreakPoint(
IDebugControl4 *control,
ULONG bpType,
ULONG64 addr
)
ULONG setSoftwareBp(ULONG64 offset, BpCallback &callback /*= BpCallback()*/)
{
IDebugBreakpoint *bp;
HRESULT hres = control->AddBreakpoint(bpType, DEBUG_ANY_ID, &bp);
if (S_OK != hres)
throw DbgException("IDebugControl::AddBreakpoint", hres);
offset = addr64( offset );
hres = bp->SetOffset(addr);
if (S_OK != hres)
{
control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::SetOffset", hres);
return breakPointSet( offset );
}
ULONG bpFlags;
hres = bp->GetFlags(&bpFlags);
if (S_OK != hres)
///////////////////////////////////////////////////////////////////////////////
ULONG setHardwareBp(ULONG64 offset, ULONG size, ULONG accessType, BpCallback &callback /*= BpCallback()*/ )
{
control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::GetFlags", hres);
offset = addr64( offset );
return breakPointSet( offset, true, size, accessType );
}
bpFlags |= DEBUG_BREAKPOINT_ENABLED;
hres = bp->SetFlags(bpFlags);
if (S_OK != hres)
{
control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::SetFlags", hres);
}
///////////////////////////////////////////////////////////////////////////////
return bp;
}
//static IDebugBreakpoint *setBreakPoint(
// IDebugControl4 *control,
// ULONG bpType,
// ULONG64 addr
//)
//{
// IDebugBreakpoint *bp;
// HRESULT hres = control->AddBreakpoint(bpType, DEBUG_ANY_ID, &bp);
// if (S_OK != hres)
// throw DbgException("IDebugControl::AddBreakpoint", hres);
//
// hres = bp->SetOffset(addr);
// if (S_OK != hres)
// {
// control->RemoveBreakpoint(bp);
// throw DbgException("IDebugBreakpoint::SetOffset", hres);
// }
//
// ULONG bpFlags;
// hres = bp->GetFlags(&bpFlags);
// if (S_OK != hres)
// {
// control->RemoveBreakpoint(bp);
// throw DbgException("IDebugBreakpoint::GetFlags", hres);
// }
//
// bpFlags |= DEBUG_BREAKPOINT_ENABLED;
// hres = bp->SetFlags(bpFlags);
// if (S_OK != hres)
// {
// control->RemoveBreakpoint(bp);
// throw DbgException("IDebugBreakpoint::SetFlags", hres);
// }
//
// return bp;
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//static BPOINT_ID getBpId(IDebugBreakpoint *bp, IDebugControl4 *control = NULL)
//{
// BPOINT_ID Id;
// HRESULT hres = bp->GetId(&Id);
// if (S_OK != hres)
// {
// if (control)
// control->RemoveBreakpoint(bp);
// throw DbgException("IDebugBreakpoint::GetId", hres);
// }
// return Id;
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//BPOINT_ID DebugClient::setSoftwareBp(ULONG64 addr, BpCallback &callback /*= BpCallback()*/)
//{
// addr = addr64(addr);
// IDebugBreakpoint *bp = setBreakPoint(m_control, DEBUG_BREAKPOINT_CODE, addr);
//
// const BPOINT_ID Id = getBpId(bp, m_control);
//
// if (!callback.is_none())
// {
// boost::recursive_mutex::scoped_lock mapBpLock(*m_bpCallbacks.m_lock);
// m_bpCallbacks.m_map[Id] = callback;
// }
//
// return Id;
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//BPOINT_ID DebugClient::setHardwareBp(ULONG64 addr, ULONG size, ULONG accessType, BpCallback &callback /*= BpCallback()*/)
//{
// addr = addr64(addr);
// IDebugBreakpoint *bp = setBreakPoint(m_control, DEBUG_BREAKPOINT_DATA, addr);
//
// HRESULT hres = bp->SetDataParameters(size, accessType);
// if (S_OK != hres)
// {
// m_control->RemoveBreakpoint(bp);
// throw DbgException("IDebugBreakpoint::SetDataParameters", hres);
// }
//
// const BPOINT_ID Id = getBpId(bp, m_control);
//
// if (!callback.is_none())
// {
// boost::recursive_mutex::scoped_lock mapBpLock(*m_bpCallbacks.m_lock);
// m_bpCallbacks.m_map[Id] = callback;
// }
//
// return Id;
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//python::list DebugClient::getAllBp()
//{
// ULONG numberOfBps;
// HRESULT hres = m_control->GetNumberBreakpoints(&numberOfBps);
// if (S_OK != hres)
// throw DbgException("IDebugControl::GetNumberBreakpoints", hres);
//
// python::list lstIds;
//
// for (ULONG i =0; i < numberOfBps; ++i)
// {
// IDebugBreakpoint *bp;
// hres = m_control->GetBreakpointByIndex(i, &bp);
// if (S_OK != hres)
// throw DbgException("IDebugControl::GetBreakpointByIndex", hres);
// lstIds.append( getBpId(bp) );
// }
//
// return lstIds;
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//void DebugClient::removeBp(BPOINT_ID Id)
//{
// IDebugBreakpoint *bp;
// HRESULT hres = m_control->GetBreakpointById(Id, &bp);
// if (S_OK != hres)
// throw DbgException("IDebugControl::GetBreakpointById", hres);
//
// hres = m_control->RemoveBreakpoint(bp);
// if (S_OK != hres)
// throw DbgException("IDebugControl::RemoveBreakpoint", hres);
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//void DebugClient::removeAllBp()
//{
// ULONG numberOfBps;
// do {
// HRESULT hres = m_control->GetNumberBreakpoints(&numberOfBps);
// if (S_OK != hres)
// throw DbgException("IDebugControl::GetNumberBreakpoints", hres);
// if (!numberOfBps)
// break;
//
// IDebugBreakpoint *bp;
// hres = m_control->GetBreakpointByIndex(0, &bp);
// if (S_OK != hres)
// throw DbgException("IDebugControl::GetBreakpointByIndex", hres);
//
// hres = m_control->RemoveBreakpoint(bp);
// if (S_OK != hres)
// throw DbgException("IDebugControl::RemoveBreakpoint", hres);
//
// } while (numberOfBps);
//}
////////////////////////////////////////////////////////////////////////////////
static BPOINT_ID getBpId(IDebugBreakpoint *bp, IDebugControl4 *control = NULL)
{
BPOINT_ID Id;
HRESULT hres = bp->GetId(&Id);
if (S_OK != hres)
{
if (control)
control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::GetId", hres);
}
return Id;
}
////////////////////////////////////////////////////////////////////////////////
BPOINT_ID DebugClient::setSoftwareBp(ULONG64 addr, BpCallback &callback /*= BpCallback()*/)
{
addr = addr64(addr);
IDebugBreakpoint *bp = setBreakPoint(m_control, DEBUG_BREAKPOINT_CODE, addr);
const BPOINT_ID Id = getBpId(bp, m_control);
if (!callback.is_none())
{
boost::recursive_mutex::scoped_lock mapBpLock(*m_bpCallbacks.m_lock);
m_bpCallbacks.m_map[Id] = callback;
}
return Id;
}
////////////////////////////////////////////////////////////////////////////////
BPOINT_ID DebugClient::setHardwareBp(ULONG64 addr, ULONG size, ULONG accessType, BpCallback &callback /*= BpCallback()*/)
{
addr = addr64(addr);
IDebugBreakpoint *bp = setBreakPoint(m_control, DEBUG_BREAKPOINT_DATA, addr);
HRESULT hres = bp->SetDataParameters(size, accessType);
if (S_OK != hres)
{
m_control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::SetDataParameters", hres);
}
const BPOINT_ID Id = getBpId(bp, m_control);
if (!callback.is_none())
{
boost::recursive_mutex::scoped_lock mapBpLock(*m_bpCallbacks.m_lock);
m_bpCallbacks.m_map[Id] = callback;
}
return Id;
}
////////////////////////////////////////////////////////////////////////////////
python::list DebugClient::getAllBp()
{
ULONG numberOfBps;
HRESULT hres = m_control->GetNumberBreakpoints(&numberOfBps);
if (S_OK != hres)
throw DbgException("IDebugControl::GetNumberBreakpoints", hres);
python::list lstIds;
for (ULONG i =0; i < numberOfBps; ++i)
{
IDebugBreakpoint *bp;
hres = m_control->GetBreakpointByIndex(i, &bp);
if (S_OK != hres)
throw DbgException("IDebugControl::GetBreakpointByIndex", hres);
lstIds.append( getBpId(bp) );
}
return lstIds;
}
////////////////////////////////////////////////////////////////////////////////
void DebugClient::removeBp(BPOINT_ID Id)
{
IDebugBreakpoint *bp;
HRESULT hres = m_control->GetBreakpointById(Id, &bp);
if (S_OK != hres)
throw DbgException("IDebugControl::GetBreakpointById", hres);
hres = m_control->RemoveBreakpoint(bp);
if (S_OK != hres)
throw DbgException("IDebugControl::RemoveBreakpoint", hres);
}
////////////////////////////////////////////////////////////////////////////////
void DebugClient::removeAllBp()
{
ULONG numberOfBps;
do {
HRESULT hres = m_control->GetNumberBreakpoints(&numberOfBps);
if (S_OK != hres)
throw DbgException("IDebugControl::GetNumberBreakpoints", hres);
if (!numberOfBps)
break;
IDebugBreakpoint *bp;
hres = m_control->GetBreakpointByIndex(0, &bp);
if (S_OK != hres)
throw DbgException("IDebugControl::GetBreakpointByIndex", hres);
hres = m_control->RemoveBreakpoint(bp);
if (S_OK != hres)
throw DbgException("IDebugControl::RemoveBreakpoint", hres);
} while (numberOfBps);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace pykd
////////////////////////////////////////////////////////////////////////////////
} // end pykd namespace

View File

@ -4,45 +4,49 @@
#pragma once
#include "dbgclient.h"
////////////////////////////////////////////////////////////////////////////////
namespace pykd {
////////////////////////////////////////////////////////////////////////////////
typedef python::object BpCallback;
inline BPOINT_ID setSoftwareBp(ULONG64 addr, BpCallback &callback = BpCallback()) {
return g_dbgClient->setSoftwareBp(addr, callback);
}
ULONG setSoftwareBp(ULONG64 offset, BpCallback &callback = BpCallback() );
ULONG setHardwareBp(ULONG64 offset, ULONG size, ULONG accessType, BpCallback &callback = BpCallback());
//////////////////////////////////////////////////////////////////////////////////
//
//inline BPOINT_ID setSoftwareBp(ULONG64 addr, BpCallback &callback = BpCallback()) {
// return g_dbgClient->setSoftwareBp(addr, callback);
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//inline BPOINT_ID setHardwareBp(ULONG64 addr, ULONG size, ULONG accessType, BpCallback &callback = BpCallback()) {
// return g_dbgClient->setHardwareBp(addr, size, accessType, callback);
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//inline python::list getAllBp() {
// return g_dbgClient->getAllBp();
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//inline void removeBp(BPOINT_ID Id) {
// return g_dbgClient->removeBp(Id);
//}
//
//////////////////////////////////////////////////////////////////////////////////
//
//inline void removeAllBp() {
// return g_dbgClient->removeAllBp();
//}
////////////////////////////////////////////////////////////////////////////////
inline BPOINT_ID setHardwareBp(ULONG64 addr, ULONG size, ULONG accessType, BpCallback &callback = BpCallback()) {
return g_dbgClient->setHardwareBp(addr, size, accessType, callback);
}
} // end pykd namespace
////////////////////////////////////////////////////////////////////////////////
inline python::list getAllBp() {
return g_dbgClient->getAllBp();
}
////////////////////////////////////////////////////////////////////////////////
inline void removeBp(BPOINT_ID Id) {
return g_dbgClient->removeBp(Id);
}
////////////////////////////////////////////////////////////////////////////////
inline void removeAllBp() {
return g_dbgClient->removeAllBp();
}
////////////////////////////////////////////////////////////////////////////////
} // namespace pykd
////////////////////////////////////////////////////////////////////////////////

View File

@ -62,5 +62,8 @@ struct STACK_FRAME_DESC {
ULONG getStackTraceFrameCount();
void getStackTrace( STACK_FRAME_DESC* frames, ULONG frameCount, ULONG* frameReturned = NULL );
//breakpoints
ULONG breakPointSet( ULONG64 offset, bool hardware = false, ULONG size = 0, ULONG accessType = 0 );
};

View File

@ -349,6 +349,10 @@
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\bpoint.cpp"
>
</File>
<File
RelativePath=".\cpureg.cpp"
>
@ -451,6 +455,10 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\bpoint.h"
>
</File>
<File
RelativePath=".\cpureg.h"
>

View File

@ -19,6 +19,7 @@
#include "disasm.h"
#include "stkframe.h"
#include "localvar.h"
#include "bpoint.h"
#include "win/dbgio.h"
#include "win/windbg.h"
@ -55,6 +56,9 @@ BOOST_PYTHON_FUNCTION_OVERLOADS( compareMemory_, compareMemory, 3, 4 );
BOOST_PYTHON_FUNCTION_OVERLOADS( getSourceLine_, getSourceLine, 0, 1 );
BOOST_PYTHON_FUNCTION_OVERLOADS( getSourceFile_, getSourceFile, 0, 1 );
BOOST_PYTHON_FUNCTION_OVERLOADS( setSoftwareBp_, setSoftwareBp, 1, 2 );
BOOST_PYTHON_FUNCTION_OVERLOADS( setHardwareBp_, setHardwareBp, 3, 4 );
BOOST_PYTHON_MODULE( pykd )
{
python::scope().attr("version") = pykdVersion;
@ -106,7 +110,7 @@ BOOST_PYTHON_MODULE( pykd )
"Return effective pointer size" );
python::def( "is64bitSystem", &is64bitSystem,
"Check if target system has 64 address space" );
python::def( "getPageSize", &getPageSize,
python::def( "pageSize", &getPageSize,
"Get the page size for the currently executing processor context" );
// Manage target memory access
@ -217,6 +221,13 @@ BOOST_PYTHON_MODULE( pykd )
"Return a current stack as a list of stackFrame objects" );
python::def( "getLocals", &getLocals, "Get list of local variables" );
// breakpoints
python::def( "setBp", &setSoftwareBp, setSoftwareBp_( python::args( "offset", "callback" ),
"Set software breakpoint on executiont" ) );
python::def( "setBp", &setHardwareBp, setHardwareBp_( python::args( "offset", "size", "accsessType", "callback" ) ,
"Set hardware breakpoint" ) );
python::class_<intBase>( "intBase", "intBase", python::no_init )
.def( python::init<python::object&>() )
.def( "__eq__", &intBase::eq )

View File

@ -769,6 +769,67 @@ std::string getProcessorType()
return processorToStr(processorMode);
}
///////////////////////////////////////////////////////////////////////////////
ULONG breakPointSet( ULONG64 offset, bool hardware, ULONG size, ULONG accessType )
{
PyThread_StateRestore pyThreadRestore( g_dbgEng->pystate );
HRESULT hres;
IDebugBreakpoint *bp;
hres = g_dbgEng->control->AddBreakpoint(
hardware ? DEBUG_BREAKPOINT_DATA : DEBUG_BREAKPOINT_CODE,
DEBUG_ANY_ID,
&bp);
if (S_OK != hres)
throw DbgException("IDebugControl::AddBreakpoint", hres);
hres = bp->SetOffset(offset);
if (S_OK != hres)
{
g_dbgEng->control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::SetOffset", hres);
}
ULONG bpFlags;
hres = bp->GetFlags(&bpFlags);
if (S_OK != hres)
{
g_dbgEng->control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::GetFlags", hres);
}
bpFlags |= DEBUG_BREAKPOINT_ENABLED;
hres = bp->SetFlags(bpFlags);
if (S_OK != hres)
{
g_dbgEng->control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::SetFlags", hres);
}
if ( hardware )
{
HRESULT hres = bp->SetDataParameters(size, accessType);
if (S_OK != hres)
{
g_dbgEng->control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::SetDataParameters", hres);
}
}
ULONG breakId;
hres = bp->GetId(&breakId);
if (S_OK != hres)
{
g_dbgEng->control->RemoveBreakpoint(bp);
throw DbgException("IDebugBreakpoint::GetId", hres);
}
return breakId;
}
///////////////////////////////////////////////////////////////////////////////
} // end pykd namespace