| 1 | n/a | |
|---|
| 2 | n/a | /* Traceback implementation */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | #include "code.h" |
|---|
| 7 | n/a | #include "frameobject.h" |
|---|
| 8 | n/a | #include "structmember.h" |
|---|
| 9 | n/a | #include "osdefs.h" |
|---|
| 10 | n/a | #ifdef HAVE_FCNTL_H |
|---|
| 11 | n/a | #include <fcntl.h> |
|---|
| 12 | n/a | #endif |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | #define OFF(x) offsetof(PyTracebackObject, x) |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str)) |
|---|
| 17 | n/a | #define MAX_STRING_LENGTH 500 |
|---|
| 18 | n/a | #define MAX_FRAME_DEPTH 100 |
|---|
| 19 | n/a | #define MAX_NTHREADS 100 |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | /* Function from Parser/tokenizer.c */ |
|---|
| 22 | n/a | extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | _Py_IDENTIFIER(TextIOWrapper); |
|---|
| 25 | n/a | _Py_IDENTIFIER(close); |
|---|
| 26 | n/a | _Py_IDENTIFIER(open); |
|---|
| 27 | n/a | _Py_IDENTIFIER(path); |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | static PyObject * |
|---|
| 30 | n/a | tb_dir(PyTracebackObject *self) |
|---|
| 31 | n/a | { |
|---|
| 32 | n/a | return Py_BuildValue("[ssss]", "tb_frame", "tb_next", |
|---|
| 33 | n/a | "tb_lasti", "tb_lineno"); |
|---|
| 34 | n/a | } |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | static PyMethodDef tb_methods[] = { |
|---|
| 37 | n/a | {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, |
|---|
| 38 | n/a | {NULL, NULL, 0, NULL}, |
|---|
| 39 | n/a | }; |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | static PyMemberDef tb_memberlist[] = { |
|---|
| 42 | n/a | {"tb_next", T_OBJECT, OFF(tb_next), READONLY}, |
|---|
| 43 | n/a | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
|---|
| 44 | n/a | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
|---|
| 45 | n/a | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
|---|
| 46 | n/a | {NULL} /* Sentinel */ |
|---|
| 47 | n/a | }; |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | static void |
|---|
| 50 | n/a | tb_dealloc(PyTracebackObject *tb) |
|---|
| 51 | n/a | { |
|---|
| 52 | n/a | PyObject_GC_UnTrack(tb); |
|---|
| 53 | n/a | Py_TRASHCAN_SAFE_BEGIN(tb) |
|---|
| 54 | n/a | Py_XDECREF(tb->tb_next); |
|---|
| 55 | n/a | Py_XDECREF(tb->tb_frame); |
|---|
| 56 | n/a | PyObject_GC_Del(tb); |
|---|
| 57 | n/a | Py_TRASHCAN_SAFE_END(tb) |
|---|
| 58 | n/a | } |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | static int |
|---|
| 61 | n/a | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | Py_VISIT(tb->tb_next); |
|---|
| 64 | n/a | Py_VISIT(tb->tb_frame); |
|---|
| 65 | n/a | return 0; |
|---|
| 66 | n/a | } |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | static void |
|---|
| 69 | n/a | tb_clear(PyTracebackObject *tb) |
|---|
| 70 | n/a | { |
|---|
| 71 | n/a | Py_CLEAR(tb->tb_next); |
|---|
| 72 | n/a | Py_CLEAR(tb->tb_frame); |
|---|
| 73 | n/a | } |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | PyTypeObject PyTraceBack_Type = { |
|---|
| 76 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 77 | n/a | "traceback", |
|---|
| 78 | n/a | sizeof(PyTracebackObject), |
|---|
| 79 | n/a | 0, |
|---|
| 80 | n/a | (destructor)tb_dealloc, /*tp_dealloc*/ |
|---|
| 81 | n/a | 0, /*tp_print*/ |
|---|
| 82 | n/a | 0, /*tp_getattr*/ |
|---|
| 83 | n/a | 0, /*tp_setattr*/ |
|---|
| 84 | n/a | 0, /*tp_reserved*/ |
|---|
| 85 | n/a | 0, /*tp_repr*/ |
|---|
| 86 | n/a | 0, /*tp_as_number*/ |
|---|
| 87 | n/a | 0, /*tp_as_sequence*/ |
|---|
| 88 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 89 | n/a | 0, /* tp_hash */ |
|---|
| 90 | n/a | 0, /* tp_call */ |
|---|
| 91 | n/a | 0, /* tp_str */ |
|---|
| 92 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 93 | n/a | 0, /* tp_setattro */ |
|---|
| 94 | n/a | 0, /* tp_as_buffer */ |
|---|
| 95 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|---|
| 96 | n/a | 0, /* tp_doc */ |
|---|
| 97 | n/a | (traverseproc)tb_traverse, /* tp_traverse */ |
|---|
| 98 | n/a | (inquiry)tb_clear, /* tp_clear */ |
|---|
| 99 | n/a | 0, /* tp_richcompare */ |
|---|
| 100 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 101 | n/a | 0, /* tp_iter */ |
|---|
| 102 | n/a | 0, /* tp_iternext */ |
|---|
| 103 | n/a | tb_methods, /* tp_methods */ |
|---|
| 104 | n/a | tb_memberlist, /* tp_members */ |
|---|
| 105 | n/a | 0, /* tp_getset */ |
|---|
| 106 | n/a | 0, /* tp_base */ |
|---|
| 107 | n/a | 0, /* tp_dict */ |
|---|
| 108 | n/a | }; |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | static PyTracebackObject * |
|---|
| 111 | n/a | newtracebackobject(PyTracebackObject *next, PyFrameObject *frame) |
|---|
| 112 | n/a | { |
|---|
| 113 | n/a | PyTracebackObject *tb; |
|---|
| 114 | n/a | if ((next != NULL && !PyTraceBack_Check(next)) || |
|---|
| 115 | n/a | frame == NULL || !PyFrame_Check(frame)) { |
|---|
| 116 | n/a | PyErr_BadInternalCall(); |
|---|
| 117 | n/a | return NULL; |
|---|
| 118 | n/a | } |
|---|
| 119 | n/a | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
|---|
| 120 | n/a | if (tb != NULL) { |
|---|
| 121 | n/a | Py_XINCREF(next); |
|---|
| 122 | n/a | tb->tb_next = next; |
|---|
| 123 | n/a | Py_XINCREF(frame); |
|---|
| 124 | n/a | tb->tb_frame = frame; |
|---|
| 125 | n/a | tb->tb_lasti = frame->f_lasti; |
|---|
| 126 | n/a | tb->tb_lineno = PyFrame_GetLineNumber(frame); |
|---|
| 127 | n/a | PyObject_GC_Track(tb); |
|---|
| 128 | n/a | } |
|---|
| 129 | n/a | return tb; |
|---|
| 130 | n/a | } |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | int |
|---|
| 133 | n/a | PyTraceBack_Here(PyFrameObject *frame) |
|---|
| 134 | n/a | { |
|---|
| 135 | n/a | PyObject *exc, *val, *tb, *newtb; |
|---|
| 136 | n/a | PyErr_Fetch(&exc, &val, &tb); |
|---|
| 137 | n/a | newtb = (PyObject *)newtracebackobject((PyTracebackObject *)tb, frame); |
|---|
| 138 | n/a | if (newtb == NULL) { |
|---|
| 139 | n/a | _PyErr_ChainExceptions(exc, val, tb); |
|---|
| 140 | n/a | return -1; |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | PyErr_Restore(exc, val, newtb); |
|---|
| 143 | n/a | Py_XDECREF(tb); |
|---|
| 144 | n/a | return 0; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | /* Insert a frame into the traceback for (funcname, filename, lineno). */ |
|---|
| 148 | n/a | void _PyTraceback_Add(const char *funcname, const char *filename, int lineno) |
|---|
| 149 | n/a | { |
|---|
| 150 | n/a | PyObject *globals; |
|---|
| 151 | n/a | PyCodeObject *code; |
|---|
| 152 | n/a | PyFrameObject *frame; |
|---|
| 153 | n/a | PyObject *exc, *val, *tb; |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | /* Save and clear the current exception. Python functions must not be |
|---|
| 156 | n/a | called with an exception set. Calling Python functions happens when |
|---|
| 157 | n/a | the codec of the filesystem encoding is implemented in pure Python. */ |
|---|
| 158 | n/a | PyErr_Fetch(&exc, &val, &tb); |
|---|
| 159 | n/a | |
|---|
| 160 | n/a | globals = PyDict_New(); |
|---|
| 161 | n/a | if (!globals) |
|---|
| 162 | n/a | goto error; |
|---|
| 163 | n/a | code = PyCode_NewEmpty(filename, funcname, lineno); |
|---|
| 164 | n/a | if (!code) { |
|---|
| 165 | n/a | Py_DECREF(globals); |
|---|
| 166 | n/a | goto error; |
|---|
| 167 | n/a | } |
|---|
| 168 | n/a | frame = PyFrame_New(PyThreadState_Get(), code, globals, NULL); |
|---|
| 169 | n/a | Py_DECREF(globals); |
|---|
| 170 | n/a | Py_DECREF(code); |
|---|
| 171 | n/a | if (!frame) |
|---|
| 172 | n/a | goto error; |
|---|
| 173 | n/a | frame->f_lineno = lineno; |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | PyErr_Restore(exc, val, tb); |
|---|
| 176 | n/a | PyTraceBack_Here(frame); |
|---|
| 177 | n/a | Py_DECREF(frame); |
|---|
| 178 | n/a | return; |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | error: |
|---|
| 181 | n/a | _PyErr_ChainExceptions(exc, val, tb); |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | static PyObject * |
|---|
| 185 | n/a | _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io) |
|---|
| 186 | n/a | { |
|---|
| 187 | n/a | Py_ssize_t i; |
|---|
| 188 | n/a | PyObject *binary; |
|---|
| 189 | n/a | PyObject *v; |
|---|
| 190 | n/a | Py_ssize_t npath; |
|---|
| 191 | n/a | size_t taillen; |
|---|
| 192 | n/a | PyObject *syspath; |
|---|
| 193 | n/a | PyObject *path; |
|---|
| 194 | n/a | const char* tail; |
|---|
| 195 | n/a | PyObject *filebytes; |
|---|
| 196 | n/a | const char* filepath; |
|---|
| 197 | n/a | Py_ssize_t len; |
|---|
| 198 | n/a | PyObject* result; |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | filebytes = PyUnicode_EncodeFSDefault(filename); |
|---|
| 201 | n/a | if (filebytes == NULL) { |
|---|
| 202 | n/a | PyErr_Clear(); |
|---|
| 203 | n/a | return NULL; |
|---|
| 204 | n/a | } |
|---|
| 205 | n/a | filepath = PyBytes_AS_STRING(filebytes); |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | /* Search tail of filename in sys.path before giving up */ |
|---|
| 208 | n/a | tail = strrchr(filepath, SEP); |
|---|
| 209 | n/a | if (tail == NULL) |
|---|
| 210 | n/a | tail = filepath; |
|---|
| 211 | n/a | else |
|---|
| 212 | n/a | tail++; |
|---|
| 213 | n/a | taillen = strlen(tail); |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | syspath = _PySys_GetObjectId(&PyId_path); |
|---|
| 216 | n/a | if (syspath == NULL || !PyList_Check(syspath)) |
|---|
| 217 | n/a | goto error; |
|---|
| 218 | n/a | npath = PyList_Size(syspath); |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | for (i = 0; i < npath; i++) { |
|---|
| 221 | n/a | v = PyList_GetItem(syspath, i); |
|---|
| 222 | n/a | if (v == NULL) { |
|---|
| 223 | n/a | PyErr_Clear(); |
|---|
| 224 | n/a | break; |
|---|
| 225 | n/a | } |
|---|
| 226 | n/a | if (!PyUnicode_Check(v)) |
|---|
| 227 | n/a | continue; |
|---|
| 228 | n/a | path = PyUnicode_EncodeFSDefault(v); |
|---|
| 229 | n/a | if (path == NULL) { |
|---|
| 230 | n/a | PyErr_Clear(); |
|---|
| 231 | n/a | continue; |
|---|
| 232 | n/a | } |
|---|
| 233 | n/a | len = PyBytes_GET_SIZE(path); |
|---|
| 234 | n/a | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { |
|---|
| 235 | n/a | Py_DECREF(path); |
|---|
| 236 | n/a | continue; /* Too long */ |
|---|
| 237 | n/a | } |
|---|
| 238 | n/a | strcpy(namebuf, PyBytes_AS_STRING(path)); |
|---|
| 239 | n/a | Py_DECREF(path); |
|---|
| 240 | n/a | if (strlen(namebuf) != (size_t)len) |
|---|
| 241 | n/a | continue; /* v contains '\0' */ |
|---|
| 242 | n/a | if (len > 0 && namebuf[len-1] != SEP) |
|---|
| 243 | n/a | namebuf[len++] = SEP; |
|---|
| 244 | n/a | strcpy(namebuf+len, tail); |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb"); |
|---|
| 247 | n/a | if (binary != NULL) { |
|---|
| 248 | n/a | result = binary; |
|---|
| 249 | n/a | goto finally; |
|---|
| 250 | n/a | } |
|---|
| 251 | n/a | PyErr_Clear(); |
|---|
| 252 | n/a | } |
|---|
| 253 | n/a | goto error; |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | error: |
|---|
| 256 | n/a | result = NULL; |
|---|
| 257 | n/a | finally: |
|---|
| 258 | n/a | Py_DECREF(filebytes); |
|---|
| 259 | n/a | return result; |
|---|
| 260 | n/a | } |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | int |
|---|
| 263 | n/a | _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) |
|---|
| 264 | n/a | { |
|---|
| 265 | n/a | int err = 0; |
|---|
| 266 | n/a | int fd; |
|---|
| 267 | n/a | int i; |
|---|
| 268 | n/a | char *found_encoding; |
|---|
| 269 | n/a | char *encoding; |
|---|
| 270 | n/a | PyObject *io; |
|---|
| 271 | n/a | PyObject *binary; |
|---|
| 272 | n/a | PyObject *fob = NULL; |
|---|
| 273 | n/a | PyObject *lineobj = NULL; |
|---|
| 274 | n/a | PyObject *res; |
|---|
| 275 | n/a | char buf[MAXPATHLEN+1]; |
|---|
| 276 | n/a | int kind; |
|---|
| 277 | n/a | void *data; |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | /* open the file */ |
|---|
| 280 | n/a | if (filename == NULL) |
|---|
| 281 | n/a | return 0; |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | io = PyImport_ImportModuleNoBlock("io"); |
|---|
| 284 | n/a | if (io == NULL) |
|---|
| 285 | n/a | return -1; |
|---|
| 286 | n/a | binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb"); |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | if (binary == NULL) { |
|---|
| 289 | n/a | PyErr_Clear(); |
|---|
| 290 | n/a | |
|---|
| 291 | n/a | binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); |
|---|
| 292 | n/a | if (binary == NULL) { |
|---|
| 293 | n/a | Py_DECREF(io); |
|---|
| 294 | n/a | return -1; |
|---|
| 295 | n/a | } |
|---|
| 296 | n/a | } |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | /* use the right encoding to decode the file as unicode */ |
|---|
| 299 | n/a | fd = PyObject_AsFileDescriptor(binary); |
|---|
| 300 | n/a | if (fd < 0) { |
|---|
| 301 | n/a | Py_DECREF(io); |
|---|
| 302 | n/a | Py_DECREF(binary); |
|---|
| 303 | n/a | return 0; |
|---|
| 304 | n/a | } |
|---|
| 305 | n/a | found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); |
|---|
| 306 | n/a | if (found_encoding == NULL) |
|---|
| 307 | n/a | PyErr_Clear(); |
|---|
| 308 | n/a | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; |
|---|
| 309 | n/a | /* Reset position */ |
|---|
| 310 | n/a | if (lseek(fd, 0, SEEK_SET) == (off_t)-1) { |
|---|
| 311 | n/a | Py_DECREF(io); |
|---|
| 312 | n/a | Py_DECREF(binary); |
|---|
| 313 | n/a | PyMem_FREE(found_encoding); |
|---|
| 314 | n/a | return 0; |
|---|
| 315 | n/a | } |
|---|
| 316 | n/a | fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding); |
|---|
| 317 | n/a | Py_DECREF(io); |
|---|
| 318 | n/a | PyMem_FREE(found_encoding); |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | if (fob == NULL) { |
|---|
| 321 | n/a | PyErr_Clear(); |
|---|
| 322 | n/a | |
|---|
| 323 | n/a | res = _PyObject_CallMethodId(binary, &PyId_close, NULL); |
|---|
| 324 | n/a | Py_DECREF(binary); |
|---|
| 325 | n/a | if (res) |
|---|
| 326 | n/a | Py_DECREF(res); |
|---|
| 327 | n/a | else |
|---|
| 328 | n/a | PyErr_Clear(); |
|---|
| 329 | n/a | return 0; |
|---|
| 330 | n/a | } |
|---|
| 331 | n/a | Py_DECREF(binary); |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | /* get the line number lineno */ |
|---|
| 334 | n/a | for (i = 0; i < lineno; i++) { |
|---|
| 335 | n/a | Py_XDECREF(lineobj); |
|---|
| 336 | n/a | lineobj = PyFile_GetLine(fob, -1); |
|---|
| 337 | n/a | if (!lineobj) { |
|---|
| 338 | n/a | PyErr_Clear(); |
|---|
| 339 | n/a | err = -1; |
|---|
| 340 | n/a | break; |
|---|
| 341 | n/a | } |
|---|
| 342 | n/a | } |
|---|
| 343 | n/a | res = _PyObject_CallMethodId(fob, &PyId_close, NULL); |
|---|
| 344 | n/a | if (res) |
|---|
| 345 | n/a | Py_DECREF(res); |
|---|
| 346 | n/a | else |
|---|
| 347 | n/a | PyErr_Clear(); |
|---|
| 348 | n/a | Py_DECREF(fob); |
|---|
| 349 | n/a | if (!lineobj || !PyUnicode_Check(lineobj)) { |
|---|
| 350 | n/a | Py_XDECREF(lineobj); |
|---|
| 351 | n/a | return err; |
|---|
| 352 | n/a | } |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | /* remove the indentation of the line */ |
|---|
| 355 | n/a | kind = PyUnicode_KIND(lineobj); |
|---|
| 356 | n/a | data = PyUnicode_DATA(lineobj); |
|---|
| 357 | n/a | for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) { |
|---|
| 358 | n/a | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
|---|
| 359 | n/a | if (ch != ' ' && ch != '\t' && ch != '\014') |
|---|
| 360 | n/a | break; |
|---|
| 361 | n/a | } |
|---|
| 362 | n/a | if (i) { |
|---|
| 363 | n/a | PyObject *truncated; |
|---|
| 364 | n/a | truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); |
|---|
| 365 | n/a | if (truncated) { |
|---|
| 366 | n/a | Py_DECREF(lineobj); |
|---|
| 367 | n/a | lineobj = truncated; |
|---|
| 368 | n/a | } else { |
|---|
| 369 | n/a | PyErr_Clear(); |
|---|
| 370 | n/a | } |
|---|
| 371 | n/a | } |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | /* Write some spaces before the line */ |
|---|
| 374 | n/a | strcpy(buf, " "); |
|---|
| 375 | n/a | assert (strlen(buf) == 10); |
|---|
| 376 | n/a | while (indent > 0) { |
|---|
| 377 | n/a | if (indent < 10) |
|---|
| 378 | n/a | buf[indent] = '\0'; |
|---|
| 379 | n/a | err = PyFile_WriteString(buf, f); |
|---|
| 380 | n/a | if (err != 0) |
|---|
| 381 | n/a | break; |
|---|
| 382 | n/a | indent -= 10; |
|---|
| 383 | n/a | } |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | /* finally display the line */ |
|---|
| 386 | n/a | if (err == 0) |
|---|
| 387 | n/a | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
|---|
| 388 | n/a | Py_DECREF(lineobj); |
|---|
| 389 | n/a | if (err == 0) |
|---|
| 390 | n/a | err = PyFile_WriteString("\n", f); |
|---|
| 391 | n/a | return err; |
|---|
| 392 | n/a | } |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | static int |
|---|
| 395 | n/a | tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name) |
|---|
| 396 | n/a | { |
|---|
| 397 | n/a | int err; |
|---|
| 398 | n/a | PyObject *line; |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | if (filename == NULL || name == NULL) |
|---|
| 401 | n/a | return -1; |
|---|
| 402 | n/a | line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n", |
|---|
| 403 | n/a | filename, lineno, name); |
|---|
| 404 | n/a | if (line == NULL) |
|---|
| 405 | n/a | return -1; |
|---|
| 406 | n/a | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
|---|
| 407 | n/a | Py_DECREF(line); |
|---|
| 408 | n/a | if (err != 0) |
|---|
| 409 | n/a | return err; |
|---|
| 410 | n/a | /* ignore errors since we can't report them, can we? */ |
|---|
| 411 | n/a | if (_Py_DisplaySourceLine(f, filename, lineno, 4)) |
|---|
| 412 | n/a | PyErr_Clear(); |
|---|
| 413 | n/a | return err; |
|---|
| 414 | n/a | } |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | static int |
|---|
| 417 | n/a | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
|---|
| 418 | n/a | { |
|---|
| 419 | n/a | int err = 0; |
|---|
| 420 | n/a | long depth = 0; |
|---|
| 421 | n/a | PyObject *last_file = NULL; |
|---|
| 422 | n/a | int last_line = -1; |
|---|
| 423 | n/a | PyObject *last_name = NULL; |
|---|
| 424 | n/a | long cnt = 0; |
|---|
| 425 | n/a | PyObject *line; |
|---|
| 426 | n/a | PyTracebackObject *tb1 = tb; |
|---|
| 427 | n/a | while (tb1 != NULL) { |
|---|
| 428 | n/a | depth++; |
|---|
| 429 | n/a | tb1 = tb1->tb_next; |
|---|
| 430 | n/a | } |
|---|
| 431 | n/a | while (tb != NULL && err == 0) { |
|---|
| 432 | n/a | if (depth <= limit) { |
|---|
| 433 | n/a | if (last_file != NULL && |
|---|
| 434 | n/a | tb->tb_frame->f_code->co_filename == last_file && |
|---|
| 435 | n/a | last_line != -1 && tb->tb_lineno == last_line && |
|---|
| 436 | n/a | last_name != NULL && |
|---|
| 437 | n/a | tb->tb_frame->f_code->co_name == last_name) { |
|---|
| 438 | n/a | cnt++; |
|---|
| 439 | n/a | } else { |
|---|
| 440 | n/a | if (cnt > 3) { |
|---|
| 441 | n/a | line = PyUnicode_FromFormat( |
|---|
| 442 | n/a | " [Previous line repeated %d more times]\n", cnt-3); |
|---|
| 443 | n/a | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
|---|
| 444 | n/a | Py_DECREF(line); |
|---|
| 445 | n/a | } |
|---|
| 446 | n/a | last_file = tb->tb_frame->f_code->co_filename; |
|---|
| 447 | n/a | last_line = tb->tb_lineno; |
|---|
| 448 | n/a | last_name = tb->tb_frame->f_code->co_name; |
|---|
| 449 | n/a | cnt = 0; |
|---|
| 450 | n/a | } |
|---|
| 451 | n/a | if (cnt < 3) |
|---|
| 452 | n/a | err = tb_displayline(f, |
|---|
| 453 | n/a | tb->tb_frame->f_code->co_filename, |
|---|
| 454 | n/a | tb->tb_lineno, |
|---|
| 455 | n/a | tb->tb_frame->f_code->co_name); |
|---|
| 456 | n/a | } |
|---|
| 457 | n/a | depth--; |
|---|
| 458 | n/a | tb = tb->tb_next; |
|---|
| 459 | n/a | if (err == 0) |
|---|
| 460 | n/a | err = PyErr_CheckSignals(); |
|---|
| 461 | n/a | } |
|---|
| 462 | n/a | if (cnt > 3) { |
|---|
| 463 | n/a | line = PyUnicode_FromFormat( |
|---|
| 464 | n/a | " [Previous line repeated %d more times]\n", cnt-3); |
|---|
| 465 | n/a | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
|---|
| 466 | n/a | Py_DECREF(line); |
|---|
| 467 | n/a | } |
|---|
| 468 | n/a | return err; |
|---|
| 469 | n/a | } |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | #define PyTraceBack_LIMIT 1000 |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | int |
|---|
| 474 | n/a | PyTraceBack_Print(PyObject *v, PyObject *f) |
|---|
| 475 | n/a | { |
|---|
| 476 | n/a | int err; |
|---|
| 477 | n/a | PyObject *limitv; |
|---|
| 478 | n/a | long limit = PyTraceBack_LIMIT; |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | if (v == NULL) |
|---|
| 481 | n/a | return 0; |
|---|
| 482 | n/a | if (!PyTraceBack_Check(v)) { |
|---|
| 483 | n/a | PyErr_BadInternalCall(); |
|---|
| 484 | n/a | return -1; |
|---|
| 485 | n/a | } |
|---|
| 486 | n/a | limitv = PySys_GetObject("tracebacklimit"); |
|---|
| 487 | n/a | if (limitv) { |
|---|
| 488 | n/a | PyObject *exc_type, *exc_value, *exc_tb; |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
|---|
| 491 | n/a | limit = PyLong_AsLong(limitv); |
|---|
| 492 | n/a | if (limit == -1 && PyErr_Occurred()) { |
|---|
| 493 | n/a | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
|---|
| 494 | n/a | limit = PyTraceBack_LIMIT; |
|---|
| 495 | n/a | } |
|---|
| 496 | n/a | else { |
|---|
| 497 | n/a | Py_XDECREF(exc_type); |
|---|
| 498 | n/a | Py_XDECREF(exc_value); |
|---|
| 499 | n/a | Py_XDECREF(exc_tb); |
|---|
| 500 | n/a | return 0; |
|---|
| 501 | n/a | } |
|---|
| 502 | n/a | } |
|---|
| 503 | n/a | else if (limit <= 0) { |
|---|
| 504 | n/a | limit = PyTraceBack_LIMIT; |
|---|
| 505 | n/a | } |
|---|
| 506 | n/a | PyErr_Restore(exc_type, exc_value, exc_tb); |
|---|
| 507 | n/a | } |
|---|
| 508 | n/a | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
|---|
| 509 | n/a | if (!err) |
|---|
| 510 | n/a | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
|---|
| 511 | n/a | return err; |
|---|
| 512 | n/a | } |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | /* Reverse a string. For example, "abcd" becomes "dcba". |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | This function is signal safe. */ |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | void |
|---|
| 519 | n/a | _Py_DumpDecimal(int fd, unsigned long value) |
|---|
| 520 | n/a | { |
|---|
| 521 | n/a | /* maximum number of characters required for output of %lld or %p. |
|---|
| 522 | n/a | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
|---|
| 523 | n/a | plus 1 for the null byte. 53/22 is an upper bound for log10(256). */ |
|---|
| 524 | n/a | char buffer[1 + (sizeof(unsigned long)*53-1) / 22 + 1]; |
|---|
| 525 | n/a | char *ptr, *end; |
|---|
| 526 | n/a | |
|---|
| 527 | n/a | end = &buffer[Py_ARRAY_LENGTH(buffer) - 1]; |
|---|
| 528 | n/a | ptr = end; |
|---|
| 529 | n/a | *ptr = '\0'; |
|---|
| 530 | n/a | do { |
|---|
| 531 | n/a | --ptr; |
|---|
| 532 | n/a | assert(ptr >= buffer); |
|---|
| 533 | n/a | *ptr = '0' + (value % 10); |
|---|
| 534 | n/a | value /= 10; |
|---|
| 535 | n/a | } while (value); |
|---|
| 536 | n/a | |
|---|
| 537 | n/a | _Py_write_noraise(fd, ptr, end - ptr); |
|---|
| 538 | n/a | } |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | /* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits, |
|---|
| 541 | n/a | and write it into the file fd. |
|---|
| 542 | n/a | |
|---|
| 543 | n/a | This function is signal safe. */ |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | void |
|---|
| 546 | n/a | _Py_DumpHexadecimal(int fd, unsigned long value, Py_ssize_t width) |
|---|
| 547 | n/a | { |
|---|
| 548 | n/a | char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end; |
|---|
| 549 | n/a | const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1; |
|---|
| 550 | n/a | |
|---|
| 551 | n/a | if (width > size) |
|---|
| 552 | n/a | width = size; |
|---|
| 553 | n/a | /* it's ok if width is negative */ |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | end = &buffer[size]; |
|---|
| 556 | n/a | ptr = end; |
|---|
| 557 | n/a | *ptr = '\0'; |
|---|
| 558 | n/a | do { |
|---|
| 559 | n/a | --ptr; |
|---|
| 560 | n/a | assert(ptr >= buffer); |
|---|
| 561 | n/a | *ptr = Py_hexdigits[value & 15]; |
|---|
| 562 | n/a | value >>= 4; |
|---|
| 563 | n/a | } while ((end - ptr) < width || value); |
|---|
| 564 | n/a | |
|---|
| 565 | n/a | _Py_write_noraise(fd, ptr, end - ptr); |
|---|
| 566 | n/a | } |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | void |
|---|
| 569 | n/a | _Py_DumpASCII(int fd, PyObject *text) |
|---|
| 570 | n/a | { |
|---|
| 571 | n/a | PyASCIIObject *ascii = (PyASCIIObject *)text; |
|---|
| 572 | n/a | Py_ssize_t i, size; |
|---|
| 573 | n/a | int truncated; |
|---|
| 574 | n/a | int kind; |
|---|
| 575 | n/a | void *data = NULL; |
|---|
| 576 | n/a | wchar_t *wstr = NULL; |
|---|
| 577 | n/a | Py_UCS4 ch; |
|---|
| 578 | n/a | |
|---|
| 579 | n/a | if (!PyUnicode_Check(text)) |
|---|
| 580 | n/a | return; |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | size = ascii->length; |
|---|
| 583 | n/a | kind = ascii->state.kind; |
|---|
| 584 | n/a | if (kind == PyUnicode_WCHAR_KIND) { |
|---|
| 585 | n/a | wstr = ((PyASCIIObject *)text)->wstr; |
|---|
| 586 | n/a | if (wstr == NULL) |
|---|
| 587 | n/a | return; |
|---|
| 588 | n/a | size = ((PyCompactUnicodeObject *)text)->wstr_length; |
|---|
| 589 | n/a | } |
|---|
| 590 | n/a | else if (ascii->state.compact) { |
|---|
| 591 | n/a | if (ascii->state.ascii) |
|---|
| 592 | n/a | data = ((PyASCIIObject*)text) + 1; |
|---|
| 593 | n/a | else |
|---|
| 594 | n/a | data = ((PyCompactUnicodeObject*)text) + 1; |
|---|
| 595 | n/a | } |
|---|
| 596 | n/a | else { |
|---|
| 597 | n/a | data = ((PyUnicodeObject *)text)->data.any; |
|---|
| 598 | n/a | if (data == NULL) |
|---|
| 599 | n/a | return; |
|---|
| 600 | n/a | } |
|---|
| 601 | n/a | |
|---|
| 602 | n/a | if (MAX_STRING_LENGTH < size) { |
|---|
| 603 | n/a | size = MAX_STRING_LENGTH; |
|---|
| 604 | n/a | truncated = 1; |
|---|
| 605 | n/a | } |
|---|
| 606 | n/a | else { |
|---|
| 607 | n/a | truncated = 0; |
|---|
| 608 | n/a | } |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | for (i=0; i < size; i++) { |
|---|
| 611 | n/a | if (kind != PyUnicode_WCHAR_KIND) |
|---|
| 612 | n/a | ch = PyUnicode_READ(kind, data, i); |
|---|
| 613 | n/a | else |
|---|
| 614 | n/a | ch = wstr[i]; |
|---|
| 615 | n/a | if (' ' <= ch && ch <= 126) { |
|---|
| 616 | n/a | /* printable ASCII character */ |
|---|
| 617 | n/a | char c = (char)ch; |
|---|
| 618 | n/a | _Py_write_noraise(fd, &c, 1); |
|---|
| 619 | n/a | } |
|---|
| 620 | n/a | else if (ch <= 0xff) { |
|---|
| 621 | n/a | PUTS(fd, "\\x"); |
|---|
| 622 | n/a | _Py_DumpHexadecimal(fd, ch, 2); |
|---|
| 623 | n/a | } |
|---|
| 624 | n/a | else if (ch <= 0xffff) { |
|---|
| 625 | n/a | PUTS(fd, "\\u"); |
|---|
| 626 | n/a | _Py_DumpHexadecimal(fd, ch, 4); |
|---|
| 627 | n/a | } |
|---|
| 628 | n/a | else { |
|---|
| 629 | n/a | PUTS(fd, "\\U"); |
|---|
| 630 | n/a | _Py_DumpHexadecimal(fd, ch, 8); |
|---|
| 631 | n/a | } |
|---|
| 632 | n/a | } |
|---|
| 633 | n/a | if (truncated) { |
|---|
| 634 | n/a | PUTS(fd, "..."); |
|---|
| 635 | n/a | } |
|---|
| 636 | n/a | } |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | /* Write a frame into the file fd: "File "xxx", line xxx in xxx". |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | This function is signal safe. */ |
|---|
| 641 | n/a | |
|---|
| 642 | n/a | static void |
|---|
| 643 | n/a | dump_frame(int fd, PyFrameObject *frame) |
|---|
| 644 | n/a | { |
|---|
| 645 | n/a | PyCodeObject *code; |
|---|
| 646 | n/a | int lineno; |
|---|
| 647 | n/a | |
|---|
| 648 | n/a | code = frame->f_code; |
|---|
| 649 | n/a | PUTS(fd, " File "); |
|---|
| 650 | n/a | if (code != NULL && code->co_filename != NULL |
|---|
| 651 | n/a | && PyUnicode_Check(code->co_filename)) |
|---|
| 652 | n/a | { |
|---|
| 653 | n/a | PUTS(fd, "\""); |
|---|
| 654 | n/a | _Py_DumpASCII(fd, code->co_filename); |
|---|
| 655 | n/a | PUTS(fd, "\""); |
|---|
| 656 | n/a | } else { |
|---|
| 657 | n/a | PUTS(fd, "???"); |
|---|
| 658 | n/a | } |
|---|
| 659 | n/a | |
|---|
| 660 | n/a | /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */ |
|---|
| 661 | n/a | lineno = PyCode_Addr2Line(code, frame->f_lasti); |
|---|
| 662 | n/a | PUTS(fd, ", line "); |
|---|
| 663 | n/a | if (lineno >= 0) { |
|---|
| 664 | n/a | _Py_DumpDecimal(fd, (unsigned long)lineno); |
|---|
| 665 | n/a | } |
|---|
| 666 | n/a | else { |
|---|
| 667 | n/a | PUTS(fd, "???"); |
|---|
| 668 | n/a | } |
|---|
| 669 | n/a | PUTS(fd, " in "); |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | if (code != NULL && code->co_name != NULL |
|---|
| 672 | n/a | && PyUnicode_Check(code->co_name)) { |
|---|
| 673 | n/a | _Py_DumpASCII(fd, code->co_name); |
|---|
| 674 | n/a | } |
|---|
| 675 | n/a | else { |
|---|
| 676 | n/a | PUTS(fd, "???"); |
|---|
| 677 | n/a | } |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | PUTS(fd, "\n"); |
|---|
| 680 | n/a | } |
|---|
| 681 | n/a | |
|---|
| 682 | n/a | static void |
|---|
| 683 | n/a | dump_traceback(int fd, PyThreadState *tstate, int write_header) |
|---|
| 684 | n/a | { |
|---|
| 685 | n/a | PyFrameObject *frame; |
|---|
| 686 | n/a | unsigned int depth; |
|---|
| 687 | n/a | |
|---|
| 688 | n/a | if (write_header) |
|---|
| 689 | n/a | PUTS(fd, "Stack (most recent call first):\n"); |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | frame = _PyThreadState_GetFrame(tstate); |
|---|
| 692 | n/a | if (frame == NULL) |
|---|
| 693 | n/a | return; |
|---|
| 694 | n/a | |
|---|
| 695 | n/a | depth = 0; |
|---|
| 696 | n/a | while (frame != NULL) { |
|---|
| 697 | n/a | if (MAX_FRAME_DEPTH <= depth) { |
|---|
| 698 | n/a | PUTS(fd, " ...\n"); |
|---|
| 699 | n/a | break; |
|---|
| 700 | n/a | } |
|---|
| 701 | n/a | if (!PyFrame_Check(frame)) |
|---|
| 702 | n/a | break; |
|---|
| 703 | n/a | dump_frame(fd, frame); |
|---|
| 704 | n/a | frame = frame->f_back; |
|---|
| 705 | n/a | depth++; |
|---|
| 706 | n/a | } |
|---|
| 707 | n/a | } |
|---|
| 708 | n/a | |
|---|
| 709 | n/a | /* Dump the traceback of a Python thread into fd. Use write() to write the |
|---|
| 710 | n/a | traceback and retry if write() is interrupted by a signal (failed with |
|---|
| 711 | n/a | EINTR), but don't call the Python signal handler. |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | The caller is responsible to call PyErr_CheckSignals() to call Python signal |
|---|
| 714 | n/a | handlers if signals were received. */ |
|---|
| 715 | n/a | void |
|---|
| 716 | n/a | _Py_DumpTraceback(int fd, PyThreadState *tstate) |
|---|
| 717 | n/a | { |
|---|
| 718 | n/a | dump_traceback(fd, tstate, 1); |
|---|
| 719 | n/a | } |
|---|
| 720 | n/a | |
|---|
| 721 | n/a | /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if |
|---|
| 722 | n/a | is_current is true, "Thread 0xHHHH:\n" otherwise. |
|---|
| 723 | n/a | |
|---|
| 724 | n/a | This function is signal safe. */ |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | static void |
|---|
| 727 | n/a | write_thread_id(int fd, PyThreadState *tstate, int is_current) |
|---|
| 728 | n/a | { |
|---|
| 729 | n/a | if (is_current) |
|---|
| 730 | n/a | PUTS(fd, "Current thread 0x"); |
|---|
| 731 | n/a | else |
|---|
| 732 | n/a | PUTS(fd, "Thread 0x"); |
|---|
| 733 | n/a | _Py_DumpHexadecimal(fd, |
|---|
| 734 | n/a | (unsigned long)tstate->thread_id, |
|---|
| 735 | n/a | sizeof(unsigned long) * 2); |
|---|
| 736 | n/a | PUTS(fd, " (most recent call first):\n"); |
|---|
| 737 | n/a | } |
|---|
| 738 | n/a | |
|---|
| 739 | n/a | /* Dump the traceback of all Python threads into fd. Use write() to write the |
|---|
| 740 | n/a | traceback and retry if write() is interrupted by a signal (failed with |
|---|
| 741 | n/a | EINTR), but don't call the Python signal handler. |
|---|
| 742 | n/a | |
|---|
| 743 | n/a | The caller is responsible to call PyErr_CheckSignals() to call Python signal |
|---|
| 744 | n/a | handlers if signals were received. */ |
|---|
| 745 | n/a | const char* |
|---|
| 746 | n/a | _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp, |
|---|
| 747 | n/a | PyThreadState *current_tstate) |
|---|
| 748 | n/a | { |
|---|
| 749 | n/a | PyThreadState *tstate; |
|---|
| 750 | n/a | unsigned int nthreads; |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | #ifdef WITH_THREAD |
|---|
| 753 | n/a | if (current_tstate == NULL) { |
|---|
| 754 | n/a | /* _Py_DumpTracebackThreads() is called from signal handlers by |
|---|
| 755 | n/a | faulthandler. |
|---|
| 756 | n/a | |
|---|
| 757 | n/a | SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals |
|---|
| 758 | n/a | and are thus delivered to the thread that caused the fault. Get the |
|---|
| 759 | n/a | Python thread state of the current thread. |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | PyThreadState_Get() doesn't give the state of the thread that caused |
|---|
| 762 | n/a | the fault if the thread released the GIL, and so this function |
|---|
| 763 | n/a | cannot be used. Read the thread local storage (TLS) instead: call |
|---|
| 764 | n/a | PyGILState_GetThisThreadState(). */ |
|---|
| 765 | n/a | current_tstate = PyGILState_GetThisThreadState(); |
|---|
| 766 | n/a | } |
|---|
| 767 | n/a | |
|---|
| 768 | n/a | if (interp == NULL) { |
|---|
| 769 | n/a | if (current_tstate == NULL) { |
|---|
| 770 | n/a | interp = _PyGILState_GetInterpreterStateUnsafe(); |
|---|
| 771 | n/a | if (interp == NULL) { |
|---|
| 772 | n/a | /* We need the interpreter state to get Python threads */ |
|---|
| 773 | n/a | return "unable to get the interpreter state"; |
|---|
| 774 | n/a | } |
|---|
| 775 | n/a | } |
|---|
| 776 | n/a | else { |
|---|
| 777 | n/a | interp = current_tstate->interp; |
|---|
| 778 | n/a | } |
|---|
| 779 | n/a | } |
|---|
| 780 | n/a | #else |
|---|
| 781 | n/a | if (current_tstate == NULL) { |
|---|
| 782 | n/a | /* Call _PyThreadState_UncheckedGet() instead of PyThreadState_Get() |
|---|
| 783 | n/a | to not fail with a fatal error if the thread state is NULL. */ |
|---|
| 784 | n/a | current_tstate = _PyThreadState_UncheckedGet(); |
|---|
| 785 | n/a | } |
|---|
| 786 | n/a | |
|---|
| 787 | n/a | if (interp == NULL) { |
|---|
| 788 | n/a | if (current_tstate == NULL) { |
|---|
| 789 | n/a | /* We need the interpreter state to get Python threads */ |
|---|
| 790 | n/a | return "unable to get the interpreter state"; |
|---|
| 791 | n/a | } |
|---|
| 792 | n/a | interp = current_tstate->interp; |
|---|
| 793 | n/a | } |
|---|
| 794 | n/a | #endif |
|---|
| 795 | n/a | assert(interp != NULL); |
|---|
| 796 | n/a | |
|---|
| 797 | n/a | /* Get the current interpreter from the current thread */ |
|---|
| 798 | n/a | tstate = PyInterpreterState_ThreadHead(interp); |
|---|
| 799 | n/a | if (tstate == NULL) |
|---|
| 800 | n/a | return "unable to get the thread head state"; |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | /* Dump the traceback of each thread */ |
|---|
| 803 | n/a | tstate = PyInterpreterState_ThreadHead(interp); |
|---|
| 804 | n/a | nthreads = 0; |
|---|
| 805 | n/a | _Py_BEGIN_SUPPRESS_IPH |
|---|
| 806 | n/a | do |
|---|
| 807 | n/a | { |
|---|
| 808 | n/a | if (nthreads != 0) |
|---|
| 809 | n/a | PUTS(fd, "\n"); |
|---|
| 810 | n/a | if (nthreads >= MAX_NTHREADS) { |
|---|
| 811 | n/a | PUTS(fd, "...\n"); |
|---|
| 812 | n/a | break; |
|---|
| 813 | n/a | } |
|---|
| 814 | n/a | write_thread_id(fd, tstate, tstate == current_tstate); |
|---|
| 815 | n/a | dump_traceback(fd, tstate, 0); |
|---|
| 816 | n/a | tstate = PyThreadState_Next(tstate); |
|---|
| 817 | n/a | nthreads++; |
|---|
| 818 | n/a | } while (tstate != NULL); |
|---|
| 819 | n/a | _Py_END_SUPPRESS_IPH |
|---|
| 820 | n/a | |
|---|
| 821 | n/a | return NULL; |
|---|
| 822 | n/a | } |
|---|
| 823 | n/a | |
|---|