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