deprecate the-last-stack
[bpt/guile.git] / libguile / backtrace.c
1 /* Printing of backtraces and error messages
2 * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010 Free Software Foundation
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <ctype.h>
26
27 #include "libguile/_scm.h"
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_IO_H
33 #include <io.h>
34 #endif
35
36 #include "libguile/stacks.h"
37 #include "libguile/srcprop.h"
38 #include "libguile/struct.h"
39 #include "libguile/strports.h"
40 #include "libguile/throw.h"
41 #include "libguile/fluids.h"
42 #include "libguile/ports.h"
43 #include "libguile/strings.h"
44 #include "libguile/dynwind.h"
45 #include "libguile/frames.h"
46
47 #include "libguile/validate.h"
48 #include "libguile/backtrace.h"
49 #include "libguile/filesys.h"
50 #include "libguile/private-options.h"
51
52 /* {Error reporting and backtraces}
53 *
54 * Note that these functions shouldn't generate errors themselves.
55 */
56
57 /* Print parameters for error messages. */
58
59 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
60 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
61
62 /* Print parameters for failing expressions in error messages.
63 * (See also `print_params' below for backtrace print parameters.)
64 */
65
66 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
67 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
68
69 #undef SCM_ASSERT
70 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
71 if (!(_cond)) \
72 return SCM_BOOL_F;
73
74 static void
75 display_header (SCM source, SCM port)
76 {
77 scm_puts ("ERROR", port);
78 scm_puts (": ", port);
79 }
80
81
82 struct display_error_message_data {
83 SCM message;
84 SCM args;
85 SCM port;
86 scm_print_state *pstate;
87 int old_fancyp;
88 int old_level;
89 int old_length;
90 };
91
92 static SCM
93 display_error_message (struct display_error_message_data *d)
94 {
95 if (scm_is_string (d->message) && scm_is_true (scm_list_p (d->args)))
96 scm_simple_format (d->port, d->message, d->args);
97 else
98 scm_display (d->message, d->port);
99 scm_newline (d->port);
100 return SCM_UNSPECIFIED;
101 }
102
103 static void
104 before_display_error_message (struct display_error_message_data *d)
105 {
106 scm_print_state *pstate = d->pstate;
107 d->old_fancyp = pstate->fancyp;
108 d->old_level = pstate->level;
109 d->old_length = pstate->length;
110 pstate->fancyp = 1;
111 pstate->level = DISPLAY_ERROR_MESSAGE_MAX_LEVEL;
112 pstate->length = DISPLAY_ERROR_MESSAGE_MAX_LENGTH;
113 }
114
115 static void
116 after_display_error_message (struct display_error_message_data *d)
117 {
118 scm_print_state *pstate = d->pstate;
119 pstate->fancyp = d->old_fancyp;
120 pstate->level = d->old_level;
121 pstate->length = d->old_length;
122 }
123
124 void
125 scm_display_error_message (SCM message, SCM args, SCM port)
126 {
127 struct display_error_message_data d;
128 SCM print_state;
129 scm_print_state *pstate;
130
131 port = scm_i_port_with_print_state (port, SCM_UNDEFINED);
132 print_state = SCM_PORT_WITH_PS_PS (port);
133 pstate = SCM_PRINT_STATE (print_state);
134
135 d.message = message;
136 d.args = args;
137 d.port = port;
138 d.pstate = pstate;
139 scm_internal_dynamic_wind ((scm_t_guard) before_display_error_message,
140 (scm_t_inner) display_error_message,
141 (scm_t_guard) after_display_error_message,
142 &d,
143 &d);
144 }
145
146 static void
147 display_expression (SCM frame, SCM pname, SCM source, SCM port)
148 {
149 SCM print_state = scm_make_print_state ();
150 scm_print_state *pstate = SCM_PRINT_STATE (print_state);
151 pstate->writingp = 0;
152 pstate->fancyp = 1;
153 pstate->level = DISPLAY_EXPRESSION_MAX_LEVEL;
154 pstate->length = DISPLAY_EXPRESSION_MAX_LENGTH;
155 if (scm_is_symbol (pname) || scm_is_string (pname))
156 {
157 scm_puts ("In procedure ", port);
158 scm_iprin1 (pname, port, pstate);
159 }
160 scm_puts (":\n", port);
161 scm_free_print_state (print_state);
162 }
163
164 struct display_error_args {
165 SCM stack;
166 SCM port;
167 SCM subr;
168 SCM message;
169 SCM args;
170 SCM rest;
171 };
172
173 static SCM
174 display_error_body (struct display_error_args *a)
175 {
176 SCM current_frame = SCM_BOOL_F;
177 SCM source = SCM_BOOL_F;
178 SCM pname = a->subr;
179
180 if (scm_is_symbol (pname) || scm_is_string (pname))
181 {
182 display_header (source, a->port);
183 display_expression (current_frame, pname, source, a->port);
184 }
185 display_header (source, a->port);
186 scm_display_error_message (a->message, a->args, a->port);
187 return SCM_UNSPECIFIED;
188 }
189
190 struct display_error_handler_data {
191 char *mode;
192 SCM port;
193 };
194
195 /* This is the exception handler for error reporting routines.
196 Note that it is very important that this handler *doesn't* try to
197 print more than the error tag, since the error very probably is
198 caused by an erroneous print call-back routine. If we would
199 try to print all objects, we would enter an infinite loop. */
200 static SCM
201 display_error_handler (struct display_error_handler_data *data,
202 SCM tag, SCM args SCM_UNUSED)
203 {
204 SCM print_state = scm_make_print_state ();
205 scm_puts ("\nException during displaying of ", data->port);
206 scm_puts (data->mode, data->port);
207 scm_puts (": ", data->port);
208 scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state));
209 scm_putc ('\n', data->port);
210 return SCM_UNSPECIFIED;
211 }
212
213
214 /* The function scm_i_display_error prints out a detailed error message. This
215 * function will be called directly within libguile to signal error messages.
216 * No parameter checks will be performed by scm_i_display_error. Thus, User
217 * code should rather use the function scm_display_error.
218 */
219 void
220 scm_i_display_error (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest)
221 {
222 struct display_error_args a;
223 struct display_error_handler_data data;
224 a.stack = stack;
225 a.port = port;
226 a.subr = subr;
227 a.message = message;
228 a.args = args;
229 a.rest = rest;
230 data.mode = "error";
231 data.port = port;
232 scm_internal_catch (SCM_BOOL_T,
233 (scm_t_catch_body) display_error_body, &a,
234 (scm_t_catch_handler) display_error_handler, &data);
235 }
236
237
238 SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
239 (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest),
240 "Display an error message to the output port @var{port}.\n"
241 "@var{stack} is the saved stack for the error, @var{subr} is\n"
242 "the name of the procedure in which the error occurred and\n"
243 "@var{message} is the actual error message, which may contain\n"
244 "formatting instructions. These will format the arguments in\n"
245 "the list @var{args} accordingly. @var{rest} is currently\n"
246 "ignored.")
247 #define FUNC_NAME s_scm_display_error
248 {
249 SCM_VALIDATE_OUTPUT_PORT (2, port);
250
251 scm_i_display_error (stack, port, subr, message, args, rest);
252
253 return SCM_UNSPECIFIED;
254 }
255 #undef FUNC_NAME
256
257
258 typedef struct {
259 int level;
260 int length;
261 } print_params_t;
262
263 static int n_print_params = 9;
264 static print_params_t default_print_params[] = {
265 { 4, 9 }, { 4, 3 },
266 { 3, 4 }, { 3, 3 },
267 { 2, 4 }, { 2, 3 },
268 { 1, 4 }, { 1, 3 }, { 1, 2 }
269 };
270 static print_params_t *print_params = default_print_params;
271
272 #ifdef GUILE_DEBUG
273 SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
274 (SCM params),
275 "Set the print parameters to the values from @var{params}.\n"
276 "@var{params} must be a list of two-element lists which must\n"
277 "hold two integer values.")
278 #define FUNC_NAME s_scm_set_print_params_x
279 {
280 int i;
281 int n;
282 SCM ls;
283 print_params_t *new_params;
284
285 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
286 for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
287 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
288 && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX)
289 && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX),
290 params,
291 SCM_ARG2,
292 s_scm_set_print_params_x);
293 new_params = scm_malloc (n * sizeof (print_params_t));
294 if (print_params != default_print_params)
295 free (print_params);
296 print_params = new_params;
297 for (i = 0; i < n; ++i)
298 {
299 print_params[i].level = scm_to_int (SCM_CAAR (params));
300 print_params[i].length = scm_to_int (SCM_CADAR (params));
301 params = SCM_CDR (params);
302 }
303 n_print_params = n;
304 return SCM_UNSPECIFIED;
305 }
306 #undef FUNC_NAME
307 #endif
308
309 static void
310 indent (int n, SCM port)
311 {
312 int i;
313 for (i = 0; i < n; ++i)
314 scm_putc (' ', port);
315 }
316
317 static void
318 display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
319 {
320 int i = 0, n;
321 scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport);
322 do
323 {
324 pstate->length = print_params[i].length;
325 ptob->seek (sport, 0, SEEK_SET);
326 if (scm_is_pair (exp))
327 {
328 pstate->level = print_params[i].level - 1;
329 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
330 scm_puts (&tlr[1], sport);
331 }
332 else
333 {
334 pstate->level = print_params[i].level;
335 scm_iprin1 (exp, sport, pstate);
336 }
337 ptob->flush (sport);
338 n = ptob->seek (sport, 0, SEEK_CUR);
339 ++i;
340 }
341 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
342 ptob->truncate (sport, n);
343
344 scm_display (scm_strport_to_string (sport), port);
345 }
346
347 static void
348 display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
349 {
350 SCM proc = scm_frame_procedure (frame);
351 SCM name = (scm_is_true (scm_procedure_p (proc))
352 ? scm_procedure_name (proc)
353 : SCM_BOOL_F);
354 display_frame_expr ("[",
355 scm_cons (scm_is_true (name) ? name : proc,
356 scm_frame_arguments (frame)),
357 "]",
358 indentation,
359 sport,
360 port,
361 pstate);
362 }
363
364 SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
365 (SCM frame, SCM port, SCM indent),
366 "Display a procedure application @var{frame} to the output port\n"
367 "@var{port}. @var{indent} specifies the indentation of the\n"
368 "output.")
369 #define FUNC_NAME s_scm_display_application
370 {
371 SCM_VALIDATE_FRAME (1, frame);
372 if (SCM_UNBNDP (port))
373 port = scm_current_output_port ();
374 else
375 SCM_VALIDATE_OPOUTPORT (2, port);
376 if (SCM_UNBNDP (indent))
377 indent = SCM_INUM0;
378
379 /* Display an application. */
380 {
381 SCM sport, print_state;
382 scm_print_state *pstate;
383
384 /* Create a string port used for adaptation of printing parameters. */
385 sport = scm_mkstrport (SCM_INUM0,
386 scm_make_string (scm_from_int (240),
387 SCM_UNDEFINED),
388 SCM_OPN | SCM_WRTNG,
389 FUNC_NAME);
390
391 /* Create a print state for printing of frames. */
392 print_state = scm_make_print_state ();
393 pstate = SCM_PRINT_STATE (print_state);
394 pstate->writingp = 1;
395 pstate->fancyp = 1;
396
397 display_application (frame, scm_to_int (indent), sport, port, pstate);
398 return SCM_BOOL_T;
399 }
400 }
401 #undef FUNC_NAME
402
403 SCM_SYMBOL (sym_base, "base");
404
405 static void
406 display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
407 {
408 SCM source = scm_frame_source (frame);
409 *file = *line = SCM_BOOL_F;
410 if (scm_is_pair (source)
411 && scm_is_pair (scm_cdr (source))
412 && scm_is_pair (scm_cddr (source))
413 && !scm_is_pair (scm_cdddr (source)))
414 {
415 /* (addr . (filename . (line . column))), from vm compilation */
416 *file = scm_cadr (source);
417 *line = scm_caddr (source);
418 }
419 }
420
421 static void
422 display_backtrace_file (frame, last_file, port, pstate)
423 SCM frame;
424 SCM *last_file;
425 SCM port;
426 scm_print_state *pstate;
427 {
428 SCM file, line;
429
430 display_backtrace_get_file_line (frame, &file, &line);
431
432 if (scm_is_true (scm_equal_p (file, *last_file)))
433 return;
434
435 *last_file = file;
436
437 scm_puts ("In ", port);
438 if (scm_is_false (file))
439 if (scm_is_false (line))
440 scm_puts ("unknown file", port);
441 else
442 scm_puts ("current input", port);
443 else
444 {
445 pstate->writingp = 0;
446 scm_iprin1 (file, port, pstate);
447 pstate->writingp = 1;
448 }
449 scm_puts (":\n", port);
450 }
451
452 static void
453 display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
454 {
455 SCM file, line;
456
457 display_backtrace_get_file_line (frame, &file, &line);
458
459 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
460 {
461 if (scm_is_false (file))
462 {
463 if (scm_is_false (line))
464 scm_putc ('?', port);
465 else
466 scm_puts ("<stdin>", port);
467 }
468 else
469 {
470 pstate -> writingp = 0;
471 #ifdef HAVE_POSIX
472 scm_iprin1 ((scm_is_string (file)?
473 scm_basename (file, SCM_UNDEFINED) : file),
474 port, pstate);
475 #else
476 scm_iprin1 (file, port, pstate);
477 #endif
478 pstate -> writingp = 1;
479 }
480
481 scm_putc (':', port);
482 }
483 else if (scm_is_true (line))
484 {
485 int i, j=0;
486 for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
487 ;
488 indent (4-j, port);
489 }
490
491 if (scm_is_false (line))
492 scm_puts (" ?", port);
493 else
494 scm_intprint (scm_to_int (line) + 1, 10, port);
495 scm_puts (": ", port);
496 }
497
498 static void
499 display_frame (SCM frame, int n, int nfield, int indentation,
500 SCM sport, SCM port, scm_print_state *pstate)
501 {
502 int i, j;
503
504 /* display file name and line number */
505 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
506 display_backtrace_file_and_line (frame, port, pstate);
507
508 /* Check size of frame number. */
509 for (i = 0, j = n; j > 0; ++i) j /= 10;
510
511 /* Number indentation. */
512 indent (nfield - (i ? i : 1), port);
513
514 /* Frame number. */
515 scm_iprin1 (scm_from_int (n), port, pstate);
516
517 /* Indentation. */
518 indent (indentation, port);
519
520 /* Display an application. */
521 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
522 scm_putc ('\n', port);
523 }
524
525 struct display_backtrace_args {
526 SCM stack;
527 SCM port;
528 SCM first;
529 SCM depth;
530 SCM highlight_objects;
531 };
532
533 static SCM
534 display_backtrace_body (struct display_backtrace_args *a)
535 #define FUNC_NAME "display_backtrace_body"
536 {
537 int n_frames, beg, end, n, i, j;
538 int nfield, indent_p, indentation;
539 SCM frame, sport, print_state;
540 SCM last_file;
541 scm_print_state *pstate;
542
543 a->port = SCM_COERCE_OUTPORT (a->port);
544
545 /* Argument checking and extraction. */
546 SCM_VALIDATE_STACK (1, a->stack);
547 SCM_VALIDATE_OPOUTPORT (2, a->port);
548 n_frames = scm_to_int (scm_stack_length (a->stack));
549 n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
550 if (SCM_BACKWARDS_P)
551 {
552 beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
553 end = beg + n - 1;
554 if (end >= n_frames)
555 end = n_frames - 1;
556 n = end - beg + 1;
557 }
558 else
559 {
560 if (scm_is_integer (a->first))
561 {
562 beg = scm_to_int (a->first);
563 end = beg - n + 1;
564 if (end < 0)
565 end = 0;
566 }
567 else
568 {
569 beg = n - 1;
570 end = 0;
571 if (beg >= n_frames)
572 beg = n_frames - 1;
573 }
574 n = beg - end + 1;
575 }
576 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
577 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
578
579 /* Create a string port used for adaptation of printing parameters. */
580 sport = scm_mkstrport (SCM_INUM0,
581 scm_make_string (scm_from_int (240), SCM_UNDEFINED),
582 SCM_OPN | SCM_WRTNG,
583 FUNC_NAME);
584
585 /* Create a print state for printing of frames. */
586 print_state = scm_make_print_state ();
587 pstate = SCM_PRINT_STATE (print_state);
588 pstate->writingp = 1;
589 pstate->fancyp = 1;
590 pstate->highlight_objects = a->highlight_objects;
591
592 /* First find out if it's reasonable to do indentation. */
593 indent_p = 0;
594
595 /* Determine size of frame number field. */
596 j = end;
597 for (i = 0; j > 0; ++i) j /= 10;
598 nfield = i ? i : 1;
599
600 /* Print frames. */
601 indentation = 1;
602 last_file = SCM_UNDEFINED;
603 if (SCM_BACKWARDS_P)
604 end++;
605 else
606 end--;
607 for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i)
608 {
609 frame = scm_stack_ref (a->stack, scm_from_int (i));
610 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
611 display_backtrace_file (frame, &last_file, a->port, pstate);
612 display_frame (frame, i, nfield, indentation, sport, a->port, pstate);
613 }
614
615 scm_remember_upto_here_1 (print_state);
616
617 return SCM_UNSPECIFIED;
618 }
619 #undef FUNC_NAME
620
621 SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
622 (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
623 "Display a backtrace to the output port @var{port}. @var{stack}\n"
624 "is the stack to take the backtrace from, @var{first} specifies\n"
625 "where in the stack to start and @var{depth} how many frames\n"
626 "to display. @var{first} and @var{depth} can be @code{#f},\n"
627 "which means that default values will be used.\n"
628 "If @var{highlights} is given it should be a list; the elements\n"
629 "of this list will be highlighted wherever they appear in the\n"
630 "backtrace.")
631 #define FUNC_NAME s_scm_display_backtrace_with_highlights
632 {
633 struct display_backtrace_args a;
634 struct display_error_handler_data data;
635 a.stack = stack;
636 a.port = port;
637 a.first = first;
638 a.depth = depth;
639 if (SCM_UNBNDP (highlights))
640 a.highlight_objects = SCM_EOL;
641 else
642 a.highlight_objects = highlights;
643 data.mode = "backtrace";
644 data.port = port;
645 scm_internal_catch (SCM_BOOL_T,
646 (scm_t_catch_body) display_backtrace_body, &a,
647 (scm_t_catch_handler) display_error_handler, &data);
648 return SCM_UNSPECIFIED;
649 }
650 #undef FUNC_NAME
651
652 SCM
653 scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
654 {
655 return scm_display_backtrace_with_highlights (stack, port, first, depth,
656 SCM_EOL);
657 }
658
659 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
660
661 SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
662 (SCM highlights),
663 "Display a backtrace of the current stack to the current\n"
664 "output port. If @var{highlights} is given, it should be\n"
665 "a list; the elements of this list will be highlighted\n"
666 "wherever they appear in the backtrace.")
667 #define FUNC_NAME s_scm_backtrace_with_highlights
668 {
669 SCM port = scm_current_output_port ();
670 SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
671
672 if (SCM_UNBNDP (highlights))
673 highlights = SCM_EOL;
674
675 scm_newline (port);
676 scm_puts ("Backtrace:\n", port);
677 scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
678 highlights);
679 scm_newline (port);
680
681 return SCM_UNSPECIFIED;
682 }
683 #undef FUNC_NAME
684
685 SCM
686 scm_backtrace (void)
687 {
688 return scm_backtrace_with_highlights (SCM_EOL);
689 }
690
691 \f
692
693 void
694 scm_init_backtrace ()
695 {
696 #include "libguile/backtrace.x"
697 }
698
699 /*
700 Local Variables:
701 c-file-style: "gnu"
702 End:
703 */