build: Don't include <config.h> in native programs when cross-compiling.
[bpt/guile.git] / libguile / backtrace.c
index 2eb4f2d..f8283ab 100644 (file)
@@ -1,22 +1,24 @@
 /* Printing of backtraces and error messages
- * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004 Free Software Foundation
+ * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009,
+ *   2010, 2011, 2014 Free Software Foundation
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
 
 #include "libguile/_scm.h"
 
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 #ifdef HAVE_IO_H
 #include <io.h>
 #endif
 
+#include "libguile/deprecation.h"
 #include "libguile/stacks.h"
 #include "libguile/srcprop.h"
 #include "libguile/struct.h"
 #include "libguile/ports.h"
 #include "libguile/strings.h"
 #include "libguile/dynwind.h"
+#include "libguile/frames.h"
 
 #include "libguile/validate.h"
-#include "libguile/lang.h"
 #include "libguile/backtrace.h"
 #include "libguile/filesys.h"
+#include "libguile/private-options.h"
 
 /* {Error reporting and backtraces}
  *
  * Note that these functions shouldn't generate errors themselves.
  */
 
+static SCM
+boot_print_exception (SCM port, SCM frame, SCM key, SCM args)
+#define FUNC_NAME "boot-print-exception"
+{
+  scm_puts ("Throw to key ", port);
+  scm_write (key, port);
+  scm_puts (" with args ", port);
+  scm_write (args, port);
+  return SCM_UNSPECIFIED;
+}
+#undef FUNC_NAME
+
+static SCM print_exception_var;
+
+static void
+init_print_exception_var (void)
+{
+  print_exception_var
+    = scm_module_variable (scm_the_root_module (),
+                           scm_from_latin1_symbol ("print-exception"));
+}
+
+SCM
+scm_print_exception (SCM port, SCM frame, SCM key, SCM args)
+#define FUNC_NAME "print-exception"
+{
+  static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
+  scm_i_pthread_once (&once, init_print_exception_var);
+
+  SCM_VALIDATE_OPOUTPORT (1, port);
+  if (scm_is_true (frame))
+    SCM_VALIDATE_FRAME (2, frame);
+  SCM_VALIDATE_SYMBOL (3, key);
+  SCM_VALIDATE_LIST (4, args);
+
+  return scm_call_4 (scm_variable_ref (print_exception_var),
+                     port, frame, key, args);
+}
+#undef FUNC_NAME
+
+
+\f
+
 /* Print parameters for error messages. */
 
 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL   7
        if (!(_cond)) \
           return SCM_BOOL_F;
 
