blob: 1f611ba1d5152c6fd753280148b9eb28c196db38 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Traceback implementation */
3
Guido van Rossum65bf9f21997-04-29 18:33:38 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00007#include "frameobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00008#include "structmember.h"
Guido van Rossum7169dbb1992-02-26 15:17:59 +00009#include "osdefs.h"
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +000010#ifdef HAVE_FCNTL_H
11#include <fcntl.h>
12#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
Nicholas Bastina7604bf2004-03-21 18:37:23 +000014#define OFF(x) offsetof(PyTracebackObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000015
Victor Stinner024e37a2011-03-31 01:31:06 +020016#define PUTS(fd, str) write(fd, str, strlen(str))
Victor Stinner54f939b2012-07-30 13:08:58 +020017#define MAX_STRING_LENGTH 500
Victor Stinner024e37a2011-03-31 01:31:06 +020018#define MAX_FRAME_DEPTH 100
19#define MAX_NTHREADS 100
20
Victor Stinnerfe7c5b52011-04-05 01:48:03 +020021/* Function from Parser/tokenizer.c */
22extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +000023
Collin Winter3eed7652007-08-14 17:53:54 +000024static PyObject *
25tb_dir(PyTracebackObject *self)
26{
27 return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
28 "tb_lasti", "tb_lineno");
29}
30
31static PyMethodDef tb_methods[] = {
32 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
33 {NULL, NULL, 0, NULL},
34};
35
Neal Norwitz8dfc4a92007-08-11 06:39:53 +000036static PyMemberDef tb_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 {"tb_next", T_OBJECT, OFF(tb_next), READONLY},
38 {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
39 {"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
40 {"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
41 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000042};
43
Guido van Rossum3f5da241990-12-20 15:06:42 +000044static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000045tb_dealloc(PyTracebackObject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 PyObject_GC_UnTrack(tb);
48 Py_TRASHCAN_SAFE_BEGIN(tb)
49 Py_XDECREF(tb->tb_next);
50 Py_XDECREF(tb->tb_frame);
51 PyObject_GC_Del(tb);
52 Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000053}
54
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000055static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +000056tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 Py_VISIT(tb->tb_next);
59 Py_VISIT(tb->tb_frame);
60 return 0;
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000061}
62
63static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000064tb_clear(PyTracebackObject *tb)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 Py_CLEAR(tb->tb_next);
67 Py_CLEAR(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000068}
69
Tim Petersd7c36522001-10-22 19:34:09 +000070PyTypeObject PyTraceBack_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 PyVarObject_HEAD_INIT(&PyType_Type, 0)
72 "traceback",
73 sizeof(PyTracebackObject),
74 0,
75 (destructor)tb_dealloc, /*tp_dealloc*/
76 0, /*tp_print*/
77 0, /*tp_getattr*/
78 0, /*tp_setattr*/
79 0, /*tp_reserved*/
80 0, /*tp_repr*/
81 0, /*tp_as_number*/
82 0, /*tp_as_sequence*/
83 0, /*tp_as_mapping*/
84 0, /* tp_hash */
85 0, /* tp_call */
86 0, /* tp_str */
87 PyObject_GenericGetAttr, /* tp_getattro */
88 0, /* tp_setattro */
89 0, /* tp_as_buffer */
90 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
91 0, /* tp_doc */
92 (traverseproc)tb_traverse, /* tp_traverse */
93 (inquiry)tb_clear, /* tp_clear */
94 0, /* tp_richcompare */
95 0, /* tp_weaklistoffset */
96 0, /* tp_iter */
97 0, /* tp_iternext */
98 tb_methods, /* tp_methods */
99 tb_memberlist, /* tp_members */
100 0, /* tp_getset */
101 0, /* tp_base */
102 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103};
104
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000105static PyTracebackObject *
106newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 PyTracebackObject *tb;
109 if ((next != NULL && !PyTraceBack_Check(next)) ||
110 frame == NULL || !PyFrame_Check(frame)) {
111 PyErr_BadInternalCall();
112 return NULL;
113 }
114 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
115 if (tb != NULL) {
116 Py_XINCREF(next);
117 tb->tb_next = next;
118 Py_XINCREF(frame);
119 tb->tb_frame = frame;
120 tb->tb_lasti = frame->f_lasti;
121 tb->tb_lineno = PyFrame_GetLineNumber(frame);
122 PyObject_GC_Track(tb);
123 }
124 return tb;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125}
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000128PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyThreadState *tstate = PyThreadState_GET();
131 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
132 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
133 if (tb == NULL)
134 return -1;
135 tstate->curexc_traceback = (PyObject *)tb;
136 Py_XDECREF(oldtb);
137 return 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138}
139
Victor Stinner0fe25a42010-06-17 23:08:50 +0000140static PyObject *
141_Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000142{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000143 Py_ssize_t i;
144 PyObject *binary;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 PyObject *v;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000146 Py_ssize_t npath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 size_t taillen;
148 PyObject *syspath;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000149 PyObject *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 const char* tail;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000151 PyObject *filebytes;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000152 const char* filepath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 Py_ssize_t len;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000154 PyObject* result;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200155 _Py_IDENTIFIER(open);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000156
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000157 filebytes = PyUnicode_EncodeFSDefault(filename);
158 if (filebytes == NULL) {
Victor Stinner0fe25a42010-06-17 23:08:50 +0000159 PyErr_Clear();
160 return NULL;
161 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000162 filepath = PyBytes_AS_STRING(filebytes);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 /* Search tail of filename in sys.path before giving up */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000165 tail = strrchr(filepath, SEP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 if (tail == NULL)
Victor Stinner0fe25a42010-06-17 23:08:50 +0000167 tail = filepath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 else
169 tail++;
170 taillen = strlen(tail);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 syspath = PySys_GetObject("path");
173 if (syspath == NULL || !PyList_Check(syspath))
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000174 goto error;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000175 npath = PyList_Size(syspath);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 for (i = 0; i < npath; i++) {
178 v = PyList_GetItem(syspath, i);
179 if (v == NULL) {
180 PyErr_Clear();
181 break;
182 }
183 if (!PyUnicode_Check(v))
184 continue;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000185 path = PyUnicode_EncodeFSDefault(v);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000186 if (path == NULL) {
187 PyErr_Clear();
188 continue;
189 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000190 len = PyBytes_GET_SIZE(path);
191 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) {
192 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 continue; /* Too long */
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000194 }
195 strcpy(namebuf, PyBytes_AS_STRING(path));
196 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (strlen(namebuf) != len)
198 continue; /* v contains '\0' */
199 if (len > 0 && namebuf[len-1] != SEP)
200 namebuf[len++] = SEP;
201 strcpy(namebuf+len, tail);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000202
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200203 binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000204 if (binary != NULL) {
205 result = binary;
206 goto finally;
207 }
Victor Stinner0fe25a42010-06-17 23:08:50 +0000208 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000210 goto error;
211
212error:
213 result = NULL;
214finally:
215 Py_DECREF(filebytes);
216 return result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000217}
218
Christian Heimes33fe8092008-04-13 13:53:33 +0000219int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000220_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 int err = 0;
223 int fd;
224 int i;
225 char *found_encoding;
226 char *encoding;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000227 PyObject *io;
228 PyObject *binary;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 PyObject *fob = NULL;
230 PyObject *lineobj = NULL;
Antoine Pitroub86680e2010-10-14 21:15:17 +0000231 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 char buf[MAXPATHLEN+1];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200233 int kind;
234 void *data;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200235 _Py_IDENTIFIER(close);
236 _Py_IDENTIFIER(open);
237 _Py_IDENTIFIER(TextIOWrapper);
Christian Heimes679db4a2008-01-18 09:56:22 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 /* open the file */
240 if (filename == NULL)
241 return 0;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000242
243 io = PyImport_ImportModuleNoBlock("io");
244 if (io == NULL)
245 return -1;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200246 binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb");
Victor Stinner0fe25a42010-06-17 23:08:50 +0000247
248 if (binary == NULL) {
249 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
250 if (binary == NULL) {
251 Py_DECREF(io);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 return 0;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* use the right encoding to decode the file as unicode */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000257 fd = PyObject_AsFileDescriptor(binary);
Christian Heimes8c077bc2013-07-21 01:53:10 +0200258 if (fd < 0) {
259 Py_DECREF(io);
260 Py_DECREF(binary);
261 return NULL;
262 }
Victor Stinnerfe7c5b52011-04-05 01:48:03 +0200263 found_encoding = PyTokenizer_FindEncodingFilename(fd, filename);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000264 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Christian Heimes1f347292013-07-21 02:12:35 +0200265 /* Reset position */
266 if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
267 Py_DECREF(io);
268 Py_DECREF(binary);
269 PyMem_FREE(found_encoding);
270 return PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, filename);
271 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200272 fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000273 Py_DECREF(io);
274 Py_DECREF(binary);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyMem_FREE(found_encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (fob == NULL) {
278 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 return 0;
280 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 /* get the line number lineno */
283 for (i = 0; i < lineno; i++) {
284 Py_XDECREF(lineobj);
285 lineobj = PyFile_GetLine(fob, -1);
286 if (!lineobj) {
287 err = -1;
288 break;
289 }
290 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200291 res = _PyObject_CallMethodId(fob, &PyId_close, "");
Antoine Pitroub86680e2010-10-14 21:15:17 +0000292 if (res)
293 Py_DECREF(res);
294 else
295 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 Py_DECREF(fob);
297 if (!lineobj || !PyUnicode_Check(lineobj)) {
298 Py_XDECREF(lineobj);
299 return err;
300 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* remove the indentation of the line */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200303 kind = PyUnicode_KIND(lineobj);
304 data = PyUnicode_DATA(lineobj);
305 for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) {
306 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
307 if (ch != ' ' && ch != '\t' && ch != '\014')
308 break;
309 }
310 if (i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 PyObject *truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200312 truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (truncated) {
314 Py_DECREF(lineobj);
315 lineobj = truncated;
316 } else {
317 PyErr_Clear();
318 }
319 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 /* Write some spaces before the line */
322 strcpy(buf, " ");
323 assert (strlen(buf) == 10);
324 while (indent > 0) {
325 if(indent < 10)
326 buf[indent] = '\0';
327 err = PyFile_WriteString(buf, f);
328 if (err != 0)
329 break;
330 indent -= 10;
331 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* finally display the line */
334 if (err == 0)
335 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
336 Py_DECREF(lineobj);
337 if (err == 0)
338 err = PyFile_WriteString("\n", f);
339 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000340}
341
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000342static int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000343tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000344{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000345 int err;
346 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (filename == NULL || name == NULL)
349 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000350 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
351 filename, lineno, name);
352 if (line == NULL)
353 return -1;
354 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
355 Py_DECREF(line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (err != 0)
357 return err;
Kristján Valur Jónssonc5963d32012-07-19 21:02:03 +0000358 /* ignore errors since we can't report them, can we? */
359 if (_Py_DisplaySourceLine(f, filename, lineno, 4))
360 PyErr_Clear();
361 return err;
Christian Heimes33fe8092008-04-13 13:53:33 +0000362}
363
364static int
365tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 int err = 0;
368 long depth = 0;
369 PyTracebackObject *tb1 = tb;
370 while (tb1 != NULL) {
371 depth++;
372 tb1 = tb1->tb_next;
373 }
374 while (tb != NULL && err == 0) {
375 if (depth <= limit) {
376 err = tb_displayline(f,
Victor Stinner0fe25a42010-06-17 23:08:50 +0000377 tb->tb_frame->f_code->co_filename,
378 tb->tb_lineno,
379 tb->tb_frame->f_code->co_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 }
381 depth--;
382 tb = tb->tb_next;
383 if (err == 0)
384 err = PyErr_CheckSignals();
385 }
386 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000387}
388
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000389#define PyTraceBack_LIMIT 1000
390
Guido van Rossum3f5da241990-12-20 15:06:42 +0000391int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000392PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 int err;
395 PyObject *limitv;
396 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (v == NULL)
399 return 0;
400 if (!PyTraceBack_Check(v)) {
401 PyErr_BadInternalCall();
402 return -1;
403 }
404 limitv = PySys_GetObject("tracebacklimit");
405 if (limitv) {
406 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
409 limit = PyLong_AsLong(limitv);
410 if (limit == -1 && PyErr_Occurred()) {
411 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
412 limit = PyTraceBack_LIMIT;
413 }
414 else {
415 Py_XDECREF(exc_type);
416 Py_XDECREF(exc_value);
417 Py_XDECREF(exc_tb);
418 return 0;
419 }
420 }
421 else if (limit <= 0) {
422 limit = PyTraceBack_LIMIT;
423 }
424 PyErr_Restore(exc_type, exc_value, exc_tb);
425 }
426 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
427 if (!err)
428 err = tb_printinternal((PyTracebackObject *)v, f, limit);
429 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000430}
Victor Stinner024e37a2011-03-31 01:31:06 +0200431
432/* Reverse a string. For example, "abcd" becomes "dcba".
433
434 This function is signal safe. */
435
436static void
437reverse_string(char *text, const size_t len)
438{
439 char tmp;
440 size_t i, j;
441 if (len == 0)
442 return;
443 for (i=0, j=len-1; i < j; i++, j--) {
444 tmp = text[i];
445 text[i] = text[j];
446 text[j] = tmp;
447 }
448}
449
450/* Format an integer in range [0; 999999] to decimal,
451 and write it into the file fd.
452
453 This function is signal safe. */
454
455static void
456dump_decimal(int fd, int value)
457{
458 char buffer[7];
459 int len;
460 if (value < 0 || 999999 < value)
461 return;
462 len = 0;
463 do {
464 buffer[len] = '0' + (value % 10);
465 value /= 10;
466 len++;
467 } while (value);
468 reverse_string(buffer, len);
469 write(fd, buffer, len);
470}
471
472/* Format an integer in range [0; 0xffffffff] to hexdecimal of 'width' digits,
473 and write it into the file fd.
474
475 This function is signal safe. */
476
477static void
478dump_hexadecimal(int width, unsigned long value, int fd)
479{
Victor Stinner024e37a2011-03-31 01:31:06 +0200480 int len;
481 char buffer[sizeof(unsigned long) * 2 + 1];
482 len = 0;
483 do {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200484 buffer[len] = Py_hexdigits[value & 15];
Victor Stinner024e37a2011-03-31 01:31:06 +0200485 value >>= 4;
486 len++;
487 } while (len < width || value);
488 reverse_string(buffer, len);
489 write(fd, buffer, len);
490}
491
492/* Write an unicode object into the file fd using ascii+backslashreplace.
493
494 This function is signal safe. */
495
496static void
497dump_ascii(int fd, PyObject *text)
498{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200499 PyASCIIObject *ascii = (PyASCIIObject *)text;
Victor Stinner024e37a2011-03-31 01:31:06 +0200500 Py_ssize_t i, size;
501 int truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200502 int kind;
Victor Stinnera336de72011-10-05 22:44:12 +0200503 void *data = NULL;
504 wchar_t *wstr = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200505 Py_UCS4 ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200506
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200507 size = ascii->length;
508 kind = ascii->state.kind;
509 if (ascii->state.compact) {
510 if (ascii->state.ascii)
511 data = ((PyASCIIObject*)text) + 1;
512 else
513 data = ((PyCompactUnicodeObject*)text) + 1;
514 }
Victor Stinnera336de72011-10-05 22:44:12 +0200515 else if (kind != PyUnicode_WCHAR_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200516 data = ((PyUnicodeObject *)text)->data.any;
517 if (data == NULL)
518 return;
519 }
Victor Stinnera336de72011-10-05 22:44:12 +0200520 else {
521 wstr = ((PyASCIIObject *)text)->wstr;
522 if (wstr == NULL)
523 return;
524 size = ((PyCompactUnicodeObject *)text)->wstr_length;
525 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200526
527 if (MAX_STRING_LENGTH < size) {
528 size = MAX_STRING_LENGTH;
529 truncated = 1;
530 }
531 else
532 truncated = 0;
533
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200534 for (i=0; i < size; i++) {
Victor Stinnera336de72011-10-05 22:44:12 +0200535 if (kind != PyUnicode_WCHAR_KIND)
536 ch = PyUnicode_READ(kind, data, i);
537 else
538 ch = wstr[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200539 if (ch < 128) {
540 char c = (char)ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200541 write(fd, &c, 1);
542 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100543 else if (ch < 0xff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200544 PUTS(fd, "\\x");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200545 dump_hexadecimal(2, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200546 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100547 else if (ch < 0xffff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200548 PUTS(fd, "\\u");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200549 dump_hexadecimal(4, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200550 }
551 else {
552 PUTS(fd, "\\U");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200553 dump_hexadecimal(8, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200554 }
555 }
556 if (truncated)
557 PUTS(fd, "...");
558}
559
560/* Write a frame into the file fd: "File "xxx", line xxx in xxx".
561
562 This function is signal safe. */
563
564static void
565dump_frame(int fd, PyFrameObject *frame)
566{
567 PyCodeObject *code;
568 int lineno;
569
570 code = frame->f_code;
571 PUTS(fd, " File ");
572 if (code != NULL && code->co_filename != NULL
573 && PyUnicode_Check(code->co_filename))
574 {
575 write(fd, "\"", 1);
576 dump_ascii(fd, code->co_filename);
577 write(fd, "\"", 1);
578 } else {
579 PUTS(fd, "???");
580 }
581
582 /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200583 lineno = PyCode_Addr2Line(code, frame->f_lasti);
Victor Stinner024e37a2011-03-31 01:31:06 +0200584 PUTS(fd, ", line ");
585 dump_decimal(fd, lineno);
586 PUTS(fd, " in ");
587
588 if (code != NULL && code->co_name != NULL
589 && PyUnicode_Check(code->co_name))
590 dump_ascii(fd, code->co_name);
591 else
592 PUTS(fd, "???");
593
594 write(fd, "\n", 1);
595}
596
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200597static void
Victor Stinner024e37a2011-03-31 01:31:06 +0200598dump_traceback(int fd, PyThreadState *tstate, int write_header)
599{
600 PyFrameObject *frame;
601 unsigned int depth;
602
Victor Stinner024e37a2011-03-31 01:31:06 +0200603 if (write_header)
604 PUTS(fd, "Traceback (most recent call first):\n");
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200605
606 frame = _PyThreadState_GetFrame(tstate);
607 if (frame == NULL)
608 return;
609
Victor Stinner024e37a2011-03-31 01:31:06 +0200610 depth = 0;
611 while (frame != NULL) {
612 if (MAX_FRAME_DEPTH <= depth) {
613 PUTS(fd, " ...\n");
614 break;
615 }
616 if (!PyFrame_Check(frame))
617 break;
618 dump_frame(fd, frame);
619 frame = frame->f_back;
620 depth++;
621 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200622}
623
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200624void
Victor Stinner024e37a2011-03-31 01:31:06 +0200625_Py_DumpTraceback(int fd, PyThreadState *tstate)
626{
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200627 dump_traceback(fd, tstate, 1);
Victor Stinner024e37a2011-03-31 01:31:06 +0200628}
629
630/* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if
631 is_current is true, "Thread 0xHHHH:\n" otherwise.
632
633 This function is signal safe. */
634
635static void
636write_thread_id(int fd, PyThreadState *tstate, int is_current)
637{
638 if (is_current)
639 PUTS(fd, "Current thread 0x");
640 else
641 PUTS(fd, "Thread 0x");
642 dump_hexadecimal(sizeof(long)*2, (unsigned long)tstate->thread_id, fd);
643 PUTS(fd, ":\n");
644}
645
646const char*
647_Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
648 PyThreadState *current_thread)
649{
650 PyThreadState *tstate;
651 unsigned int nthreads;
652
653 /* Get the current interpreter from the current thread */
654 tstate = PyInterpreterState_ThreadHead(interp);
655 if (tstate == NULL)
656 return "unable to get the thread head state";
657
658 /* Dump the traceback of each thread */
659 tstate = PyInterpreterState_ThreadHead(interp);
660 nthreads = 0;
661 do
662 {
663 if (nthreads != 0)
664 write(fd, "\n", 1);
665 if (nthreads >= MAX_NTHREADS) {
666 PUTS(fd, "...\n");
667 break;
668 }
669 write_thread_id(fd, tstate, tstate == current_thread);
670 dump_traceback(fd, tstate, 0);
671 tstate = PyThreadState_Next(tstate);
672 nthreads++;
673 } while (tstate != NULL);
674
675 return NULL;
676}
677