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