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