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