Zero-offset branches are backward branches; fix "br" backward branches
[bpt/guile.git] / libguile / backtrace.c
CommitLineData
ab4f3efb 1/* Printing of backtraces and error messages
c7c2d875 2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010, 2011 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
MD
25#include <ctype.h>
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
218d580a 36#include "libguile/deprecation.h"
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
MD
48#include "libguile/validate.h"
49#include "libguile/backtrace.h"
fec097f0 50#include "libguile/filesys.h"
22fc179a 51#include "libguile/private-options.h"
ab4f3efb
MD
52
53/* {Error reporting and backtraces}
ab4f3efb
MD
54 *
55 * Note that these functions shouldn't generate errors themselves.
56 */
57
e8df456a
AW
58static SCM
59boot_print_exception (SCM port, SCM frame, SCM key, SCM args)
60#define FUNC_NAME "boot-print-exception"
61{
0607ebbf 62 scm_puts_unlocked ("Throw to key ", port);
e8df456a 63 scm_write (key, port);
0607ebbf 64 scm_puts_unlocked (" with args ", port);
e8df456a
AW
65 scm_write (args, port);
66 return SCM_UNSPECIFIED;
67}
68#undef FUNC_NAME
69
60617d81
MW
70static SCM print_exception_var;
71
72static void
73init_print_exception_var (void)
74{
75 print_exception_var
76 = scm_module_variable (scm_the_root_module (),
77 scm_from_latin1_symbol ("print-exception"));
78}
79
e8df456a
AW
80SCM
81scm_print_exception (SCM port, SCM frame, SCM key, SCM args)
82#define FUNC_NAME "print-exception"
83{
60617d81
MW
84 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
85 scm_i_pthread_once (&once, init_print_exception_var);
e8df456a
AW
86
87 SCM_VALIDATE_OPOUTPORT (1, port);
88 if (scm_is_true (frame))
89 SCM_VALIDATE_FRAME (2, frame);
90 SCM_VALIDATE_SYMBOL (3, key);
91 SCM_VALIDATE_LIST (4, args);
e8df456a 92
60617d81 93 return scm_call_4 (scm_variable_ref (print_exception_var),
e8df456a
AW
94 port, frame, key, args);
95}
96#undef FUNC_NAME
97
98
99\f
100
dfd03fb9
MD
101/* Print parameters for error messages. */
102
103#define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
104#define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
105
106/* Print parameters for failing expressions in error messages.
107 * (See also `print_params' below for backtrace print parameters.)
108 */
109
110#define DISPLAY_EXPRESSION_MAX_LEVEL 2
111#define DISPLAY_EXPRESSION_MAX_LENGTH 3
112
ab4f3efb
MD
113#undef SCM_ASSERT
114#define SCM_ASSERT(_cond, _arg, _pos, _subr) \
115 if (!(_cond)) \
116 return SCM_BOOL_F;
ab4f3efb 117
e8df456a 118
f3acc5c1 119void
6e8d25a6 120scm_display_error_message (SCM message, SCM args, SCM port)
ab4f3efb 121{
9ddf197e
AW
122 scm_print_exception (port, SCM_BOOL_F, scm_misc_error_key,
123 scm_list_3 (SCM_BOOL_F, message, args));
bdf8afff
MD
124}
125
e40a4095
DH
126
127/* The function scm_i_display_error prints out a detailed error message. This
128 * function will be called directly within libguile to signal error messages.
129 * No parameter checks will be performed by scm_i_display_error. Thus, User
130 * code should rather use the function scm_display_error.
131 */
132void
218d580a 133scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest)
bdf8afff 134{
9ddf197e
AW
135 scm_print_exception (port, frame, scm_misc_error_key,
136 scm_list_3 (subr, message, args));
e40a4095
DH
137}
138
139
140SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
218d580a 141 (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest),
c73bdd3a 142 "Display an error message to the output port @var{port}.\n"
218d580a 143 "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
bb2c02f2 144 "the name of the procedure in which the error occurred and\n"
c73bdd3a
MG
145 "@var{message} is the actual error message, which may contain\n"
146 "formatting instructions. These will format the arguments in\n"
147 "the list @var{args} accordingly. @var{rest} is currently\n"
148 "ignored.")
e40a4095
DH
149#define FUNC_NAME s_scm_display_error
150{
151 SCM_VALIDATE_OUTPUT_PORT (2, port);
152
fb2be758
AW
153#if SCM_ENABLE_DEPRECATED
154 if (SCM_STACKP (frame))
155 {
156 scm_c_issue_deprecation_warning
157 ("Passing a stack as the first argument to `scm_display_error' is "
158 "deprecated. Pass a frame instead.");
159 if (SCM_STACK_LENGTH (frame))
160 frame = scm_stack_ref (frame, SCM_INUM0);
161 else
162 frame = SCM_BOOL_F;
163 }
164#endif
165
218d580a 166 scm_i_display_error (frame, port, subr, message, args, rest);
e40a4095 167
ab4f3efb
MD
168 return SCM_UNSPECIFIED;
169}
1bbd0b84 170#undef FUNC_NAME
ab4f3efb 171
e40a4095 172
2b407f9b
MD
173typedef struct {
174 int level;
175 int length;
176} print_params_t;
177
178static int n_print_params = 9;
179static print_params_t default_print_params[] = {
180 { 4, 9 }, { 4, 3 },
181 { 3, 4 }, { 3, 3 },
182 { 2, 4 }, { 2, 3 },
183 { 1, 4 }, { 1, 3 }, { 1, 2 }
184};
185static print_params_t *print_params = default_print_params;
186
187#ifdef GUILE_DEBUG
3b3b36dd 188SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
5623a9b4 189 (SCM params),
c73bdd3a
MG
190 "Set the print parameters to the values from @var{params}.\n"
191 "@var{params} must be a list of two-element lists which must\n"
192 "hold two integer values.")
e8e9b690 193#define FUNC_NAME s_scm_set_print_params_x
2b407f9b 194{
3fceef59
MD
195 int i;
196 int n;
2b407f9b
MD
197 SCM ls;
198 print_params_t *new_params;
3fceef59
MD
199
200 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
c96d76b8 201 for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
2b407f9b 202 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
e11e83f3
MV
203 && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX)
204 && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX),
2b407f9b
MD
205 params,
206 SCM_ARG2,
e8e9b690 207 s_scm_set_print_params_x);
4c9419ac 208 new_params = scm_malloc (n * sizeof (print_params_t));
2b407f9b 209 if (print_params != default_print_params)
4c9419ac 210 free (print_params);
2b407f9b
MD
211 print_params = new_params;
212 for (i = 0; i < n; ++i)
213 {
e11e83f3
MV
214 print_params[i].level = scm_to_int (SCM_CAAR (params));
215 print_params[i].length = scm_to_int (SCM_CADAR (params));
2b407f9b
MD
216 params = SCM_CDR (params);
217 }
218 n_print_params = n;
219 return SCM_UNSPECIFIED;
220}
1bbd0b84 221#undef FUNC_NAME
2b407f9b
MD
222#endif
223
ab4f3efb 224static void
1bbd0b84 225indent (int n, SCM port)
ab4f3efb
MD
226{
227 int i;
228 for (i = 0; i < n; ++i)
0607ebbf 229 scm_putc_unlocked (' ', port);
ab4f3efb
MD
230}
231
ab4f3efb 232static void
34d19ef6 233display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
ab4f3efb 234{
2b407f9b 235 int i = 0, n;
62bd5d66 236 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (sport);
2b407f9b 237 do
ab4f3efb 238 {
2b407f9b
MD
239 pstate->length = print_params[i].length;
240 ptob->seek (sport, 0, SEEK_SET);
d2e53ed6 241 if (scm_is_pair (exp))
2b407f9b
MD
242 {
243 pstate->level = print_params[i].level - 1;
244 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
0607ebbf 245 scm_puts_unlocked (&tlr[1], sport);
2b407f9b
MD
246 }
247 else
248 {
249 pstate->level = print_params[i].level;
250 scm_iprin1 (exp, sport, pstate);
251 }
252 ptob->flush (sport);
253 n = ptob->seek (sport, 0, SEEK_CUR);
254 ++i;
ab4f3efb 255 }
2b407f9b
MD
256 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
257 ptob->truncate (sport, n);
2b407f9b 258
22e47f69 259 scm_display (scm_strport_to_string (sport), port);
ab4f3efb
MD
260}
261
e3c37929 262static void
34d19ef6 263display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
e3c37929 264{
aa3f6951 265 SCM proc = scm_frame_procedure (frame);
7888309b 266 SCM name = (scm_is_true (scm_procedure_p (proc))
e3c37929
MD
267 ? scm_procedure_name (proc)
268 : SCM_BOOL_F);
269 display_frame_expr ("[",
7888309b 270 scm_cons (scm_is_true (name) ? name : proc,
aa3f6951
AW
271 scm_frame_arguments (frame)),
272 "]",
e3c37929
MD
273 indentation,
274 sport,
275 port,
276 pstate);
277}
278
3b3b36dd 279SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
1bbd0b84 280 (SCM frame, SCM port, SCM indent),
c73bdd3a
MG
281 "Display a procedure application @var{frame} to the output port\n"
282 "@var{port}. @var{indent} specifies the indentation of the\n"
283 "output.")
1bbd0b84 284#define FUNC_NAME s_scm_display_application
e3c37929 285{
34d19ef6 286 SCM_VALIDATE_FRAME (1, frame);
e3c37929 287 if (SCM_UNBNDP (port))
9de87eea 288 port = scm_current_output_port ();
2b407f9b 289 else
34d19ef6 290 SCM_VALIDATE_OPOUTPORT (2, port);
2b407f9b
MD
291 if (SCM_UNBNDP (indent))
292 indent = SCM_INUM0;
2b407f9b 293
aa3f6951
AW
294 /* Display an application. */
295 {
296 SCM sport, print_state;
297 scm_print_state *pstate;
e3c37929 298
aa3f6951 299 /* Create a string port used for adaptation of printing parameters. */
0b2c2ba3 300 sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
aa3f6951
AW
301 SCM_OPN | SCM_WRTNG,
302 FUNC_NAME);
303
304 /* Create a print state for printing of frames. */
305 print_state = scm_make_print_state ();
306 pstate = SCM_PRINT_STATE (print_state);
307 pstate->writingp = 1;
308 pstate->fancyp = 1;
e3c37929 309
aa3f6951
AW
310 display_application (frame, scm_to_int (indent), sport, port, pstate);
311 return SCM_BOOL_T;
312 }
e3c37929 313}
1bbd0b84 314#undef FUNC_NAME
e3c37929 315
fec097f0
MV
316SCM_SYMBOL (sym_base, "base");
317
318static void
319display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
320{
aa3f6951 321 SCM source = scm_frame_source (frame);
e052d296 322 *file = *line = SCM_BOOL_F;
b3f04491
AW
323 if (scm_is_pair (source)
324 && scm_is_pair (scm_cdr (source))
325 && scm_is_pair (scm_cddr (source))
326 && !scm_is_pair (scm_cdddr (source)))
e052d296 327 {
028e3d06
AW
328 /* (addr . (filename . (line . column))), from vm compilation */
329 *file = scm_cadr (source);
330 *line = scm_caddr (source);
e052d296 331 }
fec097f0
MV
332}
333
334static void
335display_backtrace_file (frame, last_file, port, pstate)
336 SCM frame;
337 SCM *last_file;
338 SCM port;
339 scm_print_state *pstate;
340{
341 SCM file, line;
342
343 display_backtrace_get_file_line (frame, &file, &line);
344
aa3f6951 345 if (scm_is_true (scm_equal_p (file, *last_file)))
fec097f0
MV
346 return;
347
348 *last_file = file;
349
0607ebbf 350 scm_puts_unlocked ("In ", port);
7888309b
MV
351 if (scm_is_false (file))
352 if (scm_is_false (line))
0607ebbf 353 scm_puts_unlocked ("unknown file", port);
fec097f0 354 else
0607ebbf 355 scm_puts_unlocked ("current input", port);
fec097f0
MV
356 else
357 {
358 pstate->writingp = 0;
359 scm_iprin1 (file, port, pstate);
360 pstate->writingp = 1;
361 }
0607ebbf 362 scm_puts_unlocked (":\n", port);
fec097f0
MV
363}
364
365static void
366display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
367{
368 SCM file, line;
369
370 display_backtrace_get_file_line (frame, &file, &line);
371
bc36d050 372 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
fec097f0 373 {
7888309b 374 if (scm_is_false (file))
fec097f0 375 {
7888309b 376 if (scm_is_false (line))
0607ebbf 377 scm_putc_unlocked ('?', port);
fec097f0 378 else
0607ebbf 379 scm_puts_unlocked ("<stdin>", port);
fec097f0
MV
380 }
381 else
382 {
383 pstate -> writingp = 0;
4110fa69 384#ifdef HAVE_POSIX
3b9ee0a4
MV
385 scm_iprin1 ((scm_is_string (file)?
386 scm_basename (file, SCM_UNDEFINED) : file),
fec097f0 387 port, pstate);
4110fa69
MV
388#else
389 scm_iprin1 (file, port, pstate);
390#endif
fec097f0
MV
391 pstate -> writingp = 1;
392 }
393
0607ebbf 394 scm_putc_unlocked (':', port);
fec097f0 395 }
7888309b 396 else if (scm_is_true (line))
fec097f0
MV
397 {
398 int i, j=0;
e11e83f3 399 for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
fec097f0
MV
400 ;
401 indent (4-j, port);
402 }
403
7888309b 404 if (scm_is_false (line))
0607ebbf 405 scm_puts_unlocked (" ?", port);
fec097f0 406 else
e11e83f3 407 scm_intprint (scm_to_int (line) + 1, 10, port);
0607ebbf 408 scm_puts_unlocked (": ", port);
fec097f0
MV
409}
410
ab4f3efb 411static void
aa3f6951
AW
412display_frame (SCM frame, int n, int nfield, int indentation,
413 SCM sport, SCM port, scm_print_state *pstate)
ab4f3efb 414{
aa3f6951 415 int i, j;
ab4f3efb 416
fec097f0 417 /* display file name and line number */
7888309b 418 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
fec097f0
MV
419 display_backtrace_file_and_line (frame, port, pstate);
420
ab4f3efb 421 /* Check size of frame number. */
ab4f3efb
MD
422 for (i = 0, j = n; j > 0; ++i) j /= 10;
423
424 /* Number indentation. */
425 indent (nfield - (i ? i : 1), port);
426
427 /* Frame number. */
93ccaef0 428 scm_iprin1 (scm_from_int (n), port, pstate);
ab4f3efb 429
ab4f3efb
MD
430 /* Indentation. */
431 indent (indentation, port);
432
aa3f6951
AW
433 /* Display an application. */
434 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
0607ebbf 435 scm_putc_unlocked ('\n', port);
ab4f3efb
MD
436}
437
bdf8afff
MD
438struct display_backtrace_args {
439 SCM stack;
440 SCM port;
441 SCM first;
442 SCM depth;
2b55be75 443 SCM highlight_objects;
bdf8afff
MD
444};
445
bdf8afff 446static SCM
5843e5c9 447display_backtrace_body (struct display_backtrace_args *a)
1bbd0b84 448#define FUNC_NAME "display_backtrace_body"
ab4f3efb
MD
449{
450 int n_frames, beg, end, n, i, j;
03976fee 451 int nfield, indentation;
ab4f3efb 452 SCM frame, sport, print_state;
fec097f0 453 SCM last_file;
ab4f3efb
MD
454 scm_print_state *pstate;
455
78446828
MV
456 a->port = SCM_COERCE_OUTPORT (a->port);
457
ab4f3efb 458 /* Argument checking and extraction. */
5843e5c9
DH
459 SCM_VALIDATE_STACK (1, a->stack);
460 SCM_VALIDATE_OPOUTPORT (2, a->port);
e11e83f3
MV
461 n_frames = scm_to_int (scm_stack_length (a->stack));
462 n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
ab4f3efb
MD
463 if (SCM_BACKWARDS_P)
464 {
e11e83f3 465 beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
ab4f3efb
MD
466 end = beg + n - 1;
467 if (end >= n_frames)
468 end = n_frames - 1;
469 n = end - beg + 1;
470 }
471 else
472 {
e11e83f3 473 if (scm_is_integer (a->first))
ab4f3efb 474 {
e11e83f3 475 beg = scm_to_int (a->first);
ab4f3efb
MD
476 end = beg - n + 1;
477 if (end < 0)
478 end = 0;
479 }
480 else
481 {
482 beg = n - 1;
483 end = 0;
484 if (beg >= n_frames)
485 beg = n_frames - 1;
486 }
487 n = beg - end + 1;
488 }
bdf8afff
MD
489 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
490 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
ab4f3efb
MD
491
492 /* Create a string port used for adaptation of printing parameters. */
0b2c2ba3 493 sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
ab4f3efb 494 SCM_OPN | SCM_WRTNG,
1bbd0b84 495 FUNC_NAME);
ab4f3efb
MD
496
497 /* Create a print state for printing of frames. */
498 print_state = scm_make_print_state ();
499 pstate = SCM_PRINT_STATE (print_state);
500 pstate->writingp = 1;
501 pstate->fancyp = 1;
2b55be75 502 pstate->highlight_objects = a->highlight_objects;
ab4f3efb 503
ab4f3efb 504 /* Determine size of frame number field. */
aa3f6951 505 j = end;
ab4f3efb
MD
506 for (i = 0; j > 0; ++i) j /= 10;
507 nfield = i ? i : 1;
508
ab4f3efb 509 /* Print frames. */
ab4f3efb 510 indentation = 1;
fec097f0 511 last_file = SCM_UNDEFINED;
aa3f6951
AW
512 if (SCM_BACKWARDS_P)
513 end++;
514 else
515 end--;
516 for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i)
ab4f3efb 517 {
aa3f6951 518 frame = scm_stack_ref (a->stack, scm_from_int (i));
bc36d050 519 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
fec097f0 520 display_backtrace_file (frame, &last_file, a->port, pstate);
aa3f6951 521 display_frame (frame, i, nfield, indentation, sport, a->port, pstate);
ab4f3efb 522 }
bdf8afff 523
463b2219
ML
524 scm_remember_upto_here_1 (print_state);
525
bdf8afff
MD
526 return SCM_UNSPECIFIED;
527}
1bbd0b84 528#undef FUNC_NAME
bdf8afff 529
9ddf197e
AW
530static SCM
531error_during_backtrace (void *data, SCM tag, SCM throw_args)
532{
21041372 533 SCM port = SCM_PACK_POINTER (data);
9ddf197e 534
0607ebbf 535 scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port);
9ddf197e
AW
536 scm_print_exception (port, SCM_BOOL_F, tag, throw_args);
537
538 return SCM_UNSPECIFIED;
539}
540
541
09c6d80a 542SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
2b55be75 543 (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
b5944f66 544 "Display a backtrace to the output port @var{port}. @var{stack}\n"
c73bdd3a 545 "is the stack to take the backtrace from, @var{first} specifies\n"
b5944f66
NJ
546 "where in the stack to start and @var{depth} how many frames\n"
547 "to display. @var{first} and @var{depth} can be @code{#f},\n"
c648c681 548 "which means that default values will be used.\n"
b5944f66
NJ
549 "If @var{highlights} is given it should be a list; the elements\n"
550 "of this list will be highlighted wherever they appear in the\n"
551 "backtrace.")
2b55be75 552#define FUNC_NAME s_scm_display_backtrace_with_highlights
bdf8afff 553{
0b2cb4ee 554 struct display_backtrace_args a;
0b2cb4ee
MD
555 a.stack = stack;
556 a.port = port;
557 a.first = first;
558 a.depth = depth;
2b55be75
MV
559 if (SCM_UNBNDP (highlights))
560 a.highlight_objects = SCM_EOL;
561 else
562 a.highlight_objects = highlights;
9ddf197e 563
bdf8afff 564 scm_internal_catch (SCM_BOOL_T,
92c2555f 565 (scm_t_catch_body) display_backtrace_body, &a,
21041372 566 (scm_t_catch_handler) error_during_backtrace, SCM_UNPACK_POINTER (port));
9ddf197e 567
ab4f3efb
MD
568 return SCM_UNSPECIFIED;
569}
1bbd0b84 570#undef FUNC_NAME
ab4f3efb 571
2b55be75
MV
572SCM
573scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
574{
575 return scm_display_backtrace_with_highlights (stack, port, first, depth,
576 SCM_EOL);
577}
578
86d31dfe 579SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
5aab5d96 580
2b55be75
MV
581SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
582 (SCM highlights),
ec16eb78
AW
583 "Display a backtrace of the current stack to the current\n"
584 "output port. If @var{highlights} is given, it should be\n"
585 "a list; the elements of this list will be highlighted\n"
586 "wherever they appear in the backtrace.")
2b55be75 587#define FUNC_NAME s_scm_backtrace_with_highlights
5aab5d96 588{
9de87eea 589 SCM port = scm_current_output_port ();
ec16eb78
AW
590 SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
591
2b55be75
MV
592 if (SCM_UNBNDP (highlights))
593 highlights = SCM_EOL;
594
ec16eb78 595 scm_newline (port);
0607ebbf 596 scm_puts_unlocked ("Backtrace:\n", port);
ec16eb78
AW
597 scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
598 highlights);
599 scm_newline (port);
600
5aab5d96
MD
601 return SCM_UNSPECIFIED;
602}
1bbd0b84 603#undef FUNC_NAME
5aab5d96 604
2b55be75
MV
605SCM
606scm_backtrace (void)
607{
608 return scm_backtrace_with_highlights (SCM_EOL);
609}
610
ab4f3efb
MD
611\f
612
613void
614scm_init_backtrace ()
615{
e8df456a 616 scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception);
a0599745 617#include "libguile/backtrace.x"
ab4f3efb 618}
89e00824
ML
619
620/*
621 Local Variables:
622 c-file-style: "gnu"
623 End:
624*/