Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Traceback implementation */ |
| 33 | |
| 34 | #include "allobjects.h" |
| 35 | |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 36 | #include "sysmodule.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | #include "compile.h" |
| 38 | #include "frameobject.h" |
| 39 | #include "traceback.h" |
| 40 | #include "structmember.h" |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 41 | #include "osdefs.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | |
| 43 | typedef struct _tracebackobject { |
| 44 | OB_HEAD |
| 45 | struct _tracebackobject *tb_next; |
| 46 | frameobject *tb_frame; |
| 47 | int tb_lasti; |
| 48 | int tb_lineno; |
| 49 | } tracebackobject; |
| 50 | |
| 51 | #define OFF(x) offsetof(tracebackobject, x) |
| 52 | |
| 53 | static struct memberlist tb_memberlist[] = { |
| 54 | {"tb_next", T_OBJECT, OFF(tb_next)}, |
| 55 | {"tb_frame", T_OBJECT, OFF(tb_frame)}, |
| 56 | {"tb_lasti", T_INT, OFF(tb_lasti)}, |
| 57 | {"tb_lineno", T_INT, OFF(tb_lineno)}, |
| 58 | {NULL} /* Sentinel */ |
| 59 | }; |
| 60 | |
| 61 | static object * |
| 62 | tb_getattr(tb, name) |
| 63 | tracebackobject *tb; |
| 64 | char *name; |
| 65 | { |
| 66 | return getmember((char *)tb, tb_memberlist, name); |
| 67 | } |
| 68 | |
| 69 | static void |
| 70 | tb_dealloc(tb) |
| 71 | tracebackobject *tb; |
| 72 | { |
| 73 | XDECREF(tb->tb_next); |
| 74 | XDECREF(tb->tb_frame); |
| 75 | DEL(tb); |
| 76 | } |
| 77 | |
Guido van Rossum | 9d78d8d | 1995-09-18 21:29:36 +0000 | [diff] [blame] | 78 | #define Tracebacktype PyTraceBack_Type |
| 79 | #define is_tracebackobject PyTraceBack_Check |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | |
| 81 | typeobject Tracebacktype = { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | OB_HEAD_INIT(&Typetype) |
| 83 | 0, |
| 84 | "traceback", |
| 85 | sizeof(tracebackobject), |
| 86 | 0, |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 87 | (destructor)tb_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 88 | 0, /*tp_print*/ |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 89 | (getattrfunc)tb_getattr, /*tp_getattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 90 | 0, /*tp_setattr*/ |
| 91 | 0, /*tp_compare*/ |
| 92 | 0, /*tp_repr*/ |
| 93 | 0, /*tp_as_number*/ |
| 94 | 0, /*tp_as_sequence*/ |
| 95 | 0, /*tp_as_mapping*/ |
| 96 | }; |
| 97 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | static tracebackobject * |
| 99 | newtracebackobject(next, frame, lasti, lineno) |
| 100 | tracebackobject *next; |
| 101 | frameobject *frame; |
| 102 | int lasti, lineno; |
| 103 | { |
| 104 | tracebackobject *tb; |
| 105 | if ((next != NULL && !is_tracebackobject(next)) || |
| 106 | frame == NULL || !is_frameobject(frame)) { |
| 107 | err_badcall(); |
| 108 | return NULL; |
| 109 | } |
| 110 | tb = NEWOBJ(tracebackobject, &Tracebacktype); |
| 111 | if (tb != NULL) { |
| 112 | XINCREF(next); |
| 113 | tb->tb_next = next; |
| 114 | XINCREF(frame); |
| 115 | tb->tb_frame = frame; |
| 116 | tb->tb_lasti = lasti; |
| 117 | tb->tb_lineno = lineno; |
| 118 | } |
| 119 | return tb; |
| 120 | } |
| 121 | |
| 122 | static tracebackobject *tb_current = NULL; |
| 123 | |
| 124 | int |
Guido van Rossum | c6515d1 | 1992-01-14 18:44:48 +0000 | [diff] [blame] | 125 | tb_here(frame) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 126 | frameobject *frame; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | { |
| 128 | tracebackobject *tb; |
Guido van Rossum | c6515d1 | 1992-01-14 18:44:48 +0000 | [diff] [blame] | 129 | tb = newtracebackobject(tb_current, frame, frame->f_lasti, frame->f_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 130 | if (tb == NULL) |
| 131 | return -1; |
| 132 | XDECREF(tb_current); |
| 133 | tb_current = tb; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | object * |
| 138 | tb_fetch() |
| 139 | { |
| 140 | object *v; |
| 141 | v = (object *)tb_current; |
| 142 | tb_current = NULL; |
| 143 | return v; |
| 144 | } |
| 145 | |
| 146 | int |
| 147 | tb_store(v) |
| 148 | object *v; |
| 149 | { |
| 150 | if (v != NULL && !is_tracebackobject(v)) { |
| 151 | err_badcall(); |
| 152 | return -1; |
| 153 | } |
| 154 | XDECREF(tb_current); |
| 155 | XINCREF(v); |
| 156 | tb_current = (tracebackobject *)v; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 161 | tb_displayline(f, filename, lineno, name) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 162 | object *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 163 | char *filename; |
| 164 | int lineno; |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 165 | char *name; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 166 | { |
| 167 | FILE *xfp; |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 168 | char linebuf[1000]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 169 | int i; |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 170 | #ifdef MPW |
| 171 | /* This is needed by MPW's File and Line commands */ |
| 172 | #define FMT " File \"%.900s\"; line %d # in %s\n" |
| 173 | #else |
| 174 | /* This is needed by Emacs' compile command */ |
| 175 | #define FMT " File \"%.900s\", line %d, in %s\n" |
| 176 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 177 | xfp = fopen(filename, "r"); |
| 178 | if (xfp == NULL) { |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 179 | /* Search tail of filename in sys.path before giving up */ |
| 180 | object *path; |
| 181 | char *tail = strrchr(filename, SEP); |
| 182 | if (tail == NULL) |
| 183 | tail = filename; |
| 184 | else |
| 185 | tail++; |
| 186 | path = sysget("path"); |
| 187 | if (path != NULL && is_listobject(path)) { |
| 188 | int npath = getlistsize(path); |
Guido van Rossum | bfd5d755 | 1994-09-29 09:38:04 +0000 | [diff] [blame] | 189 | int taillen = strlen(tail); |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 190 | char namebuf[MAXPATHLEN+1]; |
| 191 | for (i = 0; i < npath; i++) { |
| 192 | object *v = getlistitem(path, i); |
| 193 | if (is_stringobject(v)) { |
| 194 | int len; |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 195 | len = getstringsize(v); |
Guido van Rossum | bfd5d755 | 1994-09-29 09:38:04 +0000 | [diff] [blame] | 196 | if (len + 1 + taillen >= MAXPATHLEN) |
| 197 | continue; /* Too long */ |
| 198 | strcpy(namebuf, getstringvalue(v)); |
| 199 | if (strlen(namebuf) != len) |
| 200 | continue; /* v contains '\0' */ |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 201 | if (len > 0 && namebuf[len-1] != SEP) |
| 202 | namebuf[len++] = SEP; |
| 203 | strcpy(namebuf+len, tail); |
| 204 | xfp = fopen(namebuf, "r"); |
| 205 | if (xfp != NULL) { |
| 206 | filename = namebuf; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 212 | } |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 213 | sprintf(linebuf, FMT, filename, lineno, name); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 214 | writestring(linebuf, f); |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 215 | if (xfp == NULL) |
| 216 | return; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 217 | for (i = 0; i < lineno; i++) { |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 218 | if (fgets(linebuf, sizeof linebuf, xfp) == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | if (i == lineno) { |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 222 | char *p = linebuf; |
Guido van Rossum | e78c5d0 | 1995-07-07 22:45:41 +0000 | [diff] [blame] | 223 | while (*p == ' ' || *p == '\t' || *p == '\014') |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 224 | p++; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 225 | writestring(" ", f); |
| 226 | writestring(p, f); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 227 | if (strchr(p, '\n') == NULL) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 228 | writestring("\n", f); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 229 | } |
| 230 | fclose(xfp); |
| 231 | } |
| 232 | |
| 233 | static void |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 234 | tb_printinternal(tb, f, limit) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 235 | tracebackobject *tb; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 236 | object *f; |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 237 | int limit; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 238 | { |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 239 | int depth = 0; |
| 240 | tracebackobject *tb1 = tb; |
| 241 | while (tb1 != NULL) { |
| 242 | depth++; |
| 243 | tb1 = tb1->tb_next; |
| 244 | } |
Guido van Rossum | 7169dbb | 1992-02-26 15:17:59 +0000 | [diff] [blame] | 245 | while (tb != NULL && !intrcheck()) { |
Guido van Rossum | b3f515a | 1997-01-24 04:02:55 +0000 | [diff] [blame] | 246 | if (depth <= limit) { |
| 247 | tb->tb_lineno = PyCode_Addr2Line(tb->tb_frame->f_code, |
| 248 | tb->tb_lasti); |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 249 | tb_displayline(f, |
| 250 | getstringvalue(tb->tb_frame->f_code->co_filename), |
Guido van Rossum | 13836d9 | 1994-08-29 12:09:58 +0000 | [diff] [blame] | 251 | tb->tb_lineno, |
| 252 | getstringvalue(tb->tb_frame->f_code->co_name)); |
Guido van Rossum | b3f515a | 1997-01-24 04:02:55 +0000 | [diff] [blame] | 253 | } |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 254 | depth--; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 255 | tb = tb->tb_next; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | int |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 260 | tb_print(v, f) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 261 | object *v; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 262 | object *f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 263 | { |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 264 | object *limitv; |
| 265 | int limit = 1000; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 266 | if (v == NULL) |
| 267 | return 0; |
| 268 | if (!is_tracebackobject(v)) { |
| 269 | err_badcall(); |
| 270 | return -1; |
| 271 | } |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 272 | limitv = sysget("tracebacklimit"); |
| 273 | if (limitv && is_intobject(limitv)) { |
| 274 | limit = getintvalue(limitv); |
| 275 | if (limit <= 0) |
| 276 | return 0; |
| 277 | } |
| 278 | writestring("Traceback (innermost last):\n", f); |
| 279 | tb_printinternal((tracebackobject *)v, f, limit); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 280 | return 0; |
| 281 | } |