1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004 Free Software Foundation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "libguile/_scm.h"
36 #include "libguile/stacks.h"
37 #include "libguile/srcprop.h"
38 #include "libguile/struct.h"
39 #include "libguile/strports.h"
40 #include "libguile/throw.h"
41 #include "libguile/fluids.h"
42 #include "libguile/ports.h"
43 #include "libguile/strings.h"
44 #include "libguile/dynwind.h"
46 #include "libguile/validate.h"
47 #include "libguile/lang.h"
48 #include "libguile/backtrace.h"
49 #include "libguile/filesys.h"
51 /* {Error reporting and backtraces}
53 * Note that these functions shouldn't generate errors themselves.
56 /* Print parameters for error messages. */
58 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
59 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
61 /* Print parameters for failing expressions in error messages.
62 * (See also `print_params' below for backtrace print parameters.)
65 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
66 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
69 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
73 SCM scm_the_last_stack_fluid_var
;
76 display_header (SCM source
, SCM port
)
78 if (SCM_MEMOIZEDP (source
))
80 SCM fname
= scm_source_property (source
, scm_sym_filename
);
81 SCM line
= scm_source_property (source
, scm_sym_line
);
82 SCM col
= scm_source_property (source
, scm_sym_column
);
84 /* Dirk:FIXME:: Maybe we should store the _port_ rather than the
85 * filename with the source properties? Then we could in case of
86 * non-file ports give at least some more details than just
87 * "<unnamed port>". */
88 if (scm_is_true (fname
))
89 scm_prin1 (fname
, port
, 0);
91 scm_puts ("<unnamed port>", port
);
93 if (scm_is_true (line
) && scm_is_true (col
))
96 scm_intprint (scm_to_long (line
) + 1, 10, port
);
98 scm_intprint (scm_to_long (col
) + 1, 10, port
);
102 scm_puts ("ERROR", port
);
103 scm_puts (": ", port
);
107 struct display_error_message_data
{
111 scm_print_state
*pstate
;
118 display_error_message (struct display_error_message_data
*d
)
120 if (scm_is_string (d
->message
) && scm_is_true (scm_list_p (d
->args
)))
121 scm_simple_format (d
->port
, d
->message
, d
->args
);
123 scm_display (d
->message
, d
->port
);
124 scm_newline (d
->port
);
125 return SCM_UNSPECIFIED
;
129 before_display_error_message (struct display_error_message_data
*d
)
131 scm_print_state
*pstate
= d
->pstate
;
132 d
->old_fancyp
= pstate
->fancyp
;
133 d
->old_level
= pstate
->level
;
134 d
->old_length
= pstate
->length
;
136 pstate
->level
= DISPLAY_ERROR_MESSAGE_MAX_LEVEL
;
137 pstate
->length
= DISPLAY_ERROR_MESSAGE_MAX_LENGTH
;
141 after_display_error_message (struct display_error_message_data
*d
)
143 scm_print_state
*pstate
= d
->pstate
;
144 pstate
->fancyp
= d
->old_fancyp
;
145 pstate
->level
= d
->old_level
;
146 pstate
->length
= d
->old_length
;
150 scm_display_error_message (SCM message
, SCM args
, SCM port
)
152 struct display_error_message_data d
;
154 scm_print_state
*pstate
;
156 port
= scm_i_port_with_print_state (port
, SCM_UNDEFINED
);
157 print_state
= SCM_PORT_WITH_PS_PS (port
);
158 pstate
= SCM_PRINT_STATE (print_state
);
164 scm_internal_dynamic_wind ((scm_t_guard
) before_display_error_message
,
165 (scm_t_inner
) display_error_message
,
166 (scm_t_guard
) after_display_error_message
,
172 display_expression (SCM frame
, SCM pname
, SCM source
, SCM port
)
174 SCM print_state
= scm_make_print_state ();
175 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
176 pstate
->writingp
= 0;
178 pstate
->level
= DISPLAY_EXPRESSION_MAX_LEVEL
;
179 pstate
->length
= DISPLAY_EXPRESSION_MAX_LENGTH
;
180 if (SCM_SYMBOLP (pname
) || scm_is_string (pname
))
182 if (SCM_FRAMEP (frame
)
183 && SCM_FRAME_EVAL_ARGS_P (frame
))
184 scm_puts ("While evaluating arguments to ", port
);
186 scm_puts ("In procedure ", port
);
187 scm_iprin1 (pname
, port
, pstate
);
188 if (SCM_MEMOIZEDP (source
))
190 scm_puts (" in expression ", port
);
191 pstate
->writingp
= 1;
192 scm_iprin1 (scm_i_unmemoize_expr (source
), port
, pstate
);
195 else if (SCM_MEMOIZEDP (source
))
197 scm_puts ("In expression ", port
);
198 pstate
->writingp
= 1;
199 scm_iprin1 (scm_i_unmemoize_expr (source
), port
, pstate
);
201 scm_puts (":\n", port
);
202 scm_free_print_state (print_state
);
205 struct display_error_args
{
215 display_error_body (struct display_error_args
*a
)
217 SCM current_frame
= SCM_BOOL_F
;
218 SCM source
= SCM_BOOL_F
;
219 SCM prev_frame
= SCM_BOOL_F
;
223 && SCM_STACKP (a
->stack
)
224 && SCM_STACK_LENGTH (a
->stack
) > 0)
226 current_frame
= scm_stack_ref (a
->stack
, SCM_INUM0
);
227 source
= SCM_FRAME_SOURCE (current_frame
);
228 prev_frame
= SCM_FRAME_PREV (current_frame
);
229 if (!SCM_MEMOIZEDP (source
) && scm_is_true (prev_frame
))
230 source
= SCM_FRAME_SOURCE (prev_frame
);
231 if (!SCM_SYMBOLP (pname
)
232 && !scm_is_string (pname
)
233 && SCM_FRAME_PROC_P (current_frame
)
234 && scm_is_true (scm_procedure_p (SCM_FRAME_PROC (current_frame
))))
235 pname
= scm_procedure_name (SCM_FRAME_PROC (current_frame
));
237 if (SCM_SYMBOLP (pname
) || scm_is_string (pname
) || SCM_MEMOIZEDP (source
))
239 display_header (source
, a
->port
);
240 display_expression (current_frame
, pname
, source
, a
->port
);
242 display_header (source
, a
->port
);
243 scm_display_error_message (a
->message
, a
->args
, a
->port
);
244 return SCM_UNSPECIFIED
;
247 struct display_error_handler_data
{
252 /* This is the exception handler for error reporting routines.
253 Note that it is very important that this handler *doesn't* try to
254 print more than the error tag, since the error very probably is
255 caused by an erroneous print call-back routine. If we would
256 try to print all objects, we would enter an infinite loop. */
258 display_error_handler (struct display_error_handler_data
*data
,
259 SCM tag
, SCM args SCM_UNUSED
)
261 SCM print_state
= scm_make_print_state ();
262 scm_puts ("\nException during displaying of ", data
->port
);
263 scm_puts (data
->mode
, data
->port
);
264 scm_puts (": ", data
->port
);
265 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
266 scm_putc ('\n', data
->port
);
267 return SCM_UNSPECIFIED
;
271 /* The function scm_i_display_error prints out a detailed error message. This
272 * function will be called directly within libguile to signal error messages.
273 * No parameter checks will be performed by scm_i_display_error. Thus, User
274 * code should rather use the function scm_display_error.
277 scm_i_display_error (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
)
279 struct display_error_args a
;
280 struct display_error_handler_data data
;
289 scm_internal_catch (SCM_BOOL_T
,
290 (scm_t_catch_body
) display_error_body
, &a
,
291 (scm_t_catch_handler
) display_error_handler
, &data
);
295 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
296 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
297 "Display an error message to the output port @var{port}.\n"
298 "@var{stack} is the saved stack for the error, @var{subr} is\n"
299 "the name of the procedure in which the error occurred and\n"
300 "@var{message} is the actual error message, which may contain\n"
301 "formatting instructions. These will format the arguments in\n"
302 "the list @var{args} accordingly. @var{rest} is currently\n"
304 #define FUNC_NAME s_scm_display_error
306 SCM_VALIDATE_OUTPUT_PORT (2, port
);
308 scm_i_display_error (stack
, port
, subr
, message
, args
, rest
);
310 return SCM_UNSPECIFIED
;
320 static int n_print_params
= 9;
321 static print_params_t default_print_params
[] = {
325 { 1, 4 }, { 1, 3 }, { 1, 2 }
327 static print_params_t
*print_params
= default_print_params
;
330 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
332 "Set the print parameters to the values from @var{params}.\n"
333 "@var{params} must be a list of two-element lists which must\n"
334 "hold two integer values.")
335 #define FUNC_NAME s_scm_set_print_params_x
340 print_params_t
*new_params
;
342 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
343 for (ls
= params
; !SCM_NULL_OR_NIL_P (ls
); ls
= SCM_CDR (ls
))
344 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
345 && scm_is_unsigned_integer (SCM_CAAR (ls
), 0, INT_MAX
)
346 && scm_is_unsigned_integer (SCM_CADAR (ls
), 0, INT_MAX
),
349 s_scm_set_print_params_x
);
350 new_params
= scm_malloc (n
* sizeof (print_params_t
));
351 if (print_params
!= default_print_params
)
353 print_params
= new_params
;
354 for (i
= 0; i
< n
; ++i
)
356 print_params
[i
].level
= scm_to_int (SCM_CAAR (params
));
357 print_params
[i
].length
= scm_to_int (SCM_CADAR (params
));
358 params
= SCM_CDR (params
);
361 return SCM_UNSPECIFIED
;
367 indent (int n
, SCM port
)
370 for (i
= 0; i
< n
; ++i
)
371 scm_putc (' ', port
);
375 display_frame_expr (char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
379 scm_t_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
382 pstate
->length
= print_params
[i
].length
;
383 ptob
->seek (sport
, 0, SEEK_SET
);
386 pstate
->level
= print_params
[i
].level
- 1;
387 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
388 scm_puts (&tlr
[1], sport
);
392 pstate
->level
= print_params
[i
].level
;
393 scm_iprin1 (exp
, sport
, pstate
);
396 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
399 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
400 ptob
->truncate (sport
, n
);
401 string
= scm_strport_to_string (sport
);
402 assert (scm_is_string (string
));
404 /* Remove control characters */
405 for (i
= 0; i
< n
; ++i
)
406 if (iscntrl ((int) SCM_I_STRING_UCHARS (string
)[i
]))
407 SCM_I_STRING_UCHARS (string
)[i
] = ' ';
409 if (indentation
+ n
> SCM_BACKTRACE_WIDTH
)
411 n
= SCM_BACKTRACE_WIDTH
- indentation
;
412 SCM_I_STRING_UCHARS (string
)[n
- 1] = '$';
415 scm_lfwrite (SCM_I_STRING_CHARS (string
), n
, port
);
416 scm_remember_upto_here_1 (string
);
420 display_application (SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
422 SCM proc
= SCM_FRAME_PROC (frame
);
423 SCM name
= (scm_is_true (scm_procedure_p (proc
))
424 ? scm_procedure_name (proc
)
426 display_frame_expr ("[",
427 scm_cons (scm_is_true (name
) ? name
: proc
,
428 SCM_FRAME_ARGS (frame
)),
429 SCM_FRAME_EVAL_ARGS_P (frame
) ? " ..." : "]",
436 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
437 (SCM frame
, SCM port
, SCM indent
),
438 "Display a procedure application @var{frame} to the output port\n"
439 "@var{port}. @var{indent} specifies the indentation of the\n"
441 #define FUNC_NAME s_scm_display_application
443 SCM_VALIDATE_FRAME (1, frame
);
444 if (SCM_UNBNDP (port
))
447 SCM_VALIDATE_OPOUTPORT (2, port
);
448 if (SCM_UNBNDP (indent
))
451 if (SCM_FRAME_PROC_P (frame
))
452 /* Display an application. */
454 SCM sport
, print_state
;
455 scm_print_state
*pstate
;
457 /* Create a string port used for adaptation of printing parameters. */
458 sport
= scm_mkstrport (SCM_INUM0
,
459 scm_make_string (scm_from_int (240),
464 /* Create a print state for printing of frames. */
465 print_state
= scm_make_print_state ();
466 pstate
= SCM_PRINT_STATE (print_state
);
467 pstate
->writingp
= 1;
470 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
478 SCM_SYMBOL (sym_base
, "base");
481 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
483 SCM source
= SCM_FRAME_SOURCE (frame
);
484 *file
= SCM_MEMOIZEDP (source
) ? scm_source_property (source
, scm_sym_filename
) : SCM_BOOL_F
;
485 *line
= (SCM_MEMOIZEDP (source
)) ? scm_source_property (source
, scm_sym_line
) : SCM_BOOL_F
;
489 display_backtrace_file (frame
, last_file
, port
, pstate
)
493 scm_print_state
*pstate
;
497 display_backtrace_get_file_line (frame
, &file
, &line
);
499 if (scm_is_eq (file
, *last_file
))
504 scm_puts ("In ", port
);
505 if (scm_is_false (file
))
506 if (scm_is_false (line
))
507 scm_puts ("unknown file", port
);
509 scm_puts ("current input", port
);
512 pstate
->writingp
= 0;
513 scm_iprin1 (file
, port
, pstate
);
514 pstate
->writingp
= 1;
516 scm_puts (":\n", port
);
520 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
524 display_backtrace_get_file_line (frame
, &file
, &line
);
526 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
528 if (scm_is_false (file
))
530 if (scm_is_false (line
))
531 scm_putc ('?', port
);
533 scm_puts ("<stdin>", port
);
537 pstate
-> writingp
= 0;
539 scm_iprin1 ((scm_is_string (file
)?
540 scm_basename (file
, SCM_UNDEFINED
) : file
),
543 scm_iprin1 (file
, port
, pstate
);
545 pstate
-> writingp
= 1;
548 scm_putc (':', port
);
550 else if (scm_is_true (line
))
553 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
558 if (scm_is_false (line
))
559 scm_puts (" ?", port
);
561 scm_intprint (scm_to_int (line
) + 1, 10, port
);
562 scm_puts (": ", port
);
566 display_frame (SCM frame
, int nfield
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
570 /* Announce missing frames? */
571 if (!SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
573 indent (nfield
+ 1 + indentation
, port
);
574 scm_puts ("...\n", port
);
577 /* display file name and line number */
578 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
579 display_backtrace_file_and_line (frame
, port
, pstate
);
581 /* Check size of frame number. */
582 n
= SCM_FRAME_NUMBER (frame
);
583 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
585 /* Number indentation. */
586 indent (nfield
- (i
? i
: 1), port
);
589 scm_iprin1 (scm_from_int (n
), port
, pstate
);
591 /* Real frame marker */
592 scm_putc (SCM_FRAME_REAL_P (frame
) ? '*' : ' ', port
);
595 indent (indentation
, port
);
597 if (SCM_FRAME_PROC_P (frame
))
598 /* Display an application. */
599 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
601 /* Display a special form. */
603 SCM source
= SCM_FRAME_SOURCE (frame
);
604 SCM copy
= (SCM_CONSP (source
)
605 ? scm_source_property (source
, scm_sym_copy
)
607 SCM umcopy
= (SCM_MEMOIZEDP (source
)
608 ? scm_i_unmemoize_expr (source
)
610 display_frame_expr ("(",
611 SCM_CONSP (copy
) ? copy
: umcopy
,
613 nfield
+ 1 + indentation
,
618 scm_putc ('\n', port
);
620 /* Announce missing frames? */
621 if (SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
623 indent (nfield
+ 1 + indentation
, port
);
624 scm_puts ("...\n", port
);
628 struct display_backtrace_args
{
636 display_backtrace_body (struct display_backtrace_args
*a
)
637 #define FUNC_NAME "display_backtrace_body"
639 int n_frames
, beg
, end
, n
, i
, j
;
640 int nfield
, indent_p
, indentation
;
641 SCM frame
, sport
, print_state
;
643 scm_print_state
*pstate
;
645 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
647 /* Argument checking and extraction. */
648 SCM_VALIDATE_STACK (1, a
->stack
);
649 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
650 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
651 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
654 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
662 if (scm_is_integer (a
->first
))
664 beg
= scm_to_int (a
->first
);
678 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
679 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
681 /* Create a string port used for adaptation of printing parameters. */
682 sport
= scm_mkstrport (SCM_INUM0
,
683 scm_make_string (scm_from_int (240), SCM_UNDEFINED
),
687 /* Create a print state for printing of frames. */
688 print_state
= scm_make_print_state ();
689 pstate
= SCM_PRINT_STATE (print_state
);
690 pstate
->writingp
= 1;
693 /* First find out if it's reasonable to do indentation. */
701 frame
= scm_stack_ref (a
->stack
, scm_from_int (beg
));
702 for (i
= 0, j
= 0; i
< n
; ++i
)
704 if (SCM_FRAME_REAL_P (frame
))
706 if (j
> SCM_BACKTRACE_INDENT
)
711 frame
= (SCM_BACKWARDS_P
712 ? SCM_FRAME_PREV (frame
)
713 : SCM_FRAME_NEXT (frame
));
717 /* Determine size of frame number field. */
718 j
= SCM_FRAME_NUMBER (scm_stack_ref (a
->stack
, scm_from_int (end
)));
719 for (i
= 0; j
> 0; ++i
) j
/= 10;
723 frame
= scm_stack_ref (a
->stack
, scm_from_int (beg
));
725 last_file
= SCM_UNDEFINED
;
726 for (i
= 0; i
< n
; ++i
)
728 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
729 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
731 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
732 if (indent_p
&& SCM_FRAME_EVAL_ARGS_P (frame
))
734 frame
= (SCM_BACKWARDS_P
?
735 SCM_FRAME_PREV (frame
) : SCM_FRAME_NEXT (frame
));
738 scm_remember_upto_here_1 (print_state
);
740 return SCM_UNSPECIFIED
;
744 SCM_DEFINE (scm_display_backtrace
, "display-backtrace", 2, 2, 0,
745 (SCM stack
, SCM port
, SCM first
, SCM depth
),
746 "Display a backtrace to the output port @var{port}. @var{stack}\n"
747 "is the stack to take the backtrace from, @var{first} specifies\n"
748 "where in the stack to start and @var{depth} how much frames\n"
749 "to display. Both @var{first} and @var{depth} can be @code{#f},\n"
750 "which means that default values will be used.")
751 #define FUNC_NAME s_scm_display_backtrace
753 struct display_backtrace_args a
;
754 struct display_error_handler_data data
;
759 data
.mode
= "backtrace";
761 scm_internal_catch (SCM_BOOL_T
,
762 (scm_t_catch_body
) display_backtrace_body
, &a
,
763 (scm_t_catch_handler
) display_error_handler
, &data
);
764 return SCM_UNSPECIFIED
;
768 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
770 SCM_DEFINE (scm_backtrace
, "backtrace", 0, 0, 0,
772 "Display a backtrace of the stack saved by the last error\n"
773 "to the current output port.")
774 #define FUNC_NAME s_scm_backtrace
777 scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var
));
778 if (scm_is_true (the_last_stack
))
780 scm_newline (scm_cur_outp
);
781 scm_puts ("Backtrace:\n", scm_cur_outp
);
782 scm_display_backtrace (the_last_stack
,
786 scm_newline (scm_cur_outp
);
787 if (scm_is_false (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var
))
790 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
792 "automatically if an error occurs in the future.\n",
794 SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
799 scm_puts ("No backtrace available.\n", scm_cur_outp
);
801 return SCM_UNSPECIFIED
;
808 scm_init_backtrace ()
810 SCM f
= scm_make_fluid ();
811 scm_the_last_stack_fluid_var
= scm_c_define ("the-last-stack", f
);
813 #include "libguile/backtrace.x"