Better backtraces from C, especially for optimized closures
[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, 2014 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 display_frame_expr ("[", scm_frame_call_representation (frame), "]",
266 indentation,
267 sport,
268 port,
269 pstate);
270 }
271
272 SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0,
273 (SCM frame, SCM port, SCM indent),
274 "Display a procedure application @var{frame} to the output port\n"
275 "@var{port}. @var{indent} specifies the indentation of the\n"
276 "output.")
277 #define FUNC_NAME s_scm_display_application
278 {
279 SCM_VALIDATE_FRAME (1, frame);
280 if (SCM_UNBNDP (port))
281 port = scm_current_output_port ();
282 else
283 SCM_VALIDATE_OPOUTPORT (2, port);
284 if (SCM_UNBNDP (indent))
285 indent = SCM_INUM0;
286
287 /* Display an application. */
288 {
289 SCM sport, print_state;
290 scm_print_state *pstate;
291
292 /* Create a string port used for adaptation of printing parameters. */
293 sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
294 SCM_OPN | SCM_WRTNG,
295 FUNC_NAME);
296
297 /* Create a print state for printing of frames. */
298 print_state = scm_make_print_state ();
299 pstate = SCM_PRINT_STATE (print_state);
300 pstate->writingp = 1;
301 pstate->fancyp = 1;
302
303 display_application (frame, scm_to_int (indent), sport, port, pstate);
304 return SCM_BOOL_T;
305 }
306 }
307 #undef FUNC_NAME
308
309 SCM_SYMBOL (sym_base, "base");
310
311 static void
312 display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line)
313 {
314 SCM source = scm_frame_source (frame);
315 *file = *line = SCM_BOOL_F;
316 if (scm_is_pair (source)
317 && scm_is_pair (scm_cdr (source))
318 && scm_is_pair (scm_cddr (source))
319 && !scm_is_pair (scm_cdddr (source)))
320 {
321 /* (addr . (filename . (line . column))), from vm compilation */
322 *file = scm_cadr (source);
323 *line = scm_caddr (source);
324 }
325 }
326
327 static void
328 display_backtrace_file (frame, last_file, port, pstate)
329 SCM frame;
330 SCM *last_file;
331 SCM port;
332 scm_print_state *pstate;
333 {
334 SCM file, line;
335
336 display_backtrace_get_file_line (frame, &file, &line);
337
338 if (scm_is_true (scm_equal_p (file, *last_file)))
339 return;
340
341 *last_file = file;
342
343 scm_puts_unlocked ("In ", port);
344 if (scm_is_false (file))
345 if (scm_is_false (line))
346 scm_puts_unlocked ("unknown file", port);
347 else
348 scm_puts_unlocked ("current input", port);
349 else
350 {
351 pstate->writingp = 0;
352 scm_iprin1 (file, port, pstate);
353 pstate->writingp = 1;
354 }
355 scm_puts_unlocked (":\n", port);
356 }
357
358 static void
359 display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate)
360 {
361 SCM file, line;
362
363 display_backtrace_get_file_line (frame, &file, &line);
364
365 if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
366 {
367 if (scm_is_false (file))
368 {
369 if (scm_is_false (line))
370 scm_putc_unlocked ('?', port);
371 else
372 scm_puts_unlocked ("<stdin>", port);
373 }
374 else
375 {
376 pstate -> writingp = 0;
377 #ifdef HAVE_POSIX
378 scm_iprin1 ((scm_is_string (file)?
379 scm_basename (file, SCM_UNDEFINED) : file),
380 port, pstate);
381 #else
382 scm_iprin1 (file, port, pstate);
383 #endif
384 pstate -> writingp = 1;
385 }
386
387 scm_putc_unlocked (':', port);
388 }
389 else if (scm_is_true (line))
390 {
391 int i, j=0;
392 for (i = scm_to_int (line)+1; i > 0; i = i/10, j++)
393 ;
394 indent (4-j, port);
395 }
396
397 if (scm_is_false (line))
398 scm_puts_unlocked (" ?", port);
399 else
400 scm_intprint (scm_to_int (line) + 1, 10, port);
401 scm_puts_unlocked (": ", port);
402 }
403
404 static void
405 display_frame (SCM frame, int n, int nfield, int indentation,
406 SCM sport, SCM port, scm_print_state *pstate)
407 {
408 int i, j;
409
410 /* display file name and line number */
411 if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME)))
412 display_backtrace_file_and_line (frame, port, pstate);
413
414 /* Check size of frame number. */
415 for (i = 0, j = n; j > 0; ++i) j /= 10;
416
417 /* Number indentation. */
418 indent (nfield - (i ? i : 1), port);
419
420 /* Frame number. */
421 scm_iprin1 (scm_from_int (n), port, pstate);
422
423 /* Indentation. */
424 indent (indentation, port);
425
426 /* Display an application. */
427 display_application (frame, nfield + 1 + indentation, sport, port, pstate);
428 scm_putc_unlocked ('\n', port);
429 }
430
431 struct display_backtrace_args {
432 SCM stack;
433 SCM port;
434 SCM first;
435 SCM depth;
436 SCM highlight_objects;
437 };
438
439 static SCM
440 display_backtrace_body (struct display_backtrace_args *a)
441 #define FUNC_NAME "display_backtrace_body"
442 {
443 int n_frames, beg, end, n, i, j;
444 int nfield, indentation;
445 SCM frame, sport, print_state;
446 SCM last_file;
447 scm_print_state *pstate;
448
449 a->port = SCM_COERCE_OUTPORT (a->port);
450
451 /* Argument checking and extraction. */
452 SCM_VALIDATE_STACK (1, a->stack);
453 SCM_VALIDATE_OPOUTPORT (2, a->port);
454 n_frames = scm_to_int (scm_stack_length (a->stack));
455 n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH;
456 if (SCM_BACKWARDS_P)
457 {
458 beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0;
459 end = beg + n - 1;
460 if (end >= n_frames)
461 end = n_frames - 1;
462 n = end - beg + 1;
463 }
464 else
465 {
466 if (scm_is_integer (a->first))
467 {
468 beg = scm_to_int (a->first);
469 end = beg - n + 1;
470 if (end < 0)
471 end = 0;
472 }
473 else
474 {
475 beg = n - 1;
476 end = 0;
477 if (beg >= n_frames)
478 beg = n_frames - 1;
479 }
480 n = beg - end + 1;
481 }
482 SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace);
483 SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace);
484
485 /* Create a string port used for adaptation of printing parameters. */
486 sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
487 SCM_OPN | SCM_WRTNG,
488 FUNC_NAME);
489
490 /* Create a print state for printing of frames. */
491 print_state = scm_make_print_state ();
492 pstate = SCM_PRINT_STATE (print_state);
493 pstate->writingp = 1;
494 pstate->fancyp = 1;
495 pstate->highlight_objects = a->highlight_objects;
496
497 /* Determine size of frame number field. */
498 j = end;
499 for (i = 0; j > 0; ++i) j /= 10;
500 nfield = i ? i : 1;
501
502 /* Print frames. */
503 indentation = 1;
504 last_file = SCM_UNDEFINED;
505 if (SCM_BACKWARDS_P)
506 end++;
507 else
508 end--;
509 for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i)
510 {
511 frame = scm_stack_ref (a->stack, scm_from_int (i));
512 if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base))
513 display_backtrace_file (frame, &last_file, a->port, pstate);
514 display_frame (frame, i, nfield, indentation, sport, a->port, pstate);
515 }
516
517 scm_remember_upto_here_1 (print_state);
518
519 return SCM_UNSPECIFIED;
520 }
521 #undef FUNC_NAME
522
523 static SCM
524 error_during_backtrace (void *data, SCM tag, SCM throw_args)
525 {
526 SCM port = SCM_PACK_POINTER (data);
527
528 scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port);
529 scm_print_exception (port, SCM_BOOL_F, tag, throw_args);
530
531 return SCM_UNSPECIFIED;
532 }
533
534
535 SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0,
536 (SCM stack, SCM port, SCM first, SCM depth, SCM highlights),
537 "Display a backtrace to the output port @var{port}. @var{stack}\n"
538 "is the stack to take the backtrace from, @var{first} specifies\n"
539 "where in the stack to start and @var{depth} how many frames\n"
540 "to display. @var{first} and @var{depth} can be @code{#f},\n"
541 "which means that default values will be used.\n"
542 "If @var{highlights} is given it should be a list; the elements\n"
543 "of this list will be highlighted wherever they appear in the\n"
544 "backtrace.")
545 #define FUNC_NAME s_scm_display_backtrace_with_highlights
546 {
547 struct display_backtrace_args a;
548 a.stack = stack;
549 a.port = port;
550 a.first = first;
551 a.depth = depth;
552 if (SCM_UNBNDP (highlights))
553 a.highlight_objects = SCM_EOL;
554 else
555 a.highlight_objects = highlights;
556
557 scm_internal_catch (SCM_BOOL_T,
558 (scm_t_catch_body) display_backtrace_body, &a,
559 (scm_t_catch_handler) error_during_backtrace, SCM_UNPACK_POINTER (port));
560
561 return SCM_UNSPECIFIED;
562 }
563 #undef FUNC_NAME
564
565 SCM
566 scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth)
567 {
568 return scm_display_backtrace_with_highlights (stack, port, first, depth,
569 SCM_EOL);
570 }
571
572 SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?");
573
574 SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0,
575 (SCM highlights),
576 "Display a backtrace of the current stack to the current\n"
577 "output port. If @var{highlights} is given, it should be\n"
578 "a list; the elements of this list will be highlighted\n"
579 "wherever they appear in the backtrace.")
580 #define FUNC_NAME s_scm_backtrace_with_highlights
581 {
582 SCM port = scm_current_output_port ();
583 SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL);
584
585 if (SCM_UNBNDP (highlights))
586 highlights = SCM_EOL;
587
588 scm_newline (port);
589 scm_puts_unlocked ("Backtrace:\n", port);
590 scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F,
591 highlights);
592 scm_newline (port);
593
594 return SCM_UNSPECIFIED;
595 }
596 #undef FUNC_NAME
597
598 SCM
599 scm_backtrace (void)
600 {
601 return scm_backtrace_with_highlights (SCM_EOL);
602 }
603
604 \f
605
606 void
607 scm_init_backtrace ()
608 {
609 scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception);
610 #include "libguile/backtrace.x"
611 }
612
613 /*
614 Local Variables:
615 c-file-style: "gnu"
616 End:
617 */