1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010, 2011 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/deprecation.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/backtrace.h"
50 #include "libguile/filesys.h"
51 #include "libguile/private-options.h"
53 /* {Error reporting and backtraces}
55 * Note that these functions shouldn't generate errors themselves.
59 boot_print_exception (SCM port
, SCM frame
, SCM key
, SCM args
)
60 #define FUNC_NAME "boot-print-exception"
62 scm_puts_unlocked ("Throw to key ", port
);
63 scm_write (key
, port
);
64 scm_puts_unlocked (" with args ", port
);
65 scm_write (args
, port
);
66 return SCM_UNSPECIFIED
;
70 static SCM print_exception_var
;
73 init_print_exception_var (void)
76 = scm_module_variable (scm_the_root_module (),
77 scm_from_latin1_symbol ("print-exception"));
81 scm_print_exception (SCM port
, SCM frame
, SCM key
, SCM args
)
82 #define FUNC_NAME "print-exception"
84 static scm_i_pthread_once_t once
= SCM_I_PTHREAD_ONCE_INIT
;
85 scm_i_pthread_once (&once
, init_print_exception_var
);
87 SCM_VALIDATE_OPOUTPORT (1, port
);
88 if (scm_is_true (frame
))
89 SCM_VALIDATE_FRAME (2, frame
);
90 SCM_VALIDATE_SYMBOL (3, key
);
91 SCM_VALIDATE_LIST (4, args
);
93 return scm_call_4 (scm_variable_ref (print_exception_var
),
94 port
, frame
, key
, args
);
101 /* Print parameters for error messages. */
103 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
104 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
106 /* Print parameters for failing expressions in error messages.
107 * (See also `print_params' below for backtrace print parameters.)
110 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
111 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
114 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
120 scm_display_error_message (SCM message
, SCM args
, SCM port
)
122 scm_print_exception (port
, SCM_BOOL_F
, scm_misc_error_key
,
123 scm_list_3 (SCM_BOOL_F
, message
, args
));
127 /* The function scm_i_display_error prints out a detailed error message. This
128 * function will be called directly within libguile to signal error messages.
129 * No parameter checks will be performed by scm_i_display_error. Thus, User
130 * code should rather use the function scm_display_error.
133 scm_i_display_error (SCM frame
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
)
135 scm_print_exception (port
, frame
, scm_misc_error_key
,
136 scm_list_3 (subr
, message
, args
));
140 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
141 (SCM frame
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
142 "Display an error message to the output port @var{port}.\n"
143 "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
144 "the name of the procedure in which the error occurred and\n"
145 "@var{message} is the actual error message, which may contain\n"
146 "formatting instructions. These will format the arguments in\n"
147 "the list @var{args} accordingly. @var{rest} is currently\n"
149 #define FUNC_NAME s_scm_display_error
151 SCM_VALIDATE_OUTPUT_PORT (2, port
);
153 #if SCM_ENABLE_DEPRECATED
154 if (SCM_STACKP (frame
))
156 scm_c_issue_deprecation_warning
157 ("Passing a stack as the first argument to `scm_display_error' is "
158 "deprecated. Pass a frame instead.");
159 if (SCM_STACK_LENGTH (frame
))
160 frame
= scm_stack_ref (frame
, SCM_INUM0
);
166 scm_i_display_error (frame
, port
, subr
, message
, args
, rest
);
168 return SCM_UNSPECIFIED
;
178 static int n_print_params
= 9;
179 static print_params_t default_print_params
[] = {
183 { 1, 4 }, { 1, 3 }, { 1, 2 }
185 static print_params_t
*print_params
= default_print_params
;
188 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
190 "Set the print parameters to the values from @var{params}.\n"
191 "@var{params} must be a list of two-element lists which must\n"
192 "hold two integer values.")
193 #define FUNC_NAME s_scm_set_print_params_x
198 print_params_t
*new_params
;
200 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
201 for (ls
= params
; !SCM_NULL_OR_NIL_P (ls
); ls
= SCM_CDR (ls
))
202 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
203 && scm_is_unsigned_integer (SCM_CAAR (ls
), 0, INT_MAX
)
204 && scm_is_unsigned_integer (SCM_CADAR (ls
), 0, INT_MAX
),
207 s_scm_set_print_params_x
);
208 new_params
= scm_malloc (n
* sizeof (print_params_t
));
209 if (print_params
!= default_print_params
)
211 print_params
= new_params
;
212 for (i
= 0; i
< n
; ++i
)
214 print_params
[i
].level
= scm_to_int (SCM_CAAR (params
));
215 print_params
[i
].length
= scm_to_int (SCM_CADAR (params
));
216 params
= SCM_CDR (params
);
219 return SCM_UNSPECIFIED
;
225 indent (int n
, SCM port
)
228 for (i
= 0; i
< n
; ++i
)
229 scm_putc_unlocked (' ', port
);
233 display_frame_expr (char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
236 scm_t_ptob_descriptor
*ptob
= SCM_PORT_DESCRIPTOR (sport
);
239 pstate
->length
= print_params
[i
].length
;
240 ptob
->seek (sport
, 0, SEEK_SET
);
241 if (scm_is_pair (exp
))
243 pstate
->level
= print_params
[i
].level
- 1;
244 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
245 scm_puts_unlocked (&tlr
[1], sport
);
249 pstate
->level
= print_params
[i
].level
;
250 scm_iprin1 (exp
, sport
, pstate
);
253 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
256 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
257 ptob
->truncate (sport
, n
);
259 scm_display (scm_strport_to_string (sport
), port
);
263 display_application (SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
265 SCM proc
= scm_frame_procedure (frame
);
266 SCM name
= (scm_is_true (scm_procedure_p (proc
))
267 ? scm_procedure_name (proc
)
269 display_frame_expr ("[",
270 scm_cons (scm_is_true (name
) ? name
: proc
,
271 scm_frame_arguments (frame
)),
279 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
280 (SCM frame
, SCM port
, SCM indent
),
281 "Display a procedure application @var{frame} to the output port\n"
282 "@var{port}. @var{indent} specifies the indentation of the\n"
284 #define FUNC_NAME s_scm_display_application
286 SCM_VALIDATE_FRAME (1, frame
);
287 if (SCM_UNBNDP (port
))
288 port
= scm_current_output_port ();
290 SCM_VALIDATE_OPOUTPORT (2, port
);
291 if (SCM_UNBNDP (indent
))
294 /* Display an application. */
296 SCM sport
, print_state
;
297 scm_print_state
*pstate
;
299 /* Create a string port used for adaptation of printing parameters. */
300 sport
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
304 /* Create a print state for printing of frames. */
305 print_state
= scm_make_print_state ();
306 pstate
= SCM_PRINT_STATE (print_state
);
307 pstate
->writingp
= 1;
310 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
316 SCM_SYMBOL (sym_base
, "base");
319 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
321 SCM source
= scm_frame_source (frame
);
322 *file
= *line
= SCM_BOOL_F
;
323 if (scm_is_pair (source
)
324 && scm_is_pair (scm_cdr (source
))
325 && scm_is_pair (scm_cddr (source
))
326 && !scm_is_pair (scm_cdddr (source
)))
328 /* (addr . (filename . (line . column))), from vm compilation */
329 *file
= scm_cadr (source
);
330 *line
= scm_caddr (source
);
335 display_backtrace_file (frame
, last_file
, port
, pstate
)
339 scm_print_state
*pstate
;
343 display_backtrace_get_file_line (frame
, &file
, &line
);
345 if (scm_is_true (scm_equal_p (file
, *last_file
)))
350 scm_puts_unlocked ("In ", port
);
351 if (scm_is_false (file
))
352 if (scm_is_false (line
))
353 scm_puts_unlocked ("unknown file", port
);
355 scm_puts_unlocked ("current input", port
);
358 pstate
->writingp
= 0;
359 scm_iprin1 (file
, port
, pstate
);
360 pstate
->writingp
= 1;
362 scm_puts_unlocked (":\n", port
);
366 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
370 display_backtrace_get_file_line (frame
, &file
, &line
);
372 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
374 if (scm_is_false (file
))
376 if (scm_is_false (line
))
377 scm_putc_unlocked ('?', port
);
379 scm_puts_unlocked ("<stdin>", port
);
383 pstate
-> writingp
= 0;
385 scm_iprin1 ((scm_is_string (file
)?
386 scm_basename (file
, SCM_UNDEFINED
) : file
),
389 scm_iprin1 (file
, port
, pstate
);
391 pstate
-> writingp
= 1;
394 scm_putc_unlocked (':', port
);
396 else if (scm_is_true (line
))
399 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
404 if (scm_is_false (line
))
405 scm_puts_unlocked (" ?", port
);
407 scm_intprint (scm_to_int (line
) + 1, 10, port
);
408 scm_puts_unlocked (": ", port
);
412 display_frame (SCM frame
, int n
, int nfield
, int indentation
,
413 SCM sport
, SCM port
, scm_print_state
*pstate
)
417 /* display file name and line number */
418 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
419 display_backtrace_file_and_line (frame
, port
, pstate
);
421 /* Check size of frame number. */
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_from_int (n
), port
, pstate
);
431 indent (indentation
, port
);
433 /* Display an application. */
434 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
435 scm_putc_unlocked ('\n', port
);
438 struct display_backtrace_args
{
443 SCM highlight_objects
;
447 display_backtrace_body (struct display_backtrace_args
*a
)
448 #define FUNC_NAME "display_backtrace_body"
450 int n_frames
, beg
, end
, n
, i
, j
;
451 int nfield
, indentation
;
452 SCM frame
, sport
, print_state
;
454 scm_print_state
*pstate
;
456 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
458 /* Argument checking and extraction. */
459 SCM_VALIDATE_STACK (1, a
->stack
);
460 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
461 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
462 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
465 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
473 if (scm_is_integer (a
->first
))
475 beg
= scm_to_int (a
->first
);
489 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
490 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
492 /* Create a string port used for adaptation of printing parameters. */
493 sport
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
497 /* Create a print state for printing of frames. */
498 print_state
= scm_make_print_state ();
499 pstate
= SCM_PRINT_STATE (print_state
);
500 pstate
->writingp
= 1;
502 pstate
->highlight_objects
= a
->highlight_objects
;
504 /* Determine size of frame number field. */
506 for (i
= 0; j
> 0; ++i
) j
/= 10;
511 last_file
= SCM_UNDEFINED
;
516 for (i
= beg
; i
!= end
; SCM_BACKWARDS_P
? ++i
: --i
)
518 frame
= scm_stack_ref (a
->stack
, scm_from_int (i
));
519 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
520 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
521 display_frame (frame
, i
, nfield
, indentation
, sport
, a
->port
, pstate
);
524 scm_remember_upto_here_1 (print_state
);
526 return SCM_UNSPECIFIED
;
531 error_during_backtrace (void *data
, SCM tag
, SCM throw_args
)
533 SCM port
= SCM_PACK_POINTER (data
);
535 scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port
);
536 scm_print_exception (port
, SCM_BOOL_F
, tag
, throw_args
);
538 return SCM_UNSPECIFIED
;
542 SCM_DEFINE (scm_display_backtrace_with_highlights
, "display-backtrace", 2, 3, 0,
543 (SCM stack
, SCM port
, SCM first
, SCM depth
, SCM highlights
),
544 "Display a backtrace to the output port @var{port}. @var{stack}\n"
545 "is the stack to take the backtrace from, @var{first} specifies\n"
546 "where in the stack to start and @var{depth} how many frames\n"
547 "to display. @var{first} and @var{depth} can be @code{#f},\n"
548 "which means that default values will be used.\n"
549 "If @var{highlights} is given it should be a list; the elements\n"
550 "of this list will be highlighted wherever they appear in the\n"
552 #define FUNC_NAME s_scm_display_backtrace_with_highlights
554 struct display_backtrace_args a
;
559 if (SCM_UNBNDP (highlights
))
560 a
.highlight_objects
= SCM_EOL
;
562 a
.highlight_objects
= highlights
;
564 scm_internal_catch (SCM_BOOL_T
,
565 (scm_t_catch_body
) display_backtrace_body
, &a
,
566 (scm_t_catch_handler
) error_during_backtrace
, SCM_UNPACK_POINTER (port
));
568 return SCM_UNSPECIFIED
;
573 scm_display_backtrace (SCM stack
, SCM port
, SCM first
, SCM depth
)
575 return scm_display_backtrace_with_highlights (stack
, port
, first
, depth
,
579 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
581 SCM_DEFINE (scm_backtrace_with_highlights
, "backtrace", 0, 1, 0,
583 "Display a backtrace of the current stack to the current\n"
584 "output port. If @var{highlights} is given, it should be\n"
585 "a list; the elements of this list will be highlighted\n"
586 "wherever they appear in the backtrace.")
587 #define FUNC_NAME s_scm_backtrace_with_highlights
589 SCM port
= scm_current_output_port ();
590 SCM stack
= scm_make_stack (SCM_BOOL_T
, SCM_EOL
);
592 if (SCM_UNBNDP (highlights
))
593 highlights
= SCM_EOL
;
596 scm_puts_unlocked ("Backtrace:\n", port
);
597 scm_display_backtrace_with_highlights (stack
, port
, SCM_BOOL_F
, SCM_BOOL_F
,
601 return SCM_UNSPECIFIED
;
608 return scm_backtrace_with_highlights (SCM_EOL
);
614 scm_init_backtrace ()
616 scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception
);
617 #include "libguile/backtrace.x"