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) \
74 SCM scm_the_last_stack_fluid_var
;
77 display_header (SCM source
, SCM port
)
79 scm_puts ("ERROR", port
);
80 scm_puts (": ", port
);
84 struct display_error_message_data
{
88 scm_print_state
*pstate
;
95 display_error_message (struct display_error_message_data
*d
)
97 if (scm_is_string (d
->message
) && scm_is_true (scm_list_p (d
->args
)))
98 scm_simple_format (d
->port
, d
->message
, d
->args
);
100 scm_display (d
->message
, d
->port
);
101 scm_newline (d
->port
);
102 return SCM_UNSPECIFIED
;
106 before_display_error_message (struct display_error_message_data
*d
)
108 scm_print_state
*pstate
= d
->pstate
;
109 d
->old_fancyp
= pstate
->fancyp
;
110 d
->old_level
= pstate
->level
;
111 d
->old_length
= pstate
->length
;
113 pstate
->level
= DISPLAY_ERROR_MESSAGE_MAX_LEVEL
;
114 pstate
->length
= DISPLAY_ERROR_MESSAGE_MAX_LENGTH
;
118 after_display_error_message (struct display_error_message_data
*d
)
120 scm_print_state
*pstate
= d
->pstate
;
121 pstate
->fancyp
= d
->old_fancyp
;
122 pstate
->level
= d
->old_level
;
123 pstate
->length
= d
->old_length
;
127 scm_display_error_message (SCM message
, SCM args
, SCM port
)
129 struct display_error_message_data d
;
131 scm_print_state
*pstate
;
133 port
= scm_i_port_with_print_state (port
, SCM_UNDEFINED
);
134 print_state
= SCM_PORT_WITH_PS_PS (port
);
135 pstate
= SCM_PRINT_STATE (print_state
);
141 scm_internal_dynamic_wind ((scm_t_guard
) before_display_error_message
,
142 (scm_t_inner
) display_error_message
,
143 (scm_t_guard
) after_display_error_message
,
149 display_expression (SCM frame
, SCM pname
, SCM source
, SCM port
)
151 SCM print_state
= scm_make_print_state ();
152 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
153 pstate
->writingp
= 0;
155 pstate
->level
= DISPLAY_EXPRESSION_MAX_LEVEL
;
156 pstate
->length
= DISPLAY_EXPRESSION_MAX_LENGTH
;
157 if (scm_is_symbol (pname
) || scm_is_string (pname
))
159 scm_puts ("In procedure ", port
);
160 scm_iprin1 (pname
, port
, pstate
);
162 scm_puts (":\n", port
);
163 scm_free_print_state (print_state
);
166 struct display_error_args
{
176 display_error_body (struct display_error_args
*a
)
178 SCM current_frame
= SCM_BOOL_F
;
179 SCM source
= SCM_BOOL_F
;
182 if (scm_is_symbol (pname
) || scm_is_string (pname
))
184 display_header (source
, a
->port
);
185 display_expression (current_frame
, pname
, source
, a
->port
);
187 display_header (source
, a
->port
);
188 scm_display_error_message (a
->message
, a
->args
, a
->port
);
189 return SCM_UNSPECIFIED
;
192 struct display_error_handler_data
{
197 /* This is the exception handler for error reporting routines.
198 Note that it is very important that this handler *doesn't* try to
199 print more than the error tag, since the error very probably is
200 caused by an erroneous print call-back routine. If we would
201 try to print all objects, we would enter an infinite loop. */
203 display_error_handler (struct display_error_handler_data
*data
,
204 SCM tag
, SCM args SCM_UNUSED
)
206 SCM print_state
= scm_make_print_state ();
207 scm_puts ("\nException during displaying of ", data
->port
);
208 scm_puts (data
->mode
, data
->port
);
209 scm_puts (": ", data
->port
);
210 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
211 scm_putc ('\n', data
->port
);
212 return SCM_UNSPECIFIED
;
216 /* The function scm_i_display_error prints out a detailed error message. This
217 * function will be called directly within libguile to signal error messages.
218 * No parameter checks will be performed by scm_i_display_error. Thus, User
219 * code should rather use the function scm_display_error.
222 scm_i_display_error (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
)
224 struct display_error_args a
;
225 struct display_error_handler_data data
;
234 scm_internal_catch (SCM_BOOL_T
,
235 (scm_t_catch_body
) display_error_body
, &a
,
236 (scm_t_catch_handler
) display_error_handler
, &data
);
240 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
241 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
242 "Display an error message to the output port @var{port}.\n"
243 "@var{stack} is the saved stack for the error, @var{subr} is\n"
244 "the name of the procedure in which the error occurred and\n"
245 "@var{message} is the actual error message, which may contain\n"
246 "formatting instructions. These will format the arguments in\n"
247 "the list @var{args} accordingly. @var{rest} is currently\n"
249 #define FUNC_NAME s_scm_display_error
251 SCM_VALIDATE_OUTPUT_PORT (2, port
);
253 scm_i_display_error (stack
, port
, subr
, message
, args
, rest
);
255 return SCM_UNSPECIFIED
;
265 static int n_print_params
= 9;
266 static print_params_t default_print_params
[] = {
270 { 1, 4 }, { 1, 3 }, { 1, 2 }
272 static print_params_t
*print_params
= default_print_params
;
275 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
277 "Set the print parameters to the values from @var{params}.\n"
278 "@var{params} must be a list of two-element lists which must\n"
279 "hold two integer values.")
280 #define FUNC_NAME s_scm_set_print_params_x
285 print_params_t
*new_params
;
287 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
288 for (ls
= params
; !SCM_NULL_OR_NIL_P (ls
); ls
= SCM_CDR (ls
))
289 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
290 && scm_is_unsigned_integer (SCM_CAAR (ls
), 0, INT_MAX
)
291 && scm_is_unsigned_integer (SCM_CADAR (ls
), 0, INT_MAX
),
294 s_scm_set_print_params_x
);
295 new_params
= scm_malloc (n
* sizeof (print_params_t
));
296 if (print_params
!= default_print_params
)
298 print_params
= new_params
;
299 for (i
= 0; i
< n
; ++i
)
301 print_params
[i
].level
= scm_to_int (SCM_CAAR (params
));
302 print_params
[i
].length
= scm_to_int (SCM_CADAR (params
));
303 params
= SCM_CDR (params
);
306 return SCM_UNSPECIFIED
;
312 indent (int n
, SCM port
)
315 for (i
= 0; i
< n
; ++i
)
316 scm_putc (' ', port
);
320 display_frame_expr (char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
323 scm_t_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
326 pstate
->length
= print_params
[i
].length
;
327 ptob
->seek (sport
, 0, SEEK_SET
);
328 if (scm_is_pair (exp
))
330 pstate
->level
= print_params
[i
].level
- 1;
331 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
332 scm_puts (&tlr
[1], sport
);
336 pstate
->level
= print_params
[i
].level
;
337 scm_iprin1 (exp
, sport
, pstate
);
340 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
343 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
344 ptob
->truncate (sport
, n
);
346 scm_display (scm_strport_to_string (sport
), port
);
350 display_application (SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
352 SCM proc
= scm_frame_procedure (frame
);
353 SCM name
= (scm_is_true (scm_procedure_p (proc
))
354 ? scm_procedure_name (proc
)
356 display_frame_expr ("[",
357 scm_cons (scm_is_true (name
) ? name
: proc
,
358 scm_frame_arguments (frame
)),
366 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
367 (SCM frame
, SCM port
, SCM indent
),
368 "Display a procedure application @var{frame} to the output port\n"
369 "@var{port}. @var{indent} specifies the indentation of the\n"
371 #define FUNC_NAME s_scm_display_application
373 SCM_VALIDATE_FRAME (1, frame
);
374 if (SCM_UNBNDP (port
))
375 port
= scm_current_output_port ();
377 SCM_VALIDATE_OPOUTPORT (2, port
);
378 if (SCM_UNBNDP (indent
))
381 /* Display an application. */
383 SCM sport
, print_state
;
384 scm_print_state
*pstate
;
386 /* Create a string port used for adaptation of printing parameters. */
387 sport
= scm_mkstrport (SCM_INUM0
,
388 scm_make_string (scm_from_int (240),
393 /* Create a print state for printing of frames. */
394 print_state
= scm_make_print_state ();
395 pstate
= SCM_PRINT_STATE (print_state
);
396 pstate
->writingp
= 1;
399 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
405 SCM_SYMBOL (sym_base
, "base");
408 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
410 SCM source
= scm_frame_source (frame
);
411 *file
= *line
= SCM_BOOL_F
;
412 if (scm_is_pair (source
)
413 && scm_is_pair (scm_cdr (source
))
414 && scm_is_pair (scm_cddr (source
))
415 && !scm_is_pair (scm_cdddr (source
)))
417 /* (addr . (filename . (line . column))), from vm compilation */
418 *file
= scm_cadr (source
);
419 *line
= scm_caddr (source
);
424 display_backtrace_file (frame
, last_file
, port
, pstate
)
428 scm_print_state
*pstate
;
432 display_backtrace_get_file_line (frame
, &file
, &line
);
434 if (scm_is_true (scm_equal_p (file
, *last_file
)))
439 scm_puts ("In ", port
);
440 if (scm_is_false (file
))
441 if (scm_is_false (line
))
442 scm_puts ("unknown file", port
);
444 scm_puts ("current input", port
);
447 pstate
->writingp
= 0;
448 scm_iprin1 (file
, port
, pstate
);
449 pstate
->writingp
= 1;
451 scm_puts (":\n", port
);
455 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
459 display_backtrace_get_file_line (frame
, &file
, &line
);
461 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
463 if (scm_is_false (file
))
465 if (scm_is_false (line
))
466 scm_putc ('?', port
);
468 scm_puts ("<stdin>", port
);
472 pstate
-> writingp
= 0;
474 scm_iprin1 ((scm_is_string (file
)?
475 scm_basename (file
, SCM_UNDEFINED
) : file
),
478 scm_iprin1 (file
, port
, pstate
);
480 pstate
-> writingp
= 1;
483 scm_putc (':', port
);
485 else if (scm_is_true (line
))
488 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
493 if (scm_is_false (line
))
494 scm_puts (" ?", port
);
496 scm_intprint (scm_to_int (line
) + 1, 10, port
);
497 scm_puts (": ", port
);
501 display_frame (SCM frame
, int n
, int nfield
, int indentation
,
502 SCM sport
, SCM port
, scm_print_state
*pstate
)
506 /* display file name and line number */
507 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
508 display_backtrace_file_and_line (frame
, port
, pstate
);
510 /* Check size of frame number. */
511 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
513 /* Number indentation. */
514 indent (nfield
- (i
? i
: 1), port
);
517 scm_iprin1 (scm_from_int (n
), port
, pstate
);
520 indent (indentation
, port
);
522 /* Display an application. */
523 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
524 scm_putc ('\n', port
);
527 struct display_backtrace_args
{
532 SCM highlight_objects
;
536 display_backtrace_body (struct display_backtrace_args
*a
)
537 #define FUNC_NAME "display_backtrace_body"
539 int n_frames
, beg
, end
, n
, i
, j
;
540 int nfield
, indent_p
, indentation
;
541 SCM frame
, sport
, print_state
;
543 scm_print_state
*pstate
;
545 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
547 /* Argument checking and extraction. */
548 SCM_VALIDATE_STACK (1, a
->stack
);
549 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
550 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
551 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
554 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
562 if (scm_is_integer (a
->first
))
564 beg
= scm_to_int (a
->first
);
578 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
579 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
581 /* Create a string port used for adaptation of printing parameters. */
582 sport
= scm_mkstrport (SCM_INUM0
,
583 scm_make_string (scm_from_int (240), SCM_UNDEFINED
),
587 /* Create a print state for printing of frames. */
588 print_state
= scm_make_print_state ();
589 pstate
= SCM_PRINT_STATE (print_state
);
590 pstate
->writingp
= 1;
592 pstate
->highlight_objects
= a
->highlight_objects
;
594 /* First find out if it's reasonable to do indentation. */
597 /* Determine size of frame number field. */
599 for (i
= 0; j
> 0; ++i
) j
/= 10;
604 last_file
= SCM_UNDEFINED
;
609 for (i
= beg
; i
!= end
; SCM_BACKWARDS_P
? ++i
: --i
)
611 frame
= scm_stack_ref (a
->stack
, scm_from_int (i
));
612 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
613 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
614 display_frame (frame
, i
, nfield
, indentation
, sport
, a
->port
, pstate
);
617 scm_remember_upto_here_1 (print_state
);
619 return SCM_UNSPECIFIED
;
623 SCM_DEFINE (scm_display_backtrace_with_highlights
, "display-backtrace", 2, 3, 0,
624 (SCM stack
, SCM port
, SCM first
, SCM depth
, SCM highlights
),
625 "Display a backtrace to the output port @var{port}. @var{stack}\n"
626 "is the stack to take the backtrace from, @var{first} specifies\n"
627 "where in the stack to start and @var{depth} how many frames\n"
628 "to display. @var{first} and @var{depth} can be @code{#f},\n"
629 "which means that default values will be used.\n"
630 "If @var{highlights} is given it should be a list; the elements\n"
631 "of this list will be highlighted wherever they appear in the\n"
633 #define FUNC_NAME s_scm_display_backtrace_with_highlights
635 struct display_backtrace_args a
;
636 struct display_error_handler_data data
;
641 if (SCM_UNBNDP (highlights
))
642 a
.highlight_objects
= SCM_EOL
;
644 a
.highlight_objects
= highlights
;
645 data
.mode
= "backtrace";
647 scm_internal_catch (SCM_BOOL_T
,
648 (scm_t_catch_body
) display_backtrace_body
, &a
,
649 (scm_t_catch_handler
) display_error_handler
, &data
);
650 return SCM_UNSPECIFIED
;
655 scm_display_backtrace (SCM stack
, SCM port
, SCM first
, SCM depth
)
657 return scm_display_backtrace_with_highlights (stack
, port
, first
, depth
,
661 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
663 SCM_DEFINE (scm_backtrace_with_highlights
, "backtrace", 0, 1, 0,
665 "Display a backtrace of the stack saved by the last error\n"
666 "to the current output port. If @var{highlights} is given\n"
667 "it should be a list; the elements of this list will be\n"
668 "highlighted wherever they appear in the backtrace.")
669 #define FUNC_NAME s_scm_backtrace_with_highlights
671 SCM port
= scm_current_output_port ();
673 scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var
));
675 if (SCM_UNBNDP (highlights
))
676 highlights
= SCM_EOL
;
678 if (scm_is_true (the_last_stack
))
681 scm_puts ("Backtrace:\n", port
);
682 scm_display_backtrace_with_highlights (the_last_stack
,
688 if (scm_is_false (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var
))
691 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
693 "automatically if an error occurs in the future.\n",
695 SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
700 scm_puts ("No backtrace available.\n", port
);
702 return SCM_UNSPECIFIED
;
709 return scm_backtrace_with_highlights (SCM_EOL
);
715 scm_init_backtrace ()
717 SCM f
= scm_make_fluid ();
718 scm_the_last_stack_fluid_var
= scm_c_define ("the-last-stack", f
);
720 #include "libguile/backtrace.x"