blob: fd5309ae3f7ebf722ff625258d8c42cff561819e [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +000011
Nicholas Bastina7604bf2004-03-21 18:37:23 +000012#define OFF(x) offsetof(PyTracebackObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
Benjamin Peterson6ffe8522009-03-18 20:52:15 +000014static PyMemberDef tb_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000015 {"tb_next", T_OBJECT, OFF(tb_next), READONLY},
16 {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
17 {"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
18 {"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
19 {NULL} /* Sentinel */
Guido van Rossum3f5da241990-12-20 15:06:42 +000020};
21
Guido van Rossum3f5da241990-12-20 15:06:42 +000022static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000023tb_dealloc(PyTracebackObject *tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000024{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000025 PyObject_GC_UnTrack(tb);
26 Py_TRASHCAN_SAFE_BEGIN(tb)
27 Py_XDECREF(tb->tb_next);
28 Py_XDECREF(tb->tb_frame);
29 PyObject_GC_Del(tb);
30 Py_TRASHCAN_SAFE_END(tb)
Guido van Rossum3f5da241990-12-20 15:06:42 +000031}
32
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000033static int
Nicholas Bastina7604bf2004-03-21 18:37:23 +000034tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000035{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 Py_VISIT(tb->tb_next);
37 Py_VISIT(tb->tb_frame);
38 return 0;
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000039}
40
41static void
Nicholas Bastina7604bf2004-03-21 18:37:23 +000042tb_clear(PyTracebackObject *tb)
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000043{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000044 Py_CLEAR(tb->tb_next);
45 Py_CLEAR(tb->tb_frame);
Jeremy Hyltonfd14d8e2001-10-22 22:17:41 +000046}
47
Tim Petersd7c36522001-10-22 19:34:09 +000048PyTypeObject PyTraceBack_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000049 PyVarObject_HEAD_INIT(&PyType_Type, 0)
50 "traceback",
51 sizeof(PyTracebackObject),
52 0,
53 (destructor)tb_dealloc, /*tp_dealloc*/
54 0, /*tp_print*/
55 0, /*tp_getattr*/
56 0, /*tp_setattr*/
57 0, /*tp_compare*/
58 0, /*tp_repr*/
59 0, /*tp_as_number*/
60 0, /*tp_as_sequence*/
61 0, /*tp_as_mapping*/
62 0, /* tp_hash */
63 0, /* tp_call */
64 0, /* tp_str */
65 0, /* tp_getattro */
66 0, /* tp_setattro */
67 0, /* tp_as_buffer */
68 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
69 0, /* tp_doc */
70 (traverseproc)tb_traverse, /* tp_traverse */
71 (inquiry)tb_clear, /* tp_clear */
72 0, /* tp_richcompare */
73 0, /* tp_weaklistoffset */
74 0, /* tp_iter */
75 0, /* tp_iternext */
76 0, /* tp_methods */
77 tb_memberlist, /* tp_members */
78 0, /* tp_getset */
79 0, /* tp_base */
80 0, /* tp_dict */
Guido van Rossum3f5da241990-12-20 15:06:42 +000081};
82
Nicholas Bastina7604bf2004-03-21 18:37:23 +000083static PyTracebackObject *
84newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +000085{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 PyTracebackObject *tb;
87 if ((next != NULL && !PyTraceBack_Check(next)) ||
88 frame == NULL || !PyFrame_Check(frame)) {
89 PyErr_BadInternalCall();
90 return NULL;
91 }
92 tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
93 if (tb != NULL) {
94 Py_XINCREF(next);
95 tb->tb_next = next;
96 Py_XINCREF(frame);
97 tb->tb_frame = frame;
98 tb->tb_lasti = frame->f_lasti;
99 tb->tb_lineno = PyFrame_GetLineNumber(frame);
100 PyObject_GC_Track(tb);
101 }
102 return tb;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103}
104
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106PyTraceBack_Here(PyFrameObject *frame)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 PyThreadState *tstate = PyThreadState_GET();
109 PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
110 PyTracebackObject *tb = newtracebackobject(oldtb, frame);
111 if (tb == NULL)
112 return -1;
113 tstate->curexc_traceback = (PyObject *)tb;
114 Py_XDECREF(oldtb);
115 return 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116}
117
Brett Cannone9746892008-04-12 23:44:07 +0000118int
Amaury Forgeot d'Arc2252d112008-07-11 21:45:06 +0000119_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 int err = 0;
122 FILE *xfp = NULL;
123 char linebuf[2000];
124 int i;
125 char namebuf[MAXPATHLEN+1];
Christian Heimes3e8c8972008-01-18 08:47:59 +0000126
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 if (filename == NULL)
128 return -1;
129 /* This is needed by Emacs' compile command */
Tim Peters6d20b432001-11-27 20:30:42 +0000130#define FMT " File \"%.500s\", line %d, in %.500s\n"
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131 xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
132 if (xfp == NULL) {
133 /* Search tail of filename in sys.path before giving up */
134 PyObject *path;
135 const char *tail = strrchr(filename, SEP);
136 if (tail == NULL)
137 tail = filename;
138 else
139 tail++;
140 path = PySys_GetObject("path");
141 if (path != NULL && PyList_Check(path)) {
142 Py_ssize_t _npath = PyList_Size(path);
143 int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
144 size_t taillen = strlen(tail);
145 for (i = 0; i < npath; i++) {
146 PyObject *v = PyList_GetItem(path, i);
147 if (v == NULL) {
148 PyErr_Clear();
149 break;
150 }
151 if (PyString_Check(v)) {
152 size_t len;
153 len = PyString_GET_SIZE(v);
154 if (len + 1 + taillen >= MAXPATHLEN)
155 continue; /* Too long */
156 strcpy(namebuf, PyString_AsString(v));
157 if (strlen(namebuf) != len)
158 continue; /* v contains '\0' */
159 if (len > 0 && namebuf[len-1] != SEP)
160 namebuf[len++] = SEP;
161 strcpy(namebuf+len, tail);
162 xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
163 if (xfp != NULL) {
164 break;
165 }
166 }
167 }
168 }
169 }
Brett Cannone9746892008-04-12 23:44:07 +0000170
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000171 if (xfp == NULL)
172 return err;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000173
174 for (i = 0; i < lineno; i++) {
175 char* pLastChar = &linebuf[sizeof(linebuf)-2];
176 do {
177 *pLastChar = '\0';
178 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
179 break;
180 /* fgets read *something*; if it didn't get as
181 far as pLastChar, it must have found a newline
182 or hit the end of the file; if pLastChar is \n,
183 it obviously found a newline; else we haven't
184 yet seen a newline, so must continue */
185 } while (*pLastChar != '\0' && *pLastChar != '\n');
186 }
187 if (i == lineno) {
188 char buf[11];
189 char *p = linebuf;
190 while (*p == ' ' || *p == '\t' || *p == '\014')
191 p++;
192
193 /* Write some spaces before the line */
194 strcpy(buf, " ");
195 assert (strlen(buf) == 10);
196 while (indent > 0) {
197 if(indent < 10)
198 buf[indent] = '\0';
199 err = PyFile_WriteString(buf, f);
200 if (err != 0)
201 break;
202 indent -= 10;
Brett Cannone9746892008-04-12 23:44:07 +0000203 }
204
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 if (err == 0)
206 err = PyFile_WriteString(p, f);
207 if (err == 0 && strchr(p, '\n') == NULL)
208 err = PyFile_WriteString("\n", f);
209 }
210 fclose(xfp);
211 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000212}
213
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000214static int
Brett Cannone9746892008-04-12 23:44:07 +0000215tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000216{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000217 int err = 0;
218 char linebuf[2000];
Brett Cannone9746892008-04-12 23:44:07 +0000219
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 if (filename == NULL || name == NULL)
221 return -1;
222 /* This is needed by Emacs' compile command */
Brett Cannone9746892008-04-12 23:44:07 +0000223#define FMT " File \"%.500s\", line %d, in %.500s\n"
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
225 err = PyFile_WriteString(linebuf, f);
226 if (err != 0)
227 return err;
228 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Brett Cannone9746892008-04-12 23:44:07 +0000229}
230
231static int
232tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
233{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 int err = 0;
235 long depth = 0;
236 PyTracebackObject *tb1 = tb;
237 while (tb1 != NULL) {
238 depth++;
239 tb1 = tb1->tb_next;
240 }
241 while (tb != NULL && err == 0) {
242 if (depth <= limit) {
243 err = tb_displayline(f,
244 PyString_AsString(
245 tb->tb_frame->f_code->co_filename),
246 tb->tb_lineno,
247 PyString_AsString(tb->tb_frame->f_code->co_name));
248 }
249 depth--;
250 tb = tb->tb_next;
251 if (err == 0)
252 err = PyErr_CheckSignals();
253 }
254 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000255}
256
257int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000259{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000260 int err;
261 PyObject *limitv;
262 long limit = 1000;
263 if (v == NULL)
264 return 0;
265 if (!PyTraceBack_Check(v)) {
266 PyErr_BadInternalCall();
267 return -1;
268 }
269 limitv = PySys_GetObject("tracebacklimit");
270 if (limitv && PyInt_Check(limitv)) {
271 limit = PyInt_AsLong(limitv);
272 if (limit <= 0)
273 return 0;
274 }
275 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
276 if (!err)
277 err = tb_printinternal((PyTracebackObject *)v, f, limit);
278 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000279}