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