*** empty log message ***
[bpt/guile.git] / libguile / backtrace.c
CommitLineData
ab4f3efb 1/* Printing of backtraces and error messages
d90ca38d 2 * Copyright (C) 1996,1997,1998 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
MD
45
46#include <stdio.h>
47#include "_scm.h"
48#include "stacks.h"
49#include "srcprop.h"
50#include "genio.h"
51#include "struct.h"
52#include "strports.h"
bdf8afff 53#include "throw.h"
a5d6d578 54#include "fluids.h"
ab4f3efb
MD
55
56#include "backtrace.h"
57
58/* {Error reporting and backtraces}
59 * (A first approximation.)
60 *
61 * Note that these functions shouldn't generate errors themselves.
62 */
63
64#ifndef SCM_RECKLESS
65#undef SCM_ASSERT
66#define SCM_ASSERT(_cond, _arg, _pos, _subr) \
67 if (!(_cond)) \
68 return SCM_BOOL_F;
69#endif
70
b6609fc7
MD
71SCM scm_the_last_stack_fluid;
72
ab4f3efb
MD
73static void display_header SCM_P ((SCM source, SCM port));
74static void
75display_header (source, port)
76 SCM source;
77 SCM port;
78{
79 SCM fname = (SCM_NIMP (source) && SCM_MEMOIZEDP (source)
80 ? scm_source_property (source, scm_i_filename)
81 : SCM_BOOL_F);
82 if (SCM_NIMP (fname) && SCM_STRINGP (fname))
83 {
84 scm_prin1 (fname, port, 0);
b7f3516f 85 scm_putc (':', port);
0e929db3
MD
86 scm_intprint (SCM_INUM (scm_source_property (source, scm_i_line)) + 1,
87 10,
88 port);
b7f3516f 89 scm_putc (':', port);
0e929db3
MD
90 scm_intprint (SCM_INUM (scm_source_property (source, scm_i_column)) + 1,
91 10,
92 port);
ab4f3efb
MD
93 }
94 else
b7f3516f
TT
95 scm_puts ("ERROR", port);
96 scm_puts (": ", port);
ab4f3efb
MD
97}
98
f3acc5c1
JB
99
100void
101scm_display_error_message (message, args, port)
ab4f3efb
MD
102 SCM message;
103 SCM args;
104 SCM port;
105{
106 int writingp;
107 char *start;
108 char *p;
109
89958ad0 110 if (SCM_IMP (message) || !SCM_ROSTRINGP (message) || SCM_IMP (args)
c37e0e55 111 || !scm_list_p (args))
ab4f3efb
MD
112 {
113 scm_prin1 (message, port, 0);
b7f3516f 114 scm_putc ('\n', port);
ab4f3efb
MD
115 return;
116 }
117
89958ad0
JB
118 SCM_COERCE_SUBSTR (message);
119 start = SCM_ROCHARS (message);
ab4f3efb
MD
120 for (p = start; *p != '\0'; ++p)
121 if (*p == '%')
122 {
123 if (SCM_IMP (args) || SCM_NCONSP (args))
124 continue;
125
126 ++p;
127 if (*p == 's')
128 writingp = 0;
129 else if (*p == 'S')
130 writingp = 1;
131 else
132 continue;
133
b7f3516f 134 scm_lfwrite (start, p - start - 1, port);
ab4f3efb
MD
135 scm_prin1 (SCM_CAR (args), port, writingp);
136 args = SCM_CDR (args);
137 start = p + 1;
138 }
b7f3516f
TT
139 scm_lfwrite (start, p - start, port);
140 scm_putc ('\n', port);
ab4f3efb
MD
141}
142
143static void display_expression SCM_P ((SCM frame, SCM pname, SCM source, SCM port));
144static void
145display_expression (frame, pname, source, port)
146 SCM frame;
147 SCM pname;
148 SCM source;
149 SCM port;
150{
151 SCM print_state = scm_make_print_state ();
152 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
153 pstate->writingp = 0;
154 pstate->fancyp = 1;
155 pstate->level = 2;
156 pstate->length = 3;
157 if (SCM_NIMP (pname) && SCM_ROSTRINGP (pname))
158 {
159 if (SCM_NIMP (frame)
160 && SCM_FRAMEP (frame)
161 && SCM_FRAME_EVAL_ARGS_P (frame))
b7f3516f 162 scm_puts ("While evaluating arguments to ", port);
ab4f3efb 163 else
b7f3516f 164 scm_puts ("In procedure ", port);
ab4f3efb
MD
165 scm_iprin1 (pname, port, pstate);
166 if (SCM_NIMP (source) && SCM_MEMOIZEDP (source))
167 {
b7f3516f 168 scm_puts (" in expression ", port);
ab4f3efb
MD
169 pstate->writingp = 1;
170 scm_iprin1 (scm_unmemoize (source), port, pstate);
171 }
172 }
173 else if (SCM_NIMP (source))
174 {
b7f3516f 175 scm_puts ("In expression ", port);
ab4f3efb
MD
176 pstate->writingp = 1;
177 scm_iprin1 (scm_unmemoize (source), port, pstate);
178 }
b7f3516f 179 scm_puts (":\n", port);
ab4f3efb
MD
180 scm_free_print_state (print_state);
181}
182
bdf8afff
MD
183struct display_error_args {
184 SCM stack;
185 SCM port;
186 SCM subr;
187 SCM message;
188 SCM args;
189 SCM rest;
190};
191
192static SCM
39752bec 193display_error_body (struct display_error_args *a)
ab4f3efb
MD
194{
195 SCM current_frame = SCM_BOOL_F;
196 SCM source = SCM_BOOL_F;
197 SCM pname = SCM_BOOL_F;
a88a4c8a
JB
198 SCM prev_frame = SCM_BOOL_F;
199
841076ac 200 if (SCM_DEBUGGINGP
bdf8afff
MD
201 && SCM_NIMP (a->stack)
202 && SCM_STACKP (a->stack)
203 && SCM_STACK_LENGTH (a->stack) > 0)
ab4f3efb 204 {
bdf8afff 205 current_frame = scm_stack_ref (a->stack, SCM_INUM0);
ab4f3efb 206 source = SCM_FRAME_SOURCE (current_frame);
a88a4c8a
JB
207 prev_frame = SCM_FRAME_PREV (current_frame);
208 if (!(SCM_NIMP (source) && SCM_MEMOIZEDP (source))
209 && prev_frame != SCM_BOOL_F)
210 source = SCM_FRAME_SOURCE (prev_frame);
ab4f3efb 211 if (SCM_FRAME_PROC_P (current_frame)
016e2ce1 212 && scm_procedure_p (SCM_FRAME_PROC (current_frame)) == SCM_BOOL_T)
ab4f3efb
MD
213 pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
214 }
215 if (!(SCM_NIMP (pname) && SCM_ROSTRINGP (pname)))
bdf8afff 216 pname = a->subr;
e40a5fc8 217 if ((SCM_NIMP (pname) && SCM_ROSTRINGP (pname))
26e19854 218 || (SCM_NIMP (source) && SCM_MEMOIZEDP (source)))
ab4f3efb 219 {
bdf8afff
MD
220 display_header (source, a->port);
221 display_expression (current_frame, pname, source, a->port);
ab4f3efb 222 }
bdf8afff
MD
223 display_header (source, a->port);
224 scm_display_error_message (a->message, a->args, a->port);
225 return SCM_UNSPECIFIED;
226}
227
228struct display_error_handler_data {
229 char *mode;
230 SCM port;
231};
232
233/* This is the exception handler for error reporting routines.
234 Note that it is very important that this handler *doesn't* try to
235 print more than the error tag, since the error very probably is
236 caused by an erroneous print call-back routine. If we would
237 tru to print all objects, we would enter an infinite loop. */
238static SCM
239display_error_handler (struct display_error_handler_data *data,
240 SCM tag, SCM args)
241{
242 SCM print_state = scm_make_print_state ();
b7f3516f
TT
243 scm_puts ("\nException during displaying of ", data->port);
244 scm_puts (data->mode, data->port);
245 scm_puts (": ", data->port);
bdf8afff 246 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
b7f3516f 247 scm_putc ('\n', data->port);
bdf8afff
MD
248 return SCM_UNSPECIFIED;
249}
250
251SCM_PROC(s_display_error, "display-error", 6, 0, 0, scm_display_error);
252SCM
253scm_display_error (stack, port, subr, message, args, rest)
254 SCM stack;
255 SCM port;
256 SCM subr;
257 SCM message;
258 SCM args;
259 SCM rest;
260{
0b2cb4ee
MD
261 struct display_error_args a;
262 struct display_error_handler_data data;
263 a.stack = stack;
264 a.port = port;
265 a.subr = subr;
266 a.message = message;
267 a.args = args;
268 a.rest = rest;
269 data.mode = "error";
270 data.port = port;
bdf8afff
MD
271 scm_internal_catch (SCM_BOOL_T,
272 (scm_catch_body_t) display_error_body, &a,
273 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
274 return SCM_UNSPECIFIED;
275}
276
277static void indent SCM_P ((int n, SCM port));
278static void
279indent (n, port)
280 int n;
281 SCM port;
282{
283 int i;
284 for (i = 0; i < n; ++i)
b7f3516f 285 scm_putc (' ', port);
ab4f3efb
MD
286}
287
288static void display_frame_expr SCM_P ((char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate));
289static void
290display_frame_expr (hdr, exp, tlr, indentation, sport, port, pstate)
291 char *hdr;
292 SCM exp;
293 char *tlr;
294 int indentation;
295 SCM sport;
296 SCM port;
297 scm_print_state *pstate;
298{
ab4f3efb
MD
299 if (SCM_NIMP (exp) && SCM_CONSP (exp))
300 {
301 scm_iprlist (hdr, exp, tlr[0], port, pstate);
b7f3516f 302 scm_puts (&tlr[1], port);
ab4f3efb
MD
303 }
304 else
305 scm_iprin1 (exp, port, pstate);
b7f3516f 306 scm_putc ('\n', port);
ab4f3efb
MD
307}
308
e3c37929
MD
309static void display_application SCM_P ((SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate));
310static void
311display_application (frame, indentation, sport, port, pstate)
312 SCM frame;
313 int indentation;
314 SCM sport;
315 SCM port;
316 scm_print_state *pstate;
317{
318 SCM proc = SCM_FRAME_PROC (frame);
319 SCM name = (SCM_NFALSEP (scm_procedure_p (proc))
320 ? scm_procedure_name (proc)
321 : SCM_BOOL_F);
322 display_frame_expr ("[",
323 scm_cons (SCM_NFALSEP (name) ? name : proc,
324 SCM_FRAME_ARGS (frame)),
325 SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
326 indentation,
327 sport,
328 port,
329 pstate);
330}
331
332SCM_PROC(s_display_application, "display-application", 1, 1, 0, scm_display_application);
333
334SCM
335scm_display_application (SCM frame, SCM port)
336{
337 if (SCM_UNBNDP (port))
338 port = scm_cur_outp;
339 if (SCM_FRAME_PROC_P (frame))
340 /* Display an application. */
341 {
342 SCM print_state;
343 scm_print_state *pstate;
344
345 /* Create a print state for printing of frames. */
346 print_state = scm_make_print_state ();
347 pstate = SCM_PRINT_STATE (print_state);
348 pstate->writingp = 1;
349 pstate->fancyp = 1;
350 pstate->level = 2;
351 pstate->length = 9;
352
353 display_application (frame, 0, SCM_BOOL_F, port, pstate); /*fixme*/
354 return SCM_BOOL_T;
355 }
356 else
357 return SCM_BOOL_F;
358}
359
ab4f3efb
MD
360static void display_frame SCM_P ((SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate));
361static void
362display_frame (frame, nfield, indentation, sport, port, pstate)
363 SCM frame;
364 int nfield;
365 int indentation;
366 SCM sport;
367 SCM port;
368 scm_print_state *pstate;
369{
370 int n, i, j;
371
372 /* Announce missing frames? */
373 if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
374 {
375 indent (nfield + 1 + indentation, port);
b7f3516f 376 scm_puts ("...\n", port);
ab4f3efb
MD
377 }
378
379 /* Check size of frame number. */
380 n = SCM_FRAME_NUMBER (frame);
381 for (i = 0, j = n; j > 0; ++i) j /= 10;
382
383 /* Number indentation. */
384 indent (nfield - (i ? i : 1), port);
385
386 /* Frame number. */
387 scm_iprin1 (SCM_MAKINUM (n), port, pstate);
388
389 /* Real frame marker */
b7f3516f 390 scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
ab4f3efb
MD
391
392 /* Indentation. */
393 indent (indentation, port);
394
395 if (SCM_FRAME_PROC_P (frame))
396 /* Display an application. */
e3c37929 397 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
ab4f3efb
MD
398 else
399 /* Display a special form. */
400 {
401 SCM source = SCM_FRAME_SOURCE (frame);
402 SCM copy = scm_source_property (source, scm_i_copy);
403 display_frame_expr ("(",
404 SCM_NIMP (copy) && SCM_CONSP (copy)
405 ? copy
406 : scm_unmemoize (source),
407 ")",
408 nfield + 1 + indentation,
409 sport,
410 port,
411 pstate);
412 }
413
414 /* Announce missing frames? */
415 if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
416 {
417 indent (nfield + 1 + indentation, port);
b7f3516f 418 scm_puts ("...\n", port);
ab4f3efb
MD
419 }
420}
421
bdf8afff
MD
422struct display_backtrace_args {
423 SCM stack;
424 SCM port;
425 SCM first;
426 SCM depth;
427};
428
ab4f3efb 429SCM_PROC(s_display_backtrace, "display-backtrace", 2, 2, 0, scm_display_backtrace);
bdf8afff
MD
430
431static SCM
39752bec 432display_backtrace_body (struct display_backtrace_args *a)
ab4f3efb
MD
433{
434 int n_frames, beg, end, n, i, j;
435 int nfield, indent_p, indentation;
436 SCM frame, sport, print_state;
437 scm_print_state *pstate;
438
78446828
MV
439 a->port = SCM_COERCE_OUTPORT (a->port);
440
ab4f3efb 441 /* Argument checking and extraction. */
bdf8afff
MD
442 SCM_ASSERT (SCM_NIMP (a->stack) && SCM_STACKP (a->stack),
443 a->stack,
ab4f3efb
MD
444 SCM_ARG1,
445 s_display_backtrace);
bdf8afff
MD
446 SCM_ASSERT (SCM_NIMP (a->port) && SCM_OPOUTPORTP (a->port),
447 a->port,
ab4f3efb
MD
448 SCM_ARG2,
449 s_display_backtrace);
bdf8afff
MD
450 n_frames = SCM_INUM (scm_stack_length (a->stack));
451 n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
ab4f3efb
MD
452 if (SCM_BACKWARDS_P)
453 {
bdf8afff 454 beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
ab4f3efb
MD
455 end = beg + n - 1;
456 if (end >= n_frames)
457 end = n_frames - 1;
458 n = end - beg + 1;
459 }
460 else
461 {
bdf8afff 462 if (SCM_INUMP (a->first))
ab4f3efb 463 {
bdf8afff 464 beg = SCM_INUM (a->first);
ab4f3efb
MD
465 end = beg - n + 1;
466 if (end < 0)
467 end = 0;
468 }
469 else
470 {
471 beg = n - 1;
472 end = 0;
473 if (beg >= n_frames)
474 beg = n_frames - 1;
475 }
476 n = beg - end + 1;
477 }
bdf8afff
MD
478 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
479 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
ab4f3efb
MD
480
481 /* Create a string port used for adaptation of printing parameters. */
482 sport = scm_mkstrport (SCM_INUM0,
483 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
484 SCM_OPN | SCM_WRTNG,
485 s_display_backtrace);
486
487 /* Create a print state for printing of frames. */
488 print_state = scm_make_print_state ();
489 pstate = SCM_PRINT_STATE (print_state);
490 pstate->writingp = 1;
491 pstate->fancyp = 1;
e3c37929
MD
492 pstate->level = 2;
493 pstate->length = 3;
ab4f3efb
MD
494
495 /* First find out if it's reasonable to do indentation. */
496 if (SCM_BACKWARDS_P)
497 indent_p = 0;
498 else
499 {
500 indent_p = 1;
bdf8afff 501 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb
MD
502 for (i = 0, j = 0; i < n; ++i)
503 {
504 if (SCM_FRAME_REAL_P (frame))
505 ++j;
506 if (j > SCM_BACKTRACE_INDENT)
507 {
508 indent_p = 0;
509 break;
510 }
511 frame = (SCM_BACKWARDS_P
512 ? SCM_FRAME_PREV (frame)
513 : SCM_FRAME_NEXT (frame));
514 }
515 }
516
517 /* Determine size of frame number field. */
bdf8afff 518 j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
ab4f3efb
MD
519 for (i = 0; j > 0; ++i) j /= 10;
520 nfield = i ? i : 1;
521
b7f3516f 522 scm_puts ("Backtrace:\n", a->port);
ab4f3efb
MD
523
524 /* Print frames. */
bdf8afff 525 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb 526 indentation = 1;
bdf8afff 527 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb
MD
528 for (i = 1; i < n; ++i)
529 {
530 if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
531 ++indentation;
532 frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame);
bdf8afff 533 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb 534 }
bdf8afff
MD
535
536 return SCM_UNSPECIFIED;
537}
538
539SCM
540scm_display_backtrace (stack, port, first, depth)
541 SCM stack;
542 SCM port;
543 SCM first;
544 SCM depth;
545{
0b2cb4ee
MD
546 struct display_backtrace_args a;
547 struct display_error_handler_data data;
548 a.stack = stack;
549 a.port = port;
550 a.first = first;
551 a.depth = depth;
552 data.mode = "backtrace";
553 data.port = port;
bdf8afff
MD
554 scm_internal_catch (SCM_BOOL_T,
555 (scm_catch_body_t) display_backtrace_body, &a,
556 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
557 return SCM_UNSPECIFIED;
558}
559
78f9f47b 560SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
5aab5d96
MD
561
562SCM_PROC(s_backtrace, "backtrace", 0, 0, 0, scm_backtrace);
563SCM
564scm_backtrace ()
565{
b6609fc7 566 SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid));
a5d6d578 567 if (SCM_NFALSEP (the_last_stack))
5aab5d96
MD
568 {
569 scm_newline (scm_cur_outp);
a5d6d578 570 scm_display_backtrace (the_last_stack,
5aab5d96
MD
571 scm_cur_outp,
572 SCM_UNDEFINED,
573 SCM_UNDEFINED);
574 scm_newline (scm_cur_outp);
575 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var))
576 && !SCM_BACKTRACE_P)
577 {
b7f3516f
TT
578 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
579 "a backtrace\n"
580 "automatically if an error occurs in the future.\n",
581 scm_cur_outp);
5aab5d96
MD
582 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
583 }
584 }
585 else
586 {
b7f3516f 587 scm_puts ("No backtrace available.\n", scm_cur_outp);
5aab5d96
MD
588 }
589 return SCM_UNSPECIFIED;
590}
591
ab4f3efb
MD
592\f
593
594void
595scm_init_backtrace ()
596{
a5d6d578 597 SCM f = scm_make_fluid ();
b6609fc7 598 scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f);
5aab5d96 599
ab4f3efb
MD
600#include "backtrace.x"
601}