From 477da2e29f9fcadf5ab681ac4db820036fcee7cb Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Mon, 25 Oct 2010 07:54:10 +0000 Subject: [PATCH] [+] added: !pythonpath windbg command - print enviroment var $pythonpath [+] added: !py windbg command uses $pythonpath var for search scripts git-svn-id: https://pykd.svn.codeplex.com/svn@56329 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgext.cpp | 88 ++++++++++++++++++++++++--------- pykd/dbgpath.cpp | 118 ++++++++++++++++++++++++++++++++++++++++++++ pykd/dbgpath.h | 33 +++++++++++++ pykd/pykd.def | 3 +- pykd/pykd.vcproj | 8 +++ samples/proclist.py | 6 +-- 6 files changed, 230 insertions(+), 26 deletions(-) create mode 100644 pykd/dbgpath.cpp create mode 100644 pykd/dbgpath.h diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp index e9402ad..72c0da2 100644 --- a/pykd/dbgext.cpp +++ b/pykd/dbgext.cpp @@ -25,6 +25,7 @@ #include "dbgsession.h" #include "dbgcallback.h" #include "dbgstack.h" +#include "dbgpath.h" ///////////////////////////////////////////////////////////////////////////////// @@ -188,7 +189,6 @@ HRESULT CALLBACK py( PDEBUG_CLIENT4 client, PCSTR args) { - try { DbgExt ext = { 0 }; @@ -201,7 +201,7 @@ py( PDEBUG_CLIENT4 client, PCSTR args) boost::python::object global(main.attr("__dict__")); boost::python::object result; - + // разбор параметров typedef boost::escaped_list_separator char_separator_t; typedef boost::tokenizer< char_separator_t > char_tokenizer_t; @@ -236,31 +236,59 @@ py( PDEBUG_CLIENT4 client, PCSTR args) char *emptyParam = ""; PySys_SetArgv( 1, &emptyParam ); - } + } - result = boost::python::exec_file( argsList[0].c_str(), global, global ); - - } - catch( boost::python::error_already_set const & ) - { - // ошибка в скрипте - PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL; + // найти путь к файлу + std::string fullFileName; + std::string filePath; - PyErr_Fetch( &errtype, &errvalue, &traceback ); - - if(errvalue != NULL) + if ( dbgPythonPath.findPath( argsList[0], fullFileName, filePath ) ) { - PyObject *s = PyObject_Str(errvalue); + DWORD oldCurDirLen = GetCurrentDirectoryA( 0, NULL ); + + char *oldCurDirCstr = new char[ oldCurDirLen ]; - DbgPrint::dprintln( PyString_AS_STRING( s ) ); + GetCurrentDirectoryA( oldCurDirLen, oldCurDirCstr ); + + SetCurrentDirectoryA( filePath.c_str() ); + + try { + + result = boost::python::exec_file( fullFileName.c_str(), global, global ); + + } + catch( boost::python::error_already_set const & ) + { + // ошибка в скрипте + PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL; + + PyErr_Fetch( &errtype, &errvalue, &traceback ); + + if(errvalue != NULL) + { + PyObject *s = PyObject_Str(errvalue); + + DbgPrint::dprintln( PyString_AS_STRING( s ) ); - Py_DECREF(s); + Py_DECREF(s); + } + + Py_XDECREF(errvalue); + Py_XDECREF(errtype); + Py_XDECREF(traceback); + } + + SetCurrentDirectoryA( oldCurDirCstr ); + + delete[] oldCurDirCstr; + } - - Py_XDECREF(errvalue); - Py_XDECREF(errtype); - Py_XDECREF(traceback); - } + else + { + DbgPrint::dprintln( "script file not found" ); + } + } + catch(...) { } @@ -370,4 +398,20 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args ) } -///////////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////////////// + +HRESULT +CALLBACK +pythonpath( PDEBUG_CLIENT4 client, PCSTR args ) +{ + DbgExt ext = { 0 }; + + SetupDebugEngine( client, &ext ); + dbgExt = &ext; + + DbgPrint::dprintln( dbgPythonPath.getStr() ); + + return S_OK; +} + +///////////////////////////////////////////////////////////////////////////////// diff --git a/pykd/dbgpath.cpp b/pykd/dbgpath.cpp new file mode 100644 index 0000000..914ec3b --- /dev/null +++ b/pykd/dbgpath.cpp @@ -0,0 +1,118 @@ +#include "stdafx.h" + +#include "dbgpath.h" + +#include + +/////////////////////////////////////////////////////////////////////////////// + +DbgPythonPath &dbgPythonPath = DbgPythonPath(); + + +/////////////////////////////////////////////////////////////////////////////// + +DbgPythonPath::DbgPythonPath() +{ + DWORD enviromentSize = 0; + + enviromentSize = GetEnvironmentVariableA( "PYTHONPATH", NULL, enviromentSize ); + + char *enviromentBuffer = new char[ enviromentSize ]; + + GetEnvironmentVariableA( "PYTHONPATH", enviromentBuffer, enviromentSize ); + + typedef boost::escaped_list_separator char_separator_t; + typedef boost::tokenizer< char_separator_t > char_tokenizer_t; + + std::string pytonPath( enviromentBuffer ); + + char_tokenizer_t token( pytonPath, char_separator_t( "", "; \t", "\"" ) ); + + for ( char_tokenizer_t::iterator it = token.begin(); it != token.end(); ++it ) + { + if ( *it != "" ) + m_pathList.push_back( *it ); + } + + delete[] enviromentBuffer; +} + +/////////////////////////////////////////////////////////////////////////////// + +std::string +DbgPythonPath::getStr() const +{ + std::string str; + std::vector::const_iterator it = m_pathList.begin(); + + for ( ; it != m_pathList.end(); ++it ) + { + str += *it; + str += ";"; + } + + return str; +} + +/////////////////////////////////////////////////////////////////////////////// + +bool +DbgPythonPath::findPath( + const std::string &fileName, + std::string &fullFileName, + std::string &filePath ) const +{ + bool result = false; + + std::vector::const_iterator it = m_pathList.begin(); + + for ( ; it != m_pathList.end(); ++it ) + { + DWORD bufSize = + SearchPathA( + (*it).c_str(), + fileName.c_str(), + NULL, + 0, + NULL, + NULL ); + + if ( bufSize > 0 ) + { + char *fullFileNameCStr = new char[ bufSize ]; + char *partFileNameCStr = NULL; + + SearchPathA( + (*it).c_str(), + fileName.c_str(), + NULL, + bufSize, + fullFileNameCStr, + &partFileNameCStr ); + + fullFileName = std::string( fullFileNameCStr ); + filePath = std::string( fullFileNameCStr, partFileNameCStr ); + + delete[] fullFileNameCStr; + + result = true; + + break; + } + } + + return result; +} + +////////////////////////////////////////////////////////////////////////////// + + + +// DWORD SearchPath( +// LPCTSTR lpPath, +// LPCTSTR lpFileName, +// LPCTSTR lpExtension, +// DWORD nBufferLength, +// LPTSTR lpBuffer, +// LPTSTR* lpFilePart +//); diff --git a/pykd/dbgpath.h b/pykd/dbgpath.h new file mode 100644 index 0000000..17a59f2 --- /dev/null +++ b/pykd/dbgpath.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +/////////////////////////////////////////////////////////////////////////////// + +class DbgPythonPath +{ +public: + + DbgPythonPath(); + + std::string + getStr() const; + + bool + findPath( + const std::string &fileName, + std::string &fullFileName, + std::string &filePath ) const; + + +private: + + std::vector< std::string > m_pathList; + +}; + +extern DbgPythonPath& dbgPythonPath; + + +/////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/pykd/pykd.def b/pykd/pykd.def index 327eb31..c781c4f 100644 --- a/pykd/pykd.def +++ b/pykd/pykd.def @@ -4,4 +4,5 @@ EXPORTS info py - pycmd \ No newline at end of file + pycmd + pythonpath \ No newline at end of file diff --git a/pykd/pykd.vcproj b/pykd/pykd.vcproj index 4055a6a..948018e 100644 --- a/pykd/pykd.vcproj +++ b/pykd/pykd.vcproj @@ -375,6 +375,10 @@ RelativePath=".\dbgmodule.cpp" > + + @@ -477,6 +481,10 @@ RelativePath=".\dbgmodule.h" > + + diff --git a/samples/proclist.py b/samples/proclist.py index d4308d2..ff0c2d4 100644 --- a/samples/proclist.py +++ b/samples/proclist.py @@ -10,12 +10,12 @@ def loadSymbols(): nt.PsActiveProcessHead = getOffset( "nt", "PsActiveProcessHead" ) -def printStacks(): +def processInfo(): processList = typedVarList( nt.PsActiveProcessHead, "nt", "_EPROCESS", "ActiveProcessLinks" ) for process in processList: - dprintln( "".join( [ chr(i) for k, i in process.ImageFileName.items() ] ) ) + dprintln( "".join( [ chr(i) for i in process.ImageFileName.values() ] ) ) return @@ -29,4 +29,4 @@ if __name__ == "__main__": loadSymbols() - printStacks() + processInfo()