mirror of
https://github.com/ivellioscolin/pykd.git
synced 2025-04-20 11:43:23 +08:00
113 lines
2.8 KiB
C++
113 lines
2.8 KiB
C++
#include "stdafx.h"
|
|
|
|
#include "kdlib/kdlib.h"
|
|
#include "kdlib/windbg.h"
|
|
|
|
#include "windbgext.h"
|
|
|
|
using namespace kdlib;
|
|
using namespace kdlib::windbg;
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
KDLIB_WINDBG_EXTENSION_INIT( PykdExt );
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
extern "C" void initpykd();
|
|
|
|
void PykdExt::setUp()
|
|
{
|
|
WindbgExtension::setUp();
|
|
|
|
PyImport_AppendInittab("pykd", initpykd );
|
|
|
|
Py_Initialize();
|
|
|
|
boost::python::import( "pykd" );
|
|
|
|
// ïåðåíàïðàâëåíèå ñòàíäàðòíûõ ïîòîêîâ ÂÂ
|
|
python::object sys = python::import("sys");
|
|
|
|
sys.attr("stdout") = python::ptr( dbgout );
|
|
sys.attr("stderr") = python::ptr( dbgout );
|
|
sys.attr("stdin") = python::ptr( dbgin );
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void PykdExt::tearDown()
|
|
{
|
|
Py_Finalize();
|
|
|
|
WindbgExtension::tearDown();
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
|
|
{
|
|
const ArgsList& args = getArgs();
|
|
|
|
if ( args.size() == 0 )
|
|
{
|
|
startConsole();
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void printException()
|
|
{
|
|
// îøèáêà â ñêðèïòå
|
|
PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL;
|
|
|
|
PyErr_Fetch( &errtype, &errvalue, &traceback );
|
|
|
|
PyErr_NormalizeException( &errtype, &errvalue, &traceback );
|
|
|
|
if ( errtype == PyExc_SystemExit )
|
|
return;
|
|
|
|
python::object tracebackModule = python::import("traceback");
|
|
|
|
std::wstringstream sstr;
|
|
|
|
python::object lst =
|
|
python::object( tracebackModule.attr("format_exception" ) )(
|
|
python::handle<>( errtype ),
|
|
python::handle<>( python::allow_null( errvalue ) ),
|
|
python::handle<>( python::allow_null( traceback ) ) );
|
|
|
|
sstr << std::endl << std::endl;
|
|
|
|
for ( long i = 0; i < python::len(lst); ++i )
|
|
sstr << std::wstring( python::extract<std::wstring>(lst[i]) ) << std::endl;
|
|
|
|
eprintln( sstr.str() );
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void PykdExt::startConsole()
|
|
{
|
|
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 & )
|
|
{
|
|
printException();
|
|
}
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|