[pykd] updated: dbgprocess.cpp refactored

git-svn-id: https://pykd.svn.codeplex.com/svn@68324 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2011-07-27 08:49:09 +00:00
parent 162c58eb33
commit ab06800664
3 changed files with 170 additions and 297 deletions

View File

@ -17,111 +17,66 @@ getThreadList()
ULONG i; ULONG i;
ULONG oldThreadId = 0; 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] );
ULONG64 threadOffset;
hres = dbgExt->system->GetCurrentThreadDataOffset( &threadOffset );
ULONG threadCount;
hres = dbgExt->system->GetNumberThreads( &threadCount );
if ( FAILED( hres ) ) 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] ); dbgExt->system->SetCurrentThreadId( oldThreadId );
throw DbgException( "IDebugSystemObjects::GetCurrentThreadDataOffset failed" );
ULONG64 threadOffset;
hres = dbgExt->system->GetCurrentThreadDataOffset( &threadOffset );
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 ) return threadList;
dbgExt->system->SetCurrentThreadId( oldThreadId );
return boost::python::list();
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
bool void
setImplicitThread( setImplicitThread(
ULONG64 newThreadAddr ) ULONG64 newThreadAddr )
{ {
HRESULT hres; HRESULT hres;
try { newThreadAddr = addr64(newThreadAddr);
hres = dbgExt->system2->SetImplicitThreadDataOffset( newThreadAddr );
newThreadAddr = addr64(newThreadAddr); if ( FAILED( hres ) )
hres = dbgExt->system2->SetImplicitThreadDataOffset( newThreadAddr ); throw DbgException( "IDebugSystemObjects2::SetImplicitThreadDataOffset failed" );
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;
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
ULONG64 ULONG64
getImplicitThread() 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; return threadOffset;
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;
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -132,93 +87,66 @@ getCurrentStack()
HRESULT hres; HRESULT hres;
ULONG currentScope = 0; ULONG currentScope = 0;
try { hres = dbgExt->symbols3->GetCurrentScopeFrameIndex( &currentScope );
if ( FAILED( hres ) )
throw DbgException( "IDebugSymbol3::GetCurrentScopeFrameIndex failed" );
hres = dbgExt->symbols3->GetCurrentScopeFrameIndex( &currentScope ); boost::scoped_array<DEBUG_STACK_FRAME> frames(new DEBUG_STACK_FRAME [ 1000 ]);
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" );
ULONG filledFrames; boost::python::list frameList;
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] );
for ( ULONG i = 0; i < filledFrames; ++i ) boost::python::object frameObj( frame );
{
dbgStackFrameClass frame( frames[i] );
boost::python::object frameObj( frame ); hres = dbgExt->symbols->SetScope( NULL, &frames[i], NULL, sizeof(DEBUG_STACK_FRAME) );
if ( SUCCEEDED( hres ) )
hres = dbgExt->symbols->SetScope( NULL, &frames[i], NULL, sizeof(DEBUG_STACK_FRAME) ); frameObj.attr( "locals" ) = getLocals();
if ( SUCCEEDED( hres ) )
frameObj.attr( "locals" ) = getLocals();
frameList.append( frameObj );
}
dbgExt->symbols3->SetScopeFrameByIndex( currentScope );
return frameList;
frameList.append( frameObj );
} }
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 ); dbgExt->symbols3->SetScopeFrameByIndex( currentScope );
return boost::python::object(); return frameList;
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
boost::python::object std::string
getProcessorMode() getProcessorMode()
{ {
HRESULT hres; HRESULT hres;
try { ULONG processorMode;
hres = dbgExt->control->GetEffectiveProcessorType( &processorMode );
if ( FAILED( hres ) )
throw DbgException( "IDebugControl::GetEffectiveProcessorType failed" );
ULONG processorMode; switch( processorMode )
hres = dbgExt->control->GetEffectiveProcessorType( &processorMode ); {
if ( FAILED( hres ) ) case IMAGE_FILE_MACHINE_I386:
throw DbgException( "IDebugControl::GetEffectiveProcessorType failed" ); return "X86";
switch( processorMode ) case IMAGE_FILE_MACHINE_ARM:
{ return "ARM";
case IMAGE_FILE_MACHINE_I386:
return boost::python::str("X86");
case IMAGE_FILE_MACHINE_ARM: case IMAGE_FILE_MACHINE_IA64:
return boost::python::str("ARM"); return "IA64";
case IMAGE_FILE_MACHINE_IA64: case IMAGE_FILE_MACHINE_AMD64:
return boost::python::str("IA64"); return "X64";
case IMAGE_FILE_MACHINE_AMD64:
return boost::python::str("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( setProcessorMode(
const std::string &mode ) const std::string &mode )
{ {
HRESULT hres; HRESULT hres;
ULONG processorMode = ~0;
try { 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" );
ULONG processorMode = ~0; hres = dbgExt->control->SetEffectiveProcessorType( processorMode );
if ( FAILED( hres ) )
throw DbgException( "IDebugControl::SetEffectiveProcessorType failed" );
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" );
}
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 ULONG64
getCurrentProcess() getCurrentProcess()
{ {
HRESULT hres; HRESULT hres;
ULONG64 processAddr = 0;
try { hres = dbgExt->system2->GetImplicitProcessDataOffset( &processAddr );
if ( FAILED( hres ) )
throw DbgException( "IDebugSystemObjects2::GetImplicitProcessDataOffset failed" );
ULONG64 processAddr = 0; return processAddr;
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;
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -293,23 +196,12 @@ VOID
setCurrentProcess( setCurrentProcess(
ULONG64 processAddr ) ULONG64 processAddr )
{ {
HRESULT hres; HRESULT hres;
try { processAddr = addr64(processAddr);
hres = dbgExt->system2->SetImplicitProcessDataOffset( processAddr );
processAddr = addr64(processAddr); if ( FAILED( hres ) )
hres = dbgExt->system2->SetImplicitProcessDataOffset( processAddr ); throw DbgException( "IDebugSystemObjects2::SetImplicitProcessDataOffset failed" );
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" );
}
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -334,82 +226,62 @@ dbgStackFrameClass::print() const
boost::python::object boost::python::object
getLocals() getLocals()
{ {
HRESULT hres; HRESULT hres;
IDebugSymbolGroup *localSymbols = NULL; CComPtr<IDebugSymbolGroup> localSymbols;
IDebugSymbolGroup2 *localSymbols2 = NULL; CComPtr<IDebugSymbolGroup2> localSymbols2;
try { 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 ); 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 ) ) if ( FAILED( hres ) )
throw DbgException( "IDebugSymbols::GetScopeSymbolGroup failed" ); throw DbgException( "IDebugSymbolGroup::GetSymbolName failed" );
hres = localSymbols->QueryInterface( __uuidof(IDebugSymbolGroup2), (void**) &localSymbols2 ); DEBUG_SYMBOL_PARAMETERS symbolParam = {};
hres = localSymbols->GetSymbolParameters( i, 1, &symbolParam );
if ( FAILED( hres ) ) if ( FAILED( hres ) )
throw DbgException( "IDebugSymbols::QueryInterface failed to get IDebugSymbolGroup2" ); throw DbgException( "IDebugSymbolGroup::GetSymbolParameters failed" );
ULONG localNumber; char typeName[0x100];
hres = localSymbols->GetNumberSymbols( &localNumber ); hres = dbgExt->symbols->GetTypeName( symbolParam.Module, symbolParam.TypeId, typeName, sizeof(typeName), NULL );
if ( FAILED( hres ) )
throw DbgException( "IDebugSymbols::GetTypeName failed" );
boost::python::dict arr; char moduleName[0x100];
hres = dbgExt->symbols2->GetModuleNameString(
for ( ULONG i = 0; i < localNumber; ++i ) DEBUG_MODNAME_MODULE,
{ DEBUG_ANY_ID,
char varName[0x100]; symbolParam.Module,
moduleName,
hres = localSymbols->GetSymbolName( i, varName, sizeof(varName), NULL ); sizeof(moduleName),
if ( FAILED( hres ) ) NULL );
throw DbgException( "IDebugSymbolGroup::GetSymbolName failed" ); if ( FAILED( hres ) )
throw DbgException( "IDebugSymbols2::GetModuleNameString 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; ULONG64 varOffset;
hres = localSymbols2->GetSymbolOffset( i, &varOffset ); hres = localSymbols2->GetSymbolOffset( i, &varOffset );
if ( FAILED( hres ) ) if ( FAILED( hres ) )
throw DbgException( "IDebugSymbolGroup2::GetSymbolOffset failed" ); throw DbgException( "IDebugSymbolGroup2::GetSymbolOffset failed" );
arr[ varName ] = TypedVar( moduleName, typeName, varOffset ); 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();
return arr;
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////

View File

@ -7,7 +7,7 @@
boost::python::object boost::python::object
getThreadList(); getThreadList();
bool void
setImplicitThread( setImplicitThread(
ULONG64 newThreadAddr ); ULONG64 newThreadAddr );
@ -36,7 +36,8 @@ public:
print() const; print() const;
}; };
boost::python::object //boost::python::object
std::string
getProcessorMode(); getProcessorMode();
void void

View File

@ -28,7 +28,7 @@
#include <tchar.h> #include <tchar.h>
#include <windows.h> #include <windows.h>
#include <atlbase.h>
#ifndef __field_ecount_opt #ifndef __field_ecount_opt