1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997 Free Software Foundation
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, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
43 * The author can be reached at djurfeldt@nada.kth.se
44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
56 #include "backtrace.h"
58 /* {Error reporting and backtraces}
59 * (A first approximation.)
61 * Note that these functions shouldn't generate errors themselves.
66 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
71 SCM scm_the_last_stack_fluid
;
73 static void display_header
SCM_P ((SCM source
, SCM port
));
75 display_header (source
, port
)
79 SCM fname
= (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)
80 ? scm_source_property (source
, scm_i_filename
)
82 if (SCM_NIMP (fname
) && SCM_STRINGP (fname
))
84 scm_prin1 (fname
, port
, 0);
86 scm_prin1 (scm_source_property (source
, scm_i_line
), port
, 0);
88 scm_prin1 (scm_source_property (source
, scm_i_column
), port
, 0);
91 scm_puts ("ERROR", port
);
92 scm_puts (": ", port
);
97 scm_display_error_message (message
, args
, port
)
106 if (SCM_IMP (message
) || !SCM_ROSTRINGP (message
) || SCM_IMP (args
)
107 || !scm_list_p (args
))
109 scm_prin1 (message
, port
, 0);
110 scm_putc ('\n', port
);
114 SCM_COERCE_SUBSTR (message
);
115 start
= SCM_ROCHARS (message
);
116 for (p
= start
; *p
!= '\0'; ++p
)
119 if (SCM_IMP (args
) || SCM_NCONSP (args
))
130 scm_lfwrite (start
, p
- start
- 1, port
);
131 scm_prin1 (SCM_CAR (args
), port
, writingp
);
132 args
= SCM_CDR (args
);
135 scm_lfwrite (start
, p
- start
, port
);
136 scm_putc ('\n', port
);
139 static void display_expression
SCM_P ((SCM frame
, SCM pname
, SCM source
, SCM port
));
141 display_expression (frame
, pname
, source
, port
)
147 SCM print_state
= scm_make_print_state ();
148 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
149 pstate
->writingp
= 0;
153 if (SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
))
156 && SCM_FRAMEP (frame
)
157 && SCM_FRAME_EVAL_ARGS_P (frame
))
158 scm_puts ("While evaluating arguments to ", port
);
160 scm_puts ("In procedure ", port
);
161 scm_iprin1 (pname
, port
, pstate
);
162 if (SCM_NIMP (source
) && SCM_MEMOIZEDP (source
))
164 scm_puts (" in expression ", port
);
165 pstate
->writingp
= 1;
166 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
169 else if (SCM_NIMP (source
))
171 scm_puts ("In expression ", port
);
172 pstate
->writingp
= 1;
173 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
175 scm_puts (":\n", port
);
176 scm_free_print_state (print_state
);
179 struct display_error_args
{
189 display_error_body (struct display_error_args
*a
)
191 SCM current_frame
= SCM_BOOL_F
;
192 SCM source
= SCM_BOOL_F
;
193 SCM pname
= SCM_BOOL_F
;
195 && SCM_NIMP (a
->stack
)
196 && SCM_STACKP (a
->stack
)
197 && SCM_STACK_LENGTH (a
->stack
) > 0)
199 current_frame
= scm_stack_ref (a
->stack
, SCM_INUM0
);
200 source
= SCM_FRAME_SOURCE (current_frame
);
201 if (!(SCM_NIMP (source
) && SCM_MEMOIZEDP (source
)))
202 source
= SCM_FRAME_SOURCE (SCM_FRAME_PREV (current_frame
));
203 if (SCM_FRAME_PROC_P (current_frame
)
204 && scm_procedure_p (SCM_FRAME_PROC (current_frame
)) == SCM_BOOL_T
)
205 pname
= scm_procedure_name (SCM_FRAME_PROC (current_frame
));
207 if (!(SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
)))
209 if ((SCM_NIMP (source
) && SCM_MEMOIZEDP (source
))
210 || (SCM_NIMP (pname
) && SCM_ROSTRINGP (pname
)))
212 display_header (source
, a
->port
);
213 display_expression (current_frame
, pname
, source
, a
->port
);
215 display_header (source
, a
->port
);
216 scm_display_error_message (a
->message
, a
->args
, a
->port
);
217 return SCM_UNSPECIFIED
;
220 struct display_error_handler_data
{
225 /* This is the exception handler for error reporting routines.
226 Note that it is very important that this handler *doesn't* try to
227 print more than the error tag, since the error very probably is
228 caused by an erroneous print call-back routine. If we would
229 tru to print all objects, we would enter an infinite loop. */
231 display_error_handler (struct display_error_handler_data
*data
,
234 SCM print_state
= scm_make_print_state ();
235 scm_puts ("\nException during displaying of ", data
->port
);
236 scm_puts (data
->mode
, data
->port
);
237 scm_puts (": ", data
->port
);
238 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
239 scm_putc ('\n', data
->port
);
240 return SCM_UNSPECIFIED
;
243 SCM_PROC(s_display_error
, "display-error", 6, 0, 0, scm_display_error
);
245 scm_display_error (stack
, port
, subr
, message
, args
, rest
)
253 struct display_error_args a
= { stack
, port
, subr
, message
, args
, rest
};
254 struct display_error_handler_data data
= { "error", port
};
255 scm_internal_catch (SCM_BOOL_T
,
256 (scm_catch_body_t
) display_error_body
, &a
,
257 (scm_catch_handler_t
) display_error_handler
, &data
);
258 return SCM_UNSPECIFIED
;
261 static void indent
SCM_P ((int n
, SCM port
));
268 for (i
= 0; i
< n
; ++i
)
269 scm_putc (' ', port
);
272 static void display_frame_expr
SCM_P ((char *hdr
, SCM exp
, char *tlr
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
));
274 display_frame_expr (hdr
, exp
, tlr
, indentation
, sport
, port
, pstate
)
281 scm_print_state
*pstate
;
283 if (SCM_NIMP (exp
) && SCM_CONSP (exp
))
285 scm_iprlist (hdr
, exp
, tlr
[0], port
, pstate
);
286 scm_puts (&tlr
[1], port
);
289 scm_iprin1 (exp
, port
, pstate
);
290 scm_putc ('\n', port
);
293 static void display_application
SCM_P ((SCM frame
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
));
295 display_application (frame
, indentation
, sport
, port
, pstate
)
300 scm_print_state
*pstate
;
302 SCM proc
= SCM_FRAME_PROC (frame
);
303 SCM name
= (SCM_NFALSEP (scm_procedure_p (proc
))
304 ? scm_procedure_name (proc
)
306 display_frame_expr ("[",
307 scm_cons (SCM_NFALSEP (name
) ? name
: proc
,
308 SCM_FRAME_ARGS (frame
)),
309 SCM_FRAME_EVAL_ARGS_P (frame
) ? " ..." : "]",
316 SCM_PROC(s_display_application
, "display-application", 1, 1, 0, scm_display_application
);
319 scm_display_application (SCM frame
, SCM port
)
321 if (SCM_UNBNDP (port
))
323 if (SCM_FRAME_PROC_P (frame
))
324 /* Display an application. */
327 scm_print_state
*pstate
;
329 /* Create a print state for printing of frames. */
330 print_state
= scm_make_print_state ();
331 pstate
= SCM_PRINT_STATE (print_state
);
332 pstate
->writingp
= 1;
337 display_application (frame
, 0, SCM_BOOL_F
, port
, pstate
); /*fixme*/
344 static void display_frame
SCM_P ((SCM frame
, int nfield
, int indentation
, SCM sport
, SCM port
, scm_print_state
*pstate
));
346 display_frame (frame
, nfield
, indentation
, sport
, port
, pstate
)
352 scm_print_state
*pstate
;
356 /* Announce missing frames? */
357 if (!SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
359 indent (nfield
+ 1 + indentation
, port
);
360 scm_puts ("...\n", port
);
363 /* Check size of frame number. */
364 n
= SCM_FRAME_NUMBER (frame
);
365 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
367 /* Number indentation. */
368 indent (nfield
- (i
? i
: 1), port
);
371 scm_iprin1 (SCM_MAKINUM (n
), port
, pstate
);
373 /* Real frame marker */
374 scm_putc (SCM_FRAME_REAL_P (frame
) ? '*' : ' ', port
);
377 indent (indentation
, port
);
379 if (SCM_FRAME_PROC_P (frame
))
380 /* Display an application. */
381 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
383 /* Display a special form. */
385 SCM source
= SCM_FRAME_SOURCE (frame
);
386 SCM copy
= scm_source_property (source
, scm_i_copy
);
387 display_frame_expr ("(",
388 SCM_NIMP (copy
) && SCM_CONSP (copy
)
390 : scm_unmemoize (source
),
392 nfield
+ 1 + indentation
,
398 /* Announce missing frames? */
399 if (SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
401 indent (nfield
+ 1 + indentation
, port
);
402 scm_puts ("...\n", port
);
406 struct display_backtrace_args
{
413 SCM_PROC(s_display_backtrace
, "display-backtrace", 2, 2, 0, scm_display_backtrace
);
416 display_backtrace_body (struct display_backtrace_args
*a
)
418 int n_frames
, beg
, end
, n
, i
, j
;
419 int nfield
, indent_p
, indentation
;
420 SCM frame
, sport
, print_state
;
421 scm_print_state
*pstate
;
423 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
425 /* Argument checking and extraction. */
426 SCM_ASSERT (SCM_NIMP (a
->stack
) && SCM_STACKP (a
->stack
),
429 s_display_backtrace
);
430 SCM_ASSERT (SCM_NIMP (a
->port
) && SCM_OPOUTPORTP (a
->port
),
433 s_display_backtrace
);
434 n_frames
= SCM_INUM (scm_stack_length (a
->stack
));
435 n
= SCM_INUMP (a
->depth
) ? SCM_INUM (a
->depth
) : SCM_BACKTRACE_DEPTH
;
438 beg
= SCM_INUMP (a
->first
) ? SCM_INUM (a
->first
) : 0;
446 if (SCM_INUMP (a
->first
))
448 beg
= SCM_INUM (a
->first
);
462 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
463 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
465 /* Create a string port used for adaptation of printing parameters. */
466 sport
= scm_mkstrport (SCM_INUM0
,
467 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED
),
469 s_display_backtrace
);
471 /* Create a print state for printing of frames. */
472 print_state
= scm_make_print_state ();
473 pstate
= SCM_PRINT_STATE (print_state
);
474 pstate
->writingp
= 1;
479 /* First find out if it's reasonable to do indentation. */
485 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
486 for (i
= 0, j
= 0; i
< n
; ++i
)
488 if (SCM_FRAME_REAL_P (frame
))
490 if (j
> SCM_BACKTRACE_INDENT
)
495 frame
= (SCM_BACKWARDS_P
496 ? SCM_FRAME_PREV (frame
)
497 : SCM_FRAME_NEXT (frame
));
501 /* Determine size of frame number field. */
502 j
= SCM_FRAME_NUMBER (scm_stack_ref (a
->stack
, SCM_MAKINUM (end
)));
503 for (i
= 0; j
> 0; ++i
) j
/= 10;
506 scm_puts ("Backtrace:\n", a
->port
);
509 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
511 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
512 for (i
= 1; i
< n
; ++i
)
514 if (indent_p
&& SCM_FRAME_EVAL_ARGS_P (frame
))
516 frame
= SCM_BACKWARDS_P
? SCM_FRAME_PREV (frame
) : SCM_FRAME_NEXT (frame
);
517 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
520 return SCM_UNSPECIFIED
;
524 scm_display_backtrace (stack
, port
, first
, depth
)
530 struct display_backtrace_args a
= { stack
, port
, first
, depth
};
531 struct display_error_handler_data data
= { "backtrace", port
};
532 scm_internal_catch (SCM_BOOL_T
,
533 (scm_catch_body_t
) display_backtrace_body
, &a
,
534 (scm_catch_handler_t
) display_error_handler
, &data
);
535 return SCM_UNSPECIFIED
;
538 SCM_VCELL (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
540 SCM_PROC(s_backtrace
, "backtrace", 0, 0, 0, scm_backtrace
);
544 SCM the_last_stack
= scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid
));
545 if (SCM_NFALSEP (the_last_stack
))
547 scm_newline (scm_cur_outp
);
548 scm_display_backtrace (the_last_stack
,
552 scm_newline (scm_cur_outp
);
553 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var
))
556 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
558 "automatically if an error occurs in the future.\n",
560 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
565 scm_puts ("No backtrace available.\n", scm_cur_outp
);
567 return SCM_UNSPECIFIED
;
573 scm_init_backtrace ()
575 SCM f
= scm_make_fluid ();
576 scm_the_last_stack_fluid
= scm_sysintern ("the-last-stack", f
);
578 #include "backtrace.x"