-SCM scm_the_last_stack_fluid_var;
-
-static void
-display_header (SCM source, SCM port)
-{
-  if (SCM_MEMOIZEDP (source))
-    {
-      SCM fname = scm_source_property (source, scm_sym_filename);
-      SCM line = scm_source_property (source, scm_sym_line);
-      SCM col = scm_source_property (source, scm_sym_column);
-
-      /* Dirk:FIXME:: Maybe we should store the _port_ rather than the
-       * filename with the source properties?  Then we could in case of
-       * non-file ports give at least some more details than just
-       * "<unnamed port>". */
-      if (SCM_STRINGP (fname))
-       scm_prin1 (fname, port, 0);
-      else
-       scm_puts ("<unnamed port>", port);
-
-      if (!SCM_FALSEP (line) && !SCM_FALSEP (col))
-       {
-         scm_putc (':', port);
-         scm_intprint (SCM_INUM (line) + 1, 10, port);
-         scm_putc (':', port);
-         scm_intprint (SCM_INUM (col) + 1, 10, port);
-       }
-    }
-  else
-    scm_puts ("ERROR", port);
-  scm_puts (": ", port);
-}
-
-
-struct display_error_message_data {
-  SCM message;
-  SCM args;
-  SCM port;
-  scm_print_state *pstate;
-  int old_fancyp;
-  int old_level;
-  int old_length;
-};
-
-static SCM
-display_error_message (struct display_error_message_data *d)
-{
-  if (SCM_STRINGP (d->message) && !SCM_FALSEP (scm_list_p (d->args)))
-    scm_simple_format (d->port, d->message, d->args);
-  else
-    scm_display (d->message, d->port);
-  scm_newline (d->port);
-  return SCM_UNSPECIFIED;
-}
-
-static void
-before_display_error_message (struct display_error_message_data *d)
-{
-  scm_print_state *pstate = d->pstate;
-  d->old_fancyp = pstate->fancyp;
-  d->old_level  = pstate->level;
-  d->old_length = pstate->length;
-  pstate->fancyp = 1;
-  pstate->level  = DISPLAY_ERROR_MESSAGE_MAX_LEVEL;
-  pstate->length = DISPLAY_ERROR_MESSAGE_MAX_LENGTH;
-}
-
-static void
-after_display_error_message (struct display_error_message_data *d)
-{
-  scm_print_state *pstate = d->pstate;
-  pstate->fancyp = d->old_fancyp;
-  pstate->level  = d->old_level;
-  pstate->length = d->old_length;
-}
 
 void
 scm_display_error_message (SCM message, SCM args, SCM port)
 {
-  struct display_error_message_data d;
-  SCM print_state;
-  scm_print_state *pstate;
-
-  port = scm_i_port_with_print_state (port, SCM_UNDEFINED);
-  print_state = SCM_PORT_WITH_PS_PS (port);
-  pstate = SCM_PRINT_STATE (print_state);
-  
-  d.message = message;
-  d.args = args;
-  d.port = port;
-  d.pstate = pstate;
-  scm_internal_dynamic_wind ((scm_t_guard) before_display_error_message,
-                            (scm_t_inner) display_error_message,
-                            (scm_t_guard) after_display_error_message,
-                            &d,
-                            &d);
-}
-
-static void
-display_expression (SCM frame, SCM pname, SCM source, SCM port)
-{
-  SCM print_state = scm_make_print_state ();
-  scm_print_state *pstate = SCM_PRINT_STATE (print_state);
-  pstate->writingp = 0;
-  pstate->fancyp = 1;
-  pstate->level  = DISPLAY_EXPRESSION_MAX_LEVEL;
-  pstate->length = DISPLAY_EXPRESSION_MAX_LENGTH;
-  if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname))
-    {
-      if (SCM_FRAMEP (frame)
-         && SCM_FRAME_EVAL_ARGS_P (frame))
-       scm_puts ("While evaluating arguments to ", port);
-      else
-       scm_puts ("In procedure ", port);
-      scm_iprin1 (pname, port, pstate);
-      if (SCM_MEMOIZEDP (source))
-       {
-         scm_puts (" in expression ", port);
-         pstate->writingp = 1;
-         scm_iprin1 (scm_unmemoize (source), port, pstate);
-       }
-    }
-  else if (SCM_MEMOIZEDP (source))
-    {
-      scm_puts ("In expression ", port);
-      pstate->writingp = 1;
-      scm_iprin1 (scm_unmemoize (source), port, pstate);
-    }
-  scm_puts (":\n", port);
-  scm_free_print_state (print_state);
-}
-
-struct display_error_args {
-  SCM stack;
-  SCM port;
-  SCM subr;
-  SCM message;
-  SCM args;
-  SCM rest;
-};
-
-static SCM
-display_error_body (struct display_error_args *a)
-{
-  SCM current_frame = SCM_BOOL_F;
-  SCM source = SCM_BOOL_F;
-  SCM prev_frame = SCM_BOOL_F;
-  SCM pname = a->subr;
-
-  if (scm_debug_mode_p
-      && SCM_STACKP (a->stack)
-      && SCM_STACK_LENGTH (a->stack) > 0)
-    {
-      current_frame = scm_stack_ref (a->stack, SCM_INUM0);
-      source = SCM_FRAME_SOURCE (current_frame);
-      prev_frame = SCM_FRAME_PREV (current_frame);
-      if (!SCM_MEMOIZEDP (source) && !SCM_FALSEP (prev_frame))
-       source = SCM_FRAME_SOURCE (prev_frame);
-      if (!SCM_SYMBOLP (pname) && !SCM_STRINGP (pname) && SCM_FRAME_PROC_P (current_frame)
-         && SCM_EQ_P (scm_procedure_p (SCM_FRAME_PROC (current_frame)), SCM_BOOL_T))
-       pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
-    }
-  if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname) || SCM_MEMOIZEDP (source))
-    {
-      display_header (source, a->port);
-      display_expression (current_frame, pname, source, a->port);
-    }
-  display_header (source, a->port);
-  scm_display_error_message (a->message, a->args, a->port);
-  return SCM_UNSPECIFIED;
-}
-
-struct display_error_handler_data {
-  char *mode;
-  SCM port;
-};
-
-/* This is the exception handler for error reporting routines.
-   Note that it is very important that this handler *doesn't* try to
-   print more than the error tag, since the error very probably is
-   caused by an erroneous print call-back routine.  If we would
-   try to print all objects, we would enter an infinite loop. */
-static SCM
-display_error_handler (struct display_error_handler_data *data,
-                      SCM tag, SCM args SCM_UNUSED)
-{
-  SCM print_state = scm_make_print_state ();
-  scm_puts ("\nException during displaying of ", data->port);
-  scm_puts (data->mode, data->port);
-  scm_puts (": ", data->port);
-  scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
-  scm_putc ('\n', data->port);
-  return SCM_UNSPECIFIED;
+  scm_print_exception (port, SCM_BOOL_F, scm_misc_error_key,
+                       scm_list_3 (SCM_BOOL_F, message, args));
 }
 
 
