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