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