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