|
1 This patch adds Python debugger support. It may be contributed upstream at |
|
2 some point, but the suitability (or lack thereof) has not yet been determined. |
|
3 |
|
4 --- Python-3.4.0/Makefile.pre.in.~3~ 2014-03-19 10:48:24.881429192 -0700 |
|
5 +++ Python-3.4.0/Makefile.pre.in 2014-03-19 10:48:25.069002130 -0700 |
|
6 @@ -466,7 +466,7 @@ |
|
7 |
|
8 # Default target |
|
9 all: build_all |
|
10 -build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed python-config |
|
11 +build_all: $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks Modules/_testembed python-config build-py_db |
|
12 |
|
13 # Compile a binary with gcc profile guided optimization. |
|
14 profile-opt: |
|
15 @@ -658,6 +658,19 @@ |
|
16 @mv config.c Modules |
|
17 @echo "The Makefile was updated, you may need to re-run make." |
|
18 |
|
19 +SHLIB_FLAGS = -shared -fpic |
|
20 + |
|
21 +libpython3.4_db.so.1.0: $(srcdir)/py_db/libpython34_db.c |
|
22 + $(CC) -o $@ $(CFLAGS) $(PY_CPPFLAGS) $(CPPFLAGS) $(SHLIB_FLAGS) $< |
|
23 + |
|
24 +check_offset: $(srcdir)/py_db/check_offsets.c |
|
25 + $(CC) -o $@ $(CFLAGS) $(PY_CPPFLAGS) $(CPPFLAGS) $< |
|
26 + |
|
27 +build-py_db: libpython3.4_db.so.1.0 check_offset |
|
28 + |
|
29 +install-py_db: libpython3.4_db.so.1.0 check_offset |
|
30 + $(INSTALL_SHARED) libpython3.4_db.so.1.0 $(DESTDIR)$(LIBDIR) |
|
31 + $(INSTALL_PROGRAM) check_offset $(DESTDIR)$(BINDIR) |
|
32 |
|
33 Modules/Setup: $(srcdir)/Modules/Setup.dist |
|
34 @if test -f Modules/Setup; then \ |
|
35 @@ -1027,7 +1040,7 @@ |
|
36 $(TESTRUNNER) $(QUICKTESTOPTS) |
|
37 |
|
38 |
|
39 -install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall @FRAMEWORKINSTALLLAST@ |
|
40 +install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall install-py_db @FRAMEWORKINSTALLLAST@ |
|
41 if test "x$(ENSUREPIP)" != "xno" ; then \ |
|
42 case $(ENSUREPIP) in \ |
|
43 upgrade) ensurepip="--upgrade" ;; \ |
|
44 --- /dev/null |
|
45 +++ Python-3.4.0/py_db/check_offsets.c |
|
46 @@ -0,0 +1,87 @@ |
|
47 +/* |
|
48 + * CDDL HEADER START |
|
49 + * |
|
50 + * The contents of this file are subject to the terms of the |
|
51 + * Common Development and Distribution License (the "License"). |
|
52 + * You may not use this file except in compliance with the License. |
|
53 + * |
|
54 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
55 + * or http://www.opensolaris.org/os/licensing. |
|
56 + * See the License for the specific language governing permissions |
|
57 + * and limitations under the License. |
|
58 + * |
|
59 + * When distributing Covered Code, include this CDDL HEADER in each |
|
60 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
61 + * If applicable, add the following below this CDDL HEADER, with the |
|
62 + * fields enclosed by brackets "[]" replaced with your own identifying |
|
63 + * information: Portions Copyright [yyyy] [name of copyright owner] |
|
64 + * |
|
65 + * CDDL HEADER END |
|
66 + */ |
|
67 +/* |
|
68 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
|
69 + */ |
|
70 + |
|
71 +#include <stdio.h> |
|
72 + |
|
73 +#include <Python.h> |
|
74 +#include <frameobject.h> |
|
75 + |
|
76 +#include "libpython34_db_32.h" |
|
77 + |
|
78 +#if 0 |
|
79 +#define offsetof(s, m) ((size_t)(&(((s *)0)->m))) |
|
80 +#endif |
|
81 + |
|
82 +int |
|
83 +main(void) |
|
84 +{ |
|
85 +/* |
|
86 + * PyCodeObject co_name |
|
87 + * PyCodeObject co_filename |
|
88 + * PyCodeObject co_lnotab |
|
89 + * PyFrameObject f_back |
|
90 + * PyFrameObject f_code |
|
91 + * PyFrameObject f_lasti |
|
92 + * PyInterpreterState next |
|
93 + * PyInterpreterState tstate_head |
|
94 + * PyBytesObject ob_sval |
|
95 + * PyBytesObject ob_size |
|
96 + * PyThreadState frame |
|
97 + * PyThreadState next |
|
98 + */ |
|
99 + |
|
100 + printf("struct member: native 32\n"); |
|
101 + printf("PyCodeObject co_name: %d %d\n", offsetof(PyCodeObject, co_name), |
|
102 + offsetof(PyCodeObject32, co_name)); |
|
103 + printf("PyCodeObject co_filename: %d %d\n", offsetof(PyCodeObject, |
|
104 + co_filename), offsetof(PyCodeObject32, co_filename)); |
|
105 + printf("PyCodeObject co_lnotab: %d %d\n", offsetof(PyCodeObject, |
|
106 + co_lnotab), offsetof(PyCodeObject32, co_lnotab)); |
|
107 + printf("PyFrameObject f_back: %d %d\n", offsetof(PyFrameObject, f_back), |
|
108 + offsetof(PyFrameObject32, f_back)); |
|
109 + printf("PyFrameObject f_code: %d %d\n", offsetof(PyFrameObject, f_code), |
|
110 + offsetof(PyFrameObject32, f_code)); |
|
111 + printf("PyFrameObject f_lasti: %d %d\n", offsetof(PyFrameObject, |
|
112 + f_lasti), offsetof(PyFrameObject32, f_lasti)); |
|
113 + printf("PyInterpreterState next: %d %d\n", offsetof(PyInterpreterState, |
|
114 + next), offsetof(PyInterpreterState32, next)); |
|
115 + printf("PyInterpreterState tstate_head: %d %d\n", |
|
116 + offsetof(PyInterpreterState, tstate_head), |
|
117 + offsetof(PyInterpreterState32, tstate_head)); |
|
118 + printf("PyBytesObject ob_sval: %d %d\n", offsetof(PyBytesObject, |
|
119 + ob_sval), offsetof(PyBytesObject32, ob_sval)); |
|
120 + printf("PyBytesObject ob_size: %d %d\n", offsetof(PyBytesObject, |
|
121 + ob_base.ob_size), offsetof(PyBytesObject32, ob_size)); |
|
122 + printf("PyThreadState frame: %d %d\n", offsetof(PyThreadState, frame), |
|
123 + offsetof(PyThreadState32, frame)); |
|
124 + printf("PyThreadState next: %d %d\n", offsetof(PyThreadState, next), |
|
125 + offsetof(PyThreadState32, next)); |
|
126 + |
|
127 + printf("\nObject sizes\n"); |
|
128 + printf("PyObject: %d %d\n", sizeof (PyObject), sizeof (PyObject32)); |
|
129 + printf("PyVarObject: %d %d\n", sizeof (PyVarObject), |
|
130 + sizeof (PyVarObject32)); |
|
131 + |
|
132 + return (0); |
|
133 +} |
|
134 --- /dev/null |
|
135 +++ Python-3.4.0/py_db/libpython34_db.c |
|
136 @@ -0,0 +1,654 @@ |
|
137 +/* |
|
138 + * CDDL HEADER START |
|
139 + * |
|
140 + * The contents of this file are subject to the terms of the |
|
141 + * Common Development and Distribution License (the "License"). |
|
142 + * You may not use this file except in compliance with the License. |
|
143 + * |
|
144 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
145 + * or http://www.opensolaris.org/os/licensing. |
|
146 + * See the License for the specific language governing permissions |
|
147 + * and limitations under the License. |
|
148 + * |
|
149 + * When distributing Covered Code, include this CDDL HEADER in each |
|
150 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
151 + * If applicable, add the following below this CDDL HEADER, with the |
|
152 + * fields enclosed by brackets "[]" replaced with your own identifying |
|
153 + * information: Portions Copyright [yyyy] [name of copyright owner] |
|
154 + * |
|
155 + * CDDL HEADER END |
|
156 + */ |
|
157 +/* |
|
158 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
|
159 + */ |
|
160 + |
|
161 +#include <stdio.h> |
|
162 +#include <stdlib.h> |
|
163 +#include <string.h> |
|
164 +#include <errno.h> |
|
165 +#include <gelf.h> |
|
166 + |
|
167 +#include <Python.h> |
|
168 +#include <frameobject.h> |
|
169 + |
|
170 +#include "libpython34_db.h" |
|
171 +#if defined(_LP64) |
|
172 +#include "libpython34_db_32.h" |
|
173 +#endif /* _LP64 */ |
|
174 + |
|
175 +/* |
|
176 + * Because MDB always runs the debugger in the same datamodel as the target, |
|
177 + * only functions that are used by the procfs part of this interface (or shared |
|
178 + * between the two) are written as 64->32 aware. |
|
179 + */ |
|
180 +typedef struct pydb_arch_ops { |
|
181 + ssize_t (*strobj_readdata)(pydb_agent_t *, uintptr_t, unsigned char *, |
|
182 + size_t); |
|
183 + int (*frameinfo)(pydb_agent_t *, uintptr_t, char *, |
|
184 + size_t, char *, size_t, int *); |
|
185 +} pydb_arch_ops_t; |
|
186 + |
|
187 +struct pydb_agent { |
|
188 + struct ps_prochandle *pdb_ph; |
|
189 + int pdb_vers; |
|
190 + int pdb_is_64bit; |
|
191 + int pdb_datamodel; |
|
192 + const pydb_arch_ops_t *pdb_ops; |
|
193 +}; |
|
194 + |
|
195 +typedef uintptr_t (*pdi_next_cb_t)(pydb_iter_t *); |
|
196 + |
|
197 +struct pydb_iter { |
|
198 + struct ps_prochandle *pdi_ph; |
|
199 + uintptr_t pdi_current; |
|
200 + pdi_next_cb_t pdi_nextf; |
|
201 +}; |
|
202 + |
|
203 +#define LIBPYTHON "libpython3.4.so" |
|
204 + |
|
205 +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
|
206 + |
|
207 +/* Generic interface to helper functions */ |
|
208 +static ssize_t pydb_strobj_readdata(pydb_agent_t *py, uintptr_t addr, |
|
209 + unsigned char *buf, size_t buf_len); |
|
210 +static int pydb_getlno(pydb_agent_t *py, uintptr_t lnotab_addr, int firstline, |
|
211 + int lastinst); |
|
212 +static int pydb_frameinfo(pydb_agent_t *py, uintptr_t addr, char *funcnm, |
|
213 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno); |
|
214 + |
|
215 +/* datamodel specific implementation of helper functions */ |
|
216 +static ssize_t pydb_strobj_readdata_native(pydb_agent_t *py, uintptr_t addr, |
|
217 + unsigned char *buf, size_t buf_len); |
|
218 +static int pydb_frameinfo_native(pydb_agent_t *py, uintptr_t addr, char *funcnm, |
|
219 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno); |
|
220 + |
|
221 +#if defined (_LP64) |
|
222 +static ssize_t pydb_strobj_readdata_32(pydb_agent_t *py, uintptr_t addr, |
|
223 + unsigned char *buf, size_t buf_len); |
|
224 +static int pydb_frameinfo_32(pydb_agent_t *py, uintptr_t addr, char *funcnm, |
|
225 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno); |
|
226 +#endif /* _LP64 */ |
|
227 + |
|
228 +static ssize_t pydb_strobj_readstr(pydb_agent_t *py, uintptr_t addr, char *buf, |
|
229 + size_t len); |
|
230 + |
|
231 +/* Iterator function next routines. Plugable, configured by iterator init */ |
|
232 +static uintptr_t pydb_frame_iter_next(pydb_iter_t *iter); |
|
233 +static uintptr_t pydb_interp_iter_next(pydb_iter_t *iter); |
|
234 +static uintptr_t pydb_thread_iter_next(pydb_iter_t *iter); |
|
235 + |
|
236 +static const char *strbasename(const char *s); |
|
237 + |
|
238 +static const pydb_arch_ops_t arch_ops_native = { |
|
239 + .frameinfo = pydb_frameinfo_native, |
|
240 + .strobj_readdata = pydb_strobj_readdata_native, |
|
241 +}; |
|
242 + |
|
243 +#if defined (_LP64) |
|
244 +static const pydb_arch_ops_t arch_ops_32 = { |
|
245 + .frameinfo = pydb_frameinfo_32, |
|
246 + .strobj_readdata = pydb_strobj_readdata_32, |
|
247 +}; |
|
248 +#endif /* _LP64 */ |
|
249 + |
|
250 +static const char * |
|
251 +strbasename(const char *s) |
|
252 +{ |
|
253 + const char *p = strrchr(s, '/'); |
|
254 + |
|
255 + if (p == NULL) |
|
256 + return (s); |
|
257 + |
|
258 + return (++p); |
|
259 +} |
|
260 + |
|
261 +/* Agent creation / destruction routines */ |
|
262 + |
|
263 +pydb_agent_t * |
|
264 +pydb_agent_create(struct ps_prochandle *P, int vers) |
|
265 +{ |
|
266 + pydb_agent_t *py; |
|
267 + int datamodel; |
|
268 + |
|
269 + if (vers != PYDB_VERSION) { |
|
270 + errno = ENOTSUP; |
|
271 + return (NULL); |
|
272 + } |
|
273 + |
|
274 + if (ps_pdmodel(P, &datamodel) != PS_OK) { |
|
275 + return (NULL); |
|
276 + } |
|
277 + |
|
278 + py = (pydb_agent_t *)malloc(sizeof (pydb_agent_t)); |
|
279 + if (py == NULL) { |
|
280 + return (NULL); |
|
281 + } |
|
282 + |
|
283 + py->pdb_ph = P; |
|
284 + py->pdb_vers = vers; |
|
285 + py->pdb_datamodel = datamodel; |
|
286 + py->pdb_is_64bit = 0; |
|
287 + py->pdb_ops = &arch_ops_native; |
|
288 + |
|
289 +#if defined (_LP64) |
|
290 + py->pdb_is_64bit = (datamodel == PR_MODEL_LP64); |
|
291 + if (!py->pdb_is_64bit) { |
|
292 + py->pdb_ops = &arch_ops_32; |
|
293 + } |
|
294 +#endif /* _LP64 */ |
|
295 + |
|
296 + return (py); |
|
297 +} |
|
298 + |
|
299 +void |
|
300 +pydb_agent_destroy(pydb_agent_t *py) |
|
301 +{ |
|
302 + if (py == NULL) { |
|
303 + return; |
|
304 + } |
|
305 + |
|
306 + free(py); |
|
307 +} |
|
308 + |
|
309 +/* Helper functions */ |
|
310 +static int |
|
311 +pydb_getlno(pydb_agent_t *py, uintptr_t lnotab_addr, int firstline, |
|
312 + int lastinst) |
|
313 +{ |
|
314 + unsigned char lnotab[4096]; |
|
315 + ssize_t lnotab_len; |
|
316 + int addr, line; |
|
317 + int i; |
|
318 + |
|
319 + lnotab_len = pydb_strobj_readdata(py, lnotab_addr, lnotab, |
|
320 + sizeof (lnotab)); |
|
321 + if (lnotab_len < 0) { |
|
322 + return (-1); |
|
323 + } |
|
324 + |
|
325 + /* |
|
326 + * Python's line number algorithm is arcane. See here for details: |
|
327 + * http://svn.python.org/projects/python/trunk/Objects/lnotab_notes.txt |
|
328 + */ |
|
329 + |
|
330 + line = firstline; |
|
331 + for (addr = i = 0; i < lnotab_len; i += 2) { |
|
332 + if (addr + lnotab[i] > lastinst) { |
|
333 + break; |
|
334 + } |
|
335 + addr += lnotab[i]; |
|
336 + line += lnotab[i + 1]; |
|
337 + } |
|
338 + |
|
339 + return (line); |
|
340 +} |
|
341 + |
|
342 +static ssize_t |
|
343 +pydb_strobj_readdata(pydb_agent_t *py, uintptr_t addr, unsigned char *buf, |
|
344 + size_t buf_len) |
|
345 +{ |
|
346 + return (py->pdb_ops->strobj_readdata(py, addr, buf, buf_len)); |
|
347 +} |
|
348 + |
|
349 +static ssize_t |
|
350 +pydb_strobj_readdata_native(pydb_agent_t *py, uintptr_t addr, |
|
351 + unsigned char *buf, size_t buf_len) |
|
352 +{ |
|
353 + PyBytesObject sobj; |
|
354 + ssize_t obj_sz; |
|
355 + ssize_t read_sz; |
|
356 + psaddr_t straddr; |
|
357 + |
|
358 + /* |
|
359 + * PyBytesObject are variable size. The size of the PyBytesObject |
|
360 + * struct is fixed, and known at compile time; however, the size of the |
|
361 + * associated buffer is variable. The char[1] element at the end of the |
|
362 + * structure contains the string, and the ob_size of the PyBytesObject |
|
363 + * indicates how much extra space was allocated to contain the string |
|
364 + * buffer at the object's tail. Read in the fixed size portion of the |
|
365 + * object first, and then read the contents of the data buffer into the |
|
366 + * buffer passed by the caller. |
|
367 + */ |
|
368 + |
|
369 + if (ps_pread(py->pdb_ph, addr, &sobj, sizeof (PyBytesObject)) |
|
370 + != PS_OK) { |
|
371 + return (-1); |
|
372 + } |
|
373 + |
|
374 + obj_sz = (ssize_t)PyString_GET_SIZE(&sobj); |
|
375 + |
|
376 + read_sz = MIN(obj_sz, (ssize_t)buf_len); |
|
377 + straddr = (psaddr_t)(addr + offsetof(PyBytesObject, ob_sval)); |
|
378 + |
|
379 + if (ps_pread(py->pdb_ph, straddr, buf, (size_t)read_sz) != PS_OK) { |
|
380 + return (-1); |
|
381 + } |
|
382 + |
|
383 + return (read_sz); |
|
384 +} |
|
385 + |
|
386 +#if defined(_LP64) |
|
387 +static ssize_t |
|
388 +pydb_strobj_readdata_32(pydb_agent_t *py, uintptr_t addr, |
|
389 + unsigned char *buf, size_t buf_len) |
|
390 +{ |
|
391 + PyBytesObject32 sobj; |
|
392 + ssize_t obj_sz; |
|
393 + ssize_t read_sz; |
|
394 + psaddr_t straddr; |
|
395 + |
|
396 + /* |
|
397 + * PyBytesObject are variable size. The size of the PyBytesObject |
|
398 + * struct is fixed, and known at compile time; however, the size of the |
|
399 + * associated buffer is variable. The char[1] element at the end of the |
|
400 + * structure contains the string, and the ob_size of the PyBytesObject |
|
401 + * indicates how much extra space was allocated to contain the string |
|
402 + * buffer at the object's tail. Read in the fixed size portion of the |
|
403 + * object first, and then read the contents of the data buffer into the |
|
404 + * buffer passed by the caller. |
|
405 + */ |
|
406 + |
|
407 + if (ps_pread(py->pdb_ph, addr, &sobj, sizeof (PyBytesObject32)) |
|
408 + != PS_OK) { |
|
409 + return (-1); |
|
410 + } |
|
411 + |
|
412 + obj_sz = (ssize_t)PyString_GET_SIZE32(&sobj); |
|
413 + |
|
414 + read_sz = MIN(obj_sz, (ssize_t)buf_len); |
|
415 + straddr = (psaddr_t)(addr + offsetof(PyBytesObject32, ob_sval)); |
|
416 + |
|
417 + if (ps_pread(py->pdb_ph, straddr, buf, (size_t)read_sz) != PS_OK) { |
|
418 + return (-1); |
|
419 + } |
|
420 + |
|
421 + return (read_sz); |
|
422 +} |
|
423 +#endif /* _LP64 */ |
|
424 + |
|
425 +/* |
|
426 + * Most Python PyBytesObject contain strings, as one would expect. However, |
|
427 + * due to some sleazy hackery in parts of the Python code, some string objects |
|
428 + * are used as buffers for binary data. In the general case, |
|
429 + * pydb_strobj_readstr() should be used to read strings out of string objects. |
|
430 + * It wraps pydb_strobj_readdata(), which should be used by callers only when |
|
431 + * trying to retrieve binary data. (This routine does some string cleanup). |
|
432 + */ |
|
433 +static ssize_t |
|
434 +pydb_strobj_readstr(pydb_agent_t *py, uintptr_t addr, char *buf, |
|
435 + size_t buf_len) |
|
436 +{ |
|
437 + ssize_t read_sz; |
|
438 + |
|
439 + read_sz = pydb_strobj_readdata(py, addr, (unsigned char *)buf, buf_len); |
|
440 + |
|
441 + if (read_sz >= 0) { |
|
442 + if (read_sz >= buf_len) { |
|
443 + read_sz = buf_len - 1; |
|
444 + } |
|
445 + |
|
446 + buf[read_sz] = '\0'; |
|
447 + } |
|
448 + |
|
449 + return (read_sz); |
|
450 +} |
|
451 + |
|
452 + |
|
453 +static int |
|
454 +pydb_frameinfo(pydb_agent_t *py, uintptr_t addr, char *funcnm, |
|
455 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno) |
|
456 +{ |
|
457 + return (py->pdb_ops->frameinfo(py, addr, funcnm, funcnm_sz, |
|
458 + filenm, filenm_sz, lineno)); |
|
459 +} |
|
460 + |
|
461 +static int |
|
462 +pydb_frameinfo_native(pydb_agent_t *py, uintptr_t addr, char *funcnm, |
|
463 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno) |
|
464 +{ |
|
465 + PyFrameObject fo; |
|
466 + PyCodeObject co; |
|
467 + ssize_t rc; |
|
468 + |
|
469 + if (ps_pread(py->pdb_ph, addr, &fo, sizeof (PyFrameObject)) |
|
470 + != PS_OK) { |
|
471 + return (-1); |
|
472 + } |
|
473 + |
|
474 + if (ps_pread(py->pdb_ph, (uintptr_t)fo.f_code, &co, |
|
475 + sizeof (PyCodeObject)) != PS_OK) { |
|
476 + return (-1); |
|
477 + } |
|
478 + |
|
479 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_name, funcnm, funcnm_sz); |
|
480 + if (rc < 0) { |
|
481 + return (-1); |
|
482 + } |
|
483 + |
|
484 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_filename, filenm, |
|
485 + filenm_sz); |
|
486 + if (rc < 0) { |
|
487 + return (-1); |
|
488 + } |
|
489 + |
|
490 + *lineno = pydb_getlno(py, (uintptr_t)co.co_lnotab, co.co_firstlineno, |
|
491 + fo.f_lasti); |
|
492 + if (*lineno < 0) { |
|
493 + return (-1); |
|
494 + } |
|
495 + |
|
496 + return (0); |
|
497 +} |
|
498 + |
|
499 +#if defined (_LP64) |
|
500 +static int |
|
501 +pydb_frameinfo_32(pydb_agent_t *py, uintptr_t addr, char *funcnm, |
|
502 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno) |
|
503 +{ |
|
504 + PyFrameObject32 fo; |
|
505 + PyCodeObject32 co; |
|
506 + ssize_t rc; |
|
507 + |
|
508 + if (ps_pread(py->pdb_ph, addr, &fo, sizeof (PyFrameObject32)) |
|
509 + != PS_OK) { |
|
510 + return (-1); |
|
511 + } |
|
512 + |
|
513 + if (ps_pread(py->pdb_ph, (uintptr_t)fo.f_code, &co, |
|
514 + sizeof (PyCodeObject32)) != PS_OK) { |
|
515 + return (-1); |
|
516 + } |
|
517 + |
|
518 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_name, funcnm, funcnm_sz); |
|
519 + if (rc < 0) { |
|
520 + return (-1); |
|
521 + } |
|
522 + |
|
523 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_filename, filenm, |
|
524 + filenm_sz); |
|
525 + if (rc < 0) { |
|
526 + return (-1); |
|
527 + } |
|
528 + |
|
529 + *lineno = pydb_getlno(py, (uintptr_t)co.co_lnotab, co.co_firstlineno, |
|
530 + fo.f_lasti); |
|
531 + if (*lineno < 0) { |
|
532 + return (-1); |
|
533 + } |
|
534 + |
|
535 + return (0); |
|
536 +} |
|
537 + |
|
538 +#endif /* _LP64 */ |
|
539 + |
|
540 +/* Functions that are part of the library's interface */ |
|
541 + |
|
542 +/* |
|
543 + * Given the address of a PyFrameObject, and a buffer of a known size, |
|
544 + * fill the buffer with a description of the frame. |
|
545 + */ |
|
546 +int |
|
547 +pydb_get_frameinfo(pydb_agent_t *py, uintptr_t frame_addr, char *fbuf, |
|
548 + size_t bufsz, int verbose) |
|
549 +{ |
|
550 + char funcname[1024]; |
|
551 + char filename[1024]; |
|
552 + char *fn; |
|
553 + int lineno; |
|
554 + int length = (py->pdb_is_64bit ? 16 : 8); |
|
555 + int rc; |
|
556 + |
|
557 + rc = pydb_frameinfo(py, frame_addr, funcname, sizeof (funcname), |
|
558 + filename, sizeof (filename), &lineno); |
|
559 + if (rc < 0) { |
|
560 + return (-1); |
|
561 + } |
|
562 + |
|
563 + if (!verbose) { |
|
564 + fn = (char *)strbasename(filename); |
|
565 + } else { |
|
566 + fn = filename; |
|
567 + } |
|
568 + |
|
569 + (void) snprintf(fbuf, bufsz, "%0.*lx %s:%d %s()\n", length, |
|
570 + frame_addr, fn, lineno, funcname); |
|
571 + |
|
572 + return (0); |
|
573 +} |
|
574 + |
|
575 +/* |
|
576 + * Return a description about a PyFrameObject, if the object is |
|
577 + * actually a PyFrameObject. In this case, the pc argument is checked |
|
578 + * to make sure that it came from a function that takes a PyFrameObject |
|
579 + * as its first (argv[0]) argument. |
|
580 + */ |
|
581 +int |
|
582 +pydb_pc_frameinfo(pydb_agent_t *py, uintptr_t pc, uintptr_t frame_addr, |
|
583 + char *fbuf, size_t bufsz) |
|
584 +{ |
|
585 + char funcname[1024]; |
|
586 + char filename[1024]; |
|
587 + int lineno; |
|
588 + int rc; |
|
589 + ps_sym_t psym; |
|
590 + |
|
591 + /* |
|
592 + * If PC doesn't match PyEval_EvalFrameEx in either libpython |
|
593 + * or the executable, don't decode it. |
|
594 + */ |
|
595 + if (ps_pglobal_sym(py->pdb_ph, LIBPYTHON, "PyEval_EvalFrameEx", &psym) |
|
596 + != PS_OK) { |
|
597 + return (-1); |
|
598 + } |
|
599 + |
|
600 + /* If symbol found, ensure that PC falls within PyEval_EvalFrameEx. */ |
|
601 + if (pc < psym.st_value || pc > psym.st_value + psym.st_size) { |
|
602 + return (-1); |
|
603 + } |
|
604 + |
|
605 + rc = pydb_frameinfo(py, frame_addr, funcname, sizeof (funcname), |
|
606 + filename, sizeof (filename), &lineno); |
|
607 + if (rc < 0) { |
|
608 + return (-1); |
|
609 + } |
|
610 + |
|
611 + (void) snprintf(fbuf, bufsz, "[ %s:%d (%s) ]\n", filename, lineno, |
|
612 + funcname); |
|
613 + |
|
614 + return (0); |
|
615 +} |
|
616 + |
|
617 +/* |
|
618 + * Walks the list of PyInterpreterState objects. If caller doesn't |
|
619 + * supply address of list, this method will look it up. |
|
620 + */ |
|
621 +pydb_iter_t * |
|
622 +pydb_interp_iter_init(pydb_agent_t *py, uintptr_t addr) |
|
623 +{ |
|
624 + pydb_iter_t *itr; |
|
625 + uintptr_t i_addr; |
|
626 + int rc; |
|
627 + |
|
628 + if (addr == 0) { |
|
629 + rc = ps_pglobal_lookup(py->pdb_ph, LIBPYTHON, "interp_head", |
|
630 + (psaddr_t *)&addr); |
|
631 + if (rc != PS_OK) { |
|
632 + return (NULL); |
|
633 + } |
|
634 + } |
|
635 + |
|
636 + if (ps_pread(py->pdb_ph, (uintptr_t)addr, &i_addr, sizeof (uintptr_t)) |
|
637 + != PS_OK) { |
|
638 + return (NULL); |
|
639 + } |
|
640 + |
|
641 + itr = malloc(sizeof (pydb_iter_t)); |
|
642 + if (itr == NULL) { |
|
643 + return (NULL); |
|
644 + } |
|
645 + |
|
646 + itr->pdi_ph = py->pdb_ph; |
|
647 + itr->pdi_current = i_addr; |
|
648 + itr->pdi_nextf = pydb_interp_iter_next; |
|
649 + |
|
650 + return (itr); |
|
651 +} |
|
652 + |
|
653 +static uintptr_t |
|
654 +pydb_interp_iter_next(pydb_iter_t *iter) |
|
655 +{ |
|
656 + PyInterpreterState st; |
|
657 + uintptr_t cur; |
|
658 + |
|
659 + cur = iter->pdi_current; |
|
660 + |
|
661 + if (cur == 0) { |
|
662 + return (cur); |
|
663 + } |
|
664 + |
|
665 + if (ps_pread(iter->pdi_ph, cur, &st, sizeof (PyInterpreterState)) |
|
666 + != PS_OK) { |
|
667 + iter->pdi_current = 0; |
|
668 + return (0); |
|
669 + } |
|
670 + |
|
671 + iter->pdi_current = (uintptr_t)st.next; |
|
672 + |
|
673 + return (cur); |
|
674 +} |
|
675 + |
|
676 +/* |
|
677 + * Walk a list of Python PyFrameObjects. The addr argument must be |
|
678 + * the address of a valid PyThreadState object. |
|
679 + */ |
|
680 +pydb_iter_t * |
|
681 +pydb_frame_iter_init(pydb_agent_t *py, uintptr_t addr) |
|
682 +{ |
|
683 + pydb_iter_t *itr; |
|
684 + PyThreadState ts; |
|
685 + |
|
686 + if (ps_pread(py->pdb_ph, (uintptr_t)addr, &ts, sizeof (PyThreadState)) |
|
687 + != PS_OK) { |
|
688 + return (NULL); |
|
689 + } |
|
690 + |
|
691 + itr = malloc(sizeof (pydb_iter_t)); |
|
692 + if (itr == NULL) { |
|
693 + return (NULL); |
|
694 + } |
|
695 + |
|
696 + itr->pdi_ph = py->pdb_ph; |
|
697 + itr->pdi_current = (uintptr_t)ts.frame; |
|
698 + itr->pdi_nextf = pydb_frame_iter_next; |
|
699 + |
|
700 + return (itr); |
|
701 +} |
|
702 + |
|
703 +static uintptr_t |
|
704 +pydb_frame_iter_next(pydb_iter_t *iter) |
|
705 +{ |
|
706 + PyFrameObject fo; |
|
707 + uintptr_t cur; |
|
708 + |
|
709 + cur = iter->pdi_current; |
|
710 + |
|
711 + if (cur == 0) { |
|
712 + return (cur); |
|
713 + } |
|
714 + |
|
715 + if (ps_pread(iter->pdi_ph, cur, &fo, sizeof (PyFrameObject)) |
|
716 + != PS_OK) { |
|
717 + iter->pdi_current = 0; |
|
718 + return (0); |
|
719 + } |
|
720 + |
|
721 + iter->pdi_current = (uintptr_t)fo.f_back; |
|
722 + |
|
723 + return (cur); |
|
724 +} |
|
725 + |
|
726 +/* |
|
727 + * Walk a list of Python PyThreadState objects. The addr argument must be |
|
728 + * the address of a valid PyInterpreterState object. |
|
729 + */ |
|
730 +pydb_iter_t * |
|
731 +pydb_thread_iter_init(pydb_agent_t *py, uintptr_t addr) |
|
732 +{ |
|
733 + pydb_iter_t *itr; |
|
734 + PyInterpreterState is; |
|
735 + |
|
736 + if (ps_pread(py->pdb_ph, (uintptr_t)addr, &is, |
|
737 + sizeof (PyInterpreterState)) != PS_OK) { |
|
738 + return (NULL); |
|
739 + } |
|
740 + |
|
741 + itr = malloc(sizeof (pydb_iter_t)); |
|
742 + if (itr == NULL) { |
|
743 + return (NULL); |
|
744 + } |
|
745 + |
|
746 + itr->pdi_ph = py->pdb_ph; |
|
747 + itr->pdi_current = (uintptr_t)is.tstate_head; |
|
748 + itr->pdi_nextf = pydb_thread_iter_next; |
|
749 + |
|
750 + return (itr); |
|
751 +} |
|
752 + |
|
753 +static uintptr_t |
|
754 +pydb_thread_iter_next(pydb_iter_t *iter) |
|
755 +{ |
|
756 + PyThreadState ts; |
|
757 + uintptr_t cur; |
|
758 + |
|
759 + cur = iter->pdi_current; |
|
760 + |
|
761 + if (cur == 0) { |
|
762 + return (cur); |
|
763 + } |
|
764 + |
|
765 + if (ps_pread(iter->pdi_ph, cur, &ts, sizeof (PyThreadState)) != PS_OK) { |
|
766 + iter->pdi_current = 0; |
|
767 + return (0); |
|
768 + } |
|
769 + |
|
770 + iter->pdi_current = (uintptr_t)ts.next; |
|
771 + |
|
772 + return (cur); |
|
773 +} |
|
774 + |
|
775 + |
|
776 +uintptr_t |
|
777 +pydb_iter_next(pydb_iter_t *iter) |
|
778 +{ |
|
779 + return (iter->pdi_nextf(iter)); |
|
780 +} |
|
781 + |
|
782 +void |
|
783 +pydb_iter_fini(pydb_iter_t *iter) |
|
784 +{ |
|
785 + if (iter == NULL) { |
|
786 + return; |
|
787 + } |
|
788 + |
|
789 + free(iter); |
|
790 +} |
|
791 --- /dev/null |
|
792 +++ Python-3.4.0/py_db/libpython34_db.h |
|
793 @@ -0,0 +1,73 @@ |
|
794 +/* |
|
795 + * CDDL HEADER START |
|
796 + * |
|
797 + * The contents of this file are subject to the terms of the |
|
798 + * Common Development and Distribution License (the "License"). |
|
799 + * You may not use this file except in compliance with the License. |
|
800 + * |
|
801 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
802 + * or http://www.opensolaris.org/os/licensing. |
|
803 + * See the License for the specific language governing permissions |
|
804 + * and limitations under the License. |
|
805 + * |
|
806 + * When distributing Covered Code, include this CDDL HEADER in each |
|
807 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
808 + * If applicable, add the following below this CDDL HEADER, with the |
|
809 + * fields enclosed by brackets "[]" replaced with your own identifying |
|
810 + * information: Portions Copyright [yyyy] [name of copyright owner] |
|
811 + * |
|
812 + * CDDL HEADER END |
|
813 + */ |
|
814 +/* |
|
815 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
|
816 + */ |
|
817 + |
|
818 +#ifndef _LIBPYTHON34_DB_H |
|
819 +#define _LIBPYTHON34_DB_H |
|
820 + |
|
821 +#include <proc_service.h> |
|
822 + |
|
823 +#ifdef __cplusplus |
|
824 +extern "C" { |
|
825 +#endif |
|
826 + |
|
827 +/* Agent is opaque to library's consumers. */ |
|
828 +typedef struct pydb_agent pydb_agent_t; |
|
829 + |
|
830 +/* |
|
831 + * Library's debug version is 1. Changes to interface should increase this |
|
832 + * number. |
|
833 + */ |
|
834 +#define PYDB_VERSION 1 |
|
835 + |
|
836 +/* Agent creation/destruction routines */ |
|
837 +extern pydb_agent_t *pydb_agent_create(struct ps_prochandle *P, int vers); |
|
838 +extern void pydb_agent_destroy(pydb_agent_t *py); |
|
839 + |
|
840 +/* Used by callers that know they are looking at a PyFrameObject */ |
|
841 +extern int pydb_get_frameinfo(pydb_agent_t *py, uintptr_t frame_addr, |
|
842 + char *fbuf, size_t bufsz, int verbose); |
|
843 + |
|
844 +/* |
|
845 + * Used by callers that don't know if they're looking at PyFrameObject. |
|
846 + * Checks PC for traceable functions. |
|
847 + */ |
|
848 +extern int pydb_pc_frameinfo(pydb_agent_t *py, uintptr_t pc, |
|
849 + uintptr_t frame_addr, char *fbuf, size_t bufsz); |
|
850 + |
|
851 +/* Iterator functions */ |
|
852 +typedef struct pydb_iter pydb_iter_t; |
|
853 + |
|
854 +extern pydb_iter_t *pydb_frame_iter_init(pydb_agent_t *py, uintptr_t addr); |
|
855 +extern pydb_iter_t *pydb_interp_iter_init(pydb_agent_t *py, |
|
856 + uintptr_t addr); |
|
857 +extern pydb_iter_t *pydb_thread_iter_init(pydb_agent_t *py, |
|
858 + uintptr_t addr); |
|
859 +extern void pydb_iter_fini(pydb_iter_t *iter); |
|
860 +extern uintptr_t pydb_iter_next(pydb_iter_t *iter); |
|
861 + |
|
862 +#ifdef __cplusplus |
|
863 +} |
|
864 +#endif |
|
865 + |
|
866 +#endif /* _LIBPYTHON34_DB_H */ |
|
867 --- /dev/null |
|
868 +++ Python-3.4.0/py_db/libpython34_db_32.h |
|
869 @@ -0,0 +1,121 @@ |
|
870 +/* |
|
871 + * CDDL HEADER START |
|
872 + * |
|
873 + * The contents of this file are subject to the terms of the |
|
874 + * Common Development and Distribution License (the "License"). |
|
875 + * You may not use this file except in compliance with the License. |
|
876 + * |
|
877 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
878 + * or http://www.opensolaris.org/os/licensing. |
|
879 + * See the License for the specific language governing permissions |
|
880 + * and limitations under the License. |
|
881 + * |
|
882 + * When distributing Covered Code, include this CDDL HEADER in each |
|
883 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
884 + * If applicable, add the following below this CDDL HEADER, with the |
|
885 + * fields enclosed by brackets "[]" replaced with your own identifying |
|
886 + * information: Portions Copyright [yyyy] [name of copyright owner] |
|
887 + * |
|
888 + * CDDL HEADER END |
|
889 + */ |
|
890 +/* |
|
891 + * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
|
892 + */ |
|
893 + |
|
894 +#ifndef _LIBPYTHON34_DB_32_H |
|
895 +#define _LIBPYTHON34_DB_32_H |
|
896 + |
|
897 +#ifdef __cplusplus |
|
898 +extern "C" { |
|
899 +#endif |
|
900 + |
|
901 +#include <sys/types.h> |
|
902 + |
|
903 +/* |
|
904 + * Define 32-bit Python data structures for use by the 64-bit debugger. This |
|
905 + * is so that a 64-bit debugger may properly examine a 32-bit process. |
|
906 + * |
|
907 + * In many cases, the debug library is only concerned with a few fields in the |
|
908 + * Python structure. In that case, the other ancillary fields are elided. |
|
909 + */ |
|
910 + |
|
911 +typedef uint32_t uintptr32_t; |
|
912 +typedef int32_t Py_ssize32_t; |
|
913 + |
|
914 +typedef struct _is32 { |
|
915 + uintptr32_t next; |
|
916 + uintptr32_t tstate_head; |
|
917 +} PyInterpreterState32; |
|
918 + |
|
919 +typedef struct _ts32 { |
|
920 + uintptr32_t next; |
|
921 + uintptr32_t interp; |
|
922 + uintptr32_t frame; |
|
923 +} PyThreadState32; |
|
924 + |
|
925 +#define PyObject_HEAD32 \ |
|
926 + Py_ssize32_t ob_refcnt; \ |
|
927 + uintptr32_t ob_type; |
|
928 + |
|
929 +#define PyObject_VAR_HEAD32 \ |
|
930 + PyObject_HEAD32 \ |
|
931 + Py_ssize32_t ob_size; |
|
932 + |
|
933 +typedef struct { |
|
934 + PyObject_HEAD32 |
|
935 +} PyObject32; |
|
936 + |
|
937 +typedef struct { |
|
938 + PyObject_VAR_HEAD32 |
|
939 +} PyVarObject32; |
|
940 + |
|
941 +typedef struct { |
|
942 + PyObject_VAR_HEAD32 |
|
943 + int32_t ob_shash; |
|
944 + int ob_sstate; |
|
945 + char ob_sval[1]; |
|
946 +} PyBytesObject32; |
|
947 + |
|
948 +#define Py_SIZE32(ob) (((PyVarObject32*)(ob))->ob_size) |
|
949 +#define PyString_GET_SIZE32(op) Py_SIZE32(op) |
|
950 +#define PyString_AS_STRING32(op) (((PyBytesObject32 *)(op))->ob_sval) |
|
951 + |
|
952 +typedef struct { |
|
953 + PyObject_VAR_HEAD32 |
|
954 + uintptr32_t f_back; |
|
955 + uintptr32_t f_code; |
|
956 + uintptr32_t f_builtins; |
|
957 + uintptr32_t f_globals; |
|
958 + uintptr32_t f_locals; |
|
959 + uintptr32_t f_valuestack; |
|
960 + uintptr32_t f_stacktop; |
|
961 + uintptr32_t f_trace; |
|
962 + uintptr32_t f_exc_typpe, f_exc_value, f_exc_traceback; |
|
963 + uintptr32_t f_tstate; |
|
964 + int f_lasti; |
|
965 + int f_lineno; |
|
966 +} PyFrameObject32; |
|
967 + |
|
968 +typedef struct { |
|
969 + PyObject_HEAD32 |
|
970 + int co_argcount; |
|
971 + int co_nlocals; |
|
972 + int co_stacksize; |
|
973 + int co_flags; |
|
974 + uintptr32_t co_code; |
|
975 + uintptr32_t co_consts; |
|
976 + uintptr32_t co_names; |
|
977 + uintptr32_t co_varnames; |
|
978 + uintptr32_t co_freevars; |
|
979 + uintptr32_t co_cellvars; |
|
980 + uintptr32_t co_filename; |
|
981 + uintptr32_t co_name; |
|
982 + int co_firstlineno; |
|
983 + uintptr32_t co_lnotab; |
|
984 +} PyCodeObject32; |
|
985 + |
|
986 +#ifdef __cplusplus |
|
987 +} |
|
988 +#endif |
|
989 + |
|
990 +#endif /* _LIBPYTHON34_DB_32_H */ |
|
991 --- /dev/null |
|
992 +++ Python-3.4.0/py_db/mapfile-vers |
|
993 @@ -0,0 +1,39 @@ |
|
994 +# |
|
995 +# CDDL HEADER START |
|
996 +# |
|
997 +# The contents of this file are subject to the terms of the |
|
998 +# Common Development and Distribution License (the "License"). |
|
999 +# You may not use this file except in compliance with the License. |
|
1000 +# |
|
1001 +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
1002 +# or http://www.opensolaris.org/os/licensing. |
|
1003 +# See the License for the specific language governing permissions |
|
1004 +# and limitations under the License. |
|
1005 +# |
|
1006 +# When distributing Covered Code, include this CDDL HEADER in each |
|
1007 +# file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
1008 +# If applicable, add the following below this CDDL HEADER, with the |
|
1009 +# fields enclosed by brackets "[]" replaced with your own identifying |
|
1010 +# information: Portions Copyright [yyyy] [name of copyright owner] |
|
1011 +# |
|
1012 +# CDDL HEADER END |
|
1013 +# |
|
1014 + |
|
1015 +# |
|
1016 +# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
|
1017 +# |
|
1018 + |
|
1019 +SUNWprivate_1.1 { |
|
1020 + global: |
|
1021 + pydb_agent_create; |
|
1022 + pydb_agent_destroy; |
|
1023 + pydb_frame_iter_init; |
|
1024 + pydb_get_frameinfo; |
|
1025 + pydb_pc_frameinfo; |
|
1026 + pydb_interp_iter_init; |
|
1027 + pydb_thread_iter_init; |
|
1028 + pydb_iter_fini; |
|
1029 + pydb_iter_next; |
|
1030 + local: |
|
1031 + *; |
|
1032 +}; |