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