1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010 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 License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
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"
45 #include "libguile/frames.h"
47 #include "libguile/validate.h"
48 #include "libguile/backtrace.h"
49 #include "libguile/filesys.h"
50 #include "libguile/private-options.h"
52 /* {Error reporting and backtraces}
54 * Note that these functions shouldn't generate errors themselves.
57 /* Print parameters for error messages. */
59 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
60 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
62 /* Print parameters for failing expressions in error messages.
63 * (See also `print_params' below for backtrace print parameters.)
66 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
67 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
70 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
75 display_header (SCM source
, SCM port
)
77 scm_puts ("ERROR", port
);
78 scm_puts (": ", port
);
82 struct display_error_message_data
{
86 scm_print_state
*pstate
;
93 display_error_message (struct display_error_message_data
*d
)
95 if (scm_is_string (d
->message
) && scm_is_true (scm_list_p (d
->args
)))
96 scm_simple_format (d
->port
, d
->message
, d
->args
);
98 scm_display (d
->message
, d
->port
);
99 scm_newline (d
->port
);
100 return SCM_UNSPECIFIED
;
104 before_display_error_message (struct display_error_message_data
*d
)
106 scm_print_state
*pstate
= d
->pstate
;
107 d
->old_fancyp
= pstate
->fancyp
;
108 d
->old_level
= pstate
->level
;
109 d
->old_length
= pstate
->length
;
111 pstate
->level
= DISPLAY_ERROR_MESSAGE_MAX_LEVEL
;
112 pstate
->length
= DISPLAY_ERROR_MESSAGE_MAX_LENGTH
;
116 after_display_error_message (struct display_error_message_data
*d
)
118 scm_print_state
*pstate
= d
->pstate
;
119 pstate
->fancyp
= d
->old_fancyp
;
120 pstate
->level
= d
->old_level
;
121 pstate
->length
= d
->old_length
;
125 scm_display_error_message (SCM message
, SCM args
, SCM port
)
127 struct display_error_message_data d
;
129 scm_print_state
*pstate
;
131 port
= scm_i_port_with_print_state (port
, SCM_UNDEFINED
);
132 print_state
= SCM_PORT_WITH_PS_PS (port
);
133 pstate
= SCM_PRINT_STATE (print_state
);
139 scm_internal_dynamic_wind ((scm_t_guard
) before_display_error_message
,
140 (scm_t_inner
) display_error_message
,
141 (scm_t_guard
) after_display_error_message
,
147 display_expression (SCM frame
, SCM pname
, SCM source
, SCM port
)
149 SCM print_state
= scm_make_print_state ();
150 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
151 pstate
->writingp
= 0;
153 pstate
->level
= DISPLAY_EXPRESSION_MAX_LEVEL
;
154 pstate
->length
= DISPLAY_EXPRESSION_MAX_LENGTH
;
155 if (scm_is_symbol (pname
) || scm_is_string (pname
))
157 scm_puts ("In procedure ", port
);
158 scm_iprin1 (pname
, port
, pstate
);
160 scm_puts (":\n", port
);
161 scm_free_print_state (print_state
);
164 struct display_error_args
{
174 display_error_body (struct display_error_args
*a
)
176 SCM current_frame
= SCM_BOOL_F
;
177 SCM source
= SCM_BOOL_F
;
180 if (scm_is_symbol (pname
) || scm_is_string (pname
))
182 display_header (source
, a
->port
);
183 display_expression (current_frame
, pname
, source
, a
->port
);
185 display_header (source
, a
->port
);
186 scm_display_error_message (a
->message
, a
->args
, a
->port
);
187 return SCM_UNSPECIFIED
;
190 struct display_error_handler_data
{
195 /* This is the exception handler for error reporting routines.
196 Note that it is very important that this handler *doesn't* try to
197 print more than the error tag, since the error very probably is
198 caused by an erroneous print call-back routine. If we would
199 try to print all objects, we would enter an infinite loop. */
201 display_error_handler (struct display_error_handler_data
*data
,
202 SCM tag
, SCM args SCM_UNUSED
)
204 SCM print_state
= scm_make_print_state ();
205 scm_puts ("\nException during displaying of ", data
->port
);
206 scm_puts (data
->mode
, data
->port
);
207 scm_puts (": ", data
->port
);
208 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
209 scm_putc ('\n', data
->port
);
210 return SCM_UNSPECIFIED
;
214 /* The function scm_i_display_error prints out a detailed error message. This
215 * function will be called directly within libguile to signal error messages.
216 * No parameter checks will be performed by scm_i_display_error. Thus, User
217 * code should rather use the function scm_display_error.
220 scm_i_display_error (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
)
222 struct display_error_args a
;
223 struct display_error_handler_data data
;
232 scm_internal_catch (SCM_BOOL_T
,
233 (scm_t_catch_body
) display_error_body
, &a
,
234 (scm_t_catch_handler
) display_error_handler
, &data
);
238 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
239 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
240 "Display an error message to the output port @var{port}.\n"
241 "@var{stack} is the saved stack for the error, @var{subr} is\n"
242 "the name of the procedure in which the error occurred and\n"
243 "@var{message} is the actual error message, which may contain\n"
244 "formatting instructions. These will format the arguments in\n"
245 "the list @var{args} accordingly. @var{rest} is currently\n"
247 #define FUNC_NAME s_scm_display_error
249 SCM_VALIDATE_OUTPUT_PORT (2, port
);
251 scm_i_display_error (stack
, port
, subr
, message
, args
, rest
);
253 return SCM_UNSPECIFIED
;
263 static int n_print_params
= 9;
264 static print_params_t default_print_params
[] = {
268 { 1, 4 }, { 1, 3 }, { 1, 2 }
270 static print_params_t
*print_params
= default_print_params
;
273 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
275 "Set the print parameters to the values from @var{params}.\n"
276 "@var{params} must be a list of two-element lists which must\n"
277 "hold two integer values.")
278 #define FUNC_NAME s_scm_set_print_params_x
283 print_params_t
*new_params
;
285 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
286 for (ls
= params
; !SCM_NULL_OR_NIL_P (ls
); ls
= SCM_CDR (ls
))
287 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
288 && scm_is_unsigned_integer (SCM_CAAR (ls
), 0, INT_MAX
)
289 && scm_is_unsigned_integer (SCM_CADAR (ls
), 0, INT_MAX
),
292 s_scm_set_print_params_x
);
293 new_params
= scm_malloc (n
* sizeof (print_params_t
));
294 if (print_params
!= default_print_params
)
296 print_params
= new_params
;
297 for (i
= 0; i
< n
; ++i
)
299 print_params
[i
].level
= scm_to_int (SCM_CAAR (params
));
300 print_params
[i
].length
= scm_to_int (SCM_CADAR (params
));
301 params
= SCM_CDR (params
);
304 return SCM_UNSPECIFIED
;
310 indent (int n
, SCM port
)
313 for (i
= 0; i
< n
; ++i
)
314 scm_putc (' ', port
);
318 display_frame_expr (char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
321 scm_t_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
324 pstate
->length
= print_params
[i
].length
;
325 ptob
->seek (sport
, 0, SEEK_SET
);
326 if (scm_is_pair (exp
))
328 pstate
->level
= print_params
[i
].level
- 1;
329 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
330 scm_puts (&tlr
[1], sport
);
334 pstate
->level
= print_params
[i
].level
;
335 scm_iprin1 (exp
, sport
, pstate
);
338 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
341 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
342 ptob
->truncate (sport
, n
);
344 scm_display (scm_strport_to_string (sport
), port
);
348 display_application (SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
350 SCM proc
= scm_frame_procedure (frame
);
351 SCM name
= (scm_is_true (scm_procedure_p (proc
))
352 ? scm_procedure_name (proc
)
354 display_frame_expr ("[",
355 scm_cons (scm_is_true (name
) ? name
: proc
,
356 scm_frame_arguments (frame
)),
364 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
365 (SCM frame
, SCM port
, SCM indent
),
366 "Display a procedure application @var{frame} to the output port\n"
367 "@var{port}. @var{indent} specifies the indentation of the\n"
369 #define FUNC_NAME s_scm_display_application
371 SCM_VALIDATE_FRAME (1, frame
);
372 if (SCM_UNBNDP (port
))
373 port
= scm_current_output_port ();
375 SCM_VALIDATE_OPOUTPORT (2, port
);
376 if (SCM_UNBNDP (indent
))
379 /* Display an application. */
381 SCM sport
, print_state
;
382 scm_print_state
*pstate
;
384 /* Create a string port used for adaptation of printing parameters. */
385 sport
= scm_mkstrport (SCM_INUM0
,
386 scm_make_string (scm_from_int (240),
391 /* Create a print state for printing of frames. */
392 print_state
= scm_make_print_state ();
393 pstate
= SCM_PRINT_STATE (print_state
);
394 pstate
->writingp
= 1;
397 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
403 SCM_SYMBOL (sym_base
, "base");
406 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
408 SCM source
= scm_frame_source (frame
);
409 *file
= *line
= SCM_BOOL_F
;
410 if (scm_is_pair (source
)
411 && scm_is_pair (scm_cdr (source
))
412 && scm_is_pair (scm_cddr (source
))
413 && !scm_is_pair (scm_cdddr (source
)))
415 /* (addr . (filename . (line . column))), from vm compilation */
416 *file
= scm_cadr (source
);
417 *line
= scm_caddr (source
);
422 display_backtrace_file (frame
, last_file
, port
, pstate
)
426 scm_print_state
*pstate
;
430 display_backtrace_get_file_line (frame
, &file
, &line
);
432 if (scm_is_true (scm_equal_p (file
, *last_file
)))
437 scm_puts ("In ", port
);
438 if (scm_is_false (file
))
439 if (scm_is_false (line
))
440 scm_puts ("unknown file", port
);
442 scm_puts ("current input", port
);
445 pstate
->writingp
= 0;
446 scm_iprin1 (file
, port
, pstate
);
447 pstate
->writingp
= 1;
449 scm_puts (":\n", port
);
453 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
457 display_backtrace_get_file_line (frame
, &file
, &line
);
459 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
461 if (scm_is_false (file
))
463 if (scm_is_false (line
))
464 scm_putc ('?', port
);
466 scm_puts ("<stdin>", port
);
470 pstate
-> writingp
= 0;
472 scm_iprin1 ((scm_is_string (file
)?
473 scm_basename (file
, SCM_UNDEFINED
) : file
),
476 scm_iprin1 (file
, port
, pstate
);
478 pstate
-> writingp
= 1;
481 scm_putc (':', port
);
483 else if (scm_is_true (line
))
486 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
491 if (scm_is_false (line
))
492 scm_puts (" ?", port
);
494 scm_intprint (scm_to_int (line
) + 1, 10, port
);
495 scm_puts (": ", port
);
499 display_frame (SCM frame
, int n
, int nfield
, int indentation
,
500 SCM sport
, SCM port
, scm_print_state
*pstate
)
504 /* display file name and line number */
505 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
506 display_backtrace_file_and_line (frame
, port
, pstate
);
508 /* Check size of frame number. */
509 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
511 /* Number indentation. */
512 indent (nfield
- (i
? i
: 1), port
);
515 scm_iprin1 (scm_from_int (n
), port
, pstate
);
518 indent (indentation
, port
);
520 /* Display an application. */
521 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
522 scm_putc ('\n', port
);
525 struct display_backtrace_args
{
530 SCM highlight_objects
;
534 display_backtrace_body (struct display_backtrace_args
*a
)
535 #define FUNC_NAME "display_backtrace_body"
537 int n_frames
, beg
, end
, n
, i
, j
;
538 int nfield
, indent_p
, indentation
;
539 SCM frame
, sport
, print_state
;
541 scm_print_state
*pstate
;
543 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
545 /* Argument checking and extraction. */
546 SCM_VALIDATE_STACK (1, a
->stack
);
547 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
548 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
549 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
552 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
560 if (scm_is_integer (a
->first
))
562 beg
= scm_to_int (a
->first
);
576 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
577 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
579 /* Create a string port used for adaptation of printing parameters. */
580 sport
= scm_mkstrport (SCM_INUM0
,
581 scm_make_string (scm_from_int (240), SCM_UNDEFINED
),
585 /* Create a print state for printing of frames. */
586 print_state
= scm_make_print_state ();
587 pstate
= SCM_PRINT_STATE (print_state
);
588 pstate
->writingp
= 1;
590 pstate
->highlight_objects
= a
->highlight_objects
;
592 /* First find out if it's reasonable to do indentation. */
595 /* Determine size of frame number field. */
597 for (i
= 0; j
> 0; ++i
) j
/= 10;
602 last_file
= SCM_UNDEFINED
;
607 for (i
= beg
; i
!= end
; SCM_BACKWARDS_P
? ++i
: --i
)
609 frame
= scm_stack_ref (a
->stack
, scm_from_int (i
));
610 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
611 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
612 display_frame (frame
, i
, nfield
, indentation
, sport
, a
->port
, pstate
);
615 scm_remember_upto_here_1 (print_state
);
617 return SCM_UNSPECIFIED
;
621 SCM_DEFINE (scm_display_backtrace_with_highlights
, "display-backtrace", 2, 3, 0,
622 (SCM stack
, SCM port
, SCM first
, SCM depth
, SCM highlights
),
623 "Display a backtrace to the output port @var{port}. @var{stack}\n"
624 "is the stack to take the backtrace from, @var{first} specifies\n"
625 "where in the stack to start and @var{depth} how many frames\n"
626 "to display. @var{first} and @var{depth} can be @code{#f},\n"
627 "which means that default values will be used.\n"
628 "If @var{highlights} is given it should be a list; the elements\n"
629 "of this list will be highlighted wherever they appear in the\n"
631 #define FUNC_NAME s_scm_display_backtrace_with_highlights
633 struct display_backtrace_args a
;
634 struct display_error_handler_data data
;
639 if (SCM_UNBNDP (highlights
))
640 a
.highlight_objects
= SCM_EOL
;
642 a
.highlight_objects
= highlights
;
643 data
.mode
= "backtrace";
645 scm_internal_catch (SCM_BOOL_T
,
646 (scm_t_catch_body
) display_backtrace_body
, &a
,
647 (scm_t_catch_handler
) display_error_handler
, &data
);
648 return SCM_UNSPECIFIED
;
653 scm_display_backtrace (SCM stack
, SCM port
, SCM first
, SCM depth
)
655 return scm_display_backtrace_with_highlights (stack
, port
, first
, depth
,
659 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
661 SCM_DEFINE (scm_backtrace_with_highlights
, "backtrace", 0, 1, 0,
663 "Display a backtrace of the current stack to the current\n"
664 "output port. If @var{highlights} is given, it should be\n"
665 "a list; the elements of this list will be highlighted\n"
666 "wherever they appear in the backtrace.")
667 #define FUNC_NAME s_scm_backtrace_with_highlights
669 SCM port
= scm_current_output_port ();
670 SCM stack
= scm_make_stack (SCM_BOOL_T
, SCM_EOL
);
672 if (SCM_UNBNDP (highlights
))
673 highlights
= SCM_EOL
;
676 scm_puts ("Backtrace:\n", port
);
677 scm_display_backtrace_with_highlights (stack
, port
, SCM_BOOL_F
, SCM_BOOL_F
,
681 return SCM_UNSPECIFIED
;
688 return scm_backtrace_with_highlights (SCM_EOL
);
694 scm_init_backtrace ()
696 #include "libguile/backtrace.x"