-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpyembed.cpp
More file actions
433 lines (370 loc) · 10.7 KB
/
pyembed.cpp
File metadata and controls
433 lines (370 loc) · 10.7 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//
// To Do List
//
// Nice to Haves
// - pydbgeng and pywindbg available by default
//
// Might be useful
// - Wrap the "old" apis as python calls
#define PY_SSIZE_T_CLEAN
#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 (data == NULL)
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
Py_BEGIN_ALLOW_THREADS
buf = (PSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, BUF_SIZE);
Py_END_ALLOW_THREADS
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:
Py_BEGIN_ALLOW_THREADS
if (buf) HeapFree(GetProcessHeap(), 0, buf);
Py_END_ALLOW_THREADS
return ret;
}
static PyObject*
IoBridge_flush(IoBridgeObject* self, PyObject* args)
{
Py_RETURN_NONE;
}
static PyMethodDef IoBridge_methods[] = {
{"write", (PyCFunction)IoBridge_write, METH_VARARGS,
"Write to extensions output"
},
{"readline", (PyCFunction)IoBridge_readline, METH_VARARGS,
"Read extension input"
},
{"flush", (PyCFunction)IoBridge_flush, METH_VARARGS,
"Flush output"
},
{NULL}
};
static PyMemberDef IoBridge_members[] = {
{"width", T_INT, offsetof(IoBridgeObject, width),
0, "Output width"},
{NULL}
};
static PyTypeObject IoBridgeType = {
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_reserved*/
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."},
{NULL, NULL, 0, NULL}
};
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 NULL;
m = PyModule_Create(&extiomodule);
if (m == NULL) {
return NULL;
}
Py_INCREF(&IoBridgeType);
if (PyModule_AddObject(m, "IoBridge", (PyObject*)&IoBridgeType) != 0) {
Py_DECREF(&IoBridgeType);
Py_DECREF(m);
return NULL;
}
return m;
}
HRESULT python_init(void)
{
HRESULT hr;
PyObject *module = NULL;
PyObject *IoBridge = NULL;
IoBridgeObject *OBInst = NULL;
PyObject *ExitNop = NULL;
PyObject *BuiltIns = NULL;
if (PyImport_AppendInittab("extio", PyInit_extio) == -1)
return E_FAIL;
Py_Initialize();
//PyEval_InitThreads();
hr = E_FAIL;
if (PyRun_SimpleString("import sys") != 0)
return 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((PyObject *)&IoBridgeType, 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
eval(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
PSTR cmd = NULL;
size_t cmdlen = 0;
ENTER_CALLBACK(Client);
Py_BEGIN_ALLOW_THREADS
cmdlen = lstrlenA(args + 2);
cmd = (PSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cmdlen);
Py_END_ALLOW_THREADS
if (cmd == NULL)
goto done;
lstrcpyA(cmd, args);
lstrcatA(cmd, "\n");
if (PyRun_SimpleString(cmd) == -1) {
PyErr_Print();
goto done;
}
done:
Py_BEGIN_ALLOW_THREADS
gPyDebugControl->Output(DEBUG_OUTPUT_NORMAL, "\n");
if (cmd) HeapFree(GetProcessHeap(), 0, cmd);
Py_END_ALLOW_THREADS
LEAVE_CALLBACK();
return S_OK;
}
HRESULT CALLBACK
exec(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
FILE* file = NULL;
PyObject *m;
PyObject *d;
PyObject* v = NULL;
ENTER_CALLBACK(Client);
//Client->SetOutputWidth(100);
Py_BEGIN_ALLOW_THREADS
file = fopen(args, "rb");
Py_END_ALLOW_THREADS
if (file == NULL) {
Py_BEGIN_ALLOW_THREADS
gPyDebugControl->Output(DEBUG_OUTPUT_ERROR, "Error opening '%s'", args);
Py_END_ALLOW_THREADS
goto done;
}
if ((m = PyImport_AddModule("__main__")) == NULL)
goto done;
d = PyModule_GetDict(m);
if ((v = PyRun_File(file, args, Py_file_input, d, d)) == NULL) {
PyErr_Print();
goto done;
}
done:
Py_BEGIN_ALLOW_THREADS
if (file != NULL)
{
fclose(file);
}
Py_END_ALLOW_THREADS
Py_XDECREF(v);
// m and d are borrowed references
LEAVE_CALLBACK();
return S_OK;
}
HRESULT CALLBACK
python(IN IDebugClient *Client, IN OPTIONAL PCSTR args)
{
PyObject *m;
PyObject *d;
PyObject *v1 = NULL;
PyObject* v2 = NULL;
ULONG Mask;
const char *cmds[] = {
"import code",
"code.interact(banner=\""
"\\nEntering Python Interpreter\\n"
"type 'quit()' to quit\\n"
"\", local=globals())"
};
ENTER_CALLBACK(Client);
Py_BEGIN_ALLOW_THREADS
Client->GetOutputMask(&Mask);
Mask &= ~DEBUG_OUTPUT_PROMPT;
Client->SetOutputMask(Mask);
Py_END_ALLOW_THREADS
if ((m = PyImport_AddModule("__main__")) == NULL)
goto done;
d = PyModule_GetDict(m);
if ((v1 = PyRun_StringFlags(cmds[0], Py_single_input, d, d, NULL)) == NULL) {
PyErr_Print();
goto done;
}
if ((v2 = PyRun_StringFlags(cmds[1], Py_single_input, d, d, NULL)) == NULL) {
PyErr_Print();
goto done;
}
done:
Py_BEGIN_ALLOW_THREADS
Mask |= DEBUG_OUTPUT_PROMPT;
Client->SetOutputMask(Mask);
gPyDebugControl->Output(DEBUG_OUTPUT_NORMAL, "Leaving Python\n");
Py_END_ALLOW_THREADS
Py_XDECREF(v1);
Py_XDECREF(v2);
// m and d are borrowed references
LEAVE_CALLBACK();
return S_OK;
}