* Makefile.am (DEFS): Added. automake adds -I options to DEFS,
[bpt/guile.git] / libguile / backtrace.c
1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation
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
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
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
44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
45
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
50 #include <stdio.h>
51 #include <ctype.h>
52
53 #include "libguile/_scm.h"
54
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #endif
58
59 #include "libguile/stacks.h"
60 #include "libguile/srcprop.h"
61 #include "libguile/struct.h"
62 #include "libguile/strports.h"
63 #include "libguile/throw.h"
64 #include "libguile/fluids.h"
65 #include "libguile/ports.h"
66 #include "libguile/strings.h"
67
68 #include "libguile/validate.h"
69 #include "libguile/backtrace.h"
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
84 SCM scm_the_last_stack_fluid;
85
86 static void
87 display_header (SCM source, SCM port)
88 {
89 SCM fname = (SCM_MEMOIZEDP (source)
90 ? scm_source_property (source, scm_sym_filename)
91 : SCM_BOOL_F);
92 if (SCM_STRINGP (fname))
93 {
94 scm_prin1 (fname, port, 0);
95 scm_putc (':', port);
96 scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_line)) + 1,
97 10,
98 port);
99 scm_putc (':', port);
100 scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_column)) + 1,
101 10,
102 port);
103 }
104 else
105 scm_puts ("ERROR", port);
106 scm_puts (": ", port);
107 }
108
109
110 void
111 scm_display_error_message (SCM message, SCM args, SCM port)
112 {
113 if (SCM_ROSTRINGP (message) && SCM_NFALSEP (scm_list_p (args)))
114 {
115 scm_simple_format (port, message, args);
116 scm_newline (port);
117 }
118 else
119 {
120 scm_prin1 (message, port, 0);
121 scm_putc ('\n', port);
122 }
123 }
124
125 static void
126 display_expression (SCM frame,SCM pname,SCM source,SCM port)
127 {
128 SCM print_state = scm_make_print_state ();
129 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
130 pstate->writingp = 0;
131 pstate->fancyp = 1;
132 pstate->level = 2;
133 pstate->length = 3;
134 if (SCM_ROSTRINGP (pname))
135 {
136 if (SCM_FRAMEP (frame)
137 && SCM_FRAME_EVAL_ARGS_P (frame))
138 scm_puts ("While evaluating arguments to ", port);
139 else
140 scm_puts ("In procedure ", port);
141 scm_iprin1 (pname, port, pstate);
142 if (SCM_MEMOIZEDP (source))
143 {
144 scm_puts (" in expression ", port);
145 pstate->writingp = 1;
146 scm_iprin1 (scm_unmemoize (source), port, pstate);
147 }
148 }
149 else if (SCM_NIMP (source))
150 {
151 scm_puts ("In expression ", port);
152 pstate->writingp = 1;
153 scm_iprin1 (scm_unmemoize (source), port, pstate);
154 }
155 scm_puts (":\n", port);
156 scm_free_print_state (print_state);
157 }
158
159 struct display_error_args {
160 SCM stack;
161 SCM port;
162 SCM subr;
163 SCM message;
164 SCM args;
165 SCM rest;
166 };
167
168 static SCM
169 display_error_body (struct display_error_args *a)
170 {
171 SCM current_frame = SCM_BOOL_F;
172 SCM source = SCM_BOOL_F;
173 SCM pname = SCM_BOOL_F;
174 SCM prev_frame = SCM_BOOL_F;
175
176 if (SCM_DEBUGGINGP
177 && SCM_STACKP (a->stack)
178 && SCM_STACK_LENGTH (a->stack) > 0)
179 {
180 current_frame = scm_stack_ref (a->stack, SCM_INUM0);
181 source = SCM_FRAME_SOURCE (current_frame);
182 prev_frame = SCM_FRAME_PREV (current_frame);
183 if (!SCM_MEMOIZEDP (source) && !SCM_FALSEP (prev_frame))
184 source = SCM_FRAME_SOURCE (prev_frame);
185 if (SCM_FRAME_PROC_P (current_frame)
186 && SCM_TRUE_P (scm_procedure_p (SCM_FRAME_PROC (current_frame))))
187 pname = scm_procedure_name (SCM_FRAME_PROC (current_frame));
188 }
189 if (!SCM_ROSTRINGP (pname))
190 pname = a->subr;
191 if (SCM_ROSTRINGP (pname) || SCM_MEMOIZEDP (source))
192 {
193 display_header (source, a->port);
194 display_expression (current_frame, pname, source, a->port);
195 }
196 display_header (source, a->port);
197 scm_display_error_message (a->message, a->args, a->port);
198 return SCM_UNSPECIFIED;
199 }
200
201 struct display_error_handler_data {
202 char *mode;
203 SCM port;
204 };
205
206 /* This is the exception handler for error reporting routines.
207 Note that it is very important that this handler *doesn't* try to
208 print more than the error tag, since the error very probably is
209 caused by an erroneous print call-back routine. If we would
210 try to print all objects, we would enter an infinite loop. */
211 static SCM
212 display_error_handler (struct display_error_handler_data *data,
213 SCM tag, SCM args)
214 {
215 SCM print_state = scm_make_print_state ();
216 scm_puts ("\nException during displaying of ", data->port);
217 scm_puts (data->mode, data->port);
218 scm_puts (": ", data->port);
219 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
220 scm_putc ('\n', data->port);
221 return SCM_UNSPECIFIED;
222 }
223
224 SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
225 (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
226 "")
227 #define FUNC_NAME s_scm_display_error
228 {
229 struct display_error_args a;
230 struct display_error_handler_data data;
231 a.stack = stack;
232 a.port = port;
233 a.subr = subr;
234 a.message = message;
235 a.args = args;
236 a.rest = rest;
237 data.mode = "error";
238 data.port = port;
239 scm_internal_catch (SCM_BOOL_T,
240 (scm_catch_body_t) display_error_body, &a,
241 (scm_catch_handler_t) display_error_handler, &data);
242 return SCM_UNSPECIFIED;
243 }
244 #undef FUNC_NAME
245
246 typedef struct {
247 int level;
248 int length;
249 } print_params_t;
250
251 static int n_print_params = 9;
252 static print_params_t default_print_params[] = {
253 { 4, 9 }, { 4, 3 },
254 { 3, 4 }, { 3, 3 },
255 { 2, 4 }, { 2, 3 },
256 { 1, 4 }, { 1, 3 }, { 1, 2 }
257 };
258 static print_params_t *print_params = default_print_params;
259
260 #ifdef GUILE_DEBUG
261 SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
262 (SCM params),
263 "")
264 #define FUNC_NAME s_scm_set_print_params_x
265 {
266 int i;
267 int n;
268 SCM ls;
269 print_params_t *new_params;
270
271 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
272 for (ls = params; SCM_NNULLP (ls); ls = SCM_CDR (ls))
273 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
274 && SCM_INUMP (SCM_CAAR (ls))
275 && SCM_INUM (SCM_CAAR (ls)) >= 0
276 && SCM_INUMP (SCM_CADAR (ls))
277 && SCM_INUM (SCM_CADAR (ls)) >= 0,
278 params,
279 SCM_ARG2,
280 s_scm_set_print_params_x);
281 new_params = scm_must_malloc (n * sizeof (print_params_t),
282 FUNC_NAME);
283 if (print_params != default_print_params)
284 scm_must_free (print_params);
285 print_params = new_params;
286 for (i = 0; i < n; ++i)
287 {
288 print_params[i].level = SCM_INUM (SCM_CAAR (params));
289 print_params[i].length = SCM_INUM (SCM_CADAR (params));
290 params = SCM_CDR (params);
291 }
292 n_print_params = n;
293 return SCM_UNSPECIFIED;
294 }
295 #undef FUNC_NAME
296 #endif
297
298 static void
299 indent (int n, SCM port)
300 {
301 int i;
302 for (i = 0; i < n; ++i)
303 scm_putc (' ', port);
304 }
305
306 static void
307 display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate)
308 {
309 SCM string;
310 int i = 0, n;
311 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
312 do
313 {
314 pstate->length = print_params[i].length;
315 ptob->seek (sport, 0, SEEK_SET);
316 if (SCM_CONSP (exp))
317 {
318 pstate->level = print_params[i].level - 1;
319 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
320 scm_puts (&tlr[1], sport);
321 }
322 else
323 {
324 pstate->level = print_params[i].level;
325 scm_iprin1 (exp, sport, pstate);
326 }
327 ptob->flush (sport);
328 n = ptob->seek (sport, 0, SEEK_CUR);
329 ++i;
330 }
331 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
332 ptob->truncate (sport, n);
333 string = scm_strport_to_string (sport);
334 /* Remove control characters */
335 for (i = 0; i < n; ++i)
336 if (iscntrl (SCM_CHARS (string)[i]))
337 SCM_CHARS (string)[i] = ' ';
338 /* Truncate */
339 if (indentation + n > SCM_BACKTRACE_WIDTH)
340 {
341 n = SCM_BACKTRACE_WIDTH - indentation;
342 SCM_CHARS (string)[n - 1] = '$';
343 }
344
345 scm_lfwrite (SCM_CHARS (string), n, port);
346 }
347
348 static void
349 display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate)
350 {
351 SCM proc = SCM_FRAME_PROC (frame);
352 SCM name = (SCM_NFALSEP (scm_procedure_p (proc))
353 ? scm_procedure_name (proc)
354 : SCM_BOOL_F);
355 display_frame_expr ("[",
356 scm_cons (SCM_NFALSEP (name) ? name : proc,
357 SCM_FRAME_ARGS (frame)),
358 SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]",
359 indentation,
360 sport,
361 port,
362 pstate);
363 }
364
365 SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
366 (SCM frame, SCM port, SCM indent),
367 "")
368 #define FUNC_NAME s_scm_display_application
369 {
370 SCM_VALIDATE_FRAME (1,frame);
371 if (SCM_UNBNDP (port))
372 port = scm_cur_outp;
373 else
374 SCM_VALIDATE_OPOUTPORT (2,port);
375 if (SCM_UNBNDP (indent))
376 indent = SCM_INUM0;
377 else
378 SCM_VALIDATE_INUM (3,indent);
379
380 if (SCM_FRAME_PROC_P (frame))
381 /* Display an application. */
382 {
383 SCM sport, print_state;
384 scm_print_state *pstate;
385
386 /* Create a string port used for adaptation of printing parameters. */
387 sport = scm_mkstrport (SCM_INUM0,
388 scm_make_string (SCM_MAKINUM (240),
389 SCM_UNDEFINED),
390 SCM_OPN | SCM_WRTNG,
391 FUNC_NAME);
392
393 /* Create a print state for printing of frames. */
394 print_state = scm_make_print_state ();
395 pstate = SCM_PRINT_STATE (print_state);
396 pstate->writingp = 1;
397 pstate->fancyp = 1;
398
399 display_application (frame, SCM_INUM (indent), sport, port, pstate);
400 return SCM_BOOL_T;
401 }
402 else
403 return SCM_BOOL_F;
404 }
405 #undef FUNC_NAME
406
407 static void
408 display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate)
409 {
410 int n, i, j;
411
412 /* Announce missing frames? */
413 if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
414 {
415 indent (nfield + 1 + indentation, port);
416 scm_puts ("...\n", port);
417 }
418
419 /* Check size of frame number. */
420 n = SCM_FRAME_NUMBER (frame);
421 for (i = 0, j = n; j > 0; ++i) j /= 10;
422
423 /* Number indentation. */
424 indent (nfield - (i ? i : 1), port);
425
426 /* Frame number. */
427 scm_iprin1 (SCM_MAKINUM (n), port, pstate);
428
429 /* Real frame marker */
430 scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port);
431
432 /* Indentation. */
433 indent (indentation, port);
434
435 if (SCM_FRAME_PROC_P (frame))
436 /* Display an application. */
437 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
438 else
439 /* Display a special form. */
440 {
441 SCM source = SCM_FRAME_SOURCE (frame);
442 SCM copy = (SCM_CONSP (source)
443 ? scm_source_property (source, scm_sym_copy)
444 : SCM_BOOL_F);
445 SCM umcopy = (SCM_MEMOIZEDP (source)
446 ? scm_unmemoize (source)
447 : SCM_BOOL_F);
448 display_frame_expr ("(",
449 SCM_CONSP (copy) ? copy : umcopy,
450 ")",
451 nfield + 1 + indentation,
452 sport,
453 port,
454 pstate);
455 }
456 scm_putc ('\n', port);
457
458 /* Announce missing frames? */
459 if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame))
460 {
461 indent (nfield + 1 + indentation, port);
462 scm_puts ("...\n", port);
463 }
464 }
465
466 struct display_backtrace_args {
467 SCM stack;
468 SCM port;
469 SCM first;
470 SCM depth;
471 };
472
473 static SCM
474 display_backtrace_body(struct display_backtrace_args *a)
475 #define FUNC_NAME "display_backtrace_body"
476 {
477 int n_frames, beg, end, n, i, j;
478 int nfield, indent_p, indentation;
479 SCM frame, sport, print_state;
480 scm_print_state *pstate;
481
482 a->port = SCM_COERCE_OUTPORT (a->port);
483
484 /* Argument checking and extraction. */
485 SCM_ASSERT (SCM_STACKP (a->stack),
486 a->stack,
487 SCM_ARG1,
488 s_display_backtrace);
489 SCM_ASSERT (SCM_OPOUTPORTP (a->port),
490 a->port,
491 SCM_ARG2,
492 s_display_backtrace);
493 n_frames = SCM_INUM (scm_stack_length (a->stack));
494 n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH;
495 if (SCM_BACKWARDS_P)
496 {
497 beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0;
498 end = beg + n - 1;
499 if (end >= n_frames)
500 end = n_frames - 1;
501 n = end - beg + 1;
502 }
503 else
504 {
505 if (SCM_INUMP (a->first))
506 {
507 beg = SCM_INUM (a->first);
508 end = beg - n + 1;
509 if (end < 0)
510 end = 0;
511 }
512 else
513 {
514 beg = n - 1;
515 end = 0;
516 if (beg >= n_frames)
517 beg = n_frames - 1;
518 }
519 n = beg - end + 1;
520 }
521 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
522 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
523
524 /* Create a string port used for adaptation of printing parameters. */
525 sport = scm_mkstrport (SCM_INUM0,
526 scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED),
527 SCM_OPN | SCM_WRTNG,
528 FUNC_NAME);
529
530 /* Create a print state for printing of frames. */
531 print_state = scm_make_print_state ();
532 pstate = SCM_PRINT_STATE (print_state);
533 pstate->writingp = 1;
534 pstate->fancyp = 1;
535
536 /* First find out if it's reasonable to do indentation. */
537 if (SCM_BACKWARDS_P)
538 indent_p = 0;
539 else
540 {
541 indent_p = 1;
542 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
543 for (i = 0, j = 0; i < n; ++i)
544 {
545 if (SCM_FRAME_REAL_P (frame))
546 ++j;
547 if (j > SCM_BACKTRACE_INDENT)
548 {
549 indent_p = 0;
550 break;
551 }
552 frame = (SCM_BACKWARDS_P
553 ? SCM_FRAME_PREV (frame)
554 : SCM_FRAME_NEXT (frame));
555 }
556 }
557
558 /* Determine size of frame number field. */
559 j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end)));
560 for (i = 0; j > 0; ++i) j /= 10;
561 nfield = i ? i : 1;
562
563 /* Print frames. */
564 frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg));
565 indentation = 1;
566 display_frame (frame, nfield, indentation, sport, a->port, pstate);
567 for (i = 1; i < n; ++i)
568 {
569 if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame))
570 ++indentation;
571 frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame);
572 display_frame (frame, nfield, indentation, sport, a->port, pstate);
573 }
574
575 return SCM_UNSPECIFIED;
576 }
577 #undef FUNC_NAME
578
579 SCM_DEFINE (scm_display_backtrace, "display-backtrace", 2, 2, 0,
580 (SCM stack, SCM port, SCM first, SCM depth),
581 "")
582 #define FUNC_NAME s_scm_display_backtrace
583 {
584 struct display_backtrace_args a;
585 struct display_error_handler_data data;
586 a.stack = stack;
587 a.port = port;
588 a.first = first;
589 a.depth = depth;
590 data.mode = "backtrace";
591 data.port = port;
592 scm_internal_catch (SCM_BOOL_T,
593 (scm_catch_body_t) display_backtrace_body, &a,
594 (scm_catch_handler_t) display_error_handler, &data);
595 return SCM_UNSPECIFIED;
596 }
597 #undef FUNC_NAME
598
599 SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
600
601 SCM_DEFINE (scm_backtrace, "backtrace", 0, 0, 0,
602 (),
603 "")
604 #define FUNC_NAME s_scm_backtrace
605 {
606 SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid));
607 if (SCM_NFALSEP (the_last_stack))
608 {
609 scm_newline (scm_cur_outp);
610 scm_puts ("Backtrace:\n", scm_cur_outp);
611 scm_display_backtrace (the_last_stack,
612 scm_cur_outp,
613 SCM_UNDEFINED,
614 SCM_UNDEFINED);
615 scm_newline (scm_cur_outp);
616 if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var))
617 && !SCM_BACKTRACE_P)
618 {
619 scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like "
620 "a backtrace\n"
621 "automatically if an error occurs in the future.\n",
622 scm_cur_outp);
623 SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T);
624 }
625 }
626 else
627 {
628 scm_puts ("No backtrace available.\n", scm_cur_outp);
629 }
630 return SCM_UNSPECIFIED;
631 }
632 #undef FUNC_NAME
633
634 \f
635
636 void
637 scm_init_backtrace ()
638 {
639 SCM f = scm_make_fluid ();
640 scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f);
641
642 #include "libguile/backtrace.x"
643 }
644
645 /*
646 Local Variables:
647 c-file-style: "gnu"
648 End:
649 */