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