blob: e9169ce5e060db79afa7aecee9bdeefbf5181bfc [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
Victor Stinnerbd303c12013-11-07 23:07:29 +010024_Py_IDENTIFIER(TextIOWrapper);
25_Py_IDENTIFIER(close);
26_Py_IDENTIFIER(open);
27_Py_IDENTIFIER(path);
28
Collin Winter3eed7652007-08-14 17:53:54 +000029static PyObject *
30tb_dir(PyTracebackObject *self)
31{
32 return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
33 "tb_lasti", "tb_lineno");
34}
35
36static PyMethodDef tb_methods[] = {
37 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
38 {NULL, NULL, 0, NULL},
39};
40
Neal Norwitz8dfc4a92007-08-11 06:39:53 +000041static PyMemberDef tb_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 {"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 Rossum3f5da241990-12-20 15:06:42 +000047};
48
Guido van Rossum3f5da241990-12-20 15:06:42 +000049static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000050tb_dealloc(PyTracebackObject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 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 Rossum3f5da241990-12-20 15:06:42 +000058}
59
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000060static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +000061tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 Py_VISIT(tb->tb_next);
64 Py_VISIT(tb->tb_frame);
65 return 0;
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000066}
67
68static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000069tb_clear(PyTracebackObject *tb)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 Py_CLEAR(tb->tb_next);
72 Py_CLEAR(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000073}
74
Tim Petersd7c36522001-10-22 19:34:09 +000075PyTypeObject PyTraceBack_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 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 Rossum3f5da241990-12-20 15:06:42 +0000108};
109
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000110static PyTracebackObject *
111newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 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 Rossum3f5da241990-12-20 15:06:42 +0000130}
131
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000133PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 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 Rossum3f5da241990-12-20 15:06:42 +0000143}
144
Victor Stinner0fe25a42010-06-17 23:08:50 +0000145static PyObject *
146_Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000147{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000148 Py_ssize_t i;
149 PyObject *binary;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 PyObject *v;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000151 Py_ssize_t npath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 size_t taillen;
153 PyObject *syspath;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000154 PyObject *path;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 const char* tail;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000156 PyObject *filebytes;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000157 const char* filepath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 Py_ssize_t len;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000159 PyObject* result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000160
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000161 filebytes = PyUnicode_EncodeFSDefault(filename);
162 if (filebytes == NULL) {
Victor Stinner0fe25a42010-06-17 23:08:50 +0000163 PyErr_Clear();
164 return NULL;
165 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000166 filepath = PyBytes_AS_STRING(filebytes);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 /* Search tail of filename in sys.path before giving up */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000169 tail = strrchr(filepath, SEP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 if (tail == NULL)
Victor Stinner0fe25a42010-06-17 23:08:50 +0000171 tail = filepath;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 else
173 tail++;
174 taillen = strlen(tail);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000175
Victor Stinnerbd303c12013-11-07 23:07:29 +0100176 syspath = _PySys_GetObjectId(&PyId_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (syspath == NULL || !PyList_Check(syspath))
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000178 goto error;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000179 npath = PyList_Size(syspath);
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 for (i = 0; i < npath; i++) {
182 v = PyList_GetItem(syspath, i);
183 if (v == NULL) {
184 PyErr_Clear();
185 break;
186 }
187 if (!PyUnicode_Check(v))
188 continue;
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000189 path = PyUnicode_EncodeFSDefault(v);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000190 if (path == NULL) {
191 PyErr_Clear();
192 continue;
193 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000194 len = PyBytes_GET_SIZE(path);
195 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) {
196 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 continue; /* Too long */
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000198 }
199 strcpy(namebuf, PyBytes_AS_STRING(path));
200 Py_DECREF(path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (strlen(namebuf) != len)
202 continue; /* v contains '\0' */
203 if (len > 0 && namebuf[len-1] != SEP)
204 namebuf[len++] = SEP;
205 strcpy(namebuf+len, tail);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000206
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200207 binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb");
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000208 if (binary != NULL) {
209 result = binary;
210 goto finally;
211 }
Victor Stinner0fe25a42010-06-17 23:08:50 +0000212 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 }
Victor Stinner4c7c8c32010-10-16 13:14:10 +0000214 goto error;
215
216error:
217 result = NULL;
218finally:
219 Py_DECREF(filebytes);
220 return result;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000221}
222
Christian Heimes33fe8092008-04-13 13:53:33 +0000223int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000224_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 int err = 0;
227 int fd;
228 int i;
229 char *found_encoding;
230 char *encoding;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000231 PyObject *io;
232 PyObject *binary;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PyObject *fob = NULL;
234 PyObject *lineobj = NULL;
Antoine Pitroub86680e2010-10-14 21:15:17 +0000235 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 char buf[MAXPATHLEN+1];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200237 int kind;
238 void *data;
Christian Heimes679db4a2008-01-18 09:56:22 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 /* open the file */
241 if (filename == NULL)
242 return 0;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000243
244 io = PyImport_ImportModuleNoBlock("io");
245 if (io == NULL)
246 return -1;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200247 binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb");
Victor Stinner0fe25a42010-06-17 23:08:50 +0000248
249 if (binary == NULL) {
Victor Stinnerceceaa02013-07-16 00:32:14 +0200250 PyErr_Clear();
251
Victor Stinner0fe25a42010-06-17 23:08:50 +0000252 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
253 if (binary == NULL) {
254 Py_DECREF(io);
Victor Stinnerceceaa02013-07-16 00:32:14 +0200255 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 /* use the right encoding to decode the file as unicode */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000260 fd = PyObject_AsFileDescriptor(binary);
Christian Heimes8c077bc2013-07-21 01:53:10 +0200261 if (fd < 0) {
262 Py_DECREF(io);
263 Py_DECREF(binary);
Benjamin Peterson04b01dc2013-07-21 13:26:13 -0700264 return 0;
Christian Heimes8c077bc2013-07-21 01:53:10 +0200265 }
Victor Stinnerfe7c5b52011-04-05 01:48:03 +0200266 found_encoding = PyTokenizer_FindEncodingFilename(fd, filename);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000267 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Christian Heimes1f347292013-07-21 02:12:35 +0200268 /* Reset position */
269 if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
270 Py_DECREF(io);
271 Py_DECREF(binary);
272 PyMem_FREE(found_encoding);
Benjamin Peterson04b01dc2013-07-21 13:26:13 -0700273 return 0;
Christian Heimes1f347292013-07-21 02:12:35 +0200274 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200275 fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000276 Py_DECREF(io);
277 Py_DECREF(binary);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 PyMem_FREE(found_encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (fob == NULL) {
281 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return 0;
283 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 /* get the line number lineno */
286 for (i = 0; i < lineno; i++) {
287 Py_XDECREF(lineobj);
288 lineobj = PyFile_GetLine(fob, -1);
289 if (!lineobj) {
290 err = -1;
291 break;
292 }
293 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200294 res = _PyObject_CallMethodId(fob, &PyId_close, "");
Antoine Pitroub86680e2010-10-14 21:15:17 +0000295 if (res)
296 Py_DECREF(res);
297 else
298 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_DECREF(fob);
300 if (!lineobj || !PyUnicode_Check(lineobj)) {
301 Py_XDECREF(lineobj);
302 return err;
303 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 /* remove the indentation of the line */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200306 kind = PyUnicode_KIND(lineobj);
307 data = PyUnicode_DATA(lineobj);
308 for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) {
309 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
310 if (ch != ' ' && ch != '\t' && ch != '\014')
311 break;
312 }
313 if (i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 PyObject *truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200315 truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (truncated) {
317 Py_DECREF(lineobj);
318 lineobj = truncated;
319 } else {
320 PyErr_Clear();
321 }
322 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 /* Write some spaces before the line */
325 strcpy(buf, " ");
326 assert (strlen(buf) == 10);
327 while (indent > 0) {
Benjamin Peterson0f9b7d32013-07-21 13:29:37 -0700328 if (indent < 10)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 buf[indent] = '\0';
330 err = PyFile_WriteString(buf, f);
331 if (err != 0)
332 break;
333 indent -= 10;
334 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* finally display the line */
337 if (err == 0)
338 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
339 Py_DECREF(lineobj);
340 if (err == 0)
341 err = PyFile_WriteString("\n", f);
342 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000343}
344
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000345static int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000346tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000347{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000348 int err;
349 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (filename == NULL || name == NULL)
352 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000353 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
354 filename, lineno, name);
355 if (line == NULL)
356 return -1;
357 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
358 Py_DECREF(line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (err != 0)
360 return err;
Kristján Valur Jónssonc5963d32012-07-19 21:02:03 +0000361 /* ignore errors since we can't report them, can we? */
362 if (_Py_DisplaySourceLine(f, filename, lineno, 4))
363 PyErr_Clear();
364 return err;
Christian Heimes33fe8092008-04-13 13:53:33 +0000365}
366
367static int
368tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 int err = 0;
371 long depth = 0;
372 PyTracebackObject *tb1 = tb;
373 while (tb1 != NULL) {
374 depth++;
375 tb1 = tb1->tb_next;
376 }
377 while (tb != NULL && err == 0) {
378 if (depth <= limit) {
379 err = tb_displayline(f,
Victor Stinner0fe25a42010-06-17 23:08:50 +0000380 tb->tb_frame->f_code->co_filename,
381 tb->tb_lineno,
382 tb->tb_frame->f_code->co_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 }
384 depth--;
385 tb = tb->tb_next;
386 if (err == 0)
387 err = PyErr_CheckSignals();
388 }
389 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000390}
391
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000392#define PyTraceBack_LIMIT 1000
393
Guido van Rossum3f5da241990-12-20 15:06:42 +0000394int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 int err;
398 PyObject *limitv;
399 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (v == NULL)
402 return 0;
403 if (!PyTraceBack_Check(v)) {
404 PyErr_BadInternalCall();
405 return -1;
406 }
407 limitv = PySys_GetObject("tracebacklimit");
408 if (limitv) {
409 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
412 limit = PyLong_AsLong(limitv);
413 if (limit == -1 && PyErr_Occurred()) {
414 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
415 limit = PyTraceBack_LIMIT;
416 }
417 else {
418 Py_XDECREF(exc_type);
419 Py_XDECREF(exc_value);
420 Py_XDECREF(exc_tb);
421 return 0;
422 }
423 }
424 else if (limit <= 0) {
425 limit = PyTraceBack_LIMIT;
426 }
427 PyErr_Restore(exc_type, exc_value, exc_tb);
428 }
429 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
430 if (!err)
431 err = tb_printinternal((PyTracebackObject *)v, f, limit);
432 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000433}
Victor Stinner024e37a2011-03-31 01:31:06 +0200434
435/* Reverse a string. For example, "abcd" becomes "dcba".
436
437 This function is signal safe. */
438
439static void
440reverse_string(char *text, const size_t len)
441{
442 char tmp;
443 size_t i, j;
444 if (len == 0)
445 return;
446 for (i=0, j=len-1; i < j; i++, j--) {
447 tmp = text[i];
448 text[i] = text[j];
449 text[j] = tmp;
450 }
451}
452
453/* Format an integer in range [0; 999999] to decimal,
454 and write it into the file fd.
455
456 This function is signal safe. */
457
458static void
459dump_decimal(int fd, int value)
460{
461 char buffer[7];
462 int len;
463 if (value < 0 || 999999 < value)
464 return;
465 len = 0;
466 do {
467 buffer[len] = '0' + (value % 10);
468 value /= 10;
469 len++;
470 } while (value);
471 reverse_string(buffer, len);
472 write(fd, buffer, len);
473}
474
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700475/* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits,
Victor Stinner024e37a2011-03-31 01:31:06 +0200476 and write it into the file fd.
477
478 This function is signal safe. */
479
480static void
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700481dump_hexadecimal(int fd, unsigned long value, int width)
Victor Stinner024e37a2011-03-31 01:31:06 +0200482{
Victor Stinner024e37a2011-03-31 01:31:06 +0200483 int len;
484 char buffer[sizeof(unsigned long) * 2 + 1];
485 len = 0;
486 do {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200487 buffer[len] = Py_hexdigits[value & 15];
Victor Stinner024e37a2011-03-31 01:31:06 +0200488 value >>= 4;
489 len++;
490 } while (len < width || value);
491 reverse_string(buffer, len);
492 write(fd, buffer, len);
493}
494
495/* Write an unicode object into the file fd using ascii+backslashreplace.
496
497 This function is signal safe. */
498
499static void
500dump_ascii(int fd, PyObject *text)
501{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200502 PyASCIIObject *ascii = (PyASCIIObject *)text;
Victor Stinner024e37a2011-03-31 01:31:06 +0200503 Py_ssize_t i, size;
504 int truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200505 int kind;
Victor Stinnera336de72011-10-05 22:44:12 +0200506 void *data = NULL;
507 wchar_t *wstr = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200508 Py_UCS4 ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200509
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200510 size = ascii->length;
511 kind = ascii->state.kind;
512 if (ascii->state.compact) {
513 if (ascii->state.ascii)
514 data = ((PyASCIIObject*)text) + 1;
515 else
516 data = ((PyCompactUnicodeObject*)text) + 1;
517 }
Victor Stinnera336de72011-10-05 22:44:12 +0200518 else if (kind != PyUnicode_WCHAR_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200519 data = ((PyUnicodeObject *)text)->data.any;
520 if (data == NULL)
521 return;
522 }
Victor Stinnera336de72011-10-05 22:44:12 +0200523 else {
524 wstr = ((PyASCIIObject *)text)->wstr;
525 if (wstr == NULL)
526 return;
527 size = ((PyCompactUnicodeObject *)text)->wstr_length;
528 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200529
530 if (MAX_STRING_LENGTH < size) {
531 size = MAX_STRING_LENGTH;
532 truncated = 1;
533 }
534 else
535 truncated = 0;
536
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200537 for (i=0; i < size; i++) {
Victor Stinnera336de72011-10-05 22:44:12 +0200538 if (kind != PyUnicode_WCHAR_KIND)
539 ch = PyUnicode_READ(kind, data, i);
540 else
541 ch = wstr[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200542 if (ch < 128) {
543 char c = (char)ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200544 write(fd, &c, 1);
545 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100546 else if (ch < 0xff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200547 PUTS(fd, "\\x");
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700548 dump_hexadecimal(fd, ch, 2);
Victor Stinner024e37a2011-03-31 01:31:06 +0200549 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100550 else if (ch < 0xffff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200551 PUTS(fd, "\\u");
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700552 dump_hexadecimal(fd, ch, 4);
Victor Stinner024e37a2011-03-31 01:31:06 +0200553 }
554 else {
555 PUTS(fd, "\\U");
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700556 dump_hexadecimal(fd, ch, 8);
Victor Stinner024e37a2011-03-31 01:31:06 +0200557 }
558 }
559 if (truncated)
560 PUTS(fd, "...");
561}
562
563/* Write a frame into the file fd: "File "xxx", line xxx in xxx".
564
565 This function is signal safe. */
566
567static void
568dump_frame(int fd, PyFrameObject *frame)
569{
570 PyCodeObject *code;
571 int lineno;
572
573 code = frame->f_code;
574 PUTS(fd, " File ");
575 if (code != NULL && code->co_filename != NULL
576 && PyUnicode_Check(code->co_filename))
577 {
578 write(fd, "\"", 1);
579 dump_ascii(fd, code->co_filename);
580 write(fd, "\"", 1);
581 } else {
582 PUTS(fd, "???");
583 }
584
585 /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200586 lineno = PyCode_Addr2Line(code, frame->f_lasti);
Victor Stinner024e37a2011-03-31 01:31:06 +0200587 PUTS(fd, ", line ");
588 dump_decimal(fd, lineno);
589 PUTS(fd, " in ");
590
591 if (code != NULL && code->co_name != NULL
592 && PyUnicode_Check(code->co_name))
593 dump_ascii(fd, code->co_name);
594 else
595 PUTS(fd, "???");
596
597 write(fd, "\n", 1);
598}
599
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200600static void
Victor Stinner024e37a2011-03-31 01:31:06 +0200601dump_traceback(int fd, PyThreadState *tstate, int write_header)
602{
603 PyFrameObject *frame;
604 unsigned int depth;
605
Victor Stinner024e37a2011-03-31 01:31:06 +0200606 if (write_header)
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700607 PUTS(fd, "Stack (most recent call first):\n");
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200608
609 frame = _PyThreadState_GetFrame(tstate);
610 if (frame == NULL)
611 return;
612
Victor Stinner024e37a2011-03-31 01:31:06 +0200613 depth = 0;
614 while (frame != NULL) {
615 if (MAX_FRAME_DEPTH <= depth) {
616 PUTS(fd, " ...\n");
617 break;
618 }
619 if (!PyFrame_Check(frame))
620 break;
621 dump_frame(fd, frame);
622 frame = frame->f_back;
623 depth++;
624 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200625}
626
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200627void
Victor Stinner024e37a2011-03-31 01:31:06 +0200628_Py_DumpTraceback(int fd, PyThreadState *tstate)
629{
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200630 dump_traceback(fd, tstate, 1);
Victor Stinner024e37a2011-03-31 01:31:06 +0200631}
632
633/* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if
634 is_current is true, "Thread 0xHHHH:\n" otherwise.
635
636 This function is signal safe. */
637
638static void
639write_thread_id(int fd, PyThreadState *tstate, int is_current)
640{
641 if (is_current)
642 PUTS(fd, "Current thread 0x");
643 else
644 PUTS(fd, "Thread 0x");
Guido van Rossum7be5d7d2013-10-20 18:21:02 -0700645 dump_hexadecimal(fd, (unsigned long)tstate->thread_id, sizeof(long)*2);
646 PUTS(fd, " (most recent call first):\n");
Victor Stinner024e37a2011-03-31 01:31:06 +0200647}
648
649const char*
650_Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
651 PyThreadState *current_thread)
652{
653 PyThreadState *tstate;
654 unsigned int nthreads;
655
656 /* Get the current interpreter from the current thread */
657 tstate = PyInterpreterState_ThreadHead(interp);
658 if (tstate == NULL)
659 return "unable to get the thread head state";
660
661 /* Dump the traceback of each thread */
662 tstate = PyInterpreterState_ThreadHead(interp);
663 nthreads = 0;
664 do
665 {
666 if (nthreads != 0)
667 write(fd, "\n", 1);
668 if (nthreads >= MAX_NTHREADS) {
669 PUTS(fd, "...\n");
670 break;
671 }
672 write_thread_id(fd, tstate, tstate == current_thread);
673 dump_traceback(fd, tstate, 0);
674 tstate = PyThreadState_Next(tstate);
675 nthreads++;
676 } while (tstate != NULL);
677
678 return NULL;
679}
680