Skip to content

Commit

Permalink
Updates for Python 3 and VS 2019
Browse files Browse the repository at this point in the history
  • Loading branch information
dshikashio committed Aug 29, 2021
1 parent 875fbe8 commit 8036acd
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 336 deletions.
105 changes: 105 additions & 0 deletions Pywindbg.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{6bd2e13c-69f8-4f42-bad5-6c94ef74c7a7}</ProjectGuid>
<RootNamespace>Pywindbg</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Program Files %28x86%29\Python39-32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>pyext.def</ModuleDefinitionFile>
<AdditionalDependencies>C:\Program Files (x86)\Windows Kits\10\Debuggers\lib\x86\dbgeng.lib;C:\Program Files (x86)\Python39-32\libs\python39.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Program Files\Python39\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ModuleDefinitionFile>pyext.def</ModuleDefinitionFile>
<AdditionalDependencies>C:\Program Files (x86)\Windows Kits\10\Debuggers\lib\x64\dbgeng.lib;C:\Program Files\Python39\libs\python39.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="pyembed.cpp" />
<ClCompile Include="pyext.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pyembed.h" />
<ClInclude Include="pyext.h" />
</ItemGroup>
<ItemGroup>
<None Include="pyext.def" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
38 changes: 38 additions & 0 deletions Pywindbg.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pyembed.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pyext.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pyembed.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pyext.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="pyext.def">
<Filter>Source Files</Filter>
</None>
</ItemGroup>
</Project>
107 changes: 65 additions & 42 deletions pyembed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
//
// Nice to Haves
// - pydbgeng and pywindbg available by default
// - ability to actually unload !pyext.unload and recompile...
//
// Might be useful
// - Wrap the "old" apis as python calls

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "pyembed.h"
Expand Down Expand Up @@ -37,6 +38,9 @@ IoBridge_write(IoBridgeObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "s", &data))
return NULL;

if (data == NULL)
return NULL;

if (gPyDebugControl) {
Py_BEGIN_ALLOW_THREADS
char *p = data;
Expand Down Expand Up @@ -129,16 +133,15 @@ static PyMemberDef IoBridge_members[] = {
};

static PyTypeObject IoBridgeType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
PyVarObject_HEAD_INIT(NULL, 0)
"extio.IoBridge", /*tp_name*/
sizeof(IoBridgeObject), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_reserved*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
Expand Down Expand Up @@ -178,22 +181,42 @@ ExtIo_ExitNop(PyObject *ignore, PyObject *arg)

static PyMethodDef ExtIoMethods[] = {
{"exit_nop", (PyCFunction)ExtIo_ExitNop, METH_VARARGS,
"Type exit() or quit() to return back to Windbg."}
"Type exit() or quit() to return back to Windbg."},
{NULL, NULL, 0, NULL}
};

HRESULT initextio(void)
static struct PyModuleDef extiomodule =
{
PyModuleDef_HEAD_INIT,
"extio",
"extio module",
-1,
ExtIoMethods,
NULL, NULL, NULL, NULL
};

//HRESULT
PyMODINIT_FUNC PyInit_extio(void)
{
PyObject *m;

IoBridgeType.tp_new = PyType_GenericNew;
if (PyType_Ready(&IoBridgeType) < 0)
return E_FAIL;
return NULL;

m = PyModule_Create(&extiomodule);
if (m == NULL) {
return NULL;
}

m = Py_InitModule("extio", ExtIoMethods);
Py_INCREF(&IoBridgeType);
PyModule_AddObject(m, "IoBridge", (PyObject *)&IoBridgeType);
if (PyModule_AddObject(m, "IoBridge", (PyObject*)&IoBridgeType) != 0) {
Py_DECREF(&IoBridgeType);
Py_DECREF(m);
return NULL;
}

return S_OK;
return m;
}

HRESULT python_init(void)
Expand All @@ -205,13 +228,17 @@ HRESULT python_init(void)
PyObject *ExitNop = NULL;
PyObject *BuiltIns = NULL;

PyEval_InitThreads();
if (PyImport_AppendInittab("extio", PyInit_extio) == -1)
return E_FAIL;

Py_Initialize();
//PyEval_InitThreads();

if ((hr = initextio()) != S_OK)
return hr;
hr = E_FAIL;

if (PyRun_SimpleString("import sys") != 0)
return E_FAIL;

