forked from dshikashio/pywindbg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyembed.cpp
More file actions
382 lines (327 loc) · 9.87 KB
/
Copy pathpyembed.cpp
File metadata and controls
382 lines (327 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// To Do List
//
// 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
#include <Python.h>
#include <structmember.h>
#include "pyembed.h"
typedef struct _IoBridgeObject {
PyObject_HEAD
int width;
} IoBridgeObject;
#define CONSOLE_WIDTH 85
static int
find_line_width(char *hay, int maxwidth, char needle)
{
int i;
for (i = 0; i < maxwidth; i++) {
if (hay[i] == needle)
return i;
}
return i;
}
static PyObject *
IoBridge_write(IoBridgeObject *self, PyObject *args)
{
char *data = NULL;
if (!PyArg_ParseTuple(args, "s", &data))
return NULL;
if (gPyDebugControl) {
Py_BEGIN_ALLOW_THREADS
char *p = data;
// Windbg is 'dumb' and doesn't auto wrap lines so control
// that with width. But sometimes there is a newline already...
if (self->width) {
char tmp;
while (lstrlenA(p) > self->width) {
int width = find_line_width(p, self->width, '\n');
if (width == 0)
width = 1;
tmp = p[width];
p[width] = '\0';
gPyDebugControl->Output(DEBUG_OUTCTL_ALL_CLIENTS, "%s", p);
p += width;
*p = tmp;
if (width >= self->width)
gPyDebugControl->Output(DEBUG_OUTCTL_ALL_CLIENTS, "\n");
}
}
// Most of the time we don't need a newline here, but sometimes we do
gPyDebugControl->Output(DEBUG_OUTCTL_ALL_CLIENTS, "%s", p);
if (lstrcmpA(">>> ", p) != 0 && p[lstrlenA(p)-1] != '\n')
gPyDebugControl->Output(DEBUG_OUTCTL_ALL_CLIENTS, "\n");
Py_END_ALLOW_THREADS
}
Py_RETURN_NONE;
}
static PyObject *
IoBridge_readline(IoBridgeObject *self, PyObject *args)
{
HRESULT hr;
ULONG bufsize;
PSTR buf = NULL;
PyObject *ret = NULL;
#define BUF_SIZE 0x4000
buf = (PSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, BUF_SIZE);
if (buf != NULL) {
if (gPyDebugControl) {
Py_BEGIN_ALLOW_THREADS
hr = gPyDebugControl->Input(buf, BUF_SIZE-1, &bufsize);
Py_END_ALLOW_THREADS
if (hr == S_OK) {
if (lstrcmpiA(buf, "quit()") == 0 ||
lstrcmpiA(buf, "exit()") == 0)
{
// EOF
ret = Py_BuildValue("s", "");
}
else if (lstrlenA(buf) == 0)
{
// Empty line
ret = Py_BuildValue("s", "\n");
}
else
{
// Normal case
ret = Py_BuildValue("s", buf);
}
goto done;
}
}
}
// Something went wrong, return EOF
ret = Py_BuildValue("s", "");
done:
if (buf) HeapFree(GetProcessHeap(), 0, buf);
return ret;
}
static PyMethodDef IoBridge_methods[] = {
{"write", (PyCFunction)IoBridge_write, METH_VARARGS,
"Write to extensions output"
},
{"readline", (PyCFunction)IoBridge_readline, METH_VARARGS,
"Read extension input"
},
{NULL}
};
static PyMemberDef IoBridge_members[] = {
{"width", T_INT, offsetof(IoBridgeObject, width),
0, "Output width"},
{NULL}
};
static PyTypeObject IoBridgeType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"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_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"IoBridge objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
IoBridge_methods, /* tp_methods */
IoBridge_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
static PyObject *
ExtIo_ExitNop(PyObject *ignore, PyObject *arg)
{
Py_RETURN_NONE;
}
static PyMethodDef ExtIoMethods[] = {
{"exit_nop", (PyCFunction)ExtIo_ExitNop, METH_VARARGS,
"Type exit() or quit() to return back to Windbg."}
};
HRESULT initextio(void)
{
PyObject *m;
IoBridgeType.tp_new = PyType_GenericNew;
if (PyType_Ready(&IoBridgeType) < 0)
return E_FAIL;
m = Py_InitModule("extio", ExtIoMethods);
Py_INCREF(&IoBridgeType);
PyModule_AddObject(m, "IoBridge", (PyObject *)&IoBridgeType);
return S_OK;
}
HRESULT python_init(void)
{
HRESULT hr;
PyObject *module = NULL;
PyObject *IoBridge = NULL;
IoBridgeObject *OBInst = NULL;
PyObject *ExitNop = NULL;
PyObject *BuiltIns = NULL;
PyEval_InitThreads();
Py_Initialize();
if ((hr = initextio()) != S_OK)
return hr;
hr = E_FAIL;
module = PyImport_ImportModule("extio");
if (module == NULL)
goto fail;
ExitNop = PyObject_GetAttrString(module, "exit_nop");
if (ExitNop == NULL)
goto fail;
IoBridge = PyObject_GetAttrString(module, "IoBridge");
if (IoBridge == NULL)
goto fail;
OBInst = (IoBridgeObject *)PyObject_CallObject(IoBridge, NULL);
if (OBInst == NULL)
goto fail;
OBInst->width = CONSOLE_WIDTH;
if (PySys_SetObject("stdout", (PyObject *)OBInst) != 0)
goto fail;
if (PySys_SetObject("stderr", (PyObject *)OBInst) != 0)
goto fail;
if (PySys_SetObject("stdin", (PyObject *)OBInst) != 0)
goto fail;
if (PySys_SetObject("exit", ExitNop) != 0)
goto fail;
// Replace __builtins__.exit and __builtins__.quit
// If for some reason we fail, then ignore and go on
BuiltIns = PyEval_GetBuiltins();
if (BuiltIns != NULL) {
PyDict_SetItemString(BuiltIns, "exit", ExitNop);
PyDict_SetItemString(BuiltIns, "quit", ExitNop);
}
hr = S_OK;
fail:
Py_XDECREF(OBInst);
Py_XDECREF(IoBridge);
Py_XDECREF(ExitNop);
Py_XDECREF(module);
return hr;
}
void python_fini(void)
{
Py_Finalize();
}
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);
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) {
PyErr_Print();
goto done;
}
if (Py_FlushLine())
PyErr_Clear();
done:
gDebugControl->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;
PyObject *m;
PyObject *d;
PyObject *v = NULL;
ENTER_CALLBACK(Client);
Client->SetOutputWidth(100);
file = PyFile_FromString((char *)args, "r");
if (file == NULL) {
gDebugControl->Output(DEBUG_OUTPUT_ERROR, "Error opening '%s'", args);
goto done;
}
if ((m = PyImport_AddModule("__main__")) == NULL)
goto done;
d = PyModule_GetDict(m);
if ((v = PyRun_File(PyFile_AsFile(file), args, Py_file_input, d, d)) == NULL) {
PyErr_Print();
goto done;
}
if (Py_FlushLine())
PyErr_Clear();
done:
Py_XDECREF(v);
Py_XDECREF(file);
LEAVE_CALLBACK();
return S_OK;
}
HRESULT CALLBACK
python(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
PyObject *m;
PyObject *d;
PyObject *v = NULL;
ULONG Mask;
char *cmds[] = {
"import code",
"code.interact(banner=\""
"\\nEntering Python Interpreter\\n"
"type 'quit()' to quit\\n"
"\", local=globals())"
};
ENTER_CALLBACK(Client);
Client->GetOutputMask(&Mask);
Mask &= ~DEBUG_OUTPUT_PROMPT;
Client->SetOutputMask(Mask);
if ((m = PyImport_AddModule("__main__")) == NULL)
goto done;
d = PyModule_GetDict(m);
if ((v = 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) {
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);
LEAVE_CALLBACK();
return S_OK;
}