/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <mdb/mdb_modapi.h>
#include <pthread.h>
#include <stddef.h>
#include <dlfcn.h>
#include <link.h>
#include <libproc.h>
#include <Python.h>
#include <frameobject.h>
/*
* Decoding Python Stack Frames
* ============================
*
* Python2 uses a variety of objects to construct its call chain. An address
* space may have one or more PyInterpreterState objects, which are the base
* object in the interpreter's state. These objects are kept in a linked list
* with a head pointer named interp_head. This makes it possible for the
* debugger to get a toehold on data structures necessary to understand the
* interpreter. Since most of these structures are linked out of the
* InterpreterState, traversals generally start here.
*
* In order to decode a frame, the debugger needs to walk from
* PyInterpreterState down to a PyCodeObject. The diagram below shows the
* the objects that must be examined in order to reach a leaf PyCodeObject.
*
* +--------------------+ next +--------------------+ next
* interp_head -> | PyInterpreterState | ----> | PyInterpreterState | ---> ...
* +--------------------+ +--------------------+
* | | tstate_head
* | tstate_head V
* | +---------------+ frame
* V | PyThreadState | -----> ...
* +---------------+ frame +---------------+
* | PyThreadState | ---> ...
* +---------------+
* | next
* V
* +---------------+ frame +---------------+ f_back +---------------+
* | PyThreadState | ------> | PyFrameObject | -----> | PyFrameObject |
* +---------------+ +---------------+ +---------------+
* | |
* | f_code | f_code
* V V
* +--------------+ ...
* | PyCodeObject |
* +--------------+
* co_filename | | | co_lnotab
* +-------------+ | +-------------+
* | co_name | |
* V V V
* +----------------+ +----------------+ +----------------+
* | PyStringObject | | PyStringObject | | PyStringObject |
* +----------------+ +----------------+ +----------------+
*
* The interp_head pointer is a list of one or more PyInterpreterState
* objects. Each of these objects can contain one or more PyThreadState
* objects. The PyInterpreterState object keeps a pointer to the head of the
* list of PyThreadState objects as tstate_head.
*
* Each thread keeps ahold of its stack frames. The PyThreadState object
* has a pointer to the topmost PyFrameObject, kept in frame. The
* successive frames on the stack are kept linked in the PyFrameObject's
* f_back pointer, with each frame pointing to its caller.
*
* In order to decode each call frame, our code needs to look at the
* PyCodeObject for each frame. Essentially, this is the code that is
* being executed in the frame. The PyFrameObject keeps a pointer to this
* code object in f_code. In order to print meaningful debug information,
* it's necessary to extract the Python filename (co_filename), the
* function name (co_name), and the line number within the file
* (co_lnotab). The filename and function are stored as strings, but the
* line number is a mapping of bytecode offsets to line numbers. The
* description of the lnotab algorithm lives here:
*
*
* In order to decode the frame, the debugger needs to walk each
* InterpreterState object. For each InterpreterState, every PyThreadState
* must be traversed. The PyThreadState objects point to the
* PyFrameObjects. For every thread, we must walk the frames backwards and
* decode the strings that are in the PyCodeObjects.
*/
/*
* The Python-dependent debugging functionality lives in its own helper
* library. The helper agent is provided by libpython2.[67]_db.so, which
* is also used by pstack(1) for debugging Python processes.
*
* Define needed prototypes here.
*/
/*ARGSUSED*/
static int
{
return (DCMD_USAGE);
}
if (flags & DCMD_PIPE_OUT) {
mdb_warn("py_stack cannot output into a pipe\n");
return (DCMD_ERR);
}
if (!(flags & DCMD_ADDRSPEC)) {
mdb_warn("no address");
return (DCMD_USAGE);
}
verbose) < 0) {
return (DCMD_ERR);
}
return (DCMD_OK);
}
int
{
mdb_warn("unable to create interpreter iterator\n");
return (DCMD_ERR);
}
return (WALK_NEXT);
}
int
{
int status;
return (WALK_DONE);
}
return (status);
}
void
{
}
int
{
mdb_warn("unable to create thread iterator\n");
return (DCMD_ERR);
}
return (WALK_NEXT);
}
int
{
mdb_warn("unable to create frame iterator\n");
return (DCMD_ERR);
}
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
{
/*
* Pass the ThreadState to the frame walker. Have frame walker
* call frame dcmd.
*/
mdb_warn("can't walk 'pyframe'");
return (WALK_ERR);
}
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
{
/*
* Pass the InterpreterState to the threadstate walker.
*/
addr) == -1) {
mdb_warn("can't walk 'pythread'");
return (WALK_ERR);
}
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
{
return (DCMD_USAGE);
if (flags & DCMD_PIPE_OUT) {
mdb_warn("py_stack cannot output into a pipe\n");
return (DCMD_ERR);
}
if (flags & DCMD_ADDRSPEC) {
== -1) {
mdb_warn("can't walk 'pyframe'");
return (DCMD_ERR);
}
return (DCMD_OK);
}
&verbose) == -1) {
mdb_warn("can't walk 'pyinterp'");
return (DCMD_ERR);
}
return (DCMD_OK);
}
{ NULL }
};
{ "pyinterp", "walk python interpreter structures",
{ "pythread", "given an interpreter, walk the list of python threads",
{ "pyframe", "given a thread state, walk the list of frame objects",
{ NULL }
};
};
/*ARGSUSED*/
static int
{
char *name;
if (name) {
s2 += 3;
}
s2 += 3;
return (1);
}
return (0);
}
static int
python_db_init(void)
{
mdb_warn("couldn't read pshandle xdata\n");
pydb_dlhdl = NULL;
return (-1);
}
mdb_warn("couldn't load pydb functions");
pydb_dlhdl = NULL;
return (-1);
}
mdb_warn("unable to create pydb_agent");
pydb_dlhdl = NULL;
return (-1);
}
return (0);
}
static void
python_db_fini(void)
{
if (pydb_dlhdl) {
pydb_dlhdl = NULL;
}
}
const mdb_modinfo_t *
_mdb_init(void)
{
if (python_db_init() != 0)
return (NULL);
return (&modinfo);
}
void
_mdb_fini(void)
{
}