Commit fabdd9de authored by ussrhero's avatar ussrhero

pykd_ext initial commit into new repo

parents

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2042
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E1809804-6329-494A-94AA-76A090F15973}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pykd_ext_2.0", "sources\pykd_ext.vcxproj", "{583F9A6C-AF6D-45E0-A8F4-290D93611185}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Debug|x64.ActiveCfg = Debug|x64
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Debug|x64.Build.0 = Debug|x64
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Debug|x86.ActiveCfg = Debug|Win32
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Debug|x86.Build.0 = Debug|Win32
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Release|x64.ActiveCfg = Release|x64
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Release|x64.Build.0 = Release|x64
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Release|x86.ActiveCfg = Release|Win32
{583F9A6C-AF6D-45E0-A8F4-290D93611185}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BB208C6D-5FA8-45D3-B3DB-F800AC8AE104}
EndGlobalSection
EndGlobal
#include "stdafx.h"
#include <boost/tokenizer.hpp>
#include <regex>
#include <list>
#include "arglist.h"
namespace {
typedef boost::escaped_list_separator<char> char_separator_t;
typedef boost::tokenizer< char_separator_t > char_tokenizer_t;
ArgsList getArgsList(const std::string& argsStr)
{
char_tokenizer_t token(argsStr, char_separator_t("", " \t", "\""));
ArgsList argsList;
for (char_tokenizer_t::iterator it = token.begin(); it != token.end(); ++it)
{
if (*it != "")
argsList.push_back(*it);
}
return argsList;
}
} // anonymous namespace
static const std::regex versionRe("^-([2,3])(?:\\.(\\d+))?$");
Options::Options(const std::string& cmdline) :
pyMajorVersion(-1),
pyMinorVersion(-1),
global(false),
showHelp(false),
runModule(false)
{
args = getArgsList( cmdline );
if ( args.empty() )
{
global = true;
}
for (auto it = args.begin(); it != args.end();)
{
if (*it == "--global" || *it == "-g")
{
global = true;
it = args.erase(it);
continue;
}
if (*it == "--local" || *it == "-l")
{
global = false;
it = args.erase(it);
continue;
}
if (*it == "--help" || *it == "-h")
{
showHelp = true;
it = args.erase(it);
continue;
}
if (*it == "--module" || *it == "-m")
{
runModule = true;
it = args.erase(it);
continue;
}
std::smatch mres;
if (std::regex_match(*it, mres, versionRe))
{
pyMajorVersion = atol(std::string(mres[1].first, mres[1].second).c_str());
if (mres[2].matched)
{
pyMinorVersion = atol(std::string(mres[2].first, mres[2].second).c_str());
}
it = args.erase(it);
continue;
}
break;
}
}
#pragma once
#include <vector>
#include <string>
typedef std::vector< std::string > ArgsList;
struct Options
{
int pyMajorVersion;
int pyMinorVersion;
bool global;
bool showHelp;
bool runModule;
std::vector<std::string> args;
Options() :
pyMajorVersion(-1),
pyMinorVersion(-1),
global(true),
showHelp(false),
runModule(false)
{}
Options(const std::string& cmdline);
};
#pragma once
#include <DbgEng.h>
#include <atlbase.h>
#include <comutil.h>
#include <string>
#include <vector>
#include "pycontext.h"
#include "pyclass.h"
//////////////////////////////////////////////////////////////////////////////
class DbgOut
{
public:
DbgOut(PDEBUG_CLIENT client) :
m_control(client)
{}
void write(const std::wstring& str)
{
AutoRestorePyState pystate;
m_control->ControlledOutputWide(
DEBUG_OUTCTL_AMBIENT_TEXT, //DEBUG_OUTCTL_THIS_CLIENT,
DEBUG_OUTPUT_NORMAL,
L"%ws",
str.c_str()
);
}
void writedml(const std::wstring& str)
{
AutoRestorePyState pystate;
m_control->ControlledOutputWide(
DEBUG_OUTCTL_AMBIENT_DML, //DEBUG_OUTCTL_THIS_CLIENT | DEBUG_OUTCTL_DML,
DEBUG_OUTPUT_NORMAL,
L"%ws",
str.c_str()
);
}
void flush() {
}
std::wstring encoding() {
return L"ascii";
}
bool closed() {
return false;
}
bool isatty() {
return false;
}
public:
BEGIN_PYTHON_METHOD_MAP(DbgOut, "dbgout")
PYTHON_METHOD1("write", write, "write");
PYTHON_METHOD1("writedml", writedml, "writedml");
PYTHON_METHOD0("flush", flush, "flush");
PYTHON_PROPERTY("encoding", encoding, "encoding");
PYTHON_PROPERTY("closed", closed, "closed");
PYTHON_METHOD0("isatty", isatty, "isatty");
END_PYTHON_METHOD_MAP
private:
CComQIPtr<IDebugControl4> m_control;
};
///////////////////////////////////////////////////////////////////////////////
class DbgIn
{
public:
DbgIn(PDEBUG_CLIENT client) :
m_control(client)
{}
std::wstring readline()
{
AutoRestorePyState pystate;
std::vector<wchar_t> inputBuffer(0x10000);
ULONG read = 0;
m_control->InputWide(&inputBuffer[0], static_cast<ULONG>(inputBuffer.size()), &read);
std::wstring inputstr = std::wstring(&inputBuffer[0]);
return inputstr.empty() ? L"\n" : inputstr;
}
bool closed() {
return false;
}
public:
BEGIN_PYTHON_METHOD_MAP(DbgIn, "dbgin")
PYTHON_METHOD0("readline", readline, "readline");
PYTHON_PROPERTY("closed", closed, "closed");
END_PYTHON_METHOD_MAP
private:
CComQIPtr<IDebugControl4> m_control;
};
//////////////////////////////////////////////////////////////////////////////
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
static HMODULE pinHandle = NULL;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (!pinHandle)
{
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
(LPCTSTR)hModule,
&pinHandle);
}
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
EXPORTS
DebugExtensionInitialize
DebugExtensionUninitialize
py
info
pip
help
select = selectVersion
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="boost" version="1.67.0.0" targetFramework="native" />
</packages>
\ No newline at end of file
#pragma once
typedef void* PyObject;
typedef void* PyThreadState;
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
typedef enum { PyGILState_LOCKED, PyGILState_UNLOCKED } PyGILState_STATE;
const int Py_single_input = 256;
const int Py_file_input = 257;
const int Py_eval_input = 258;
const int METH_VARARGS = 0x0001;
struct PyMethodDef {
const char *ml_name; /* The name of the built-in function/method */
PyCFunction ml_meth; /* The C function that implements it */
int ml_flags; /* Combination of METH_xxx flags, which mostly
describe the args expected by the C func */
const char *ml_doc; /* The __doc__ attribute, or NULL */
};
typedef struct PyMethodDef PyMethodDef;
void __stdcall Py_IncRef(PyObject* object);
void __stdcall Py_DecRef(PyObject* object);
PyObject* __stdcall PyString_FromString(const char *v);
char* PyString_AsString(PyObject *string);
int PyString_Check(PyObject *o);
PyObject* PyImport_Import(PyObject *name);
PyObject* __stdcall PyImport_ImportModule(const char *name);
PyObject* __stdcall PyImport_AddModule(const char *name);
PyObject* __stdcall PyDict_New();
int __stdcall PyDict_SetItemString(PyObject *p, const char *key, PyObject *val);
PyObject* __stdcall PyTuple_New(size_t len);
PyObject* __stdcall PyTuple_GetItem(PyObject *p, size_t pos);
int __stdcall PyTuple_SetItem(PyObject *p, size_t pos, PyObject *obj);
PyObject* __stdcall PyDict_GetItemString(PyObject *p, const char *key);
void __stdcall PyDict_Clear(PyObject *p);
size_t __stdcall PyTuple_Size(PyObject *p);
size_t PyList_Size(PyObject* list);
PyObject* PyList_GetItem(PyObject *list, size_t index);
PyObject* __stdcall PyCFunction_NewEx(PyMethodDef *, PyObject *, PyObject *);
PyObject* __stdcall PyClass_New(PyObject* className, PyObject* classBases, PyObject* classDict);
PyObject* __stdcall PyMethod_New(PyObject *func, PyObject *self, PyObject *classobj);
PyObject* __stdcall PyInstance_New(PyObject *classobj, PyObject *arg, PyObject *kw);
PyThreadState* __stdcall PyEval_SaveThread();
void __stdcall PyEval_RestoreThread(PyThreadState *tstate);
int __stdcall PySys_SetObject(char *name, PyObject *v);
PyObject* __stdcall PySys_GetObject(char *name);
void __stdcall PySys_SetArgv(int argc, char **argv);
void __stdcall PySys_SetArgv_Py3(int argc, wchar_t **argv);
int __stdcall PyRun_SimpleString(const char* str);
PyObject* __stdcall PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals);
PyObject* PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals);
typedef void(*PyCapsule_Destructor)(PyObject *);
PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor);
void* PyCapsule_GetPointer(PyObject *capsule, const char *name);
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args);
PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw);
PyObject* PyUnicode_FromWideChar(const wchar_t *w, size_t size);
int PyUnicode_Check(PyObject *o);
PyObject* PyBool_FromLong(long v);
PyObject* Py_None();
PyObject* PyExc_SystemExit();
PyObject* PyExc_TypeError();
PyObject* PyType_Type();
PyObject* PyProperty_Type();
void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback);
void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb);
void PyErr_SetString(PyObject *type, const char *message);
void PyErr_Clear();
PyObject* PyFile_FromString(char *filename, char *mode);
FILE* PyFile_AsFile(PyObject *pyfile);
FILE* _Py_fopen(const char* filename, const char* mode);
PyObject* __stdcall PyUnicode_FromString(const char* str);
PyObject* __stdcall PyInstanceMethod_New(PyObject *func);
size_t __stdcall PyUnicode_AsWideChar(PyObject *unicode, wchar_t *w, size_t size);
int __stdcall Py_AddPendingCall(int(*func)(void *), void *arg);
PyGILState_STATE __stdcall PyGILState_Ensure();
void __stdcall PyGILState_Release(PyGILState_STATE);
PyObject* __stdcall PyDescr_NewMethod(PyObject* type, struct PyMethodDef *meth);
size_t __stdcall PyGC_Collect(void);
bool IsPy3();
class PyObjectRef;
class PyObjectBorrowedRef
{
friend PyObjectRef;
public:
PyObjectBorrowedRef(PyObject* obj)
{
m_obj = obj;
if (m_obj)
Py_IncRef(m_obj);
}
~PyObjectBorrowedRef()
{
if (m_obj)
Py_DecRef(m_obj);
}
operator PyObject*()
{
return m_obj;
}
private:
PyObjectBorrowedRef(const PyObjectRef& obj) = delete;
PyObject* m_obj;
};
class PyObjectRef
{
public:
PyObjectRef() : m_obj(0)
{}
PyObjectRef(PyObject* obj)
{
m_obj = obj;
}
~PyObjectRef()
{
if (m_obj)
Py_DecRef(m_obj);
}
operator PyObject*()
{
return m_obj;
}
PyObjectRef& operator= (const PyObjectRef& ref)
{
if (m_obj)
Py_DecRef(m_obj);
m_obj = ref.m_obj;
if (m_obj)
Py_IncRef(m_obj);
return *this;
}
PyObjectRef& operator= (const PyObjectBorrowedRef& ref)
{
if (m_obj)
Py_DecRef(m_obj);
m_obj = ref.m_obj;
if (m_obj)
Py_IncRef(m_obj);
return *this;
}
PyObjectRef& operator= (PyObject* obj)
{
if (m_obj)
Py_DecRef(m_obj);
m_obj = obj;
return *this;
}
private:
PyObjectRef(const PyObjectRef& obj) = delete;
PyObject* m_obj;
};
#pragma once
#include "pyapi.h"
#include <comutil.h>
#include <string>
#include <vector>
class convert_python_exception : public std::exception
{
public:
convert_python_exception(const char* message) : std::exception(message)
{}
};
struct convert_from_python
{
convert_from_python(PyObject* obj) : m_obj(obj){}
operator std::wstring()
{
if ( PyUnicode_Check(m_obj) )
{
std::vector<wchar_t> buf(0x10000);
size_t len = buf.size();
len = PyUnicode_AsWideChar(m_obj, &buf[0], len);
return std::wstring(&buf[0], len);
}
if ( !IsPy3() && PyString_Check(m_obj) )
{
return std::wstring(_bstr_t(PyString_AsString(m_obj)));
}
throw convert_python_exception("failed convert argument");
}
operator std::string()
{
if (PyUnicode_Check(m_obj))
{
std::vector<wchar_t> buf(0x10000);
size_t len = buf.size();
len = PyUnicode_AsWideChar(m_obj, &buf[0], len);
std::wstring str(&buf[0], len);
return std::string(_bstr_t(str.c_str()));
}
if ( !IsPy3() && PyString_Check(m_obj) )
{
return std::string(PyString_AsString(m_obj));
}
throw convert_python_exception("failed convert argument");
}
PyObject* m_obj;
};
struct convert_to_python
{
operator PyObject* (){
return m_obj;
}
convert_to_python()
{
}
convert_to_python(const std::wstring& str)
{
m_obj = PyUnicode_FromWideChar(str.c_str(), str.size());
}
convert_to_python(bool v)
{
m_obj = PyBool_FromLong(v == true ? 1 : 0);
}
PyObject* m_obj;
};
#define BEGIN_PYTHON_METHOD_MAP(classType, className) \
template <typename TRet> \
PyObject* callMethod0( \
TRet (classType::*method)()) \
{ \
TRet r = (this->*method)(); \
return convert_to_python(r); \
} \
PyObject* callMethod0(\
void (classType::*method)()) \
{ \
(this->*method)(); \
Py_IncRef(Py_None()); \
return Py_None(); \
} \
template <typename TRet, typename V1> \