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 | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 16 | #define PUTS(fd, str) _Py_write_noraise(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 | { |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 135 | PyObject *exc, *val, *tb, *newtb; |
| 136 | PyErr_Fetch(&exc, &val, &tb); |
| 137 | newtb = (PyObject *)newtracebackobject((PyTracebackObject *)tb, frame); |
| 138 | if (newtb == NULL) { |
| 139 | _PyErr_ChainExceptions(exc, val, tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | return -1; |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 141 | } |
| 142 | PyErr_Restore(exc, val, newtb); |
| 143 | Py_XDECREF(tb); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | return 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 147 | /* Insert a frame into the traceback for (funcname, filename, lineno). */ |
Serhiy Storchaka | 73c95f1 | 2015-06-21 15:59:46 +0300 | [diff] [blame] | 148 | void _PyTraceback_Add(const char *funcname, const char *filename, int lineno) |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 149 | { |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 150 | PyObject *globals; |
| 151 | PyCodeObject *code; |
| 152 | PyFrameObject *frame; |
| 153 | PyObject *exc, *val, *tb; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 154 | |
| 155 | /* Save and clear the current exception. Python functions must not be |
| 156 | called with an exception set. Calling Python functions happens when |
| 157 | the codec of the filesystem encoding is implemented in pure Python. */ |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 158 | PyErr_Fetch(&exc, &val, &tb); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 159 | |
| 160 | globals = PyDict_New(); |
| 161 | if (!globals) |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 162 | goto error; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 163 | code = PyCode_NewEmpty(filename, funcname, lineno); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 164 | if (!code) { |
| 165 | Py_DECREF(globals); |
| 166 | goto error; |
| 167 | } |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 168 | frame = PyFrame_New(PyThreadState_Get(), code, globals, NULL); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 169 | Py_DECREF(globals); |
| 170 | Py_DECREF(code); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 171 | if (!frame) |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 172 | goto error; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 173 | frame->f_lineno = lineno; |
| 174 | |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 175 | PyErr_Restore(exc, val, tb); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 176 | PyTraceBack_Here(frame); |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 177 | Py_DECREF(frame); |
| 178 | return; |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 179 | |
Serhiy Storchaka | 04eb777 | 2016-10-18 13:23:18 +0300 | [diff] [blame] | 180 | error: |
| 181 | _PyErr_ChainExceptions(exc, val, tb); |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 184 | static PyObject * |
| 185 | _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] | 186 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 187 | Py_ssize_t i; |
| 188 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | PyObject *v; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 190 | Py_ssize_t npath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | size_t taillen; |
| 192 | PyObject *syspath; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 193 | PyObject *path; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | const char* tail; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 195 | PyObject *filebytes; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 196 | const char* filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | Py_ssize_t len; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 198 | PyObject* result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 199 | |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 200 | filebytes = PyUnicode_EncodeFSDefault(filename); |
| 201 | if (filebytes == NULL) { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 202 | PyErr_Clear(); |
| 203 | return NULL; |
| 204 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 205 | filepath = PyBytes_AS_STRING(filebytes); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | /* Search tail of filename in sys.path before giving up */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 208 | tail = strrchr(filepath, SEP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | if (tail == NULL) |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 210 | tail = filepath; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 211 | else |
| 212 | tail++; |
| 213 | taillen = strlen(tail); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 214 | |
Victor Stinner | bd303c1 | 2013-11-07 23:07:29 +0100 | [diff] [blame] | 215 | syspath = _PySys_GetObjectId(&PyId_path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | if (syspath == NULL || !PyList_Check(syspath)) |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 217 | goto error; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 218 | npath = PyList_Size(syspath); |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | for (i = 0; i < npath; i++) { |
| 221 | v = PyList_GetItem(syspath, i); |
| 222 | if (v == NULL) { |
| 223 | PyErr_Clear(); |
| 224 | break; |
| 225 | } |
| 226 | if (!PyUnicode_Check(v)) |
| 227 | continue; |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 228 | path = PyUnicode_EncodeFSDefault(v); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 229 | if (path == NULL) { |
| 230 | PyErr_Clear(); |
| 231 | continue; |
| 232 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 233 | len = PyBytes_GET_SIZE(path); |
| 234 | if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { |
| 235 | Py_DECREF(path); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | continue; /* Too long */ |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 237 | } |
| 238 | strcpy(namebuf, PyBytes_AS_STRING(path)); |
| 239 | Py_DECREF(path); |
Victor Stinner | 98ea54c | 2014-08-15 23:30:40 +0200 | [diff] [blame] | 240 | if (strlen(namebuf) != (size_t)len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | continue; /* v contains '\0' */ |
| 242 | if (len > 0 && namebuf[len-1] != SEP) |
| 243 | namebuf[len++] = SEP; |
| 244 | strcpy(namebuf+len, tail); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 245 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 246 | binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb"); |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 247 | if (binary != NULL) { |
| 248 | result = binary; |
| 249 | goto finally; |
| 250 | } |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 251 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | } |
Victor Stinner | 4c7c8c3 | 2010-10-16 13:14:10 +0000 | [diff] [blame] | 253 | goto error; |
| 254 | |
| 255 | error: |
| 256 | result = NULL; |
| 257 | finally: |
| 258 | Py_DECREF(filebytes); |
| 259 | return result; |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 262 | int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 263 | _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | int err = 0; |
| 266 | int fd; |
| 267 | int i; |
| 268 | char *found_encoding; |
| 269 | char *encoding; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 270 | PyObject *io; |
| 271 | PyObject *binary; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | PyObject *fob = NULL; |
| 273 | PyObject *lineobj = NULL; |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 274 | PyObject *res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | char buf[MAXPATHLEN+1]; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 276 | int kind; |
| 277 | void *data; |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | /* open the file */ |
| 280 | if (filename == NULL) |
| 281 | return 0; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 282 | |
| 283 | io = PyImport_ImportModuleNoBlock("io"); |
| 284 | if (io == NULL) |
| 285 | return -1; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 286 | binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb"); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 287 | |
| 288 | if (binary == NULL) { |
Victor Stinner | ceceaa0 | 2013-07-16 00:32:14 +0200 | [diff] [blame] | 289 | PyErr_Clear(); |
| 290 | |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 291 | binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); |
| 292 | if (binary == NULL) { |
| 293 | Py_DECREF(io); |
Victor Stinner | ceceaa0 | 2013-07-16 00:32:14 +0200 | [diff] [blame] | 294 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 295 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | } |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | /* use the right encoding to decode the file as unicode */ |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 299 | fd = PyObject_AsFileDescriptor(binary); |
Christian Heimes | 8c077bc | 2013-07-21 01:53:10 +0200 | [diff] [blame] | 300 | if (fd < 0) { |
| 301 | Py_DECREF(io); |
| 302 | Py_DECREF(binary); |
Benjamin Peterson | 04b01dc | 2013-07-21 13:26:13 -0700 | [diff] [blame] | 303 | return 0; |
Christian Heimes | 8c077bc | 2013-07-21 01:53:10 +0200 | [diff] [blame] | 304 | } |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 305 | found_encoding = PyTokenizer_FindEncodingFilename(fd, filename); |
Victor Stinner | 5272fa9 | 2013-12-19 13:39:32 +0100 | [diff] [blame] | 306 | if (found_encoding == NULL) |
| 307 | PyErr_Clear(); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 308 | encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; |
Christian Heimes | 1f34729 | 2013-07-21 02:12:35 +0200 | [diff] [blame] | 309 | /* Reset position */ |
| 310 | if (lseek(fd, 0, SEEK_SET) == (off_t)-1) { |
| 311 | Py_DECREF(io); |
| 312 | Py_DECREF(binary); |
| 313 | PyMem_FREE(found_encoding); |
Benjamin Peterson | 04b01dc | 2013-07-21 13:26:13 -0700 | [diff] [blame] | 314 | return 0; |
Christian Heimes | 1f34729 | 2013-07-21 02:12:35 +0200 | [diff] [blame] | 315 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 316 | fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 317 | Py_DECREF(io); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | PyMem_FREE(found_encoding); |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | if (fob == NULL) { |
| 321 | PyErr_Clear(); |
Victor Stinner | 81f241a | 2015-03-25 02:25:25 +0100 | [diff] [blame] | 322 | |
| 323 | res = _PyObject_CallMethodId(binary, &PyId_close, ""); |
| 324 | Py_DECREF(binary); |
| 325 | if (res) |
| 326 | Py_DECREF(res); |
| 327 | else |
| 328 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | return 0; |
| 330 | } |
Victor Stinner | 81f241a | 2015-03-25 02:25:25 +0100 | [diff] [blame] | 331 | Py_DECREF(binary); |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | /* get the line number lineno */ |
| 334 | for (i = 0; i < lineno; i++) { |
| 335 | Py_XDECREF(lineobj); |
| 336 | lineobj = PyFile_GetLine(fob, -1); |
| 337 | if (!lineobj) { |
Victor Stinner | 5e78f4d | 2014-10-30 10:17:27 +0100 | [diff] [blame] | 338 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | err = -1; |
| 340 | break; |
| 341 | } |
| 342 | } |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 343 | res = _PyObject_CallMethodId(fob, &PyId_close, ""); |
Antoine Pitrou | b86680e | 2010-10-14 21:15:17 +0000 | [diff] [blame] | 344 | if (res) |
| 345 | Py_DECREF(res); |
| 346 | else |
| 347 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | Py_DECREF(fob); |
| 349 | if (!lineobj || !PyUnicode_Check(lineobj)) { |
| 350 | Py_XDECREF(lineobj); |
| 351 | return err; |
| 352 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | /* remove the indentation of the line */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 355 | kind = PyUnicode_KIND(lineobj); |
| 356 | data = PyUnicode_DATA(lineobj); |
| 357 | for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) { |
| 358 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
| 359 | if (ch != ' ' && ch != '\t' && ch != '\014') |
| 360 | break; |
| 361 | } |
| 362 | if (i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | PyObject *truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 364 | truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | if (truncated) { |
| 366 | Py_DECREF(lineobj); |
| 367 | lineobj = truncated; |
| 368 | } else { |
| 369 | PyErr_Clear(); |
| 370 | } |
| 371 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | /* Write some spaces before the line */ |
| 374 | strcpy(buf, " "); |
| 375 | assert (strlen(buf) == 10); |
| 376 | while (indent > 0) { |
Benjamin Peterson | 0f9b7d3 | 2013-07-21 13:29:37 -0700 | [diff] [blame] | 377 | if (indent < 10) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | buf[indent] = '\0'; |
| 379 | err = PyFile_WriteString(buf, f); |
| 380 | if (err != 0) |
| 381 | break; |
| 382 | indent -= 10; |
| 383 | } |
Amaury Forgeot d'Arc | cf8016a | 2008-10-09 23:37:48 +0000 | [diff] [blame] | 384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | /* finally display the line */ |
| 386 | if (err == 0) |
| 387 | err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW); |
| 388 | Py_DECREF(lineobj); |
| 389 | if (err == 0) |
| 390 | err = PyFile_WriteString("\n", f); |
| 391 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Guido van Rossum | 7e8d26d | 1997-05-22 22:35:47 +0000 | [diff] [blame] | 394 | static int |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 395 | tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 396 | { |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 397 | int err; |
| 398 | PyObject *line; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 400 | if (filename == NULL || name == NULL) |
| 401 | return -1; |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 402 | line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n", |
| 403 | filename, lineno, name); |
| 404 | if (line == NULL) |
| 405 | return -1; |
| 406 | err = PyFile_WriteObject(line, f, Py_PRINT_RAW); |
| 407 | Py_DECREF(line); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | if (err != 0) |
| 409 | return err; |
Kristján Valur Jónsson | c5963d3 | 2012-07-19 21:02:03 +0000 | [diff] [blame] | 410 | /* ignore errors since we can't report them, can we? */ |
| 411 | if (_Py_DisplaySourceLine(f, filename, lineno, 4)) |
| 412 | PyErr_Clear(); |
| 413 | return err; |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int |
| 417 | tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) |
| 418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | int err = 0; |
| 420 | long depth = 0; |
| 421 | PyTracebackObject *tb1 = tb; |
| 422 | while (tb1 != NULL) { |
| 423 | depth++; |
| 424 | tb1 = tb1->tb_next; |
| 425 | } |
| 426 | while (tb != NULL && err == 0) { |
| 427 | if (depth <= limit) { |
| 428 | err = tb_displayline(f, |
Victor Stinner | 0fe25a4 | 2010-06-17 23:08:50 +0000 | [diff] [blame] | 429 | tb->tb_frame->f_code->co_filename, |
| 430 | tb->tb_lineno, |
| 431 | tb->tb_frame->f_code->co_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | } |
| 433 | depth--; |
| 434 | tb = tb->tb_next; |
| 435 | if (err == 0) |
| 436 | err = PyErr_CheckSignals(); |
| 437 | } |
| 438 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 441 | #define PyTraceBack_LIMIT 1000 |
| 442 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 443 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 444 | PyTraceBack_Print(PyObject *v, PyObject *f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 445 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | int err; |
| 447 | PyObject *limitv; |
| 448 | long limit = PyTraceBack_LIMIT; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | if (v == NULL) |
| 451 | return 0; |
| 452 | if (!PyTraceBack_Check(v)) { |
| 453 | PyErr_BadInternalCall(); |
| 454 | return -1; |
| 455 | } |
| 456 | limitv = PySys_GetObject("tracebacklimit"); |
| 457 | if (limitv) { |
| 458 | PyObject *exc_type, *exc_value, *exc_tb; |
Christian Heimes | 0fbab7f | 2007-12-04 21:55:18 +0000 | [diff] [blame] | 459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | PyErr_Fetch(&exc_type, &exc_value, &exc_tb); |
| 461 | limit = PyLong_AsLong(limitv); |
| 462 | if (limit == -1 && PyErr_Occurred()) { |
| 463 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 464 | limit = PyTraceBack_LIMIT; |
| 465 | } |
| 466 | else { |
| 467 | Py_XDECREF(exc_type); |
| 468 | Py_XDECREF(exc_value); |
| 469 | Py_XDECREF(exc_tb); |
| 470 | return 0; |
| 471 | } |
| 472 | } |
| 473 | else if (limit <= 0) { |
| 474 | limit = PyTraceBack_LIMIT; |
| 475 | } |
| 476 | PyErr_Restore(exc_type, exc_value, exc_tb); |
| 477 | } |
| 478 | err = PyFile_WriteString("Traceback (most recent call last):\n", f); |
| 479 | if (!err) |
| 480 | err = tb_printinternal((PyTracebackObject *)v, f, limit); |
| 481 | return err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 482 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 483 | |
| 484 | /* Reverse a string. For example, "abcd" becomes "dcba". |
| 485 | |
| 486 | This function is signal safe. */ |
| 487 | |
| 488 | static void |
| 489 | reverse_string(char *text, const size_t len) |
| 490 | { |
| 491 | char tmp; |
| 492 | size_t i, j; |
| 493 | if (len == 0) |
| 494 | return; |
| 495 | for (i=0, j=len-1; i < j; i++, j--) { |
| 496 | tmp = text[i]; |
| 497 | text[i] = text[j]; |
| 498 | text[j] = tmp; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* Format an integer in range [0; 999999] to decimal, |
| 503 | and write it into the file fd. |
| 504 | |
| 505 | This function is signal safe. */ |
| 506 | |
| 507 | static void |
| 508 | dump_decimal(int fd, int value) |
| 509 | { |
| 510 | char buffer[7]; |
| 511 | int len; |
| 512 | if (value < 0 || 999999 < value) |
| 513 | return; |
| 514 | len = 0; |
| 515 | do { |
| 516 | buffer[len] = '0' + (value % 10); |
| 517 | value /= 10; |
| 518 | len++; |
| 519 | } while (value); |
| 520 | reverse_string(buffer, len); |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 521 | _Py_write_noraise(fd, buffer, len); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 524 | /* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits, |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 525 | and write it into the file fd. |
| 526 | |
| 527 | This function is signal safe. */ |
| 528 | |
| 529 | static void |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 530 | dump_hexadecimal(int fd, unsigned long value, int width) |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 531 | { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 532 | int len; |
| 533 | char buffer[sizeof(unsigned long) * 2 + 1]; |
| 534 | len = 0; |
| 535 | do { |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 536 | buffer[len] = Py_hexdigits[value & 15]; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 537 | value >>= 4; |
| 538 | len++; |
| 539 | } while (len < width || value); |
| 540 | reverse_string(buffer, len); |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 541 | _Py_write_noraise(fd, buffer, len); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /* Write an unicode object into the file fd using ascii+backslashreplace. |
| 545 | |
| 546 | This function is signal safe. */ |
| 547 | |
| 548 | static void |
| 549 | dump_ascii(int fd, PyObject *text) |
| 550 | { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 551 | PyASCIIObject *ascii = (PyASCIIObject *)text; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 552 | Py_ssize_t i, size; |
| 553 | int truncated; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 554 | int kind; |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 555 | void *data = NULL; |
| 556 | wchar_t *wstr = NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 557 | Py_UCS4 ch; |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 558 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 559 | size = ascii->length; |
| 560 | kind = ascii->state.kind; |
| 561 | if (ascii->state.compact) { |
| 562 | if (ascii->state.ascii) |
| 563 | data = ((PyASCIIObject*)text) + 1; |
| 564 | else |
| 565 | data = ((PyCompactUnicodeObject*)text) + 1; |
| 566 | } |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 567 | else if (kind != PyUnicode_WCHAR_KIND) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 568 | data = ((PyUnicodeObject *)text)->data.any; |
| 569 | if (data == NULL) |
| 570 | return; |
| 571 | } |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 572 | else { |
| 573 | wstr = ((PyASCIIObject *)text)->wstr; |
| 574 | if (wstr == NULL) |
| 575 | return; |
| 576 | size = ((PyCompactUnicodeObject *)text)->wstr_length; |
| 577 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 578 | |
| 579 | if (MAX_STRING_LENGTH < size) { |
| 580 | size = MAX_STRING_LENGTH; |
| 581 | truncated = 1; |
| 582 | } |
| 583 | else |
| 584 | truncated = 0; |
| 585 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 586 | for (i=0; i < size; i++) { |
Victor Stinner | a336de7 | 2011-10-05 22:44:12 +0200 | [diff] [blame] | 587 | if (kind != PyUnicode_WCHAR_KIND) |
| 588 | ch = PyUnicode_READ(kind, data, i); |
| 589 | else |
| 590 | ch = wstr[i]; |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 591 | if (' ' <= ch && ch <= 126) { |
| 592 | /* printable ASCII character */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 593 | char c = (char)ch; |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 594 | _Py_write_noraise(fd, &c, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 595 | } |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 596 | else if (ch <= 0xff) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 597 | PUTS(fd, "\\x"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 598 | dump_hexadecimal(fd, ch, 2); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 599 | } |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 600 | else if (ch <= 0xffff) { |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 601 | PUTS(fd, "\\u"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 602 | dump_hexadecimal(fd, ch, 4); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 603 | } |
| 604 | else { |
| 605 | PUTS(fd, "\\U"); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 606 | dump_hexadecimal(fd, ch, 8); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | if (truncated) |
| 610 | PUTS(fd, "..."); |
| 611 | } |
| 612 | |
| 613 | /* Write a frame into the file fd: "File "xxx", line xxx in xxx". |
| 614 | |
| 615 | This function is signal safe. */ |
| 616 | |
| 617 | static void |
| 618 | dump_frame(int fd, PyFrameObject *frame) |
| 619 | { |
| 620 | PyCodeObject *code; |
| 621 | int lineno; |
| 622 | |
| 623 | code = frame->f_code; |
| 624 | PUTS(fd, " File "); |
| 625 | if (code != NULL && code->co_filename != NULL |
| 626 | && PyUnicode_Check(code->co_filename)) |
| 627 | { |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 628 | PUTS(fd, "\""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 629 | dump_ascii(fd, code->co_filename); |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 630 | PUTS(fd, "\""); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 631 | } else { |
| 632 | PUTS(fd, "???"); |
| 633 | } |
| 634 | |
| 635 | /* 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] | 636 | lineno = PyCode_Addr2Line(code, frame->f_lasti); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 637 | PUTS(fd, ", line "); |
| 638 | dump_decimal(fd, lineno); |
| 639 | PUTS(fd, " in "); |
| 640 | |
| 641 | if (code != NULL && code->co_name != NULL |
| 642 | && PyUnicode_Check(code->co_name)) |
| 643 | dump_ascii(fd, code->co_name); |
| 644 | else |
| 645 | PUTS(fd, "???"); |
| 646 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 647 | PUTS(fd, "\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 650 | static void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 651 | dump_traceback(int fd, PyThreadState *tstate, int write_header) |
| 652 | { |
| 653 | PyFrameObject *frame; |
| 654 | unsigned int depth; |
| 655 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 656 | if (write_header) |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 657 | PUTS(fd, "Stack (most recent call first):\n"); |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 658 | |
| 659 | frame = _PyThreadState_GetFrame(tstate); |
| 660 | if (frame == NULL) |
| 661 | return; |
| 662 | |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 663 | depth = 0; |
| 664 | while (frame != NULL) { |
| 665 | if (MAX_FRAME_DEPTH <= depth) { |
| 666 | PUTS(fd, " ...\n"); |
| 667 | break; |
| 668 | } |
| 669 | if (!PyFrame_Check(frame)) |
| 670 | break; |
| 671 | dump_frame(fd, frame); |
| 672 | frame = frame->f_back; |
| 673 | depth++; |
| 674 | } |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 677 | /* Dump the traceback of a Python thread into fd. Use write() to write the |
| 678 | traceback and retry if write() is interrupted by a signal (failed with |
| 679 | EINTR), but don't call the Python signal handler. |
| 680 | |
| 681 | The caller is responsible to call PyErr_CheckSignals() to call Python signal |
| 682 | handlers if signals were received. */ |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 683 | void |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 684 | _Py_DumpTraceback(int fd, PyThreadState *tstate) |
| 685 | { |
Victor Stinner | fcb88c4 | 2011-04-01 15:34:01 +0200 | [diff] [blame] | 686 | dump_traceback(fd, tstate, 1); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if |
| 690 | is_current is true, "Thread 0xHHHH:\n" otherwise. |
| 691 | |
| 692 | This function is signal safe. */ |
| 693 | |
| 694 | static void |
| 695 | write_thread_id(int fd, PyThreadState *tstate, int is_current) |
| 696 | { |
| 697 | if (is_current) |
| 698 | PUTS(fd, "Current thread 0x"); |
| 699 | else |
| 700 | PUTS(fd, "Thread 0x"); |
Victor Stinner | b86f08f | 2014-10-03 14:18:09 +0200 | [diff] [blame] | 701 | dump_hexadecimal(fd, (unsigned long)tstate->thread_id, sizeof(unsigned long)*2); |
Guido van Rossum | 7be5d7d | 2013-10-20 18:21:02 -0700 | [diff] [blame] | 702 | PUTS(fd, " (most recent call first):\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 705 | /* Dump the traceback of all Python threads into fd. Use write() to write the |
| 706 | traceback and retry if write() is interrupted by a signal (failed with |
| 707 | EINTR), but don't call the Python signal handler. |
| 708 | |
| 709 | The caller is responsible to call PyErr_CheckSignals() to call Python signal |
| 710 | handlers if signals were received. */ |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 711 | const char* |
| 712 | _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp, |
| 713 | PyThreadState *current_thread) |
| 714 | { |
| 715 | PyThreadState *tstate; |
| 716 | unsigned int nthreads; |
| 717 | |
| 718 | /* Get the current interpreter from the current thread */ |
| 719 | tstate = PyInterpreterState_ThreadHead(interp); |
| 720 | if (tstate == NULL) |
| 721 | return "unable to get the thread head state"; |
| 722 | |
| 723 | /* Dump the traceback of each thread */ |
| 724 | tstate = PyInterpreterState_ThreadHead(interp); |
| 725 | nthreads = 0; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 726 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 727 | do |
| 728 | { |
| 729 | if (nthreads != 0) |
Victor Stinner | 97f86b8 | 2015-04-01 18:38:01 +0200 | [diff] [blame] | 730 | PUTS(fd, "\n"); |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 731 | if (nthreads >= MAX_NTHREADS) { |
| 732 | PUTS(fd, "...\n"); |
| 733 | break; |
| 734 | } |
| 735 | write_thread_id(fd, tstate, tstate == current_thread); |
| 736 | dump_traceback(fd, tstate, 0); |
| 737 | tstate = PyThreadState_Next(tstate); |
| 738 | nthreads++; |
| 739 | } while (tstate != NULL); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 740 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 024e37a | 2011-03-31 01:31:06 +0200 | [diff] [blame] | 741 | |
| 742 | return NULL; |
| 743 | } |
| 744 | |