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