mirror of
https://github.com/ivellioscolin/pykd.git
synced 2025-04-19 19:13:22 +08:00
[0.3.x] updated : py command
git-svn-id: https://pykd.svn.codeplex.com/svn@83972 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
parent
89bb1c89ac
commit
663ffdf641
@ -11,6 +11,7 @@
|
||||
#include "memaccess.h"
|
||||
#include "typeinfo.h"
|
||||
#include "typedvar.h"
|
||||
#include "windbgext.h"
|
||||
|
||||
using namespace pykd;
|
||||
|
||||
@ -94,8 +95,8 @@ BOOST_PYTHON_MODULE( pykd )
|
||||
"Check if it is a dump analyzing ( not living debuggee )" );
|
||||
python::def( "isKernelDebugging", &kdlib::isKernelDebugging,
|
||||
"Check if kernel dubugging is running" );
|
||||
//python::def( "isWindbgExt", &WindbgGlobalSession::isInit,
|
||||
// "Check if script works in windbg context" );
|
||||
python::def( "isWindbgExt", &PykdExt::isInit,
|
||||
"Check if script works in windbg context" );
|
||||
python::def( "writeDump", &kdlib::writeDump,
|
||||
"Create memory dump file" );
|
||||
|
||||
|
@ -1,17 +1,26 @@
|
||||
#include "stdafx.h"
|
||||
|
||||
|
||||
#include <boost/python.hpp>
|
||||
namespace python = boost::python;
|
||||
|
||||
#include "kdlib/kdlib.h"
|
||||
#include "kdlib/windbg.h"
|
||||
|
||||
#include "windbgext.h"
|
||||
#include "dbgexcept.h"
|
||||
|
||||
using namespace kdlib;
|
||||
using namespace kdlib::windbg;
|
||||
using namespace pykd;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
KDLIB_WINDBG_EXTENSION_INIT( PykdExt );
|
||||
|
||||
bool PykdExt::isInit() {
|
||||
return WinDbgExt->isInit();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@ -25,7 +34,7 @@ void PykdExt::setUp()
|
||||
|
||||
Py_Initialize();
|
||||
|
||||
boost::python::import( "pykd" );
|
||||
python::import( "pykd" );
|
||||
|
||||
// ïåðåíàïðàâëåíèå ñòàíäàðòíûõ ïîòîêîâ ÂÂ
|
||||
python::object sys = python::import("sys");
|
||||
@ -33,6 +42,14 @@ void PykdExt::setUp()
|
||||
sys.attr("stdout") = python::ptr( dbgout );
|
||||
sys.attr("stderr") = python::ptr( dbgout );
|
||||
sys.attr("stdin") = python::ptr( dbgin );
|
||||
|
||||
|
||||
python::list pathList(sys.attr("path"));
|
||||
|
||||
python::ssize_t n = python::len(pathList);
|
||||
|
||||
for (python::ssize_t i = 0; i < n ; i++)
|
||||
m_paths.push_back(boost::python::extract<std::string>(pathList[i]));
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -55,52 +72,50 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
|
||||
startConsole();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
std::string scriptFileName = getScriptFileName( args[0] );
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void printException()
|
||||
{
|
||||
// îøèáêà â ñêðèïòå
|
||||
PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL;
|
||||
|
||||
PyErr_Fetch( &errtype, &errvalue, &traceback );
|
||||
|
||||
PyErr_NormalizeException( &errtype, &errvalue, &traceback );
|
||||
|
||||
if ( errtype == PyExc_SystemExit )
|
||||
if ( scriptFileName.empty() )
|
||||
{
|
||||
eprintln( L"script file not found" );
|
||||
return;
|
||||
}
|
||||
|
||||
python::object tracebackModule = python::import("traceback");
|
||||
// óñòàíàâèëèâàåì ïèòîíîâñêèå àðãóìåíòû
|
||||
char **pythonArgs = new char* [ args.size() ];
|
||||
|
||||
std::wstringstream sstr;
|
||||
for ( size_t i = 0; i < args.size(); ++i )
|
||||
pythonArgs[i] = const_cast<char*>( args[i].c_str() );
|
||||
|
||||
python::object lst =
|
||||
python::object( tracebackModule.attr("format_exception" ) )(
|
||||
python::handle<>( errtype ),
|
||||
python::handle<>( python::allow_null( errvalue ) ),
|
||||
python::handle<>( python::allow_null( traceback ) ) );
|
||||
PySys_SetArgv( (int)args.size(), pythonArgs );
|
||||
|
||||
sstr << std::endl << std::endl;
|
||||
delete[] pythonArgs;
|
||||
|
||||
for ( long i = 0; i < python::len(lst); ++i )
|
||||
sstr << std::wstring( python::extract<std::wstring>(lst[i]) ) << std::endl;
|
||||
// ïîëó÷àåì äîñòïó ê ãëîáàëüíîìó ìàïó ( íóæåí äëÿ âûçîâà exec_file )
|
||||
python::object main = python::import("__main__");
|
||||
|
||||
eprintln( sstr.str() );
|
||||
python::object global(main.attr("__dict__"));
|
||||
|
||||
try {
|
||||
python::exec_file( scriptFileName.c_str(), global );
|
||||
}
|
||||
catch( python::error_already_set const & )
|
||||
{
|
||||
printException();
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void PykdExt::startConsole()
|
||||
{
|
||||
|
||||
// ïîëó÷àåì äîñòïó ê ãëîáàëüíîìó ìàïó ( íóæåí äëÿ âûçîâà exec_file )
|
||||
python::object main = python::import("__main__");
|
||||
|
||||
python::object global(main.attr("__dict__"));
|
||||
|
||||
try {
|
||||
|
||||
// ïîëó÷àåì äîñòïó ê ãëîáàëüíîìó ìàïó ( íóæåí äëÿ âûçîâà exec_file )
|
||||
python::object main = python::import("__main__");
|
||||
|
||||
python::object global(main.attr("__dict__"));
|
||||
|
||||
python::exec( "__import__('code').InteractiveConsole(__import__('__main__').__dict__).interact()\n", global );
|
||||
}
|
||||
catch( python::error_already_set const & )
|
||||
@ -110,3 +125,56 @@ void PykdExt::startConsole()
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::string PykdExt::getScriptFileName( const std::string &scriptName )
|
||||
{
|
||||
bool fileHasPyExt = false;
|
||||
|
||||
if ( scriptName.length() > 3 )
|
||||
fileHasPyExt = scriptName.rfind(".py") == scriptName.length() - 3;
|
||||
|
||||
std::string fullFileName = scriptName;
|
||||
|
||||
if (!fileHasPyExt)
|
||||
fullFileName += ".py";
|
||||
|
||||
if ( GetFileAttributesA(fullFileName.c_str()) != INVALID_FILE_ATTRIBUTES )
|
||||
return fullFileName;
|
||||
|
||||
std::vector<std::string>::const_iterator it = m_paths.begin();
|
||||
for ( ; it != m_paths.end(); ++it)
|
||||
{
|
||||
DWORD bufSize = SearchPathA(
|
||||
(*it).c_str(),
|
||||
fullFileName.c_str(),
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
NULL);
|
||||
|
||||
if (bufSize > 0)
|
||||
{
|
||||
bufSize += 1;
|
||||
std::vector<char> fullFileNameCStr(bufSize);
|
||||
char *partFileNameCStr = NULL;
|
||||
|
||||
bufSize = SearchPathA(
|
||||
(*it).c_str(),
|
||||
fullFileName.c_str(),
|
||||
NULL,
|
||||
bufSize,
|
||||
&fullFileNameCStr[0],
|
||||
&partFileNameCStr);
|
||||
|
||||
if (bufSize > 0)
|
||||
{
|
||||
fullFileName = std::string(&fullFileNameCStr[0]);
|
||||
return fullFileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "kdlib/windbg.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@ -10,6 +12,8 @@ public:
|
||||
|
||||
KDLIB_EXT_COMMAND_METHOD(py);
|
||||
|
||||
static bool isInit();
|
||||
|
||||
private:
|
||||
|
||||
void startConsole();
|
||||
@ -18,6 +22,9 @@ private:
|
||||
|
||||
virtual void tearDown();
|
||||
|
||||
std::string getScriptFileName( const std::string &scriptName );
|
||||
|
||||
std::vector<std::string> m_paths;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user