1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010, 2011, 2014 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 display_frame_expr ("[", scm_frame_call_representation (frame
), "]",
272 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
273 (SCM frame
, SCM port
, SCM indent
),
274 "Display a procedure application @var{frame} to the output port\n"
275 "@var{port}. @var{indent} specifies the indentation of the\n"
277 #define FUNC_NAME s_scm_display_application
279 SCM_VALIDATE_FRAME (1, frame
);
280 if (SCM_UNBNDP (port
))
281 port
= scm_current_output_port ();
283 SCM_VALIDATE_OPOUTPORT (2, port
);
284 if (SCM_UNBNDP (indent
))
287 /* Display an application. */
289 SCM sport
, print_state
;
290 scm_print_state
*pstate
;
292 /* Create a string port used for adaptation of printing parameters. */
293 sport
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
297 /* Create a print state for printing of frames. */
298 print_state
= scm_make_print_state ();
299 pstate
= SCM_PRINT_STATE (print_state
);
300 pstate
->writingp
= 1;
303 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
309 SCM_SYMBOL (sym_base
, "base");
312 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
314 SCM source
= scm_frame_source (frame
);
315 *file
= *line
= SCM_BOOL_F
;
316 if (scm_is_pair (source
)
317 && scm_is_pair (scm_cdr (source
))
318 && scm_is_pair (scm_cddr (source
))
319 && !scm_is_pair (scm_cdddr (source
)))
321 /* (addr . (filename . (line . column))), from vm compilation */
322 *file
= scm_cadr (source
);
323 *line
= scm_caddr (source
);
328 display_backtrace_file (frame
, last_file
, port
, pstate
)
332 scm_print_state
*pstate
;
336 display_backtrace_get_file_line (frame
, &file
, &line
);
338 if (scm_is_true (scm_equal_p (file
, *last_file
)))
343 scm_puts_unlocked ("In ", port
);
344 if (scm_is_false (file
))
345 if (scm_is_false (line
))
346 scm_puts_unlocked ("unknown file", port
);
348 scm_puts_unlocked ("current input", port
);
351 pstate
->writingp
= 0;
352 scm_iprin1 (file
, port
, pstate
);
353 pstate
->writingp
= 1;
355 scm_puts_unlocked (":\n", port
);
359 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
363 display_backtrace_get_file_line (frame
, &file
, &line
);
365 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
367 if (scm_is_false (file
))
369 if (scm_is_false (line
))
370 scm_putc_unlocked ('?', port
);
372 scm_puts_unlocked ("<stdin>", port
);
376 pstate
-> writingp
= 0;
378 scm_iprin1 ((scm_is_string (file
)?
379 scm_basename (file
, SCM_UNDEFINED
) : file
),
382 scm_iprin1 (file
, port
, pstate
);
384 pstate
-> writingp
= 1;
387 scm_putc_unlocked (':', port
);
389 else if (scm_is_true (line
))
392 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
397 if (scm_is_false (line
))
398 scm_puts_unlocked (" ?", port
);
400 scm_intprint (scm_to_int (line
) + 1, 10, port
);
401 scm_puts_unlocked (": ", port
);
405 display_frame (SCM frame
, int n
, int nfield
, int indentation
,
406 SCM sport
, SCM port
, scm_print_state
*pstate
)
410 /* display file name and line number */
411 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
412 display_backtrace_file_and_line (frame
, port
, pstate
);
414 /* Check size of frame number. */
415 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
417 /* Number indentation. */
418 indent (nfield
- (i
? i
: 1), port
);
421 scm_iprin1 (scm_from_int (n
), port
, pstate
);
424 indent (indentation
, port
);
426 /* Display an application. */
427 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
428 scm_putc_unlocked ('\n', port
);
431 struct display_backtrace_args
{
436 SCM highlight_objects
;
440 display_backtrace_body (struct display_backtrace_args
*a
)
441 #define FUNC_NAME "display_backtrace_body"
443 int n_frames
, beg
, end
, n
, i
, j
;
444 int nfield
, indentation
;
445 SCM frame
, sport
, print_state
;
447 scm_print_state
*pstate
;
449 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
451 /* Argument checking and extraction. */
452 SCM_VALIDATE_STACK (1, a
->stack
);
453 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
454 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
455 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
458 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
466 if (scm_is_integer (a
->first
))
468 beg
= scm_to_int (a
->first
);
482 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
483 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
485 /* Create a string port used for adaptation of printing parameters. */
486 sport
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
490 /* Create a print state for printing of frames. */
491 print_state
= scm_make_print_state ();
492 pstate
= SCM_PRINT_STATE (print_state
);
493 pstate
->writingp
= 1;
495 pstate
->highlight_objects
= a
->highlight_objects
;
497 /* Determine size of frame number field. */
499 for (i
= 0; j
> 0; ++i
) j
/= 10;
504 last_file
= SCM_UNDEFINED
;
509 for (i
= beg
; i
!= end
; SCM_BACKWARDS_P
? ++i
: --i
)
511 frame
= scm_stack_ref (a
->stack
, scm_from_int (i
));
512 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
513 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
514 display_frame (frame
, i
, nfield
, indentation
, sport
, a
->port
, pstate
);
517 scm_remember_upto_here_1 (print_state
);
519 return SCM_UNSPECIFIED
;
524 error_during_backtrace (void *data
, SCM tag
, SCM throw_args
)
526 SCM port
= SCM_PACK_POINTER (data
);
528 scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port
);
529 scm_print_exception (port
, SCM_BOOL_F
, tag
, throw_args
);
531 return SCM_UNSPECIFIED
;
535 SCM_DEFINE (scm_display_backtrace_with_highlights
, "display-backtrace", 2, 3, 0,
536 (SCM stack
, SCM port
, SCM first
, SCM depth
, SCM highlights
),
537 "Display a backtrace to the output port @var{port}. @var{stack}\n"
538 "is the stack to take the backtrace from, @var{first} specifies\n"
539 "where in the stack to start and @var{depth} how many frames\n"
540 "to display. @var{first} and @var{depth} can be @code{#f},\n"
541 "which means that default values will be used.\n"
542 "If @var{highlights} is given it should be a list; the elements\n"
543 "of this list will be highlighted wherever they appear in the\n"
545 #define FUNC_NAME s_scm_display_backtrace_with_highlights
547 struct display_backtrace_args a
;
552 if (SCM_UNBNDP (highlights
))
553 a
.highlight_objects
= SCM_EOL
;
555 a
.highlight_objects
= highlights
;
557 scm_internal_catch (SCM_BOOL_T
,
558 (scm_t_catch_body
) display_backtrace_body
, &a
,
559 (scm_t_catch_handler
) error_during_backtrace
, SCM_UNPACK_POINTER (port
));
561 return SCM_UNSPECIFIED
;
566 scm_display_backtrace (SCM stack
, SCM port
, SCM first
, SCM depth
)
568 return scm_display_backtrace_with_highlights (stack
, port
, first
, depth
,
572 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
574 SCM_DEFINE (scm_backtrace_with_highlights
, "backtrace", 0, 1, 0,
576 "Display a backtrace of the current stack to the current\n"
577 "output port. If @var{highlights} is given, it should be\n"
578 "a list; the elements of this list will be highlighted\n"
579 "wherever they appear in the backtrace.")
580 #define FUNC_NAME s_scm_backtrace_with_highlights
582 SCM port
= scm_current_output_port ();
583 SCM stack
= scm_make_stack (SCM_BOOL_T
, SCM_EOL
);
585 if (SCM_UNBNDP (highlights
))
586 highlights
= SCM_EOL
;
589 scm_puts_unlocked ("Backtrace:\n", port
);
590 scm_display_backtrace_with_highlights (stack
, port
, SCM_BOOL_F
, SCM_BOOL_F
,
594 return SCM_UNSPECIFIED
;
601 return scm_backtrace_with_highlights (SCM_EOL
);
607 scm_init_backtrace ()
609 scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception
);
610 #include "libguile/backtrace.x"