@@ -271,28 +129,17 @@ display_error_handler (struct display_error_handler_data *data,
  * code should rather use the function scm_display_error.
  */
 void
-scm_i_display_error (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest)
+scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest)
 {
-  struct display_error_args a;
-  struct display_error_handler_data data;
-  a.stack = stack;
-  a.port  = port;
-  a.subr  = subr;
-  a.message = message;
-  a.args  = args;
-  a.rest  = rest;
-  data.mode = "error";
-  data.port = port;
-  scm_internal_catch (SCM_BOOL_T,
-                     (scm_t_catch_body) display_error_body, &a,
-                     (scm_t_catch_handler) display_error_handler, &data);
+  scm_print_exception (port, frame, scm_misc_error_key,
+                       scm_list_3 (subr, message, args));
 }
 
 
 SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
-           (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
+           (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest),
            "Display an error message to the output port @var{port}.\n"
-           "@var{stack} is the saved stack for the error, @var{subr} is\n"
+           "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
            "the name of the procedure in which the error occurred and\n"
            "@var{message} is the actual error message, which may contain\n"
            "formatting instructions. These will format the arguments in\n"
@@ -302,7 +149,20 @@ SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
 {
   SCM_VALIDATE_OUTPUT_PORT (2, port);
 
-  scm_i_display_error (stack, port, subr, message, args, rest);
+#if SCM_ENABLE_DEPRECATED
+  if (SCM_STACKP (frame))
+    {
+      scm_c_issue_deprecation_warning
+        ("Passing a stack as the first argument to `scm_display_error' is "
+         "deprecated.  Pass a frame instead.");
+      if (SCM_STACK_LENGTH (frame))
+        frame = scm_stack_ref (frame, SCM_INUM0);
+      else
+        frame = SCM_BOOL_F;
+    }
+#endif
+
+  scm_i_display_error (frame, port, subr, message, args, rest);
 
   return SCM_UNSPECIFIED;
 }
@@ -339,10 +199,8 @@ SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
   SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
   for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
     SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
-               && SCM_INUMP (SCM_CAAR (ls))
-               && SCM_INUM (SCM_CAAR (ls)) >= 0
-               && SCM_INUMP (SCM_CADAR (ls))
-               && SCM_INUM (SCM_CADAR (ls)) >= 0,
+               && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX)
+               && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX),
                params,
                SCM_ARG2,
                s_scm_set_print_params_x);
@@ -352,8 +210,8 @@ SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
   print_params = new_params;
   for (i = 0; i < n; ++i)
     {
-      print_params[i].level = SCM_INUM (SCM_CAAR (params));
-      print_params[i].length = SCM_INUM (SCM_CADAR (params));
+      print_params[i].level = scm_to_int (SCM_CAAR (params));
+      print_params[i].length = scm_to_int (SCM_CADAR (params));
       params = SCM_CDR (params);
     }
   n_print_params = n;
