blob: c8b3ee1b63f930e72f378d20d5b1304472de39ba [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 Stinner024e37a2011-03-31 01:31:06 +020016#define PUTS(fd, str) write(fd, str, strlen(str))
17#define MAX_STRING_LENGTH 100
18#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) {
249 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
250 if (binary == NULL) {
251 Py_DECREF(io);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 return 0;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* use the right encoding to decode the file as unicode */
Victor Stinner0fe25a42010-06-17 23:08:50 +0000257 fd = PyObject_AsFileDescriptor(binary);
Victor Stinnerfe7c5b52011-04-05 01:48:03 +0200258 found_encoding = PyTokenizer_FindEncodingFilename(fd, filename);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000259 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 lseek(fd, 0, 0); /* Reset position */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200261 fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000262 Py_DECREF(io);
263 Py_DECREF(binary);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PyMem_FREE(found_encoding);
Victor Stinner0fe25a42010-06-17 23:08:50 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 if (fob == NULL) {
267 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 return 0;
269 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 /* get the line number lineno */
272 for (i = 0; i < lineno; i++) {
273 Py_XDECREF(lineobj);
274 lineobj = PyFile_GetLine(fob, -1);
275 if (!lineobj) {
276 err = -1;
277 break;
278 }
279 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200280 res = _PyObject_CallMethodId(fob, &PyId_close, "");
Antoine Pitroub86680e2010-10-14 21:15:17 +0000281 if (res)
282 Py_DECREF(res);
283 else
284 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_DECREF(fob);
286 if (!lineobj || !PyUnicode_Check(lineobj)) {
287 Py_XDECREF(lineobj);
288 return err;
289 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* remove the indentation of the line */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200292 kind = PyUnicode_KIND(lineobj);
293 data = PyUnicode_DATA(lineobj);
294 for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) {
295 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
296 if (ch != ' ' && ch != '\t' && ch != '\014')
297 break;
298 }
299 if (i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 PyObject *truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200301 truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (truncated) {
303 Py_DECREF(lineobj);
304 lineobj = truncated;
305 } else {
306 PyErr_Clear();
307 }
308 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 /* Write some spaces before the line */
311 strcpy(buf, " ");
312 assert (strlen(buf) == 10);
313 while (indent > 0) {
314 if(indent < 10)
315 buf[indent] = '\0';
316 err = PyFile_WriteString(buf, f);
317 if (err != 0)
318 break;
319 indent -= 10;
320 }
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 /* finally display the line */
323 if (err == 0)
324 err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
325 Py_DECREF(lineobj);
326 if (err == 0)
327 err = PyFile_WriteString("\n", f);
328 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000329}
330
Guido van Rossum7e8d26d1997-05-22 22:35:47 +0000331static int
Victor Stinner0fe25a42010-06-17 23:08:50 +0000332tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000333{
Victor Stinner0fe25a42010-06-17 23:08:50 +0000334 int err;
335 PyObject *line;
Christian Heimes33fe8092008-04-13 13:53:33 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 if (filename == NULL || name == NULL)
338 return -1;
Victor Stinner0fe25a42010-06-17 23:08:50 +0000339 line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
340 filename, lineno, name);
341 if (line == NULL)
342 return -1;
343 err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
344 Py_DECREF(line);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (err != 0)
346 return err;
347 return _Py_DisplaySourceLine(f, filename, lineno, 4);
Christian Heimes33fe8092008-04-13 13:53:33 +0000348}
349
350static int
351tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 int err = 0;
354 long depth = 0;
355 PyTracebackObject *tb1 = tb;
356 while (tb1 != NULL) {
357 depth++;
358 tb1 = tb1->tb_next;
359 }
360 while (tb != NULL && err == 0) {
361 if (depth <= limit) {
362 err = tb_displayline(f,
Victor Stinner0fe25a42010-06-17 23:08:50 +0000363 tb->tb_frame->f_code->co_filename,
364 tb->tb_lineno,
365 tb->tb_frame->f_code->co_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 }
367 depth--;
368 tb = tb->tb_next;
369 if (err == 0)
370 err = PyErr_CheckSignals();
371 }
372 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000373}
374
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000375#define PyTraceBack_LIMIT 1000
376
Guido van Rossum3f5da241990-12-20 15:06:42 +0000377int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378PyTraceBack_Print(PyObject *v, PyObject *f)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 int err;
381 PyObject *limitv;
382 long limit = PyTraceBack_LIMIT;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (v == NULL)
385 return 0;
386 if (!PyTraceBack_Check(v)) {
387 PyErr_BadInternalCall();
388 return -1;
389 }
390 limitv = PySys_GetObject("tracebacklimit");
391 if (limitv) {
392 PyObject *exc_type, *exc_value, *exc_tb;
Christian Heimes0fbab7f2007-12-04 21:55:18 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
395 limit = PyLong_AsLong(limitv);
396 if (limit == -1 && PyErr_Occurred()) {
397 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
398 limit = PyTraceBack_LIMIT;
399 }
400 else {
401 Py_XDECREF(exc_type);
402 Py_XDECREF(exc_value);
403 Py_XDECREF(exc_tb);
404 return 0;
405 }
406 }
407 else if (limit <= 0) {
408 limit = PyTraceBack_LIMIT;
409 }
410 PyErr_Restore(exc_type, exc_value, exc_tb);
411 }
412 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
413 if (!err)
414 err = tb_printinternal((PyTracebackObject *)v, f, limit);
415 return err;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000416}
Victor Stinner024e37a2011-03-31 01:31:06 +0200417
418/* Reverse a string. For example, "abcd" becomes "dcba".
419
420 This function is signal safe. */
421
422static void
423reverse_string(char *text, const size_t len)
424{
425 char tmp;
426 size_t i, j;
427 if (len == 0)
428 return;
429 for (i=0, j=len-1; i < j; i++, j--) {
430 tmp = text[i];
431 text[i] = text[j];
432 text[j] = tmp;
433 }
434}
435
436/* Format an integer in range [0; 999999] to decimal,
437 and write it into the file fd.
438
439 This function is signal safe. */
440
441static void
442dump_decimal(int fd, int value)
443{
444 char buffer[7];
445 int len;
446 if (value < 0 || 999999 < value)
447 return;
448 len = 0;
449 do {
450 buffer[len] = '0' + (value % 10);
451 value /= 10;
452 len++;
453 } while (value);
454 reverse_string(buffer, len);
455 write(fd, buffer, len);
456}
457
458/* Format an integer in range [0; 0xffffffff] to hexdecimal of 'width' digits,
459 and write it into the file fd.
460
461 This function is signal safe. */
462
463static void
464dump_hexadecimal(int width, unsigned long value, int fd)
465{
Victor Stinner024e37a2011-03-31 01:31:06 +0200466 int len;
467 char buffer[sizeof(unsigned long) * 2 + 1];
468 len = 0;
469 do {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200470 buffer[len] = Py_hexdigits[value & 15];
Victor Stinner024e37a2011-03-31 01:31:06 +0200471 value >>= 4;
472 len++;
473 } while (len < width || value);
474 reverse_string(buffer, len);
475 write(fd, buffer, len);
476}
477
478/* Write an unicode object into the file fd using ascii+backslashreplace.
479
480 This function is signal safe. */
481
482static void
483dump_ascii(int fd, PyObject *text)
484{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200485 PyASCIIObject *ascii = (PyASCIIObject *)text;
Victor Stinner024e37a2011-03-31 01:31:06 +0200486 Py_ssize_t i, size;
487 int truncated;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200488 int kind;
Victor Stinnera336de72011-10-05 22:44:12 +0200489 void *data = NULL;
490 wchar_t *wstr = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200491 Py_UCS4 ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200492
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200493 size = ascii->length;
494 kind = ascii->state.kind;
495 if (ascii->state.compact) {
496 if (ascii->state.ascii)
497 data = ((PyASCIIObject*)text) + 1;
498 else
499 data = ((PyCompactUnicodeObject*)text) + 1;
500 }
Victor Stinnera336de72011-10-05 22:44:12 +0200501 else if (kind != PyUnicode_WCHAR_KIND) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200502 data = ((PyUnicodeObject *)text)->data.any;
503 if (data == NULL)
504 return;
505 }
Victor Stinnera336de72011-10-05 22:44:12 +0200506 else {
507 wstr = ((PyASCIIObject *)text)->wstr;
508 if (wstr == NULL)
509 return;
510 size = ((PyCompactUnicodeObject *)text)->wstr_length;
511 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200512
513 if (MAX_STRING_LENGTH < size) {
514 size = MAX_STRING_LENGTH;
515 truncated = 1;
516 }
517 else
518 truncated = 0;
519
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200520 for (i=0; i < size; i++) {
Victor Stinnera336de72011-10-05 22:44:12 +0200521 if (kind != PyUnicode_WCHAR_KIND)
522 ch = PyUnicode_READ(kind, data, i);
523 else
524 ch = wstr[i];
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200525 if (ch < 128) {
526 char c = (char)ch;
Victor Stinner024e37a2011-03-31 01:31:06 +0200527 write(fd, &c, 1);
528 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100529 else if (ch < 0xff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200530 PUTS(fd, "\\x");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200531 dump_hexadecimal(2, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200532 }
Victor Stinner63ab8752011-11-22 03:31:20 +0100533 else if (ch < 0xffff) {
Victor Stinner024e37a2011-03-31 01:31:06 +0200534 PUTS(fd, "\\u");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200535 dump_hexadecimal(4, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200536 }
537 else {
538 PUTS(fd, "\\U");
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200539 dump_hexadecimal(8, ch, fd);
Victor Stinner024e37a2011-03-31 01:31:06 +0200540 }
541 }
542 if (truncated)
543 PUTS(fd, "...");
544}
545
546/* Write a frame into the file fd: "File "xxx", line xxx in xxx".
547
548 This function is signal safe. */
549
550static void
551dump_frame(int fd, PyFrameObject *frame)
552{
553 PyCodeObject *code;
554 int lineno;
555
556 code = frame->f_code;
557 PUTS(fd, " File ");
558 if (code != NULL && code->co_filename != NULL
559 && PyUnicode_Check(code->co_filename))
560 {
561 write(fd, "\"", 1);
562 dump_ascii(fd, code->co_filename);
563 write(fd, "\"", 1);
564 } else {
565 PUTS(fd, "???");
566 }
567
568 /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200569 lineno = PyCode_Addr2Line(code, frame->f_lasti);
Victor Stinner024e37a2011-03-31 01:31:06 +0200570 PUTS(fd, ", line ");
571 dump_decimal(fd, lineno);
572 PUTS(fd, " in ");
573
574 if (code != NULL && code->co_name != NULL
575 && PyUnicode_Check(code->co_name))
576 dump_ascii(fd, code->co_name);
577 else
578 PUTS(fd, "???");
579
580 write(fd, "\n", 1);
581}
582
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200583static void
Victor Stinner024e37a2011-03-31 01:31:06 +0200584dump_traceback(int fd, PyThreadState *tstate, int write_header)
585{
586 PyFrameObject *frame;
587 unsigned int depth;
588
Victor Stinner024e37a2011-03-31 01:31:06 +0200589 if (write_header)
590 PUTS(fd, "Traceback (most recent call first):\n");
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200591
592 frame = _PyThreadState_GetFrame(tstate);
593 if (frame == NULL)
594 return;
595
Victor Stinner024e37a2011-03-31 01:31:06 +0200596 depth = 0;
597 while (frame != NULL) {
598 if (MAX_FRAME_DEPTH <= depth) {
599 PUTS(fd, " ...\n");
600 break;
601 }
602 if (!PyFrame_Check(frame))
603 break;
604 dump_frame(fd, frame);
605 frame = frame->f_back;
606 depth++;
607 }
Victor Stinner024e37a2011-03-31 01:31:06 +0200608}
609
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200610void
Victor Stinner024e37a2011-03-31 01:31:06 +0200611_Py_DumpTraceback(int fd, PyThreadState *tstate)
612{
Victor Stinnerfcb88c42011-04-01 15:34:01 +0200613 dump_traceback(fd, tstate, 1);
Victor Stinner024e37a2011-03-31 01:31:06 +0200614}
615
616/* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if
617 is_current is true, "Thread 0xHHHH:\n" otherwise.
618
619 This function is signal safe. */
620
621static void
622write_thread_id(int fd, PyThreadState *tstate, int is_current)
623{
624 if (is_current)
625 PUTS(fd, "Current thread 0x");
626 else
627 PUTS(fd, "Thread 0x");
628 dump_hexadecimal(sizeof(long)*2, (unsigned long)tstate->thread_id, fd);
629 PUTS(fd, ":\n");
630}
631
632const char*
633_Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
634 PyThreadState *current_thread)
635{
636 PyThreadState *tstate;
637 unsigned int nthreads;
638
639 /* Get the current interpreter from the current thread */
640 tstate = PyInterpreterState_ThreadHead(interp);
641 if (tstate == NULL)
642 return "unable to get the thread head state";
643
644 /* Dump the traceback of each thread */
645 tstate = PyInterpreterState_ThreadHead(interp);
646 nthreads = 0;
647 do
648 {
649 if (nthreads != 0)
650 write(fd, "\n", 1);
651 if (nthreads >= MAX_NTHREADS) {
652 PUTS(fd, "...\n");
653 break;
654 }
655 write_thread_id(fd, tstate, tstate == current_thread);
656 dump_traceback(fd, tstate, 0);
657 tstate = PyThreadState_Next(tstate);
658 nthreads++;
659 } while (tstate != NULL);
660
661 return NULL;
662}
663