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