* Make sure that only open file ports are used as readline ports.
[bpt/guile.git] / libguile / backtrace.c
CommitLineData
ab4f3efb 1/* Printing of backtraces and error messages
f2c9fcb0 2 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation
ab4f3efb
MD
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; see the file COPYING. If not, write to
82892bed
JB
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
ab4f3efb
MD
18 *
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
21 *
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
27 *
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
30 *
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
38 *
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
42 *
43 * The author can be reached at djurfeldt@nada.kth.se
82892bed 44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
ab4f3efb 45
1bbd0b84
GB
46/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48
49
ab4f3efb 50#include <stdio.h>
2b407f9b
MD
51#include <ctype.h>
52
a0599745 53#include "libguile/_scm.h"
2fb36297 54
b1508ade
MD
55#ifdef HAVE_UNISTD_H
56#include <unistd.h>
57#endif
58
a0599745
MD
59#include "libguile/stacks.h"
60#include "libguile/srcprop.h"
61#include "libguile/struct.h"
62#include "libguile/strports.h"
63#include "libguile/throw.h"
64#include "libguile/fluids.h"
65#include "libguile/ports.h"
66#include "libguile/strings.h"
ab4f3efb 67
a0599745
MD
68#include "libguile/validate.h"
69#include "libguile/backtrace.h"
ab4f3efb
MD
70
71/* {Error reporting and backtraces}
72 * (A first approximation.)
73 *
74 * Note that these functions shouldn't generate errors themselves.
75 */
76
77#ifndef SCM_RECKLESS
78#undef SCM_ASSERT
79#define SCM_ASSERT(_cond, _arg, _pos, _subr) \
80 if (!(_cond)) \
81 return SCM_BOOL_F;
82#endif
83
b6609fc7
MD
84SCM scm_the_last_stack_fluid;
85
ab4f3efb 86static void
1bbd0b84 87display_header (SCM source, SCM port)
ab4f3efb 88{
0c95b57d 89 SCM fname = (SCM_MEMOIZEDP (source)
7f2d92b1 90 ? scm_source_property (source, scm_sym_filename)
ab4f3efb 91 : SCM_BOOL_F);
0c95b57d 92 if (SCM_STRINGP (fname))
ab4f3efb
MD
93 {
94 scm_prin1 (fname, port, 0);
b7f3516f 95 scm_putc (':', port);
7f2d92b1 96 scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_line)) + 1,
0e929db3
MD
97 10,
98 port);
b7f3516f 99 scm_putc (':', port);
7f2d92b1 100 scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_column)) + 1,
0e929db3
MD
101 10,
102 port);
ab4f3efb
MD
103 }
104 else
b7f3516f
TT
105 scm_puts ("ERROR", port);
106 scm_puts (": ", port);
ab4f3efb
MD
107}
108
f3acc5c1
JB
109
110void
6e8d25a6 111scm_display_error_message (SCM message, SCM args, SCM port)
ab4f3efb 112{
b24b5e13 113 if (SCM_STRINGP (message) && !SCM_FALSEP (scm_list_p (args)))
3fceef59
MD
114 {
115 scm_simple_format (port, message, args);
116 scm_newline (port);
117 }
118 else
ab4f3efb 119 {
b24b5e13
DH
120 scm_display (message, port);
121 scm_newline (port);
ab4f3efb 122 }
ab4f3efb
MD
123}
124
ab4f3efb 125static void
1bbd0b84 126display_expression (SCM frame,SCM pname,SCM source,SCM port)
ab4f3efb
MD
127{
128 SCM print_state = scm_make_print_state ();
129 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
130 pstate->writingp = 0;
131 pstate->fancyp = 1;
132 pstate->level = 2;
133 pstate->length = 3;
b24b5e13 134 if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname))
ab4f3efb 135 {
cabe682c 136 if (SCM_FRAMEP (frame)
ab4f3efb 137 && SCM_FRAME_EVAL_ARGS_P (frame))
b7f3516f 138 scm_puts ("While evaluating arguments to ", port);
ab4f3efb 139 else
b7f3516f 140 scm_puts ("In procedure ", port);
ab4f3efb 141 scm_iprin1 (pname, port, pstate);
0c95b57d 142 if (SCM_MEMOIZEDP (source))
ab4f3efb 143 {
b7f3516f 144 scm_puts (" in expression ", port);
ab4f3efb
MD
145 pstate->writingp = 1;
146 scm_iprin1 (scm_unmemoize (source), port, pstate);
147 }
148 }
149 else if (SCM_NIMP (source))
150 {
b7f3516f 151 scm_puts ("In expression ", port);
ab4f3efb
MD
152 pstate->writingp = 1;
153 scm_iprin1 (scm_unmemoize (source), port, pstate);
154 }
b7f3516f 155 scm_puts (":\n", port);
ab4f3efb
MD
156 scm_free_print_state (print_state);
157}
158
bdf8afff
MD
159struct display_error_args {
160 SCM stack;
161 SCM port;
162 SCM subr;
163 SCM message;
164 SCM args;
165 SCM rest;
166};
167
168static SCM
39752bec 169display_error_body (struct display_error_args *a)
ab4f3efb
MD
170{
171 SCM current_frame = SCM_BOOL_F;
172 SCM source = SCM_BOOL_F;
a88a4c8a 173 SCM prev_frame = SCM_BOOL_F;
b24b5e13 174 SCM pname = a->subr;
a88a4c8a 175
841076ac 176 if (SCM_DEBUGGINGP
bdf8afff
MD
177 && SCM_STACKP (a->stack)
178 && SCM_STACK_LENGTH (a->stack) > 0)
ab4f3efb 179 {
bdf8afff 180 current_frame = scm_stack_ref (a->stack, SCM_INUM0);
ab4f3efb 181 source = SCM_FRAME_SOURCE (current_frame);
a88a4c8a 182 prev_frame = SCM_FRAME_PREV (current_frame);
cffcab30 183 if (!SCM_MEMOIZEDP (source) && !SCM_FALSEP (prev_frame))
a88a4c8a 184 source = SCM_FRAME_SOURCE (prev_frame);
b24b5e13 185 if (!SCM_SYMBOLP (pname) && !SCM_STRINGP (pname) && SCM_FRAME_PROC_P (current_frame)
9a09deb1 186 && SCM_EQ_P (scm_procedure_p (SCM_FRAME_PROC (current_frame)), SCM_BOOL_T))
ab4f3efb
MD
187 pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
188 }
b24b5e13 189 if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname) || SCM_MEMOIZEDP (source))
ab4f3efb 190 {
bdf8afff
MD
191 display_header (source, a->port);
192 display_expression (current_frame, pname, source, a->port);
ab4f3efb 193 }
bdf8afff
MD
194 display_header (source, a->port);
195 scm_display_error_message (a->message, a->args, a->port);
196 return SCM_UNSPECIFIED;
197}
198
199struct display_error_handler_data {
200 char *mode;
201 SCM port;
202};
203
204/* This is the exception handler for error reporting routines.
205 Note that it is very important that this handler *doesn't* try to
206 print more than the error tag, since the error very probably is
207 caused by an erroneous print call-back routine. If we would
2fdcf8bd 208 try to print all objects, we would enter an infinite loop. */
bdf8afff
MD
209static SCM
210display_error_handler (struct display_error_handler_data *data,
211 SCM tag, SCM args)
212{
213 SCM print_state = scm_make_print_state ();
b7f3516f
TT
214 scm_puts ("\nException during displaying of ", data->port);
215 scm_puts (data->mode, data->port);
216 scm_puts (": ", data->port);
bdf8afff 217 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
b7f3516f 218 scm_putc ('\n', data->port);
bdf8afff
MD
219 return SCM_UNSPECIFIED;
220}
221
e40a4095
DH
222
223/* The function scm_i_display_error prints out a detailed error message. This
224 * function will be called directly within libguile to signal error messages.
225 * No parameter checks will be performed by scm_i_display_error. Thus, User
226 * code should rather use the function scm_display_error.
227 */
228void
229scm_i_display_error (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest)
bdf8afff 230{
0b2cb4ee
MD
231 struct display_error_args a;
232 struct display_error_handler_data data;
233 a.stack = stack;
234 a.port = port;
235 a.subr = subr;
236 a.message = message;
237 a.args = args;
238 a.rest = rest;
239 data.mode = "error";
240 data.port = port;
bdf8afff
MD
241 scm_internal_catch (SCM_BOOL_T,
242 (scm_catch_body_t) display_error_body, &a,
243 (scm_catch_handler_t) display_error_handler, &data);
e40a4095
DH
244}
245
246
247SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
248 (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
249 "")
250#define FUNC_NAME s_scm_display_error
251{
252 SCM_VALIDATE_OUTPUT_PORT (2, port);
253
254 scm_i_display_error (stack, port, subr, message, args, rest);
255
ab4f3efb
MD
256 return SCM_UNSPECIFIED;
257}
1bbd0b84 258#undef FUNC_NAME
ab4f3efb 259
e40a4095 260
2b407f9b
MD
261typedef struct {
262 int level;
263 int length;
264} print_params_t;
265
266static int n_print_params = 9;
267static print_params_t default_print_params[] = {
268 { 4, 9 }, { 4, 3 },
269 { 3, 4 }, { 3, 3 },
270 { 2, 4 }, { 2, 3 },
271 { 1, 4 }, { 1, 3 }, { 1, 2 }
272};
273static print_params_t *print_params = default_print_params;
274
275#ifdef GUILE_DEBUG
3b3b36dd 276SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
5623a9b4
MD
277 (SCM params),
278"")
e8e9b690 279#define FUNC_NAME s_scm_set_print_params_x
2b407f9b 280{
3fceef59
MD
281 int i;
282 int n;
2b407f9b
MD
283 SCM ls;
284 print_params_t *new_params;
3fceef59
MD
285
286 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
287 for (ls = params; SCM_NNULLP (ls); ls = SCM_CDR (ls))
2b407f9b
MD
288 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
289 && SCM_INUMP (SCM_CAAR (ls))
290 && SCM_INUM (SCM_CAAR (ls)) >= 0
291 && SCM_INUMP (SCM_CADAR (ls))
292 && SCM_INUM (SCM_CADAR (ls)) >= 0,
293 params,
294 SCM_ARG2,
e8e9b690 295 s_scm_set_print_params_x);
2b407f9b 296 new_params = scm_must_malloc (n * sizeof (print_params_t),
1bbd0b84 297 FUNC_NAME);
2b407f9b
MD
298 if (print_params != default_print_params)
299 scm_must_free (print_params);
300 print_params = new_params;
301 for (i = 0; i < n; ++i)
302 {
303 print_params[i].level = SCM_INUM (SCM_CAAR (params));
304 print_params[i].length = SCM_INUM (SCM_CADAR (params));
305 params = SCM_CDR (params);
306 }
307 n_print_params = n;
308 return SCM_UNSPECIFIED;
309}
1bbd0b84 310#undef FUNC_NAME
2b407f9b
MD
311#endif
312
ab4f3efb 313static void
1bbd0b84 314indent (int n, SCM port)
ab4f3efb
MD
315{
316 int i;
317 for (i = 0; i < n; ++i)
b7f3516f 318 scm_putc (' ', port);
ab4f3efb
MD
319}
320
ab4f3efb 321static void
1bbd0b84 322display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate)
ab4f3efb 323{
2b407f9b
MD
324 SCM string;
325 int i = 0, n;
326 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
327 do
ab4f3efb 328 {
2b407f9b
MD
329 pstate->length = print_params[i].length;
330 ptob->seek (sport, 0, SEEK_SET);
0c95b57d 331 if (SCM_CONSP (exp))
2b407f9b
MD
332 {
333 pstate->level = print_params[i].level - 1;
334 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
335 scm_puts (&tlr[1], sport);
336 }
337 else
338 {
339 pstate->level = print_params[i].level;
340 scm_iprin1 (exp, sport, pstate);
341 }
342 ptob->flush (sport);
343 n = ptob->seek (sport, 0, SEEK_CUR);
344 ++i;
ab4f3efb 345 }
2b407f9b
MD
346 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
347 ptob->truncate (sport, n);
348 string = scm_strport_to_string (sport);
349 /* Remove control characters */
350 for (i = 0; i < n; ++i)
86c991c2
DH
351 if (iscntrl (SCM_STRING_CHARS (string)[i]))
352 SCM_STRING_CHARS (string)[i] = ' ';
2b407f9b
MD
353 /* Truncate */
354 if (indentation + n > SCM_BACKTRACE_WIDTH)
355 {
356 n = SCM_BACKTRACE_WIDTH - indentation;
86c991c2 357 SCM_STRING_CHARS (string)[n - 1] = '$';
2b407f9b
MD
358 }
359
86c991c2 360 scm_lfwrite (SCM_STRING_CHARS (string), n, port);
ab4f3efb
MD
361}
362
e3c37929 363static void
1bbd0b84 364display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate)
e3c37929
MD
365{
366 SCM proc = SCM_FRAME_PROC (frame);
367 SCM name = (SCM_NFALSEP (scm_procedure_p (proc))
368 ? scm_procedure_name (proc)
369 : SCM_BOOL_F);
370 display_frame_expr ("[",
371 scm_cons (SCM_NFALSEP (name) ? name : proc,
372 SCM_FRAME_ARGS (frame)),
373 SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
374 indentation,
375 sport,
376 port,
377 pstate);
378}
379
3b3b36dd 380SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
1bbd0b84
GB
381 (SCM frame, SCM port, SCM indent),
382"")
383#define FUNC_NAME s_scm_display_application
e3c37929 384{
3b3b36dd 385 SCM_VALIDATE_FRAME (1,frame);
e3c37929
MD
386 if (SCM_UNBNDP (port))
387 port = scm_cur_outp;
2b407f9b 388 else
3b3b36dd 389 SCM_VALIDATE_OPOUTPORT (2,port);
2b407f9b
MD
390 if (SCM_UNBNDP (indent))
391 indent = SCM_INUM0;
392 else
3b3b36dd 393 SCM_VALIDATE_INUM (3,indent);
2b407f9b 394
e3c37929
MD
395 if (SCM_FRAME_PROC_P (frame))
396 /* Display an application. */
397 {
2b407f9b 398 SCM sport, print_state;
e3c37929
MD
399 scm_print_state *pstate;
400
2b407f9b
MD
401 /* Create a string port used for adaptation of printing parameters. */
402 sport = scm_mkstrport (SCM_INUM0,
403 scm_make_string (SCM_MAKINUM (240),
404 SCM_UNDEFINED),
405 SCM_OPN | SCM_WRTNG,
1bbd0b84 406 FUNC_NAME);
2b407f9b 407
e3c37929
MD
408 /* Create a print state for printing of frames. */
409 print_state = scm_make_print_state ();
410 pstate = SCM_PRINT_STATE (print_state);
411 pstate->writingp = 1;
412 pstate->fancyp = 1;
e3c37929 413
2b407f9b 414 display_application (frame, SCM_INUM (indent), sport, port, pstate);
e3c37929
MD
415 return SCM_BOOL_T;
416 }
417 else
418 return SCM_BOOL_F;
419}
1bbd0b84 420#undef FUNC_NAME
e3c37929 421
ab4f3efb 422static void
1bbd0b84 423display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate)
ab4f3efb
MD
424{
425 int n, i, j;
426
427 /* Announce missing frames? */
428 if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
429 {
430 indent (nfield + 1 + indentation, port);
b7f3516f 431 scm_puts ("...\n", port);
ab4f3efb
MD
432 }
433
434 /* Check size of frame number. */
435 n = SCM_FRAME_NUMBER (frame);
436 for (i = 0, j = n; j > 0; ++i) j /= 10;
437
438 /* Number indentation. */
439 indent (nfield - (i ? i : 1), port);
440
441 /* Frame number. */
442 scm_iprin1 (SCM_MAKINUM (n), port, pstate);
443
444 /* Real frame marker */
b7f3516f 445 scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
ab4f3efb
MD
446
447 /* Indentation. */
448 indent (indentation, port);
449
450 if (SCM_FRAME_PROC_P (frame))
451 /* Display an application. */
e3c37929 452 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
ab4f3efb
MD
453 else
454 /* Display a special form. */
455 {
456 SCM source = SCM_FRAME_SOURCE (frame);
0c95b57d 457 SCM copy = (SCM_CONSP (source)
7f2d92b1 458 ? scm_source_property (source, scm_sym_copy)
2e9d6c6d 459 : SCM_BOOL_F);
0c95b57d 460 SCM umcopy = (SCM_MEMOIZEDP (source)
2e9d6c6d
MD
461 ? scm_unmemoize (source)
462 : SCM_BOOL_F);
ab4f3efb 463 display_frame_expr ("(",
0c95b57d 464 SCM_CONSP (copy) ? copy : umcopy,
ab4f3efb
MD
465 ")",
466 nfield + 1 + indentation,
467 sport,
468 port,
469 pstate);
470 }
1ae6af28 471 scm_putc ('\n', port);
ab4f3efb
MD
472
473 /* Announce missing frames? */
474 if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
475 {
476 indent (nfield + 1 + indentation, port);
b7f3516f 477 scm_puts ("...\n", port);
ab4f3efb
MD
478 }
479}
480
bdf8afff
MD
481struct display_backtrace_args {
482 SCM stack;
483 SCM port;
484 SCM first;
485 SCM depth;
486};
487
bdf8afff 488static SCM
1bbd0b84
GB
489display_backtrace_body(struct display_backtrace_args *a)
490#define FUNC_NAME "display_backtrace_body"
ab4f3efb
MD
491{
492 int n_frames, beg, end, n, i, j;
493 int nfield, indent_p, indentation;
494 SCM frame, sport, print_state;
495 scm_print_state *pstate;
496
78446828
MV
497 a->port = SCM_COERCE_OUTPORT (a->port);
498
ab4f3efb 499 /* Argument checking and extraction. */
0c95b57d 500 SCM_ASSERT (SCM_STACKP (a->stack),
bdf8afff 501 a->stack,
ab4f3efb
MD
502 SCM_ARG1,
503 s_display_backtrace);
0c95b57d 504 SCM_ASSERT (SCM_OPOUTPORTP (a->port),
bdf8afff 505 a->port,
ab4f3efb
MD
506 SCM_ARG2,
507 s_display_backtrace);
bdf8afff
MD
508 n_frames = SCM_INUM (scm_stack_length (a->stack));
509 n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
ab4f3efb
MD
510 if (SCM_BACKWARDS_P)
511 {
bdf8afff 512 beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
ab4f3efb
MD
513 end = beg + n - 1;
514 if (end >= n_frames)
515 end = n_frames - 1;
516 n = end - beg + 1;
517 }
518 else
519 {
bdf8afff 520 if (SCM_INUMP (a->first))
ab4f3efb 521 {
bdf8afff 522 beg = SCM_INUM (a->first);
ab4f3efb
MD
523 end = beg - n + 1;
524 if (end < 0)
525 end = 0;
526 }
527 else
528 {
529 beg = n - 1;
530 end = 0;
531 if (beg >= n_frames)
532 beg = n_frames - 1;
533 }
534 n = beg - end + 1;
535 }
bdf8afff
MD
536 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
537 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
ab4f3efb
MD
538
539 /* Create a string port used for adaptation of printing parameters. */
540 sport = scm_mkstrport (SCM_INUM0,
541 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
542 SCM_OPN | SCM_WRTNG,
1bbd0b84 543 FUNC_NAME);
ab4f3efb
MD
544
545 /* Create a print state for printing of frames. */
546 print_state = scm_make_print_state ();
547 pstate = SCM_PRINT_STATE (print_state);
548 pstate->writingp = 1;
549 pstate->fancyp = 1;
550
551 /* First find out if it's reasonable to do indentation. */
552 if (SCM_BACKWARDS_P)
553 indent_p = 0;
554 else
555 {
556 indent_p = 1;
bdf8afff 557 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb
MD
558 for (i = 0, j = 0; i < n; ++i)
559 {
560 if (SCM_FRAME_REAL_P (frame))
561 ++j;
562 if (j > SCM_BACKTRACE_INDENT)
563 {
564 indent_p = 0;
565 break;
566 }
567 frame = (SCM_BACKWARDS_P
568 ? SCM_FRAME_PREV (frame)
569 : SCM_FRAME_NEXT (frame));
570 }
571 }
572
573 /* Determine size of frame number field. */
bdf8afff 574 j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
ab4f3efb
MD
575 for (i = 0; j > 0; ++i) j /= 10;
576 nfield = i ? i : 1;
577
ab4f3efb 578 /* Print frames. */
bdf8afff 579 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb 580 indentation = 1;
bdf8afff 581 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb
MD
582 for (i = 1; i < n; ++i)
583 {
584 if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
585 ++indentation;
586 frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame);
bdf8afff 587 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb 588 }
bdf8afff
MD
589
590 return SCM_UNSPECIFIED;
591}
1bbd0b84 592#undef FUNC_NAME
bdf8afff 593
3b3b36dd 594SCM_DEFINE (scm_display_backtrace, "display-backtrace", 2, 2, 0,
1bbd0b84
GB
595 (SCM stack, SCM port, SCM first, SCM depth),
596"")
597#define FUNC_NAME s_scm_display_backtrace
bdf8afff 598{
0b2cb4ee
MD
599 struct display_backtrace_args a;
600 struct display_error_handler_data data;
601 a.stack = stack;
602 a.port = port;
603 a.first = first;
604 a.depth = depth;
605 data.mode = "backtrace";
606 data.port = port;
bdf8afff
MD
607 scm_internal_catch (SCM_BOOL_T,
608 (scm_catch_body_t) display_backtrace_body, &a,
609 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
610 return SCM_UNSPECIFIED;
611}
1bbd0b84 612#undef FUNC_NAME
ab4f3efb 613
78f9f47b 614SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
5aab5d96 615
3b3b36dd 616SCM_DEFINE (scm_backtrace, "backtrace", 0, 0, 0,
1bbd0b84
GB
617 (),
618"")
619#define FUNC_NAME s_scm_backtrace
5aab5d96 620{
b6609fc7 621 SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid));
a5d6d578 622 if (SCM_NFALSEP (the_last_stack))
5aab5d96
MD
623 {
624 scm_newline (scm_cur_outp);
3a1f8447 625 scm_puts ("Backtrace:\n", scm_cur_outp);
a5d6d578 626 scm_display_backtrace (the_last_stack,
5aab5d96
MD
627 scm_cur_outp,
628 SCM_UNDEFINED,
629 SCM_UNDEFINED);
630 scm_newline (scm_cur_outp);
631 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var))
632 && !SCM_BACKTRACE_P)
633 {
b7f3516f
TT
634 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
635 "a backtrace\n"
636 "automatically if an error occurs in the future.\n",
637 scm_cur_outp);
5aab5d96
MD
638 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
639 }
640 }
641 else
642 {
b7f3516f 643 scm_puts ("No backtrace available.\n", scm_cur_outp);
5aab5d96
MD
644 }
645 return SCM_UNSPECIFIED;
646}
1bbd0b84 647#undef FUNC_NAME
5aab5d96 648
ab4f3efb
MD
649\f
650
651void
652scm_init_backtrace ()
653{
a5d6d578 654 SCM f = scm_make_fluid ();
b6609fc7 655 scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f);
5aab5d96 656
8dc9439f 657#ifndef SCM_MAGIC_SNARFER
a0599745 658#include "libguile/backtrace.x"
8dc9439f 659#endif
ab4f3efb 660}
89e00824
ML
661
662/*
663 Local Variables:
664 c-file-style: "gnu"
665 End:
666*/