[0.2.x] added : Ctrl+Break support

git-svn-id: https://pykd.svn.codeplex.com/svn@80474 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2012-10-25 08:58:47 +00:00 committed by Mikhail I. Izmestev
parent d11fd6a958
commit fbec5c4e5e
2 changed files with 100 additions and 21 deletions

View File

@ -59,6 +59,79 @@ WindbgGlobalSession *WindbgGlobalSession::windbgGlobalSession = NULL;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class InterruptWatch
{
public:
InterruptWatch( PDEBUG_CLIENT4 client, python::object& global )
{
m_debugControl = client;
m_global = global;
m_stopEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_thread =
CreateThread(
NULL,
0,
threadRoutine,
this,
0,
NULL );
}
~InterruptWatch()
{
SetEvent( m_stopEvent );
WaitForSingleObject( m_thread, INFINITE );
CloseHandle( m_stopEvent );
CloseHandle( m_thread );
}
private:
static int quit(void *) {
PyErr_SetString( PyExc_SystemExit, "" );
return -1;
}
DWORD workRoutine(){
while( WAIT_TIMEOUT == WaitForSingleObject( m_stopEvent, 250 ) )
{
HRESULT hres = m_debugControl->GetInterrupt();
if ( hres == S_OK )
{
PyGILState_STATE state = PyGILState_Ensure();
Py_AddPendingCall(&quit, NULL);
PyGILState_Release(state);
break;
}
}
return 0;
}
static DWORD WINAPI threadRoutine(LPVOID lpParameter) {
return static_cast<InterruptWatch*>(lpParameter)->workRoutine();
}
HANDLE m_thread;
HANDLE m_stopEvent;
python::object m_global;
CComQIPtr<IDebugControl> m_debugControl;
};
////////////////////////////////////////////////////////////////////////////////
BOOL WINAPI DllMain( BOOL WINAPI DllMain(
__in HINSTANCE /*hinstDLL*/, __in HINSTANCE /*hinstDLL*/,
__in DWORD fdwReason, __in DWORD fdwReason,
@ -121,6 +194,8 @@ py( PDEBUG_CLIENT4 client, PCSTR args )
python::object global(main.attr("__dict__")); python::object global(main.attr("__dict__"));
InterruptWatch interruptWatch( client, global );
// íàñòðàèâàåì ââîä/âûâîä ( ÷òîáû â ñêðèïòå ìîæíî áûëî ïèñàòü print ) // íàñòðàèâàåì ââîä/âûâîä ( ÷òîáû â ñêðèïòå ìîæíî áûëî ïèñàòü print )
python::object sys = python::import("sys"); python::object sys = python::import("sys");
@ -228,24 +303,28 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args )
client->GetOutputMask( &mask ); client->GetOutputMask( &mask );
client->SetOutputMask( mask & ~DEBUG_OUTPUT_PROMPT ); // óáðàòü ýõî ââîäà client->SetOutputMask( mask & ~DEBUG_OUTPUT_PROMPT ); // óáðàòü ýõî ââîäà
InterruptWatch interruptWatch( client, WindbgGlobalSession::global() );
try { try {
PyRun_String( python::exec(
"__import__('code').InteractiveConsole(__import__('__main__').__dict__).interact()", "try:\n"
Py_file_input, " __import__('code').InteractiveConsole(__import__('__main__').__dict__).interact()\n"
WindbgGlobalSession::global().ptr(), "except SystemExit:\n"
WindbgGlobalSession::global().ptr() " print 'Ctrl+Break'\n",
WindbgGlobalSession::global(),
WindbgGlobalSession::global()
); );
// âûõîä èç èíòåðïðåòàòîðà ïðîèñõîäèò ÷åðåç èñêëþ÷åíèå raise SystemExit(code)
// êîòîðîå ïîòîì ìîæåò ïîìåøàòü èñïîëíåíèþ callback îâ
PyErr_Clear();
} }
catch(...) catch(...)
{ {
eprintln( L"unexpected error" ); eprintln( L"unexpected error" );
} }
// âûõîä èç èíòåðïðåòàòîðà ïðîèñõîäèò ÷åðåç èñêëþ÷åíèå raise SystemExit(code)
// êîòîðîå ïîòîì ìîæåò ïîìåøàòü èñïîëíåíèþ callback îâ
PyErr_Clear();
client->SetOutputMask( mask ); client->SetOutputMask( mask );
WindbgGlobalSession::SavePyState(); WindbgGlobalSession::SavePyState();

View File

@ -154,8 +154,8 @@ class IpProtocol:
def getNextLayerPacket( self, dataPos ): def getNextLayerPacket( self, dataPos ):
return { return {
ICMP_PROTO : lambda x : "", ICMP_PROTO : lambda x : "",
UDP_PROTO : lambda x : UdpPacket(x), UDP_PROTO : lambda x : UdpPacket(x),
TCP_PROTO : lambda x : TcpPacket(x) TCP_PROTO : lambda x : TcpPacket(x)
}.get( self.typeVal, lambda x : "Unknown protocol" )(dataPos) }.get( self.typeVal, lambda x : "Unknown protocol" )(dataPos)
@ -382,7 +382,7 @@ def getPacketFromNb( nb ):
pcktBytes = list() pcktBytes = list()
mdl = typedVar( "ndis", "_MDL", nb.CurrentMdl ) mdl = typedVar( "ndis!_MDL", nb.CurrentMdl )
dataLength = nb.DataLength dataLength = nb.DataLength
dataOffset = nb.CurrentMdlOffset dataOffset = nb.CurrentMdlOffset
@ -395,9 +395,9 @@ def getPacketFromNb( nb ):
dataLength -= copyData dataLength -= copyData
mdl = typedVar( "ndis", "_MDL", mdl.Next ) mdl = typedVar( "ndis!_MDL", mdl.Next )
return pcktBytes return pcktBytes
@ -405,11 +405,11 @@ def getPacketsFromNbl( nblAddr ):
pcktList = list() pcktList = list()
nbl = typedVar( "ndis", "_NET_BUFFER_LIST", nblAddr ) nbl = typedVar( "ndis!_NET_BUFFER_LIST", nblAddr )
while True: while True:
nb = typedVar( "ndis", "_NET_BUFFER", nbl.FirstNetBuffer ) nb = typedVar( "ndis!_NET_BUFFER", nbl.FirstNetBuffer )
while True: while True:
@ -418,12 +418,12 @@ def getPacketsFromNbl( nblAddr ):
if nb.Next == 0: if nb.Next == 0:
break break
nb = typedVar( "ndis", "_NET_BUFFER", nb.Next ) nb = typedVar( "ndis!_NET_BUFFER", nb.Next )
if nbl.Next == 0: if nbl.Next == 0:
break break
nbl = typedVar( "ndis", "_NET_BUFFER_LIST", nbl.Next ) nbl = typedVar( "ndis!_NET_BUFFER_LIST", nbl.Next )
return pcktList return pcktList
@ -437,7 +437,7 @@ def printNblStruct( nblAddr ):
dprintln( "NET_BUFFER_LIST %#x" % nblAddr ) dprintln( "NET_BUFFER_LIST %#x" % nblAddr )
nbl = typedVar( "ndis", "_NET_BUFFER_LIST", nblAddr ) nbl = typedVar( "ndis!_NET_BUFFER_LIST", nblAddr )
nbAddr = nbl.FirstNetBuffer nbAddr = nbl.FirstNetBuffer
@ -445,7 +445,7 @@ def printNblStruct( nblAddr ):
dprint( "\tNET_BUFFER %#x" % nbAddr ) dprint( "\tNET_BUFFER %#x" % nbAddr )
nb = typedVar( "ndis", "_NET_BUFFER", nbAddr ) nb = typedVar( "ndis!_NET_BUFFER", nbAddr )
dprintln( " data length = %d, data offset = %#x " % ( nb.DataLength, nb.DataOffset ) ) dprintln( " data length = %d, data offset = %#x " % ( nb.DataLength, nb.DataOffset ) )
@ -455,7 +455,7 @@ def printNblStruct( nblAddr ):
dprint( "\t\tMDL %#x" % mdlAddr ) dprint( "\t\tMDL %#x" % mdlAddr )
mdl = typedVar( "ndis", "_MDL", mdlAddr ) mdl = typedVar( "ndis!_MDL", mdlAddr )
dprintln( " byte count = %d, byte offset = %#x, mapped addr = %#x" % ( mdl.ByteCount, mdl.ByteOffset, mdl.MappedSystemVa ) ) dprintln( " byte count = %d, byte offset = %#x, mapped addr = %#x" % ( mdl.ByteCount, mdl.ByteOffset, mdl.MappedSystemVa ) )