From 17cf689e360a36f69769a6a4ed2e0c26dee3574b Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Thu, 20 Feb 2014 09:32:17 +0000 Subject: [PATCH] [0.3.x] refactored : working with processes and threads git-svn-id: https://pykd.svn.codeplex.com/svn@87322 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/pydbgeng.h | 26 ++++++++++++++--- pykd/pydbgio.h | 28 +++++++++++++++++++ pykd/pymod.cpp | 70 ++++++++++++++++++++++++---------------------- pykd/windbgext.cpp | 23 ++++++++------- 4 files changed, 97 insertions(+), 50 deletions(-) diff --git a/pykd/pydbgeng.h b/pykd/pydbgeng.h index 62c1e2a..504106b 100644 --- a/pykd/pydbgeng.h +++ b/pykd/pydbgeng.h @@ -111,6 +111,12 @@ inline unsigned long 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() { AutoRestorePyState pystate; @@ -123,7 +129,7 @@ inline kdlib::THREAD_DEBUG_ID getThreadIdByOffset(kdlib::MEMOFFSET_64 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; return kdlib::getThreadIdBySystemId(tid); @@ -141,6 +147,12 @@ inline kdlib::MEMOFFSET_64 getThreadOffset(kdlib::THREAD_DEBUG_ID id = -1) return kdlib::getThreadOffset(id); } +inline kdlib::MEMOFFSET_64 getCurrentThread() +{ + AutoRestorePyState pystate; + return kdlib::getThreadOffset(kdlib::getCurrentThreadId()); +} + inline void setCurrentThread(kdlib::THREAD_DEBUG_ID id) { AutoRestorePyState pystate; @@ -165,10 +177,16 @@ inline unsigned long getNumberProcesses() return kdlib::getNumberProcesses(); } -inline kdlib::PROCESS_DEBUG_ID getCurrentProcessId() +inline kdlib::PROCESS_DEBUG_ID getProcessIdByIndex(unsigned long index) { 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 ) @@ -177,7 +195,7 @@ inline kdlib::PROCESS_DEBUG_ID getProcessIdByOffset( kdlib::MEMOFFSET_64 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; return kdlib::getProcessIdBySystemId(pid); diff --git a/pykd/pydbgio.h b/pykd/pydbgio.h index 4d33379..43130b9 100644 --- a/pykd/pydbgio.h +++ b/pykd/pydbgio.h @@ -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); +} + +/////////////////////////////////////////////////////////////////////////////// + } diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index 4694a68..e58a028 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -3,8 +3,6 @@ #include -#include "kdlib/kdlib.h" - #include "pykdver.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( evaluate_, pykd::evaluate, 1, 2 ); -BOOST_PYTHON_FUNCTION_OVERLOADS( dprint_, kdlib::dprint, 1, 2 ); -BOOST_PYTHON_FUNCTION_OVERLOADS( dprintln_, kdlib::dprintln, 1, 2 ); +BOOST_PYTHON_FUNCTION_OVERLOADS( dprint_, pykd::dprint, 1, 2 ); +BOOST_PYTHON_FUNCTION_OVERLOADS( dprintln_, pykd::dprintln, 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( findSymbol_, pykd::findSymbol, 1, 2 ); -//BOOST_PYTHON_FUNCTION_OVERLOADS( setHardwareBp_, setHardwareBp, 3, 4 ); -// -BOOST_PYTHON_FUNCTION_OVERLOADS( createStruct_, kdlib::defineStruct, 1, 2 ); -BOOST_PYTHON_FUNCTION_OVERLOADS( createUnion_, kdlib::defineUnion, 1, 2 ); -// +BOOST_PYTHON_FUNCTION_OVERLOADS( getProcessOffset_, pykd::getProcessOffset, 0, 1); +BOOST_PYTHON_FUNCTION_OVERLOADS( getProcessSystemId_, pykd::getProcessSystemId, 0, 1); +BOOST_PYTHON_FUNCTION_OVERLOADS( getProcessIdBySystemId_, pykd::getProcessIdBySystemId, 0, 1 ); + +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_findSymbol, ModuleAdapter::findSymbol, 2, 3 ); - 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 sysPykdErr; pykd::SysDbgIn sysPykdIn; - BOOST_PYTHON_MODULE( pykd ) { // использовать вместо консоли потоки из sys @@ -91,6 +91,7 @@ BOOST_PYTHON_MODULE( pykd ) kdlib::dbgin = &sysPykdIn; python::scope().attr("__version__") = pykdVersion; + python::scope().attr("version") = pykdVersion; python::def( "initialize", &kdlib::initialize, "Initialize debug engine, only for console mode" ); @@ -157,9 +158,9 @@ BOOST_PYTHON_MODULE( pykd ) "Return current execution status" ); // 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 )" ) ); - 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 )" ) ); // Python debug output console helper classes @@ -348,20 +349,20 @@ BOOST_PYTHON_MODULE( pykd ) // processes and threads python::def ( "getNumberProcesses", pykd::getNumberProcesses, "Return number of processes on the target system" ); - python::def( "getCurrentProcess", pykd::getCurrentProcessId, - "Return ID of the current process. This ID can be used with terminateProcess" ); - python::def( "getProcessOffset", pykd::getProcessOffset, - "Return the location in the target's memory of the process structure ( PEB )" ); - python::def( "getProcessSystemID", pykd::getProcessSystemId, - "Return system process ID ( PID )" ); - python::def( "getProcessId", pykd::getProcessIdByOffset, - "Return process ID by the location in the target's memory of the process structure" ); - python::def( "getProcessId", pykd::getProcessIdBySystemId, - "Return process ID by the system's process ID ( PID )" ); + python::def( "getProcessId", pykd::getProcessIdByIndex, + "Return process ID by index" ); + python::def( "getProcessOffset", pykd::getProcessOffset, getProcessOffset_( python::args( "Id" ), + "Return the location in the target's memory of the process structure ( PEB )" ) ); + python::def( "getProcessSystemID", pykd::getProcessSystemId, getProcessSystemId_( python::args( "Id" ), + "Return system process ID ( PID )" ) ); + python::def( "getProcessIdBySystemID", pykd::getProcessIdBySystemId, getProcessIdBySystemId_( python::args("Pid"), + "Return process ID by the system's process ID ( PID )" ) ); python::def( "setCurrentProcess", pykd::setCurrentProcess, "Set current process by ID" ); python::def( "getImplicitProcess", pykd::getImplicitProcessOffset, "Return implicit process" ); + python::def( "getCurrentProcess", pykd::getCurrentProcess, + "Return current offset" ); python::def( "setImplicitProcess", pykd::setImplicitProcess, "Set implicit process" ); // python::def( "getCurrentProcessExeName", &getCurrentProcessExecutableName, @@ -371,19 +372,20 @@ BOOST_PYTHON_MODULE( pykd ) python::def( "getTargetProcesses", pykd::getTargetProcesses, "Get all target processes " ); - python::def ( "getNumberThreads", pykd::getNumberThreads, "Return number of threads on the target system" ); - python::def( "getCurrentThread", pykd::getCurrentThreadId, - "Return ID of the current thread" ); - python::def( "getThreadOffset", pykd::getThreadOffset, - "Return the location in the target's memory of the thread structure ( TEB )" ); - python::def( "getThreadSystemID", pykd::getThreadSystemId, - "Return system thread ID ( TID )" ); + python::def( "getThreadId", pykd::getThreadIdByIndex, + "Return thread id by index"); + python::def( "getThreadOffset", pykd::getThreadOffset, getThreadOffset_( python::args("Id"), + "Return the location in the target's memory of the thread structure ( TEB )" ) ); + python::def( "getThreadSystemID", pykd::getThreadSystemId, getThreadSystemId_( python::args("Id"), + "Return system thread ID ( TID )" ) ); python::def( "getThreadId", pykd::getThreadIdByOffset, "Return thread ID by the location in the target's memory of the thread structure" ); - python::def( "getThreadId", pykd::getThreadIdBySystemId, - "Return thread ID by the system's thread ID ( PID )" ); + python::def( "getThreadId", pykd::getThreadIdBySystemId, getThreadIdBySystemId_( python::args("Tid"), + "Return thread ID by the system's thread ID ( PID )" ) ); + python::def( "getCurrentThread", pykd::getCurrentThread, + "Return current thread offset" ); python::def("setCurrentThread", pykd::setCurrentThread, "Set current thread" ); python::def( "getImplicitThread", pykd::getImplicitThreadOffset, diff --git a/pykd/windbgext.cpp b/pykd/windbgext.cpp index cbf58c0..2863b98 100644 --- a/pykd/windbgext.cpp +++ b/pykd/windbgext.cpp @@ -11,9 +11,7 @@ namespace python = boost::python; #include "dbgexcept.h" #include "pydbgio.h" -using namespace kdlib; using namespace kdlib::windbg; -using namespace pykd; /////////////////////////////////////////////////////////////////////////////// @@ -148,7 +146,7 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) 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; } @@ -159,7 +157,7 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) if ( scriptFileName.empty() ) { - eprintln( L"script file not found" ); + pykd::eprintln( L"script file not found" ); return; } @@ -177,7 +175,9 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) if ( !global ) { - globalState = Py_NewInterpreter(); + globalState = PyThreadState_Swap(NULL); + + Py_NewInterpreter(); localState = PyThreadState_Get(); @@ -219,13 +219,12 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) } catch( python::error_already_set const & ) { - printException(); + pykd::printException(); } } if ( !global ) { - PyInterpreterState *interpreter = localState->interp; while( interpreter->tstate_head != NULL ) @@ -266,7 +265,7 @@ void PykdExt::startConsole() } catch( python::error_already_set const & ) { - printException(); + pykd::printException(); } } @@ -274,10 +273,10 @@ void PykdExt::startConsole() void PykdExt::printUsage() { - dprintln( L"usage: !py [options] [file]" ); - dprintln( L"Options:" ); - dprintln( L"-g --global : run code in the common namespace" ); - dprintln( L"-l --local : run code in the isolate namespace" ); + pykd::dprintln( L"usage: !py [options] [file]" ); + pykd::dprintln( L"Options:" ); + pykd::dprintln( L"-g --global : run code in the common namespace" ); + pykd::dprintln( L"-l --local : run code in the isolate namespace" ); } ///////////////////////////////////////////////////////////////////////////////