From 1d27cb91d7972229f19fdee349fea22f44630944 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Thu, 14 Apr 2011 12:28:22 +0000 Subject: [PATCH] [pykd] fixed: issue 8470 ( python.exe crashes after first pykd call ) git-svn-id: https://pykd.svn.codeplex.com/svn@63970 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgclient.h | 49 +++++++++++++++++++++++++++++++++++++++++++++ pykd/dbgdump.cpp | 39 +++++++++++++++++------------------- pykd/dbgeventcb.cpp | 29 ++++++--------------------- pykd/dbgeventcb.h | 2 +- pykd/dbgext.cpp | 21 ++++++++++--------- pykd/dbgext.h | 2 ++ pykd/dbgsession.cpp | 27 ------------------------- pykd/dbgsession.h | 19 ------------------ pykd/pykd.vcproj | 8 ++++---- 9 files changed, 92 insertions(+), 104 deletions(-) create mode 100644 pykd/dbgclient.h delete mode 100644 pykd/dbgsession.cpp delete mode 100644 pykd/dbgsession.h diff --git a/pykd/dbgclient.h b/pykd/dbgclient.h new file mode 100644 index 0000000..c7d37e2 --- /dev/null +++ b/pykd/dbgclient.h @@ -0,0 +1,49 @@ +#pragma once + +#include "dbgext.h" +#include "dbgeventcb.h" + +/////////////////////////////////////////////////////////////////////////////// + +class dbgClient { + +public: + + dbgClient() + { + IDebugClient4 *client = NULL; + DebugCreate( __uuidof(IDebugClient4), (void **)&client ); + + m_ext = new DbgExt( client ); + } + + ~dbgClient() + { + removeEventsMgr(); + + delete m_ext; + } + + void startEventsMgr() { + + m_callbacks = new DbgEventCallbacksManager( m_ext->client ); + } + + void removeEventsMgr() { + + if ( m_callbacks ) + { + delete m_callbacks; + m_callbacks = NULL; + } + } + +private: + + DbgExt *m_ext; + DbgEventCallbacksManager *m_callbacks; +}; + +extern dbgClient g_dbgClient; + +/////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/pykd/dbgdump.cpp b/pykd/dbgdump.cpp index 79d0800..282702a 100644 --- a/pykd/dbgdump.cpp +++ b/pykd/dbgdump.cpp @@ -6,9 +6,14 @@ #include "dbgdump.h" #include "dbgexcept.h" #include "dbgeventcb.h" -#include "dbgsession.h" #include "dbgsystem.h" #include "dbgcmd.h" +#include "dbgclient.h" + +///////////////////////////////////////////////////////////////////////////////// + +static +bool dbgStarted = false; ///////////////////////////////////////////////////////////////////////////////// @@ -19,17 +24,11 @@ dbgLoadDump( const std::wstring &fileName ) HRESULT hres; try { - - if ( dbgExt ) - return false; - - IDebugClient4 *client = NULL; - hres = DebugCreate( __uuidof(IDebugClient4), (void **)&client ); - if ( FAILED( hres ) ) - return false; - dbgExt = new DbgExt( client ); - new DbgEventCallbacksManager( (IDebugClient*)client ); + if ( dbgStarted || isWindbgExt() ) + return false; + + g_dbgClient.startEventsMgr(); hres = dbgExt->client4->OpenDumpFileWide( fileName.c_str(), NULL ); @@ -39,6 +38,8 @@ dbgLoadDump( const std::wstring &fileName ) hres = dbgExt->control->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE); if ( FAILED( hres ) ) throw DbgException( "IDebugControl::WaitForEvent failed" ); + + dbgStarted = true; return true; } @@ -61,17 +62,11 @@ startProcess( const std::wstring &processName ) HRESULT hres; try { - - if ( dbgExt ) - return false; - IDebugClient4 *client = NULL; - hres = DebugCreate( __uuidof(IDebugClient4), (void **)&client ); - if ( FAILED( hres ) ) - return false; - - dbgExt = new DbgExt( client ); - new DbgEventCallbacksManager( (IDebugClient*)client ); + if ( dbgStarted || isWindbgExt() ) + return false; + + g_dbgClient.startEventsMgr(); ULONG opt; hres = dbgExt->control->GetEngineOptions( &opt ); @@ -93,6 +88,8 @@ startProcess( const std::wstring &processName ) hres = dbgExt->control->WaitForEvent(DEBUG_WAIT_DEFAULT, INFINITE); if ( FAILED( hres ) ) throw DbgException( "IDebugControl::WaitForEvent failed" ); + + dbgStarted = true; return true; } diff --git a/pykd/dbgeventcb.cpp b/pykd/dbgeventcb.cpp index 8a66813..de9d781 100644 --- a/pykd/dbgeventcb.cpp +++ b/pykd/dbgeventcb.cpp @@ -13,39 +13,24 @@ DbgEventCallbacksManager::DbgEventCallbacksManager( IDebugClient *client ) { HRESULT hres; - - m_debugClient = NULL; - + try { - if ( client == NULL ) - { - // случай, когда мы работаем в windbg. Мы не хотим менять поведение клиента отладчика - он - // должен продолжать обработку событий, поэтому мы создаем своего клиента - hres = DebugCreate( __uuidof(IDebugClient4), reinterpret_cast(&m_debugClient)); - if (FAILED(hres)) - throw DbgException( "DebugCreate failed" ); - } - else - { - // случай, когда мы работаем отдельно. В этом случае клиент весь в нашем распоряжении - hres =client->QueryInterface( __uuidof(IDebugClient4), reinterpret_cast(&m_debugClient)); - if (FAILED(hres)) - throw DbgException( "DebugCreate failed" ); - } + m_debugClient = client; + m_debugClient->AddRef(); hres = m_debugClient->SetEventCallbacks(this); if (FAILED(hres)) throw DbgException( "IDebugClient::SetEventCallbacks" ); } - catch( std::exception& ) + catch( std::exception& e) { - //dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); + dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); } catch(...) { - //dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); + dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); } } @@ -74,8 +59,6 @@ HRESULT DbgEventCallbacksManager::ChangeSymbolState( __in ULONG64 Argument ) { - DbgExt ext( m_debugClient ); - if ((DEBUG_CSS_LOADS & Flags)) { if (Argument) diff --git a/pykd/dbgeventcb.h b/pykd/dbgeventcb.h index 8bd196d..89ae44c 100644 --- a/pykd/dbgeventcb.h +++ b/pykd/dbgeventcb.h @@ -38,7 +38,7 @@ private: private: - IDebugClient4 *m_debugClient; + IDebugClient *m_debugClient; }; ////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp index cb3be0e..d4268f7 100644 --- a/pykd/dbgext.cpp +++ b/pykd/dbgext.cpp @@ -22,19 +22,20 @@ #include "dbgdump.h" #include "dbgexcept.h" #include "dbgeventcb.h" -#include "dbgsession.h" #include "dbgcallback.h" #include "dbgpath.h" #include "dbginput.h" #include "dbgprocess.h" #include "dbgsynsym.h" +#include "dbgclient.h" ////////////////////////////////////////////////////////////////////////////// // указатель на текущий интерфейс DbgExt *dbgExt = NULL; -static bool isWindbgExt(); +// глобальный клиент +dbgClient g_dbgClient; ////////////////////////////////////////////////////////////////////////////// @@ -382,7 +383,7 @@ public: private: WindbgGlobalSession() { - + PyImport_AppendInittab("pykd", initpykd ); Py_Initialize(); @@ -399,19 +400,20 @@ private: dbgIn din; sys.attr("stdin") = boost::python::object( din ); + + g_dbgClient.startEventsMgr(); } ~WindbgGlobalSession() { Py_Finalize(); + g_dbgClient.removeEventsMgr(); } boost::python::object main; - - DbgEventCallbacksManager callbackMgr; - + static volatile LONG sessionCount; - static WindbgGlobalSession *windbgGlobalSession; + static WindbgGlobalSession *windbgGlobalSession; }; volatile LONG WindbgGlobalSession::sessionCount = 0; @@ -633,12 +635,13 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args ) try { DbgExt ext( client ); - if ( !std::string( args ).empty() ) { try { + OutputReader outputReader( dbgExt->client ); + boost::python::exec( args, WindbgGlobalSession::global(), WindbgGlobalSession::global() ); } catch( boost::python::error_already_set const & ) @@ -676,7 +679,7 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args ) do { - OutputReader outputReader( dbgExt->client ); + OutputReader outputReader( (IDebugClient*)client ); HRESULT hres = dbgExt->control->Input( str, sizeof(str), &inputSize ); diff --git a/pykd/dbgext.h b/pykd/dbgext.h index ee0022a..461aab9 100644 --- a/pykd/dbgext.h +++ b/pykd/dbgext.h @@ -38,3 +38,5 @@ private: extern DbgExt *dbgExt; + +bool isWindbgExt(); diff --git a/pykd/dbgsession.cpp b/pykd/dbgsession.cpp deleted file mode 100644 index b711187..0000000 --- a/pykd/dbgsession.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "stdafx.h" - -//#include "dbgext.h" -//#include "dbgeventcb.h" -//#include "dbgsession.h" -// -//DbgExt dbgGlobalSession; -// -//bool dbgSessionStarted = false; -// -//void -//dbgCreateSession() -//{ -// IDebugClient4 *client = NULL; -// DebugCreate( __uuidof(IDebugClient4), (void **)&client ); -// -// SetupDebugEngine( client, &dbgGlobalSession ); -// dbgExt = &dbgGlobalSession; -// -// setDbgSessionStarted(); -//} -// -//bool -//dbgIsSessionStart() -//{ -// return dbgSessionStarted; -//} \ No newline at end of file diff --git a/pykd/dbgsession.h b/pykd/dbgsession.h deleted file mode 100644 index fe6d2d7..0000000 --- a/pykd/dbgsession.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -//void -//dbgCreateSession(); -// -//extern -//bool dbgSessionStarted; -// -//inline HRESULT setDbgSessionStarted() -//{ -// HRESULT hres = DbgEventCallbacks::Start(); -// if (SUCCEEDED(hres)) -// dbgSessionStarted = true; -// return hres; -//} -// -//bool -//dbgIsSessionStart(); - diff --git a/pykd/pykd.vcproj b/pykd/pykd.vcproj index 3ea8514..87b3ebe 100644 --- a/pykd/pykd.vcproj +++ b/pykd/pykd.vcproj @@ -462,6 +462,10 @@ RelativePath=".\dbgcallback.h" > + + @@ -510,10 +514,6 @@ RelativePath=".\dbgreg.h" > - -