Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Traceback implementation */ |
| 3 | |
Guido van Rossum | 65bf9f2 | 1997-04-29 18:33:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 7 | #include "frameobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #include "structmember.h" |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 9 | #include "osdefs.h" |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 10 | #ifdef HAVE_FCNTL_H |
| 11 | #include <fcntl.h> |
| 12 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 14 | #define OFF(x) offsetof(PyTracebackObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | |
Victor Stinner | 84bb1cf | 2013-05-17 00:12:04 +0200 | [diff] [blame] | 16 | #define PUTS(fd, str) write(fd, str, (int)strlen(str)) |
Victor Stinner | 54f939b | 2012-07-30 13:08:58 +0200 | [diff] [blame] | 17 | #define MAX_STRING_LENGTH 500 |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 18 | #define MAX_FRAME_DEPTH 100 |
| 19 | #define MAX_NTHREADS 100 |
| 20 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 21 | /* Function from Parser/tokenizer.c */ |
| 22 | extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 23 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 24 | _Py_IDENTIFIER(TextIOWrapper); |
| 25 | _Py_IDENTIFIER(close); |
| 26 | _Py_IDENTIFIER(open); |
| 27 | _Py_IDENTIFIER(path); |
| 28 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 29 | static PyObject * |
| 30 | tb_dir(PyTracebackObject *self) |
| 31 | { |
| 32 | return Py_BuildValue("[ssss]", "tb_frame", "tb_next", |
| 33 | "tb_lasti", "tb_lineno"); |
| 34 | } |
| 35 | |
| 36 | static PyMethodDef tb_methods[] = { |
| 37 | {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, |
| 38 | {NULL, NULL, 0, NULL}, |
| 39 | }; |
| 40 | |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 41 | static PyMemberDef tb_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | {"tb_next", T_OBJECT, OFF(tb_next), READONLY}, |
| 43 | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
| 44 | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
| 45 | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 46 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 50 | tb_dealloc(PyTracebackObject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 51 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | PyObject_GC_UnTrack(tb); |
| 53 | Py_TRASHCAN_SAFE_BEGIN(tb) |
| 54 | Py_XDECREF(tb->tb_next); |
| 55 | Py_XDECREF(tb->tb_frame); |
| 56 | PyObject_GC_Del(tb); |
| 57 | Py_TRASHCAN_SAFE_END(tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 60 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 61 | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 62 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | Py_VISIT(tb->tb_next); |
| 64 | Py_VISIT(tb->tb_frame); |
| 65 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 69 | tb_clear(PyTracebackObject *tb) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 70 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | Py_CLEAR(tb->tb_next); |
| 72 | Py_CLEAR(tb->tb_frame); |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Tim Peters | d7c3652 | 2001-10-22 19:34:09 +0000 | [diff] [blame] | 75 | PyTypeObject PyTraceBack_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 77 | "traceback", |
| 78 | sizeof(PyTracebackObject), |
| 79 | 0, |
| 80 | (destructor)tb_dealloc, /*tp_dealloc*/ |
| 81 | 0, /*tp_print*/ |
| 82 | 0, /*tp_getattr*/ |
| 83 | 0, /*tp_setattr*/ |
| 84 | 0, /*tp_reserved*/ |
| 85 | 0, /*tp_repr*/ |
| 86 | 0, /*tp_as_number*/ |
| 87 | 0, /*tp_as_sequence*/ |
| 88 | 0, /*tp_as_mapping*/ |
| 89 | 0, /* tp_hash */ |
| 90 | 0, /* tp_call */ |
| 91 | 0, /* tp_str */ |
| 92 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 93 | 0, /* tp_setattro */ |
| 94 | 0, /* tp_as_buffer */ |
| 95 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 96 | 0, /* tp_doc */ |
| 97 | (traverseproc)tb_traverse, /* tp_traverse */ |
| 98 | (inquiry)tb_clear, /* tp_clear */ |
| 99 | 0, /* tp_richcompare */ |
| 100 | 0, /* tp_weaklistoffset */ |
| 101 | 0, /* tp_iter */ |
| 102 | 0, /* tp_iternext */ |
| 103 | tb_methods, /* tp_methods */ |
| 104 | tb_memberlist, /* tp_members */ |
| 105 | 0, /* tp_getset */ |
| 106 | 0, /* tp_base */ |
| 107 | 0, /* tp_dict */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 110 | static PyTracebackObject * |
| 111 | newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 112 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | PyTracebackObject *tb; |
| 114 | if ((next != NULL && !PyTraceBack_Check(next)) || |
| 115 | frame == NULL || !PyFrame_Check(frame)) { |
| 116 | PyErr_BadInternalCall(); |
| 117 | return NULL; |
| 118 | } |
| 119 | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 120 | if (tb != NULL) { |
| 121 | Py_XINCREF(next); |
| 122 | tb->tb_next = next; |
| 123 | Py_XINCREF(frame); |
| 124 | tb->tb_frame = frame; |
| 125 | tb->tb_lasti = frame->f_lasti; |
| 126 | tb->tb_lineno = PyFrame_GetLineNumber(frame); |
| 127 | PyObject_GC_Track(tb); |
| 128 | } |
| 129 | return tb; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 133 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | PyThreadState *tstate = PyThreadState_GET(); |
| 136 | PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback; |
| 137 | PyTracebackObject *tb = newtracebackobject(oldtb, frame); |
| 138 | if (tb == NULL) |
| 139 | return -1; |
| 140 | tstate->curexc_traceback = (PyObject *)tb; |
| 141 | Py_XDECREF(oldtb); |
| 142 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 145 | static PyObject * |
| 146 | _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io) |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 147 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 148 | Py_ssize_t i; |
| 149 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | PyObject *v; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 151 | Py_ssize_t npath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | size_t taillen; |
| 153 | PyObject *syspath; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 154 | PyObject *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | const char* tail; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 156 | PyObject *filebytes; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 157 | const char* filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | Py_ssize_t len; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 159 | PyObject* result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 160 | |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 161 | filebytes = PyUnicode_EncodeFSDefault(filename); |
| 162 | if (filebytes == NULL) { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 163 | PyErr_Clear(); |
| 164 | return NULL; |
| 165 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 166 | filepath = PyBytes_AS_STRING(filebytes); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 167 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | /* Search tail of filename in sys.path before giving up */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 169 | tail = strrchr(filepath, SEP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | if (tail == NULL) |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 171 | tail = filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | else |
| 173 | tail++; |
| 174 | taillen = strlen(tail); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 175 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 176 | syspath = _PySys_GetObjectId(&PyId_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (syspath == NULL || !PyList_Check(syspath)) |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 178 | goto error; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 179 | npath = PyList_Size(syspath); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | for (i = 0; i < npath; i++) { |
| 182 | v = PyList_GetItem(syspath, i); |
| 183 | if (v == NULL) { |
| 184 | PyErr_Clear(); |
| 185 | break; |
| 186 | } |
| 187 | if (!PyUnicode_Check(v)) |
| 188 | continue; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 189 | path = PyUnicode_EncodeFSDefault(v); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 190 | if (path == NULL) { |
| 191 | PyErr_Clear(); |
| 192 | continue; |
| 193 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 194 | len = PyBytes_GET_SIZE(path); |
| 195 | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { |
| 196 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | continue; /* Too long */ |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 198 | } |
| 199 | strcpy(namebuf, PyBytes_AS_STRING(path)); |
| 200 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | if (strlen(namebuf) != len) |
| 202 | continue; /* v contains '\0' */ |
| 203 | if (len > 0 && namebuf[len-1] != SEP) |
| 204 | namebuf[len++] = SEP; |
| 205 | strcpy(namebuf+len, tail); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 206 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 207 | binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb"); |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 208 | if (binary != NULL) { |
| 209 | result = binary; |
| 210 | goto finally; |
| 211 | } |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 212 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 214 | goto error; |
| 215 | |
| 216 | error: |
| 217 | result = NULL; |
| 218 | finally: |
| 219 | Py_DECREF(filebytes); |
| 220 | return result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 223 | int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 224 | _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | int err = 0; |
| 227 | int fd; |
| 228 | int i; |
| 229 | char *found_encoding; |
| 230 | char *encoding; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 231 | PyObject *io; |
| 232 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | PyObject *fob = NULL; |
| 234 | PyObject *lineobj = NULL; |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 235 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | char buf[MAXPATHLEN+1]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 237 | int kind; |
| 238 | void *data; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | /* open the file */ |
| 241 | if (filename == NULL) |
| 242 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 243 | |
| 244 | io = PyImport_ImportModuleNoBlock("io"); |
| 245 | if (io == NULL) |
| 246 | return -1; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 247 | binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb"); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 248 | |
| 249 | if (binary == NULL) { |
Victor Stinner | ceceaa0 | 2013-07-16 00:32:14 +0200 | [diff] [blame] | 250 | PyErr_Clear(); |
| 251 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 252 | binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); |
| 253 | if (binary == NULL) { |
| 254 | Py_DECREF(io); |
Victor Stinner | ceceaa0 | 2013-07-16 00:32:14 +0200 | [diff] [blame] | 255 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 256 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | /* use the right encoding to decode the file as unicode */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 260 | fd = PyObject_AsFileDescriptor(binary); |
Christian Heimes | 8c077bc | 2013-07-21 01:53:10 +0200 | [diff] [blame] | 261 | if (fd < 0) { |
| 262 | Py_DECREF(io); |
| 263 | Py_DECREF(binary); |
Benjamin Peterson | 04b01dc | 2013-07-21 13:26:13 -0700 | [diff] [blame] | 264 | return 0; |
Christian Heimes | 8c077bc | 2013-07-21 01:53:10 +0200 | [diff] [blame] | 265 | } |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 266 | found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); |
Victor Stinner | 5272fa9 | 2013-12-19 13:39:32 +0100 | [diff] [blame] | 267 | if (found_encoding == NULL) |
| 268 | PyErr_Clear(); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 269 | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; |
Christian Heimes | 1f34729 | 2013-07-21 02:12:35 +0200 | [diff] [blame] | 270 | /* Reset position */ |
| 271 | if (lseek(fd, 0, SEEK_SET) == (off_t)-1) { |
| 272 | Py_DECREF(io); |
| 273 | Py_DECREF(binary); |
| 274 | PyMem_FREE(found_encoding); |
Benjamin Peterson | 04b01dc | 2013-07-21 13:26:13 -0700 | [diff] [blame] | 275 | return 0; |
Christian Heimes | 1f34729 | 2013-07-21 02:12:35 +0200 | [diff] [blame] | 276 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 277 | fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 278 | Py_DECREF(io); |
| 279 | Py_DECREF(binary); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | PyMem_FREE(found_encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | if (fob == NULL) { |
| 283 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | return 0; |
| 285 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 287 | /* get the line number lineno */ |
| 288 | for (i = 0; i < lineno; i++) { |
| 289 | Py_XDECREF(lineobj); |
| 290 | lineobj = PyFile_GetLine(fob, -1); |
| 291 | if (!lineobj) { |
| 292 | err = -1; |
| 293 | break; |
| 294 | } |
| 295 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 296 | res = _PyObject_CallMethodId(fob, &PyId_close, ""); |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 297 | if (res) |
| 298 | Py_DECREF(res); |
| 299 | else |
| 300 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | Py_DECREF(fob); |
| 302 | if (!lineobj || !PyUnicode_Check(lineobj)) { |
| 303 | Py_XDECREF(lineobj); |
| 304 | return err; |
| 305 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | /* remove the indentation of the line */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 308 | kind = PyUnicode_KIND(lineobj); |
| 309 | data = PyUnicode_DATA(lineobj); |
| 310 | for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) { |
| 311 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 312 | if (ch != ' ' && ch != '\t' && ch != '\014') |
| 313 | break; |
| 314 | } |
| 315 | if (i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | PyObject *truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 317 | truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | if (truncated) { |
| 319 | Py_DECREF(lineobj); |
| 320 | lineobj = truncated; |
| 321 | } else { |
| 322 | PyErr_Clear(); |
| 323 | } |
| 324 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | /* Write some spaces before the line */ |
| 327 | strcpy(buf, " "); |
| 328 | assert (strlen(buf) == 10); |
| 329 | while (indent > 0) { |
Benjamin Peterson | 0f9b7d3 | 2013-07-21 13:29:37 -0700 | [diff] [blame] | 330 | if (indent < 10) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | buf[indent] = '\0'; |
| 332 | err = PyFile_WriteString(buf, f); |
| 333 | if (err != 0) |
| 334 | break; |
| 335 | indent -= 10; |
| 336 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | /* finally display the line */ |
| 339 | if (err == 0) |
| 340 | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
| 341 | Py_DECREF(lineobj); |
| 342 | if (err == 0) |
| 343 | err = PyFile_WriteString("\n", f); |
| 344 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 347 | static int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 348 | tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 349 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 350 | int err; |
| 351 | PyObject *line; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | if (filename == NULL || name == NULL) |
| 354 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 355 | line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n", |
| 356 | filename, lineno, name); |
| 357 | if (line == NULL) |
| 358 | return -1; |
| 359 | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
| 360 | Py_DECREF(line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | if (err != 0) |
| 362 | return err; |
Kristján Valur Jónsson | c5963d3 | 2012-07-19 21:02:03 +0000 | [diff] [blame] | 363 | /* ignore errors since we can't report them, can we? */ |
| 364 | if (_Py_DisplaySourceLine(f, filename, lineno, 4)) |
| 365 | PyErr_Clear(); |
| 366 | return err; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static int |
| 370 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | int err = 0; |
| 373 | long depth = 0; |
| 374 | PyTracebackObject *tb1 = tb; |
| 375 | while (tb1 != NULL) { |
| 376 | depth++; |
| 377 | tb1 = tb1->tb_next; |
| 378 | } |
| 379 | while (tb != NULL && err == 0) { |
| 380 | if (depth <= limit) { |
| 381 | err = tb_displayline(f, |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 382 | tb->tb_frame->f_code->co_filename, |
| 383 | tb->tb_lineno, |
| 384 | tb->tb_frame->f_code->co_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | } |
| 386 | depth--; |
| 387 | tb = tb->tb_next; |
| 388 | if (err == 0) |
| 389 | err = PyErr_CheckSignals(); |
| 390 | } |
| 391 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 394 | #define PyTraceBack_LIMIT 1000 |
| 395 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 396 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 397 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 398 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | int err; |
| 400 | PyObject *limitv; |
| 401 | long limit = PyTraceBack_LIMIT; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | if (v == NULL) |
| 404 | return 0; |
| 405 | if (!PyTraceBack_Check(v)) { |
| 406 | PyErr_BadInternalCall(); |
| 407 | return -1; |
| 408 | } |
| 409 | limitv = PySys_GetObject("tracebacklimit"); |
| 410 | if (limitv) { |
| 411 | PyObject *exc_type, *exc_value, *exc_tb; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
| 414 | limit = PyLong_AsLong(limitv); |
| 415 | if (limit == -1 && PyErr_Occurred()) { |
| 416 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 417 | limit = PyTraceBack_LIMIT; |
| 418 | } |
| 419 | else { |
| 420 | Py_XDECREF(exc_type); |
| 421 | Py_XDECREF(exc_value); |
| 422 | Py_XDECREF(exc_tb); |
| 423 | return 0; |
| 424 | } |
| 425 | } |
| 426 | else if (limit <= 0) { |
| 427 | limit = PyTraceBack_LIMIT; |
| 428 | } |
| 429 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 430 | } |
| 431 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 432 | if (!err) |
| 433 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 434 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 435 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 436 | |
| 437 | /* Reverse a string. For example, "abcd" becomes "dcba". |
| 438 | |
| 439 | This function is signal safe. */ |
| 440 | |
| 441 | static void |
| 442 | reverse_string(char *text, const size_t len) |
| 443 | { |
| 444 | char tmp; |
| 445 | size_t i, j; |
| 446 | if (len == 0) |
| 447 | return; |
| 448 | for (i=0, j=len-1; i < j; i++, j--) { |
| 449 | tmp = text[i]; |
| 450 | text[i] = text[j]; |
| 451 | text[j] = tmp; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /* Format an integer in range [0; 999999] to decimal, |
| 456 | and write it into the file fd. |
| 457 | |
| 458 | This function is signal safe. */ |
| 459 | |
| 460 | static void |
| 461 | dump_decimal(int fd, int value) |
| 462 | { |
| 463 | char buffer[7]; |
| 464 | int len; |
| 465 | if (value < 0 || 999999 < value) |
| 466 | return; |
| 467 | len = 0; |
| 468 | do { |
| 469 | buffer[len] = '0' + (value % 10); |
| 470 | value /= 10; |
| 471 | len++; |
| 472 | } while (value); |
| 473 | reverse_string(buffer, len); |
| 474 | write(fd, buffer, len); |
| 475 | } |
| 476 | |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 477 | /* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 478 | and write it into the file fd. |
| 479 | |
| 480 | This function is signal safe. */ |
| 481 | |
| 482 | static void |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 483 | dump_hexadecimal(int fd, unsigned long value, int width) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 484 | { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 485 | int len; |
| 486 | char buffer[sizeof(unsigned long) * 2 + 1]; |
| 487 | len = 0; |
| 488 | do { |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 489 | buffer[len] = Py_hexdigits[value & 15]; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 490 | value >>= 4; |
| 491 | len++; |
| 492 | } while (len < width || value); |
| 493 | reverse_string(buffer, len); |
| 494 | write(fd, buffer, len); |
| 495 | } |
| 496 | |
| 497 | /* Write an unicode object into the file fd using ascii+backslashreplace. |
| 498 | |
| 499 | This function is signal safe. */ |
| 500 | |
| 501 | static void |
| 502 | dump_ascii(int fd, PyObject *text) |
| 503 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 504 | PyASCIIObject *ascii = (PyASCIIObject *)text; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 505 | Py_ssize_t i, size; |
| 506 | int truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 507 | int kind; |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 508 | void *data = NULL; |
| 509 | wchar_t *wstr = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 510 | Py_UCS4 ch; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 511 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 512 | size = ascii->length; |
| 513 | kind = ascii->state.kind; |
| 514 | if (ascii->state.compact) { |
| 515 | if (ascii->state.ascii) |
| 516 | data = ((PyASCIIObject*)text) + 1; |
| 517 | else |
| 518 | data = ((PyCompactUnicodeObject*)text) + 1; |
| 519 | } |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 520 | else if (kind != PyUnicode_WCHAR_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 521 | data = ((PyUnicodeObject *)text)->data.any; |
| 522 | if (data == NULL) |
| 523 | return; |
| 524 | } |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 525 | else { |
| 526 | wstr = ((PyASCIIObject *)text)->wstr; |
| 527 | if (wstr == NULL) |
| 528 | return; |
| 529 | size = ((PyCompactUnicodeObject *)text)->wstr_length; |
| 530 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 531 | |
| 532 | if (MAX_STRING_LENGTH < size) { |
| 533 | size = MAX_STRING_LENGTH; |
| 534 | truncated = 1; |
| 535 | } |
| 536 | else |
| 537 | truncated = 0; |
| 538 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 539 | for (i=0; i < size; i++) { |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 540 | if (kind != PyUnicode_WCHAR_KIND) |
| 541 | ch = PyUnicode_READ(kind, data, i); |
| 542 | else |
| 543 | ch = wstr[i]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 544 | if (ch < 128) { |
| 545 | char c = (char)ch; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 546 | write(fd, &c, 1); |
| 547 | } |
Victor Stinner | 63ab875 | 2011-11-22 03:31:20 +0100 | [diff] [blame] | 548 | else if (ch < 0xff) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 549 | PUTS(fd, "\\x"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 550 | dump_hexadecimal(fd, ch, 2); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 551 | } |
Victor Stinner | 63ab875 | 2011-11-22 03:31:20 +0100 | [diff] [blame] | 552 | else if (ch < 0xffff) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 553 | PUTS(fd, "\\u"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 554 | dump_hexadecimal(fd, ch, 4); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 555 | } |
| 556 | else { |
| 557 | PUTS(fd, "\\U"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 558 | dump_hexadecimal(fd, ch, 8); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | if (truncated) |
| 562 | PUTS(fd, "..."); |
| 563 | } |
| 564 | |
| 565 | /* Write a frame into the file fd: "File "xxx", line xxx in xxx". |
| 566 | |
| 567 | This function is signal safe. */ |
| 568 | |
| 569 | static void |
| 570 | dump_frame(int fd, PyFrameObject *frame) |
| 571 | { |
| 572 | PyCodeObject *code; |
| 573 | int lineno; |
| 574 | |
| 575 | code = frame->f_code; |
| 576 | PUTS(fd, " File "); |
| 577 | if (code != NULL && code->co_filename != NULL |
| 578 | && PyUnicode_Check(code->co_filename)) |
| 579 | { |
| 580 | write(fd, "\"", 1); |
| 581 | dump_ascii(fd, code->co_filename); |
| 582 | write(fd, "\"", 1); |
| 583 | } else { |
| 584 | PUTS(fd, "???"); |
| 585 | } |
| 586 | |
| 587 | /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 588 | lineno = PyCode_Addr2Line(code, frame->f_lasti); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 589 | PUTS(fd, ", line "); |
| 590 | dump_decimal(fd, lineno); |
| 591 | PUTS(fd, " in "); |
| 592 | |
| 593 | if (code != NULL && code->co_name != NULL |
| 594 | && PyUnicode_Check(code->co_name)) |
| 595 | dump_ascii(fd, code->co_name); |
| 596 | else |
| 597 | PUTS(fd, "???"); |
| 598 | |
| 599 | write(fd, "\n", 1); |
| 600 | } |
| 601 | |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 602 | static void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 603 | dump_traceback(int fd, PyThreadState *tstate, int write_header) |
| 604 | { |
| 605 | PyFrameObject *frame; |
| 606 | unsigned int depth; |
| 607 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 608 | if (write_header) |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 609 | PUTS(fd, "Stack (most recent call first):\n"); |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 610 | |
| 611 | frame = _PyThreadState_GetFrame(tstate); |
| 612 | if (frame == NULL) |
| 613 | return; |
| 614 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 615 | depth = 0; |
| 616 | while (frame != NULL) { |
| 617 | if (MAX_FRAME_DEPTH <= depth) { |
| 618 | PUTS(fd, " ...\n"); |
| 619 | break; |
| 620 | } |
| 621 | if (!PyFrame_Check(frame)) |
| 622 | break; |
| 623 | dump_frame(fd, frame); |
| 624 | frame = frame->f_back; |
| 625 | depth++; |
| 626 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 629 | void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 630 | _Py_DumpTraceback(int fd, PyThreadState *tstate) |
| 631 | { |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 632 | dump_traceback(fd, tstate, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if |
| 636 | is_current is true, "Thread 0xHHHH:\n" otherwise. |
| 637 | |
| 638 | This function is signal safe. */ |
| 639 | |
| 640 | static void |
| 641 | write_thread_id(int fd, PyThreadState *tstate, int is_current) |
| 642 | { |
| 643 | if (is_current) |
| 644 | PUTS(fd, "Current thread 0x"); |
| 645 | else |
| 646 | PUTS(fd, "Thread 0x"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 647 | dump_hexadecimal(fd, (unsigned long)tstate->thread_id, sizeof(long)*2); |
| 648 | PUTS(fd, " (most recent call first):\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | const char* |
| 652 | _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp, |
| 653 | PyThreadState *current_thread) |
| 654 | { |
| 655 | PyThreadState *tstate; |
| 656 | unsigned int nthreads; |
| 657 | |
| 658 | /* Get the current interpreter from the current thread */ |
| 659 | tstate = PyInterpreterState_ThreadHead(interp); |
| 660 | if (tstate == NULL) |
| 661 | return "unable to get the thread head state"; |
| 662 | |
| 663 | /* Dump the traceback of each thread */ |
| 664 | tstate = PyInterpreterState_ThreadHead(interp); |
| 665 | nthreads = 0; |
| 666 | do |
| 667 | { |
| 668 | if (nthreads != 0) |
| 669 | write(fd, "\n", 1); |
| 670 | if (nthreads >= MAX_NTHREADS) { |
| 671 | PUTS(fd, "...\n"); |
| 672 | break; |
| 673 | } |
| 674 | write_thread_id(fd, tstate, tstate == current_thread); |
| 675 | dump_traceback(fd, tstate, 0); |
| 676 | tstate = PyThreadState_Next(tstate); |
| 677 | nthreads++; |
| 678 | } while (tstate != NULL); |
| 679 | |
| 680 | return NULL; |
| 681 | } |
| 682 | |