[0.3.x] refactored : working with processes and threads

git-svn-id: https://pykd.svn.codeplex.com/svn@87322 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2014-02-20 09:32:17 +00:00 committed by Mikhail I. Izmestev
parent 7f5fc629bf
commit 17cf689e36
4 changed files with 97 additions and 50 deletions

View File

@ -111,6 +111,12 @@ inline unsigned long getNumberThreads()
return kdlib::getNumberThreads(); return kdlib::getNumberThreads();
} }
inline kdlib::THREAD_DEBUG_ID getThreadIdByIndex(unsigned long index)
{
AutoRestorePyState pystate;
return kdlib::getThreadIdByIndex(index);
}
inline kdlib::THREAD_DEBUG_ID getCurrentThreadId() inline kdlib::THREAD_DEBUG_ID getCurrentThreadId()
{ {
AutoRestorePyState pystate; AutoRestorePyState pystate;
@ -123,7 +129,7 @@ inline kdlib::THREAD_DEBUG_ID getThreadIdByOffset(kdlib::MEMOFFSET_64 offset)
return kdlib::getThreadIdByOffset(offset); return kdlib::getThreadIdByOffset(offset);
} }
inline kdlib::THREAD_DEBUG_ID getThreadIdBySystemId(kdlib::THREAD_ID tid) inline kdlib::THREAD_DEBUG_ID getThreadIdBySystemId(kdlib::THREAD_ID tid = -1)
{ {
AutoRestorePyState pystate; AutoRestorePyState pystate;
return kdlib::getThreadIdBySystemId(tid); return kdlib::getThreadIdBySystemId(tid);
@ -141,6 +147,12 @@ inline kdlib::MEMOFFSET_64 getThreadOffset(kdlib::THREAD_DEBUG_ID id = -1)
return kdlib::getThreadOffset(id); return kdlib::getThreadOffset(id);
} }
inline kdlib::MEMOFFSET_64 getCurrentThread()
{
AutoRestorePyState pystate;
return kdlib::getThreadOffset(kdlib::getCurrentThreadId());
}
inline void setCurrentThread(kdlib::THREAD_DEBUG_ID id) inline void setCurrentThread(kdlib::THREAD_DEBUG_ID id)
{ {
AutoRestorePyState pystate; AutoRestorePyState pystate;
@ -165,10 +177,16 @@ inline unsigned long getNumberProcesses()
return kdlib::getNumberProcesses(); return kdlib::getNumberProcesses();
} }
inline kdlib::PROCESS_DEBUG_ID getCurrentProcessId() inline kdlib::PROCESS_DEBUG_ID getProcessIdByIndex(unsigned long index)
{ {
AutoRestorePyState pystate; AutoRestorePyState pystate;
return kdlib::getCurrentProcessId(); return kdlib::getProcessIdByIndex(index);
}
inline kdlib::MEMOFFSET_64 getCurrentProcess()
{
AutoRestorePyState pystate;
return kdlib::getProcessOffset( kdlib::getCurrentProcessId() );
} }
inline kdlib::PROCESS_DEBUG_ID getProcessIdByOffset( kdlib::MEMOFFSET_64 offset ) inline kdlib::PROCESS_DEBUG_ID getProcessIdByOffset( kdlib::MEMOFFSET_64 offset )
@ -177,7 +195,7 @@ inline kdlib::PROCESS_DEBUG_ID getProcessIdByOffset( kdlib::MEMOFFSET_64 offset
return kdlib::getProcessIdByOffset(offset); return kdlib::getProcessIdByOffset(offset);
} }
inline kdlib::PROCESS_DEBUG_ID getProcessIdBySystemId( kdlib::PROCESS_ID pid ) inline kdlib::PROCESS_DEBUG_ID getProcessIdBySystemId( kdlib::PROCESS_ID pid = -1 )
{ {
AutoRestorePyState pystate; AutoRestorePyState pystate;
return kdlib::getProcessIdBySystemId(pid); return kdlib::getProcessIdBySystemId(pid);

View File

@ -66,4 +66,32 @@ public:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
inline void dprint( const std::wstring &str, bool dml = false )
{
kdlib::dprint(str,dml);
}
///////////////////////////////////////////////////////////////////////////////
inline void dprintln( const std::wstring &str, bool dml = false )
{
kdlib::dprintln(str,dml);
}
///////////////////////////////////////////////////////////////////////////////
inline void eprint( const std::wstring &str )
{
kdlib::eprint(str);
}
///////////////////////////////////////////////////////////////////////////////
inline void eprintln( const std::wstring &str )
{
kdlib::eprintln(str);
}
///////////////////////////////////////////////////////////////////////////////
} }

