mirror of
https://github.com/ivellioscolin/pykd.git
synced 2025-04-21 04:13:22 +08:00
[pykd_ext_2.0] added : pykd auto load
git-svn-id: https://pykd.svn.codeplex.com/svn@90990 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
parent
c53436ffe5
commit
ceba5b7857
@ -9,13 +9,13 @@ BOOL APIENTRY DllMain( HMODULE hModule,
|
|||||||
LPVOID lpReserved
|
LPVOID lpReserved
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
//if (!pinHandle)
|
if (!pinHandle)
|
||||||
//{
|
{
|
||||||
// GetModuleHandleEx(
|
GetModuleHandleEx(
|
||||||
// GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
|
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
|
||||||
// (LPCTSTR)hModule,
|
(LPCTSTR)hModule,
|
||||||
// &pinHandle);
|
&pinHandle);
|
||||||
//}
|
}
|
||||||
|
|
||||||
switch (ul_reason_for_call)
|
switch (ul_reason_for_call)
|
||||||
{
|
{
|
||||||
|
@ -52,6 +52,9 @@ public:
|
|||||||
|
|
||||||
bool isPy3;
|
bool isPy3;
|
||||||
|
|
||||||
|
void checkPykd();
|
||||||
|
void deactivate();
|
||||||
|
|
||||||
PyObject* PyType_Type;
|
PyObject* PyType_Type;
|
||||||
PyObject* PyProperty_Type;
|
PyObject* PyProperty_Type;
|
||||||
PyObject* Py_None;
|
PyObject* Py_None;
|
||||||
@ -121,8 +124,11 @@ public:
|
|||||||
HMODULE m_handlePython;
|
HMODULE m_handlePython;
|
||||||
PyThreadState* m_globalState;
|
PyThreadState* m_globalState;
|
||||||
PythonInterpreter* m_globalInterpreter;
|
PythonInterpreter* m_globalInterpreter;
|
||||||
|
bool m_pykdInit;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PythonInterpreter
|
class PythonInterpreter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -227,12 +233,19 @@ public:
|
|||||||
return m_modules.find(std::make_pair(majorVersion, minorVersion)) != m_modules.end();
|
return m_modules.find(std::make_pair(majorVersion, minorVersion)) != m_modules.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void checkPykd()
|
||||||
|
{
|
||||||
|
m_currentInterpter->m_module->checkPykd();
|
||||||
|
}
|
||||||
|
|
||||||
void stopAllInterpreter()
|
void stopAllInterpreter()
|
||||||
{
|
{
|
||||||
for (auto m : m_modules)
|
for (auto m : m_modules)
|
||||||
delete m.second;
|
{
|
||||||
|
m_currentInterpter = m.second->m_globalInterpreter;
|
||||||
m_modules.clear();
|
m.second->deactivate();
|
||||||
|
}
|
||||||
|
m_currentInterpter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -404,23 +417,80 @@ PyModule::PyModule(int majorVesion, int minorVersion)
|
|||||||
*reinterpret_cast<FARPROC*>(&PyGILState_Release) = GetProcAddress(m_handlePython, "PyGILState_Release");
|
*reinterpret_cast<FARPROC*>(&PyGILState_Release) = GetProcAddress(m_handlePython, "PyGILState_Release");
|
||||||
*reinterpret_cast<FARPROC*>(&PyDescr_NewMethod) = GetProcAddress(m_handlePython, "PyDescr_NewMethod");
|
*reinterpret_cast<FARPROC*>(&PyDescr_NewMethod) = GetProcAddress(m_handlePython, "PyDescr_NewMethod");
|
||||||
|
|
||||||
|
|
||||||
Py_Initialize();
|
Py_Initialize();
|
||||||
PyEval_InitThreads();
|
PyEval_InitThreads();
|
||||||
|
|
||||||
m_globalState = PyEval_SaveThread();
|
m_globalState = PyEval_SaveThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PyModule::~PyModule()
|
PyModule::~PyModule()
|
||||||
{
|
{
|
||||||
PyEval_RestoreThread(m_globalState);
|
assert(0);
|
||||||
|
|
||||||
Py_Finalize();
|
//if (m_globalInterpreter)
|
||||||
|
//{
|
||||||
|
// delete m_globalInterpreter;
|
||||||
|
// m_globalInterpreter = 0;
|
||||||
|
//}
|
||||||
|
|
||||||
FreeLibrary(m_handlePython);
|
//PyThreadState_Swap(m_globalState);
|
||||||
|
|
||||||
|
//Py_Finalize();
|
||||||
|
|
||||||
|
//FreeLibrary(m_handlePython);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void PyModule::deactivate()
|
||||||
|
{
|
||||||
|
if (m_globalInterpreter)
|
||||||
|
{
|
||||||
|
delete m_globalInterpreter;
|
||||||
|
m_globalInterpreter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyThreadState_Swap(m_globalState);
|
||||||
|
|
||||||
|
if (m_pykdInit)
|
||||||
|
{
|
||||||
|
PyObject* mainName = isPy3 ? PyUnicode_FromString("__main__") : PyString_FromString("__main__");
|
||||||
|
PyObject* mainMod = PyImport_Import(mainName);
|
||||||
|
PyObject* globals = PyObject_GetAttrString(mainMod, "__dict__");
|
||||||
|
PyObject* result = PyRun_String("__import__('pykd').deinitialize()\n", Py_file_input, globals, globals);
|
||||||
|
|
||||||
|
if (mainName) Py_DecRef(mainName);
|
||||||
|
if (mainMod) Py_DecRef(mainMod);
|
||||||
|
if (globals) Py_DecRef(globals);
|
||||||
|
if (result) Py_DecRef(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_globalState = PyEval_SaveThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PyModule::checkPykd()
|
||||||
|
{
|
||||||
|
PyObject* pykdMod = PyImport_ImportModule("pykd");
|
||||||
|
|
||||||
|
if (!pykdMod)
|
||||||
|
throw std::exception("Pykd package is not installed.You can install it by command \"!pip install pykd\"");
|
||||||
|
|
||||||
|
PyObject* mainName = isPy3 ? PyUnicode_FromString("__main__") : PyString_FromString("__main__");
|
||||||
|
PyObject* mainMod = PyImport_Import(mainName);
|
||||||
|
PyObject* globals = PyObject_GetAttrString(mainMod, "__dict__");
|
||||||
|
PyObject* result = PyRun_String("__import__('pykd').initialize()\n", Py_file_input, globals, globals);
|
||||||
|
|
||||||
|
if (mainName) Py_DecRef(mainName);
|
||||||
|
if (mainMod) Py_DecRef(mainMod);
|
||||||
|
if (globals) Py_DecRef(globals);
|
||||||
|
if (result) Py_DecRef(result);
|
||||||
|
if (pykdMod) Py_DecRef(pykdMod);
|
||||||
|
|
||||||
|
m_pykdInit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
PythonInterpreter* activateInterpreter(bool global, int majorVersion, int minorVersion)
|
PythonInterpreter* activateInterpreter(bool global, int majorVersion, int minorVersion)
|
||||||
{
|
{
|
||||||
return PythonSingleton::get()->getInterpreter(majorVersion, minorVersion, global);
|
return PythonSingleton::get()->getInterpreter(majorVersion, minorVersion, global);
|
||||||
@ -441,6 +511,11 @@ void stopAllInterpreter()
|
|||||||
PythonSingleton::get()->stopAllInterpreter();
|
PythonSingleton::get()->stopAllInterpreter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void checkPykd()
|
||||||
|
{
|
||||||
|
PythonSingleton::get()->checkPykd();
|
||||||
|
}
|
||||||
|
|
||||||
void __stdcall Py_IncRef(PyObject* object)
|
void __stdcall Py_IncRef(PyObject* object)
|
||||||
{
|
{
|
||||||
PythonSingleton::get()->currentInterpreter()->m_module->Py_IncRef(object);
|
PythonSingleton::get()->currentInterpreter()->m_module->Py_IncRef(object);
|
||||||
|
@ -24,6 +24,8 @@ bool isInterpreterLoaded(int majorVersion, int minorVersion);
|
|||||||
|
|
||||||
void stopAllInterpreter();
|
void stopAllInterpreter();
|
||||||
|
|
||||||
|
void checkPykd();
|
||||||
|
|
||||||
|
|
||||||
class AutoInterpreter
|
class AutoInterpreter
|
||||||
{
|
{
|
||||||
|
@ -100,7 +100,7 @@ VOID
|
|||||||
CALLBACK
|
CALLBACK
|
||||||
DebugExtensionUninitialize()
|
DebugExtensionUninitialize()
|
||||||
{
|
{
|
||||||
// stopAllInterpreter();
|
stopAllInterpreter();
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
@ -200,6 +200,13 @@ help(
|
|||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
static const char consoleScript[] =
|
||||||
|
"import pykd\n"
|
||||||
|
"from pykd import *\n"
|
||||||
|
"import code\n"
|
||||||
|
"code.InteractiveConsole(globals()).interact()\n";
|
||||||
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
HRESULT
|
HRESULT
|
||||||
CALLBACK
|
CALLBACK
|
||||||
@ -239,11 +246,13 @@ py(
|
|||||||
PyObjectRef mainMod = PyImport_Import(mainName);
|
PyObjectRef mainMod = PyImport_Import(mainName);
|
||||||
PyObjectRef globals = PyObject_GetAttrString(mainMod, "__dict__");
|
PyObjectRef globals = PyObject_GetAttrString(mainMod, "__dict__");
|
||||||
|
|
||||||
|
checkPykd();
|
||||||
|
|
||||||
InterruptWatch interruptWatch(client);
|
InterruptWatch interruptWatch(client);
|
||||||
|
|
||||||
if (opts.args.empty())
|
if (opts.args.empty())
|
||||||
{
|
{
|
||||||
PyObjectRef result = PyRun_String("__import__('code').InteractiveConsole(__import__('__main__').__dict__).interact()\n", Py_file_input, globals, globals);
|
PyObjectRef result = PyRun_String(consoleScript, Py_file_input, globals, globals);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user