1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009 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
28 #include "libguile/_scm.h"
37 #include "libguile/stacks.h"
38 #include "libguile/srcprop.h"
39 #include "libguile/struct.h"
40 #include "libguile/strports.h"
41 #include "libguile/throw.h"
42 #include "libguile/fluids.h"
43 #include "libguile/ports.h"
44 #include "libguile/strings.h"
45 #include "libguile/dynwind.h"
46 #include "libguile/frames.h"
48 #include "libguile/validate.h"
49 #include "libguile/lang.h"
50 #include "libguile/backtrace.h"
51 #include "libguile/filesys.h"
52 #include "libguile/private-options.h"
54 /* {Error reporting and backtraces}
56 * Note that these functions shouldn't generate errors themselves.
59 /* Print parameters for error messages. */
61 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
62 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
64 /* Print parameters for failing expressions in error messages.
65 * (See also `print_params' below for backtrace print parameters.)
68 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
69 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
72 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
76 SCM scm_the_last_stack_fluid_var
;
79 display_header (SCM source
, SCM port
)
81 scm_puts ("ERROR", port
);
82 scm_puts (": ", port
);
86 struct display_error_message_data
{
90 scm_print_state
*pstate
;
97 display_error_message (struct display_error_message_data
*d
)
99 if (scm_is_string (d
->message
) && scm_is_true (scm_list_p (d
->args
)))
100 scm_simple_format (d
->port
, d
->message
, d
->args
);
102 scm_display (d
->message
, d
->port
);
103 scm_newline (d
->port
);
104 return SCM_UNSPECIFIED
;
108 before_display_error_message (struct display_error_message_data
*d
)
110 scm_print_state
*pstate
= d
->pstate
;
111 d
->old_fancyp
= pstate
->fancyp
;
112 d
->old_level
= pstate
->level
;
113 d
->old_length
= pstate
->length
;
115 pstate
->level
= DISPLAY_ERROR_MESSAGE_MAX_LEVEL
;
116 pstate
->length
= DISPLAY_ERROR_MESSAGE_MAX_LENGTH
;
120 after_display_error_message (struct display_error_message_data
*d
)
122 scm_print_state
*pstate
= d
->pstate
;
123 pstate
->fancyp
= d
->old_fancyp
;
124 pstate
->level
= d
->old_level
;
125 pstate
->length
= d
->old_length
;
129 scm_display_error_message (SCM message
, SCM args
, SCM port
)
131 struct display_error_message_data d
;
133 scm_print_state
*pstate
;
135 port
= scm_i_port_with_print_state (port
, SCM_UNDEFINED
);
136 print_state
= SCM_PORT_WITH_PS_PS (port
);
137 pstate
= SCM_PRINT_STATE (print_state
);
143 scm_internal_dynamic_wind ((scm_t_guard
) before_display_error_message
,
144 (scm_t_inner
) display_error_message
,
145 (scm_t_guard
) after_display_error_message
,
151 display_expression (SCM frame
, SCM pname
, SCM source
, SCM port
)
153 SCM print_state
= scm_make_print_state ();
154 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
155 pstate
->writingp
= 0;
157 pstate
->level
= DISPLAY_EXPRESSION_MAX_LEVEL
;
158 pstate
->length
= DISPLAY_EXPRESSION_MAX_LENGTH
;
159 if (scm_is_symbol (pname
) || scm_is_string (pname
))
161 scm_puts ("In procedure ", port
);
162 scm_iprin1 (pname
, port
, pstate
);
164 scm_puts (":\n", port
);
165 scm_free_print_state (print_state
);
168 struct display_error_args
{
178 display_error_body (struct display_error_args
*a
)
180 SCM current_frame
= SCM_BOOL_F
;
181 SCM source
= SCM_BOOL_F
;
184 if (scm_is_symbol (pname
) || scm_is_string (pname
))
186 display_header (source
, a
->port
);
187 display_expression (current_frame
, pname
, source
, a
->port
);
189 display_header (source
, a
->port
);
190 scm_display_error_message (a
->message
, a
->args
, a
->port
);
191 return SCM_UNSPECIFIED
;
194 struct display_error_handler_data
{
199 /* This is the exception handler for error reporting routines.
200 Note that it is very important that this handler *doesn't* try to
201 print more than the error tag, since the error very probably is
202 caused by an erroneous print call-back routine. If we would
203 try to print all objects, we would enter an infinite loop. */
205 display_error_handler (struct display_error_handler_data
*data
,
206 SCM tag
, SCM args SCM_UNUSED
)
208 SCM print_state
= scm_make_print_state ();
209 scm_puts ("\nException during displaying of ", data
->port
);
210 scm_puts (data
->mode
, data
->port
);
211 scm_puts (": ", data
->port
);
212 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
213 scm_putc ('\n', data
->port
);
214 return SCM_UNSPECIFIED
;
218 /* The function scm_i_display_error prints out a detailed error message. This
219 * function will be called directly within libguile to signal error messages.
220 * No parameter checks will be performed by scm_i_display_error. Thus, User
221 * code should rather use the function scm_display_error.
224 scm_i_display_error (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
)
226 struct display_error_args a
;
227 struct display_error_handler_data data
;
236 scm_internal_catch (SCM_BOOL_T
,
237 (scm_t_catch_body
) display_error_body
, &a
,
238 (scm_t_catch_handler
) display_error_handler
, &data
);
242 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
243 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
244 "Display an error message to the output port @var{port}.\n"
245 "@var{stack} is the saved stack for the error, @var{subr} is\n"
246 "the name of the procedure in which the error occurred and\n"
247 "@var{message} is the actual error message, which may contain\n"
248 "formatting instructions. These will format the arguments in\n"
249 "the list @var{args} accordingly. @var{rest} is currently\n"
251 #define FUNC_NAME s_scm_display_error
253 SCM_VALIDATE_OUTPUT_PORT (2, port
);
255 scm_i_display_error (stack
, port
, subr
, message
, args
, rest
);
257 return SCM_UNSPECIFIED
;
267 static int n_print_params
= 9;
268 static print_params_t default_print_params
[] = {
272 { 1, 4 }, { 1, 3 }, { 1, 2 }
274 static print_params_t
*print_params
= default_print_params
;
277 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
279 "Set the print parameters to the values from @var{params}.\n"
280 "@var{params} must be a list of two-element lists which must\n"
281 "hold two integer values.")
282 #define FUNC_NAME s_scm_set_print_params_x
287 print_params_t
*new_params
;
289 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
290 for (ls
= params
; !SCM_NULL_OR_NIL_P (ls
); ls
= SCM_CDR (ls
))
291 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
292 && scm_is_unsigned_integer (SCM_CAAR (ls
), 0, INT_MAX
)
293 && scm_is_unsigned_integer (SCM_CADAR (ls
), 0, INT_MAX
),
296 s_scm_set_print_params_x
);
297 new_params
= scm_malloc (n
* sizeof (print_params_t
));
298 if (print_params
!= default_print_params
)
300 print_params
= new_params
;
301 for (i
= 0; i
< n
; ++i
)
303 print_params
[i
].level
= scm_to_int (SCM_CAAR (params
));
304 print_params
[i
].length
= scm_to_int (SCM_CADAR (params
));
305 params
= SCM_CDR (params
);
308 return SCM_UNSPECIFIED
;
314 indent (int n
, SCM port
)
317 for (i
= 0; i
< n
; ++i
)
318 scm_putc (' ', port
);
322 display_frame_expr (char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
325 scm_t_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
328 pstate
->length
= print_params
[i
].length
;
329 ptob
->seek (sport
, 0, SEEK_SET
);
330 if (scm_is_pair (exp
))
332 pstate
->level
= print_params
[i
].level
- 1;
333 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
334 scm_puts (&tlr
[1], sport
);
338 pstate
->level
= print_params
[i
].level
;
339 scm_iprin1 (exp
, sport
, pstate
);
342 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
345 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
346 ptob
->truncate (sport
, n
);
348 scm_display (scm_strport_to_string (sport
), port
);
352 display_application (SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
354 SCM proc
= scm_frame_procedure (frame
);
355 SCM name
= (scm_is_true (scm_procedure_p (proc
))
356 ? scm_procedure_name (proc
)
358 display_frame_expr ("[",
359 scm_cons (scm_is_true (name
) ? name
: proc
,
360 scm_frame_arguments (frame
)),
368 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
369 (SCM frame
, SCM port
, SCM indent
),
370 "Display a procedure application @var{frame} to the output port\n"
371 "@var{port}. @var{indent} specifies the indentation of the\n"
373 #define FUNC_NAME s_scm_display_application
375 SCM_VALIDATE_FRAME (1, frame
);
376 if (SCM_UNBNDP (port
))
377 port
= scm_current_output_port ();
379 SCM_VALIDATE_OPOUTPORT (2, port
);
380 if (SCM_UNBNDP (indent
))
383 /* Display an application. */
385 SCM sport
, print_state
;
386 scm_print_state
*pstate
;
388 /* Create a string port used for adaptation of printing parameters. */
389 sport
= scm_mkstrport (SCM_INUM0
,
390 scm_make_string (scm_from_int (240),
395 /* Create a print state for printing of frames. */
396 print_state
= scm_make_print_state ();
397 pstate
= SCM_PRINT_STATE (print_state
);
398 pstate
->writingp
= 1;
401 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
407 SCM_SYMBOL (sym_base
, "base");
410 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
412 SCM source
= scm_frame_source (frame
);
413 *file
= *line
= SCM_BOOL_F
;
414 if (scm_is_pair (source
)
415 && scm_is_pair (scm_cdr (source
))
416 && scm_is_pair (scm_cddr (source
))
417 && !scm_is_pair (scm_cdddr (source
)))
419 /* (addr . (filename . (line . column))), from vm compilation */
420 *file
= scm_cadr (source
);
421 *line
= scm_caddr (source
);
426 display_backtrace_file (frame
, last_file
, port
, pstate
)
430 scm_print_state
*pstate
;
434 display_backtrace_get_file_line (frame
, &file
, &line
);
436 if (scm_is_true (scm_equal_p (file
, *last_file
)))
441 scm_puts ("In ", port
);
442 if (scm_is_false (file
))
443 if (scm_is_false (line
))
444 scm_puts ("unknown file", port
);
446 scm_puts ("current input", port
);
449 pstate
->writingp
= 0;
450 scm_iprin1 (file
, port
, pstate
);
451 pstate
->writingp
= 1;
453 scm_puts (":\n", port
);
457 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
461 display_backtrace_get_file_line (frame
, &file
, &line
);
463 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
465 if (scm_is_false (file
))
467 if (scm_is_false (line
))
468 scm_putc ('?', port
);
470 scm_puts ("<stdin>", port
);
474 pstate
-> writingp
= 0;
476 scm_iprin1 ((scm_is_string (file
)?
477 scm_basename (file
, SCM_UNDEFINED
) : file
),
480 scm_iprin1 (file
, port
, pstate
);
482 pstate
-> writingp
= 1;
485 scm_putc (':', port
);
487 else if (scm_is_true (line
))
490 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
495 if (scm_is_false (line
))
496 scm_puts (" ?", port
);
498 scm_intprint (scm_to_int (line
) + 1, 10, port
);
499 scm_puts (": ", port
);
503 display_frame (SCM frame
, int n
, int nfield
, int indentation
,
504 SCM sport
, SCM port
, scm_print_state
*pstate
)
508 /* display file name and line number */
509 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
510 display_backtrace_file_and_line (frame
, port
, pstate
);
512 /* Check size of frame number. */
513 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
515 /* Number indentation. */
516 indent (nfield
- (i
? i
: 1), port
);
519 scm_iprin1 (scm_from_int (n
), port
, pstate
);
522 indent (indentation
, port
);
524 /* Display an application. */
525 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
526 scm_putc ('\n', port
);
529 struct display_backtrace_args
{
534 SCM highlight_objects
;
538 display_backtrace_body (struct display_backtrace_args
*a
)
539 #define FUNC_NAME "display_backtrace_body"
541 int n_frames
, beg
, end
, n
, i
, j
;
542 int nfield
, indent_p
, indentation
;
543 SCM frame
, sport
, print_state
;
545 scm_print_state
*pstate
;
547 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
549 /* Argument checking and extraction. */
550 SCM_VALIDATE_STACK (1, a
->stack
);
551 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
552 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
553 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
556 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
564 if (scm_is_integer (a
->first
))
566 beg
= scm_to_int (a
->first
);
580 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
581 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
583 /* Create a string port used for adaptation of printing parameters. */
584 sport
= scm_mkstrport (SCM_INUM0
,
585 scm_make_string (scm_from_int (240), SCM_UNDEFINED
),
589 /* Create a print state for printing of frames. */
590 print_state
= scm_make_print_state ();
591 pstate
= SCM_PRINT_STATE (print_state
);
592 pstate
->writingp
= 1;
594 pstate
->highlight_objects
= a
->highlight_objects
;
596 /* First find out if it's reasonable to do indentation. */
599 /* Determine size of frame number field. */
601 for (i
= 0; j
> 0; ++i
) j
/= 10;
606 last_file
= SCM_UNDEFINED
;
611 for (i
= beg
; i
!= end
; SCM_BACKWARDS_P
? ++i
: --i
)
613 frame
= scm_stack_ref (a
->stack
, scm_from_int (i
));
614 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
615 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
616 display_frame (frame
, i
, nfield
, indentation
, sport
, a
->port
, pstate
);
619 scm_remember_upto_here_1 (print_state
);
621 return SCM_UNSPECIFIED
;
625 SCM_DEFINE (scm_display_backtrace_with_highlights
, "display-backtrace", 2, 3, 0,
626 (SCM stack
, SCM port
, SCM first
, SCM depth
, SCM highlights
),
627 "Display a backtrace to the output port @var{port}. @var{stack}\n"
628 "is the stack to take the backtrace from, @var{first} specifies\n"
629 "where in the stack to start and @var{depth} how many frames\n"
630 "to display. @var{first} and @var{depth} can be @code{#f},\n"
631 "which means that default values will be used.\n"
632 "If @var{highlights} is given it should be a list; the elements\n"
633 "of this list will be highlighted wherever they appear in the\n"
635 #define FUNC_NAME s_scm_display_backtrace_with_highlights
637 struct display_backtrace_args a
;
638 struct display_error_handler_data data
;
643 if (SCM_UNBNDP (highlights
))
644 a
.highlight_objects
= SCM_EOL
;
646 a
.highlight_objects
= highlights
;
647 data
.mode
= "backtrace";
649 scm_internal_catch (SCM_BOOL_T
,
650 (scm_t_catch_body
) display_backtrace_body
, &a
,
651 (scm_t_catch_handler
) display_error_handler
, &data
);
652 return SCM_UNSPECIFIED
;
657 scm_display_backtrace (SCM stack
, SCM port
, SCM first
, SCM depth
)
659 return scm_display_backtrace_with_highlights (stack
, port
, first
, depth
,
663 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
665 SCM_DEFINE (scm_backtrace_with_highlights
, "backtrace", 0, 1, 0,
667 "Display a backtrace of the stack saved by the last error\n"
668 "to the current output port. If @var{highlights} is given\n"
669 "it should be a list; the elements of this list will be\n"
670 "highlighted wherever they appear in the backtrace.")
671 #define FUNC_NAME s_scm_backtrace_with_highlights
673 SCM port
= scm_current_output_port ();
675 scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var
));
677 if (SCM_UNBNDP (highlights
))
678 highlights
= SCM_EOL
;
680 if (scm_is_true (the_last_stack
))
683 scm_puts ("Backtrace:\n", port
);
684 scm_display_backtrace_with_highlights (the_last_stack
,
690 if (scm_is_false (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var
))
693 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
695 "automatically if an error occurs in the future.\n",
697 SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
702 scm_puts ("No backtrace available.\n", port
);
704 return SCM_UNSPECIFIED
;
711 return scm_backtrace_with_highlights (SCM_EOL
);
717 scm_init_backtrace ()
719 SCM f
= scm_make_fluid ();
720 scm_the_last_stack_fluid_var
= scm_c_define ("the-last-stack", f
);
722 #include "libguile/backtrace.x"