Commit | Line | Data |
---|---|---|
ab4f3efb | 1 | /* Printing of backtraces and error messages |
78a0461a | 2 | * Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation |
ab4f3efb MD |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2, or (at your option) | |
7 | * any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
16 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
17 | * Boston, MA 02111-1307 USA | |
ab4f3efb MD |
18 | * |
19 | * As a special exception, the Free Software Foundation gives permission | |
20 | * for additional uses of the text contained in its release of GUILE. | |
21 | * | |
22 | * The exception is that, if you link the GUILE library with other files | |
23 | * to produce an executable, this does not by itself cause the | |
24 | * resulting executable to be covered by the GNU General Public License. | |
25 | * Your use of that executable is in no way restricted on account of | |
26 | * linking the GUILE library code into it. | |
27 | * | |
28 | * This exception does not however invalidate any other reasons why | |
29 | * the executable file might be covered by the GNU General Public License. | |
30 | * | |
31 | * This exception applies only to the code released by the | |
32 | * Free Software Foundation under the name GUILE. If you copy | |
33 | * code from other Free Software Foundation releases into a copy of | |
34 | * GUILE, as the General Public License permits, the exception does | |
35 | * not apply to the code that you add in this way. To avoid misleading | |
36 | * anyone as to the status of such modified files, you must delete | |
37 | * this exception notice from them. | |
38 | * | |
39 | * If you write modifications of your own for GUILE, it is your choice | |
40 | * whether to permit this exception to apply to your modifications. | |
41 | * If you do not wish that, delete this exception notice. | |
42 | * | |
43 | * The author can be reached at djurfeldt@nada.kth.se | |
82892bed | 44 | * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */ |
ab4f3efb | 45 | |
1bbd0b84 GB |
46 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
47 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
48 | ||
49 | ||
ab4f3efb | 50 | #include <stdio.h> |
2b407f9b MD |
51 | #include <ctype.h> |
52 | ||
2fb36297 JB |
53 | #include "_scm.h" |
54 | ||
b1508ade MD |
55 | #ifdef HAVE_UNISTD_H |
56 | #include <unistd.h> | |
57 | #endif | |
58 | ||
ab4f3efb MD |
59 | #include "stacks.h" |
60 | #include "srcprop.h" | |
61 | #include "genio.h" | |
62 | #include "struct.h" | |
63 | #include "strports.h" | |
bdf8afff | 64 | #include "throw.h" |
a5d6d578 | 65 | #include "fluids.h" |
ab4f3efb | 66 | |
0c95b57d | 67 | #include "scm_validate.h" |
ab4f3efb MD |
68 | #include "backtrace.h" |
69 | ||
70 | /* {Error reporting and backtraces} | |
71 | * (A first approximation.) | |
72 | * | |
73 | * Note that these functions shouldn't generate errors themselves. | |
74 | */ | |
75 | ||
76 | #ifndef SCM_RECKLESS | |
77 | #undef SCM_ASSERT | |
78 | #define SCM_ASSERT(_cond, _arg, _pos, _subr) \ | |
79 | if (!(_cond)) \ | |
80 | return SCM_BOOL_F; | |
81 | #endif | |
82 | ||
b6609fc7 MD |
83 | SCM scm_the_last_stack_fluid; |
84 | ||
ab4f3efb | 85 | static void |
1bbd0b84 | 86 | display_header (SCM source, SCM port) |
ab4f3efb | 87 | { |
0c95b57d | 88 | SCM fname = (SCM_MEMOIZEDP (source) |
7f2d92b1 | 89 | ? scm_source_property (source, scm_sym_filename) |
ab4f3efb | 90 | : SCM_BOOL_F); |
0c95b57d | 91 | if (SCM_STRINGP (fname)) |
ab4f3efb MD |
92 | { |
93 | scm_prin1 (fname, port, 0); | |
b7f3516f | 94 | scm_putc (':', port); |
7f2d92b1 | 95 | scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_line)) + 1, |
0e929db3 MD |
96 | 10, |
97 | port); | |
b7f3516f | 98 | scm_putc (':', port); |
7f2d92b1 | 99 | scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_column)) + 1, |
0e929db3 MD |
100 | 10, |
101 | port); | |
ab4f3efb MD |
102 | } |
103 | else | |
b7f3516f TT |
104 | scm_puts ("ERROR", port); |
105 | scm_puts (": ", port); | |
ab4f3efb MD |
106 | } |
107 | ||
f3acc5c1 JB |
108 | |
109 | void | |
6e8d25a6 | 110 | scm_display_error_message (SCM message, SCM args, SCM port) |
ab4f3efb MD |
111 | { |
112 | int writingp; | |
113 | char *start; | |
114 | char *p; | |
115 | ||
89958ad0 | 116 | if (SCM_IMP (message) || !SCM_ROSTRINGP (message) || SCM_IMP (args) |
c37e0e55 | 117 | || !scm_list_p (args)) |
ab4f3efb MD |
118 | { |
119 | scm_prin1 (message, port, 0); | |
b7f3516f | 120 | scm_putc ('\n', port); |
ab4f3efb MD |
121 | return; |
122 | } | |
123 | ||
89958ad0 JB |
124 | SCM_COERCE_SUBSTR (message); |
125 | start = SCM_ROCHARS (message); | |
ab4f3efb MD |
126 | for (p = start; *p != '\0'; ++p) |
127 | if (*p == '%') | |
128 | { | |
129 | if (SCM_IMP (args) || SCM_NCONSP (args)) | |
130 | continue; | |
131 | ||
132 | ++p; | |
133 | if (*p == 's') | |
134 | writingp = 0; | |
135 | else if (*p == 'S') | |
136 | writingp = 1; | |
137 | else | |
138 | continue; | |
139 | ||
b7f3516f | 140 | scm_lfwrite (start, p - start - 1, port); |
ab4f3efb MD |
141 | scm_prin1 (SCM_CAR (args), port, writingp); |
142 | args = SCM_CDR (args); | |
143 | start = p + 1; | |
144 | } | |
b7f3516f TT |
145 | scm_lfwrite (start, p - start, port); |
146 | scm_putc ('\n', port); | |
ab4f3efb MD |
147 | } |
148 | ||
ab4f3efb | 149 | static void |
1bbd0b84 | 150 | display_expression (SCM frame,SCM pname,SCM source,SCM port) |
ab4f3efb MD |
151 | { |
152 | SCM print_state = scm_make_print_state (); | |
153 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
154 | pstate->writingp = 0; | |
155 | pstate->fancyp = 1; | |
156 | pstate->level = 2; | |
157 | pstate->length = 3; | |
0c95b57d | 158 | if (SCM_ROSTRINGP (pname)) |
ab4f3efb | 159 | { |
cabe682c | 160 | if (SCM_FRAMEP (frame) |
ab4f3efb | 161 | && SCM_FRAME_EVAL_ARGS_P (frame)) |
b7f3516f | 162 | scm_puts ("While evaluating arguments to ", port); |
ab4f3efb | 163 | else |
b7f3516f | 164 | scm_puts ("In procedure ", port); |
ab4f3efb | 165 | scm_iprin1 (pname, port, pstate); |
0c95b57d | 166 | if (SCM_MEMOIZEDP (source)) |
ab4f3efb | 167 | { |
b7f3516f | 168 | scm_puts (" in expression ", port); |
ab4f3efb MD |
169 | pstate->writingp = 1; |
170 | scm_iprin1 (scm_unmemoize (source), port, pstate); | |
171 | } | |
172 | } | |
173 | else if (SCM_NIMP (source)) | |
174 | { | |
b7f3516f | 175 | scm_puts ("In expression ", port); |
ab4f3efb MD |
176 | pstate->writingp = 1; |
177 | scm_iprin1 (scm_unmemoize (source), port, pstate); | |
178 | } | |
b7f3516f | 179 | scm_puts (":\n", port); |
ab4f3efb MD |
180 | scm_free_print_state (print_state); |
181 | } | |
182 | ||
bdf8afff MD |
183 | struct display_error_args { |
184 | SCM stack; | |
185 | SCM port; | |
186 | SCM subr; | |
187 | SCM message; | |
188 | SCM args; | |
189 | SCM rest; | |
190 | }; | |
191 | ||
192 | static SCM | |
39752bec | 193 | display_error_body (struct display_error_args *a) |
ab4f3efb MD |
194 | { |
195 | SCM current_frame = SCM_BOOL_F; | |
196 | SCM source = SCM_BOOL_F; | |
197 | SCM pname = SCM_BOOL_F; | |
a88a4c8a JB |
198 | SCM prev_frame = SCM_BOOL_F; |
199 | ||
841076ac | 200 | if (SCM_DEBUGGINGP |
bdf8afff MD |
201 | && SCM_STACKP (a->stack) |
202 | && SCM_STACK_LENGTH (a->stack) > 0) | |
ab4f3efb | 203 | { |
bdf8afff | 204 | current_frame = scm_stack_ref (a->stack, SCM_INUM0); |
ab4f3efb | 205 | source = SCM_FRAME_SOURCE (current_frame); |
a88a4c8a | 206 | prev_frame = SCM_FRAME_PREV (current_frame); |
0c95b57d | 207 | if (!SCM_MEMOIZEDP (source) |
a88a4c8a JB |
208 | && prev_frame != SCM_BOOL_F) |
209 | source = SCM_FRAME_SOURCE (prev_frame); | |
ab4f3efb | 210 | if (SCM_FRAME_PROC_P (current_frame) |
016e2ce1 | 211 | && scm_procedure_p (SCM_FRAME_PROC (current_frame)) == SCM_BOOL_T) |
ab4f3efb MD |
212 | pname = scm_procedure_name (SCM_FRAME_PROC (current_frame)); |
213 | } | |
0c95b57d | 214 | if (!SCM_ROSTRINGP (pname)) |
bdf8afff | 215 | pname = a->subr; |
0c95b57d | 216 | if (SCM_ROSTRINGP (pname) || SCM_MEMOIZEDP (source)) |
ab4f3efb | 217 | { |
bdf8afff MD |
218 | display_header (source, a->port); |
219 | display_expression (current_frame, pname, source, a->port); | |
ab4f3efb | 220 | } |
bdf8afff MD |
221 | display_header (source, a->port); |
222 | scm_display_error_message (a->message, a->args, a->port); | |
223 | return SCM_UNSPECIFIED; | |
224 | } | |
225 | ||
226 | struct display_error_handler_data { | |
227 | char *mode; | |
228 | SCM port; | |
229 | }; | |
230 | ||
231 | /* This is the exception handler for error reporting routines. | |
232 | Note that it is very important that this handler *doesn't* try to | |
233 | print more than the error tag, since the error very probably is | |
234 | caused by an erroneous print call-back routine. If we would | |
235 | tru to print all objects, we would enter an infinite loop. */ | |
236 | static SCM | |
237 | display_error_handler (struct display_error_handler_data *data, | |
238 | SCM tag, SCM args) | |
239 | { | |
240 | SCM print_state = scm_make_print_state (); | |
b7f3516f TT |
241 | scm_puts ("\nException during displaying of ", data->port); |
242 | scm_puts (data->mode, data->port); | |
243 | scm_puts (": ", data->port); | |
bdf8afff | 244 | scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state)); |
b7f3516f | 245 | scm_putc ('\n', data->port); |
bdf8afff MD |
246 | return SCM_UNSPECIFIED; |
247 | } | |
248 | ||
a1ec6916 | 249 | SCM_DEFINE(scm_display_error, "display-error", 6, 0, 0, |
1bbd0b84 GB |
250 | (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest), |
251 | "") | |
252 | #define FUNC_NAME s_scm_display_error | |
bdf8afff | 253 | { |
0b2cb4ee MD |
254 | struct display_error_args a; |
255 | struct display_error_handler_data data; | |
256 | a.stack = stack; | |
257 | a.port = port; | |
258 | a.subr = subr; | |
259 | a.message = message; | |
260 | a.args = args; | |
261 | a.rest = rest; | |
262 | data.mode = "error"; | |
263 | data.port = port; | |
bdf8afff MD |
264 | scm_internal_catch (SCM_BOOL_T, |
265 | (scm_catch_body_t) display_error_body, &a, | |
266 | (scm_catch_handler_t) display_error_handler, &data); | |
ab4f3efb MD |
267 | return SCM_UNSPECIFIED; |
268 | } | |
1bbd0b84 | 269 | #undef FUNC_NAME |
ab4f3efb | 270 | |
2b407f9b MD |
271 | typedef struct { |
272 | int level; | |
273 | int length; | |
274 | } print_params_t; | |
275 | ||
276 | static int n_print_params = 9; | |
277 | static print_params_t default_print_params[] = { | |
278 | { 4, 9 }, { 4, 3 }, | |
279 | { 3, 4 }, { 3, 3 }, | |
280 | { 2, 4 }, { 2, 3 }, | |
281 | { 1, 4 }, { 1, 3 }, { 1, 2 } | |
282 | }; | |
283 | static print_params_t *print_params = default_print_params; | |
284 | ||
285 | #ifdef GUILE_DEBUG | |
a1ec6916 | 286 | SCM_DEFINE(scm_set_print_params_x, "set-print-params!", 1, 0, 0, |
5623a9b4 MD |
287 | (SCM params), |
288 | "") | |
e8e9b690 | 289 | #define FUNC_NAME s_scm_set_print_params_x |
2b407f9b MD |
290 | { |
291 | int i, n = scm_ilength (params); | |
292 | SCM ls; | |
293 | print_params_t *new_params; | |
1bbd0b84 | 294 | SCM_ASSERT (n >= 1, params, SCM_ARG2, FUNC_NAME); |
2b407f9b MD |
295 | for (ls = params; SCM_NIMP (ls); ls = SCM_CDR (ls)) |
296 | SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2 | |
297 | && SCM_INUMP (SCM_CAAR (ls)) | |
298 | && SCM_INUM (SCM_CAAR (ls)) >= 0 | |
299 | && SCM_INUMP (SCM_CADAR (ls)) | |
300 | && SCM_INUM (SCM_CADAR (ls)) >= 0, | |
301 | params, | |
302 | SCM_ARG2, | |
e8e9b690 | 303 | s_scm_set_print_params_x); |
2b407f9b | 304 | new_params = scm_must_malloc (n * sizeof (print_params_t), |
1bbd0b84 | 305 | FUNC_NAME); |
2b407f9b MD |
306 | if (print_params != default_print_params) |
307 | scm_must_free (print_params); | |
308 | print_params = new_params; | |
309 | for (i = 0; i < n; ++i) | |
310 | { | |
311 | print_params[i].level = SCM_INUM (SCM_CAAR (params)); | |
312 | print_params[i].length = SCM_INUM (SCM_CADAR (params)); | |
313 | params = SCM_CDR (params); | |
314 | } | |
315 | n_print_params = n; | |
316 | return SCM_UNSPECIFIED; | |
317 | } | |
1bbd0b84 | 318 | #undef FUNC_NAME |
2b407f9b MD |
319 | #endif |
320 | ||
ab4f3efb | 321 | static void |
1bbd0b84 | 322 | indent (int n, SCM port) |
ab4f3efb MD |
323 | { |
324 | int i; | |
325 | for (i = 0; i < n; ++i) | |
b7f3516f | 326 | scm_putc (' ', port); |
ab4f3efb MD |
327 | } |
328 | ||
ab4f3efb | 329 | static void |
1bbd0b84 | 330 | display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
ab4f3efb | 331 | { |
2b407f9b MD |
332 | SCM string; |
333 | int i = 0, n; | |
334 | scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport); | |
335 | do | |
ab4f3efb | 336 | { |
2b407f9b MD |
337 | pstate->length = print_params[i].length; |
338 | ptob->seek (sport, 0, SEEK_SET); | |
0c95b57d | 339 | if (SCM_CONSP (exp)) |
2b407f9b MD |
340 | { |
341 | pstate->level = print_params[i].level - 1; | |
342 | scm_iprlist (hdr, exp, tlr[0], sport, pstate); | |
343 | scm_puts (&tlr[1], sport); | |
344 | } | |
345 | else | |
346 | { | |
347 | pstate->level = print_params[i].level; | |
348 | scm_iprin1 (exp, sport, pstate); | |
349 | } | |
350 | ptob->flush (sport); | |
351 | n = ptob->seek (sport, 0, SEEK_CUR); | |
352 | ++i; | |
ab4f3efb | 353 | } |
2b407f9b MD |
354 | while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params); |
355 | ptob->truncate (sport, n); | |
356 | string = scm_strport_to_string (sport); | |
357 | /* Remove control characters */ | |
358 | for (i = 0; i < n; ++i) | |
359 | if (iscntrl (SCM_CHARS (string)[i])) | |
360 | SCM_CHARS (string)[i] = ' '; | |
361 | /* Truncate */ | |
362 | if (indentation + n > SCM_BACKTRACE_WIDTH) | |
363 | { | |
364 | n = SCM_BACKTRACE_WIDTH - indentation; | |
365 | SCM_CHARS (string)[n - 1] = '$'; | |
366 | } | |
367 | ||
368 | scm_lfwrite (SCM_CHARS (string), n, port); | |
ab4f3efb MD |
369 | } |
370 | ||
e3c37929 | 371 | static void |
1bbd0b84 | 372 | display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
e3c37929 MD |
373 | { |
374 | SCM proc = SCM_FRAME_PROC (frame); | |
375 | SCM name = (SCM_NFALSEP (scm_procedure_p (proc)) | |
376 | ? scm_procedure_name (proc) | |
377 | : SCM_BOOL_F); | |
378 | display_frame_expr ("[", | |
379 | scm_cons (SCM_NFALSEP (name) ? name : proc, | |
380 | SCM_FRAME_ARGS (frame)), | |
381 | SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]", | |
382 | indentation, | |
383 | sport, | |
384 | port, | |
385 | pstate); | |
386 | } | |
387 | ||
a1ec6916 | 388 | SCM_DEFINE(scm_display_application, "display-application", 1, 2, 0, |
1bbd0b84 GB |
389 | (SCM frame, SCM port, SCM indent), |
390 | "") | |
391 | #define FUNC_NAME s_scm_display_application | |
e3c37929 | 392 | { |
0c95b57d | 393 | SCM_VALIDATE_FRAME(1,frame); |
e3c37929 MD |
394 | if (SCM_UNBNDP (port)) |
395 | port = scm_cur_outp; | |
2b407f9b | 396 | else |
0c95b57d | 397 | SCM_VALIDATE_OPOUTPORT(2,port); |
2b407f9b MD |
398 | if (SCM_UNBNDP (indent)) |
399 | indent = SCM_INUM0; | |
400 | else | |
47c6b75e | 401 | SCM_VALIDATE_INUM(3,indent); |
2b407f9b | 402 | |
e3c37929 MD |
403 | if (SCM_FRAME_PROC_P (frame)) |
404 | /* Display an application. */ | |
405 | { | |
2b407f9b | 406 | SCM sport, print_state; |
e3c37929 MD |
407 | scm_print_state *pstate; |
408 | ||
2b407f9b MD |
409 | /* Create a string port used for adaptation of printing parameters. */ |
410 | sport = scm_mkstrport (SCM_INUM0, | |
411 | scm_make_string (SCM_MAKINUM (240), | |
412 | SCM_UNDEFINED), | |
413 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 414 | FUNC_NAME); |
2b407f9b | 415 | |
e3c37929 MD |
416 | /* Create a print state for printing of frames. */ |
417 | print_state = scm_make_print_state (); | |
418 | pstate = SCM_PRINT_STATE (print_state); | |
419 | pstate->writingp = 1; | |
420 | pstate->fancyp = 1; | |
e3c37929 | 421 | |
2b407f9b | 422 | display_application (frame, SCM_INUM (indent), sport, port, pstate); |
e3c37929 MD |
423 | return SCM_BOOL_T; |
424 | } | |
425 | else | |
426 | return SCM_BOOL_F; | |
427 | } | |
1bbd0b84 | 428 | #undef FUNC_NAME |
e3c37929 | 429 | |
ab4f3efb | 430 | static void |
1bbd0b84 | 431 | display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
ab4f3efb MD |
432 | { |
433 | int n, i, j; | |
434 | ||
435 | /* Announce missing frames? */ | |
436 | if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame)) | |
437 | { | |
438 | indent (nfield + 1 + indentation, port); | |
b7f3516f | 439 | scm_puts ("...\n", port); |
ab4f3efb MD |
440 | } |
441 | ||
442 | /* Check size of frame number. */ | |
443 | n = SCM_FRAME_NUMBER (frame); | |
444 | for (i = 0, j = n; j > 0; ++i) j /= 10; | |
445 | ||
446 | /* Number indentation. */ | |
447 | indent (nfield - (i ? i : 1), port); | |
448 | ||
449 | /* Frame number. */ | |
450 | scm_iprin1 (SCM_MAKINUM (n), port, pstate); | |
451 | ||
452 | /* Real frame marker */ | |
b7f3516f | 453 | scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port); |
ab4f3efb MD |
454 | |
455 | /* Indentation. */ | |
456 | indent (indentation, port); | |
457 | ||
458 | if (SCM_FRAME_PROC_P (frame)) | |
459 | /* Display an application. */ | |
e3c37929 | 460 | display_application (frame, nfield + 1 + indentation, sport, port, pstate); |
ab4f3efb MD |
461 | else |
462 | /* Display a special form. */ | |
463 | { | |
464 | SCM source = SCM_FRAME_SOURCE (frame); | |
0c95b57d | 465 | SCM copy = (SCM_CONSP (source) |
7f2d92b1 | 466 | ? scm_source_property (source, scm_sym_copy) |
2e9d6c6d | 467 | : SCM_BOOL_F); |
0c95b57d | 468 | SCM umcopy = (SCM_MEMOIZEDP (source) |
2e9d6c6d MD |
469 | ? scm_unmemoize (source) |
470 | : SCM_BOOL_F); | |
ab4f3efb | 471 | display_frame_expr ("(", |
0c95b57d | 472 | SCM_CONSP (copy) ? copy : umcopy, |
ab4f3efb MD |
473 | ")", |
474 | nfield + 1 + indentation, | |
475 | sport, | |
476 | port, | |
477 | pstate); | |
478 | } | |
1ae6af28 | 479 | scm_putc ('\n', port); |
ab4f3efb MD |
480 | |
481 | /* Announce missing frames? */ | |
482 | if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame)) | |
483 | { | |
484 | indent (nfield + 1 + indentation, port); | |
b7f3516f | 485 | scm_puts ("...\n", port); |
ab4f3efb MD |
486 | } |
487 | } | |
488 | ||
bdf8afff MD |
489 | struct display_backtrace_args { |
490 | SCM stack; | |
491 | SCM port; | |
492 | SCM first; | |
493 | SCM depth; | |
494 | }; | |
495 | ||
bdf8afff | 496 | static SCM |
1bbd0b84 GB |
497 | display_backtrace_body(struct display_backtrace_args *a) |
498 | #define FUNC_NAME "display_backtrace_body" | |
ab4f3efb MD |
499 | { |
500 | int n_frames, beg, end, n, i, j; | |
501 | int nfield, indent_p, indentation; | |
502 | SCM frame, sport, print_state; | |
503 | scm_print_state *pstate; | |
504 | ||
78446828 MV |
505 | a->port = SCM_COERCE_OUTPORT (a->port); |
506 | ||
ab4f3efb | 507 | /* Argument checking and extraction. */ |
0c95b57d | 508 | SCM_ASSERT (SCM_STACKP (a->stack), |
bdf8afff | 509 | a->stack, |
ab4f3efb MD |
510 | SCM_ARG1, |
511 | s_display_backtrace); | |
0c95b57d | 512 | SCM_ASSERT (SCM_OPOUTPORTP (a->port), |
bdf8afff | 513 | a->port, |
ab4f3efb MD |
514 | SCM_ARG2, |
515 | s_display_backtrace); | |
bdf8afff MD |
516 | n_frames = SCM_INUM (scm_stack_length (a->stack)); |
517 | n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH; | |
ab4f3efb MD |
518 | if (SCM_BACKWARDS_P) |
519 | { | |
bdf8afff | 520 | beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0; |
ab4f3efb MD |
521 | end = beg + n - 1; |
522 | if (end >= n_frames) | |
523 | end = n_frames - 1; | |
524 | n = end - beg + 1; | |
525 | } | |
526 | else | |
527 | { | |
bdf8afff | 528 | if (SCM_INUMP (a->first)) |
ab4f3efb | 529 | { |
bdf8afff | 530 | beg = SCM_INUM (a->first); |
ab4f3efb MD |
531 | end = beg - n + 1; |
532 | if (end < 0) | |
533 | end = 0; | |
534 | } | |
535 | else | |
536 | { | |
537 | beg = n - 1; | |
538 | end = 0; | |
539 | if (beg >= n_frames) | |
540 | beg = n_frames - 1; | |
541 | } | |
542 | n = beg - end + 1; | |
543 | } | |
bdf8afff MD |
544 | SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace); |
545 | SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace); | |
ab4f3efb MD |
546 | |
547 | /* Create a string port used for adaptation of printing parameters. */ | |
548 | sport = scm_mkstrport (SCM_INUM0, | |
549 | scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED), | |
550 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 551 | FUNC_NAME); |
ab4f3efb MD |
552 | |
553 | /* Create a print state for printing of frames. */ | |
554 | print_state = scm_make_print_state (); | |
555 | pstate = SCM_PRINT_STATE (print_state); | |
556 | pstate->writingp = 1; | |
557 | pstate->fancyp = 1; | |
558 | ||
559 | /* First find out if it's reasonable to do indentation. */ | |
560 | if (SCM_BACKWARDS_P) | |
561 | indent_p = 0; | |
562 | else | |
563 | { | |
564 | indent_p = 1; | |
bdf8afff | 565 | frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg)); |
ab4f3efb MD |
566 | for (i = 0, j = 0; i < n; ++i) |
567 | { | |
568 | if (SCM_FRAME_REAL_P (frame)) | |
569 | ++j; | |
570 | if (j > SCM_BACKTRACE_INDENT) | |
571 | { | |
572 | indent_p = 0; | |
573 | break; | |
574 | } | |
575 | frame = (SCM_BACKWARDS_P | |
576 | ? SCM_FRAME_PREV (frame) | |
577 | : SCM_FRAME_NEXT (frame)); | |
578 | } | |
579 | } | |
580 | ||
581 | /* Determine size of frame number field. */ | |
bdf8afff | 582 | j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end))); |
ab4f3efb MD |
583 | for (i = 0; j > 0; ++i) j /= 10; |
584 | nfield = i ? i : 1; | |
585 | ||
ab4f3efb | 586 | /* Print frames. */ |
bdf8afff | 587 | frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg)); |
ab4f3efb | 588 | indentation = 1; |
bdf8afff | 589 | display_frame (frame, nfield, indentation, sport, a->port, pstate); |
ab4f3efb MD |
590 | for (i = 1; i < n; ++i) |
591 | { | |
592 | if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame)) | |
593 | ++indentation; | |
594 | frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame); | |
bdf8afff | 595 | display_frame (frame, nfield, indentation, sport, a->port, pstate); |
ab4f3efb | 596 | } |
bdf8afff MD |
597 | |
598 | return SCM_UNSPECIFIED; | |
599 | } | |
1bbd0b84 | 600 | #undef FUNC_NAME |
bdf8afff | 601 | |
a1ec6916 | 602 | SCM_DEFINE(scm_display_backtrace, "display-backtrace", 2, 2, 0, |
1bbd0b84 GB |
603 | (SCM stack, SCM port, SCM first, SCM depth), |
604 | "") | |
605 | #define FUNC_NAME s_scm_display_backtrace | |
bdf8afff | 606 | { |
0b2cb4ee MD |
607 | struct display_backtrace_args a; |
608 | struct display_error_handler_data data; | |
609 | a.stack = stack; | |
610 | a.port = port; | |
611 | a.first = first; | |
612 | a.depth = depth; | |
613 | data.mode = "backtrace"; | |
614 | data.port = port; | |
bdf8afff MD |
615 | scm_internal_catch (SCM_BOOL_T, |
616 | (scm_catch_body_t) display_backtrace_body, &a, | |
617 | (scm_catch_handler_t) display_error_handler, &data); | |
ab4f3efb MD |
618 | return SCM_UNSPECIFIED; |
619 | } | |
1bbd0b84 | 620 | #undef FUNC_NAME |
ab4f3efb | 621 | |
78f9f47b | 622 | SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?"); |
5aab5d96 | 623 | |
a1ec6916 | 624 | SCM_DEFINE(scm_backtrace, "backtrace", 0, 0, 0, |
1bbd0b84 GB |
625 | (), |
626 | "") | |
627 | #define FUNC_NAME s_scm_backtrace | |
5aab5d96 | 628 | { |
b6609fc7 | 629 | SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid)); |
a5d6d578 | 630 | if (SCM_NFALSEP (the_last_stack)) |
5aab5d96 MD |
631 | { |
632 | scm_newline (scm_cur_outp); | |
3a1f8447 | 633 | scm_puts ("Backtrace:\n", scm_cur_outp); |
a5d6d578 | 634 | scm_display_backtrace (the_last_stack, |
5aab5d96 MD |
635 | scm_cur_outp, |
636 | SCM_UNDEFINED, | |
637 | SCM_UNDEFINED); | |
638 | scm_newline (scm_cur_outp); | |
639 | if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var)) | |
640 | && !SCM_BACKTRACE_P) | |
641 | { | |
b7f3516f TT |
642 | scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like " |
643 | "a backtrace\n" | |
644 | "automatically if an error occurs in the future.\n", | |
645 | scm_cur_outp); | |
5aab5d96 MD |
646 | SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T); |
647 | } | |
648 | } | |
649 | else | |
650 | { | |
b7f3516f | 651 | scm_puts ("No backtrace available.\n", scm_cur_outp); |
5aab5d96 MD |
652 | } |
653 | return SCM_UNSPECIFIED; | |
654 | } | |
1bbd0b84 | 655 | #undef FUNC_NAME |
5aab5d96 | 656 | |
ab4f3efb MD |
657 | \f |
658 | ||
659 | void | |
660 | scm_init_backtrace () | |
661 | { | |
a5d6d578 | 662 | SCM f = scm_make_fluid (); |
b6609fc7 | 663 | scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f); |
5aab5d96 | 664 | |
ab4f3efb MD |
665 | #include "backtrace.x" |
666 | } |