1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation
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)
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.
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
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
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.
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
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.
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.
43 * The author can be reached at djurfeldt@nada.kth.se
44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
67 #include "backtrace.h"
69 /* {Error reporting and backtraces}
70 * (A first approximation.)
72 * Note that these functions shouldn't generate errors themselves.
77 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
82 SCM scm_the_last_stack_fluid
;
85 display_header (SCM source
, SCM port
)
87 SCM fname
= (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)
88 ? scm_source_property (source
, scm_sym_filename
)
90 if (SCM_NIMP (fname
) && SCM_STRINGP (fname
))
92 scm_prin1 (fname
, port
, 0);
94 scm_intprint (SCM_INUM (scm_source_property (source
, scm_sym_line
)) + 1,
98 scm_intprint (SCM_INUM (scm_source_property (source
, scm_sym_column
)) + 1,
103 scm_puts ("ERROR", port
);
104 scm_puts (": ", port
);
109 scm_display_error_message (message
, args
, port
)
118 if (SCM_IMP (message
) || !SCM_ROSTRINGP (message
) || SCM_IMP (args
)
119 || !scm_list_p (args
))
121 scm_prin1 (message
, port
, 0);
122 scm_putc ('\n', port
);
126 SCM_COERCE_SUBSTR (message
);
127 start
= SCM_ROCHARS (message
);
128 for (p
= start
; *p
!= '\0'; ++p
)
131 if (SCM_IMP (args
) || SCM_NCONSP (args
))
142 scm_lfwrite (start
, p
- start
- 1, port
);
143 scm_prin1 (SCM_CAR (args
), port
, writingp
);
144 args
= SCM_CDR (args
);
147 scm_lfwrite (start
, p
- start
, port
);
148 scm_putc ('\n', port
);
152 display_expression (SCM frame
,SCM pname
,SCM source
,SCM port
)
154 SCM print_state
= scm_make_print_state ();
155 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
156 pstate
->writingp
= 0;
160 if (SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
))
163 && SCM_FRAMEP (frame
)
164 && SCM_FRAME_EVAL_ARGS_P (frame
))
165 scm_puts ("While evaluating arguments to ", port
);
167 scm_puts ("In procedure ", port
);
168 scm_iprin1 (pname
, port
, pstate
);
169 if (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
))
171 scm_puts (" in expression ", port
);
172 pstate
->writingp
= 1;
173 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
176 else if (SCM_NIMP (source
))
178 scm_puts ("In expression ", port
);
179 pstate
->writingp
= 1;
180 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
182 scm_puts (":\n", port
);
183 scm_free_print_state (print_state
);
186 struct display_error_args
{
196 display_error_body (struct display_error_args
*a
)
198 SCM current_frame
= SCM_BOOL_F
;
199 SCM source
= SCM_BOOL_F
;
200 SCM pname
= SCM_BOOL_F
;
201 SCM prev_frame
= SCM_BOOL_F
;
204 && SCM_NIMP (a
->stack
)
205 && SCM_STACKP (a
->stack
)
206 && SCM_STACK_LENGTH (a
->stack
) > 0)
208 current_frame
= scm_stack_ref (a
->stack
, SCM_INUM0
);
209 source
= SCM_FRAME_SOURCE (current_frame
);
210 prev_frame
= SCM_FRAME_PREV (current_frame
);
211 if (!(SCM_NIMP (source
) && SCM_MEMOIZEDP (source
))
212 && prev_frame
!= SCM_BOOL_F
)
213 source
= SCM_FRAME_SOURCE (prev_frame
);
214 if (SCM_FRAME_PROC_P (current_frame
)
215 && scm_procedure_p (SCM_FRAME_PROC (current_frame
)) == SCM_BOOL_T
)
216 pname
= scm_procedure_name (SCM_FRAME_PROC (current_frame
));
218 if (!(SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
)))
220 if ((SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
))
221 || (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)))
223 display_header (source
, a
->port
);
224 display_expression (current_frame
, pname
, source
, a
->port
);
226 display_header (source
, a
->port
);
227 scm_display_error_message (a
->message
, a
->args
, a
->port
);
228 return SCM_UNSPECIFIED
;
231 struct display_error_handler_data
{
236 /* This is the exception handler for error reporting routines.
237 Note that it is very important that this handler *doesn't* try to
238 print more than the error tag, since the error very probably is
239 caused by an erroneous print call-back routine. If we would
240 tru to print all objects, we would enter an infinite loop. */
242 display_error_handler (struct display_error_handler_data
*data
,
245 SCM print_state
= scm_make_print_state ();
246 scm_puts ("\nException during displaying of ", data
->port
);
247 scm_puts (data
->mode
, data
->port
);
248 scm_puts (": ", data
->port
);
249 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
250 scm_putc ('\n', data
->port
);
251 return SCM_UNSPECIFIED
;
254 GUILE_PROC(scm_display_error
, "display-error", 6, 0, 0,
255 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
257 #define FUNC_NAME s_scm_display_error
259 struct display_error_args a
;
260 struct display_error_handler_data data
;
269 scm_internal_catch (SCM_BOOL_T
,
270 (scm_catch_body_t
) display_error_body
, &a
,
271 (scm_catch_handler_t
) display_error_handler
, &data
);
272 return SCM_UNSPECIFIED
;
281 static int n_print_params
= 9;
282 static print_params_t default_print_params
[] = {
286 { 1, 4 }, { 1, 3 }, { 1, 2 }
288 static print_params_t
*print_params
= default_print_params
;
291 GUILE_PROC(set_print_params_x
, "set-print-params!", 1, 0, 0,
293 #define FUNC_NAME s_set_print_params_x
295 int i
, n
= scm_ilength (params
);
297 print_params_t
*new_params
;
298 SCM_ASSERT (n
>= 1, params
, SCM_ARG2
, FUNC_NAME
);
299 for (ls
= params
; SCM_NIMP (ls
); ls
= SCM_CDR (ls
))
300 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
301 && SCM_INUMP (SCM_CAAR (ls
))
302 && SCM_INUM (SCM_CAAR (ls
)) >= 0
303 && SCM_INUMP (SCM_CADAR (ls
))
304 && SCM_INUM (SCM_CADAR (ls
)) >= 0,
307 s_set_print_params_x
);
308 new_params
= scm_must_malloc (n
* sizeof (print_params_t
),
310 if (print_params
!= default_print_params
)
311 scm_must_free (print_params
);
312 print_params
= new_params
;
313 for (i
= 0; i
< n
; ++i
)
315 print_params
[i
].level
= SCM_INUM (SCM_CAAR (params
));
316 print_params
[i
].length
= SCM_INUM (SCM_CADAR (params
));
317 params
= SCM_CDR (params
);
320 return SCM_UNSPECIFIED
;
326 indent (int n
, SCM port
)
329 for (i
= 0; i
< n
; ++i
)
330 scm_putc (' ', port
);
334 display_frame_expr (char *hdr
,SCM exp
,char *tlr
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
338 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
341 pstate
->length
= print_params
[i
].length
;
342 ptob
->seek (sport
, 0, SEEK_SET
);
343 if (SCM_NIMP (exp
) && SCM_CONSP (exp
))
345 pstate
->level
= print_params
[i
].level
- 1;
346 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
347 scm_puts (&tlr
[1], sport
);
351 pstate
->level
= print_params
[i
].level
;
352 scm_iprin1 (exp
, sport
, pstate
);
355 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
358 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
359 ptob
->truncate (sport
, n
);
360 string
= scm_strport_to_string (sport
);
361 /* Remove control characters */
362 for (i
= 0; i
< n
; ++i
)
363 if (iscntrl (SCM_CHARS (string
)[i
]))
364 SCM_CHARS (string
)[i
] = ' ';
366 if (indentation
+ n
> SCM_BACKTRACE_WIDTH
)
368 n
= SCM_BACKTRACE_WIDTH
- indentation
;
369 SCM_CHARS (string
)[n
- 1] = '$';
372 scm_lfwrite (SCM_CHARS (string
), n
, port
);
376 display_application (SCM frame
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
378 SCM proc
= SCM_FRAME_PROC (frame
);
379 SCM name
= (SCM_NFALSEP (scm_procedure_p (proc
))
380 ? scm_procedure_name (proc
)
382 display_frame_expr ("[",
383 scm_cons (SCM_NFALSEP (name
) ? name
: proc
,
384 SCM_FRAME_ARGS (frame
)),
385 SCM_FRAME_EVAL_ARGS_P (frame
) ? " ..." : "]",
392 GUILE_PROC(scm_display_application
, "display-application", 1, 2, 0,
393 (SCM frame
, SCM port
, SCM indent
),
395 #define FUNC_NAME s_scm_display_application
397 SCM_ASSERT (SCM_NIMP (frame
) && SCM_FRAMEP (frame
),
398 frame
, SCM_ARG1
, s_display_application
);
399 if (SCM_UNBNDP (port
))
402 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
),
403 port
, SCM_ARG2
, s_display_application
);
404 if (SCM_UNBNDP (indent
))
407 SCM_ASSERT (SCM_INUMP (indent
), indent
, SCM_ARG3
, s_display_application
);
409 if (SCM_FRAME_PROC_P (frame
))
410 /* Display an application. */
412 SCM sport
, print_state
;
413 scm_print_state
*pstate
;
415 /* Create a string port used for adaptation of printing parameters. */
416 sport
= scm_mkstrport (SCM_INUM0
,
417 scm_make_string (SCM_MAKINUM (240),
422 /* Create a print state for printing of frames. */
423 print_state
= scm_make_print_state ();
424 pstate
= SCM_PRINT_STATE (print_state
);
425 pstate
->writingp
= 1;
428 display_application (frame
, SCM_INUM (indent
), sport
, port
, pstate
);
437 display_frame (SCM frame
,int nfield
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
441 /* Announce missing frames? */
442 if (!SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
444 indent (nfield
+ 1 + indentation
, port
);
445 scm_puts ("...\n", port
);
448 /* Check size of frame number. */
449 n
= SCM_FRAME_NUMBER (frame
);
450 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
452 /* Number indentation. */
453 indent (nfield
- (i
? i
: 1), port
);
456 scm_iprin1 (SCM_MAKINUM (n
), port
, pstate
);
458 /* Real frame marker */
459 scm_putc (SCM_FRAME_REAL_P (frame
) ? '*' : ' ', port
);
462 indent (indentation
, port
);
464 if (SCM_FRAME_PROC_P (frame
))
465 /* Display an application. */
466 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
468 /* Display a special form. */
470 SCM source
= SCM_FRAME_SOURCE (frame
);
471 SCM copy
= (SCM_NIMP (source
) && SCM_CONSP (source
)
472 ? scm_source_property (source
, scm_sym_copy
)
474 SCM umcopy
= (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)
475 ? scm_unmemoize (source
)
477 display_frame_expr ("(",
478 SCM_NIMP (copy
) && SCM_CONSP (copy
) ? copy
: umcopy
,
480 nfield
+ 1 + indentation
,
485 scm_putc ('\n', port
);
487 /* Announce missing frames? */
488 if (SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
490 indent (nfield
+ 1 + indentation
, port
);
491 scm_puts ("...\n", port
);
495 struct display_backtrace_args
{
503 display_backtrace_body(struct display_backtrace_args
*a
)
504 #define FUNC_NAME "display_backtrace_body"
506 int n_frames
, beg
, end
, n
, i
, j
;
507 int nfield
, indent_p
, indentation
;
508 SCM frame
, sport
, print_state
;
509 scm_print_state
*pstate
;
511 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
513 /* Argument checking and extraction. */
514 SCM_ASSERT (SCM_NIMP (a
->stack
) && SCM_STACKP (a
->stack
),
517 s_display_backtrace
);
518 SCM_ASSERT (SCM_NIMP (a
->port
) && SCM_OPOUTPORTP (a
->port
),
521 s_display_backtrace
);
522 n_frames
= SCM_INUM (scm_stack_length (a
->stack
));
523 n
= SCM_INUMP (a
->depth
) ? SCM_INUM (a
->depth
) : SCM_BACKTRACE_DEPTH
;
526 beg
= SCM_INUMP (a
->first
) ? SCM_INUM (a
->first
) : 0;
534 if (SCM_INUMP (a
->first
))
536 beg
= SCM_INUM (a
->first
);
550 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
551 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
553 /* Create a string port used for adaptation of printing parameters. */
554 sport
= scm_mkstrport (SCM_INUM0
,
555 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED
),
559 /* Create a print state for printing of frames. */
560 print_state
= scm_make_print_state ();
561 pstate
= SCM_PRINT_STATE (print_state
);
562 pstate
->writingp
= 1;
565 /* First find out if it's reasonable to do indentation. */
571 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
572 for (i
= 0, j
= 0; i
< n
; ++i
)
574 if (SCM_FRAME_REAL_P (frame
))
576 if (j
> SCM_BACKTRACE_INDENT
)
581 frame
= (SCM_BACKWARDS_P
582 ? SCM_FRAME_PREV (frame
)
583 : SCM_FRAME_NEXT (frame
));
587 /* Determine size of frame number field. */
588 j
= SCM_FRAME_NUMBER (scm_stack_ref (a
->stack
, SCM_MAKINUM (end
)));
589 for (i
= 0; j
> 0; ++i
) j
/= 10;
593 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
595 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
596 for (i
= 1; i
< n
; ++i
)
598 if (indent_p
&& SCM_FRAME_EVAL_ARGS_P (frame
))
600 frame
= SCM_BACKWARDS_P
? SCM_FRAME_PREV (frame
) : SCM_FRAME_NEXT (frame
);
601 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
604 return SCM_UNSPECIFIED
;
608 GUILE_PROC(scm_display_backtrace
, "display-backtrace", 2, 2, 0,
609 (SCM stack
, SCM port
, SCM first
, SCM depth
),
611 #define FUNC_NAME s_scm_display_backtrace
613 struct display_backtrace_args a
;
614 struct display_error_handler_data data
;
619 data
.mode
= "backtrace";
621 scm_internal_catch (SCM_BOOL_T
,
622 (scm_catch_body_t
) display_backtrace_body
, &a
,
623 (scm_catch_handler_t
) display_error_handler
, &data
);
624 return SCM_UNSPECIFIED
;
628 SCM_VCELL (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
630 GUILE_PROC(scm_backtrace
, "backtrace", 0, 0, 0,
633 #define FUNC_NAME s_scm_backtrace
635 SCM the_last_stack
= scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid
));
636 if (SCM_NFALSEP (the_last_stack
))
638 scm_newline (scm_cur_outp
);
639 scm_puts ("Backtrace:\n", scm_cur_outp
);
640 scm_display_backtrace (the_last_stack
,
644 scm_newline (scm_cur_outp
);
645 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var
))
648 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
650 "automatically if an error occurs in the future.\n",
652 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
657 scm_puts ("No backtrace available.\n", scm_cur_outp
);
659 return SCM_UNSPECIFIED
;
666 scm_init_backtrace ()
668 SCM f
= scm_make_fluid ();
669 scm_the_last_stack_fluid
= scm_sysintern ("the-last-stack", f
);
671 #include "backtrace.x"