[+] 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
This commit is contained in:
SND\kernelnet_cp 2010-10-25 07:54:10 +00:00
parent f8341a1a6d
commit 477da2e29f
6 changed files with 230 additions and 26 deletions

View File

@ -25,6 +25,7 @@
#include "dbgsession.h" #include "dbgsession.h"
#include "dbgcallback.h" #include "dbgcallback.h"
#include "dbgstack.h" #include "dbgstack.h"
#include "dbgpath.h"
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -188,7 +189,6 @@ HRESULT
CALLBACK CALLBACK
py( PDEBUG_CLIENT4 client, PCSTR args) py( PDEBUG_CLIENT4 client, PCSTR args)
{ {
try { try {
DbgExt ext = { 0 }; DbgExt ext = { 0 };
@ -238,29 +238,57 @@ py( PDEBUG_CLIENT4 client, PCSTR args)
PySys_SetArgv( 1, &emptyParam ); PySys_SetArgv( 1, &emptyParam );
} }
result = boost::python::exec_file( argsList[0].c_str(), global, global ); // íàéòè ïóòü ê ôàéëó
std::string fullFileName;
std::string filePath;
} if ( dbgPythonPath.findPath( argsList[0], fullFileName, filePath ) )
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); DWORD oldCurDirLen = GetCurrentDirectoryA( 0, NULL );
DbgPrint::dprintln( PyString_AS_STRING( s ) ); char *oldCurDirCstr = new char[ oldCurDirLen ];
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_XDECREF(errvalue);
Py_XDECREF(errtype);
Py_XDECREF(traceback);
}
SetCurrentDirectoryA( oldCurDirCstr );
delete[] oldCurDirCstr;
Py_DECREF(s);
} }
else
Py_XDECREF(errvalue); {
Py_XDECREF(errtype); DbgPrint::dprintln( "script file not found" );
Py_XDECREF(traceback); }
} }
catch(...) catch(...)
{ {
} }
@ -371,3 +399,19 @@ 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;
}
/////////////////////////////////////////////////////////////////////////////////

118
pykd/dbgpath.cpp Normal file
View File

@ -0,0 +1,118 @@
#include "stdafx.h"
#include "dbgpath.h"
#include <boost/tokenizer.hpp>
///////////////////////////////////////////////////////////////////////////////
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> 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<std::string>::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<std::string>::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
//);

33
pykd/dbgpath.h Normal file
View File

@ -0,0 +1,33 @@
#pragma once
#include <string>
#include <vector>
///////////////////////////////////////////////////////////////////////////////
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;
///////////////////////////////////////////////////////////////////////////////

View File

@ -5,3 +5,4 @@ EXPORTS
info info
py py
pycmd pycmd
pythonpath

View File

@ -375,6 +375,10 @@
RelativePath=".\dbgmodule.cpp" RelativePath=".\dbgmodule.cpp"
> >
</File> </File>
<File
RelativePath=".\dbgpath.cpp"
>
</File>
<File <File
RelativePath=".\dbgprint.cpp" RelativePath=".\dbgprint.cpp"
> >
@ -477,6 +481,10 @@
RelativePath=".\dbgmodule.h" RelativePath=".\dbgmodule.h"
> >
</File> </File>
<File
RelativePath=".\dbgpath.h"
>
</File>
<File <File
RelativePath=".\dbgprint.h" RelativePath=".\dbgprint.h"
> >

View File

@ -10,12 +10,12 @@ def loadSymbols():
nt.PsActiveProcessHead = getOffset( "nt", "PsActiveProcessHead" ) nt.PsActiveProcessHead = getOffset( "nt", "PsActiveProcessHead" )
def printStacks(): def processInfo():
processList = typedVarList( nt.PsActiveProcessHead, "nt", "_EPROCESS", "ActiveProcessLinks" ) processList = typedVarList( nt.PsActiveProcessHead, "nt", "_EPROCESS", "ActiveProcessLinks" )
for process in processList: 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 return
@ -29,4 +29,4 @@ if __name__ == "__main__":
loadSymbols() loadSymbols()
printStacks() processInfo()