@@ -373,14 +231,13 @@ indent (int n, SCM port)
 static void
 display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
 {
-  SCM string;
   int i = 0, n;
   scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
   do
     {
       pstate->length = print_params[i].length;
       ptob->seek (sport, 0, SEEK_SET);
-      if (SCM_CONSP (exp))
+      if (scm_is_pair (exp))
        {
          pstate->level = print_params[i].level - 1;
          scm_iprlist (hdr, exp, tlr[0], sport, pstate);
@@ -397,32 +254,21 @@ display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, S
     }
   while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
   ptob->truncate (sport, n);
-  string = scm_strport_to_string (sport);
-  /* Remove control characters */
-  for (i = 0; i < n; ++i)
-    if (iscntrl ((int) (unsigned char) SCM_STRING_CHARS (string)[i]))
-      SCM_STRING_CHARS (string)[i] = ' ';
-  /* Truncate */
-  if (indentation + n > SCM_BACKTRACE_WIDTH)
-    {
-      n = SCM_BACKTRACE_WIDTH - indentation;
-      SCM_STRING_CHARS (string)[n - 1] = '$';
-    }
       
-  scm_lfwrite (SCM_STRING_CHARS (string), n, port);
+  scm_display (scm_strport_to_string (sport), port);
 }
 
 static void
 display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
 {
-  SCM proc = SCM_FRAME_PROC (frame);
-  SCM name = (!SCM_FALSEP (scm_procedure_p (proc))
+  SCM proc = scm_frame_procedure (frame);
+  SCM name = (scm_is_true (scm_procedure_p (proc))
              ? scm_procedure_name (proc)
              : SCM_BOOL_F);
   display_frame_expr ("[",
-                     scm_cons (!SCM_FALSEP (name) ? name : proc,
-                               SCM_FRAME_ARGS (frame)),
-                     SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
+                     scm_cons (scm_is_true (name) ? name : proc,
+                               scm_frame_arguments (frame)),
+                     "]",
                      indentation,
                      sport,
                      port,
@@ -438,38 +284,31 @@ SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
 {
   SCM_VALIDATE_FRAME (1, frame);
   if (SCM_UNBNDP (port))
-    port = scm_cur_outp;
+    port = scm_current_output_port ();
   else
     SCM_VALIDATE_OPOUTPORT (2, port);
   if (SCM_UNBNDP (indent))
     indent = SCM_INUM0;
-  else
-    SCM_VALIDATE_INUM (3, indent);
   
-  if (SCM_FRAME_PROC_P (frame))
-    /* Display an application. */
-    {
-      SCM sport, print_state;
-      scm_print_state *pstate;
+  /* Display an application. */
+  {
+    SCM sport, print_state;
+    scm_print_state *pstate;
       
-      /* Create a string port used for adaptation of printing parameters. */
-      sport = scm_mkstrport (SCM_INUM0,
-                            scm_make_string (SCM_MAKINUM (240),
-                                             SCM_UNDEFINED),
-                            SCM_OPN | SCM_WRTNG,
-                            FUNC_NAME);
-
-      /* Create a print state for printing of frames. */
-      print_state = scm_make_print_state ();
-      pstate = SCM_PRINT_STATE (print_state);
-      pstate->writingp = 1;
-      pstate->fancyp = 1;
+    /* Create a string port used for adaptation of printing parameters. */
+    sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
+                           SCM_OPN | SCM_WRTNG,
+                           FUNC_NAME);
+
+    /* Create a print state for printing of frames. */
+    print_state = scm_make_print_state ();
+    pstate = SCM_PRINT_STATE (print_state);
+    pstate->writingp = 1;
+    pstate->fancyp = 1;
       
-      display_application (frame, SCM_INUM (indent), sport, port, pstate);
-      return SCM_BOOL_T;
-    }
-  else
-    return SCM_BOOL_F;
+    display_application (frame, scm_to_int (indent), sport, port, pstate);
+    return SCM_BOOL_T;
+  }
 }
 #undef FUNC_NAME
 
@@ -478,9 +317,17 @@ SCM_SYMBOL (sym_base, "base");
 static void
 display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
 {
-  SCM source = SCM_FRAME_SOURCE (frame);
-  *file = SCM_MEMOIZEDP (source) ? scm_source_property (source, scm_sym_filename) : SCM_BOOL_F;
-  *line = (SCM_MEMOIZEDP (source)) ? scm_source_property (source, scm_sym_line) : SCM_BOOL_F;
+  SCM source = scm_frame_source (frame);
+  *file = *line = SCM_BOOL_F;
+  if (scm_is_pair (source)
+      && scm_is_pair (scm_cdr (source))
+      && scm_is_pair (scm_cddr (source))
+      && !scm_is_pair (scm_cdddr (source)))
+    {
+      /* (addr . (filename . (line . column))), from vm compilation */
+      *file = scm_cadr (source);
+      *line = scm_caddr (source);
+    }
 }
 
 static void
@@ -494,14 +341,14 @@ display_backtrace_file (frame, last_file, port, pstate)
 
   display_backtrace_get_file_line (frame, &file, &line);
 
-  if (SCM_EQ_P (file, *last_file))
+  if (scm_is_true (scm_equal_p (file, *last_file)))
     return;
 
   *last_file = file;
 
   scm_puts ("In ", port);
-  if (SCM_FALSEP (file))
-    if (SCM_FALSEP (line))
+  if (scm_is_false (file))
+    if (scm_is_false (line))
       scm_puts ("unknown file", port);
     else
       scm_puts ("current input", port);
@@ -521,11 +368,11 @@ display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
 
   display_backtrace_get_file_line (frame, &file, &line);
 
-  if (SCM_EQ_P (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
+  if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
     {
-      if (SCM_FALSEP (file))
+      if (scm_is_false (file))
        {
-         if (SCM_FALSEP (line))
+         if (scm_is_false (line))
            scm_putc ('?', port);
          else
            scm_puts ("<stdin>", port);
@@ -534,7 +381,8 @@ display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
        {
          pstate -> writingp = 0;
 #ifdef HAVE_POSIX
-         scm_iprin1 (SCM_STRINGP (file) ? scm_basename (file, SCM_UNDEFINED) : file,
+         scm_iprin1 ((scm_is_string (file)?
+                      scm_basename (file, SCM_UNDEFINED) : file),
                      port, pstate);
 #else
          scm_iprin1 (file, port, pstate);
@@ -544,82 +392,46 @@ display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
 
       scm_putc (':', port);
     }
-  else if (!SCM_FALSEP (line))
+  else if (scm_is_true (line))
     {
       int i, j=0;
-      for (i = SCM_INUM (line)+1; i > 0; i = i/10, j++)
+      for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
        ;
       indent (4-j, port);
     }
 
-  if (SCM_FALSEP (line))
+  if (scm_is_false (line))
     scm_puts ("   ?", port);
   else
-    scm_intprint (SCM_INUM (line) + 1, 10, port);
+    scm_intprint (scm_to_int (line) + 1, 10, port);
   scm_puts (": ", port);
 }
 
 static void
-display_frame (SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate)
+display_frame (SCM frame, int n, int nfield, int indentation,
+               SCM sport, SCM port, scm_print_state *pstate)
 {
-  int n, i, j;
-
-  /* Announce missing frames? */
-  if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
-    {
-      indent (nfield + 1 + indentation, port);
-      scm_puts ("...\n", port);
-    }
+  int i, j;
 
   /* display file name and line number */
-  if (!SCM_FALSEP (SCM_PACK (SCM_SHOW_FILE_NAME)))
+  if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
     display_backtrace_file_and_line (frame, port, pstate);
 
   /* Check size of frame number. */
-  n = SCM_FRAME_NUMBER (frame);
   for (i = 0, j = n; j > 0; ++i) j /= 10;
 
   /* Number indentation. */
   indent (nfield - (i ? i : 1), port);
 
   /* Frame number. */
-  scm_iprin1 (SCM_MAKINUM (n), port, pstate);
-
-  /* Real frame marker */
-  scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
+  scm_iprin1 (scm_from_int (n), port, pstate);
 
   /* Indentation. */
   indent (indentation, port);
 
-  if (SCM_FRAME_PROC_P (frame))
-    /* Display an application. */
-    display_application (frame, nfield + 1 + indentation, sport, port, pstate);
-  else
-    /* Display a special form. */
-    {
-      SCM source = SCM_FRAME_SOURCE (frame);
-      SCM copy = (SCM_CONSP (source) 
-                 ? scm_source_property (source, scm_sym_copy)
-                 : SCM_BOOL_F);
-      SCM umcopy = (SCM_MEMOIZEDP (source)
-                   ? scm_unmemoize (source)
-                   : SCM_BOOL_F);
-      display_frame_expr ("(",
-                         SCM_CONSP (copy) ? copy : umcopy,
-                         ")",
-                         nfield + 1 + indentation,
-                         sport,
-                         port,
-                         pstate);
-    }
+  /* Display an application. */
+  display_application (frame, nfield + 1 + indentation, sport, port, pstate);
   scm_putc ('\n', port);
-
-  /* Announce missing frames? */
-  if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
-    {
-      indent (nfield + 1 + indentation, port);
-      scm_puts ("...\n", port);
-    }
 }
 
 struct display_backtrace_args {
@@ -627,6 +439,7 @@ struct display_backtrace_args {
   SCM port;
   SCM first;
   SCM depth;
+  SCM highlight_objects;
 };
 
 static SCM
@@ -634,7 +447,7 @@ display_backtrace_body (struct display_backtrace_args *a)
 #define FUNC_NAME "display_backtrace_body"
 {
   int n_frames, beg, end, n, i, j;
-  int nfield, indent_p, indentation;
+  int nfield, indentation;
   SCM frame, sport, print_state;
   SCM last_file;
   scm_print_state *pstate;
@@ -644,11 +457,11 @@ display_backtrace_body (struct display_backtrace_args *a)
   /* Argument checking and extraction. */
   SCM_VALIDATE_STACK (1, a->stack);
   SCM_VALIDATE_OPOUTPORT (2, a->port);
-  n_frames = SCM_INUM (scm_stack_length (a->stack));
-  n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
+  n_frames = scm_to_int (scm_stack_length (a->stack));
+  n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
   if (SCM_BACKWARDS_P)
     {
-      beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
+      beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
       end = beg + n - 1;
       if (end >= n_frames)
        end = n_frames - 1;
@@ -656,9 +469,9 @@ display_backtrace_body (struct display_backtrace_args *a)
     }
   else
     {
-      if (SCM_INUMP (a->first))
+      if (scm_is_integer (a->first))
        {
-         beg = SCM_INUM (a->first);
+         beg = scm_to_int (a->first);
          end = beg - n + 1;
          if (end < 0)
            end = 0;
@@ -676,8 +489,7 @@ display_backtrace_body (struct display_backtrace_args *a)
   SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
 
   /* Create a string port used for adaptation of printing parameters. */
-  sport = scm_mkstrport (SCM_INUM0,
-                        scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
+  sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
                         SCM_OPN | SCM_WRTNG,
                         FUNC_NAME);
 
@@ -686,50 +498,26 @@ display_backtrace_body (struct display_backtrace_args *a)
   pstate = SCM_PRINT_STATE (print_state);
   pstate->writingp = 1;
   pstate->fancyp = 1;
+  pstate->highlight_objects = a->highlight_objects;
 
-  /* First find out if it's reasonable to do indentation. */
-  if (SCM_BACKWARDS_P)
-    indent_p = 0;
-  else
-    {
-      unsigned int j;
-
-      indent_p = 1;
-      frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
-      for (i = 0, j = 0; i < n; ++i)
-       {
-         if (SCM_FRAME_REAL_P (frame))
-           ++j;
-         if (j > SCM_BACKTRACE_INDENT)
-           {
-             indent_p = 0;
-             break;
-           }
-         frame = (SCM_BACKWARDS_P
-                  ? SCM_FRAME_PREV (frame)
-                  : SCM_FRAME_NEXT (frame));
-       }
-    }
-  
   /* Determine size of frame number field. */
-  j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
+  j = end;
   for (i = 0; j > 0; ++i) j /= 10;
   nfield = i ? i : 1;
   
   /* Print frames. */
-  frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
   indentation = 1;
   last_file = SCM_UNDEFINED;
-  for (i = 0; i < n; ++i)
+  if (SCM_BACKWARDS_P)
+    end++;
+  else
+    end--;
+  for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i)
     {
-      if (!SCM_EQ_P (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
+      frame = scm_stack_ref (a->stack, scm_from_int (i));
+      if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
        display_backtrace_file (frame, &last_file, a->port, pstate);
-
-      display_frame (frame, nfield, indentation, sport, a->port, pstate);
-      if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
-       ++indentation;
-      frame = (SCM_BACKWARDS_P ? 
-              SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame));
+      display_frame (frame, i, nfield, indentation, sport, a->port, pstate);
     }
 
   scm_remember_upto_here_1 (print_state);
@@ -738,75 +526,93 @@ display_backtrace_body (struct display_backtrace_args *a)
 }
 #undef FUNC_NAME
 
-SCM_DEFINE (scm_display_backtrace, "display-backtrace", 2, 2, 0, 
-           (SCM stack, SCM port, SCM first, SCM depth),
-           "Display a backtrace to the output port @var{port}. @var{stack}\n"
+static SCM
+error_during_backtrace (void *data, SCM tag, SCM throw_args)
+{
+  SCM port = PTR2SCM (data);
+  
+  scm_puts ("Exception thrown while printing backtrace:\n", port);
+  scm_print_exception (port, SCM_BOOL_F, tag, throw_args);
+
+  return SCM_UNSPECIFIED;
+}
+
+
+SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0, 
+           (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
+           "Display a backtrace to the output port @var{port}.  @var{stack}\n"
            "is the stack to take the backtrace from, @var{first} specifies\n"
-           "where in the stack to start and @var{depth} how much frames\n"
-           "to display. Both @var{first} and @var{depth} can be @code{#f},\n"
-           "which means that default values will be used.")
-#define FUNC_NAME s_scm_display_backtrace
+           "where in the stack to start and @var{depth} how many frames\n"
+           "to display.  @var{first} and @var{depth} can be @code{#f},\n"
+           "which means that default values will be used.\n"
+           "If @var{highlights} is given it should be a list; the elements\n"
+           "of this list will be highlighted wherever they appear in the\n"
+           "backtrace.")
+#define FUNC_NAME s_scm_display_backtrace_with_highlights
 {
   struct display_backtrace_args a;
-  struct display_error_handler_data data;
   a.stack = stack;
   a.port  = port;
   a.first = first;
   a.depth = depth;
-  data.mode = "backtrace";
-  data.port = port;
+  if (SCM_UNBNDP (highlights))
+    a.highlight_objects = SCM_EOL;
+  else
+    a.highlight_objects = highlights;
+
   scm_internal_catch (SCM_BOOL_T,
                      (scm_t_catch_body) display_backtrace_body, &a,
-                     (scm_t_catch_handler) display_error_handler, &data);
+                     (scm_t_catch_handler) error_during_backtrace, SCM2PTR (port));
+
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
+SCM
+scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
+{
+  return scm_display_backtrace_with_highlights (stack, port, first, depth,
+                                               SCM_EOL);
+}
+
 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
 
-SCM_DEFINE (scm_backtrace, "backtrace", 0, 0, 0, 
-           (),
-           "Display a backtrace of the stack saved by the last error\n"
-           "to the current output port.")
-#define FUNC_NAME s_scm_backtrace
+SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0, 
+           (SCM highlights),
+           "Display a backtrace of the current stack to the current\n"
+            "output port.  If @var{highlights} is given, it should be\n"
+           "a list; the elements of this list will be highlighted\n"
+           "wherever they appear in the backtrace.")
+#define FUNC_NAME s_scm_backtrace_with_highlights
 {
-  SCM the_last_stack =
-    scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var));
-  if (!SCM_FALSEP (the_last_stack))
-    {
-      scm_newline (scm_cur_outp);
-      scm_puts ("Backtrace:\n", scm_cur_outp);
-      scm_display_backtrace (the_last_stack,
-                            scm_cur_outp,
-                            SCM_UNDEFINED,
-                            SCM_UNDEFINED);
-      scm_newline (scm_cur_outp);
-      if (SCM_FALSEP (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var))
-         && !SCM_BACKTRACE_P)
-       {
-         scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
-                   "a backtrace\n"
-                   "automatically if an error occurs in the future.\n",
-                   scm_cur_outp);
-         SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
-       }
-    }
-  else
-    {
-      scm_puts ("No backtrace available.\n", scm_cur_outp);
-    }
+  SCM port = scm_current_output_port ();
+  SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
+  
+  if (SCM_UNBNDP (highlights))
+    highlights = SCM_EOL;
+
+  scm_newline (port);
+  scm_puts ("Backtrace:\n", port);
+  scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
+                                         highlights);
+  scm_newline (port);
+
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
 
+SCM
+scm_backtrace (void)
+{
+  return scm_backtrace_with_highlights (SCM_EOL);
+}
+
 \f
 
 void
 scm_init_backtrace ()
 {
-  SCM f = scm_make_fluid ();
-  scm_the_last_stack_fluid_var = scm_c_define ("the-last-stack", f);
-
+  scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception);
 #include "libguile/backtrace.x"
 }