1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996, 1997, 1998, 1999 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 */
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
67 #include "scm_validate.h"
68 #include "backtrace.h"
70 /* {Error reporting and backtraces}
71 * (A first approximation.)
73 * Note that these functions shouldn't generate errors themselves.
78 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
83 SCM scm_the_last_stack_fluid
;
86 display_header (SCM source
, SCM port
)
88 SCM fname
= (SCM_MEMOIZEDP (source
)
89 ? scm_source_property (source
, scm_sym_filename
)
91 if (SCM_STRINGP (fname
))
93 scm_prin1 (fname
, port
, 0);
95 scm_intprint (SCM_INUM (scm_source_property (source
, scm_sym_line
)) + 1,
99 scm_intprint (SCM_INUM (scm_source_property (source
, scm_sym_column
)) + 1,
104 scm_puts ("ERROR", port
);
105 scm_puts (": ", port
);
110 scm_display_error_message (SCM message
, SCM args
, SCM port
)
116 if (SCM_IMP (message
) || !SCM_ROSTRINGP (message
) || SCM_IMP (args
)
117 || !scm_list_p (args
))
119 scm_prin1 (message
, port
, 0);
120 scm_putc ('\n', port
);
124 SCM_COERCE_SUBSTR (message
);
125 start
= SCM_ROCHARS (message
);
126 for (p
= start
; *p
!= '\0'; ++p
)
129 if (SCM_IMP (args
) || SCM_NCONSP (args
))
140 scm_lfwrite (start
, p
- start
- 1, port
);
141 scm_prin1 (SCM_CAR (args
), port
, writingp
);
142 args
= SCM_CDR (args
);
145 scm_lfwrite (start
, p
- start
, port
);
146 scm_putc ('\n', port
);
150 display_expression (SCM frame
,SCM pname
,SCM source
,SCM port
)
152 SCM print_state
= scm_make_print_state ();
153 scm_print_state
*pstate
= SCM_PRINT_STATE (print_state
);
154 pstate
->writingp
= 0;
158 if (SCM_ROSTRINGP (pname
))
160 if (SCM_FRAMEP (frame
)
161 && SCM_FRAME_EVAL_ARGS_P (frame
))
162 scm_puts ("While evaluating arguments to ", port
);
164 scm_puts ("In procedure ", port
);
165 scm_iprin1 (pname
, port
, pstate
);
166 if (SCM_MEMOIZEDP (source
))
168 scm_puts (" in expression ", port
);
169 pstate
->writingp
= 1;
170 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
173 else if (SCM_NIMP (source
))
175 scm_puts ("In expression ", port
);
176 pstate
->writingp
= 1;
177 scm_iprin1 (scm_unmemoize (source
), port
, pstate
);
179 scm_puts (":\n", port
);
180 scm_free_print_state (print_state
);
183 struct display_error_args
{
193 display_error_body (struct display_error_args
*a
)
195 SCM current_frame
= SCM_BOOL_F
;
196 SCM source
= SCM_BOOL_F
;
197 SCM pname
= SCM_BOOL_F
;
198 SCM prev_frame
= SCM_BOOL_F
;
201 && SCM_STACKP (a
->stack
)
202 && SCM_STACK_LENGTH (a
->stack
) > 0)
204 current_frame
= scm_stack_ref (a
->stack
, SCM_INUM0
);
205 source
= SCM_FRAME_SOURCE (current_frame
);
206 prev_frame
= SCM_FRAME_PREV (current_frame
);
207 if (!SCM_MEMOIZEDP (source
)
208 && prev_frame
!= SCM_BOOL_F
)
209 source
= SCM_FRAME_SOURCE (prev_frame
);
210 if (SCM_FRAME_PROC_P (current_frame
)
211 && scm_procedure_p (SCM_FRAME_PROC (current_frame
)) == SCM_BOOL_T
)
212 pname
= scm_procedure_name (SCM_FRAME_PROC (current_frame
));
214 if (!SCM_ROSTRINGP (pname
))
216 if (SCM_ROSTRINGP (pname
) || SCM_MEMOIZEDP (source
))
218 display_header (source
, a
->port
);
219 display_expression (current_frame
, pname
, source
, a
->port
);
221 display_header (source
, a
->port
);
222 scm_display_error_message (a
->message
, a
->args
, a
->port
);
223 return SCM_UNSPECIFIED
;
226 struct display_error_handler_data
{
231 /* This is the exception handler for error reporting routines.
232 Note that it is very important that this handler *doesn't* try to
233 print more than the error tag, since the error very probably is
234 caused by an erroneous print call-back routine. If we would
235 tru to print all objects, we would enter an infinite loop. */
237 display_error_handler (struct display_error_handler_data
*data
,
240 SCM print_state
= scm_make_print_state ();
241 scm_puts ("\nException during displaying of ", data
->port
);
242 scm_puts (data
->mode
, data
->port
);
243 scm_puts (": ", data
->port
);
244 scm_iprin1 (tag
, data
->port
, SCM_PRINT_STATE (print_state
));
245 scm_putc ('\n', data
->port
);
246 return SCM_UNSPECIFIED
;
249 SCM_DEFINE(scm_display_error
, "display-error", 6, 0, 0,
250 (SCM stack
, SCM port
, SCM subr
, SCM message
, SCM args
, SCM rest
),
252 #define FUNC_NAME s_scm_display_error
254 struct display_error_args a
;
255 struct display_error_handler_data data
;
264 scm_internal_catch (SCM_BOOL_T
,
265 (scm_catch_body_t
) display_error_body
, &a
,
266 (scm_catch_handler_t
) display_error_handler
, &data
);
267 return SCM_UNSPECIFIED
;
276 static int n_print_params
= 9;
277 static print_params_t default_print_params
[] = {
281 { 1, 4 }, { 1, 3 }, { 1, 2 }
283 static print_params_t
*print_params
= default_print_params
;
286 SCM_DEFINE(scm_set_print_params_x
, "set-print-params!", 1, 0, 0,
289 #define FUNC_NAME s_scm_set_print_params_x
291 int i
, n
= scm_ilength (params
);
293 print_params_t
*new_params
;
294 SCM_ASSERT (n
>= 1, params
, SCM_ARG2
, FUNC_NAME
);
295 for (ls
= params
; SCM_NIMP (ls
); ls
= SCM_CDR (ls
))
296 SCM_ASSERT (scm_ilength (SCM_CAR (params
)) == 2
297 && SCM_INUMP (SCM_CAAR (ls
))
298 && SCM_INUM (SCM_CAAR (ls
)) >= 0
299 && SCM_INUMP (SCM_CADAR (ls
))
300 && SCM_INUM (SCM_CADAR (ls
)) >= 0,
303 s_scm_set_print_params_x
);
304 new_params
= scm_must_malloc (n
* sizeof (print_params_t
),
306 if (print_params
!= default_print_params
)
307 scm_must_free (print_params
);
308 print_params
= new_params
;
309 for (i
= 0; i
< n
; ++i
)
311 print_params
[i
].level
= SCM_INUM (SCM_CAAR (params
));
312 print_params
[i
].length
= SCM_INUM (SCM_CADAR (params
));
313 params
= SCM_CDR (params
);
316 return SCM_UNSPECIFIED
;
322 indent (int n
, SCM port
)
325 for (i
= 0; i
< n
; ++i
)
326 scm_putc (' ', port
);
330 display_frame_expr (char *hdr
,SCM exp
,char *tlr
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
334 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (sport
);
337 pstate
->length
= print_params
[i
].length
;
338 ptob
->seek (sport
, 0, SEEK_SET
);
341 pstate
->level
= print_params
[i
].level
- 1;
342 scm_iprlist (hdr
, exp
, tlr
[0], sport
, pstate
);
343 scm_puts (&tlr
[1], sport
);
347 pstate
->level
= print_params
[i
].level
;
348 scm_iprin1 (exp
, sport
, pstate
);
351 n
= ptob
->seek (sport
, 0, SEEK_CUR
);
354 while (indentation
+ n
> SCM_BACKTRACE_WIDTH
&& i
< n_print_params
);
355 ptob
->truncate (sport
, n
);
356 string
= scm_strport_to_string (sport
);
357 /* Remove control characters */
358 for (i
= 0; i
< n
; ++i
)
359 if (iscntrl (SCM_CHARS (string
)[i
]))
360 SCM_CHARS (string
)[i
] = ' ';
362 if (indentation
+ n
> SCM_BACKTRACE_WIDTH
)
364 n
= SCM_BACKTRACE_WIDTH
- indentation
;
365 SCM_CHARS (string
)[n
- 1] = '$';
368 scm_lfwrite (SCM_CHARS (string
), n
, port
);
372 display_application (SCM frame
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
374 SCM proc
= SCM_FRAME_PROC (frame
);
375 SCM name
= (SCM_NFALSEP (scm_procedure_p (proc
))
376 ? scm_procedure_name (proc
)
378 display_frame_expr ("[",
379 scm_cons (SCM_NFALSEP (name
) ? name
: proc
,
380 SCM_FRAME_ARGS (frame
)),
381 SCM_FRAME_EVAL_ARGS_P (frame
) ? " ..." : "]",
388 SCM_DEFINE(scm_display_application
, "display-application", 1, 2, 0,
389 (SCM frame
, SCM port
, SCM indent
),
391 #define FUNC_NAME s_scm_display_application
393 SCM_VALIDATE_FRAME(1,frame
);
394 if (SCM_UNBNDP (port
))
397 SCM_VALIDATE_OPOUTPORT(2,port
);
398 if (SCM_UNBNDP (indent
))
401 SCM_VALIDATE_INUM(3,indent
);
403 if (SCM_FRAME_PROC_P (frame
))
404 /* Display an application. */
406 SCM sport
, print_state
;
407 scm_print_state
*pstate
;
409 /* Create a string port used for adaptation of printing parameters. */
410 sport
= scm_mkstrport (SCM_INUM0
,
411 scm_make_string (SCM_MAKINUM (240),
416 /* Create a print state for printing of frames. */
417 print_state
= scm_make_print_state ();
418 pstate
= SCM_PRINT_STATE (print_state
);
419 pstate
->writingp
= 1;
422 display_application (frame
, SCM_INUM (indent
), sport
, port
, pstate
);
431 display_frame (SCM frame
,int nfield
,int indentation
,SCM sport
,SCM port
,scm_print_state
*pstate
)
435 /* Announce missing frames? */
436 if (!SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
438 indent (nfield
+ 1 + indentation
, port
);
439 scm_puts ("...\n", port
);
442 /* Check size of frame number. */
443 n
= SCM_FRAME_NUMBER (frame
);
444 for (i
= 0, j
= n
; j
> 0; ++i
) j
/= 10;
446 /* Number indentation. */
447 indent (nfield
- (i
? i
: 1), port
);
450 scm_iprin1 (SCM_MAKINUM (n
), port
, pstate
);
452 /* Real frame marker */
453 scm_putc (SCM_FRAME_REAL_P (frame
) ? '*' : ' ', port
);
456 indent (indentation
, port
);
458 if (SCM_FRAME_PROC_P (frame
))
459 /* Display an application. */
460 display_application (frame
, nfield
+ 1 + indentation
, sport
, port
, pstate
);
462 /* Display a special form. */
464 SCM source
= SCM_FRAME_SOURCE (frame
);
465 SCM copy
= (SCM_CONSP (source
)
466 ? scm_source_property (source
, scm_sym_copy
)
468 SCM umcopy
= (SCM_MEMOIZEDP (source
)
469 ? scm_unmemoize (source
)
471 display_frame_expr ("(",
472 SCM_CONSP (copy
) ? copy
: umcopy
,
474 nfield
+ 1 + indentation
,
479 scm_putc ('\n', port
);
481 /* Announce missing frames? */
482 if (SCM_BACKWARDS_P
&& SCM_FRAME_OVERFLOW_P (frame
))
484 indent (nfield
+ 1 + indentation
, port
);
485 scm_puts ("...\n", port
);
489 struct display_backtrace_args
{
497 display_backtrace_body(struct display_backtrace_args
*a
)
498 #define FUNC_NAME "display_backtrace_body"
500 int n_frames
, beg
, end
, n
, i
, j
;
501 int nfield
, indent_p
, indentation
;
502 SCM frame
, sport
, print_state
;
503 scm_print_state
*pstate
;
505 a
->port
= SCM_COERCE_OUTPORT (a
->port
);
507 /* Argument checking and extraction. */
508 SCM_ASSERT (SCM_STACKP (a
->stack
),
511 s_display_backtrace
);
512 SCM_ASSERT (SCM_OPOUTPORTP (a
->port
),
515 s_display_backtrace
);
516 n_frames
= SCM_INUM (scm_stack_length (a
->stack
));
517 n
= SCM_INUMP (a
->depth
) ? SCM_INUM (a
->depth
) : SCM_BACKTRACE_DEPTH
;
520 beg
= SCM_INUMP (a
->first
) ? SCM_INUM (a
->first
) : 0;
528 if (SCM_INUMP (a
->first
))
530 beg
= SCM_INUM (a
->first
);
544 SCM_ASSERT (beg
>= 0 && beg
< n_frames
, a
->first
, SCM_ARG3
, s_display_backtrace
);
545 SCM_ASSERT (n
> 0, a
->depth
, SCM_ARG4
, s_display_backtrace
);
547 /* Create a string port used for adaptation of printing parameters. */
548 sport
= scm_mkstrport (SCM_INUM0
,
549 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED
),
553 /* Create a print state for printing of frames. */
554 print_state
= scm_make_print_state ();
555 pstate
= SCM_PRINT_STATE (print_state
);
556 pstate
->writingp
= 1;
559 /* First find out if it's reasonable to do indentation. */
565 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
566 for (i
= 0, j
= 0; i
< n
; ++i
)
568 if (SCM_FRAME_REAL_P (frame
))
570 if (j
> SCM_BACKTRACE_INDENT
)
575 frame
= (SCM_BACKWARDS_P
576 ? SCM_FRAME_PREV (frame
)
577 : SCM_FRAME_NEXT (frame
));
581 /* Determine size of frame number field. */
582 j
= SCM_FRAME_NUMBER (scm_stack_ref (a
->stack
, SCM_MAKINUM (end
)));
583 for (i
= 0; j
> 0; ++i
) j
/= 10;
587 frame
= scm_stack_ref (a
->stack
, SCM_MAKINUM (beg
));
589 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
590 for (i
= 1; i
< n
; ++i
)
592 if (indent_p
&& SCM_FRAME_EVAL_ARGS_P (frame
))
594 frame
= SCM_BACKWARDS_P
? SCM_FRAME_PREV (frame
) : SCM_FRAME_NEXT (frame
);
595 display_frame (frame
, nfield
, indentation
, sport
, a
->port
, pstate
);
598 return SCM_UNSPECIFIED
;
602 SCM_DEFINE(scm_display_backtrace
, "display-backtrace", 2, 2, 0,
603 (SCM stack
, SCM port
, SCM first
, SCM depth
),
605 #define FUNC_NAME s_scm_display_backtrace
607 struct display_backtrace_args a
;
608 struct display_error_handler_data data
;
613 data
.mode
= "backtrace";
615 scm_internal_catch (SCM_BOOL_T
,
616 (scm_catch_body_t
) display_backtrace_body
, &a
,
617 (scm_catch_handler_t
) display_error_handler
, &data
);
618 return SCM_UNSPECIFIED
;
622 SCM_VCELL (scm_has_shown_backtrace_hint_p_var
, "has-shown-backtrace-hint?");
624 SCM_DEFINE(scm_backtrace
, "backtrace", 0, 0, 0,
627 #define FUNC_NAME s_scm_backtrace
629 SCM the_last_stack
= scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid
));
630 if (SCM_NFALSEP (the_last_stack
))
632 scm_newline (scm_cur_outp
);
633 scm_puts ("Backtrace:\n", scm_cur_outp
);
634 scm_display_backtrace (the_last_stack
,
638 scm_newline (scm_cur_outp
);
639 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var
))
642 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
644 "automatically if an error occurs in the future.\n",
646 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var
, SCM_BOOL_T
);
651 scm_puts ("No backtrace available.\n", scm_cur_outp
);
653 return SCM_UNSPECIFIED
;
660 scm_init_backtrace ()
662 SCM f
= scm_make_fluid ();
663 scm_the_last_stack_fluid
= scm_sysintern ("the-last-stack", f
);
665 #include "backtrace.x"