1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996 Mikael Djurfeldt
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this software; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice.
42 * The author can be reached at djurfeldt@nada.kth.se
43 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
54 #include "backtrace.h"
56 /* {Error reporting and backtraces}
57 * (A first approximation.)
59 * Note that these functions shouldn't generate errors themselves.
64 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
69 static void display_header
SCM_P ((SCM source
, SCM port
));
71 display_header (source
, port
)
75 SCM fname
= (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)
76 ? scm_source_property (source
, scm_i_filename
)
78 if (SCM_NIMP (fname
) && SCM_STRINGP (fname
))
80 scm_prin1 (fname
, port
, 0);
81 scm_gen_putc (':', port
);
82 scm_prin1 (scm_source_property (source
, scm_i_line
), port
, 0);
83 scm_gen_putc (':', port
);
84 scm_prin1 (scm_source_property (source
, scm_i_column
), port
, 0);
87 scm_gen_puts (scm_regular_string
, "ERROR", port
);
88 scm_gen_puts (scm_regular_string
, ": ", port
);
93 scm_display_error_message (message
, args
, port
)
102 if (!SCM_STRINGP (message
) || SCM_IMP (args
) || !scm_list_p (args
))
104 scm_prin1 (message
, port
, 0);
105 scm_gen_putc ('\n', port
);
109 start
= SCM_CHARS (message
);
110 for (p
= start
; *p
!= '\0'; ++p
)
113 if (SCM_IMP (args
) || SCM_NCONSP (args
))
124 scm_gen_write (scm_regular_string
, start
, p
- start
- 1, port
);
125 scm_prin1 (SCM_CAR (args
), port
, writingp
);
126 args
= SCM_CDR (args
);
129 scm_gen_write (scm_regular_string
, start
, p
- start
, port
);
130 scm_gen_putc ('\n', port
);
133 static void display_expression
SCM_P ((SCM frame
, SCM pname
, SCM source
, SCM port
));
135 display_expression (frame
, pname
, source
, port
)
141 SCM print_state
= scm_make_print_state ();
142 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
143 pstate
->writingp
= 0;
147 if (SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
))
150 && SCM_FRAMEP (frame
)
151 && SCM_FRAME_EVAL_ARGS_P (frame
))
152 scm_gen_puts (scm_regular_string
, "While evaluating arguments to ", port
);
154 scm_gen_puts (scm_regular_string
, "In procedure ", port
);
155 scm_iprin1 (pname
, port
, pstate
);
156 if (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
))
158 scm_gen_puts (scm_regular_string
, " in expression ", port
);
159 pstate
->writingp
= 1;
160 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
163 else if (SCM_NIMP (source
))
165 scm_gen_puts (scm_regular_string
, "In expression ", port
);
166 pstate
->writingp
= 1;
167 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
169 scm_gen_puts (scm_regular_string
, ":\n", port
);
170 scm_free_print_state (print_state
);
173 SCM_PROC(s_display_error
, "display-error", 6, 0, 0, scm_display_error
);
175 scm_display_error (stack
, port
, subr
, message
, args
, rest
)
183 SCM current_frame
= SCM_BOOL_F
;
184 SCM source
= SCM_BOOL_F
;
185 SCM pname
= SCM_BOOL_F
;
188 && SCM_STACKP (stack
)
189 && SCM_STACK_LENGTH (stack
) > 0)
191 current_frame
= scm_stack_ref (stack
, SCM_INUM0
);
192 source
= SCM_FRAME_SOURCE (current_frame
);
193 if (!(SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)))
194 source
= SCM_FRAME_SOURCE (SCM_FRAME_PREV (current_frame
));
195 if (SCM_FRAME_PROC_P (current_frame
)
196 && scm_procedure_p (SCM_FRAME_PROC (current_frame
)) == SCM_BOOL_T
)
197 pname
= scm_procedure_name (SCM_FRAME_PROC (current_frame
));
199 if (!(SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
)))
201 if ((SCM_NIMP (source
) && SCM_MEMOIZEDP (source
))
202 || (SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
)))
204 display_header (source
, port
);
205 display_expression (current_frame
, pname
, source
, port
);
207 display_header (source
, port
);
208 scm_display_error_message (message
, args
, port
);
209 return SCM_UNSPECIFIED
;
212 static void indent
SCM_P ((int n
, SCM port
));
219 for (i
= 0; i
< n
; ++i
)
220 scm_gen_putc (' ', port
);
223 static void display_frame_expr
SCM_P ((char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
));
225 display_frame_expr (hdr
, exp
, tlr
, indentation
, sport
, port
, pstate
)
232 scm_print_state
*pstate
;
236 if (SCM_NIMP (exp
) && SCM_CONSP (exp
))
238 scm_iprlist (hdr
, exp
, tlr
[0], port
, pstate
);
239 scm_gen_puts (scm_regular_string
, &tlr
[1], port
);
242 scm_iprin1 (exp
, port
, pstate
);
243 scm_gen_putc ('\n', port
);
246 static void display_frame
SCM_P ((SCM frame
, int nfield
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
));
248 display_frame (frame
, nfield
, indentation
, sport
, port
, pstate
)
254 scm_print_state
*pstate
;
258 /* Announce missing frames? */
259 if (!SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
261 indent (nfield
+ 1 + indentation
, port
);
262 scm_gen_puts (scm_regular_string
, "...\n", port
);
265 /* Check size of frame number. */
266 n
= SCM_FRAME_NUMBER (frame
);
267 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
269 /* Number indentation. */
270 indent (nfield
- (i
? i
: 1), port
);
273 scm_iprin1 (SCM_MAKINUM (n
), port
, pstate
);
275 /* Real frame marker */
276 scm_gen_putc (SCM_FRAME_REAL_P (frame
) ? '*' : ' ', port
);
279 indent (indentation
, port
);
281 if (SCM_FRAME_PROC_P (frame
))
282 /* Display an application. */
284 SCM proc
= SCM_FRAME_PROC (frame
);
285 SCM name
= (SCM_NFALSEP (scm_procedure_p (proc
))
286 ? scm_procedure_name (proc
)
288 display_frame_expr ("[",
289 scm_cons (SCM_NFALSEP (name
) ? name
: proc
,
290 SCM_FRAME_ARGS (frame
)),
291 SCM_FRAME_EVAL_ARGS_P (frame
) ? " ..." : "]",
292 nfield
+ 1 + indentation
,
298 /* Display a special form. */
300 SCM source
= SCM_FRAME_SOURCE (frame
);
301 SCM copy
= scm_source_property (source
, scm_i_copy
);
302 display_frame_expr ("(",
303 SCM_NIMP (copy
) && SCM_CONSP (copy
)
305 : scm_unmemoize (source
),
307 nfield
+ 1 + indentation
,
313 /* Announce missing frames? */
314 if (SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
316 indent (nfield
+ 1 + indentation
, port
);
317 scm_gen_puts (scm_regular_string
, "...\n", port
);
321 SCM_PROC(s_display_backtrace
, "display-backtrace", 2, 2, 0, scm_display_backtrace
);
323 scm_display_backtrace (stack
, port
, first
, depth
)
329 int n_frames
, beg
, end
, n
, i
, j
;
330 int nfield
, indent_p
, indentation
;
331 SCM frame
, sport
, print_state
;
332 scm_print_state
*pstate
;
334 /* Argument checking and extraction. */
335 SCM_ASSERT (SCM_NIMP (stack
) && SCM_STACKP (stack
),
338 s_display_backtrace
);
339 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
),
342 s_display_backtrace
);
343 n_frames
= SCM_INUM (scm_stack_length (stack
));
344 n
= SCM_INUMP (depth
) ? SCM_INUM (depth
) : SCM_BACKTRACE_DEPTH
;
347 beg
= SCM_INUMP (first
) ? SCM_INUM (first
) : 0;
355 if (SCM_INUMP (first
))
357 beg
= SCM_INUM (first
);
371 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, first
, SCM_ARG3
, s_display_backtrace
);
372 SCM_ASSERT (n
> 0, depth
, SCM_ARG4
, s_display_backtrace
);
374 /* Create a string port used for adaptation of printing parameters. */
375 sport
= scm_mkstrport (SCM_INUM0
,
376 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED
),
378 s_display_backtrace
);
380 /* Create a print state for printing of frames. */
381 print_state
= scm_make_print_state ();
382 pstate
= SCM_PRINT_STATE (print_state
);
383 pstate
->writingp
= 1;
386 /* First find out if it's reasonable to do indentation. */
392 frame
= scm_stack_ref (stack
, SCM_MAKINUM (beg
));
393 for (i
= 0, j
= 0; i
< n
; ++i
)
395 if (SCM_FRAME_REAL_P (frame
))
397 if (j
> SCM_BACKTRACE_INDENT
)
402 frame
= (SCM_BACKWARDS_P
403 ? SCM_FRAME_PREV (frame
)
404 : SCM_FRAME_NEXT (frame
));
408 /* Determine size of frame number field. */
409 j
= SCM_FRAME_NUMBER (scm_stack_ref (stack
, SCM_MAKINUM (end
)));
410 for (i
= 0; j
> 0; ++i
) j
/= 10;
413 scm_gen_puts (scm_regular_string
, "Backtrace:\n", port
);
416 frame
= scm_stack_ref (stack
, SCM_MAKINUM (beg
));
418 display_frame (frame
, nfield
, indentation
, sport
, port
, pstate
);
419 for (i
= 1; i
< n
; ++i
)
421 if (indent_p
&& SCM_FRAME_EVAL_ARGS_P (frame
))
423 frame
= SCM_BACKWARDS_P
? SCM_FRAME_PREV (frame
) : SCM_FRAME_NEXT (frame
);
424 display_frame (frame
, nfield
, indentation
, sport
, port
, pstate
);
427 return SCM_UNSPECIFIED
;
430 SCM_GLOBAL (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
432 SCM_PROC(s_backtrace
, "backtrace", 0, 0, 0, scm_backtrace
);
436 if (SCM_NFALSEP (SCM_CDR (scm_the_last_stack_var
)))
438 scm_newline (scm_cur_outp
);
439 scm_display_backtrace (SCM_CDR (scm_the_last_stack_var
),
443 scm_newline (scm_cur_outp
);
444 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var
))
447 scm_gen_puts (scm_regular_string
,
448 "Type \"(debug-enable 'backtrace)\" if you would like a backtrace
449 automatically if an error occurs in the future.\n",
451 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
456 scm_gen_puts (scm_regular_string
,
457 "No backtrace available.\n",
460 return SCM_UNSPECIFIED
;
466 scm_init_backtrace ()
468 scm_the_last_stack_var
= scm_sysintern ("the-last-stack", SCM_BOOL_F
);
470 #include "backtrace.x"