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.
4 --- Python-2.7.15/Makefile.pre.in 2018-08-23 19:21:55.679929900 +0000
5 +++ Python-2.7.15/Makefile.pre.in 2018-08-23 19:32:47.145140718 +0000
6 @@ -403,7 +403,7 @@ LIBRARY_OBJS= \
9 all: @DEF_MAKE_ALL_RULE@
10 -build_all: check-clean-src $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks
11 +build_all: check-clean-src $(BUILDPYTHON) oldsharedmods sharedmods gdbhooks build-py_db
13 # Check that the source is clean when building out of source.
17 $(LIBRARY_OBJS) $(MODOBJS) Modules/python.o: $(PYTHON_HEADERS)
19 +SHLIB_FLAGS = -shared -fpic
21 +libpython2.7_db.so.1.0: $(srcdir)/py_db/libpython27_db.c
22 + $(CC) -o $@ $(CFLAGS) $(CPPFLAGS) $(SHLIB_FLAGS) $<
24 +build-py_db: libpython2.7_db.so.1.0
26 +install-py_db: libpython2.7_db.so.1.0
27 + $(INSTALL_SHARED) libpython2.7_db.so.1.0 $(DESTDIR)$(LIBDIR)
29 ######################################################################
32 $(TESTPYTHON) $(TESTPROG) $(MEMTESTOPTS)
35 -install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall @FRAMEWORKINSTALLLAST@
36 +install: @FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall @FRAMEWORKINSTALLLAST@ install-py_db
37 if test "x$(ENSUREPIP)" != "xno" ; then \
38 case $(ENSUREPIP) in \
39 upgrade) ensurepip="--upgrade" ;; \
41 +++ Python-2.7.1/py_db/libpython27_db.c
46 + * The contents of this file are subject to the terms of the
47 + * Common Development and Distribution License (the "License").
48 + * You may not use this file except in compliance with the License.
50 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
51 + * or http://www.opensolaris.org/os/licensing.
52 + * See the License for the specific language governing permissions
53 + * and limitations under the License.
55 + * When distributing Covered Code, include this CDDL HEADER in each
56 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
57 + * If applicable, add the following below this CDDL HEADER, with the
58 + * fields enclosed by brackets "[]" replaced with your own identifying
59 + * information: Portions Copyright [yyyy] [name of copyright owner]
64 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
74 +#include <frameobject.h>
76 +#include "libpython27_db.h"
78 +#include "libpython27_db_32.h"
82 + * Because MDB always runs the debugger in the same datamodel as the target,
83 + * only functions that are used by the procfs part of this interface (or shared
84 + * between the two) are written as 64->32 aware.
86 +typedef struct pydb_arch_ops {
87 + ssize_t (*strobj_readdata)(pydb_agent_t *, uintptr_t, unsigned char *,
89 + int (*frameinfo)(pydb_agent_t *, uintptr_t, char *,
90 + size_t, char *, size_t, int *);
94 + struct ps_prochandle *pdb_ph;
98 + const pydb_arch_ops_t *pdb_ops;
101 +typedef uintptr_t (*pdi_next_cb_t)(pydb_iter_t *);
104 + struct ps_prochandle *pdi_ph;
105 + uintptr_t pdi_current;
106 + pdi_next_cb_t pdi_nextf;
109 +#define LIBPYTHON "libpython2.7.so"
111 +#define MIN(x, y) (((x) < (y)) ? (x) : (y))
113 +/* Generic interface to helper functions */
114 +static ssize_t pydb_strobj_readdata(pydb_agent_t *py, uintptr_t addr,
115 + unsigned char *buf, size_t buf_len);
116 +static int pydb_getlno(pydb_agent_t *py, uintptr_t lnotab_addr, int firstline,
118 +static int pydb_frameinfo(pydb_agent_t *py, uintptr_t addr, char *funcnm,
119 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno);
121 +/* datamodel specific implementation of helper functions */
122 +static ssize_t pydb_strobj_readdata_native(pydb_agent_t *py, uintptr_t addr,
123 + unsigned char *buf, size_t buf_len);
124 +static int pydb_frameinfo_native(pydb_agent_t *py, uintptr_t addr, char *funcnm,
125 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno);
128 +static ssize_t pydb_strobj_readdata_32(pydb_agent_t *py, uintptr_t addr,
129 + unsigned char *buf, size_t buf_len);
130 +static int pydb_frameinfo_32(pydb_agent_t *py, uintptr_t addr, char *funcnm,
131 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno);
134 +static ssize_t pydb_strobj_readstr(pydb_agent_t *py, uintptr_t addr, char *buf,
137 +/* Iterator function next routines. Plugable, configured by iterator init */
138 +static uintptr_t pydb_frame_iter_next(pydb_iter_t *iter);
139 +static uintptr_t pydb_interp_iter_next(pydb_iter_t *iter);
140 +static uintptr_t pydb_thread_iter_next(pydb_iter_t *iter);
142 +static const char *strbasename(const char *s);
144 +static const pydb_arch_ops_t arch_ops_native = {
145 + .frameinfo = pydb_frameinfo_native,
146 + .strobj_readdata = pydb_strobj_readdata_native,
150 +static const pydb_arch_ops_t arch_ops_32 = {
151 + .frameinfo = pydb_frameinfo_32,
152 + .strobj_readdata = pydb_strobj_readdata_32,
157 +strbasename(const char *s)
159 + const char *p = strrchr(s, '/');
167 +/* Agent creation / destruction routines */
170 +pydb_agent_create(struct ps_prochandle *P, int vers)
175 + if (vers != PYDB_VERSION) {
180 + if (ps_pdmodel(P, &datamodel) != PS_OK) {
184 + py = (pydb_agent_t *)malloc(sizeof (pydb_agent_t));
190 + py->pdb_vers = vers;
191 + py->pdb_datamodel = datamodel;
192 + py->pdb_is_64bit = 0;
193 + py->pdb_ops = &arch_ops_native;
196 + py->pdb_is_64bit = (datamodel == PR_MODEL_LP64);
197 + if (!py->pdb_is_64bit) {
198 + py->pdb_ops = &arch_ops_32;
206 +pydb_agent_destroy(pydb_agent_t *py)
215 +/* Helper functions */
217 +pydb_getlno(pydb_agent_t *py, uintptr_t lnotab_addr, int firstline,
220 + unsigned char lnotab[4096];
221 + ssize_t lnotab_len;
225 + lnotab_len = pydb_strobj_readdata(py, lnotab_addr, lnotab,
227 + if (lnotab_len < 0) {
232 + * Python's line number algorithm is arcane. See here for details:
233 + * http://svn.python.org/projects/python/trunk/Objects/lnotab_notes.txt
237 + for (addr = i = 0; i < lnotab_len; i += 2) {
238 + if (addr + lnotab[i] > lastinst) {
242 + line += lnotab[i + 1];
249 +pydb_strobj_readdata(pydb_agent_t *py, uintptr_t addr, unsigned char *buf,
252 + return (py->pdb_ops->strobj_readdata(py, addr, buf, buf_len));
256 +pydb_strobj_readdata_native(pydb_agent_t *py, uintptr_t addr,
257 + unsigned char *buf, size_t buf_len)
259 + PyStringObject sobj;
265 + * PyStringObjects are variable size. The size of the PyStringObject
266 + * struct is fixed, and known at compile time; however, the size of the
267 + * associated buffer is variable. The char[1] element at the end of the
268 + * structure contains the string, and the ob_size of the PyStringObject
269 + * indicates how much extra space was allocated to contain the string
270 + * buffer at the object's tail. Read in the fixed size portion of the
271 + * object first, and then read the contents of the data buffer into the
272 + * buffer passed by the caller.
275 + if (ps_pread(py->pdb_ph, addr, &sobj, sizeof (PyStringObject))
280 + obj_sz = (ssize_t)PyString_GET_SIZE(&sobj);
282 + read_sz = MIN(obj_sz, (ssize_t)buf_len);
283 + straddr = (psaddr_t)(addr + offsetof(PyStringObject, ob_sval));
285 + if (ps_pread(py->pdb_ph, straddr, buf, (size_t)read_sz) != PS_OK) {
294 +pydb_strobj_readdata_32(pydb_agent_t *py, uintptr_t addr,
295 + unsigned char *buf, size_t buf_len)
297 + PyStringObject32 sobj;
303 + * PyStringObjects are variable size. The size of the PyStringObject
304 + * struct is fixed, and known at compile time; however, the size of the
305 + * associated buffer is variable. The char[1] element at the end of the
306 + * structure contains the string, and the ob_size of the PyStringObject
307 + * indicates how much extra space was allocated to contain the string
308 + * buffer at the object's tail. Read in the fixed size portion of the
309 + * object first, and then read the contents of the data buffer into the
310 + * buffer passed by the caller.
313 + if (ps_pread(py->pdb_ph, addr, &sobj, sizeof (PyStringObject32))
318 + obj_sz = (ssize_t)PyString_GET_SIZE32(&sobj);
320 + read_sz = MIN(obj_sz, (ssize_t)buf_len);
321 + straddr = (psaddr_t)(addr + offsetof(PyStringObject32, ob_sval));
323 + if (ps_pread(py->pdb_ph, straddr, buf, (size_t)read_sz) != PS_OK) {
332 + * Most Python PyStringObjects contain strings, as one would expect. However,
333 + * due to some sleazy hackery in parts of the Python code, some string objects
334 + * are used as buffers for binary data. In the general case,
335 + * pydb_strobj_readstr() should be used to read strings out of string objects.
336 + * It wraps pydb_strobj_readdata(), which should be used by callers only when
337 + * trying to retrieve binary data. (This routine does some string cleanup).
340 +pydb_strobj_readstr(pydb_agent_t *py, uintptr_t addr, char *buf,
345 + read_sz = pydb_strobj_readdata(py, addr, (unsigned char *)buf, buf_len);
347 + if (read_sz >= 0) {
348 + if (read_sz >= buf_len) {
349 + read_sz = buf_len - 1;
352 + buf[read_sz] = '\0';
360 +pydb_frameinfo(pydb_agent_t *py, uintptr_t addr, char *funcnm,
361 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno)
363 + return (py->pdb_ops->frameinfo(py, addr, funcnm, funcnm_sz,
364 + filenm, filenm_sz, lineno));
368 +pydb_frameinfo_native(pydb_agent_t *py, uintptr_t addr, char *funcnm,
369 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno)
375 + if (ps_pread(py->pdb_ph, addr, &fo, sizeof (PyFrameObject))
380 + if (ps_pread(py->pdb_ph, (uintptr_t)fo.f_code, &co,
381 + sizeof (PyCodeObject)) != PS_OK) {
385 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_name, funcnm, funcnm_sz);
390 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_filename, filenm,
396 + *lineno = pydb_getlno(py, (uintptr_t)co.co_lnotab, co.co_firstlineno,
407 +pydb_frameinfo_32(pydb_agent_t *py, uintptr_t addr, char *funcnm,
408 + size_t funcnm_sz, char *filenm, size_t filenm_sz, int *lineno)
410 + PyFrameObject32 fo;
414 + if (ps_pread(py->pdb_ph, addr, &fo, sizeof (PyFrameObject32))
419 + if (ps_pread(py->pdb_ph, (uintptr_t)fo.f_code, &co,
420 + sizeof (PyCodeObject32)) != PS_OK) {
424 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_name, funcnm, funcnm_sz);
429 + rc = pydb_strobj_readstr(py, (uintptr_t)co.co_filename, filenm,
435 + *lineno = pydb_getlno(py, (uintptr_t)co.co_lnotab, co.co_firstlineno,
446 +/* Functions that are part of the library's interface */
449 + * Given the address of a PyFrameObject, and a buffer of a known size,
450 + * fill the buffer with a description of the frame.
453 +pydb_get_frameinfo(pydb_agent_t *py, uintptr_t frame_addr, char *fbuf,
454 + size_t bufsz, int verbose)
456 + char funcname[1024];
457 + char filename[1024];
460 + int length = (py->pdb_is_64bit ? 16 : 8);
463 + rc = pydb_frameinfo(py, frame_addr, funcname, sizeof (funcname),
464 + filename, sizeof (filename), &lineno);
470 + fn = (char *)strbasename(filename);
475 + (void) snprintf(fbuf, bufsz, "%0.*lx %s:%d %s()\n", length,
476 + frame_addr, fn, lineno, funcname);
482 + * Return a description about a PyFrameObject, if the object is
483 + * actually a PyFrameObject. In this case, the pc argument is checked
484 + * to make sure that it came from a function that takes a PyFrameObject
485 + * as its first (argv[0]) argument.
488 +pydb_pc_frameinfo(pydb_agent_t *py, uintptr_t pc, uintptr_t frame_addr,
489 + char *fbuf, size_t bufsz)
491 + char funcname[1024];
492 + char filename[1024];
498 + * If PC doesn't match PyEval_EvalFrameEx in either libpython
499 + * or the executable, don't decode it.
501 + if (ps_pglobal_sym(py->pdb_ph, LIBPYTHON, "PyEval_EvalFrameEx", &psym)
506 + /* If symbol found, ensure that PC falls within PyEval_EvalFrameEx. */
507 + if (pc < psym.st_value || pc > psym.st_value + psym.st_size) {
511 + rc = pydb_frameinfo(py, frame_addr, funcname, sizeof (funcname),
512 + filename, sizeof (filename), &lineno);
517 + (void) snprintf(fbuf, bufsz, "[ %s:%d (%s) ]\n", filename, lineno,
524 + * Walks the list of PyInterpreterState objects. If caller doesn't
525 + * supply address of list, this method will look it up.
528 +pydb_interp_iter_init(pydb_agent_t *py, uintptr_t addr)
535 + rc = ps_pglobal_lookup(py->pdb_ph, LIBPYTHON, "interp_head",
536 + (psaddr_t *)&addr);
542 + if (ps_pread(py->pdb_ph, (uintptr_t)addr, &i_addr, sizeof (uintptr_t))
547 + itr = malloc(sizeof (pydb_iter_t));
552 + itr->pdi_ph = py->pdb_ph;
553 + itr->pdi_current = i_addr;
554 + itr->pdi_nextf = pydb_interp_iter_next;
560 +pydb_interp_iter_next(pydb_iter_t *iter)
562 + PyInterpreterState st;
565 + cur = iter->pdi_current;
571 + if (ps_pread(iter->pdi_ph, cur, &st, sizeof (PyInterpreterState))
573 + iter->pdi_current = 0;
577 + iter->pdi_current = (uintptr_t)st.next;
583 + * Walk a list of Python PyFrameObjects. The addr argument must be
584 + * the address of a valid PyThreadState object.
587 +pydb_frame_iter_init(pydb_agent_t *py, uintptr_t addr)
592 + if (ps_pread(py->pdb_ph, (uintptr_t)addr, &ts, sizeof (PyThreadState))
597 + itr = malloc(sizeof (pydb_iter_t));
602 + itr->pdi_ph = py->pdb_ph;
603 + itr->pdi_current = (uintptr_t)ts.frame;
604 + itr->pdi_nextf = pydb_frame_iter_next;
610 +pydb_frame_iter_next(pydb_iter_t *iter)
615 + cur = iter->pdi_current;
621 + if (ps_pread(iter->pdi_ph, cur, &fo, sizeof (PyFrameObject))
623 + iter->pdi_current = 0;
627 + iter->pdi_current = (uintptr_t)fo.f_back;
633 + * Walk a list of Python PyThreadState objects. The addr argument must be
634 + * the address of a valid PyInterpreterState object.
637 +pydb_thread_iter_init(pydb_agent_t *py, uintptr_t addr)
640 + PyInterpreterState is;
642 + if (ps_pread(py->pdb_ph, (uintptr_t)addr, &is,
643 + sizeof (PyInterpreterState)) != PS_OK) {
647 + itr = malloc(sizeof (pydb_iter_t));
652 + itr->pdi_ph = py->pdb_ph;
653 + itr->pdi_current = (uintptr_t)is.tstate_head;
654 + itr->pdi_nextf = pydb_thread_iter_next;
660 +pydb_thread_iter_next(pydb_iter_t *iter)
665 + cur = iter->pdi_current;
671 + if (ps_pread(iter->pdi_ph, cur, &ts, sizeof (PyThreadState)) != PS_OK) {
672 + iter->pdi_current = 0;
676 + iter->pdi_current = (uintptr_t)ts.next;
683 +pydb_iter_next(pydb_iter_t *iter)
685 + return (iter->pdi_nextf(iter));
689 +pydb_iter_fini(pydb_iter_t *iter)
691 + if (iter == NULL) {
697 diff --git Python-2.7.1/py_db/libpython27_db.h Python-2.7.1/py_db/libpython27_db.h
700 +++ Python-2.7.1/py_db/libpython27_db.h
703 + * CDDL HEADER START
705 + * The contents of this file are subject to the terms of the
706 + * Common Development and Distribution License (the "License").
707 + * You may not use this file except in compliance with the License.
709 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
710 + * or http://www.opensolaris.org/os/licensing.
711 + * See the License for the specific language governing permissions
712 + * and limitations under the License.
714 + * When distributing Covered Code, include this CDDL HEADER in each
715 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
716 + * If applicable, add the following below this CDDL HEADER, with the
717 + * fields enclosed by brackets "[]" replaced with your own identifying
718 + * information: Portions Copyright [yyyy] [name of copyright owner]
723 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
726 +#ifndef _LIBPYTHON27_DB_H
727 +#define _LIBPYTHON27_DB_H
729 +#include <proc_service.h>
735 +/* Agent is opaque to library's consumers. */
736 +typedef struct pydb_agent pydb_agent_t;
739 + * Library's debug version is 1. Changes to interface should increase this
742 +#define PYDB_VERSION 1
744 +/* Agent creation/destruction routines */
745 +extern pydb_agent_t *pydb_agent_create(struct ps_prochandle *P, int vers);
746 +extern void pydb_agent_destroy(pydb_agent_t *py);
748 +/* Used by callers that know they are looking at a PyFrameObject */
749 +extern int pydb_get_frameinfo(pydb_agent_t *py, uintptr_t frame_addr,
750 + char *fbuf, size_t bufsz, int verbose);
753 + * Used by callers that don't know if they're looking at PyFrameObject.
754 + * Checks PC for traceable functions.
756 +extern int pydb_pc_frameinfo(pydb_agent_t *py, uintptr_t pc,
757 + uintptr_t frame_addr, char *fbuf, size_t bufsz);
759 +/* Iterator functions */
760 +typedef struct pydb_iter pydb_iter_t;
762 +extern pydb_iter_t *pydb_frame_iter_init(pydb_agent_t *py, uintptr_t addr);
763 +extern pydb_iter_t *pydb_interp_iter_init(pydb_agent_t *py,
765 +extern pydb_iter_t *pydb_thread_iter_init(pydb_agent_t *py,
767 +extern void pydb_iter_fini(pydb_iter_t *iter);
768 +extern uintptr_t pydb_iter_next(pydb_iter_t *iter);
774 +#endif /* _LIBPYTHON27_DB_H */
775 diff --git Python-2.7.1/py_db/libpython27_db_32.h Python-2.7.1/py_db/libpython27_db_32.h
778 +++ Python-2.7.1/py_db/libpython27_db_32.h
781 + * CDDL HEADER START
783 + * The contents of this file are subject to the terms of the
784 + * Common Development and Distribution License (the "License").
785 + * You may not use this file except in compliance with the License.
787 + * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
788 + * or http://www.opensolaris.org/os/licensing.
789 + * See the License for the specific language governing permissions
790 + * and limitations under the License.
792 + * When distributing Covered Code, include this CDDL HEADER in each
793 + * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
794 + * If applicable, add the following below this CDDL HEADER, with the
795 + * fields enclosed by brackets "[]" replaced with your own identifying
796 + * information: Portions Copyright [yyyy] [name of copyright owner]
801 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
804 +#ifndef _LIBPYTHON27_DB_32_H
805 +#define _LIBPYTHON27_DB_32_H
811 +#include <sys/types.h>
814 + * Define 32-bit Python data structures for use by the 64-bit debugger. This
815 + * is so that a 64-bit debugger may properly examine a 32-bit process.
817 + * In many cases, the debug library is only concerned with a few fields in the
818 + * Python structure. In that case, the other ancillary fields are elided.
821 +typedef uint32_t uintptr32_t;
822 +typedef int32_t Py_ssize32_t;
824 +typedef struct _is32 {
826 + uintptr32_t tstate_head;
827 +} PyInterpreterState32;
829 +typedef struct _ts32 {
831 + uintptr32_t interp;
835 +#define PyObject_HEAD32 \
836 + Py_ssize32_t ob_refcnt; \
837 + uintptr32_t ob_type;
839 +#define PyObject_VAR_HEAD32 \
841 + Py_ssize32_t ob_size;
848 + PyObject_VAR_HEAD32
852 + PyObject_VAR_HEAD32
858 +#define Py_SIZE32(ob) (((PyVarObject32*)(ob))->ob_size)
859 +#define PyString_GET_SIZE32(op) Py_SIZE32(op)
860 +#define PyString_AS_STRING32(op) (((PyStringObject32 *)(op))->ob_sval)
863 + PyObject_VAR_HEAD32
864 + uintptr32_t f_back;
865 + uintptr32_t f_code;
866 + uintptr32_t f_builtins;
867 + uintptr32_t f_globals;
868 + uintptr32_t f_locals;
869 + uintptr32_t f_valuestack;
870 + uintptr32_t f_stacktop;
871 + uintptr32_t f_trace;
872 + uintptr32_t f_exc_typpe, f_exc_value, f_exc_traceback;
873 + uintptr32_t f_tstate;
884 + uintptr32_t co_code;
885 + uintptr32_t co_consts;
886 + uintptr32_t co_names;
887 + uintptr32_t co_varnames;
888 + uintptr32_t co_freevars;
889 + uintptr32_t co_cellvars;
890 + uintptr32_t co_filename;
891 + uintptr32_t co_name;
892 + int co_firstlineno;
893 + uintptr32_t co_lnotab;
900 +#endif /* _LIBPYTHON27_DB_32_H */
901 diff --git Python-2.7.1/py_db/mapfile-vers Python-2.7.1/py_db/mapfile-vers
904 +++ Python-2.7.1/py_db/mapfile-vers
909 +# The contents of this file are subject to the terms of the
910 +# Common Development and Distribution License (the "License").
911 +# You may not use this file except in compliance with the License.
913 +# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
914 +# or http://www.opensolaris.org/os/licensing.
915 +# See the License for the specific language governing permissions
916 +# and limitations under the License.
918 +# When distributing Covered Code, include this CDDL HEADER in each
919 +# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
920 +# If applicable, add the following below this CDDL HEADER, with the
921 +# fields enclosed by brackets "[]" replaced with your own identifying
922 +# information: Portions Copyright [yyyy] [name of copyright owner]
928 +# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
934 + pydb_agent_destroy;
935 + pydb_frame_iter_init;
936 + pydb_get_frameinfo;
938 + pydb_interp_iter_init;
939 + pydb_thread_iter_init;