Merge commit '9b5da400dde6e6bc8fd0e318e7ca1feffa5870db'
[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, 2011 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/deprecation.h"
37 #include "libguile/stacks.h"
38 #include "libguile/srcprop.h"
39 #include "libguile/struct.h"
40 #include "libguile/strports.h"
41 #include "libguile/throw.h"
42 #include "libguile/fluids.h"
43 #include "libguile/ports.h"
44 #include "libguile/strings.h"
45 #include "libguile/dynwind.h"
46 #include "libguile/frames.h"
47
48 #include "libguile/validate.h"
49 #include "libguile/backtrace.h"
50 #include "libguile/filesys.h"
51 #include "libguile/private-options.h"
52
53 /* {Error reporting and backtraces}
54 *
55 * Note that these functions shouldn't generate errors themselves.
56 */
57
58 static SCM
59 boot_print_exception (SCM port, SCM frame, SCM key, SCM args)
60 #define FUNC_NAME "boot-print-exception"
61 {
62 scm_puts_unlocked ("Throw to key ", port);
63 scm_write (key, port);
64 scm_puts_unlocked (" with args ", port);
65 scm_write (args, port);
66 return SCM_UNSPECIFIED;
67 }
68 #undef FUNC_NAME
69
70 static SCM print_exception_var;
71
72 static void
73 init_print_exception_var (void)
74 {
75 print_exception_var
76 = scm_module_variable (scm_the_root_module (),
77 scm_from_latin1_symbol ("print-exception"));
78 }
79
80 SCM
81 scm_print_exception (SCM port, SCM frame, SCM key, SCM args)
82 #define FUNC_NAME "print-exception"
83 {
84 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
85 scm_i_pthread_once (&once, init_print_exception_var);
86
87 SCM_VALIDATE_OPOUTPORT (1, port);
88 if (scm_is_true (frame))
89 SCM_VALIDATE_FRAME (2, frame);
90 SCM_VALIDATE_SYMBOL (3, key);
91 SCM_VALIDATE_LIST (4, args);
92
93 return scm_call_4 (scm_variable_ref (print_exception_var),
94 port, frame, key, args);
95 }
96 #undef FUNC_NAME
97
98
99 \f
100
101 /* Print parameters for error messages. */
102
103 #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7
104 #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10
105
106 /* Print parameters for failing expressions in error messages.
107 * (See also `print_params' below for backtrace print parameters.)
108 */
109
110 #define DISPLAY_EXPRESSION_MAX_LEVEL 2
111 #define DISPLAY_EXPRESSION_MAX_LENGTH 3
112
113 #undef SCM_ASSERT
114 #define SCM_ASSERT(_cond, _arg, _pos, _subr) \
115 if (!(_cond)) \
116 return SCM_BOOL_F;
117
118
119 void
120 scm_display_error_message (SCM message, SCM args, SCM port)
121 {
122 scm_print_exception (port, SCM_BOOL_F, scm_misc_error_key,
123 scm_list_3 (SCM_BOOL_F, message, args));
124 }
125
126
127 /* The function scm_i_display_error prints out a detailed error message. This
128 * function will be called directly within libguile to signal error messages.
129 * No parameter checks will be performed by scm_i_display_error. Thus, User
130 * code should rather use the function scm_display_error.
131 */
132 void
133 scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest)
134 {
135 scm_print_exception (port, frame, scm_misc_error_key,
136 scm_list_3 (subr, message, args));
137 }
138
139
140 SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0,
141 (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest),
142 "Display an error message to the output port @var{port}.\n"
143 "@var{frame} is the frame in which the error occurred, @var{subr} is\n"
144 "the name of the procedure in which the error occurred and\n"
145 "@var{message} is the actual error message, which may contain\n"
146 "formatting instructions. These will format the arguments in\n"
147 "the list @var{args} accordingly. @var{rest} is currently\n"
148 "ignored.")
149 #define FUNC_NAME s_scm_display_error
150 {
151 SCM_VALIDATE_OUTPUT_PORT (2, port);
152
153 #if SCM_ENABLE_DEPRECATED
154 if (SCM_STACKP (frame))
155 {
156 scm_c_issue_deprecation_warning
157 ("Passing a stack as the first argument to `scm_display_error' is "
158 "deprecated. Pass a frame instead.");
159 if (SCM_STACK_LENGTH (frame))
160 frame = scm_stack_ref (frame, SCM_INUM0);
161 else
162 frame = SCM_BOOL_F;
163 }
164 #endif
165
166 scm_i_display_error (frame, port, subr, message, args, rest);
167
168 return SCM_UNSPECIFIED;
169 }
170 #undef FUNC_NAME
171
172
173 typedef struct {
174 int level;
175 int length;
176 } print_params_t;
177
178 static int n_print_params = 9;
179 static print_params_t default_print_params[] = {
180 { 4, 9 }, { 4, 3 },
181 { 3, 4 }, { 3, 3 },
182 { 2, 4 }, { 2, 3 },
183 { 1, 4 }, { 1, 3 }, { 1, 2 }
184 };
185 static print_params_t *print_params = default_print_params;
186
187 #ifdef GUILE_DEBUG
188 SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0,
189 (SCM params),
190 "Set the print parameters to the values from @var{params}.\n"
191 "@var{params} must be a list of two-element lists which must\n"
192 "hold two integer values.")
193 #define FUNC_NAME s_scm_set_print_params_x
194 {
195 int i;
196 int n;
197 SCM ls;
198 print_params_t *new_params;
199
200 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n);
201 for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls))
202 SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2
203 && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX)
204 && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX),
205 params,
206 SCM_ARG2,
207 s_scm_set_print_params_x);
208 new_params = scm_malloc (n * sizeof (print_params_t));
209 if (print_params != default_print_params)
210 free (print_params);
211 print_params = new_params;
212 for (i = 0; i < n; ++i)
213 {
214 print_params[i].level = scm_to_int (SCM_CAAR (params));
215 print_params[i].length = scm_to_int (SCM_CADAR (params));
216 params = SCM_CDR (params);
217 }
218 n_print_params = n;
219 return SCM_UNSPECIFIED;
220 }
221 #undef FUNC_NAME
222 #endif
223
224 static void
225 indent (int n, SCM port)
226 {
227 int i;
228 for (i = 0; i < n; ++i)
229 scm_putc_unlocked (' ', port);
230 }
231
232 static void
233 display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate)
234 {
235 int i = 0, n;
236 scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (sport);
237 do
238 {
239 pstate->length = print_params[i].length;
240 ptob->seek (sport, 0, SEEK_SET);
241 if (scm_is_pair (exp))
242 {
243 pstate->level = print_params[i].level - 1;
244 scm_iprlist (hdr, exp, tlr[0], sport, pstate);
245 scm_puts_unlocked (&tlr[1], sport);
246 }
247 else
248 {
249 pstate->level = print_params[i].level;
250 scm_iprin1 (exp, sport, pstate);
251 }
252 ptob->flush (sport);
253 n = ptob->seek (sport, 0, SEEK_CUR);
254 ++i;
255 }
256 while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params);
257 ptob->truncate (sport, n);
258
259 scm_display (scm_strport_to_string (sport), port);
260 }
261
262 static void
263 display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate)
264 {
265 SCM proc = scm_frame_procedure (frame);
266 SCM name = (scm_is_true (scm_procedure_p (proc))
267 ? scm_procedure_name (proc)
268 : SCM_BOOL_F);
269 display_frame_expr ("[",
270 scm_cons (scm_is_true (name) ? name : proc,
271 scm_frame_arguments (frame)),
272 "]",
273 indentation,
274 sport,
275 port,
276 pstate);
277 }
278
279 SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
280 (SCM frame, SCM port, SCM indent),
281 "Display a procedure application @var{frame} to the output port\n"
282 "@var{port}. @var{indent} specifies the indentation of the\n"
283 "output.")
284 #define FUNC_NAME s_scm_display_application
285 {
286 SCM_VALIDATE_FRAME (1, frame);
287 if (SCM_UNBNDP (port))
288 port = scm_current_output_port ();
289 else
290 SCM_VALIDATE_OPOUTPORT (2, port);
291 if (SCM_UNBNDP (indent))
292 indent = SCM_INUM0;
293
294 /* Display an application. */
295 {
296 SCM sport, print_state;
297 scm_print_state *pstate;
298
299 /* Create a string port used for adaptation of printing parameters. */
300 sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
301 SCM_OPN | SCM_WRTNG,
302 FUNC_NAME);
303
304 /* Create a print state for printing of frames. */
305 print_state = scm_make_print_state ();
306 pstate = SCM_PRINT_STATE (print_state);
307 pstate->writingp = 1;
308 pstate->fancyp = 1;
309
310 display_application (frame, scm_to_int (indent), sport, port, pstate);
311 return SCM_BOOL_T;
312 }
313 }
314 #undef FUNC_NAME
315
316 SCM_SYMBOL (sym_base, "base");
317
318 static void
319 display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
320 {
321 SCM source = scm_frame_source (frame);
322 *file = *line = SCM_BOOL_F;
323 if (scm_is_pair (source)
324 && scm_is_pair (scm_cdr (source))
325 && scm_is_pair (scm_cddr (source))
326 && !scm_is_pair (scm_cdddr (source)))
327 {
328 /* (addr . (filename . (line . column))), from vm compilation */
329 *file = scm_cadr (source);
330 *line = scm_caddr (source);
331 }
332 }
333
334 static void
335 display_backtrace_file (frame, last_file, port, pstate)
336 SCM frame;
337 SCM *last_file;
338 SCM port;
339 scm_print_state *pstate;
340 {
341 SCM file, line;
342
343 display_backtrace_get_file_line (frame, &file, &line);
344
345 if (scm_is_true (scm_equal_p (file, *last_file)))
346 return;
347
348 *last_file = file;
349
350 scm_puts_unlocked ("In ", port);
351 if (scm_is_false (file))
352 if (scm_is_false (line))
353 scm_puts_unlocked ("unknown file", port);
354 else
355 scm_puts_unlocked ("current input", port);
356 else
357 {
358 pstate->writingp = 0;
359 scm_iprin1 (file, port, pstate);
360 pstate->writingp = 1;
361 }
362 scm_puts_unlocked (":\n", port);
363 }
364
365 static void
366 display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
367 {
368 SCM file, line;
369
370 display_backtrace_get_file_line (frame, &file, &line);
371
372 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
373 {
374 if (scm_is_false (file))
375 {
376 if (scm_is_false (line))
377 scm_putc_unlocked ('?', port);
378 else
379 scm_puts_unlocked ("<stdin>", port);
380 }
381 else
382 {
383 pstate -> writingp = 0;
384 #ifdef HAVE_POSIX
385 scm_iprin1 ((scm_is_string (file)?
386 scm_basename (file, SCM_UNDEFINED) : file),
387 port, pstate);
388 #else
389 scm_iprin1 (file, port, pstate);
390 #endif
391 pstate -> writingp = 1;
392 }
393
394 scm_putc_unlocked (':', port);
395 }
396 else if (scm_is_true (line))
397 {
398 int i, j=0;
399 for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
400 ;
401 indent (4-j, port);
402 }
403
404 if (scm_is_false (line))
405 scm_puts_unlocked (" ?", port);
406 else
407 scm_intprint (scm_to_int (line) + 1, 10, port);
408 scm_puts_unlocked (": ", port);
409 }
410
411 static void
412 display_frame (SCM frame, int n, int nfield, int indentation,
413 SCM sport, SCM port, scm_print_state *pstate)
414 {
415 int i, j;
416
417 /* display file name and line number */
418 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
419 display_backtrace_file_and_line (frame, port, pstate);
420
421 /* Check size of frame number. */
422 for (i = 0, j = n; j > 0; ++i) j /= 10;
423
424 /* Number indentation. */
425 indent (nfield - (i ? i : 1), port);
426
427 /* Frame number. */
428 scm_iprin1 (scm_from_int (n), port, pstate);
429
430 /* Indentation. */
431 indent (indentation, port);
432
433 /* Display an application. */
434 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
435 scm_putc_unlocked ('\n', port);
436 }
437
438 struct display_backtrace_args {
439 SCM stack;
440 SCM port;
441 SCM first;
442 SCM depth;
443 SCM highlight_objects;
444 };
445
446 static SCM
447 display_backtrace_body (struct display_backtrace_args *a)
448 #define FUNC_NAME "display_backtrace_body"
449 {
450 int n_frames, beg, end, n, i, j;
451 int nfield, indentation;
452 SCM frame, sport, print_state;
453 SCM last_file;
454 scm_print_state *pstate;
455
456 a->port = SCM_COERCE_OUTPORT (a->port);
457
458 /* Argument checking and extraction. */
459 SCM_VALIDATE_STACK (1, a->stack);
460 SCM_VALIDATE_OPOUTPORT (2, a->port);
461 n_frames = scm_to_int (scm_stack_length (a->stack));
462 n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
463 if (SCM_BACKWARDS_P)
464 {
465 beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
466 end = beg + n - 1;
467 if (end >= n_frames)
468 end = n_frames - 1;
469 n = end - beg + 1;
470 }
471 else
472 {
473 if (scm_is_integer (a->first))
474 {
475 beg = scm_to_int (a->first);
476 end = beg - n + 1;
477 if (end < 0)
478 end = 0;
479 }
480 else
481 {
482 beg = n - 1;
483 end = 0;
484 if (beg >= n_frames)
485 beg = n_frames - 1;
486 }
487 n = beg - end + 1;
488 }
489 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
490 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
491
492 /* Create a string port used for adaptation of printing parameters. */
493 sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
494 SCM_OPN | SCM_WRTNG,
495 FUNC_NAME);
496
497 /* Create a print state for printing of frames. */
498 print_state = scm_make_print_state ();
499 pstate = SCM_PRINT_STATE (print_state);
500 pstate->writingp = 1;
501 pstate->fancyp = 1;
502 pstate->highlight_objects = a->highlight_objects;
503
504 /* Determine size of frame number field. */
505 j = end;
506 for (i = 0; j > 0; ++i) j /= 10;
507 nfield = i ? i : 1;
508
509 /* Print frames. */
510 indentation = 1;
511 last_file = SCM_UNDEFINED;
512 if (SCM_BACKWARDS_P)
513 end++;
514 else
515 end--;
516 for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i)
517 {
518 frame = scm_stack_ref (a->stack, scm_from_int (i));
519 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
520 display_backtrace_file (frame, &last_file, a->port, pstate);
521 display_frame (frame, i, nfield, indentation, sport, a->port, pstate);
522 }
523
524 scm_remember_upto_here_1 (print_state);
525
526 return SCM_UNSPECIFIED;
527 }
528 #undef FUNC_NAME
529
530 static SCM
531 error_during_backtrace (void *data, SCM tag, SCM throw_args)
532 {
533 SCM port = SCM_PACK_POINTER (data);
534
535 scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port);
536 scm_print_exception (port, SCM_BOOL_F, tag, throw_args);
537
538 return SCM_UNSPECIFIED;
539 }
540
541
542 SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
543 (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
544 "Display a backtrace to the output port @var{port}. @var{stack}\n"
545 "is the stack to take the backtrace from, @var{first} specifies\n"
546 "where in the stack to start and @var{depth} how many frames\n"
547 "to display. @var{first} and @var{depth} can be @code{#f},\n"
548 "which means that default values will be used.\n"
549 "If @var{highlights} is given it should be a list; the elements\n"
550 "of this list will be highlighted wherever they appear in the\n"
551 "backtrace.")
552 #define FUNC_NAME s_scm_display_backtrace_with_highlights
553 {
554 struct display_backtrace_args a;
555 a.stack = stack;
556 a.port = port;
557 a.first = first;
558 a.depth = depth;
559 if (SCM_UNBNDP (highlights))
560 a.highlight_objects = SCM_EOL;
561 else
562 a.highlight_objects = highlights;
563
564 scm_internal_catch (SCM_BOOL_T,
565 (scm_t_catch_body) display_backtrace_body, &a,
566 (scm_t_catch_handler) error_during_backtrace, SCM_UNPACK_POINTER (port));
567
568 return SCM_UNSPECIFIED;
569 }
570 #undef FUNC_NAME
571
572 SCM
573 scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
574 {
575 return scm_display_backtrace_with_highlights (stack, port, first, depth,
576 SCM_EOL);
577 }
578
579 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
580
581 SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
582 (SCM highlights),
583 "Display a backtrace of the current stack to the current\n"
584 "output port. If @var{highlights} is given, it should be\n"
585 "a list; the elements of this list will be highlighted\n"
586 "wherever they appear in the backtrace.")
587 #define FUNC_NAME s_scm_backtrace_with_highlights
588 {
589 SCM port = scm_current_output_port ();
590 SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
591
592 if (SCM_UNBNDP (highlights))
593 highlights = SCM_EOL;
594
595 scm_newline (port);
596 scm_puts_unlocked ("Backtrace:\n", port);
597 scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
598 highlights);
599 scm_newline (port);
600
601 return SCM_UNSPECIFIED;
602 }
603 #undef FUNC_NAME
604
605 SCM
606 scm_backtrace (void)
607 {
608 return scm_backtrace_with_highlights (SCM_EOL);
609 }
610
611 \f
612
613 void
614 scm_init_backtrace ()
615 {
616 scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception);
617 #include "libguile/backtrace.x"
618 }
619
620 /*
621 Local Variables:
622 c-file-style: "gnu"
623 End:
624 */