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