4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <mdb/mdb_modapi.h>
35 #include <frameobject.h>
38 * Decoding Python Stack Frames
39 * ============================
41 * Python2 uses a variety of objects to construct its call chain. An address
42 * space may have one or more PyInterpreterState objects, which are the base
43 * object in the interpreter's state. These objects are kept in a linked list
44 * with a head pointer named interp_head. This makes it possible for the
45 * debugger to get a toehold on data structures necessary to understand the
46 * interpreter. Since most of these structures are linked out of the
47 * InterpreterState, traversals generally start here.
49 * In order to decode a frame, the debugger needs to walk from
50 * PyInterpreterState down to a PyCodeObject. The diagram below shows the
51 * the objects that must be examined in order to reach a leaf PyCodeObject.
53 * +--------------------+ next +--------------------+ next
54 * interp_head -> | PyInterpreterState | ----> | PyInterpreterState | ---> ...
55 * +--------------------+ +--------------------+
58 * | +---------------+ frame
59 * V | PyThreadState | -----> ...
60 * +---------------+ frame +---------------+
61 * | PyThreadState | ---> ...
65 * +---------------+ frame +---------------+ f_back +---------------+
66 * | PyThreadState | ------> | PyFrameObject | -----> | PyFrameObject |
67 * +---------------+ +---------------+ +---------------+
71 * +--------------+ ...
74 * co_filename | | | co_lnotab
75 * +-------------+ | +-------------+
78 * +----------------+ +----------------+ +----------------+
79 * | PyStringObject | | PyStringObject | | PyStringObject |
80 * +----------------+ +----------------+ +----------------+
82 * The interp_head pointer is a list of one or more PyInterpreterState
83 * objects. Each of these objects can contain one or more PyThreadState
84 * objects. The PyInterpreterState object keeps a pointer to the head of the
85 * list of PyThreadState objects as tstate_head.
87 * Each thread keeps ahold of its stack frames. The PyThreadState object
88 * has a pointer to the topmost PyFrameObject, kept in frame. The
89 * successive frames on the stack are kept linked in the PyFrameObject's
90 * f_back pointer, with each frame pointing to its caller.
92 * In order to decode each call frame, our code needs to look at the
93 * PyCodeObject for each frame. Essentially, this is the code that is
94 * being executed in the frame. The PyFrameObject keeps a pointer to this
95 * code object in f_code. In order to print meaningful debug information,
96 * it's necessary to extract the Python filename (co_filename), the
97 * function name (co_name), and the line number within the file
98 * (co_lnotab). The filename and function are stored as strings, but the
99 * line number is a mapping of bytecode offsets to line numbers. The
100 * description of the lnotab algorithm lives here:
102 * http://svn.python.org/projects/python/trunk/Objects/lnotab_notes.txt
104 * In order to decode the frame, the debugger needs to walk each
105 * InterpreterState object. For each InterpreterState, every PyThreadState
106 * must be traversed. The PyThreadState objects point to the
107 * PyFrameObjects. For every thread, we must walk the frames backwards and
108 * decode the strings that are in the PyCodeObjects.
112 * The Python-dependent debugging functionality lives in its own helper
113 * library. The helper agent is provided by libpython2.[67]_db.so, which
114 * is also used by pstack(1) for debugging Python processes.
116 * Define needed prototypes here.
119 #define PYDB_VERSION 1
120 typedef struct pydb_agent pydb_agent_t
;
121 typedef struct pydb_iter pydb_iter_t
;
123 typedef pydb_agent_t
*(*pydb_agent_create_f
)(struct ps_prochandle
*P
, int vers
);
124 typedef void (*pydb_agent_destroy_f
)(pydb_agent_t
*py
);
125 typedef int (*pydb_get_frameinfo_f
)(pydb_agent_t
*py
, uintptr_t frame_addr
,
126 char *fbuf
, size_t bufsz
, int verbose
);
127 typedef pydb_iter_t
*(*pydb_iter_init_f
)(pydb_agent_t
*py
, uintptr_t addr
);
128 typedef uintptr_t (*pydb_iter_next_f
)(pydb_iter_t
*iter
);
129 typedef void (*pydb_iter_fini_f
)(pydb_iter_t
*iter
);
131 static pydb_agent_create_f pydb_agent_create
;
132 static pydb_agent_destroy_f pydb_agent_destroy
;
133 static pydb_get_frameinfo_f pydb_get_frameinfo
;
134 static pydb_iter_init_f pydb_frame_iter_init
;
135 static pydb_iter_init_f pydb_interp_iter_init
;
136 static pydb_iter_init_f pydb_thread_iter_init
;
137 static pydb_iter_next_f pydb_iter_next
;
138 static pydb_iter_fini_f pydb_iter_fini
;
140 static pydb_agent_t
*pydb_hdl
= NULL
;
141 static void *pydb_dlhdl
= NULL
;
145 py_frame(uintptr_t addr
, uint_t flags
, int argc
, const mdb_arg_t
*argv
)
150 if (mdb_getopts(argc
, argv
,
151 'v', MDB_OPT_SETBITS
, TRUE
, &verbose
,
156 if (flags
& DCMD_PIPE_OUT
) {
157 mdb_warn("py_stack cannot output into a pipe\n");
161 if (!(flags
& DCMD_ADDRSPEC
)) {
162 mdb_warn("no address");
166 if (pydb_get_frameinfo(pydb_hdl
, addr
, buf
, sizeof (buf
),
168 mdb_warn("Unable to find frame at address %p\n", addr
);
172 mdb_printf("%s", buf
);
178 py_interp_walk_init(mdb_walk_state_t
*wsp
)
182 pdi
= pydb_interp_iter_init(pydb_hdl
, wsp
->walk_addr
);
185 mdb_warn("unable to create interpreter iterator\n");
189 wsp
->walk_data
= pdi
;
195 py_walk_step(mdb_walk_state_t
*wsp
)
197 pydb_iter_t
*pdi
= wsp
->walk_data
;
201 addr
= pydb_iter_next(pdi
);
203 if (addr
== (uintptr_t)NULL
) {
207 status
= wsp
->walk_callback(addr
, 0, wsp
->walk_cbdata
);
213 py_walk_fini(mdb_walk_state_t
*wsp
)
215 pydb_iter_t
*pdi
= wsp
->walk_data
;
220 py_thread_walk_init(mdb_walk_state_t
*wsp
)
224 pdi
= pydb_thread_iter_init(pydb_hdl
, wsp
->walk_addr
);
226 mdb_warn("unable to create thread iterator\n");
230 wsp
->walk_data
= pdi
;
236 py_frame_walk_init(mdb_walk_state_t
*wsp
)
240 pdi
= pydb_frame_iter_init(pydb_hdl
, wsp
->walk_addr
);
242 mdb_warn("unable to create frame iterator\n");
246 wsp
->walk_data
= pdi
;
253 python_stack(uintptr_t addr
, const PyThreadState
*ts
, uint_t
*verbose
)
256 uint_t nargc
= (verbose
!= NULL
&& *verbose
) ? 1 : 0;
258 * Pass the ThreadState to the frame walker. Have frame walker
261 mdb_printf("PyThreadState: %0?p\n", addr
);
263 nargv
.a_type
= MDB_TYPE_STRING
;
264 nargv
.a_un
.a_str
= "-v";
266 if (mdb_pwalk_dcmd("pyframe", "pyframe", nargc
, &nargv
, addr
) == -1) {
267 mdb_warn("can't walk 'pyframe'");
276 python_thread(uintptr_t addr
, const PyInterpreterState
*is
, uint_t
*verbose
)
279 * Pass the InterpreterState to the threadstate walker.
281 if (mdb_pwalk("pythread", (mdb_walk_cb_t
)python_stack
, verbose
,
283 mdb_warn("can't walk 'pythread'");
292 py_stack(uintptr_t addr
, uint_t flags
, int argc
, const mdb_arg_t
*argv
)
294 uint_t verbose
= FALSE
;
296 if (mdb_getopts(argc
, argv
,
297 'v', MDB_OPT_SETBITS
, TRUE
, &verbose
,
301 if (flags
& DCMD_PIPE_OUT
) {
302 mdb_warn("py_stack cannot output into a pipe\n");
306 if (flags
& DCMD_ADDRSPEC
) {
308 uint_t nargc
= verbose
? 1 : 0;
310 nargv
.a_type
= MDB_TYPE_STRING
;
311 nargv
.a_un
.a_str
= "-v";
313 if (mdb_pwalk_dcmd("pyframe", "pyframe", nargc
, &nargv
, addr
)
315 mdb_warn("can't walk 'pyframe'");
321 if (mdb_walk("pyinterp", (mdb_walk_cb_t
)python_thread
,
323 mdb_warn("can't walk 'pyinterp'");
330 static const mdb_dcmd_t dcmds
[] = {
331 { "pystack", "[-v]", "print python stacks", py_stack
},
332 { "pyframe", "[-v]", "print python frames", py_frame
},
336 static const mdb_walker_t walkers
[] = {
337 { "pyinterp", "walk python interpreter structures",
338 py_interp_walk_init
, py_walk_step
, py_walk_fini
},
339 { "pythread", "given an interpreter, walk the list of python threads",
340 py_thread_walk_init
, py_walk_step
, py_walk_fini
},
341 { "pyframe", "given a thread state, walk the list of frame objects",
342 py_frame_walk_init
, py_walk_step
, py_walk_fini
},
346 static const mdb_modinfo_t modinfo
= {
347 MDB_API_VERSION
, dcmds
, walkers
352 python_object_iter(void *cd
, const prmap_t
*pmp
, const char *obj
)
357 struct ps_prochandle
*Pr
= cd
;
359 name
= strstr(obj
, "/libpython");
362 (void) strcpy(path
, obj
);
363 if (Pstatus(Pr
)->pr_dmodel
!= PR_MODEL_NATIVE
) {
365 s2
= path
+ (s1
- obj
);
366 (void) strcpy(s2
, "/64");
368 (void) strcpy(s2
, s1
);
371 s1
= strstr(obj
, ".so");
372 s2
= strstr(path
, ".so");
373 (void) strcpy(s2
, "_db");
375 (void) strcpy(s2
, s1
);
377 if ((pydb_dlhdl
= dlopen(path
, RTLD_LAZY
|RTLD_GLOBAL
)) != NULL
)
387 struct ps_prochandle
*Ph
;
389 if (mdb_get_xdata("pshandle", &Ph
, sizeof (Ph
)) == -1) {
390 mdb_warn("couldn't read pshandle xdata\n");
396 (void) Pobject_iter(Ph
, python_object_iter
, Ph
);
398 pydb_agent_create
= (pydb_agent_create_f
)
399 dlsym(pydb_dlhdl
, "pydb_agent_create");
400 pydb_agent_destroy
= (pydb_agent_destroy_f
)
401 dlsym(pydb_dlhdl
, "pydb_agent_destroy");
402 pydb_get_frameinfo
= (pydb_get_frameinfo_f
)
403 dlsym(pydb_dlhdl
, "pydb_get_frameinfo");
405 pydb_frame_iter_init
= (pydb_iter_init_f
)
406 dlsym(pydb_dlhdl
, "pydb_frame_iter_init");
407 pydb_interp_iter_init
= (pydb_iter_init_f
)
408 dlsym(pydb_dlhdl
, "pydb_interp_iter_init");
409 pydb_thread_iter_init
= (pydb_iter_init_f
)
410 dlsym(pydb_dlhdl
, "pydb_thread_iter_init");
411 pydb_iter_next
= (pydb_iter_next_f
)dlsym(pydb_dlhdl
, "pydb_iter_next");
412 pydb_iter_fini
= (pydb_iter_fini_f
)dlsym(pydb_dlhdl
, "pydb_iter_fini");
415 if (pydb_agent_create
== NULL
|| pydb_agent_destroy
== NULL
||
416 pydb_get_frameinfo
== NULL
|| pydb_frame_iter_init
== NULL
||
417 pydb_interp_iter_init
== NULL
|| pydb_thread_iter_init
== NULL
||
418 pydb_iter_next
== NULL
|| pydb_iter_fini
== NULL
) {
419 mdb_warn("couldn't load pydb functions");
425 pydb_hdl
= pydb_agent_create(Ph
, PYDB_VERSION
);
426 if (pydb_hdl
== NULL
) {
427 mdb_warn("unable to create pydb_agent");
440 pydb_agent_destroy(pydb_hdl
);
448 const mdb_modinfo_t
*
451 if (python_db_init() != 0)