From 041a58f6e047472b2cff808ce3bfee044db34f30 Mon Sep 17 00:00:00 2001 From: "SND\\ussrhero_cp" <SND\ussrhero_cp@9b283d60-5439-405e-af05-b73fd8c4d996> Date: Tue, 14 Apr 2015 20:46:17 +0000 Subject: [PATCH] [0.3.x] added : DbgOut.writedml method [0.3.x] added : targetSystem.getProcessById git-svn-id: https://pykd.svn.codeplex.com/svn@90380 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/pydbgio.h | 115 +++++++++++++++-------------------------------- pykd/pymod.cpp | 19 +++----- pykd/pyprocess.h | 12 +++++ 3 files changed, 54 insertions(+), 92 deletions(-) diff --git a/pykd/pydbgio.h b/pykd/pydbgio.h index d47f1f0..9874d76 100644 --- a/pykd/pydbgio.h +++ b/pykd/pydbgio.h @@ -6,6 +6,41 @@ namespace pykd { + +/////////////////////////////////////////////////////////////////////////////// + +inline void dprint( const std::wstring &str, bool dml = false ) +{ + python::object sys = python::import("sys"); + + if (dml && PyObject_HasAttrString(python::object(sys.attr("stdout")).ptr(), "writedml")) + sys.attr("stdout").attr("writedml")(str); + else + sys.attr("stdout").attr("write")( str ); +} + +/////////////////////////////////////////////////////////////////////////////// + +inline void dprintln( const std::wstring &str, bool dml = false ) +{ + pykd::dprint(str + L"\n", dml); +} + +/////////////////////////////////////////////////////////////////////////////// + +inline void eprint( const std::wstring &str ) +{ + python::object sys = python::import("sys"); + sys.attr("stderr").attr("write")(str); +} + +/////////////////////////////////////////////////////////////////////////////// + +inline void eprintln( const std::wstring &str ) +{ + pykd::eprint(str + L"\n"); +} + /////////////////////////////////////////////////////////////////////////////// class DbgOut : public kdlib::windbg::WindbgOut @@ -48,84 +83,4 @@ public: /////////////////////////////////////////////////////////////////////////////// -class SysDbgOut : public DbgOut -{ -public: - - SysDbgOut() { - m_state = PyThreadState_Get(); - } - - virtual void write( const std::wstring& str) { - AutoSavePythonState pystate( &m_state ); - python::object sys = python::import("sys"); - sys.attr("stdout").attr("write")( str ); - } - - virtual void writedml( const std::wstring& str) { - AutoSavePythonState pystate( &m_state ); - python::object sys = python::import("sys"); - sys.attr("stdout").attr("write")(str); - } - -private: - - PyThreadState* m_state; -}; - -/////////////////////////////////////////////////////////////////////////////// - -class SysDbgIn : public DbgIn -{ -public: - - SysDbgIn() { - m_state = PyThreadState_Get(); - } - - virtual std::wstring readline() { - AutoSavePythonState pystate( &m_state ); - python::object sys = python::import("sys"); - return python::extract<std::wstring>( sys.attr("stdin").attr("readline") ); - } - -private: - - PyThreadState* m_state; -}; - -/////////////////////////////////////////////////////////////////////////////// - -inline void dprint( const std::wstring &str, bool dml = false ) -{ - AutoRestorePyState pystate; - kdlib::dprint(str,dml); -} - -/////////////////////////////////////////////////////////////////////////////// - -inline void dprintln( const std::wstring &str, bool dml = false ) -{ - AutoRestorePyState pystate; - kdlib::dprintln(str,dml); -} - -/////////////////////////////////////////////////////////////////////////////// - -inline void eprint( const std::wstring &str ) -{ - AutoRestorePyState pystate; - kdlib::eprint(str); -} - -/////////////////////////////////////////////////////////////////////////////// - -inline void eprintln( const std::wstring &str ) -{ - AutoRestorePyState pystate; - kdlib::eprintln(str); -} - -/////////////////////////////////////////////////////////////////////////////// - } diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index 4b76516..c4dcd39 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -89,18 +89,8 @@ namespace pykd { void initialize() { - pykd::SysDbgOut *sysPykdOut = new pykd::SysDbgOut(); - pykd::SysDbgOut *sysPykdErr = new pykd::SysDbgOut(); - pykd::SysDbgIn *sysPykdIn = new pykd::SysDbgIn(); - AutoRestorePyState pystate; - kdlib::initialize(); - - // ������������ ������ ������� ������ �� sys - kdlib::dbgout = sysPykdOut; - kdlib::dbgerr = sysPykdErr; - kdlib::dbgin = sysPykdIn; } void remote_initialize( const std::wstring& remoteOptions ) @@ -204,6 +194,7 @@ BOOST_PYTHON_MODULE( pykd ) // Python debug output console helper classes python::class_<DbgOut>( "dout", "dout", python::no_init ) .def( "write", &DbgOut::write ) + .def( "writedml", &DbgOut::writedml) .def( "flush", &DbgOut::flush ) .add_property( "encoding", &DbgOut::encoding ); @@ -532,8 +523,10 @@ BOOST_PYTHON_MODULE( pykd ) "Check if the target is current") .def("getNumberProcesses", TargetSystemAdapter::getNumberProcesses, "Return processed number of the target system") - .def("process", TargetSystemAdapter::getProcessByIndex, + .def("getProcess", TargetSystemAdapter::getProcessByIndex, "Return process by index") + .def("getProcessById", TargetSystemAdapter::getProcessById, + "Return process by id") .def("currentProcess", TargetSystemAdapter::getCurrentProcess, "Return current process") ; @@ -559,8 +552,10 @@ BOOST_PYTHON_MODULE( pykd ) "Check if the target is current") .def("getNumberThreads", TargetProcessAdapter::getNumberThreads, "Return number of threads for this process" ) - .def("thread", TargetProcessAdapter::getThreadByIndex, + .def("getThread", TargetProcessAdapter::getThreadByIndex, "Return thread by its index" ) + .def("getThreadById", TargetProcessAdapter::getThreadById, + "Return thread by its index") .def("currentThread", TargetProcessAdapter::getCurrentThread, "Return current thread" ) .def("getNumberBreakpoints", TargetProcessAdapter::getNumberBreakpoints, diff --git a/pykd/pyprocess.h b/pykd/pyprocess.h index 46d71cf..783d47f 100644 --- a/pykd/pyprocess.h +++ b/pykd/pyprocess.h @@ -75,6 +75,12 @@ struct TargetSystemAdapter { return system.getProcessByIndex(index); } + static kdlib::TargetProcessPtr getProcessById(kdlib::TargetSystem& system, kdlib::PROCESS_DEBUG_ID id) + { + AutoRestorePyState pystate; + return system.getProcessById(id); + } + static kdlib::TargetProcessPtr getCurrentProcess(kdlib::TargetSystem& system) { AutoRestorePyState pystate; @@ -151,6 +157,12 @@ struct TargetProcessAdapter { return process.getThreadByIndex(index); } + static kdlib::TargetThreadPtr getThreadById(kdlib::TargetProcess& process, kdlib::THREAD_DEBUG_ID id) + { + AutoRestorePyState pystate; + return process.getThreadById(id); + } + static kdlib::TargetThreadPtr getCurrentThread(kdlib::TargetProcess& process) { AutoRestorePyState pystate;