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