* coop-threads.c: Remove K&R function headers.
[bpt/guile.git] / libguile / backtrace.c
CommitLineData
ab4f3efb 1/* Printing of backtraces and error messages
78a0461a 2 * Copyright (C) 1996, 1997, 1998, 1999 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
2fb36297
JB
53#include "_scm.h"
54
b1508ade
MD
55#ifdef HAVE_UNISTD_H
56#include <unistd.h>
57#endif
58
ab4f3efb
MD
59#include "stacks.h"
60#include "srcprop.h"
61#include "genio.h"
62#include "struct.h"
63#include "strports.h"
bdf8afff 64#include "throw.h"
a5d6d578 65#include "fluids.h"
ab4f3efb 66
0c95b57d 67#include "scm_validate.h"
ab4f3efb
MD
68#include "backtrace.h"
69
70/* {Error reporting and backtraces}
71 * (A first approximation.)
72 *
73 * Note that these functions shouldn't generate errors themselves.
74 */
75
76#ifndef SCM_RECKLESS
77#undef SCM_ASSERT
78#define SCM_ASSERT(_cond, _arg, _pos, _subr) \
79 if (!(_cond)) \
80 return SCM_BOOL_F;
81#endif
82
b6609fc7
MD
83SCM scm_the_last_stack_fluid;
84
ab4f3efb 85static void
1bbd0b84 86display_header (SCM source, SCM port)
ab4f3efb 87{
0c95b57d 88 SCM fname = (SCM_MEMOIZEDP (source)
7f2d92b1 89 ? scm_source_property (source, scm_sym_filename)
ab4f3efb 90 : SCM_BOOL_F);
0c95b57d 91 if (SCM_STRINGP (fname))
ab4f3efb
MD
92 {
93 scm_prin1 (fname, port, 0);
b7f3516f 94 scm_putc (':', port);
7f2d92b1 95 scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_line)) + 1,
0e929db3
MD
96 10,
97 port);
b7f3516f 98 scm_putc (':', port);
7f2d92b1 99 scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_column)) + 1,
0e929db3
MD
100 10,
101 port);
ab4f3efb
MD
102 }
103 else
b7f3516f
TT
104 scm_puts ("ERROR", port);
105 scm_puts (": ", port);
ab4f3efb
MD
106}
107
f3acc5c1
JB
108
109void
6e8d25a6 110scm_display_error_message (SCM message, SCM args, SCM port)
ab4f3efb
MD
111{
112 int writingp;
113 char *start;
114 char *p;
115
89958ad0 116 if (SCM_IMP (message) || !SCM_ROSTRINGP (message) || SCM_IMP (args)
c37e0e55 117 || !scm_list_p (args))
ab4f3efb
MD
118 {
119 scm_prin1 (message, port, 0);
b7f3516f 120 scm_putc ('\n', port);
ab4f3efb
MD
121 return;
122 }
123
89958ad0
JB
124 SCM_COERCE_SUBSTR (message);
125 start = SCM_ROCHARS (message);
ab4f3efb
MD
126 for (p = start; *p != '\0'; ++p)
127 if (*p == '%')
128 {
129 if (SCM_IMP (args) || SCM_NCONSP (args))
130 continue;
131
132 ++p;
133 if (*p == 's')
134 writingp = 0;
135 else if (*p == 'S')
136 writingp = 1;
137 else
138 continue;
139
b7f3516f 140 scm_lfwrite (start, p - start - 1, port);
ab4f3efb
MD
141 scm_prin1 (SCM_CAR (args), port, writingp);
142 args = SCM_CDR (args);
143 start = p + 1;
144 }
b7f3516f
TT
145 scm_lfwrite (start, p - start, port);
146 scm_putc ('\n', port);
ab4f3efb
MD
147}
148
ab4f3efb 149static void
1bbd0b84 150display_expression (SCM frame,SCM pname,SCM source,SCM port)
ab4f3efb
MD
151{
152 SCM print_state = scm_make_print_state ();
153 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
154 pstate->writingp = 0;
155 pstate->fancyp = 1;
156 pstate->level = 2;
157 pstate->length = 3;
0c95b57d 158 if (SCM_ROSTRINGP (pname))
ab4f3efb
MD
159 {
160 if (SCM_NIMP (frame)
161 && SCM_FRAMEP (frame)
162 && SCM_FRAME_EVAL_ARGS_P (frame))
b7f3516f 163 scm_puts ("While evaluating arguments to ", port);
ab4f3efb 164 else
b7f3516f 165 scm_puts ("In procedure ", port);
ab4f3efb 166 scm_iprin1 (pname, port, pstate);
0c95b57d 167 if (SCM_MEMOIZEDP (source))
ab4f3efb 168 {
b7f3516f 169 scm_puts (" in expression ", port);
ab4f3efb
MD
170 pstate->writingp = 1;
171 scm_iprin1 (scm_unmemoize (source), port, pstate);
172 }
173 }
174 else if (SCM_NIMP (source))
175 {
b7f3516f 176 scm_puts ("In expression ", port);
ab4f3efb
MD
177 pstate->writingp = 1;
178 scm_iprin1 (scm_unmemoize (source), port, pstate);
179 }
b7f3516f 180 scm_puts (":\n", port);
ab4f3efb
MD
181 scm_free_print_state (print_state);
182}
183
bdf8afff
MD
184struct display_error_args {
185 SCM stack;
186 SCM port;
187 SCM subr;
188 SCM message;
189 SCM args;
190 SCM rest;
191};
192
193static SCM
39752bec 194display_error_body (struct display_error_args *a)
ab4f3efb
MD
195{
196 SCM current_frame = SCM_BOOL_F;
197 SCM source = SCM_BOOL_F;
198 SCM pname = SCM_BOOL_F;
a88a4c8a
JB
199 SCM prev_frame = SCM_BOOL_F;
200
841076ac 201 if (SCM_DEBUGGINGP
bdf8afff
MD
202 && SCM_NIMP (a->stack)
203 && SCM_STACKP (a->stack)
204 && SCM_STACK_LENGTH (a->stack) > 0)
ab4f3efb 205 {
bdf8afff 206 current_frame = scm_stack_ref (a->stack, SCM_INUM0);
ab4f3efb 207 source = SCM_FRAME_SOURCE (current_frame);
a88a4c8a 208 prev_frame = SCM_FRAME_PREV (current_frame);
0c95b57d 209 if (!SCM_MEMOIZEDP (source)
a88a4c8a
JB
210 && prev_frame != SCM_BOOL_F)
211 source = SCM_FRAME_SOURCE (prev_frame);
ab4f3efb 212 if (SCM_FRAME_PROC_P (current_frame)
016e2ce1 213 && scm_procedure_p (SCM_FRAME_PROC (current_frame)) == SCM_BOOL_T)
ab4f3efb
MD
214 pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
215 }
0c95b57d 216 if (!SCM_ROSTRINGP (pname))
bdf8afff 217 pname = a->subr;
0c95b57d 218 if (SCM_ROSTRINGP (pname) || SCM_MEMOIZEDP (source))
ab4f3efb 219 {
bdf8afff
MD
220 display_header (source, a->port);
221 display_expression (current_frame, pname, source, a->port);
ab4f3efb 222 }
bdf8afff
MD
223 display_header (source, a->port);
224 scm_display_error_message (a->message, a->args, a->port);
225 return SCM_UNSPECIFIED;
226}
227
228struct display_error_handler_data {
229 char *mode;
230 SCM port;
231};
232
233/* This is the exception handler for error reporting routines.
234 Note that it is very important that this handler *doesn't* try to
235 print more than the error tag, since the error very probably is
236 caused by an erroneous print call-back routine. If we would
237 tru to print all objects, we would enter an infinite loop. */
238static SCM
239display_error_handler (struct display_error_handler_data *data,
240 SCM tag, SCM args)
241{
242 SCM print_state = scm_make_print_state ();
b7f3516f
TT
243 scm_puts ("\nException during displaying of ", data->port);
244 scm_puts (data->mode, data->port);
245 scm_puts (": ", data->port);
bdf8afff 246 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
b7f3516f 247 scm_putc ('\n', data->port);
bdf8afff
MD
248 return SCM_UNSPECIFIED;
249}
250
1bbd0b84
GB
251GUILE_PROC(scm_display_error, "display-error", 6, 0, 0,
252 (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
253"")
254#define FUNC_NAME s_scm_display_error
bdf8afff 255{
0b2cb4ee
MD
256 struct display_error_args a;
257 struct display_error_handler_data data;
258 a.stack = stack;
259 a.port = port;
260 a.subr = subr;
261 a.message = message;
262 a.args = args;
263 a.rest = rest;
264 data.mode = "error";
265 data.port = port;
bdf8afff
MD
266 scm_internal_catch (SCM_BOOL_T,
267 (scm_catch_body_t) display_error_body, &a,
268 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
269 return SCM_UNSPECIFIED;
270}
1bbd0b84 271#undef FUNC_NAME
ab4f3efb 272
2b407f9b
MD
273typedef struct {
274 int level;
275 int length;
276} print_params_t;
277
278static int n_print_params = 9;
279static print_params_t default_print_params[] = {
280 { 4, 9 }, { 4, 3 },
281 { 3, 4 }, { 3, 3 },
282 { 2, 4 }, { 2, 3 },
283 { 1, 4 }, { 1, 3 }, { 1, 2 }
284};
285static print_params_t *print_params = default_print_params;
286
287#ifdef GUILE_DEBUG
e8e9b690 288GUILE_PROC(scm_set_print_params_x, "set-print-params!", 1, 0, 0,
5623a9b4
MD
289 (SCM params),
290"")
e8e9b690 291#define FUNC_NAME s_scm_set_print_params_x
2b407f9b
MD
292{
293 int i, n = scm_ilength (params);
294 SCM ls;
295 print_params_t *new_params;
1bbd0b84 296 SCM_ASSERT (n >= 1, params, SCM_ARG2, FUNC_NAME);
2b407f9b
MD
297 for (ls = params; SCM_NIMP (ls); ls = SCM_CDR (ls))
298 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
299 && SCM_INUMP (SCM_CAAR (ls))
300 && SCM_INUM (SCM_CAAR (ls)) >= 0
301 && SCM_INUMP (SCM_CADAR (ls))
302 && SCM_INUM (SCM_CADAR (ls)) >= 0,
303 params,
304 SCM_ARG2,
e8e9b690 305 s_scm_set_print_params_x);
2b407f9b 306 new_params = scm_must_malloc (n * sizeof (print_params_t),
1bbd0b84 307 FUNC_NAME);
2b407f9b
MD
308 if (print_params != default_print_params)
309 scm_must_free (print_params);
310 print_params = new_params;
311 for (i = 0; i < n; ++i)
312 {
313 print_params[i].level = SCM_INUM (SCM_CAAR (params));
314 print_params[i].length = SCM_INUM (SCM_CADAR (params));
315 params = SCM_CDR (params);
316 }
317 n_print_params = n;
318 return SCM_UNSPECIFIED;
319}
1bbd0b84 320#undef FUNC_NAME
2b407f9b
MD
321#endif
322
ab4f3efb 323static void
1bbd0b84 324indent (int n, SCM port)
ab4f3efb
MD
325{
326 int i;
327 for (i = 0; i < n; ++i)
b7f3516f 328 scm_putc (' ', port);
ab4f3efb
MD
329}
330
ab4f3efb 331static void
1bbd0b84 332display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate)
ab4f3efb 333{
2b407f9b
MD
334 SCM string;
335 int i = 0, n;
336 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
337 do
ab4f3efb 338 {
2b407f9b
MD
339 pstate->length = print_params[i].length;
340 ptob->seek (sport, 0, SEEK_SET);
0c95b57d 341 if (SCM_CONSP (exp))
2b407f9b
MD
342 {
343 pstate->level = print_params[i].level - 1;
344 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
345 scm_puts (&tlr[1], sport);
346 }
347 else
348 {
349 pstate->level = print_params[i].level;
350 scm_iprin1 (exp, sport, pstate);
351 }
352 ptob->flush (sport);
353 n = ptob->seek (sport, 0, SEEK_CUR);
354 ++i;
ab4f3efb 355 }
2b407f9b
MD
356 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
357 ptob->truncate (sport, n);
358 string = scm_strport_to_string (sport);
359 /* Remove control characters */
360 for (i = 0; i < n; ++i)
361 if (iscntrl (SCM_CHARS (string)[i]))
362 SCM_CHARS (string)[i] = ' ';
363 /* Truncate */
364 if (indentation + n > SCM_BACKTRACE_WIDTH)
365 {
366 n = SCM_BACKTRACE_WIDTH - indentation;
367 SCM_CHARS (string)[n - 1] = '$';
368 }
369
370 scm_lfwrite (SCM_CHARS (string), n, port);
ab4f3efb
MD
371}
372
e3c37929 373static void
1bbd0b84 374display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate)
e3c37929
MD
375{
376 SCM proc = SCM_FRAME_PROC (frame);
377 SCM name = (SCM_NFALSEP (scm_procedure_p (proc))
378 ? scm_procedure_name (proc)
379 : SCM_BOOL_F);
380 display_frame_expr ("[",
381 scm_cons (SCM_NFALSEP (name) ? name : proc,
382 SCM_FRAME_ARGS (frame)),
383 SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
384 indentation,
385 sport,
386 port,
387 pstate);
388}
389
1bbd0b84
GB
390GUILE_PROC(scm_display_application, "display-application", 1, 2, 0,
391 (SCM frame, SCM port, SCM indent),
392"")
393#define FUNC_NAME s_scm_display_application
e3c37929 394{
0c95b57d 395 SCM_VALIDATE_FRAME(1,frame);
e3c37929
MD
396 if (SCM_UNBNDP (port))
397 port = scm_cur_outp;
2b407f9b 398 else
0c95b57d 399 SCM_VALIDATE_OPOUTPORT(2,port);
2b407f9b
MD
400 if (SCM_UNBNDP (indent))
401 indent = SCM_INUM0;
402 else
0c95b57d 403 SCM_VALIDATE_INT(3,indent);
2b407f9b 404
e3c37929
MD
405 if (SCM_FRAME_PROC_P (frame))
406 /* Display an application. */
407 {
2b407f9b 408 SCM sport, print_state;
e3c37929
MD
409 scm_print_state *pstate;
410
2b407f9b
MD
411 /* Create a string port used for adaptation of printing parameters. */
412 sport = scm_mkstrport (SCM_INUM0,
413 scm_make_string (SCM_MAKINUM (240),
414 SCM_UNDEFINED),
415 SCM_OPN | SCM_WRTNG,
1bbd0b84 416 FUNC_NAME);
2b407f9b 417
e3c37929
MD
418 /* Create a print state for printing of frames. */
419 print_state = scm_make_print_state ();
420 pstate = SCM_PRINT_STATE (print_state);
421 pstate->writingp = 1;
422 pstate->fancyp = 1;
e3c37929 423
2b407f9b 424 display_application (frame, SCM_INUM (indent), sport, port, pstate);
e3c37929
MD
425 return SCM_BOOL_T;
426 }
427 else
428 return SCM_BOOL_F;
429}
1bbd0b84 430#undef FUNC_NAME
e3c37929 431
ab4f3efb 432static void
1bbd0b84 433display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate)
ab4f3efb
MD
434{
435 int n, i, j;
436
437 /* Announce missing frames? */
438 if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
439 {
440 indent (nfield + 1 + indentation, port);
b7f3516f 441 scm_puts ("...\n", port);
ab4f3efb
MD
442 }
443
444 /* Check size of frame number. */
445 n = SCM_FRAME_NUMBER (frame);
446 for (i = 0, j = n; j > 0; ++i) j /= 10;
447
448 /* Number indentation. */
449 indent (nfield - (i ? i : 1), port);
450
451 /* Frame number. */
452 scm_iprin1 (SCM_MAKINUM (n), port, pstate);
453
454 /* Real frame marker */
b7f3516f 455 scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
ab4f3efb
MD
456
457 /* Indentation. */
458 indent (indentation, port);
459
460 if (SCM_FRAME_PROC_P (frame))
461 /* Display an application. */
e3c37929 462 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
ab4f3efb
MD
463 else
464 /* Display a special form. */
465 {
466 SCM source = SCM_FRAME_SOURCE (frame);
0c95b57d 467 SCM copy = (SCM_CONSP (source)
7f2d92b1 468 ? scm_source_property (source, scm_sym_copy)
2e9d6c6d 469 : SCM_BOOL_F);
0c95b57d 470 SCM umcopy = (SCM_MEMOIZEDP (source)
2e9d6c6d
MD
471 ? scm_unmemoize (source)
472 : SCM_BOOL_F);
ab4f3efb 473 display_frame_expr ("(",
0c95b57d 474 SCM_CONSP (copy) ? copy : umcopy,
ab4f3efb
MD
475 ")",
476 nfield + 1 + indentation,
477 sport,
478 port,
479 pstate);
480 }
1ae6af28 481 scm_putc ('\n', port);
ab4f3efb
MD
482
483 /* Announce missing frames? */
484 if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
485 {
486 indent (nfield + 1 + indentation, port);
b7f3516f 487 scm_puts ("...\n", port);
ab4f3efb
MD
488 }
489}
490
bdf8afff
MD
491struct display_backtrace_args {
492 SCM stack;
493 SCM port;
494 SCM first;
495 SCM depth;
496};
497
bdf8afff 498static SCM
1bbd0b84
GB
499display_backtrace_body(struct display_backtrace_args *a)
500#define FUNC_NAME "display_backtrace_body"
ab4f3efb
MD
501{
502 int n_frames, beg, end, n, i, j;
503 int nfield, indent_p, indentation;
504 SCM frame, sport, print_state;
505 scm_print_state *pstate;
506
78446828
MV
507 a->port = SCM_COERCE_OUTPORT (a->port);
508
ab4f3efb 509 /* Argument checking and extraction. */
0c95b57d 510 SCM_ASSERT (SCM_STACKP (a->stack),
bdf8afff 511 a->stack,
ab4f3efb
MD
512 SCM_ARG1,
513 s_display_backtrace);
0c95b57d 514 SCM_ASSERT (SCM_OPOUTPORTP (a->port),
bdf8afff 515 a->port,
ab4f3efb
MD
516 SCM_ARG2,
517 s_display_backtrace);
bdf8afff
MD
518 n_frames = SCM_INUM (scm_stack_length (a->stack));
519 n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
ab4f3efb
MD
520 if (SCM_BACKWARDS_P)
521 {
bdf8afff 522 beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
ab4f3efb
MD
523 end = beg + n - 1;
524 if (end >= n_frames)
525 end = n_frames - 1;
526 n = end - beg + 1;
527 }
528 else
529 {
bdf8afff 530 if (SCM_INUMP (a->first))
ab4f3efb 531 {
bdf8afff 532 beg = SCM_INUM (a->first);
ab4f3efb
MD
533 end = beg - n + 1;
534 if (end < 0)
535 end = 0;
536 }
537 else
538 {
539 beg = n - 1;
540 end = 0;
541 if (beg >= n_frames)
542 beg = n_frames - 1;
543 }
544 n = beg - end + 1;
545 }
bdf8afff
MD
546 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
547 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
ab4f3efb
MD
548
549 /* Create a string port used for adaptation of printing parameters. */
550 sport = scm_mkstrport (SCM_INUM0,
551 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
552 SCM_OPN | SCM_WRTNG,
1bbd0b84 553 FUNC_NAME);
ab4f3efb
MD
554
555 /* Create a print state for printing of frames. */
556 print_state = scm_make_print_state ();
557 pstate = SCM_PRINT_STATE (print_state);
558 pstate->writingp = 1;
559 pstate->fancyp = 1;
560
561 /* First find out if it's reasonable to do indentation. */
562 if (SCM_BACKWARDS_P)
563 indent_p = 0;
564 else
565 {
566 indent_p = 1;
bdf8afff 567 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb
MD
568 for (i = 0, j = 0; i < n; ++i)
569 {
570 if (SCM_FRAME_REAL_P (frame))
571 ++j;
572 if (j > SCM_BACKTRACE_INDENT)
573 {
574 indent_p = 0;
575 break;
576 }
577 frame = (SCM_BACKWARDS_P
578 ? SCM_FRAME_PREV (frame)
579 : SCM_FRAME_NEXT (frame));
580 }
581 }
582
583 /* Determine size of frame number field. */
bdf8afff 584 j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
ab4f3efb
MD
585 for (i = 0; j > 0; ++i) j /= 10;
586 nfield = i ? i : 1;
587
ab4f3efb 588 /* Print frames. */
bdf8afff 589 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb 590 indentation = 1;
bdf8afff 591 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb
MD
592 for (i = 1; i < n; ++i)
593 {
594 if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
595 ++indentation;
596 frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame);
bdf8afff 597 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb 598 }
bdf8afff
MD
599
600 return SCM_UNSPECIFIED;
601}
1bbd0b84 602#undef FUNC_NAME
bdf8afff 603
1bbd0b84
GB
604GUILE_PROC(scm_display_backtrace, "display-backtrace", 2, 2, 0,
605 (SCM stack, SCM port, SCM first, SCM depth),
606"")
607#define FUNC_NAME s_scm_display_backtrace
bdf8afff 608{
0b2cb4ee
MD
609 struct display_backtrace_args a;
610 struct display_error_handler_data data;
611 a.stack = stack;
612 a.port = port;
613 a.first = first;
614 a.depth = depth;
615 data.mode = "backtrace";
616 data.port = port;
bdf8afff
MD
617 scm_internal_catch (SCM_BOOL_T,
618 (scm_catch_body_t) display_backtrace_body, &a,
619 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
620 return SCM_UNSPECIFIED;
621}
1bbd0b84 622#undef FUNC_NAME
ab4f3efb 623
78f9f47b 624SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
5aab5d96 625
1bbd0b84
GB
626GUILE_PROC(scm_backtrace, "backtrace", 0, 0, 0,
627 (),
628"")
629#define FUNC_NAME s_scm_backtrace
5aab5d96 630{
b6609fc7 631 SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid));
a5d6d578 632 if (SCM_NFALSEP (the_last_stack))
5aab5d96
MD
633 {
634 scm_newline (scm_cur_outp);
3a1f8447 635 scm_puts ("Backtrace:\n", scm_cur_outp);
a5d6d578 636 scm_display_backtrace (the_last_stack,
5aab5d96
MD
637 scm_cur_outp,
638 SCM_UNDEFINED,
639 SCM_UNDEFINED);
640 scm_newline (scm_cur_outp);
641 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var))
642 && !SCM_BACKTRACE_P)
643 {
b7f3516f
TT
644 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
645 "a backtrace\n"
646 "automatically if an error occurs in the future.\n",
647 scm_cur_outp);
5aab5d96
MD
648 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
649 }
650 }
651 else
652 {
b7f3516f 653 scm_puts ("No backtrace available.\n", scm_cur_outp);
5aab5d96
MD
654 }
655 return SCM_UNSPECIFIED;
656}
1bbd0b84 657#undef FUNC_NAME
5aab5d96 658
ab4f3efb
MD
659\f
660
661void
662scm_init_backtrace ()
663{
a5d6d578 664 SCM f = scm_make_fluid ();
b6609fc7 665 scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f);
5aab5d96 666
ab4f3efb
MD
667#include "backtrace.x"
668}