View File

@ -3,8 +3,6 @@
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include "kdlib/kdlib.h"
#include "pykdver.h" #include "pykdver.h"
#include "variant.h" #include "variant.h"
@ -42,8 +40,8 @@ BOOST_PYTHON_FUNCTION_OVERLOADS( terminateProcess_, pykd::terminateProcess, 0,
BOOST_PYTHON_FUNCTION_OVERLOADS( attachKernel_, pykd::attachKernel, 0, 1 ); BOOST_PYTHON_FUNCTION_OVERLOADS( attachKernel_, pykd::attachKernel, 0, 1 );
BOOST_PYTHON_FUNCTION_OVERLOADS( evaluate_, pykd::evaluate, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( evaluate_, pykd::evaluate, 1, 2 );
BOOST_PYTHON_FUNCTION_OVERLOADS( dprint_, kdlib::dprint, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( dprint_, pykd::dprint, 1, 2 );
BOOST_PYTHON_FUNCTION_OVERLOADS( dprintln_, kdlib::dprintln, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( dprintln_, pykd::dprintln, 1, 2 );
//BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( Module_findSymbol, Module::getSymbolNameByVa, 1, 2 ); //BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( Module_findSymbol, Module::getSymbolNameByVa, 1, 2 );
@ -65,24 +63,26 @@ BOOST_PYTHON_FUNCTION_OVERLOADS( getSourceFile_, pykd::getSourceFile, 0, 1 );
BOOST_PYTHON_FUNCTION_OVERLOADS( getSourceLine_, pykd::getSourceLine, 0, 1 ); BOOST_PYTHON_FUNCTION_OVERLOADS( getSourceLine_, pykd::getSourceLine, 0, 1 );
BOOST_PYTHON_FUNCTION_OVERLOADS( findSymbol_, pykd::findSymbol, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( findSymbol_, pykd::findSymbol, 1, 2 );
//BOOST_PYTHON_FUNCTION_OVERLOADS( setHardwareBp_, setHardwareBp, 3, 4 ); BOOST_PYTHON_FUNCTION_OVERLOADS( getProcessOffset_, pykd::getProcessOffset, 0, 1);
// BOOST_PYTHON_FUNCTION_OVERLOADS( getProcessSystemId_, pykd::getProcessSystemId, 0, 1);
BOOST_PYTHON_FUNCTION_OVERLOADS( createStruct_, kdlib::defineStruct, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( getProcessIdBySystemId_, pykd::getProcessIdBySystemId, 0, 1 );
BOOST_PYTHON_FUNCTION_OVERLOADS( createUnion_, kdlib::defineUnion, 1, 2 );
// BOOST_PYTHON_FUNCTION_OVERLOADS( getThreadOffset_, pykd::getThreadOffset, 0, 1);
BOOST_PYTHON_FUNCTION_OVERLOADS( getThreadSystemId_, pykd::getThreadSystemId, 0, 1);
BOOST_PYTHON_FUNCTION_OVERLOADS( getThreadIdBySystemId_, pykd::getThreadIdBySystemId, 0, 1);
BOOST_PYTHON_FUNCTION_OVERLOADS( createStruct_, pykd::defineStruct, 1, 2 );
BOOST_PYTHON_FUNCTION_OVERLOADS( createUnion_, pykd::defineUnion, 1, 2 );
BOOST_PYTHON_FUNCTION_OVERLOADS( Module_enumSymbols, ModuleAdapter::enumSymbols, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( Module_enumSymbols, ModuleAdapter::enumSymbols, 1, 2 );
BOOST_PYTHON_FUNCTION_OVERLOADS( Module_findSymbol, ModuleAdapter::findSymbol, 2, 3 ); BOOST_PYTHON_FUNCTION_OVERLOADS( Module_findSymbol, ModuleAdapter::findSymbol, 2, 3 );
BOOST_PYTHON_FUNCTION_OVERLOADS( TypeInfo_ptrTo, TypeInfoAdapter::ptrTo, 1, 2 ); BOOST_PYTHON_FUNCTION_OVERLOADS( TypeInfo_ptrTo, TypeInfoAdapter::ptrTo, 1, 2 );
//BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS( Module_findSymbol, Module::getSymbolNameByVa, 1, 2 );
pykd::SysDbgOut sysPykdOut; pykd::SysDbgOut sysPykdOut;
pykd::SysDbgOut sysPykdErr; pykd::SysDbgOut sysPykdErr;
pykd::SysDbgIn sysPykdIn; pykd::SysDbgIn sysPykdIn;
BOOST_PYTHON_MODULE( pykd ) BOOST_PYTHON_MODULE( pykd )
{ {
// èñïîëüçîâàòü âìåñòî êîíñîëè ïîòîêè èç sys // èñïîëüçîâàòü âìåñòî êîíñîëè ïîòîêè èç sys
@ -91,6 +91,7 @@ BOOST_PYTHON_MODULE( pykd )
kdlib::dbgin = &sysPykdIn; kdlib::dbgin = &sysPykdIn;
python::scope().attr("__version__") = pykdVersion; python::scope().attr("__version__") = pykdVersion;
python::scope().attr("version") = pykdVersion;
python::def( "initialize", &kdlib::initialize, python::def( "initialize", &kdlib::initialize,
"Initialize debug engine, only for console mode" ); "Initialize debug engine, only for console mode" );
@ -157,9 +158,9 @@ BOOST_PYTHON_MODULE( pykd )
"Return current execution status" ); "Return current execution status" );
// Debug output // Debug output
python::def( "dprint", &kdlib::dprint, dprint_( python::args( "str", "dml" ), python::def( "dprint", &pykd::dprint, dprint_( python::args( "str", "dml" ),
"Print out string. If dml = True string is printed with dml highlighting ( only for windbg )" ) ); "Print out string. If dml = True string is printed with dml highlighting ( only for windbg )" ) );
python::def( "dprintln", &kdlib::dprintln, dprintln_( python::args( "str", "dml" ), python::def( "dprintln", &pykd::dprintln, dprintln_( python::args( "str", "dml" ),
"Print out string and insert end of line symbol. If dml = True string is printed with dml highlighting ( only for windbg )" ) ); "Print out string and insert end of line symbol. If dml = True string is printed with dml highlighting ( only for windbg )" ) );
// Python debug output console helper classes // Python debug output console helper classes
@ -348,20 +349,20 @@ BOOST_PYTHON_MODULE( pykd )
// processes and threads // processes and threads
python::def ( "getNumberProcesses", pykd::getNumberProcesses, python::def ( "getNumberProcesses", pykd::getNumberProcesses,
"Return number of processes on the target system" ); "Return number of processes on the target system" );
python::def( "getCurrentProcess", pykd::getCurrentProcessId, python::def( "getProcessId", pykd::getProcessIdByIndex,
"Return ID of the current process. This ID can be used with terminateProcess" ); "Return process ID by index" );
python::def( "getProcessOffset", pykd::getProcessOffset, python::def( "getProcessOffset", pykd::getProcessOffset, getProcessOffset_( python::args( "Id" ),
"Return the location in the target's memory of the process structure ( PEB )" ); "Return the location in the target's memory of the process structure ( PEB )" ) );
python::def( "getProcessSystemID", pykd::getProcessSystemId, python::def( "getProcessSystemID", pykd::getProcessSystemId, getProcessSystemId_( python::args( "Id" ),
"Return system process ID ( PID )" ); "Return system process ID ( PID )" ) );
python::def( "getProcessId", pykd::getProcessIdByOffset, python::def( "getProcessIdBySystemID", pykd::getProcessIdBySystemId, getProcessIdBySystemId_( python::args("Pid"),
"Return process ID by the location in the target's memory of the process structure" ); "Return process ID by the system's process ID ( PID )" ) );
python::def( "getProcessId", pykd::getProcessIdBySystemId,
"Return process ID by the system's process ID ( PID )" );
python::def( "setCurrentProcess", pykd::setCurrentProcess, python::def( "setCurrentProcess", pykd::setCurrentProcess,
"Set current process by ID" ); "Set current process by ID" );
python::def( "getImplicitProcess", pykd::getImplicitProcessOffset, python::def( "getImplicitProcess", pykd::getImplicitProcessOffset,
"Return implicit process" ); "Return implicit process" );
python::def( "getCurrentProcess", pykd::getCurrentProcess,
"Return current offset" );
python::def( "setImplicitProcess", pykd::setImplicitProcess, python::def( "setImplicitProcess", pykd::setImplicitProcess,
"Set implicit process" ); "Set implicit process" );
// python::def( "getCurrentProcessExeName", &getCurrentProcessExecutableName, // python::def( "getCurrentProcessExeName", &getCurrentProcessExecutableName,
@ -371,19 +372,20 @@ BOOST_PYTHON_MODULE( pykd )
python::def( "getTargetProcesses", pykd::getTargetProcesses, python::def( "getTargetProcesses", pykd::getTargetProcesses,
"Get all target processes " ); "Get all target processes " );
python::def ( "getNumberThreads", pykd::getNumberThreads, python::def ( "getNumberThreads", pykd::getNumberThreads,
"Return number of threads on the target system" ); "Return number of threads on the target system" );
python::def( "getCurrentThread", pykd::getCurrentThreadId, python::def( "getThreadId", pykd::getThreadIdByIndex,
"Return ID of the current thread" ); "Return thread id by index");
python::def( "getThreadOffset", pykd::getThreadOffset, python::def( "getThreadOffset", pykd::getThreadOffset, getThreadOffset_( python::args("Id"),
"Return the location in the target's memory of the thread structure ( TEB )" ); "Return the location in the target's memory of the thread structure ( TEB )" ) );
python::def( "getThreadSystemID", pykd::getThreadSystemId, python::def( "getThreadSystemID", pykd::getThreadSystemId, getThreadSystemId_( python::args("Id"),
"Return system thread ID ( TID )" ); "Return system thread ID ( TID )" ) );
python::def( "getThreadId", pykd::getThreadIdByOffset, python::def( "getThreadId", pykd::getThreadIdByOffset,
"Return thread ID by the location in the target's memory of the thread structure" ); "Return thread ID by the location in the target's memory of the thread structure" );
python::def( "getThreadId", pykd::getThreadIdBySystemId, python::def( "getThreadId", pykd::getThreadIdBySystemId, getThreadIdBySystemId_( python::args("Tid"),
"Return thread ID by the system's thread ID ( PID )" ); "Return thread ID by the system's thread ID ( PID )" ) );
python::def( "getCurrentThread", pykd::getCurrentThread,
"Return current thread offset" );
python::def("setCurrentThread", pykd::setCurrentThread, python::def("setCurrentThread", pykd::setCurrentThread,
"Set current thread" ); "Set current thread" );
python::def( "getImplicitThread", pykd::getImplicitThreadOffset, python::def( "getImplicitThread", pykd::getImplicitThreadOffset,

View File

@ -11,9 +11,7 @@ namespace python = boost::python;
#include "dbgexcept.h" #include "dbgexcept.h"
#include "pydbgio.h" #include "pydbgio.h"
using namespace kdlib;
using namespace kdlib::windbg; using namespace kdlib::windbg;
using namespace pykd;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -148,7 +146,7 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
if ( global && local ) if ( global && local )
{ {
eprintln( L"-g(--global) and -l(--local) cannot be set together" ); pykd::eprintln( L"-g(--global) and -l(--local) cannot be set together" );
return; return;
} }
@ -159,7 +157,7 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
if ( scriptFileName.empty() ) if ( scriptFileName.empty() )
{ {
eprintln( L"script file not found" ); pykd::eprintln( L"script file not found" );
return; return;
} }
@ -177,7 +175,9 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
if ( !global ) if ( !global )
{ {
globalState = Py_NewInterpreter(); globalState = PyThreadState_Swap(NULL);
Py_NewInterpreter();
localState = PyThreadState_Get(); localState = PyThreadState_Get();
@ -219,13 +219,12 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
} }
catch( python::error_already_set const & ) catch( python::error_already_set const & )
{ {
printException(); pykd::printException();
} }
} }
if ( !global ) if ( !global )
{ {
PyInterpreterState *interpreter = localState->interp; PyInterpreterState *interpreter = localState->interp;
while( interpreter->tstate_head != NULL ) while( interpreter->tstate_head != NULL )
@ -266,7 +265,7 @@ void PykdExt::startConsole()
} }
catch( python::error_already_set const & ) catch( python::error_already_set const & )
{ {
printException(); pykd::printException();
} }
} }
@ -274,10 +273,10 @@ void PykdExt::startConsole()
void PykdExt::printUsage() void PykdExt::printUsage()
{ {
dprintln( L"usage: !py [options] [file]" ); pykd::dprintln( L"usage: !py [options] [file]" );
dprintln( L"Options:" ); pykd::dprintln( L"Options:" );
dprintln( L"-g --global : run code in the common namespace" ); pykd::dprintln( L"-g --global : run code in the common namespace" );
dprintln( L"-l --local : run code in the isolate namespace" ); pykd::dprintln( L"-l --local : run code in the isolate namespace" );
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////