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