module = PyImport_ImportModule("extio");
if (module == NULL)
goto fail;
Expand All @@ -224,7 +251,7 @@ HRESULT python_init(void)
if (IoBridge == NULL)
goto fail;

OBInst = (IoBridgeObject *)PyObject_CallObject(IoBridge, NULL);
OBInst = (IoBridgeObject *)PyObject_CallObject((PyObject *)&IoBridgeType, NULL);
if (OBInst == NULL)
goto fail;

Expand Down Expand Up @@ -265,50 +292,43 @@ void python_fini(void)
HRESULT CALLBACK
pyeval(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
PyObject *m;
PyObject *d;
PyObject *v = NULL;
PSTR cmd = NULL;
size_t cmdlen = 0;

ENTER_CALLBACK(Client);

cmdlen = lstrlenA(args + 2);
cmd = (PSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cmdlen);
if (cmd == NULL)
goto done;

lstrcpyA(cmd, args);
lstrcatA(cmd, "\n");

if ((m = PyImport_AddModule("__main__")) == NULL)
goto done;

d = PyModule_GetDict(m);
if ((v = PyRun_StringFlags(cmd, Py_single_input, d, d, NULL)) == NULL) {
if (PyRun_SimpleString(cmd) == -1) {
PyErr_Print();
goto done;
}
if (Py_FlushLine())
PyErr_Clear();

done:
gDebugControl->Output(DEBUG_OUTPUT_NORMAL, "\n");
gPyDebugControl->Output(DEBUG_OUTPUT_NORMAL, "\n");
if (cmd) HeapFree(GetProcessHeap(), 0, cmd);
Py_XDECREF(v);
LEAVE_CALLBACK();
return S_OK;
}

HRESULT CALLBACK
pyexec(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
PyObject *file = NULL;
FILE* file = NULL;
PyObject *m;
PyObject *d;
PyObject *v = NULL;
PyObject* v = NULL;

ENTER_CALLBACK(Client);
Client->SetOutputWidth(100);

file = PyFile_FromString((char *)args, "r");
file = fopen(args, "rb");
if (file == NULL) {
gDebugControl->Output(DEBUG_OUTPUT_ERROR, "Error opening '%s'", args);
goto done;
Expand All @@ -318,15 +338,19 @@ pyexec(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
goto done;

d = PyModule_GetDict(m);
if ((v = PyRun_File(PyFile_AsFile(file), args, Py_file_input, d, d)) == NULL) {

if ((v = PyRun_File(file, args, Py_file_input, d, d)) == NULL) {
PyErr_Print();
goto done;
}
if (Py_FlushLine())
PyErr_Clear();

done:
if (file != NULL)
{
fclose(file);
}
Py_XDECREF(v);
Py_XDECREF(file);
// Do we need to decref d or m?
LEAVE_CALLBACK();
return S_OK;
}
Expand All @@ -337,10 +361,11 @@ python(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
PyObject *m;
PyObject *d;
PyObject *v = NULL;
PyObject *v1 = NULL;
PyObject* v2 = NULL;
ULONG Mask;

char *cmds[] = {
const char *cmds[] = {
"import code",

"code.interact(banner=\""
Expand All @@ -359,24 +384,22 @@ python(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
goto done;

d = PyModule_GetDict(m);
if ((v = PyRun_StringFlags(cmds[0], Py_single_input, d, d, NULL)) == NULL) {
if ((v1 = PyRun_StringFlags(cmds[0], Py_single_input, d, d, NULL)) == NULL) {
PyErr_Print();
goto done;
}
if ((v = PyRun_StringFlags(cmds[1], Py_single_input, d, d, NULL)) == NULL) {
if ((v2 = PyRun_StringFlags(cmds[1], Py_single_input, d, d, NULL)) == NULL) {
PyErr_Print();
goto done;
}
if (Py_FlushLine())
PyErr_Clear();


done:
Mask |= DEBUG_OUTPUT_PROMPT;
Client->SetOutputMask(Mask);

gDebugControl->Output(DEBUG_OUTPUT_NORMAL, "Leaving Python\n");
Py_XDECREF(v);
Py_XDECREF(v1);
Py_XDECREF(v2);
LEAVE_CALLBACK();
return S_OK;
}

Loading

0 comments on commit 8036acd

Please sign in to comment.