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" |
Victor Stinner | 621cebe | 2018-11-12 16:53:38 +0100 | [diff] [blame] | 5 | #include "pycore_pystate.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7 | #include "code.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | #include "frameobject.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | #include "structmember.h" |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 10 | #include "osdefs.h" |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 11 | #ifdef HAVE_FCNTL_H |
| 12 | #include <fcntl.h> |
| 13 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 14 | |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 15 | #define OFF(x) offsetof(PyTracebackObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 17 | #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str)) |
Victor Stinner | 54f939b | 2012-07-30 13:08:58 +0200 | [diff] [blame] | 18 | #define MAX_STRING_LENGTH 500 |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 19 | #define MAX_FRAME_DEPTH 100 |
| 20 | #define MAX_NTHREADS 100 |
| 21 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 22 | /* Function from Parser/tokenizer.c */ |
| 23 | extern char * PyTokenizer_FindEncodingFilename(int, PyObject *); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 24 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 25 | _Py_IDENTIFIER(TextIOWrapper); |
| 26 | _Py_IDENTIFIER(close); |
| 27 | _Py_IDENTIFIER(open); |
| 28 | _Py_IDENTIFIER(path); |
| 29 | |
Nathaniel J. Smith | e46a8af | 2018-01-07 05:30:18 -0800 | [diff] [blame] | 30 | /*[clinic input] |
| 31 | class TracebackType "PyTracebackObject *" "&PyTraceback_Type" |
| 32 | [clinic start generated code]*/ |
| 33 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=928fa06c10151120]*/ |
| 34 | |
| 35 | #include "clinic/traceback.c.h" |
| 36 | |
| 37 | static PyObject * |
| 38 | tb_create_raw(PyTracebackObject *next, PyFrameObject *frame, int lasti, |
| 39 | int lineno) |
| 40 | { |
| 41 | PyTracebackObject *tb; |
| 42 | if ((next != NULL && !PyTraceBack_Check(next)) || |
| 43 | frame == NULL || !PyFrame_Check(frame)) { |
| 44 | PyErr_BadInternalCall(); |
| 45 | return NULL; |
| 46 | } |
| 47 | tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); |
| 48 | if (tb != NULL) { |
| 49 | Py_XINCREF(next); |
| 50 | tb->tb_next = next; |
| 51 | Py_XINCREF(frame); |
| 52 | tb->tb_frame = frame; |
| 53 | tb->tb_lasti = lasti; |
| 54 | tb->tb_lineno = lineno; |
| 55 | PyObject_GC_Track(tb); |
| 56 | } |
| 57 | return (PyObject *)tb; |
| 58 | } |
| 59 | |
| 60 | /*[clinic input] |
| 61 | @classmethod |
| 62 | TracebackType.__new__ as tb_new |
| 63 | |
| 64 | tb_next: object |
| 65 | tb_frame: object(type='PyFrameObject *', subclass_of='&PyFrame_Type') |
| 66 | tb_lasti: int |
| 67 | tb_lineno: int |
| 68 | |
| 69 | Create a new traceback object. |
| 70 | [clinic start generated code]*/ |
| 71 | |
| 72 | static PyObject * |
| 73 | tb_new_impl(PyTypeObject *type, PyObject *tb_next, PyFrameObject *tb_frame, |
| 74 | int tb_lasti, int tb_lineno) |
| 75 | /*[clinic end generated code: output=fa077debd72d861a input=01cbe8ec8783fca7]*/ |
| 76 | { |
| 77 | if (tb_next == Py_None) { |
| 78 | tb_next = NULL; |
| 79 | } else if (!PyTraceBack_Check(tb_next)) { |
| 80 | return PyErr_Format(PyExc_TypeError, |
| 81 | "expected traceback object or None, got '%s'", |
| 82 | Py_TYPE(tb_next)->tp_name); |
| 83 | } |
| 84 | |
| 85 | return tb_create_raw((PyTracebackObject *)tb_next, tb_frame, tb_lasti, |
| 86 | tb_lineno); |
| 87 | } |
| 88 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 89 | static PyObject * |
Siddhesh Poyarekar | 55edd0c | 2018-04-30 00:29:33 +0530 | [diff] [blame] | 90 | tb_dir(PyTracebackObject *self, PyObject *Py_UNUSED(ignored)) |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 91 | { |
| 92 | return Py_BuildValue("[ssss]", "tb_frame", "tb_next", |
| 93 | "tb_lasti", "tb_lineno"); |
| 94 | } |
| 95 | |
Nathaniel J. Smith | e46a8af | 2018-01-07 05:30:18 -0800 | [diff] [blame] | 96 | static PyObject * |
| 97 | tb_next_get(PyTracebackObject *self, void *Py_UNUSED(_)) |
| 98 | { |
| 99 | PyObject* ret = (PyObject*)self->tb_next; |
| 100 | if (!ret) { |
| 101 | ret = Py_None; |
| 102 | } |
| 103 | Py_INCREF(ret); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | tb_next_set(PyTracebackObject *self, PyObject *new_next, void *Py_UNUSED(_)) |
| 109 | { |
| 110 | if (!new_next) { |
| 111 | PyErr_Format(PyExc_TypeError, "can't delete tb_next attribute"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | /* We accept None or a traceback object, and map None -> NULL (inverse of |
| 116 | tb_next_get) */ |
| 117 | if (new_next == Py_None) { |
| 118 | new_next = NULL; |
| 119 | } else if (!PyTraceBack_Check(new_next)) { |
| 120 | PyErr_Format(PyExc_TypeError, |
| 121 | "expected traceback object, got '%s'", |
| 122 | Py_TYPE(new_next)->tp_name); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | /* Check for loops */ |
| 127 | PyTracebackObject *cursor = (PyTracebackObject *)new_next; |
| 128 | while (cursor) { |
| 129 | if (cursor == self) { |
| 130 | PyErr_Format(PyExc_ValueError, "traceback loop detected"); |
| 131 | return -1; |
| 132 | } |
| 133 | cursor = cursor->tb_next; |
| 134 | } |
| 135 | |
| 136 | PyObject *old_next = (PyObject*)self->tb_next; |
| 137 | Py_XINCREF(new_next); |
| 138 | self->tb_next = (PyTracebackObject *)new_next; |
| 139 | Py_XDECREF(old_next); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | |
Collin Winter | 3eed765 | 2007-08-14 17:53:54 +0000 | [diff] [blame] | 145 | static PyMethodDef tb_methods[] = { |
| 146 | {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, |
| 147 | {NULL, NULL, 0, NULL}, |
| 148 | }; |
| 149 | |
Neal Norwitz | 8dfc4a9 | 2007-08-11 06:39:53 +0000 | [diff] [blame] | 150 | static PyMemberDef tb_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY}, |
| 152 | {"tb_lasti", T_INT, OFF(tb_lasti), READONLY}, |
| 153 | {"tb_lineno", T_INT, OFF(tb_lineno), READONLY}, |
| 154 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Nathaniel J. Smith | e46a8af | 2018-01-07 05:30:18 -0800 | [diff] [blame] | 157 | static PyGetSetDef tb_getsetters[] = { |
| 158 | {"tb_next", (getter)tb_next_get, (setter)tb_next_set, NULL, NULL}, |
| 159 | {NULL} /* Sentinel */ |
| 160 | }; |
| 161 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 162 | static void |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 163 | tb_dealloc(PyTracebackObject *tb) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | PyObject_GC_UnTrack(tb); |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 166 | Py_TRASHCAN_BEGIN(tb, tb_dealloc) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | Py_XDECREF(tb->tb_next); |
| 168 | Py_XDECREF(tb->tb_frame); |
| 169 | PyObject_GC_Del(tb); |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 170 | Py_TRASHCAN_END |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 173 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 174 | tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | Py_VISIT(tb->tb_next); |
| 177 | Py_VISIT(tb->tb_frame); |
| 178 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 181 | static int |
Nicholas Bastin | a7604bf | 2004-03-21 18:37:23 +0000 | [diff] [blame] | 182 | tb_clear(PyTracebackObject *tb) |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | Py_CLEAR(tb->tb_next); |
| 185 | Py_CLEAR(tb->tb_frame); |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 186 | return 0; |
Jeremy Hylton | fd14d8e | 2001-10-22 22:17:41 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Tim Peters | d7c3652 | 2001-10-22 19:34:09 +0000 | [diff] [blame] | 189 | PyTypeObject PyTraceBack_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 191 | "traceback", |
| 192 | sizeof(PyTracebackObject), |
| 193 | 0, |
| 194 | (destructor)tb_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 195 | 0, /*tp_vectorcall_offset*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | 0, /*tp_getattr*/ |
| 197 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 198 | 0, /*tp_as_async*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | 0, /*tp_repr*/ |
| 200 | 0, /*tp_as_number*/ |
| 201 | 0, /*tp_as_sequence*/ |
| 202 | 0, /*tp_as_mapping*/ |
| 203 | 0, /* tp_hash */ |
| 204 | 0, /* tp_call */ |
| 205 | 0, /* tp_str */ |
| 206 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 207 | 0, /* tp_setattro */ |
| 208 | 0, /* tp_as_buffer */ |
| 209 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Nathaniel J. Smith | e46a8af | 2018-01-07 05:30:18 -0800 | [diff] [blame] | 210 | tb_new__doc__, /* tp_doc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | (traverseproc)tb_traverse, /* tp_traverse */ |
| 212 | (inquiry)tb_clear, /* tp_clear */ |
| 213 | 0, /* tp_richcompare */ |
| 214 | 0, /* tp_weaklistoffset */ |
| 215 | 0, /* tp_iter */ |
| 216 | 0, /* tp_iternext */ |
| 217 | tb_methods, /* tp_methods */ |
| 218 | tb_memberlist, /* tp_members */ |
Nathaniel J. Smith | e46a8af | 2018-01-07 05:30:18 -0800 | [diff] [blame] | 219 | tb_getsetters, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | 0, /* tp_base */ |
| 221 | 0, /* tp_dict */ |
Nathaniel J. Smith | e46a8af | 2018-01-07 05:30:18 -0800 | [diff] [blame] | 222 | 0, /* tp_descr_get */ |
| 223 | 0, /* tp_descr_set */ |
| 224 | 0, /* tp_dictoffset */ |
| 225 | 0, /* tp_init */ |
| 226 | 0, /* tp_alloc */ |
| 227 | tb_new, /* tp_new */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 230 | |
| 231 | PyObject* |
| 232 | _PyTraceBack_FromFrame(PyObject *tb_next, PyFrameObject *frame) |
| 233 | { |
| 234 | assert(tb_next == NULL || PyTraceBack_Check(tb_next)); |
| 235 | assert(frame != NULL); |
| 236 | |
| 237 | return tb_create_raw((PyTracebackObject *)tb_next, frame, frame->f_lasti, |
| 238 | PyFrame_GetLineNumber(frame)); |
| 239 | } |
| 240 | |
| 241 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 242 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 243 | PyTraceBack_Here(PyFrameObject *frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 244 | { |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 245 | PyObject *exc, *val, *tb, *newtb; |
| 246 | PyErr_Fetch(&exc, &val, &tb); |
Victor Stinner | df22c03 | 2019-05-23 01:00:58 +0200 | [diff] [blame] | 247 | newtb = _PyTraceBack_FromFrame(tb, frame); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 248 | if (newtb == NULL) { |
| 249 | _PyErr_ChainExceptions(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | return -1; |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 251 | } |
| 252 | PyErr_Restore(exc, val, newtb); |
| 253 | Py_XDECREF(tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 257 | /* Insert a frame into the traceback for (funcname, filename, lineno). */ |
Serhiy Storchaka | 73c95f1 | 2015-06-21 15:59:46 +0300 | [diff] [blame] | 258 | void _PyTraceback_Add(const char *funcname, const char *filename, int lineno) |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 259 | { |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 260 | PyObject *globals; |
| 261 | PyCodeObject *code; |
| 262 | PyFrameObject *frame; |
| 263 | PyObject *exc, *val, *tb; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 264 | |
| 265 | /* Save and clear the current exception. Python functions must not be |
| 266 | called with an exception set. Calling Python functions happens when |
| 267 | the codec of the filesystem encoding is implemented in pure Python. */ |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 268 | PyErr_Fetch(&exc, &val, &tb); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 269 | |
| 270 | globals = PyDict_New(); |
| 271 | if (!globals) |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 272 | goto error; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 273 | code = PyCode_NewEmpty(filename, funcname, lineno); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 274 | if (!code) { |
| 275 | Py_DECREF(globals); |
| 276 | goto error; |
| 277 | } |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 278 | frame = PyFrame_New(PyThreadState_Get(), code, globals, NULL); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 279 | Py_DECREF(globals); |
| 280 | Py_DECREF(code); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 281 | if (!frame) |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 282 | goto error; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 283 | frame->f_lineno = lineno; |
| 284 | |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 285 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 286 | PyTraceBack_Here(frame); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 287 | Py_DECREF(frame); |
| 288 | return; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 289 | |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 290 | error: |
| 291 | _PyErr_ChainExceptions(exc, val, tb); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 294 | static PyObject * |
| 295 | _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] | 296 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 297 | Py_ssize_t i; |
| 298 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyObject *v; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 300 | Py_ssize_t npath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | size_t taillen; |
| 302 | PyObject *syspath; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 303 | PyObject *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | const char* tail; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 305 | PyObject *filebytes; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 306 | const char* filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | Py_ssize_t len; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 308 | PyObject* result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 309 | |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 310 | filebytes = PyUnicode_EncodeFSDefault(filename); |
| 311 | if (filebytes == NULL) { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 312 | PyErr_Clear(); |
| 313 | return NULL; |
| 314 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 315 | filepath = PyBytes_AS_STRING(filebytes); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | /* Search tail of filename in sys.path before giving up */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 318 | tail = strrchr(filepath, SEP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | if (tail == NULL) |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 320 | tail = filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | else |
| 322 | tail++; |
| 323 | taillen = strlen(tail); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 324 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 325 | syspath = _PySys_GetObjectId(&PyId_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (syspath == NULL || !PyList_Check(syspath)) |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 327 | goto error; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 328 | npath = PyList_Size(syspath); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | for (i = 0; i < npath; i++) { |
| 331 | v = PyList_GetItem(syspath, i); |
| 332 | if (v == NULL) { |
| 333 | PyErr_Clear(); |
| 334 | break; |
| 335 | } |
| 336 | if (!PyUnicode_Check(v)) |
| 337 | continue; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 338 | path = PyUnicode_EncodeFSDefault(v); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 339 | if (path == NULL) { |
| 340 | PyErr_Clear(); |
| 341 | continue; |
| 342 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 343 | len = PyBytes_GET_SIZE(path); |
| 344 | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { |
| 345 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | continue; /* Too long */ |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 347 | } |
| 348 | strcpy(namebuf, PyBytes_AS_STRING(path)); |
| 349 | Py_DECREF(path); |
Victor Stinner | 98ea54c | 2014-08-15 23:30:40 +0200 | [diff] [blame] | 350 | if (strlen(namebuf) != (size_t)len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | continue; /* v contains '\0' */ |
| 352 | if (len > 0 && namebuf[len-1] != SEP) |
| 353 | namebuf[len++] = SEP; |
| 354 | strcpy(namebuf+len, tail); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 355 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 356 | binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb"); |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 357 | if (binary != NULL) { |
| 358 | result = binary; |
| 359 | goto finally; |
| 360 | } |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 361 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 363 | goto error; |
| 364 | |
| 365 | error: |
| 366 | result = NULL; |
| 367 | finally: |
| 368 | Py_DECREF(filebytes); |
| 369 | return result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 372 | int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 373 | _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | int err = 0; |
| 376 | int fd; |
| 377 | int i; |
| 378 | char *found_encoding; |
| 379 | char *encoding; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 380 | PyObject *io; |
| 381 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 382 | PyObject *fob = NULL; |
| 383 | PyObject *lineobj = NULL; |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 384 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | char buf[MAXPATHLEN+1]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 386 | int kind; |
| 387 | void *data; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | /* open the file */ |
| 390 | if (filename == NULL) |
| 391 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 392 | |
| 393 | io = PyImport_ImportModuleNoBlock("io"); |
| 394 | if (io == NULL) |
| 395 | return -1; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 396 | binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb"); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 397 | |
| 398 | if (binary == NULL) { |
Victor Stinner | ceceaa0 | 2013-07-16 00:32:14 +0200 | [diff] [blame] | 399 | PyErr_Clear(); |
| 400 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 401 | binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); |
| 402 | if (binary == NULL) { |
| 403 | Py_DECREF(io); |
Victor Stinner | ceceaa0 | 2013-07-16 00:32:14 +0200 | [diff] [blame] | 404 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 405 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | /* use the right encoding to decode the file as unicode */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 409 | fd = PyObject_AsFileDescriptor(binary); |
Christian Heimes | 8c077bc | 2013-07-21 01:53:10 +0200 | [diff] [blame] | 410 | if (fd < 0) { |
| 411 | Py_DECREF(io); |
| 412 | Py_DECREF(binary); |
Benjamin Peterson | 04b01dc | 2013-07-21 13:26:13 -0700 | [diff] [blame] | 413 | return 0; |
Christian Heimes | 8c077bc | 2013-07-21 01:53:10 +0200 | [diff] [blame] | 414 | } |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 415 | found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); |
Victor Stinner | 5272fa9 | 2013-12-19 13:39:32 +0100 | [diff] [blame] | 416 | if (found_encoding == NULL) |
| 417 | PyErr_Clear(); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 418 | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; |
Christian Heimes | 1f34729 | 2013-07-21 02:12:35 +0200 | [diff] [blame] | 419 | /* Reset position */ |
| 420 | if (lseek(fd, 0, SEEK_SET) == (off_t)-1) { |
| 421 | Py_DECREF(io); |
| 422 | Py_DECREF(binary); |
| 423 | PyMem_FREE(found_encoding); |
Benjamin Peterson | 04b01dc | 2013-07-21 13:26:13 -0700 | [diff] [blame] | 424 | return 0; |
Christian Heimes | 1f34729 | 2013-07-21 02:12:35 +0200 | [diff] [blame] | 425 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 426 | fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 427 | Py_DECREF(io); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | PyMem_FREE(found_encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | if (fob == NULL) { |
| 431 | PyErr_Clear(); |
Victor Stinner | 81f241a | 2015-03-25 02:25:25 +0100 | [diff] [blame] | 432 | |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 433 | res = _PyObject_CallMethodId(binary, &PyId_close, NULL); |
Victor Stinner | 81f241a | 2015-03-25 02:25:25 +0100 | [diff] [blame] | 434 | Py_DECREF(binary); |
| 435 | if (res) |
| 436 | Py_DECREF(res); |
| 437 | else |
| 438 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | return 0; |
| 440 | } |
Victor Stinner | 81f241a | 2015-03-25 02:25:25 +0100 | [diff] [blame] | 441 | Py_DECREF(binary); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | /* get the line number lineno */ |
| 444 | for (i = 0; i < lineno; i++) { |
| 445 | Py_XDECREF(lineobj); |
| 446 | lineobj = PyFile_GetLine(fob, -1); |
| 447 | if (!lineobj) { |
Victor Stinner | 5e78f4d | 2014-10-30 10:17:27 +0100 | [diff] [blame] | 448 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | err = -1; |
| 450 | break; |
| 451 | } |
| 452 | } |
Victor Stinner | 3466bde | 2016-09-05 18:16:01 -0700 | [diff] [blame] | 453 | res = _PyObject_CallMethodId(fob, &PyId_close, NULL); |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 454 | if (res) |
| 455 | Py_DECREF(res); |
| 456 | else |
| 457 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | Py_DECREF(fob); |
| 459 | if (!lineobj || !PyUnicode_Check(lineobj)) { |
| 460 | Py_XDECREF(lineobj); |
| 461 | return err; |
| 462 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | /* remove the indentation of the line */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 465 | kind = PyUnicode_KIND(lineobj); |
| 466 | data = PyUnicode_DATA(lineobj); |
| 467 | for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) { |
| 468 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 469 | if (ch != ' ' && ch != '\t' && ch != '\014') |
| 470 | break; |
| 471 | } |
| 472 | if (i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | PyObject *truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 474 | truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | if (truncated) { |
| 476 | Py_DECREF(lineobj); |
| 477 | lineobj = truncated; |
| 478 | } else { |
| 479 | PyErr_Clear(); |
| 480 | } |
| 481 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | /* Write some spaces before the line */ |
| 484 | strcpy(buf, " "); |
| 485 | assert (strlen(buf) == 10); |
| 486 | while (indent > 0) { |
Benjamin Peterson | 0f9b7d3 | 2013-07-21 13:29:37 -0700 | [diff] [blame] | 487 | if (indent < 10) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | buf[indent] = '\0'; |
| 489 | err = PyFile_WriteString(buf, f); |
| 490 | if (err != 0) |
| 491 | break; |
| 492 | indent -= 10; |
| 493 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 494 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | /* finally display the line */ |
| 496 | if (err == 0) |
| 497 | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
| 498 | Py_DECREF(lineobj); |
| 499 | if (err == 0) |
| 500 | err = PyFile_WriteString("\n", f); |
| 501 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 504 | static int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 505 | tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 506 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 507 | int err; |
| 508 | PyObject *line; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | if (filename == NULL || name == NULL) |
| 511 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 512 | line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n", |
| 513 | filename, lineno, name); |
| 514 | if (line == NULL) |
| 515 | return -1; |
| 516 | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
| 517 | Py_DECREF(line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | if (err != 0) |
| 519 | return err; |
Kristján Valur Jónsson | c5963d3 | 2012-07-19 21:02:03 +0000 | [diff] [blame] | 520 | /* ignore errors since we can't report them, can we? */ |
| 521 | if (_Py_DisplaySourceLine(f, filename, lineno, 4)) |
| 522 | PyErr_Clear(); |
| 523 | return err; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 526 | static const int TB_RECURSIVE_CUTOFF = 3; // Also hardcoded in traceback.py. |
| 527 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 528 | static int |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 529 | tb_print_line_repeated(PyObject *f, long cnt) |
| 530 | { |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 531 | cnt -= TB_RECURSIVE_CUTOFF; |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 532 | PyObject *line = PyUnicode_FromFormat( |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 533 | (cnt > 1) |
| 534 | ? " [Previous line repeated %ld more times]\n" |
| 535 | : " [Previous line repeated %ld more time]\n", |
| 536 | cnt); |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 537 | if (line == NULL) { |
| 538 | return -1; |
| 539 | } |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 540 | int err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 541 | Py_DECREF(line); |
| 542 | return err; |
| 543 | } |
| 544 | |
| 545 | static int |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 546 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | int err = 0; |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 549 | Py_ssize_t depth = 0; |
Nick Coghlan | d003423 | 2016-08-15 13:11:34 +1000 | [diff] [blame] | 550 | PyObject *last_file = NULL; |
| 551 | int last_line = -1; |
| 552 | PyObject *last_name = NULL; |
| 553 | long cnt = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | PyTracebackObject *tb1 = tb; |
| 555 | while (tb1 != NULL) { |
| 556 | depth++; |
| 557 | tb1 = tb1->tb_next; |
| 558 | } |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 559 | while (tb != NULL && depth > limit) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | depth--; |
| 561 | tb = tb->tb_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 562 | } |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 563 | while (tb != NULL && err == 0) { |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 564 | if (last_file == NULL || |
| 565 | tb->tb_frame->f_code->co_filename != last_file || |
| 566 | last_line == -1 || tb->tb_lineno != last_line || |
| 567 | last_name == NULL || tb->tb_frame->f_code->co_name != last_name) { |
| 568 | if (cnt > TB_RECURSIVE_CUTOFF) { |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 569 | err = tb_print_line_repeated(f, cnt); |
| 570 | } |
| 571 | last_file = tb->tb_frame->f_code->co_filename; |
| 572 | last_line = tb->tb_lineno; |
| 573 | last_name = tb->tb_frame->f_code->co_name; |
| 574 | cnt = 0; |
| 575 | } |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 576 | cnt++; |
| 577 | if (err == 0 && cnt <= TB_RECURSIVE_CUTOFF) { |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 578 | err = tb_displayline(f, |
| 579 | tb->tb_frame->f_code->co_filename, |
| 580 | tb->tb_lineno, |
| 581 | tb->tb_frame->f_code->co_name); |
| 582 | if (err == 0) { |
| 583 | err = PyErr_CheckSignals(); |
| 584 | } |
| 585 | } |
| 586 | tb = tb->tb_next; |
| 587 | } |
Benjamin Peterson | d545869 | 2018-09-10 08:43:10 -0700 | [diff] [blame] | 588 | if (err == 0 && cnt > TB_RECURSIVE_CUTOFF) { |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 589 | err = tb_print_line_repeated(f, cnt); |
Nick Coghlan | d003423 | 2016-08-15 13:11:34 +1000 | [diff] [blame] | 590 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 594 | #define PyTraceBack_LIMIT 1000 |
| 595 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 596 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 597 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | int err; |
| 600 | PyObject *limitv; |
| 601 | long limit = PyTraceBack_LIMIT; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | if (v == NULL) |
| 604 | return 0; |
| 605 | if (!PyTraceBack_Check(v)) { |
| 606 | PyErr_BadInternalCall(); |
| 607 | return -1; |
| 608 | } |
| 609 | limitv = PySys_GetObject("tracebacklimit"); |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 610 | if (limitv && PyLong_Check(limitv)) { |
| 611 | int overflow; |
| 612 | limit = PyLong_AsLongAndOverflow(limitv, &overflow); |
| 613 | if (overflow > 0) { |
| 614 | limit = LONG_MAX; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | } |
| 616 | else if (limit <= 0) { |
Serhiy Storchaka | edad8ee | 2017-11-15 17:38:52 +0200 | [diff] [blame] | 617 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | } |
| 620 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 621 | if (!err) |
| 622 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 623 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 624 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 625 | |
| 626 | /* Reverse a string. For example, "abcd" becomes "dcba". |
| 627 | |
| 628 | This function is signal safe. */ |
| 629 | |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 630 | void |
| 631 | _Py_DumpDecimal(int fd, unsigned long value) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 632 | { |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 633 | /* maximum number of characters required for output of %lld or %p. |
| 634 | We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
| 635 | plus 1 for the null byte. 53/22 is an upper bound for log10(256). */ |
| 636 | char buffer[1 + (sizeof(unsigned long)*53-1) / 22 + 1]; |
| 637 | char *ptr, *end; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 638 | |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 639 | end = &buffer[Py_ARRAY_LENGTH(buffer) - 1]; |
| 640 | ptr = end; |
| 641 | *ptr = '\0'; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 642 | do { |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 643 | --ptr; |
| 644 | assert(ptr >= buffer); |
| 645 | *ptr = '0' + (value % 10); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 646 | value /= 10; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 647 | } while (value); |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 648 | |
| 649 | _Py_write_noraise(fd, ptr, end - ptr); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 650 | } |
| 651 | |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 652 | /* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 653 | and write it into the file fd. |
| 654 | |
| 655 | This function is signal safe. */ |
| 656 | |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 657 | void |
| 658 | _Py_DumpHexadecimal(int fd, unsigned long value, Py_ssize_t width) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 659 | { |
Victor Stinner | 013024e | 2016-03-16 09:43:14 +0100 | [diff] [blame] | 660 | char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end; |
| 661 | const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1; |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 662 | |
| 663 | if (width > size) |
| 664 | width = size; |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 665 | /* it's ok if width is negative */ |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 666 | |
Victor Stinner | 013024e | 2016-03-16 09:43:14 +0100 | [diff] [blame] | 667 | end = &buffer[size]; |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 668 | ptr = end; |
| 669 | *ptr = '\0'; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 670 | do { |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 671 | --ptr; |
| 672 | assert(ptr >= buffer); |
| 673 | *ptr = Py_hexdigits[value & 15]; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 674 | value >>= 4; |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 675 | } while ((end - ptr) < width || value); |
| 676 | |
| 677 | _Py_write_noraise(fd, ptr, end - ptr); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 680 | void |
| 681 | _Py_DumpASCII(int fd, PyObject *text) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 682 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 683 | PyASCIIObject *ascii = (PyASCIIObject *)text; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 684 | Py_ssize_t i, size; |
| 685 | int truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 686 | int kind; |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 687 | void *data = NULL; |
| 688 | wchar_t *wstr = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 689 | Py_UCS4 ch; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 690 | |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 691 | if (!PyUnicode_Check(text)) |
| 692 | return; |
| 693 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 694 | size = ascii->length; |
| 695 | kind = ascii->state.kind; |
Victor Stinner | 1c3069a | 2016-03-23 16:10:07 +0100 | [diff] [blame] | 696 | if (kind == PyUnicode_WCHAR_KIND) { |
| 697 | wstr = ((PyASCIIObject *)text)->wstr; |
| 698 | if (wstr == NULL) |
| 699 | return; |
| 700 | size = ((PyCompactUnicodeObject *)text)->wstr_length; |
| 701 | } |
| 702 | else if (ascii->state.compact) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 703 | if (ascii->state.ascii) |
| 704 | data = ((PyASCIIObject*)text) + 1; |
| 705 | else |
| 706 | data = ((PyCompactUnicodeObject*)text) + 1; |
| 707 | } |
Victor Stinner | 1c3069a | 2016-03-23 16:10:07 +0100 | [diff] [blame] | 708 | else { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 709 | data = ((PyUnicodeObject *)text)->data.any; |
| 710 | if (data == NULL) |
| 711 | return; |
| 712 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 713 | |
| 714 | if (MAX_STRING_LENGTH < size) { |
| 715 | size = MAX_STRING_LENGTH; |
| 716 | truncated = 1; |
| 717 | } |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 718 | else { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 719 | truncated = 0; |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 720 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 721 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 722 | for (i=0; i < size; i++) { |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 723 | if (kind != PyUnicode_WCHAR_KIND) |
| 724 | ch = PyUnicode_READ(kind, data, i); |
| 725 | else |
| 726 | ch = wstr[i]; |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 727 | if (' ' <= ch && ch <= 126) { |
| 728 | /* printable ASCII character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 729 | char c = (char)ch; |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 730 | _Py_write_noraise(fd, &c, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 731 | } |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 732 | else if (ch <= 0xff) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 733 | PUTS(fd, "\\x"); |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 734 | _Py_DumpHexadecimal(fd, ch, 2); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 735 | } |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 736 | else if (ch <= 0xffff) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 737 | PUTS(fd, "\\u"); |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 738 | _Py_DumpHexadecimal(fd, ch, 4); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 739 | } |
| 740 | else { |
| 741 | PUTS(fd, "\\U"); |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 742 | _Py_DumpHexadecimal(fd, ch, 8); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 743 | } |
| 744 | } |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 745 | if (truncated) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 746 | PUTS(fd, "..."); |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 747 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /* Write a frame into the file fd: "File "xxx", line xxx in xxx". |
| 751 | |
| 752 | This function is signal safe. */ |
| 753 | |
| 754 | static void |
| 755 | dump_frame(int fd, PyFrameObject *frame) |
| 756 | { |
| 757 | PyCodeObject *code; |
| 758 | int lineno; |
| 759 | |
| 760 | code = frame->f_code; |
| 761 | PUTS(fd, " File "); |
| 762 | if (code != NULL && code->co_filename != NULL |
| 763 | && PyUnicode_Check(code->co_filename)) |
| 764 | { |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 765 | PUTS(fd, "\""); |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 766 | _Py_DumpASCII(fd, code->co_filename); |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 767 | PUTS(fd, "\""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 768 | } else { |
| 769 | PUTS(fd, "???"); |
| 770 | } |
| 771 | |
| 772 | /* 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] | 773 | lineno = PyCode_Addr2Line(code, frame->f_lasti); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 774 | PUTS(fd, ", line "); |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 775 | if (lineno >= 0) { |
| 776 | _Py_DumpDecimal(fd, (unsigned long)lineno); |
| 777 | } |
| 778 | else { |
| 779 | PUTS(fd, "???"); |
| 780 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 781 | PUTS(fd, " in "); |
| 782 | |
| 783 | if (code != NULL && code->co_name != NULL |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 784 | && PyUnicode_Check(code->co_name)) { |
| 785 | _Py_DumpASCII(fd, code->co_name); |
| 786 | } |
| 787 | else { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 788 | PUTS(fd, "???"); |
Victor Stinner | 89e7cdc | 2016-03-15 21:49:37 +0100 | [diff] [blame] | 789 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 790 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 791 | PUTS(fd, "\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 792 | } |
| 793 | |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 794 | static void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 795 | dump_traceback(int fd, PyThreadState *tstate, int write_header) |
| 796 | { |
| 797 | PyFrameObject *frame; |
| 798 | unsigned int depth; |
| 799 | |
Victor Stinner | 47bbab9 | 2019-09-18 14:10:16 +0200 | [diff] [blame] | 800 | if (write_header) { |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 801 | PUTS(fd, "Stack (most recent call first):\n"); |
Victor Stinner | 47bbab9 | 2019-09-18 14:10:16 +0200 | [diff] [blame] | 802 | } |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 803 | |
| 804 | frame = _PyThreadState_GetFrame(tstate); |
Victor Stinner | 47bbab9 | 2019-09-18 14:10:16 +0200 | [diff] [blame] | 805 | if (frame == NULL) { |
| 806 | PUTS(fd, "<no Python frame>\n"); |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 807 | return; |
Victor Stinner | 47bbab9 | 2019-09-18 14:10:16 +0200 | [diff] [blame] | 808 | } |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 809 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 810 | depth = 0; |
| 811 | while (frame != NULL) { |
| 812 | if (MAX_FRAME_DEPTH <= depth) { |
| 813 | PUTS(fd, " ...\n"); |
| 814 | break; |
| 815 | } |
| 816 | if (!PyFrame_Check(frame)) |
| 817 | break; |
| 818 | dump_frame(fd, frame); |
| 819 | frame = frame->f_back; |
| 820 | depth++; |
| 821 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 824 | /* Dump the traceback of a Python thread into fd. Use write() to write the |
| 825 | traceback and retry if write() is interrupted by a signal (failed with |
| 826 | EINTR), but don't call the Python signal handler. |
| 827 | |
| 828 | The caller is responsible to call PyErr_CheckSignals() to call Python signal |
| 829 | handlers if signals were received. */ |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 830 | void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 831 | _Py_DumpTraceback(int fd, PyThreadState *tstate) |
| 832 | { |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 833 | dump_traceback(fd, tstate, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if |
| 837 | is_current is true, "Thread 0xHHHH:\n" otherwise. |
| 838 | |
| 839 | This function is signal safe. */ |
| 840 | |
| 841 | static void |
| 842 | write_thread_id(int fd, PyThreadState *tstate, int is_current) |
| 843 | { |
| 844 | if (is_current) |
| 845 | PUTS(fd, "Current thread 0x"); |
| 846 | else |
| 847 | PUTS(fd, "Thread 0x"); |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 848 | _Py_DumpHexadecimal(fd, |
Serhiy Storchaka | aefa7eb | 2017-03-23 15:48:39 +0200 | [diff] [blame] | 849 | tstate->thread_id, |
Victor Stinner | bd31b7c | 2016-03-23 10:32:26 +0100 | [diff] [blame] | 850 | sizeof(unsigned long) * 2); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 851 | PUTS(fd, " (most recent call first):\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 852 | } |
| 853 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 854 | /* Dump the traceback of all Python threads into fd. Use write() to write the |
| 855 | traceback and retry if write() is interrupted by a signal (failed with |
| 856 | EINTR), but don't call the Python signal handler. |
| 857 | |
| 858 | The caller is responsible to call PyErr_CheckSignals() to call Python signal |
| 859 | handlers if signals were received. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 860 | const char* |
| 861 | _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp, |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 862 | PyThreadState *current_tstate) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 863 | { |
| 864 | PyThreadState *tstate; |
| 865 | unsigned int nthreads; |
| 866 | |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 867 | if (current_tstate == NULL) { |
| 868 | /* _Py_DumpTracebackThreads() is called from signal handlers by |
| 869 | faulthandler. |
| 870 | |
| 871 | SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals |
| 872 | and are thus delivered to the thread that caused the fault. Get the |
| 873 | Python thread state of the current thread. |
| 874 | |
| 875 | PyThreadState_Get() doesn't give the state of the thread that caused |
Victor Stinner | 47bbab9 | 2019-09-18 14:10:16 +0200 | [diff] [blame] | 876 | the fault if the thread released the GIL, and so |
| 877 | _PyThreadState_GET() cannot be used. Read the thread specific |
| 878 | storage (TSS) instead: call PyGILState_GetThisThreadState(). */ |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 879 | current_tstate = PyGILState_GetThisThreadState(); |
| 880 | } |
| 881 | |
| 882 | if (interp == NULL) { |
| 883 | if (current_tstate == NULL) { |
| 884 | interp = _PyGILState_GetInterpreterStateUnsafe(); |
| 885 | if (interp == NULL) { |
| 886 | /* We need the interpreter state to get Python threads */ |
| 887 | return "unable to get the interpreter state"; |
| 888 | } |
| 889 | } |
| 890 | else { |
| 891 | interp = current_tstate->interp; |
| 892 | } |
| 893 | } |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 894 | assert(interp != NULL); |
| 895 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 896 | /* Get the current interpreter from the current thread */ |
| 897 | tstate = PyInterpreterState_ThreadHead(interp); |
| 898 | if (tstate == NULL) |
| 899 | return "unable to get the thread head state"; |
| 900 | |
| 901 | /* Dump the traceback of each thread */ |
| 902 | tstate = PyInterpreterState_ThreadHead(interp); |
| 903 | nthreads = 0; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 904 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 905 | do |
| 906 | { |
| 907 | if (nthreads != 0) |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 908 | PUTS(fd, "\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 909 | if (nthreads >= MAX_NTHREADS) { |
| 910 | PUTS(fd, "...\n"); |
| 911 | break; |
| 912 | } |
Victor Stinner | 861d9ab | 2016-03-16 22:45:24 +0100 | [diff] [blame] | 913 | write_thread_id(fd, tstate, tstate == current_tstate); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 914 | dump_traceback(fd, tstate, 0); |
| 915 | tstate = PyThreadState_Next(tstate); |
| 916 | nthreads++; |
| 917 | } while (tstate != NULL); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 918 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 919 | |
| 920 | return NULL; |
| 921 | } |
| 922 | |