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 */
69 #include "backtrace.h"
71 /* {Error reporting and backtraces}
72 * (A first approximation.)
74 * Note that these functions shouldn't generate errors themselves.
79 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
84 SCM scm_the_last_stack_fluid
;
87 display_header (SCM source
, SCM port
)
89 SCM fname
= (SCM_MEMOIZEDP (source
)
90 ? scm_source_property (source
, scm_sym_filename
)
92 if (SCM_STRINGP (fname
))
94 scm_prin1 (fname
, port
, 0);
96 scm_intprint (SCM_INUM (scm_source_property (source
, scm_sym_line
)) + 1,
100 scm_intprint (SCM_INUM (scm_source_property (source
, scm_sym_column
)) + 1,
105 scm_puts ("ERROR", port
);
106 scm_puts (": ", port
);
111 scm_display_error_message (SCM message
, SCM args
, SCM port
)
113 if (SCM_ROSTRINGP (message
) && SCM_NFALSEP (scm_list_p (args
)))
115 scm_simple_format (port
, message
, args
);
120 scm_prin1 (message
, port
, 0);
121 scm_putc ('\n', port
);
126 display_expression (SCM frame
,SCM pname
,SCM source
,SCM port
)
128 SCM print_state
= scm_make_print_state ();
129 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
130 pstate
->writingp
= 0;
134 if (SCM_ROSTRINGP (pname
))
136 if (SCM_FRAMEP (frame
)
137 && SCM_FRAME_EVAL_ARGS_P (frame
))
138 scm_puts ("While evaluating arguments to ", port
);
140 scm_puts ("In procedure ", port
);
141 scm_iprin1 (pname
, port
, pstate
);
142 if (SCM_MEMOIZEDP (source
))
144 scm_puts (" in expression ", port
);
145 pstate
->writingp
= 1;
146 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
149 else if (SCM_NIMP (source
))
151 scm_puts ("In expression ", port
);
152 pstate
->writingp
= 1;
153 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
155 scm_puts (":\n", port
);
156 scm_free_print_state (print_state
);
159 struct display_error_args
{
169 display_error_body (struct display_error_args
*a
)
171 SCM current_frame
= SCM_BOOL_F
;
172 SCM source
= SCM_BOOL_F
;
173 SCM pname
= SCM_BOOL_F
;
174 SCM prev_frame
= SCM_BOOL_F
;
177 && SCM_STACKP (a
->stack
)
178 && SCM_STACK_LENGTH (a
->stack
) > 0)
180 current_frame
= scm_stack_ref (a
->stack
, SCM_INUM0
);
181 source
= SCM_FRAME_SOURCE (current_frame
);
182 prev_frame
= SCM_FRAME_PREV (current_frame
);
183 if (!SCM_MEMOIZEDP (source
)
184 && prev_frame
!= SCM_BOOL_F
)
185 source
= SCM_FRAME_SOURCE (prev_frame
);
186 if (SCM_FRAME_PROC_P (current_frame
)
187 && scm_procedure_p (SCM_FRAME_PROC (current_frame
)) == SCM_BOOL_T
)
188 pname
= scm_procedure_name (SCM_FRAME_PROC (current_frame
));
190 if (!SCM_ROSTRINGP (pname
))
192 if (SCM_ROSTRINGP (pname
) || SCM_MEMOIZEDP (source
))
194 display_header (source
, a
->port
);
195 display_expression (current_frame
, pname
, source
, a
->port
);
197 display_header (source
, a
->port
);
198 scm_display_error_message (a
->message
, a
->args
, a
->port
);
199 return SCM_UNSPECIFIED
;
202 struct display_error_handler_data
{
207 /* This is the exception handler for error reporting routines.
208 Note that it is very important that this handler *doesn't* try to
209 print more than the error tag, since the error very probably is
210 caused by an erroneous print call-back routine. If we would
211 try to print all objects, we would enter an infinite loop. */
213 display_error_handler (struct display_error_handler_data
*data
,
216 SCM print_state
= scm_make_print_state ();
217 scm_puts ("\nException during displaying of ", data
->port
);
218 scm_puts (data
->mode
, data
->port
);
219 scm_puts (": ", data
->port
);
220 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
221 scm_putc ('\n', data
->port
);
222 return SCM_UNSPECIFIED
;
225 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
226 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
228 #define FUNC_NAME s_scm_display_error
230 struct display_error_args a
;
231 struct display_error_handler_data data
;
240 scm_internal_catch (SCM_BOOL_T
,
241 (scm_catch_body_t
) display_error_body
, &a
,
242 (scm_catch_handler_t
) display_error_handler
, &data
);
243 return SCM_UNSPECIFIED
;
252 static int n_print_params
= 9;
253 static print_params_t default_print_params
[] = {
257 { 1, 4 }, { 1, 3 }, { 1, 2 }
259 static print_params_t
*print_params
= default_print_params
;
262 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
265 #define FUNC_NAME s_scm_set_print_params_x
270 print_params_t
*new_params
;
272 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
273 for (ls
= params
; SCM_NNULLP (ls
); ls
= SCM_CDR (ls
))
274 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
275 && SCM_INUMP (SCM_CAAR (ls
))
276 && SCM_INUM (SCM_CAAR (ls
)) >= 0
277 && SCM_INUMP (SCM_CADAR (ls
))
278 && SCM_INUM (SCM_CADAR (ls
)) >= 0,
281 s_scm_set_print_params_x
);
282 new_params
= scm_must_malloc (n
* sizeof (print_params_t
),
284 if (print_params
!= default_print_params
)
285 scm_must_free (print_params
);
286 print_params
= new_params
;
287 for (i
= 0; i
< n
; ++i
)
289 print_params
[i
].level
= SCM_INUM (SCM_CAAR (params
));
290 print_params
[i
].length
= SCM_INUM (SCM_CADAR (params
));
291 params
= SCM_CDR (params
);
294 return SCM_UNSPECIFIED
;
300 indent (int n
, SCM port
)
303 for (i
= 0; i
< n
; ++i
)
304 scm_putc (' ', port
);
308 display_frame_expr (char *hdr
,SCM exp
,char *tlr
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
312 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
315 pstate
->length
= print_params
[i
].length
;
316 ptob
->seek (sport
, 0, SEEK_SET
);
319 pstate
->level
= print_params
[i
].level
- 1;
320 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
321 scm_puts (&tlr
[1], sport
);
325 pstate
->level
= print_params
[i
].level
;
326 scm_iprin1 (exp
, sport
, pstate
);
329 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
332 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
333 ptob
->truncate (sport
, n
);
334 string
= scm_strport_to_string (sport
);
335 /* Remove control characters */
336 for (i
= 0; i
< n
; ++i
)
337 if (iscntrl (SCM_CHARS (string
)[i
]))
338 SCM_CHARS (string
)[i
] = ' ';
340 if (indentation
+ n
> SCM_BACKTRACE_WIDTH
)
342 n
= SCM_BACKTRACE_WIDTH
- indentation
;
343 SCM_CHARS (string
)[n
- 1] = '$';
346 scm_lfwrite (SCM_CHARS (string
), n
, port
);
350 display_application (SCM frame
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
352 SCM proc
= SCM_FRAME_PROC (frame
);
353 SCM name
= (SCM_NFALSEP (scm_procedure_p (proc
))
354 ? scm_procedure_name (proc
)
356 display_frame_expr ("[",
357 scm_cons (SCM_NFALSEP (name
) ? name
: proc
,
358 SCM_FRAME_ARGS (frame
)),
359 SCM_FRAME_EVAL_ARGS_P (frame
) ? " ..." : "]",
366 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
367 (SCM frame
, SCM port
, SCM indent
),
369 #define FUNC_NAME s_scm_display_application
371 SCM_VALIDATE_FRAME (1,frame
);
372 if (SCM_UNBNDP (port
))
375 SCM_VALIDATE_OPOUTPORT (2,port
);
376 if (SCM_UNBNDP (indent
))
379 SCM_VALIDATE_INUM (3,indent
);
381 if (SCM_FRAME_PROC_P (frame
))
382 /* Display an application. */
384 SCM sport
, print_state
;
385 scm_print_state
*pstate
;
387 /* Create a string port used for adaptation of printing parameters. */
388 sport
= scm_mkstrport (SCM_INUM0
,
389 scm_make_string (SCM_MAKINUM (240),
394 /* Create a print state for printing of frames. */
395 print_state
= scm_make_print_state ();
396 pstate
= SCM_PRINT_STATE (print_state
);
397 pstate
->writingp
= 1;
400 display_application (frame
, SCM_INUM (indent
), sport
, port
, pstate
);
409 display_frame (SCM frame
,int nfield
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
413 /* Announce missing frames? */
414 if (!SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
416 indent (nfield
+ 1 + indentation
, port
);
417 scm_puts ("...\n", port
);
420 /* Check size of frame number. */
421 n
= SCM_FRAME_NUMBER (frame
);
422 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
424 /* Number indentation. */
425 indent (nfield
- (i
? i
: 1), port
);
428 scm_iprin1 (SCM_MAKINUM (n
), port
, pstate
);
430 /* Real frame marker */
431 scm_putc (SCM_FRAME_REAL_P (frame
) ? '*' : ' ', port
);
434 indent (indentation
, port
);
436 if (SCM_FRAME_PROC_P (frame
))
437 /* Display an application. */
438 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
440 /* Display a special form. */
442 SCM source
= SCM_FRAME_SOURCE (frame
);
443 SCM copy
= (SCM_CONSP (source
)
444 ? scm_source_property (source
, scm_sym_copy
)
446 SCM umcopy
= (SCM_MEMOIZEDP (source
)
447 ? scm_unmemoize (source
)
449 display_frame_expr ("(",
450 SCM_CONSP (copy
) ? copy
: umcopy
,
452 nfield
+ 1 + indentation
,
457 scm_putc ('\n', port
);
459 /* Announce missing frames? */
460 if (SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
462 indent (nfield
+ 1 + indentation
, port
);
463 scm_puts ("...\n", port
);
467 struct display_backtrace_args
{
475 display_backtrace_body(struct display_backtrace_args
*a
)
476 #define FUNC_NAME "display_backtrace_body"
478 int n_frames
, beg
, end
, n
, i
, j
;
479 int nfield
, indent_p
, indentation
;
480 SCM frame
, sport
, print_state
;
481 scm_print_state
*pstate
;
483 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
485 /* Argument checking and extraction. */
486 SCM_ASSERT (SCM_STACKP (a
->stack
),
489 s_display_backtrace
);
490 SCM_ASSERT (SCM_OPOUTPORTP (a
->port
),
493 s_display_backtrace
);
494 n_frames
= SCM_INUM (scm_stack_length (a
->stack
));
495 n
= SCM_INUMP (a
->depth
) ? SCM_INUM (a
->depth
) : SCM_BACKTRACE_DEPTH
;
498 beg
= SCM_INUMP (a
->first
) ? SCM_INUM (a
->first
) : 0;
506 if (SCM_INUMP (a
->first
))
508 beg
= SCM_INUM (a
->first
);
522 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
523 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
525 /* Create a string port used for adaptation of printing parameters. */
526 sport
= scm_mkstrport (SCM_INUM0
,
527 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED
),
531 /* Create a print state for printing of frames. */
532 print_state
= scm_make_print_state ();
533 pstate
= SCM_PRINT_STATE (print_state
);
534 pstate
->writingp
= 1;
537 /* First find out if it's reasonable to do indentation. */
543 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
544 for (i
= 0, j
= 0; i
< n
; ++i
)
546 if (SCM_FRAME_REAL_P (frame
))
548 if (j
> SCM_BACKTRACE_INDENT
)
553 frame
= (SCM_BACKWARDS_P
554 ? SCM_FRAME_PREV (frame
)
555 : SCM_FRAME_NEXT (frame
));
559 /* Determine size of frame number field. */
560 j
= SCM_FRAME_NUMBER (scm_stack_ref (a
->stack
, SCM_MAKINUM (end
)));
561 for (i
= 0; j
> 0; ++i
) j
/= 10;
565 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
567 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
568 for (i
= 1; i
< n
; ++i
)
570 if (indent_p
&& SCM_FRAME_EVAL_ARGS_P (frame
))
572 frame
= SCM_BACKWARDS_P
? SCM_FRAME_PREV (frame
) : SCM_FRAME_NEXT (frame
);
573 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
576 return SCM_UNSPECIFIED
;
580 SCM_DEFINE (scm_display_backtrace
, "display-backtrace", 2, 2, 0,
581 (SCM stack
, SCM port
, SCM first
, SCM depth
),
583 #define FUNC_NAME s_scm_display_backtrace
585 struct display_backtrace_args a
;
586 struct display_error_handler_data data
;
591 data
.mode
= "backtrace";
593 scm_internal_catch (SCM_BOOL_T
,
594 (scm_catch_body_t
) display_backtrace_body
, &a
,
595 (scm_catch_handler_t
) display_error_handler
, &data
);
596 return SCM_UNSPECIFIED
;
600 SCM_VCELL (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
602 SCM_DEFINE (scm_backtrace
, "backtrace", 0, 0, 0,
605 #define FUNC_NAME s_scm_backtrace
607 SCM the_last_stack
= scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid
));
608 if (SCM_NFALSEP (the_last_stack
))
610 scm_newline (scm_cur_outp
);
611 scm_puts ("Backtrace:\n", scm_cur_outp
);
612 scm_display_backtrace (the_last_stack
,
616 scm_newline (scm_cur_outp
);
617 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var
))
620 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
622 "automatically if an error occurs in the future.\n",
624 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
629 scm_puts ("No backtrace available.\n", scm_cur_outp
);
631 return SCM_UNSPECIFIED
;
638 scm_init_backtrace ()
640 SCM f
= scm_make_fluid ();
641 scm_the_last_stack_fluid
= scm_sysintern ("the-last-stack", f
);
643 #include "backtrace.x"