*** 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;
841076ac 198 if (SCM_DEBUGGINGP
bdf8afff
MD
199 && SCM_NIMP (a->stack)
200 && SCM_STACKP (a->stack)
201 && SCM_STACK_LENGTH (a->stack) > 0)
ab4f3efb 202 {
bdf8afff 203 current_frame = scm_stack_ref (a->stack, SCM_INUM0);
ab4f3efb
MD
204 source = SCM_FRAME_SOURCE (current_frame);
205 if (!(SCM_NIMP (source) && SCM_MEMOIZEDP (source)))
206 source = SCM_FRAME_SOURCE (SCM_FRAME_PREV (current_frame));
207 if (SCM_FRAME_PROC_P (current_frame)
016e2ce1 208 && scm_procedure_p (SCM_FRAME_PROC (current_frame)) == SCM_BOOL_T)
ab4f3efb
MD
209 pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
210 }
211 if (!(SCM_NIMP (pname) && SCM_ROSTRINGP (pname)))
bdf8afff 212 pname = a->subr;
ab4f3efb
MD
213 if ((SCM_NIMP (source) && SCM_MEMOIZEDP (source))
214 || (SCM_NIMP (pname) && SCM_ROSTRINGP (pname)))
215 {
bdf8afff
MD
216 display_header (source, a->port);
217 display_expression (current_frame, pname, source, a->port);
ab4f3efb 218 }
bdf8afff
MD
219 display_header (source, a->port);
220 scm_display_error_message (a->message, a->args, a->port);
221 return SCM_UNSPECIFIED;
222}
223
224struct display_error_handler_data {
225 char *mode;
226 SCM port;
227};
228
229/* This is the exception handler for error reporting routines.
230 Note that it is very important that this handler *doesn't* try to
231 print more than the error tag, since the error very probably is
232 caused by an erroneous print call-back routine. If we would
233 tru to print all objects, we would enter an infinite loop. */
234static SCM
235display_error_handler (struct display_error_handler_data *data,
236 SCM tag, SCM args)
237{
238 SCM print_state = scm_make_print_state ();
b7f3516f
TT
239 scm_puts ("\nException during displaying of ", data->port);
240 scm_puts (data->mode, data->port);
241 scm_puts (": ", data->port);
bdf8afff 242 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
b7f3516f 243 scm_putc ('\n', data->port);
bdf8afff
MD
244 return SCM_UNSPECIFIED;
245}
246
247SCM_PROC(s_display_error, "display-error", 6, 0, 0, scm_display_error);
248SCM
249scm_display_error (stack, port, subr, message, args, rest)
250 SCM stack;
251 SCM port;
252 SCM subr;
253 SCM message;
254 SCM args;
255 SCM rest;
256{
0b2cb4ee
MD
257 struct display_error_args a;
258 struct display_error_handler_data data;
259 a.stack = stack;
260 a.port = port;
261 a.subr = subr;
262 a.message = message;
263 a.args = args;
264 a.rest = rest;
265 data.mode = "error";
266 data.port = port;
bdf8afff
MD
267 scm_internal_catch (SCM_BOOL_T,
268 (scm_catch_body_t) display_error_body, &a,
269 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
270 return SCM_UNSPECIFIED;
271}
272
273static void indent SCM_P ((int n, SCM port));
274static void
275indent (n, port)
276 int n;
277 SCM port;
278{
279 int i;
280 for (i = 0; i < n; ++i)
b7f3516f 281 scm_putc (' ', port);
ab4f3efb
MD
282}
283
284static void display_frame_expr SCM_P ((char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate));
285static void
286display_frame_expr (hdr, exp, tlr, indentation, sport, port, pstate)
287 char *hdr;
288 SCM exp;
289 char *tlr;
290 int indentation;
291 SCM sport;
292 SCM port;
293 scm_print_state *pstate;
294{
ab4f3efb
MD
295 if (SCM_NIMP (exp) && SCM_CONSP (exp))
296 {
297 scm_iprlist (hdr, exp, tlr[0], port, pstate);
b7f3516f 298 scm_puts (&tlr[1], port);
ab4f3efb
MD
299 }
300 else
301 scm_iprin1 (exp, port, pstate);
b7f3516f 302 scm_putc ('\n', port);
ab4f3efb
MD
303}
304
e3c37929
MD
305static void display_application SCM_P ((SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate));
306static void
307display_application (frame, indentation, sport, port, pstate)
308 SCM frame;
309 int indentation;
310 SCM sport;
311 SCM port;
312 scm_print_state *pstate;
313{
314 SCM proc = SCM_FRAME_PROC (frame);
315 SCM name = (SCM_NFALSEP (scm_procedure_p (proc))
316 ? scm_procedure_name (proc)
317 : SCM_BOOL_F);
318 display_frame_expr ("[",
319 scm_cons (SCM_NFALSEP (name) ? name : proc,
320 SCM_FRAME_ARGS (frame)),
321 SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
322 indentation,
323 sport,
324 port,
325 pstate);
326}
327
328SCM_PROC(s_display_application, "display-application", 1, 1, 0, scm_display_application);
329
330SCM
331scm_display_application (SCM frame, SCM port)
332{
333 if (SCM_UNBNDP (port))
334 port = scm_cur_outp;
335 if (SCM_FRAME_PROC_P (frame))
336 /* Display an application. */
337 {
338 SCM print_state;
339 scm_print_state *pstate;
340
341 /* Create a print state for printing of frames. */
342 print_state = scm_make_print_state ();
343 pstate = SCM_PRINT_STATE (print_state);
344 pstate->writingp = 1;
345 pstate->fancyp = 1;
346 pstate->level = 2;
347 pstate->length = 9;
348
349 display_application (frame, 0, SCM_BOOL_F, port, pstate); /*fixme*/
350 return SCM_BOOL_T;
351 }
352 else
353 return SCM_BOOL_F;
354}
355
ab4f3efb
MD
356static void display_frame SCM_P ((SCM frame, int nfield, int indentation, SCM sport, SCM port, scm_print_state *pstate));
357static void
358display_frame (frame, nfield, indentation, sport, port, pstate)
359 SCM frame;
360 int nfield;
361 int indentation;
362 SCM sport;
363 SCM port;
364 scm_print_state *pstate;
365{
366 int n, i, j;
367
368 /* Announce missing frames? */
369 if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
370 {
371 indent (nfield + 1 + indentation, port);
b7f3516f 372 scm_puts ("...\n", port);
ab4f3efb
MD
373 }
374
375 /* Check size of frame number. */
376 n = SCM_FRAME_NUMBER (frame);
377 for (i = 0, j = n; j > 0; ++i) j /= 10;
378
379 /* Number indentation. */
380 indent (nfield - (i ? i : 1), port);
381
382 /* Frame number. */
383 scm_iprin1 (SCM_MAKINUM (n), port, pstate);
384
385 /* Real frame marker */
b7f3516f 386 scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
ab4f3efb
MD
387
388 /* Indentation. */
389 indent (indentation, port);
390
391 if (SCM_FRAME_PROC_P (frame))
392 /* Display an application. */
e3c37929 393 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
ab4f3efb
MD
394 else
395 /* Display a special form. */
396 {
397 SCM source = SCM_FRAME_SOURCE (frame);
398 SCM copy = scm_source_property (source, scm_i_copy);
399 display_frame_expr ("(",
400 SCM_NIMP (copy) && SCM_CONSP (copy)
401 ? copy
402 : scm_unmemoize (source),
403 ")",
404 nfield + 1 + indentation,
405 sport,
406 port,
407 pstate);
408 }
409
410 /* Announce missing frames? */
411 if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
412 {
413 indent (nfield + 1 + indentation, port);
b7f3516f 414 scm_puts ("...\n", port);
ab4f3efb
MD
415 }
416}
417
bdf8afff
MD
418struct display_backtrace_args {
419 SCM stack;
420 SCM port;
421 SCM first;
422 SCM depth;
423};
424
ab4f3efb 425SCM_PROC(s_display_backtrace, "display-backtrace", 2, 2, 0, scm_display_backtrace);
bdf8afff
MD
426
427static SCM
39752bec 428display_backtrace_body (struct display_backtrace_args *a)
ab4f3efb
MD
429{
430 int n_frames, beg, end, n, i, j;
431 int nfield, indent_p, indentation;
432 SCM frame, sport, print_state;
433 scm_print_state *pstate;
434
78446828
MV
435 a->port = SCM_COERCE_OUTPORT (a->port);
436
ab4f3efb 437 /* Argument checking and extraction. */
bdf8afff
MD
438 SCM_ASSERT (SCM_NIMP (a->stack) && SCM_STACKP (a->stack),
439 a->stack,
ab4f3efb
MD
440 SCM_ARG1,
441 s_display_backtrace);
bdf8afff
MD
442 SCM_ASSERT (SCM_NIMP (a->port) && SCM_OPOUTPORTP (a->port),
443 a->port,
ab4f3efb
MD
444 SCM_ARG2,
445 s_display_backtrace);
bdf8afff
MD
446 n_frames = SCM_INUM (scm_stack_length (a->stack));
447 n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
ab4f3efb
MD
448 if (SCM_BACKWARDS_P)
449 {
bdf8afff 450 beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
ab4f3efb
MD
451 end = beg + n - 1;
452 if (end >= n_frames)
453 end = n_frames - 1;
454 n = end - beg + 1;
455 }
456 else
457 {
bdf8afff 458 if (SCM_INUMP (a->first))
ab4f3efb 459 {
bdf8afff 460 beg = SCM_INUM (a->first);
ab4f3efb
MD
461 end = beg - n + 1;
462 if (end < 0)
463 end = 0;
464 }
465 else
466 {
467 beg = n - 1;
468 end = 0;
469 if (beg >= n_frames)
470 beg = n_frames - 1;
471 }
472 n = beg - end + 1;
473 }
bdf8afff
MD
474 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
475 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
ab4f3efb
MD
476
477 /* Create a string port used for adaptation of printing parameters. */
478 sport = scm_mkstrport (SCM_INUM0,
479 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
480 SCM_OPN | SCM_WRTNG,
481 s_display_backtrace);
482
483 /* Create a print state for printing of frames. */
484 print_state = scm_make_print_state ();
485 pstate = SCM_PRINT_STATE (print_state);
486 pstate->writingp = 1;
487 pstate->fancyp = 1;
e3c37929
MD
488 pstate->level = 2;
489 pstate->length = 3;
ab4f3efb
MD
490
491 /* First find out if it's reasonable to do indentation. */
492 if (SCM_BACKWARDS_P)
493 indent_p = 0;
494 else
495 {
496 indent_p = 1;
bdf8afff 497 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb
MD
498 for (i = 0, j = 0; i < n; ++i)
499 {
500 if (SCM_FRAME_REAL_P (frame))
501 ++j;
502 if (j > SCM_BACKTRACE_INDENT)
503 {
504 indent_p = 0;
505 break;
506 }
507 frame = (SCM_BACKWARDS_P
508 ? SCM_FRAME_PREV (frame)
509 : SCM_FRAME_NEXT (frame));
510 }
511 }
512
513 /* Determine size of frame number field. */
bdf8afff 514 j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
ab4f3efb
MD
515 for (i = 0; j > 0; ++i) j /= 10;
516 nfield = i ? i : 1;
517
b7f3516f 518 scm_puts ("Backtrace:\n", a->port);
ab4f3efb
MD
519
520 /* Print frames. */
bdf8afff 521 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
ab4f3efb 522 indentation = 1;
bdf8afff 523 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb
MD
524 for (i = 1; i < n; ++i)
525 {
526 if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
527 ++indentation;
528 frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame);
bdf8afff 529 display_frame (frame, nfield, indentation, sport, a->port, pstate);
ab4f3efb 530 }
bdf8afff
MD
531
532 return SCM_UNSPECIFIED;
533}
534
535SCM
536scm_display_backtrace (stack, port, first, depth)
537 SCM stack;
538 SCM port;
539 SCM first;
540 SCM depth;
541{
0b2cb4ee
MD
542 struct display_backtrace_args a;
543 struct display_error_handler_data data;
544 a.stack = stack;
545 a.port = port;
546 a.first = first;
547 a.depth = depth;
548 data.mode = "backtrace";
549 data.port = port;
bdf8afff
MD
550 scm_internal_catch (SCM_BOOL_T,
551 (scm_catch_body_t) display_backtrace_body, &a,
552 (scm_catch_handler_t) display_error_handler, &data);
ab4f3efb
MD
553 return SCM_UNSPECIFIED;
554}
555
78f9f47b 556SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
5aab5d96
MD
557
558SCM_PROC(s_backtrace, "backtrace", 0, 0, 0, scm_backtrace);
559SCM
560scm_backtrace ()
561{
b6609fc7 562 SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid));
a5d6d578 563 if (SCM_NFALSEP (the_last_stack))
5aab5d96
MD
564 {
565 scm_newline (scm_cur_outp);
a5d6d578 566 scm_display_backtrace (the_last_stack,
5aab5d96
MD
567 scm_cur_outp,
568 SCM_UNDEFINED,
569 SCM_UNDEFINED);
570 scm_newline (scm_cur_outp);
571 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var))
572 && !SCM_BACKTRACE_P)
573 {
b7f3516f
TT
574 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
575 "a backtrace\n"
576 "automatically if an error occurs in the future.\n",
577 scm_cur_outp);
5aab5d96
MD
578 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
579 }
580 }
581 else
582 {
b7f3516f 583 scm_puts ("No backtrace available.\n", scm_cur_outp);
5aab5d96
MD
584 }
585 return SCM_UNSPECIFIED;
586}
587
ab4f3efb
MD
588\f
589
590void
591scm_init_backtrace ()
592{
a5d6d578 593 SCM f = scm_make_fluid ();
b6609fc7 594 scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f);
5aab5d96 595
ab4f3efb
MD
596#include "backtrace.x"
597}