blob: 63ecc3cb16e4329c755374db2de203633cbc7d11 [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*/
75 0, /*tp_compare*/
76 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;
117 tb->tb_lineno = PyCode_Addr2Line(frame->f_code,
118 frame->f_lasti);
Neil Schemenauer83584052002-03-29 03:07:29 +0000119 PyObject_GC_Track(tb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 }
121 return tb;
122}
123
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126{
Thomas Woutersb2137042007-02-01 18:02:27 +0000127 PyThreadState *tstate = PyThreadState_GET();
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000128 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
129 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130 if (tb == NULL)
131 return -1;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132 tstate->curexc_traceback = (PyObject *)tb;
133 Py_XDECREF(oldtb);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134 return 0;
135}
136
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000137static int
138_Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open_flags)
139{
140 int i;
141 int fd = -1;
142 PyObject *v;
143 Py_ssize_t _npath;
144 int npath;
145 size_t taillen;
146 PyObject *syspath;
147 const char* path;
148 const char* tail;
149 Py_ssize_t len;
150
151 /* Search tail of filename in sys.path before giving up */
152 tail = strrchr(filename, SEP);
153 if (tail == NULL)
154 tail = filename;
155 else
156 tail++;
157 taillen = strlen(tail);
158
159 syspath = PySys_GetObject("path");
160 if (syspath == NULL || !PyList_Check(syspath))
161 return -1;
162 _npath = PyList_Size(syspath);
163 npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
164
165 for (i = 0; i < npath; i++) {
166 v = PyList_GetItem(syspath, i);
167 if (v == NULL) {
168 PyErr_Clear();
169 break;
170 }
171 if (!PyUnicode_Check(v))
172 continue;
173 path = _PyUnicode_AsStringAndSize(v, &len);
174 if (len + 1 + taillen >= (Py_ssize_t)namelen - 1)
175 continue; /* Too long */
176 strcpy(namebuf, path);
177 if (strlen(namebuf) != len)
178 continue; /* v contains '\0' */
179 if (len > 0 && namebuf[len-1] != SEP)
180 namebuf[len++] = SEP;
181 strcpy(namebuf+len, tail);
182 Py_BEGIN_ALLOW_THREADS
183 fd = open(namebuf, open_flags);
184 Py_END_ALLOW_THREADS
185 if (0 <= fd) {
186 return fd;
187 }
188 }
189 return -1;
190}
191
Christian Heimes33fe8092008-04-13 13:53:33 +0000192int
Georg Brandl2ee470f2008-07-16 12:55:28 +0000193_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000194{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000195 int err = 0;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000196 int fd;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000197 int i;
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000198 char *found_encoding;
199 char *encoding;
200 PyObject *fob = NULL;
201 PyObject *lineobj = NULL;
202#ifdef O_BINARY
203 const int open_flags = O_RDONLY | O_BINARY; /* necessary for Windows */
204#else
205 const int open_flags = O_RDONLY;
206#endif
207 char buf[MAXPATHLEN+1];
208 Py_UNICODE *u, *p;
209 Py_ssize_t len;
Christian Heimes679db4a2008-01-18 09:56:22 +0000210
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000211 /* open the file */
Christian Heimes33fe8092008-04-13 13:53:33 +0000212 if (filename == NULL)
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000213 return 0;
214 Py_BEGIN_ALLOW_THREADS
215 fd = open(filename, open_flags);
216 Py_END_ALLOW_THREADS
217 if (fd < 0) {
218 fd = _Py_FindSourceFile(filename, buf, sizeof(buf), open_flags);
219 if (fd < 0)
220 return 0;
221 filename = buf;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000222 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000223
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000224 /* use the right encoding to decode the file as unicode */
225 found_encoding = PyTokenizer_FindEncoding(fd);
226 encoding = (found_encoding != NULL) ? found_encoding :
227 (char*)PyUnicode_GetDefaultEncoding();
228 lseek(fd, 0, 0); /* Reset position */
229 fob = PyFile_FromFd(fd, (char*)filename, "r", -1, (char*)encoding,
230 NULL, NULL, 1);
231 PyMem_FREE(found_encoding);
232 if (fob == NULL) {
233 PyErr_Clear();
234 close(fd);
235 return 0;
236 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000237
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000238 /* get the line number lineno */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000239 for (i = 0; i < lineno; i++) {
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000240 Py_XDECREF(lineobj);
241 lineobj = PyFile_GetLine(fob, -1);
242 if (!lineobj) {
243 err = -1;
244 break;
Benjamin Petersone6528212008-07-15 15:32:09 +0000245 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000246 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000247 Py_DECREF(fob);
248 if (!lineobj || !PyUnicode_Check(lineobj)) {
249 Py_XDECREF(lineobj);
250 return err;
251 }
252
253 /* remove the indentation of the line */
254 u = PyUnicode_AS_UNICODE(lineobj);
255 len = PyUnicode_GET_SIZE(lineobj);
256 for (p=u; *p == ' ' || *p == '\t' || *p == '\014'; p++)
257 len--;
258 if (u != p) {
259 PyObject *truncated;
260 truncated = PyUnicode_FromUnicode(p, len);
261 if (truncated) {
262 Py_DECREF(lineobj);
263 lineobj = truncated;
264 } else {
265 PyErr_Clear();
266 }
267 }
268
269 /* Write some spaces before the line */
270 strcpy(buf, " ");
271 assert (strlen(buf) == 10);
272 while (indent > 0) {
273 if(indent < 10)
274 buf[indent] = '\0';
275 err = PyFile_WriteString(buf, f);
276 if (err != 0)
277 break;
278 indent -= 10;
279 }
280
281 /* finally display the line */
282 if (err == 0)
283 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
284 Py_DECREF(lineobj);
285 if (err == 0)
286 err = PyFile_WriteString("\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000287 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000288}
289
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000290static int
Christian Heimes33fe8092008-04-13 13:53:33 +0000291tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000292{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000293 int err = 0;
Christian Heimes33fe8092008-04-13 13:53:33 +0000294 char linebuf[2000];
295
296 if (filename == NULL || name == NULL)
297 return -1;
298 /* This is needed by Emacs' compile command */
299#define FMT " File \"%.500s\", line %d, in %.500s\n"
300 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
301 err = PyFile_WriteString(linebuf, f);
302 if (err != 0)
303 return err;
Georg Brandl2ee470f2008-07-16 12:55:28 +0000304 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Christian Heimes33fe8092008-04-13 13:53:33 +0000305}
306
307static int
308tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
309{
310 int err = 0;
311 long depth = 0;
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000312 PyTracebackObject *tb1 = tb;
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000313 while (tb1 != NULL) {
314 depth++;
315 tb1 = tb1->tb_next;
316 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000317 while (tb != NULL && err == 0) {
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000318 if (depth <= limit) {
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000319 err = tb_displayline(f,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000320 _PyUnicode_AsString(
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000321 tb->tb_frame->f_code->co_filename),
Guido van Rossum13836d91994-08-29 12:09:58 +0000322 tb->tb_lineno,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000323 _PyUnicode_AsString(tb->tb_frame->f_code->co_name));
Guido van Rossumb3f515a1997-01-24 04:02:55 +0000324 }
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000325 depth--;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000326 tb = tb->tb_next;
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000327 if (err == 0)
328 err = PyErr_CheckSignals();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329 }
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000330 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000331}
332
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000333#define PyTraceBack_LIMIT 1000
334
Guido van Rossum3f5da241990-12-20 15:06:42 +0000335int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000337{
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000338 int err;
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000339 PyObject *limitv;
Christian Heimes33fe8092008-04-13 13:53:33 +0000340 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000341
Guido van Rossum3f5da241990-12-20 15:06:42 +0000342 if (v == NULL)
343 return 0;
Tim Petersd7c36522001-10-22 19:34:09 +0000344 if (!PyTraceBack_Check(v)) {
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000345 PyErr_BadInternalCall();
Guido van Rossum3f5da241990-12-20 15:06:42 +0000346 return -1;
347 }
Guido van Rossum65bf9f21997-04-29 18:33:38 +0000348 limitv = PySys_GetObject("tracebacklimit");
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000349 if (limitv) {
350 PyObject *exc_type, *exc_value, *exc_tb;
351
352 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
Christian Heimes217cfd12007-12-02 14:31:20 +0000353 limit = PyLong_AsLong(limitv);
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000354 if (limit == -1 && PyErr_Occurred()) {
355 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
356 limit = PyTraceBack_LIMIT;
357 }
358 else {
359 Py_XDECREF(exc_type);
360 Py_XDECREF(exc_value);
361 Py_XDECREF(exc_tb);
362 return 0;
363 }
364 }
365 else if (limit <= 0) {
366 limit = PyTraceBack_LIMIT;
367 }
368 PyErr_Restore(exc_type, exc_value, exc_tb);
Guido van Rossum67a5fdb1993-12-17 12:09:14 +0000369 }
Guido van Rossum6d108872000-03-31 00:39:23 +0000370 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000371 if (!err)
Nicholas Bastina7604bf2004-03-21 18:37:23 +0000372 err = tb_printinternal((PyTracebackObject *)v, f, limit);
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000373 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000374}