blob: 8130d4bcec5d85bd8adb0f4b6fd77dc21abcfa7e [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 Stinner84bb1cf2013-05-17 00:12:04 +020016#define PUTS(fd, str) write(fd, str, (int)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) {
Victor Stinnerceceaa02013-07-16 00:32:14 +0200249 PyErr_Clear();
250
Victor Stinner0fe25a42010-06-17 23:08:50 +0000251 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
252 if (binary == NULL) {
253 Py_DECREF(io);
Victor Stinnerceceaa02013-07-16 00:32:14 +0200254 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000255 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* use the right encoding to decode the file as unicode */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000259 fd = PyObject_AsFileDescriptor(binary);
Victor Stinnerfe7c5b52011-04-05 01:48:03 +0200260 found_encoding = PyTokenizer_FindEncodingFilename(fd, filename);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000261 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 lseek(fd, 0, 0); /* Reset position */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200263 fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000264 Py_DECREF(io);
265 Py_DECREF(binary);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 PyMem_FREE(found_encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (fob == NULL) {
269 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 return 0;
271 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 /* get the line number lineno */
274 for (i = 0; i < lineno; i++) {
275 Py_XDECREF(lineobj);
276 lineobj = PyFile_GetLine(fob, -1);
277 if (!lineobj) {
278 err = -1;
279 break;
280 }
281 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200282 res = _PyObject_CallMethodId(fob, &PyId_close, "");
Antoine Pitroub86680e2010-10-14 21:15:17 +0000283 if (res)
284 Py_DECREF(res);
285 else
286 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_DECREF(fob);
288 if (!lineobj || !PyUnicode_Check(lineobj)) {
289 Py_XDECREF(lineobj);
290 return err;
291 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 /* remove the indentation of the line */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200294 kind = PyUnicode_KIND(lineobj);
295 data = PyUnicode_DATA(lineobj);
296 for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) {
297 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
298 if (ch != ' ' && ch != '\t' && ch != '\014')
299 break;
300 }
301 if (i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 PyObject *truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200303 truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (truncated) {
305 Py_DECREF(lineobj);
306 lineobj = truncated;
307 } else {
308 PyErr_Clear();
309 }
310 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 /* Write some spaces before the line */
313 strcpy(buf, " ");
314 assert (strlen(buf) == 10);
315 while (indent > 0) {
316 if(indent < 10)
317 buf[indent] = '\0';
318 err = PyFile_WriteString(buf, f);
319 if (err != 0)
320 break;
321 indent -= 10;
322 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 /* finally display the line */
325 if (err == 0)
326 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
327 Py_DECREF(lineobj);
328 if (err == 0)
329 err = PyFile_WriteString("\n", f);
330 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331}
332
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000333static int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000334tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000336 int err;
337 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (filename == NULL || name == NULL)
340 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000341 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
342 filename, lineno, name);
343 if (line == NULL)
344 return -1;
345 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
346 Py_DECREF(line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (err != 0)
348 return err;
Kristján Valur Jónssonc5963d32012-07-19 21:02:03 +0000349 /* ignore errors since we can't report them, can we? */
350 if (_Py_DisplaySourceLine(f, filename, lineno, 4))
351 PyErr_Clear();
352 return err;
Christian Heimes33fe8092008-04-13 13:53:33 +0000353}
354
355static int
356tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 int err = 0;
359 long depth = 0;
360 PyTracebackObject *tb1 = tb;
361 while (tb1 != NULL) {
362 depth++;
363 tb1 = tb1->tb_next;
364 }
365 while (tb != NULL && err == 0) {
366 if (depth <= limit) {
367 err = tb_displayline(f,
Victor Stinner0fe25a42010-06-17 23:08:50 +0000368 tb->tb_frame->f_code->co_filename,
369 tb->tb_lineno,
370 tb->tb_frame->f_code->co_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
372 depth--;
373 tb = tb->tb_next;
374 if (err == 0)
375 err = PyErr_CheckSignals();
376 }
377 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000378}
379
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000380#define PyTraceBack_LIMIT 1000
381
Guido van Rossum3f5da241990-12-20 15:06:42 +0000382int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000383PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 int err;
386 PyObject *limitv;
387 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (v == NULL)
390 return 0;
391 if (!PyTraceBack_Check(v)) {
392 PyErr_BadInternalCall();
393 return -1;
394 }
395 limitv = PySys_GetObject("tracebacklimit");
396 if (limitv) {
397 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
400 limit = PyLong_AsLong(limitv);
401 if (limit == -1 && PyErr_Occurred()) {
402 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
403 limit = PyTraceBack_LIMIT;
404 }
405 else {
406 Py_XDECREF(exc_type);
407 Py_XDECREF(exc_value);
408 Py_XDECREF(exc_tb);
409 return 0;
410 }
411 }
412 else if (limit <= 0) {
413 limit = PyTraceBack_LIMIT;
414 }
415 PyErr_Restore(exc_type, exc_value, exc_tb);
416 }
417 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
418 if (!err)
419 err = tb_printinternal((PyTracebackObject *)v, f, limit);
420 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000421}
Victor Stinner024e37a2011-03-31 01:31:06 +0200422
423/* Reverse a string. For example, "abcd" becomes "dcba".
424
425 This function is signal safe. */
426
427static void
428reverse_string(char *text, const size_t len)
429{
430 char tmp;
431 size_t i, j;
432 if (len == 0)
433 return;
434 for (i=0, j=len-1; i < j; i++, j--) {
435 tmp = text[i];
436 text[i] = text[j];
437 text[j] = tmp;
438 }
439}
440
441/* Format an integer in range [0; 999999] to decimal,
442 and write it into the file fd.
443
444 This function is signal safe. */
445
446static void
447dump_decimal(int fd, int value)
448{
449 char buffer[7];
450 int len;
451 if (value < 0 || 999999 < value)
452 return;
453 len = 0;
454 do {
455 buffer[len] = '0' + (value % 10);
456 value /= 10;
457 len++;
458 } while (value);
459 reverse_string(buffer, len);
460 write(fd, buffer, len);
461}
462
463/* Format an integer in range [0; 0xffffffff] to hexdecimal of 'width' digits,
464 and write it into the file fd.
465
466 This function is signal safe. */
467
468static void
469dump_hexadecimal(int width, unsigned long value, int fd)
470{
Victor Stinner024e37a2011-03-31 01:31:06 +0200471 int len;
472 char buffer[sizeof(unsigned long) * 2 + 1];
473 len = 0;
474 do {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200475 buffer[len] = Py_hexdigits[value & 15];
Victor Stinner024e37a2011-03-31 01:31:06 +0200476 value >>= 4;
477 len++;
478 } while (len < width || value);
479 reverse_string(buffer, len);
480 write(fd, buffer, len);
481}
482
483/* Write an unicode object into the file fd using ascii+backslashreplace.
484
485 This function is signal safe. */
486
487static void
488dump_ascii(int fd, PyObject *text)
489{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200490 PyASCIIObject *ascii = (PyASCIIObject *)text;
Victor Stinner024e37a2011-03-31 01:31:06 +0200491 Py_ssize_t i, size;
492 int truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200493 int kind;
Victor Stinnera336de72011-10-05 22:44:12 +0200494 void *data = NULL;
495 wchar_t *wstr = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200496 Py_UCS4 ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200497
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200498 size = ascii->length;
499 kind = ascii->state.kind;
500 if (ascii->state.compact) {
501 if (ascii->state.ascii)
502 data = ((PyASCIIObject*)text) + 1;
503 else
504 data = ((PyCompactUnicodeObject*)text) + 1;
505 }
Victor Stinnera336de72011-10-05 22:44:12 +0200506 else if (kind != PyUnicode_WCHAR_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200507 data = ((PyUnicodeObject *)text)->data.any;
508 if (data == NULL)
509 return;
510 }
Victor Stinnera336de72011-10-05 22:44:12 +0200511 else {
512 wstr = ((PyASCIIObject *)text)->wstr;
513 if (wstr == NULL)
514 return;
515 size = ((PyCompactUnicodeObject *)text)->wstr_length;
516 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200517
518 if (MAX_STRING_LENGTH < size) {
519 size = MAX_STRING_LENGTH;
520 truncated = 1;
521 }
522 else
523 truncated = 0;
524
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200525 for (i=0; i < size; i++) {
Victor Stinnera336de72011-10-05 22:44:12 +0200526 if (kind != PyUnicode_WCHAR_KIND)
527 ch = PyUnicode_READ(kind, data, i);
528 else
529 ch = wstr[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200530 if (ch < 128) {
531 char c = (char)ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200532 write(fd, &c, 1);
533 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100534 else if (ch < 0xff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200535 PUTS(fd, "\\x");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200536 dump_hexadecimal(2, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200537 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100538 else if (ch < 0xffff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200539 PUTS(fd, "\\u");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200540 dump_hexadecimal(4, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200541 }
542 else {
543 PUTS(fd, "\\U");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200544 dump_hexadecimal(8, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200545 }
546 }
547 if (truncated)
548 PUTS(fd, "...");
549}
550
551/* Write a frame into the file fd: "File "xxx", line xxx in xxx".
552
553 This function is signal safe. */
554
555static void
556dump_frame(int fd, PyFrameObject *frame)
557{
558 PyCodeObject *code;
559 int lineno;
560
561 code = frame->f_code;
562 PUTS(fd, " File ");
563 if (code != NULL && code->co_filename != NULL
564 && PyUnicode_Check(code->co_filename))
565 {
566 write(fd, "\"", 1);
567 dump_ascii(fd, code->co_filename);
568 write(fd, "\"", 1);
569 } else {
570 PUTS(fd, "???");
571 }
572
573 /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200574 lineno = PyCode_Addr2Line(code, frame->f_lasti);
Victor Stinner024e37a2011-03-31 01:31:06 +0200575 PUTS(fd, ", line ");
576 dump_decimal(fd, lineno);
577 PUTS(fd, " in ");
578
579 if (code != NULL && code->co_name != NULL
580 && PyUnicode_Check(code->co_name))
581 dump_ascii(fd, code->co_name);
582 else
583 PUTS(fd, "???");
584
585 write(fd, "\n", 1);
586}
587
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200588static void
Victor Stinner024e37a2011-03-31 01:31:06 +0200589dump_traceback(int fd, PyThreadState *tstate, int write_header)
590{
591 PyFrameObject *frame;
592 unsigned int depth;
593
Victor Stinner024e37a2011-03-31 01:31:06 +0200594 if (write_header)
595 PUTS(fd, "Traceback (most recent call first):\n");
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200596
597 frame = _PyThreadState_GetFrame(tstate);
598 if (frame == NULL)
599 return;
600
Victor Stinner024e37a2011-03-31 01:31:06 +0200601 depth = 0;
602 while (frame != NULL) {
603 if (MAX_FRAME_DEPTH <= depth) {
604 PUTS(fd, " ...\n");
605 break;
606 }
607 if (!PyFrame_Check(frame))
608 break;
609 dump_frame(fd, frame);
610 frame = frame->f_back;
611 depth++;
612 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200613}
614
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200615void
Victor Stinner024e37a2011-03-31 01:31:06 +0200616_Py_DumpTraceback(int fd, PyThreadState *tstate)
617{
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200618 dump_traceback(fd, tstate, 1);
Victor Stinner024e37a2011-03-31 01:31:06 +0200619}
620
621/* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if
622 is_current is true, "Thread 0xHHHH:\n" otherwise.
623
624 This function is signal safe. */
625
626static void
627write_thread_id(int fd, PyThreadState *tstate, int is_current)
628{
629 if (is_current)
630 PUTS(fd, "Current thread 0x");
631 else
632 PUTS(fd, "Thread 0x");
633 dump_hexadecimal(sizeof(long)*2, (unsigned long)tstate->thread_id, fd);
634 PUTS(fd, ":\n");
635}
636
637const char*
638_Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
639 PyThreadState *current_thread)
640{
641 PyThreadState *tstate;
642 unsigned int nthreads;
643
644 /* Get the current interpreter from the current thread */
645 tstate = PyInterpreterState_ThreadHead(interp);
646 if (tstate == NULL)
647 return "unable to get the thread head state";
648
649 /* Dump the traceback of each thread */
650 tstate = PyInterpreterState_ThreadHead(interp);
651 nthreads = 0;
652 do
653 {
654 if (nthreads != 0)
655 write(fd, "\n", 1);
656 if (nthreads >= MAX_NTHREADS) {
657 PUTS(fd, "...\n");
658 break;
659 }
660 write_thread_id(fd, tstate, tstate == current_thread);
661 dump_traceback(fd, tstate, 0);
662 tstate = PyThreadState_Next(tstate);
663 nthreads++;
664 } while (tstate != NULL);
665
666 return NULL;
667}
668