2002-07-20 Han-Wen <hanwen@cs.uu.nl>
[bpt/guile.git] / libguile / backtrace.c
1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation
3 *
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)
7 * any later version.
8 *
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.
13 *
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
18 *
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
21 *
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.
27 *
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
30 *
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.
38 *
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.
42 *
43 * The author can be reached at djurfeldt@nada.kth.se
44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
45
46
47
48 #include <stdio.h>
49 #include <ctype.h>
50
51 #include "libguile/_scm.h"
52
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56 #ifdef HAVE_IO_H
57 #include <io.h>
58 #endif
59
60 #include "libguile/stacks.h"
61 #include "libguile/srcprop.h"
62 #include "libguile/struct.h"
63 #include "libguile/strports.h"
64 #include "libguile/throw.h"
65 #include "libguile/fluids.h"
66 #include "libguile/ports.h"
67 #include "libguile/strings.h"
68
69 #include "libguile/validate.h"
70 #include "libguile/lang.h"
71 #include "libguile/backtrace.h"
72 #include "libguile/filesys.h"
73
74 /* {Error reporting and backtraces}
75 * (A first approximation.)
76 *
77 * Note that these functions shouldn't generate errors themselves.
78 */
79
80 #undef SCM_ASSERT
81 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
82 if (!(_cond)) \
83 return SCM_BOOL_F;
84
85 SCM scm_the_last_stack_fluid_var;
86
87 static void
88 display_header (SCM source, SCM port)
89 {
90 if (SCM_MEMOIZEDP (source))
91 {
92 SCM fname = scm_source_property (source, scm_sym_filename);
93 SCM line = scm_source_property (source, scm_sym_line);
94 SCM col = scm_source_property (source, scm_sym_column);
95
96 /* Dirk:FIXME:: Maybe we should store the _port_ rather than the
97 * filename with the source properties? Then we could in case of
98 * non-file ports give at least some more details than just
99 * "<unnamed port>". */
100 if (SCM_STRINGP (fname))
101 scm_prin1 (fname, port, 0);
102 else
103 scm_puts ("<unnamed port>", port);
104
105 if (!SCM_FALSEP (line) && !SCM_FALSEP (col))
106 {
107 scm_putc (':', port);
108 scm_intprint (SCM_INUM (line) + 1, 10, port);
109 scm_putc (':', port);
110 scm_intprint (SCM_INUM (col) + 1, 10, port);
111 }
112 }
113 else
114 scm_puts ("ERROR", port);
115 scm_puts (": ", port);
116 }
117
118
119 void
120 scm_display_error_message (SCM message, SCM args, SCM port)
121 {
122 if (SCM_STRINGP (message) && !SCM_FALSEP (scm_list_p (args)))
123 {
124 scm_simple_format (port, message, args);
125 scm_newline (port);
126 }
127 else
128 {
129 scm_display (message, port);
130 scm_newline (port);
131 }
132 }
133
134 static void
135 display_expression (SCM frame, SCM pname, SCM source, SCM port)
136 {
137 SCM print_state = scm_make_print_state ();
138 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
139 pstate->writingp = 0;
140 pstate->fancyp = 1;
141 pstate->level = 2;
142 pstate->length = 3;
143 if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname))
144 {
145 if (SCM_FRAMEP (frame)
146 && SCM_FRAME_EVAL_ARGS_P (frame))
147 scm_puts ("While evaluating arguments to ", port);
148 else
149 scm_puts ("In procedure ", port);
150 scm_iprin1 (pname, port, pstate);
151 if (SCM_MEMOIZEDP (source))
152 {
153 scm_puts (" in expression ", port);
154 pstate->writingp = 1;
155 scm_iprin1 (scm_unmemoize (source), port, pstate);
156 }
157 }
158 else if (SCM_MEMOIZEDP (source))
159 {
160 scm_puts ("In expression ", port);
161 pstate->writingp = 1;
162 scm_iprin1 (scm_unmemoize (source), port, pstate);
163 }
164 scm_puts (":\n", port);
165 scm_free_print_state (print_state);
166 }
167
168 struct display_error_args {
169 SCM stack;
170 SCM port;
171 SCM subr;
172 SCM message;
173 SCM args;
174 SCM rest;
175 };
176
177 static SCM
178 display_error_body (struct display_error_args *a)
179 {
180 SCM current_frame = SCM_BOOL_F;
181 SCM source = SCM_BOOL_F;
182 SCM prev_frame = SCM_BOOL_F;
183 SCM pname = a->subr;
184
185 if (SCM_DEBUGGINGP
186 && SCM_STACKP (a->stack)
187 && SCM_STACK_LENGTH (a->stack) > 0)
188 {
189 current_frame = scm_stack_ref (a->stack, SCM_INUM0);
190 source = SCM_FRAME_SOURCE (current_frame);
191 prev_frame = SCM_FRAME_PREV (current_frame);
192 if (!SCM_MEMOIZEDP (source) && !SCM_FALSEP (prev_frame))
193 source = SCM_FRAME_SOURCE (prev_frame);
194 if (!SCM_SYMBOLP (pname) && !SCM_STRINGP (pname) && SCM_FRAME_PROC_P (current_frame)
195 && SCM_EQ_P (scm_procedure_p (SCM_FRAME_PROC (current_frame)), SCM_BOOL_T))
196 pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
197 }
198 if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname) || SCM_MEMOIZEDP (source))
199 {
200 display_header (source, a->port);
201 display_expression (current_frame, pname, source, a->port);
202 }
203 display_header (source, a->port);
204 scm_display_error_message (a->message, a->args, a->port);
205 return SCM_UNSPECIFIED;
206 }
207
208 struct display_error_handler_data {
209 char *mode;
210 SCM port;
211 };
212
213 /* This is the exception handler for error reporting routines.
214 Note that it is very important that this handler *doesn't* try to
215 print more than the error tag, since the error very probably is
216 caused by an erroneous print call-back routine. If we would
217 try to print all objects, we would enter an infinite loop. */
218 static SCM
219 display_error_handler (struct display_error_handler_data *data,
220 SCM tag, SCM args SCM_UNUSED)
221 {
222 SCM print_state = scm_make_print_state ();
223 scm_puts ("\nException during displaying of ", data->port);
224 scm_puts (data->mode, data->port);
225 scm_puts (": ", data->port);
226 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
227 scm_putc ('\n', data->port);
228 return SCM_UNSPECIFIED;
229 }
230
231
232 /* The function scm_i_display_error prints out a detailed error message. This
233 * function will be called directly within libguile to signal error messages.
234 * No parameter checks will be performed by scm_i_display_error. Thus, User
235 * code should rather use the function scm_display_error.
236 */
237 void
238 scm_i_display_error (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest)
239 {
240 struct display_error_args a;
241 struct display_error_handler_data data;
242 a.stack = stack;
243 a.port = port;
244 a.subr = subr;
245 a.message = message;
246 a.args = args;
247 a.rest = rest;
248 data.mode = "error";
249 data.port = port;
250 scm_internal_catch (SCM_BOOL_T,
251 (scm_t_catch_body) display_error_body, &a,
252 (scm_t_catch_handler) display_error_handler, &data);
253 }
254
255
256 SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
257 (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
258 "Display an error message to the output port @var{port}.\n"
259 "@var{stack} is the saved stack for the error, @var{subr} is\n"
260 "the name of the procedure in which the error occurred and\n"
261 "@var{message} is the actual error message, which may contain\n"
262 "formatting instructions. These will format the arguments in\n"
263 "the list @var{args} accordingly. @var{rest} is currently\n"
264 "ignored.")
265 #define FUNC_NAME s_scm_display_error
266 {
267 SCM_VALIDATE_OUTPUT_PORT (2, port);
268
269 scm_i_display_error (stack, port, subr, message, args, rest);
270
271 return SCM_UNSPECIFIED;
272 }
273 #undef FUNC_NAME
274
275
276 typedef struct {
277 int level;
278 int length;
279 } print_params_t;
280
281 static int n_print_params = 9;
282 static print_params_t default_print_params[] = {
283 { 4, 9 }, { 4, 3 },
284 { 3, 4 }, { 3, 3 },
285 { 2, 4 }, { 2, 3 },
286 { 1, 4 }, { 1, 3 }, { 1, 2 }
287 };
288 static print_params_t *print_params = default_print_params;
289
290 #ifdef GUILE_DEBUG
291 SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
292 (SCM params),
293 "Set the print parameters to the values from @var{params}.\n"
294 "@var{params} must be a list of two-element lists which must\n"
295 "hold two integer values.")
296 #define FUNC_NAME s_scm_set_print_params_x
297 {
298 int i;
299 int n;
300 SCM ls;
301 print_params_t *new_params;
302
303 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
304 for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
305 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
306 && SCM_INUMP (SCM_CAAR (ls))
307 && SCM_INUM (SCM_CAAR (ls)) >= 0
308 && SCM_INUMP (SCM_CADAR (ls))
309 && SCM_INUM (SCM_CADAR (ls)) >= 0,
310 params,
311 SCM_ARG2,
312 s_scm_set_print_params_x);
313 new_params = scm_malloc (n * sizeof (print_params_t));
314 if (print_params != default_print_params)
315 free (print_params);
316 print_params = new_params;
317 for (i = 0; i < n; ++i)
318 {
319 print_params[i].level = SCM_INUM (SCM_CAAR (params));
320 print_params[i].length = SCM_INUM (SCM_CADAR (params));
321 params = SCM_CDR (params);
322 }
323 n_print_params = n;
324 return SCM_UNSPECIFIED;
325 }
326 #undef FUNC_NAME
327 #endif
328
329 static void
330 indent (int n, SCM port)
331 {
332 int i;
333 for (i = 0; i < n; ++i)
334 scm_putc (' ', port);
335 }
336
337 static void
338 display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
339 {
340 SCM string;
341 int i = 0, n;
342 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
343 do
344 {
345 pstate->length = print_params[i].length;
346 ptob->seek (sport, 0, SEEK_SET);
347 if (SCM_CONSP (exp))
348 {
349 pstate->level = print_params[i].level - 1;
350 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
351 scm_puts (&tlr[1], sport);
352 }
353 else
354 {
355 pstate->level = print_params[i].level;
356 scm_iprin1 (exp, sport, pstate);
357 }
358 ptob->flush (sport);
359 n = ptob->seek (sport, 0, SEEK_CUR);
360 ++i;
361 }
362 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
363 ptob->truncate (sport, n);
364 string = scm_strport_to_string (sport);
365 /* Remove control characters */
366 for (i = 0; i < n; ++i)
367 if (iscntrl (SCM_STRING_CHARS (string)[i]))
368 SCM_STRING_CHARS (string)[i] = ' ';
369 /* Truncate */
370 if (indentation + n > SCM_BACKTRACE_WIDTH)
371 {
372 n = SCM_BACKTRACE_WIDTH - indentation;
373 SCM_STRING_CHARS (string)[n - 1] = '$';
374 }
375
376 scm_lfwrite (SCM_STRING_CHARS (string), n, port);
377 }
378
379 static void
380 display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
381 {
382 SCM proc = SCM_FRAME_PROC (frame);
383 SCM name = (!SCM_FALSEP (scm_procedure_p (proc))
384 ? scm_procedure_name (proc)
385 : SCM_BOOL_F);
386 display_frame_expr ("[",
387 scm_cons (!SCM_FALSEP (name) ? name : proc,
388 SCM_FRAME_ARGS (frame)),
389 SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
390 indentation,
391 sport,
392 port,
393 pstate);
394 }
395
396 SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
397 (SCM frame, SCM port, SCM indent),
398 "Display a procedure application @var{frame} to the output port\n"
399 "@var{port}. @var{indent} specifies the indentation of the\n"
400 "output.")
401 #define FUNC_NAME s_scm_display_application
402 {
403 SCM_VALIDATE_FRAME (1, frame);
404 if (SCM_UNBNDP (port))
405 port = scm_cur_outp;
406 else
407 SCM_VALIDATE_OPOUTPORT (2, port);
408 if (SCM_UNBNDP (indent))
409 indent = SCM_INUM0;
410 else
411 SCM_VALIDATE_INUM (3, indent);
412
413 if (SCM_FRAME_PROC_P (frame))
414 /* Display an application. */
415 {
416 SCM sport, print_state;
417 scm_print_state *pstate;
418
419 /* Create a string port used for adaptation of printing parameters. */
420 sport = scm_mkstrport (SCM_INUM0,
421 scm_make_string (SCM_MAKINUM (240),
422 SCM_UNDEFINED),
423 SCM_OPN | SCM_WRTNG,
424 FUNC_NAME);
425
426 /* Create a print state for printing of frames. */
427 print_state = scm_make_print_state ();
428 pstate = SCM_PRINT_STATE (print_state);
429 pstate->writingp = 1;
430 pstate->fancyp = 1;
431
432 display_application (frame, SCM_INUM (indent), sport, port, pstate);
433 return SCM_BOOL_T;
434 }
435 else
436 return SCM_BOOL_F;
437 }
438 #undef FUNC_NAME
439
440 SCM_SYMBOL (sym_base, "base");
441
442 static void
443 display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
444 {
445 SCM source = SCM_FRAME_SOURCE (frame);
446 *file = SCM_MEMOIZEDP (source) ? scm_source_property (source, scm_sym_filename) : SCM_BOOL_F;
447 *line = (SCM_MEMOIZEDP (source)) ? scm_source_property (source, scm_sym_line) : SCM_BOOL_F;
448 }
449
450 static void
451 display_backtrace_file (frame, last_file, port, pstate)
452 SCM frame;
453 SCM *last_file;
454 SCM port;
455 scm_print_state *pstate;
456 {
457 SCM file, line;
458
459 display_backtrace_get_file_line (frame, &file, &line);
460
461 if (SCM_EQ_P (file, *last_file))
462 return;
463
464 *last_file = file;
465
466 scm_puts ("In ", port);
467 if (SCM_FALSEP (file))
468 if (SCM_FALSEP (line))
469 scm_puts ("unknown file", port);
470 else
471 scm_puts ("current input", port);
472 else
473 {
474 pstate->writingp = 0;
475 scm_iprin1 (file, port, pstate);
476 pstate->writingp = 1;
477 }
478 scm_puts (":\n", port);
479 }
480
481 static void
482 display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
483 {
484 SCM file, line;
485
486 display_backtrace_get_file_line (frame, &file, &line);
487
488 if (SCM_EQ_P (SCM_SHOW_FILE_NAME, sym_base))
489 {
490 if (SCM_FALSEP (file))
491 {
492 if (SCM_FALSEP (line))
493 scm_putc ('?', port);
494 else
495 scm_puts ("<stdin>", port);
496 }
497 else
498 {
499 pstate -> writingp = 0;
500 #ifdef HAVE_POSIX
501 scm_iprin1 (SCM_STRINGP (file) ? scm_basename (file, SCM_UNDEFINED) : file,
502 port, pstate);
503 #else
504 scm_iprin1 (file, port, pstate);
505 #endif
506 pstate -> writingp = 1;
507 }
508
509 scm_putc (':', port);
510 }
511 else if (!SCM_FALSEP (line))
512 {
513 int i, j=0;
514 for (i = SCM_INUM (line)+1; i > 0; i = i/10, j++)
515 ;
516 indent (4-j, port);
517 }
518
519 if (SCM_FALSEP (line))
520 scm_puts (" ?", port);
521 else
522 scm_intprint (SCM_INUM (line) + 1, 10, port);
523 scm_puts (": ", port);
524 }
525
526 static void
527 display_frame (SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate)
528 {
529 int n, i, j;
530
531 /* Announce missing frames? */
532 if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
533 {
534 indent (nfield + 1 + indentation, port);
535 scm_puts ("...\n", port);
536 }
537
538 /* display file name and line number */
539 if (!SCM_FALSEP (SCM_SHOW_FILE_NAME))
540 display_backtrace_file_and_line (frame, port, pstate);
541
542 /* Check size of frame number. */
543 n = SCM_FRAME_NUMBER (frame);
544 for (i = 0, j = n; j > 0; ++i) j /= 10;
545
546 /* Number indentation. */
547 indent (nfield - (i ? i : 1), port);
548
549 /* Frame number. */
550 scm_iprin1 (SCM_MAKINUM (n), port, pstate);
551
552 /* Real frame marker */
553 scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
554
555 /* Indentation. */
556 indent (indentation, port);
557
558 if (SCM_FRAME_PROC_P (frame))
559 /* Display an application. */
560 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
561 else
562 /* Display a special form. */
563 {
564 SCM source = SCM_FRAME_SOURCE (frame);
565 SCM copy = (SCM_CONSP (source)
566 ? scm_source_property (source, scm_sym_copy)
567 : SCM_BOOL_F);
568 SCM umcopy = (SCM_MEMOIZEDP (source)
569 ? scm_unmemoize (source)
570 : SCM_BOOL_F);
571 display_frame_expr ("(",
572 SCM_CONSP (copy) ? copy : umcopy,
573 ")",
574 nfield + 1 + indentation,
575 sport,
576 port,
577 pstate);
578 }
579 scm_putc ('\n', port);
580
581 /* Announce missing frames? */
582 if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
583 {
584 indent (nfield + 1 + indentation, port);
585 scm_puts ("...\n", port);
586 }
587 }
588
589 struct display_backtrace_args {
590 SCM stack;
591 SCM port;
592 SCM first;
593 SCM depth;
594 };
595
596 static SCM
597 display_backtrace_body (struct display_backtrace_args *a)
598 #define FUNC_NAME "display_backtrace_body"
599 {
600 int n_frames, beg, end, n, i, j;
601 int nfield, indent_p, indentation;
602 SCM frame, sport, print_state;
603 SCM last_file;
604 scm_print_state *pstate;
605
606 a->port = SCM_COERCE_OUTPORT (a->port);
607
608 /* Argument checking and extraction. */
609 SCM_VALIDATE_STACK (1, a->stack);
610 SCM_VALIDATE_OPOUTPORT (2, a->port);
611 n_frames = SCM_INUM (scm_stack_length (a->stack));
612 n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
613 if (SCM_BACKWARDS_P)
614 {
615 beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
616 end = beg + n - 1;
617 if (end >= n_frames)
618 end = n_frames - 1;
619 n = end - beg + 1;
620 }
621 else
622 {
623 if (SCM_INUMP (a->first))
624 {
625 beg = SCM_INUM (a->first);
626 end = beg - n + 1;
627 if (end < 0)
628 end = 0;
629 }
630 else
631 {
632 beg = n - 1;
633 end = 0;
634 if (beg >= n_frames)
635 beg = n_frames - 1;
636 }
637 n = beg - end + 1;
638 }
639 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
640 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
641
642 /* Create a string port used for adaptation of printing parameters. */
643 sport = scm_mkstrport (SCM_INUM0,
644 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
645 SCM_OPN | SCM_WRTNG,
646 FUNC_NAME);
647
648 /* Create a print state for printing of frames. */
649 print_state = scm_make_print_state ();
650 pstate = SCM_PRINT_STATE (print_state);
651 pstate->writingp = 1;
652 pstate->fancyp = 1;
653
654 /* First find out if it's reasonable to do indentation. */
655 if (SCM_BACKWARDS_P)
656 indent_p = 0;
657 else
658 {
659 unsigned int j;
660
661 indent_p = 1;
662 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
663 for (i = 0, j = 0; i < n; ++i)
664 {
665 if (SCM_FRAME_REAL_P (frame))
666 ++j;
667 if (j > SCM_BACKTRACE_INDENT)
668 {
669 indent_p = 0;
670 break;
671 }
672 frame = (SCM_BACKWARDS_P
673 ? SCM_FRAME_PREV (frame)
674 : SCM_FRAME_NEXT (frame));
675 }
676 }
677
678 /* Determine size of frame number field. */
679 j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
680 for (i = 0; j > 0; ++i) j /= 10;
681 nfield = i ? i : 1;
682
683 /* Print frames. */
684 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
685 indentation = 1;
686 last_file = SCM_UNDEFINED;
687 for (i = 0; i < n; ++i)
688 {
689 if (!SCM_EQ_P (SCM_SHOW_FILE_NAME, sym_base))
690 display_backtrace_file (frame, &last_file, a->port, pstate);
691
692 display_frame (frame, nfield, indentation, sport, a->port, pstate);
693 if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
694 ++indentation;
695 frame = (SCM_BACKWARDS_P ?
696 SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame));
697 }
698
699 scm_remember_upto_here_1 (print_state);
700
701 return SCM_UNSPECIFIED;
702 }
703 #undef FUNC_NAME
704
705 SCM_DEFINE (scm_display_backtrace, "display-backtrace", 2, 2, 0,
706 (SCM stack, SCM port, SCM first, SCM depth),
707 "Display a backtrace to the output port @var{port}. @var{stack}\n"
708 "is the stack to take the backtrace from, @var{first} specifies\n"
709 "where in the stack to start and @var{depth} how much frames\n"
710 "to display. Both @var{first} and @var{depth} can be @code{#f},\n"
711 "which means that default values will be used.")
712 #define FUNC_NAME s_scm_display_backtrace
713 {
714 struct display_backtrace_args a;
715 struct display_error_handler_data data;
716 a.stack = stack;
717 a.port = port;
718 a.first = first;
719 a.depth = depth;
720 data.mode = "backtrace";
721 data.port = port;
722 scm_internal_catch (SCM_BOOL_T,
723 (scm_t_catch_body) display_backtrace_body, &a,
724 (scm_t_catch_handler) display_error_handler, &data);
725 return SCM_UNSPECIFIED;
726 }
727 #undef FUNC_NAME
728
729 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
730
731 SCM_DEFINE (scm_backtrace, "backtrace", 0, 0, 0,
732 (),
733 "Display a backtrace of the stack saved by the last error\n"
734 "to the current output port.")
735 #define FUNC_NAME s_scm_backtrace
736 {
737 SCM the_last_stack =
738 scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var));
739 if (!SCM_FALSEP (the_last_stack))
740 {
741 scm_newline (scm_cur_outp);
742 scm_puts ("Backtrace:\n", scm_cur_outp);
743 scm_display_backtrace (the_last_stack,
744 scm_cur_outp,
745 SCM_UNDEFINED,
746 SCM_UNDEFINED);
747 scm_newline (scm_cur_outp);
748 if (SCM_FALSEP (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var))
749 && !SCM_BACKTRACE_P)
750 {
751 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
752 "a backtrace\n"
753 "automatically if an error occurs in the future.\n",
754 scm_cur_outp);
755 SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
756 }
757 }
758 else
759 {
760 scm_puts ("No backtrace available.\n", scm_cur_outp);
761 }
762 return SCM_UNSPECIFIED;
763 }
764 #undef FUNC_NAME
765
766 \f
767
768 void
769 scm_init_backtrace ()
770 {
771 SCM f = scm_make_fluid ();
772 scm_the_last_stack_fluid_var = scm_c_define ("the-last-stack", f);
773
774 #include "libguile/backtrace.x"
775 }
776
777 /*
778 Local Variables:
779 c-file-style: "gnu"
780 End:
781 */