blob: 1de2df6f5d9ba87a95c088c020b759fe60865204 [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"
Nicholas Bastina7604bf2004-03-21 18:37:23 +000010#include "traceback.h"
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +000011#ifdef HAVE_FCNTL_H
12#include <fcntl.h>
13#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Nicholas Bastina7604bf2004-03-21 18:37:23 +000015#define OFF(x) offsetof(PyTracebackObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000016
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +000017/* Method from Parser/tokenizer.c */
18extern char * PyTokenizer_FindEncoding(int);
19
Collin Winter3eed7652007-08-14 17:53:54 +000020static PyObject *
21tb_dir(PyTracebackObject *self)
22{
23 return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
24 "tb_lasti", "tb_lineno");
25}
26
27static PyMethodDef tb_methods[] = {
28 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
29 {NULL, NULL, 0, NULL},
30};
31
Neal Norwitz8dfc4a92007-08-11 06:39:53 +000032static PyMemberDef tb_memberlist[] = {
33 {"tb_next", T_OBJECT, OFF(tb_next), READONLY},
34 {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
35 {"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
36 {"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000037 {NULL} /* Sentinel */
38};
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000041tb_dealloc(PyTracebackObject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000042{
Neil Schemenauer83584052002-03-29 03:07:29 +000043 PyObject_GC_UnTrack(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000044 Py_TRASHCAN_SAFE_BEGIN(tb)
Guido van Rossum65bf9f21997-04-29 18:33:38 +000045 Py_XDECREF(tb->tb_next);
46 Py_XDECREF(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000047 PyObject_GC_Del(tb);
Guido van Rossumd724b232000-03-13 16:01:29 +000048 Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000049}
50
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000051static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +000052tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000053{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054 Py_VISIT(tb->tb_next);
55 Py_VISIT(tb->tb_frame);
56 return 0;
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000057}
58
59static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000060tb_clear(PyTracebackObject *tb)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000061{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062 Py_CLEAR(tb->tb_next);
63 Py_CLEAR(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000064}
65
Tim Petersd7c36522001-10-22 19:34:09 +000066PyTypeObject PyTraceBack_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000067 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum3f5da241990-12-20 15:06:42 +000068 "traceback",
Nicholas Bastina7604bf2004-03-21 18:37:23 +000069 sizeof(PyTracebackObject),
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 0,
Guido van Rossum13836d91994-08-29 12:09:58 +000071 (destructor)tb_dealloc, /*tp_dealloc*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000072 0, /*tp_print*/
Collin Winteree634a42007-08-14 17:47:27 +000073 0, /*tp_getattr*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000074 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +000075 0, /*tp_reserved*/
Guido van Rossum3f5da241990-12-20 15:06:42 +000076 0, /*tp_repr*/
77 0, /*tp_as_number*/
78 0, /*tp_as_sequence*/
79 0, /*tp_as_mapping*/
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000080 0, /* tp_hash */
81 0, /* tp_call */
82 0, /* tp_str */
Collin Winteree634a42007-08-14 17:47:27 +000083 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000084 0, /* tp_setattro */
85 0, /* tp_as_buffer */
86 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
87 0, /* tp_doc */
88 (traverseproc)tb_traverse, /* tp_traverse */
89 (inquiry)tb_clear, /* tp_clear */
90 0, /* tp_richcompare */
91 0, /* tp_weaklistoffset */
92 0, /* tp_iter */
93 0, /* tp_iternext */
Collin Winter3eed7652007-08-14 17:53:54 +000094 tb_methods, /* tp_methods */
Collin Winteree634a42007-08-14 17:47:27 +000095 tb_memberlist, /* tp_members */
Neal Norwitz8dfc4a92007-08-11 06:39:53 +000096 0, /* tp_getset */
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000097 0, /* tp_base */
98 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +000099};
100
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000101static PyTracebackObject *
102newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103{
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000104 PyTracebackObject *tb;
Tim Petersd7c36522001-10-22 19:34:09 +0000105 if ((next != NULL && !PyTraceBack_Check(next)) ||
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000106 frame == NULL || !PyFrame_Check(frame)) {
107 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 return NULL;
109 }
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000110 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111 if (tb != NULL) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000112 Py_XINCREF(next);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113 tb->tb_next = next;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000114 Py_XINCREF(frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115 tb->tb_frame = frame;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000116 tb->tb_lasti = frame->f_lasti;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000117 tb->tb_lineno = PyFrame_GetLineNumber(frame);
Neil Schemenauer83584052002-03-29 03:07:29 +0000118 PyObject_GC_Track(tb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000119 }
120 return tb;
121}
122
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000124PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125{
Thomas Woutersb2137042007-02-01 18:02:27 +0000126 PyThreadState *tstate = PyThreadState_GET();
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000127 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
128 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129 if (tb == NULL)
130 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 tstate->curexc_traceback = (PyObject *)tb;
132 Py_XDECREF(oldtb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000133 return 0;
134}
135
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000136static int
137_Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open_flags)
138{
139 int i;
140 int fd = -1;
141 PyObject *v;
142 Py_ssize_t _npath;
143 int npath;
144 size_t taillen;
145 PyObject *syspath;
146 const char* path;
147 const char* tail;
148 Py_ssize_t len;
149
150 /* Search tail of filename in sys.path before giving up */
151 tail = strrchr(filename, SEP);
152 if (tail == NULL)
153 tail = filename;
154 else
155 tail++;
156 taillen = strlen(tail);
157
158 syspath = PySys_GetObject("path");
159 if (syspath == NULL || !PyList_Check(syspath))
160 return -1;
161 _npath = PyList_Size(syspath);
162 npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
163
164 for (i = 0; i < npath; i++) {
165 v = PyList_GetItem(syspath, i);
166 if (v == NULL) {
167 PyErr_Clear();
168 break;
169 }
170 if (!PyUnicode_Check(v))
171 continue;
172 path = _PyUnicode_AsStringAndSize(v, &len);
Raymond Hettinger04869042008-12-02 20:59:48 +0000173 if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000174 continue; /* Too long */
175 strcpy(namebuf, path);
176 if (strlen(namebuf) != len)
177 continue; /* v contains '\0' */
178 if (len > 0 && namebuf[len-1] != SEP)
179 namebuf[len++] = SEP;
180 strcpy(namebuf+len, tail);
181 Py_BEGIN_ALLOW_THREADS
182 fd = open(namebuf, open_flags);
183 Py_END_ALLOW_THREADS
184 if (0 <= fd) {
185 return fd;
186 }
187 }
188 return -1;
189}
190
Christian Heimes33fe8092008-04-13 13:53:33 +0000191int
Georg Brandl2ee470f2008-07-16 12:55:28 +0000192_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000193{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000194 int err = 0;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000195 int fd;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000196 int i;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000197 char *found_encoding;
198 char *encoding;
199 PyObject *fob = NULL;
200 PyObject *lineobj = NULL;
201#ifdef O_BINARY
202 const int open_flags = O_RDONLY | O_BINARY; /* necessary for Windows */
203#else
204 const int open_flags = O_RDONLY;
205#endif
206 char buf[MAXPATHLEN+1];
207 Py_UNICODE *u, *p;
208 Py_ssize_t len;
Christian Heimes679db4a2008-01-18 09:56:22 +0000209
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000210 /* open the file */
Christian Heimes33fe8092008-04-13 13:53:33 +0000211 if (filename == NULL)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000212 return 0;
213 Py_BEGIN_ALLOW_THREADS
214 fd = open(filename, open_flags);
215 Py_END_ALLOW_THREADS
216 if (fd < 0) {
217 fd = _Py_FindSourceFile(filename, buf, sizeof(buf), open_flags);
218 if (fd < 0)
219 return 0;
220 filename = buf;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000221 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000222
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000223 /* use the right encoding to decode the file as unicode */
224 found_encoding = PyTokenizer_FindEncoding(fd);
225 encoding = (found_encoding != NULL) ? found_encoding :
226 (char*)PyUnicode_GetDefaultEncoding();
227 lseek(fd, 0, 0); /* Reset position */
228 fob = PyFile_FromFd(fd, (char*)filename, "r", -1, (char*)encoding,
229 NULL, NULL, 1);
230 PyMem_FREE(found_encoding);
231 if (fob == NULL) {
232 PyErr_Clear();
233 close(fd);
234 return 0;
235 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000236
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000237 /* get the line number lineno */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000238 for (i = 0; i < lineno; i++) {
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000239 Py_XDECREF(lineobj);
240 lineobj = PyFile_GetLine(fob, -1);
241 if (!lineobj) {
242 err = -1;
243 break;
Benjamin Petersone6528212008-07-15 15:32:09 +0000244 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000245 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000246 Py_DECREF(fob);
247 if (!lineobj || !PyUnicode_Check(lineobj)) {
248 Py_XDECREF(lineobj);
249 return err;
250 }
251
252 /* remove the indentation of the line */
253 u = PyUnicode_AS_UNICODE(lineobj);
254 len = PyUnicode_GET_SIZE(lineobj);
255 for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++)
256 len--;
257 if (u != p) {
258 PyObject *truncated;
259 truncated = PyUnicode_FromUnicode(p, len);
260 if (truncated) {
261 Py_DECREF(lineobj);
262 lineobj = truncated;
263 } else {
264 PyErr_Clear();
265 }
266 }
267
268 /* Write some spaces before the line */
269 strcpy(buf, " ");
270 assert (strlen(buf) == 10);
271 while (indent > 0) {
272 if(indent < 10)
273 buf[indent] = '\0';
274 err = PyFile_WriteString(buf, f);
275 if (err != 0)
276 break;
277 indent -= 10;
278 }
279
280 /* finally display the line */
281 if (err == 0)
282 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
283 Py_DECREF(lineobj);
284 if (err == 0)
285 err = PyFile_WriteString("\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000286 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000287}
288
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000289static int
Christian Heimes33fe8092008-04-13 13:53:33 +0000290tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000291{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000292 int err = 0;
Christian Heimes33fe8092008-04-13 13:53:33 +0000293 char linebuf[2000];
294
295 if (filename == NULL || name == NULL)
296 return -1;
297 /* This is needed by Emacs' compile command */
298#define FMT " File \"%.500s\", line %d, in %.500s\n"
299 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
300 err = PyFile_WriteString(linebuf, f);
301 if (err != 0)
302 return err;
Georg Brandl2ee470f2008-07-16 12:55:28 +0000303 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Christian Heimes33fe8092008-04-13 13:53:33 +0000304}
305
306static int
307tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
308{
309 int err = 0;
310 long depth = 0;
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000311 PyTracebackObject *tb1 = tb;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000312 while (tb1 != NULL) {
313 depth++;
314 tb1 = tb1->tb_next;
315 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000316 while (tb != NULL && err == 0) {
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000317 if (depth <= limit) {
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000318 err = tb_displayline(f,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000319 _PyUnicode_AsString(
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000320 tb->tb_frame->f_code->co_filename),
Guido van Rossum13836d91994-08-29 12:09:58 +0000321 tb->tb_lineno,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000322 _PyUnicode_AsString(tb->tb_frame->f_code->co_name));
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000323 }
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000324 depth--;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000325 tb = tb->tb_next;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000326 if (err == 0)
327 err = PyErr_CheckSignals();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000328 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000329 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000330}
331
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000332#define PyTraceBack_LIMIT 1000
333
Guido van Rossum3f5da241990-12-20 15:06:42 +0000334int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000336{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000337 int err;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000338 PyObject *limitv;
Christian Heimes33fe8092008-04-13 13:53:33 +0000339 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000340
Guido van Rossum3f5da241990-12-20 15:06:42 +0000341 if (v == NULL)
342 return 0;
Tim Petersd7c36522001-10-22 19:34:09 +0000343 if (!PyTraceBack_Check(v)) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000344 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000345 return -1;
346 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000347 limitv = PySys_GetObject("tracebacklimit");
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000348 if (limitv) {
349 PyObject *exc_type, *exc_value, *exc_tb;
350
351 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
Christian Heimes217cfd12007-12-02 14:31:20 +0000352 limit = PyLong_AsLong(limitv);
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000353 if (limit == -1 && PyErr_Occurred()) {
354 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
355 limit = PyTraceBack_LIMIT;
356 }
357 else {
358 Py_XDECREF(exc_type);
359 Py_XDECREF(exc_value);
360 Py_XDECREF(exc_tb);
361 return 0;
362 }
363 }
364 else if (limit <= 0) {
365 limit = PyTraceBack_LIMIT;
366 }
367 PyErr_Restore(exc_type, exc_value, exc_tb);
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000368 }
Guido van Rossum6d108872000-03-31 00:39:23 +0000369 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000370 if (!err)
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000371 err = tb_printinternal((PyTracebackObject *)v, f, limit);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000372 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000373}