1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009,
3 * 2010, 2011, 2014 Free Software Foundation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28 #include "libguile/_scm.h"
35 #include "libguile/deprecation.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.
58 boot_print_exception (SCM port
, SCM frame
, SCM key
, SCM args
)
59 #define FUNC_NAME "boot-print-exception"
61 scm_puts_unlocked ("Throw to key ", port
);
62 scm_write (key
, port
);
63 scm_puts_unlocked (" with args ", port
);
64 scm_write (args
, port
);
65 return SCM_UNSPECIFIED
;
69 static SCM print_exception_var
;
72 init_print_exception_var (void)
75 = scm_module_variable (scm_the_root_module (),
76 scm_from_latin1_symbol ("print-exception"));
80 scm_print_exception (SCM port
, SCM frame
, SCM key
, SCM args
)
81 #define FUNC_NAME "print-exception"
83 static scm_i_pthread_once_t once
= SCM_I_PTHREAD_ONCE_INIT
;
84 scm_i_pthread_once (&once
, init_print_exception_var
);
86 SCM_VALIDATE_OPOUTPORT (1, port
);
87 if (scm_is_true (frame
))
88 SCM_VALIDATE_FRAME (2, frame
);
89 SCM_VALIDATE_SYMBOL (3, key
);
90 SCM_VALIDATE_LIST (4, args
);
92 return scm_call_4 (scm_variable_ref (print_exception_var
),
93 port
, frame
, key
, args
);
100 /* Print parameters for error messages. */
102 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
103 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
105 /* Print parameters for failing expressions in error messages.
106 * (See also `print_params' below for backtrace print parameters.)
109 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
110 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
113 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
119 scm_display_error_message (SCM message
, SCM args
, SCM port
)
121 scm_print_exception (port
, SCM_BOOL_F
, scm_misc_error_key
,
122 scm_list_3 (SCM_BOOL_F
, message
, args
));
126 /* The function scm_i_display_error prints out a detailed error message. This
127 * function will be called directly within libguile to signal error messages.
128 * No parameter checks will be performed by scm_i_display_error. Thus, User
129 * code should rather use the function scm_display_error.
132 scm_i_display_error (SCM frame
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
)
134 scm_print_exception (port
, frame
, scm_misc_error_key
,
135 scm_list_3 (subr
, message
, args
));
139 SCM_DEFINE (scm_display_error
, "display-error", 6, 0, 0,
140 (SCM frame
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
141 "Display an error message to the output port @var{port}.\n"
142 "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
143 "the name of the procedure in which the error occurred and\n"
144 "@var{message} is the actual error message, which may contain\n"
145 "formatting instructions. These will format the arguments in\n"
146 "the list @var{args} accordingly. @var{rest} is currently\n"
148 #define FUNC_NAME s_scm_display_error
150 SCM_VALIDATE_OUTPUT_PORT (2, port
);
152 #if SCM_ENABLE_DEPRECATED
153 if (SCM_STACKP (frame
))
155 scm_c_issue_deprecation_warning
156 ("Passing a stack as the first argument to `scm_display_error' is "
157 "deprecated. Pass a frame instead.");
158 if (SCM_STACK_LENGTH (frame
))
159 frame
= scm_stack_ref (frame
, SCM_INUM0
);
165 scm_i_display_error (frame
, port
, subr
, message
, args
, rest
);
167 return SCM_UNSPECIFIED
;
177 static int n_print_params
= 9;
178 static print_params_t default_print_params
[] = {
182 { 1, 4 }, { 1, 3 }, { 1, 2 }
184 static print_params_t
*print_params
= default_print_params
;
187 SCM_DEFINE (scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
189 "Set the print parameters to the values from @var{params}.\n"
190 "@var{params} must be a list of two-element lists which must\n"
191 "hold two integer values.")
192 #define FUNC_NAME s_scm_set_print_params_x
197 print_params_t
*new_params
;
199 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params
, n
);
200 for (ls
= params
; !SCM_NULL_OR_NIL_P (ls
); ls
= SCM_CDR (ls
))
201 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
202 && scm_is_unsigned_integer (SCM_CAAR (ls
), 0, INT_MAX
)
203 && scm_is_unsigned_integer (SCM_CADAR (ls
), 0, INT_MAX
),
206 s_scm_set_print_params_x
);
207 new_params
= scm_malloc (n
* sizeof (print_params_t
));
208 if (print_params
!= default_print_params
)
210 print_params
= new_params
;
211 for (i
= 0; i
< n
; ++i
)
213 print_params
[i
].level
= scm_to_int (SCM_CAAR (params
));
214 print_params
[i
].length
= scm_to_int (SCM_CADAR (params
));
215 params
= SCM_CDR (params
);
218 return SCM_UNSPECIFIED
;
224 indent (int n
, SCM port
)
227 for (i
= 0; i
< n
; ++i
)
228 scm_putc_unlocked (' ', port
);
232 display_frame_expr (char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
235 scm_t_ptob_descriptor
*ptob
= SCM_PORT_DESCRIPTOR (sport
);
238 pstate
->length
= print_params
[i
].length
;
239 ptob
->seek (sport
, 0, SEEK_SET
);
240 if (scm_is_pair (exp
))
242 pstate
->level
= print_params
[i
].level
- 1;
243 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
244 scm_puts_unlocked (&tlr
[1], sport
);
248 pstate
->level
= print_params
[i
].level
;
249 scm_iprin1 (exp
, sport
, pstate
);
252 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
255 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
256 ptob
->truncate (sport
, n
);
258 scm_display (scm_strport_to_string (sport
), port
);
262 display_application (SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
)
264 display_frame_expr ("[", scm_frame_call_representation (frame
), "]",
271 SCM_DEFINE (scm_display_application
, "display-application", 1, 2, 0,
272 (SCM frame
, SCM port
, SCM indent
),
273 "Display a procedure application @var{frame} to the output port\n"
274 "@var{port}. @var{indent} specifies the indentation of the\n"
276 #define FUNC_NAME s_scm_display_application
278 SCM_VALIDATE_FRAME (1, frame
);
279 if (SCM_UNBNDP (port
))
280 port
= scm_current_output_port ();
282 SCM_VALIDATE_OPOUTPORT (2, port
);
283 if (SCM_UNBNDP (indent
))
286 /* Display an application. */
288 SCM sport
, print_state
;
289 scm_print_state
*pstate
;
291 /* Create a string port used for adaptation of printing parameters. */
292 sport
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
296 /* Create a print state for printing of frames. */
297 print_state
= scm_make_print_state ();
298 pstate
= SCM_PRINT_STATE (print_state
);
299 pstate
->writingp
= 1;
302 display_application (frame
, scm_to_int (indent
), sport
, port
, pstate
);
308 SCM_SYMBOL (sym_base
, "base");
311 display_backtrace_get_file_line (SCM frame
, SCM
*file
, SCM
*line
)
313 SCM source
= scm_frame_source (frame
);
314 *file
= *line
= SCM_BOOL_F
;
315 if (scm_is_pair (source
)
316 && scm_is_pair (scm_cdr (source
))
317 && scm_is_pair (scm_cddr (source
))
318 && !scm_is_pair (scm_cdddr (source
)))
320 /* (addr . (filename . (line . column))), from vm compilation */
321 *file
= scm_cadr (source
);
322 *line
= scm_caddr (source
);
327 display_backtrace_file (frame
, last_file
, port
, pstate
)
331 scm_print_state
*pstate
;
335 display_backtrace_get_file_line (frame
, &file
, &line
);
337 if (scm_is_true (scm_equal_p (file
, *last_file
)))
342 scm_puts_unlocked ("In ", port
);
343 if (scm_is_false (file
))
344 if (scm_is_false (line
))
345 scm_puts_unlocked ("unknown file", port
);
347 scm_puts_unlocked ("current input", port
);
350 pstate
->writingp
= 0;
351 scm_iprin1 (file
, port
, pstate
);
352 pstate
->writingp
= 1;
354 scm_puts_unlocked (":\n", port
);
358 display_backtrace_file_and_line (SCM frame
, SCM port
, scm_print_state
*pstate
)
362 display_backtrace_get_file_line (frame
, &file
, &line
);
364 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
366 if (scm_is_false (file
))
368 if (scm_is_false (line
))
369 scm_putc_unlocked ('?', port
);
371 scm_puts_unlocked ("<stdin>", port
);
375 pstate
-> writingp
= 0;
377 scm_iprin1 ((scm_is_string (file
)?
378 scm_basename (file
, SCM_UNDEFINED
) : file
),
381 scm_iprin1 (file
, port
, pstate
);
383 pstate
-> writingp
= 1;
386 scm_putc_unlocked (':', port
);
388 else if (scm_is_true (line
))
391 for (i
= scm_to_int (line
)+1; i
> 0; i
= i
/10, j
++)
396 if (scm_is_false (line
))
397 scm_puts_unlocked (" ?", port
);
399 scm_intprint (scm_to_int (line
) + 1, 10, port
);
400 scm_puts_unlocked (": ", port
);
404 display_frame (SCM frame
, int n
, int nfield
, int indentation
,
405 SCM sport
, SCM port
, scm_print_state
*pstate
)
409 /* display file name and line number */
410 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME
)))
411 display_backtrace_file_and_line (frame
, port
, pstate
);
413 /* Check size of frame number. */
414 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
416 /* Number indentation. */
417 indent (nfield
- (i
? i
: 1), port
);
420 scm_iprin1 (scm_from_int (n
), port
, pstate
);
423 indent (indentation
, port
);
425 /* Display an application. */
426 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
427 scm_putc_unlocked ('\n', port
);
430 struct display_backtrace_args
{
435 SCM highlight_objects
;
439 display_backtrace_body (struct display_backtrace_args
*a
)
440 #define FUNC_NAME "display_backtrace_body"
442 int n_frames
, beg
, end
, n
, i
, j
;
443 int nfield
, indentation
;
444 SCM frame
, sport
, print_state
;
446 scm_print_state
*pstate
;
448 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
450 /* Argument checking and extraction. */
451 SCM_VALIDATE_STACK (1, a
->stack
);
452 SCM_VALIDATE_OPOUTPORT (2, a
->port
);
453 n_frames
= scm_to_int (scm_stack_length (a
->stack
));
454 n
= scm_is_integer (a
->depth
) ? scm_to_int (a
->depth
) : SCM_BACKTRACE_DEPTH
;
457 beg
= scm_is_integer (a
->first
) ? scm_to_int (a
->first
) : 0;
465 if (scm_is_integer (a
->first
))
467 beg
= scm_to_int (a
->first
);
481 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
482 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
484 /* Create a string port used for adaptation of printing parameters. */
485 sport
= scm_mkstrport (SCM_INUM0
, SCM_BOOL_F
,
489 /* Create a print state for printing of frames. */
490 print_state
= scm_make_print_state ();
491 pstate
= SCM_PRINT_STATE (print_state
);
492 pstate
->writingp
= 1;
494 pstate
->highlight_objects
= a
->highlight_objects
;
496 /* Determine size of frame number field. */
498 for (i
= 0; j
> 0; ++i
) j
/= 10;
503 last_file
= SCM_UNDEFINED
;
508 for (i
= beg
; i
!= end
; SCM_BACKWARDS_P
? ++i
: --i
)
510 frame
= scm_stack_ref (a
->stack
, scm_from_int (i
));
511 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME
), sym_base
))
512 display_backtrace_file (frame
, &last_file
, a
->port
, pstate
);
513 display_frame (frame
, i
, nfield
, indentation
, sport
, a
->port
, pstate
);
516 scm_remember_upto_here_1 (print_state
);
518 return SCM_UNSPECIFIED
;
523 error_during_backtrace (void *data
, SCM tag
, SCM throw_args
)
525 SCM port
= SCM_PACK_POINTER (data
);
527 scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port
);
528 scm_print_exception (port
, SCM_BOOL_F
, tag
, throw_args
);
530 return SCM_UNSPECIFIED
;
534 SCM_DEFINE (scm_display_backtrace_with_highlights
, "display-backtrace", 2, 3, 0,
535 (SCM stack
, SCM port
, SCM first
, SCM depth
, SCM highlights
),
536 "Display a backtrace to the output port @var{port}. @var{stack}\n"
537 "is the stack to take the backtrace from, @var{first} specifies\n"
538 "where in the stack to start and @var{depth} how many frames\n"
539 "to display. @var{first} and @var{depth} can be @code{#f},\n"
540 "which means that default values will be used.\n"
541 "If @var{highlights} is given it should be a list; the elements\n"
542 "of this list will be highlighted wherever they appear in the\n"
544 #define FUNC_NAME s_scm_display_backtrace_with_highlights
546 struct display_backtrace_args a
;
551 if (SCM_UNBNDP (highlights
))
552 a
.highlight_objects
= SCM_EOL
;
554 a
.highlight_objects
= highlights
;
556 scm_internal_catch (SCM_BOOL_T
,
557 (scm_t_catch_body
) display_backtrace_body
, &a
,
558 (scm_t_catch_handler
) error_during_backtrace
, SCM_UNPACK_POINTER (port
));
560 return SCM_UNSPECIFIED
;
565 scm_display_backtrace (SCM stack
, SCM port
, SCM first
, SCM depth
)
567 return scm_display_backtrace_with_highlights (stack
, port
, first
, depth
,
571 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
573 SCM_DEFINE (scm_backtrace_with_highlights
, "backtrace", 0, 1, 0,
575 "Display a backtrace of the current stack to the current\n"
576 "output port. If @var{highlights} is given, it should be\n"
577 "a list; the elements of this list will be highlighted\n"
578 "wherever they appear in the backtrace.")
579 #define FUNC_NAME s_scm_backtrace_with_highlights
581 SCM port
= scm_current_output_port ();
582 SCM stack
= scm_make_stack (SCM_BOOL_T
, SCM_EOL
);
584 if (SCM_UNBNDP (highlights
))
585 highlights
= SCM_EOL
;
588 scm_puts_unlocked ("Backtrace:\n", port
);
589 scm_display_backtrace_with_highlights (stack
, port
, SCM_BOOL_F
, SCM_BOOL_F
,
593 return SCM_UNSPECIFIED
;
600 return scm_backtrace_with_highlights (SCM_EOL
);
606 scm_init_backtrace ()
608 scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception
);
609 #include "libguile/backtrace.x"