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