diff --git a/pykd/dbgprocess.cpp b/pykd/dbgprocess.cpp index ec32030..78284fe 100644 --- a/pykd/dbgprocess.cpp +++ b/pykd/dbgprocess.cpp @@ -17,111 +17,66 @@ getThreadList() ULONG i; ULONG oldThreadId = 0; - try { + ULONG threadCount; + hres = dbgExt->system->GetNumberThreads( &threadCount ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSystemObjects::GetNumberThreads failed" ); + + boost::scoped_array<ULONG> ids(new ULONG[threadCount]); + hres = dbgExt->system->GetThreadIdsByIndex( 0, threadCount, ids.get(), NULL ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSystemObjects::GetThreadIdsByIndex failed" ); + + hres = dbgExt->system->GetCurrentThreadId( &oldThreadId ); + + boost::python::list threadList; + + for ( i = 0; i < threadCount; ++i ) + { + dbgExt->system->SetCurrentThreadId( ids[i] ); - ULONG threadCount; - hres = dbgExt->system->GetNumberThreads( &threadCount ); + ULONG64 threadOffset; + hres = dbgExt->system->GetCurrentThreadDataOffset( &threadOffset ); + if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects::GetNumberThreads failed" ); - - boost::scoped_array<ULONG> ids(new ULONG[threadCount]); - hres = dbgExt->system->GetThreadIdsByIndex( 0, threadCount, ids.get(), NULL ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects::GetThreadIdsByIndex failed" ); - - hres = dbgExt->system->GetCurrentThreadId( &oldThreadId ); - - boost::python::list threadList; - - for ( i = 0; i < threadCount; ++i ) { - dbgExt->system->SetCurrentThreadId( ids[i] ); - - ULONG64 threadOffset; - hres = dbgExt->system->GetCurrentThreadDataOffset( &threadOffset ); + dbgExt->system->SetCurrentThreadId( oldThreadId ); + throw DbgException( "IDebugSystemObjects::GetCurrentThreadDataOffset failed" ); + } - if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects::GetCurrentThreadDataOffset failed" ); - - threadList.append( threadOffset ); - } - - return threadList; + threadList.append( threadOffset ); } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - if ( oldThreadId ) - dbgExt->system->SetCurrentThreadId( oldThreadId ); - - return boost::python::list(); + + return threadList; } ///////////////////////////////////////////////////////////////////////////////// -bool +void setImplicitThread( ULONG64 newThreadAddr ) { HRESULT hres; - try { - - newThreadAddr = addr64(newThreadAddr); - hres = dbgExt->system2->SetImplicitThreadDataOffset( newThreadAddr ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects2::SetImplicitThreadDataOffset failed" ); - - return true; - - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - return false; + newThreadAddr = addr64(newThreadAddr); + hres = dbgExt->system2->SetImplicitThreadDataOffset( newThreadAddr ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSystemObjects2::SetImplicitThreadDataOffset failed" ); } - ///////////////////////////////////////////////////////////////////////////////// ULONG64 getImplicitThread() { - HRESULT hres; + HRESULT hres; + ULONG64 threadOffset = -1; - try { + hres = dbgExt->system2->GetImplicitThreadDataOffset( &threadOffset ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSystemObjects2::GetImplicitThreadDataOffset failed" ); - ULONG64 threadOffset = -1; - - hres = dbgExt->system2->GetImplicitThreadDataOffset( &threadOffset ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects2::GetImplicitThreadDataOffset failed" ); - - return threadOffset; - - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - return -1; + return threadOffset; } ///////////////////////////////////////////////////////////////////////////////// @@ -131,94 +86,67 @@ getCurrentStack() { HRESULT hres; ULONG currentScope = 0; + + hres = dbgExt->symbols3->GetCurrentScopeFrameIndex( ¤tScope ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbol3::GetCurrentScopeFrameIndex failed" ); - try { - - hres = dbgExt->symbols3->GetCurrentScopeFrameIndex( ¤tScope ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbol3::GetCurrentScopeFrameIndex failed" ); - - boost::scoped_array<DEBUG_STACK_FRAME> frames(new DEBUG_STACK_FRAME [ 1000 ]); - - ULONG filledFrames; - hres = dbgExt->control->GetStackTrace( 0, 0, 0, frames.get(), 1000, &filledFrames ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugControl::GetStackTrace failed" ); - - boost::python::list frameList; - - for ( ULONG i = 0; i < filledFrames; ++i ) - { - dbgStackFrameClass frame( frames[i] ); - - boost::python::object frameObj( frame ); - - hres = dbgExt->symbols->SetScope( NULL, &frames[i], NULL, sizeof(DEBUG_STACK_FRAME) ); - if ( SUCCEEDED( hres ) ) - frameObj.attr( "locals" ) = getLocals(); - - frameList.append( frameObj ); - } - - dbgExt->symbols3->SetScopeFrameByIndex( currentScope ); + boost::scoped_array<DEBUG_STACK_FRAME> frames(new DEBUG_STACK_FRAME [ 1000 ]); - return frameList; + ULONG filledFrames; + hres = dbgExt->control->GetStackTrace( 0, 0, 0, frames.get(), 1000, &filledFrames ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::GetStackTrace failed" ); - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - dbgExt->symbols3->SetScopeFrameByIndex( currentScope ); + boost::python::list frameList; + + for ( ULONG i = 0; i < filledFrames; ++i ) + { + dbgStackFrameClass frame( frames[i] ); + + boost::python::object frameObj( frame ); - return boost::python::object(); + hres = dbgExt->symbols->SetScope( NULL, &frames[i], NULL, sizeof(DEBUG_STACK_FRAME) ); + if ( SUCCEEDED( hres ) ) + frameObj.attr( "locals" ) = getLocals(); + + frameList.append( frameObj ); + } + + dbgExt->symbols3->SetScopeFrameByIndex( currentScope ); + + return frameList; } ///////////////////////////////////////////////////////////////////////////////// -boost::python::object +std::string getProcessorMode() { HRESULT hres; - try { - - ULONG processorMode; - hres = dbgExt->control->GetEffectiveProcessorType( &processorMode ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugControl::GetEffectiveProcessorType failed" ); + ULONG processorMode; + hres = dbgExt->control->GetEffectiveProcessorType( &processorMode ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::GetEffectiveProcessorType failed" ); + + switch( processorMode ) + { + case IMAGE_FILE_MACHINE_I386: + return "X86"; - switch( processorMode ) - { - case IMAGE_FILE_MACHINE_I386: - return boost::python::str("X86"); - - case IMAGE_FILE_MACHINE_ARM: - return boost::python::str("ARM"); - - case IMAGE_FILE_MACHINE_IA64: - return boost::python::str("IA64"); - - case IMAGE_FILE_MACHINE_AMD64: - return boost::python::str("X64"); - - } + case IMAGE_FILE_MACHINE_ARM: + return "ARM"; + + case IMAGE_FILE_MACHINE_IA64: + return "IA64"; + + case IMAGE_FILE_MACHINE_AMD64: + return "X64"; } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - return boost::python::object(); + + throw DbgException( "Unknown CPU type" ); + } ///////////////////////////////////////////////////////////////////////////////// @@ -227,36 +155,24 @@ void setProcessorMode( const std::string &mode ) { - HRESULT hres; + HRESULT hres; + ULONG processorMode = ~0; - try { - - ULONG processorMode = ~0; - - if ( mode == "X86" ) - processorMode = IMAGE_FILE_MACHINE_I386; - else if ( mode == "ARM" ) - processorMode = IMAGE_FILE_MACHINE_ARM; - else if ( mode == "IA64" ) - processorMode = IMAGE_FILE_MACHINE_IA64; - else if ( mode == "X64" ) - processorMode = IMAGE_FILE_MACHINE_AMD64; - else - throw DbgException( "Unknown processor type" ); + if ( mode == "X86" ) + processorMode = IMAGE_FILE_MACHINE_I386; + else if ( mode == "ARM" ) + processorMode = IMAGE_FILE_MACHINE_ARM; + else if ( mode == "IA64" ) + processorMode = IMAGE_FILE_MACHINE_IA64; + else if ( mode == "X64" ) + processorMode = IMAGE_FILE_MACHINE_AMD64; + else + throw DbgException( "Unknown processor type" ); + + hres = dbgExt->control->SetEffectiveProcessorType( processorMode ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugControl::SetEffectiveProcessorType failed" ); - hres = dbgExt->control->SetEffectiveProcessorType( processorMode ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugControl::SetEffectiveProcessorType failed" ); - - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } } ///////////////////////////////////////////////////////////////////////////////// @@ -264,27 +180,14 @@ setProcessorMode( ULONG64 getCurrentProcess() { - HRESULT hres; - - try { - - ULONG64 processAddr = 0; - hres = dbgExt->system2->GetImplicitProcessDataOffset( &processAddr ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects2::GetImplicitProcessDataOffset failed" ); - - return processAddr; - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - return 0; + HRESULT hres; + ULONG64 processAddr = 0; + + hres = dbgExt->system2->GetImplicitProcessDataOffset( &processAddr ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSystemObjects2::GetImplicitProcessDataOffset failed" ); + + return processAddr; } ///////////////////////////////////////////////////////////////////////////////// @@ -293,23 +196,12 @@ VOID setCurrentProcess( ULONG64 processAddr ) { - HRESULT hres; + HRESULT hres; - try { - - processAddr = addr64(processAddr); - hres = dbgExt->system2->SetImplicitProcessDataOffset( processAddr ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSystemObjects2::SetImplicitProcessDataOffset failed" ); - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } + processAddr = addr64(processAddr); + hres = dbgExt->system2->SetImplicitProcessDataOffset( processAddr ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSystemObjects2::SetImplicitProcessDataOffset failed" ); } ///////////////////////////////////////////////////////////////////////////////// @@ -334,82 +226,62 @@ dbgStackFrameClass::print() const boost::python::object getLocals() { - HRESULT hres; - IDebugSymbolGroup *localSymbols = NULL; - IDebugSymbolGroup2 *localSymbols2 = NULL; - - try { + HRESULT hres; + CComPtr<IDebugSymbolGroup> localSymbols; + CComPtr<IDebugSymbolGroup2> localSymbols2; - hres = dbgExt->symbols->GetScopeSymbolGroup( DEBUG_SCOPE_GROUP_ARGUMENTS|DEBUG_SCOPE_GROUP_LOCALS, NULL, &localSymbols ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbols::GetScopeSymbolGroup failed" ); + hres = dbgExt->symbols->GetScopeSymbolGroup( DEBUG_SCOPE_GROUP_ARGUMENTS|DEBUG_SCOPE_GROUP_LOCALS, NULL, &localSymbols ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbols::GetScopeSymbolGroup failed" ); - hres = localSymbols->QueryInterface( __uuidof(IDebugSymbolGroup2), (void**) &localSymbols2 ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbols::QueryInterface failed to get IDebugSymbolGroup2" ); + hres = localSymbols->QueryInterface( __uuidof(IDebugSymbolGroup2), (void**) &localSymbols2 ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbols::QueryInterface failed to get IDebugSymbolGroup2" ); - ULONG localNumber; - hres = localSymbols->GetNumberSymbols( &localNumber ); - - boost::python::dict arr; - - for ( ULONG i = 0; i < localNumber; ++i ) - { - char varName[0x100]; - - hres = localSymbols->GetSymbolName( i, varName, sizeof(varName), NULL ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbolGroup::GetSymbolName failed" ); - - DEBUG_SYMBOL_PARAMETERS symbolParam = {}; - hres = localSymbols->GetSymbolParameters( i, 1, &symbolParam ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbolGroup::GetSymbolParameters failed" ); - - char typeName[0x100]; - hres = dbgExt->symbols->GetTypeName( symbolParam.Module, symbolParam.TypeId, typeName, sizeof(typeName), NULL ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbols::GetTypeName failed" ); - - char moduleName[0x100]; - hres = dbgExt->symbols2->GetModuleNameString( - DEBUG_MODNAME_MODULE, - DEBUG_ANY_ID, - symbolParam.Module, - moduleName, - sizeof(moduleName), - NULL ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbols2::GetModuleNameString failed" ); - - - ULONG64 varOffset; - hres = localSymbols2->GetSymbolOffset( i, &varOffset ); - if ( FAILED( hres ) ) - throw DbgException( "IDebugSymbolGroup2::GetSymbolOffset failed" ); - - arr[ varName ] = TypedVar( moduleName, typeName, varOffset ); - } - - return arr; - } - catch( std::exception &e ) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd error: %s\n", e.what() ); - } - catch(...) - { - dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "pykd unexpected error\n" ); - } - - if ( localSymbols ) - localSymbols->Release(); - - if ( localSymbols2 ) - localSymbols2->Release(); - - return boost::python::object(); - + ULONG localNumber; + hres = localSymbols->GetNumberSymbols( &localNumber ); + + boost::python::dict arr; + + for ( ULONG i = 0; i < localNumber; ++i ) + { + char varName[0x100]; + + hres = localSymbols->GetSymbolName( i, varName, sizeof(varName), NULL ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbolGroup::GetSymbolName failed" ); + + DEBUG_SYMBOL_PARAMETERS symbolParam = {}; + hres = localSymbols->GetSymbolParameters( i, 1, &symbolParam ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbolGroup::GetSymbolParameters failed" ); + + char typeName[0x100]; + hres = dbgExt->symbols->GetTypeName( symbolParam.Module, symbolParam.TypeId, typeName, sizeof(typeName), NULL ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbols::GetTypeName failed" ); + + char moduleName[0x100]; + hres = dbgExt->symbols2->GetModuleNameString( + DEBUG_MODNAME_MODULE, + DEBUG_ANY_ID, + symbolParam.Module, + moduleName, + sizeof(moduleName), + NULL ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbols2::GetModuleNameString failed" ); + + + ULONG64 varOffset; + hres = localSymbols2->GetSymbolOffset( i, &varOffset ); + if ( FAILED( hres ) ) + throw DbgException( "IDebugSymbolGroup2::GetSymbolOffset failed" ); + + arr[ varName ] = TypedVar( moduleName, typeName, varOffset ); + } + + return arr; } ///////////////////////////////////////////////////////////////////////////////// diff --git a/pykd/dbgprocess.h b/pykd/dbgprocess.h index a1c2566..1425c7b 100644 --- a/pykd/dbgprocess.h +++ b/pykd/dbgprocess.h @@ -7,7 +7,7 @@ boost::python::object getThreadList(); -bool +void setImplicitThread( ULONG64 newThreadAddr ); @@ -36,7 +36,8 @@ public: print() const; }; -boost::python::object +//boost::python::object +std::string getProcessorMode(); void diff --git a/pykd/stdafx.h b/pykd/stdafx.h index 871ab52..94377b2 100644 --- a/pykd/stdafx.h +++ b/pykd/stdafx.h @@ -28,7 +28,7 @@ #include <tchar.h> #include <windows.h> - +#include <atlbase.h> #ifndef __field_ecount_opt