Commit | Line | Data |
---|---|---|
ab4f3efb | 1 | /* Printing of backtraces and error messages |
bc8e6d7d MW |
2 | * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, |
3 | * 2010, 2011, 2014 Free Software Foundation | |
ab4f3efb | 4 | * |
73be1d9e | 5 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
ab4f3efb | 9 | * |
53befeb7 NJ |
10 | * This library is distributed in the hope that it will be useful, but |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
ab4f3efb | 14 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 | * 02110-1301 USA | |
73be1d9e | 19 | */ |
ab4f3efb | 20 | |
dbb605f5 | 21 | #ifdef HAVE_CONFIG_H |
615873d2 RB |
22 | # include <config.h> |
23 | #endif | |
1bbd0b84 | 24 | |
7beabedb | 25 | #include <stdio.h> |
2b407f9b MD |
26 | #include <ctype.h> |
27 | ||
a0599745 | 28 | #include "libguile/_scm.h" |
2fb36297 | 29 | |
b1508ade | 30 | #include <unistd.h> |
82893676 MG |
31 | #ifdef HAVE_IO_H |
32 | #include <io.h> | |
33 | #endif | |
b1508ade | 34 | |
218d580a | 35 | #include "libguile/deprecation.h" |
a0599745 MD |
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" | |
dfd03fb9 | 44 | #include "libguile/dynwind.h" |
aa3f6951 | 45 | #include "libguile/frames.h" |
ab4f3efb | 46 | |
a0599745 MD |
47 | #include "libguile/validate.h" |
48 | #include "libguile/backtrace.h" | |
fec097f0 | 49 | #include "libguile/filesys.h" |
22fc179a | 50 | #include "libguile/private-options.h" |
ab4f3efb MD |
51 | |
52 | /* {Error reporting and backtraces} | |
ab4f3efb MD |
53 | * |
54 | * Note that these functions shouldn't generate errors themselves. | |
55 | */ | |
56 | ||
e8df456a AW |
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 ("Throw to key ", port); | |
62 | scm_write (key, port); | |
63 | scm_puts (" with args ", port); | |
64 | scm_write (args, port); | |
65 | return SCM_UNSPECIFIED; | |
66 | } | |
67 | #undef FUNC_NAME | |
68 | ||
60617d81 MW |
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 | ||
e8df456a AW |
79 | SCM |
80 | scm_print_exception (SCM port, SCM frame, SCM key, SCM args) | |
81 | #define FUNC_NAME "print-exception" | |
82 | { | |
60617d81 MW |
83 | static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT; |
84 | scm_i_pthread_once (&once, init_print_exception_var); | |
e8df456a AW |
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); | |
e8df456a | 91 | |
60617d81 | 92 | return scm_call_4 (scm_variable_ref (print_exception_var), |
e8df456a AW |
93 | port, frame, key, args); |
94 | } | |
95 | #undef FUNC_NAME | |
96 | ||
97 | ||
98 | \f | |
99 | ||
dfd03fb9 MD |
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 | ||
ab4f3efb MD |
112 | #undef SCM_ASSERT |
113 | #define SCM_ASSERT(_cond, _arg, _pos, _subr) \ | |
114 | if (!(_cond)) \ | |
115 | return SCM_BOOL_F; | |
ab4f3efb | 116 | |
e8df456a | 117 | |
f3acc5c1 | 118 | void |
6e8d25a6 | 119 | scm_display_error_message (SCM message, SCM args, SCM port) |
ab4f3efb | 120 | { |
9ddf197e AW |
121 | scm_print_exception (port, SCM_BOOL_F, scm_misc_error_key, |
122 | scm_list_3 (SCM_BOOL_F, message, args)); | |
bdf8afff MD |
123 | } |
124 | ||
e40a4095 DH |
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 | |
218d580a | 132 | scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest) |
bdf8afff | 133 | { |
9ddf197e AW |
134 | scm_print_exception (port, frame, scm_misc_error_key, |
135 | scm_list_3 (subr, message, args)); | |
e40a4095 DH |
136 | } |
137 | ||
138 | ||
139 | SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0, | |
218d580a | 140 | (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest), |
c73bdd3a | 141 | "Display an error message to the output port @var{port}.\n" |
218d580a | 142 | "@var{frame} is the frame in which the error occurred, @var{subr} is\n" |
bb2c02f2 | 143 | "the name of the procedure in which the error occurred and\n" |
c73bdd3a MG |
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.") | |
e40a4095 DH |
148 | #define FUNC_NAME s_scm_display_error |
149 | { | |
150 | SCM_VALIDATE_OUTPUT_PORT (2, port); | |
151 | ||
fb2be758 AW |
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 | ||
218d580a | 165 | scm_i_display_error (frame, port, subr, message, args, rest); |
e40a4095 | 166 | |
ab4f3efb MD |
167 | return SCM_UNSPECIFIED; |
168 | } | |
1bbd0b84 | 169 | #undef FUNC_NAME |
ab4f3efb | 170 | |
e40a4095 | 171 | |
2b407f9b MD |
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 | |
3b3b36dd | 187 | SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0, |
5623a9b4 | 188 | (SCM params), |
c73bdd3a MG |
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.") | |
e8e9b690 | 192 | #define FUNC_NAME s_scm_set_print_params_x |
2b407f9b | 193 | { |
3fceef59 MD |
194 | int i; |
195 | int n; | |
2b407f9b MD |
196 | SCM ls; |
197 | print_params_t *new_params; | |
3fceef59 MD |
198 | |
199 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n); | |
c96d76b8 | 200 | for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls)) |
2b407f9b | 201 | SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2 |
e11e83f3 MV |
202 | && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX) |
203 | && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX), | |
2b407f9b MD |
204 | params, |
205 | SCM_ARG2, | |
e8e9b690 | 206 | s_scm_set_print_params_x); |
4c9419ac | 207 | new_params = scm_malloc (n * sizeof (print_params_t)); |
2b407f9b | 208 | if (print_params != default_print_params) |
4c9419ac | 209 | free (print_params); |
2b407f9b MD |
210 | print_params = new_params; |
211 | for (i = 0; i < n; ++i) | |
212 | { | |
e11e83f3 MV |
213 | print_params[i].level = scm_to_int (SCM_CAAR (params)); |
214 | print_params[i].length = scm_to_int (SCM_CADAR (params)); | |
2b407f9b MD |
215 | params = SCM_CDR (params); |
216 | } | |
217 | n_print_params = n; | |
218 | return SCM_UNSPECIFIED; | |
219 | } | |
1bbd0b84 | 220 | #undef FUNC_NAME |
2b407f9b MD |
221 | #endif |
222 | ||
ab4f3efb | 223 | static void |
1bbd0b84 | 224 | indent (int n, SCM port) |
ab4f3efb MD |
225 | { |
226 | int i; | |
227 | for (i = 0; i < n; ++i) | |
b7f3516f | 228 | scm_putc (' ', port); |
ab4f3efb MD |
229 | } |
230 | ||
ab4f3efb | 231 | static void |
34d19ef6 | 232 | display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate) |
ab4f3efb | 233 | { |
2b407f9b | 234 | int i = 0, n; |
92c2555f | 235 | scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport); |
2b407f9b | 236 | do |
ab4f3efb | 237 | { |
2b407f9b MD |
238 | pstate->length = print_params[i].length; |
239 | ptob->seek (sport, 0, SEEK_SET); | |
d2e53ed6 | 240 | if (scm_is_pair (exp)) |
2b407f9b MD |
241 | { |
242 | pstate->level = print_params[i].level - 1; | |
243 | scm_iprlist (hdr, exp, tlr[0], sport, pstate); | |
244 | scm_puts (&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; | |
ab4f3efb | 254 | } |
2b407f9b MD |
255 | while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params); |
256 | ptob->truncate (sport, n); | |
2b407f9b | 257 | |
22e47f69 | 258 | scm_display (scm_strport_to_string (sport), port); |
ab4f3efb MD |
259 | } |
260 | ||
e3c37929 | 261 | static void |
34d19ef6 | 262 | display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate) |
e3c37929 | 263 | { |
aa3f6951 | 264 | SCM proc = scm_frame_procedure (frame); |
7888309b | 265 | SCM name = (scm_is_true (scm_procedure_p (proc)) |
e3c37929 MD |
266 | ? scm_procedure_name (proc) |
267 | : SCM_BOOL_F); | |
268 | display_frame_expr ("[", | |
7888309b | 269 | scm_cons (scm_is_true (name) ? name : proc, |
aa3f6951 AW |
270 | scm_frame_arguments (frame)), |
271 | "]", | |
e3c37929 MD |
272 | indentation, |
273 | sport, | |
274 | port, | |
275 | pstate); | |
276 | } | |
277 | ||
3b3b36dd | 278 | SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0, |
1bbd0b84 | 279 | (SCM frame, SCM port, SCM indent), |
c73bdd3a MG |
280 | "Display a procedure application @var{frame} to the output port\n" |
281 | "@var{port}. @var{indent} specifies the indentation of the\n" | |
282 | "output.") | |
1bbd0b84 | 283 | #define FUNC_NAME s_scm_display_application |
e3c37929 | 284 | { |
34d19ef6 | 285 | SCM_VALIDATE_FRAME (1, frame); |
e3c37929 | 286 | if (SCM_UNBNDP (port)) |
9de87eea | 287 | port = scm_current_output_port (); |
2b407f9b | 288 | else |
34d19ef6 | 289 | SCM_VALIDATE_OPOUTPORT (2, port); |
2b407f9b MD |
290 | if (SCM_UNBNDP (indent)) |
291 | indent = SCM_INUM0; | |
2b407f9b | 292 | |
aa3f6951 AW |
293 | /* Display an application. */ |
294 | { | |
295 | SCM sport, print_state; | |
296 | scm_print_state *pstate; | |
e3c37929 | 297 | |
aa3f6951 | 298 | /* Create a string port used for adaptation of printing parameters. */ |
0b2c2ba3 | 299 | sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
aa3f6951 AW |
300 | SCM_OPN | SCM_WRTNG, |
301 | FUNC_NAME); | |
302 | ||
303 | /* Create a print state for printing of frames. */ | |
304 | print_state = scm_make_print_state (); | |
305 | pstate = SCM_PRINT_STATE (print_state); | |
306 | pstate->writingp = 1; | |
307 | pstate->fancyp = 1; | |
e3c37929 | 308 | |
aa3f6951 AW |
309 | display_application (frame, scm_to_int (indent), sport, port, pstate); |
310 | return SCM_BOOL_T; | |
311 | } | |
e3c37929 | 312 | } |
1bbd0b84 | 313 | #undef FUNC_NAME |
e3c37929 | 314 | |
fec097f0 MV |
315 | SCM_SYMBOL (sym_base, "base"); |
316 | ||
317 | static void | |
318 | display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line) | |
319 | { | |
aa3f6951 | 320 | SCM source = scm_frame_source (frame); |
e052d296 | 321 | *file = *line = SCM_BOOL_F; |
b3f04491 AW |
322 | if (scm_is_pair (source) |
323 | && scm_is_pair (scm_cdr (source)) | |
324 | && scm_is_pair (scm_cddr (source)) | |
325 | && !scm_is_pair (scm_cdddr (source))) | |
e052d296 | 326 | { |
028e3d06 AW |
327 | /* (addr . (filename . (line . column))), from vm compilation */ |
328 | *file = scm_cadr (source); | |
329 | *line = scm_caddr (source); | |
e052d296 | 330 | } |
fec097f0 MV |
331 | } |
332 | ||
333 | static void | |
334 | display_backtrace_file (frame, last_file, port, pstate) | |
335 | SCM frame; | |
336 | SCM *last_file; | |
337 | SCM port; | |
338 | scm_print_state *pstate; | |
339 | { | |
340 | SCM file, line; | |
341 | ||
342 | display_backtrace_get_file_line (frame, &file, &line); | |
343 | ||
aa3f6951 | 344 | if (scm_is_true (scm_equal_p (file, *last_file))) |
fec097f0 MV |
345 | return; |
346 | ||
347 | *last_file = file; | |
348 | ||
349 | scm_puts ("In ", port); | |
7888309b MV |
350 | if (scm_is_false (file)) |
351 | if (scm_is_false (line)) | |
fec097f0 MV |
352 | scm_puts ("unknown file", port); |
353 | else | |
354 | scm_puts ("current input", port); | |
355 | else | |
356 | { | |
357 | pstate->writingp = 0; | |
358 | scm_iprin1 (file, port, pstate); | |
359 | pstate->writingp = 1; | |
360 | } | |
361 | scm_puts (":\n", port); | |
362 | } | |
363 | ||
364 | static void | |
365 | display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate) | |
366 | { | |
367 | SCM file, line; | |
368 | ||
369 | display_backtrace_get_file_line (frame, &file, &line); | |
370 | ||
bc36d050 | 371 | if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base)) |
fec097f0 | 372 | { |
7888309b | 373 | if (scm_is_false (file)) |
fec097f0 | 374 | { |
7888309b | 375 | if (scm_is_false (line)) |
fec097f0 MV |
376 | scm_putc ('?', port); |
377 | else | |
378 | scm_puts ("<stdin>", port); | |
379 | } | |
380 | else | |
381 | { | |
382 | pstate -> writingp = 0; | |
4110fa69 | 383 | #ifdef HAVE_POSIX |
3b9ee0a4 MV |
384 | scm_iprin1 ((scm_is_string (file)? |
385 | scm_basename (file, SCM_UNDEFINED) : file), | |
fec097f0 | 386 | port, pstate); |
4110fa69 MV |
387 | #else |
388 | scm_iprin1 (file, port, pstate); | |
389 | #endif | |
fec097f0 MV |
390 | pstate -> writingp = 1; |
391 | } | |
392 | ||
393 | scm_putc (':', port); | |
394 | } | |
7888309b | 395 | else if (scm_is_true (line)) |
fec097f0 MV |
396 | { |
397 | int i, j=0; | |
e11e83f3 | 398 | for (i = scm_to_int (line)+1; i > 0; i = i/10, j++) |
fec097f0 MV |
399 | ; |
400 | indent (4-j, port); | |
401 | } | |
402 | ||
7888309b | 403 | if (scm_is_false (line)) |
fec097f0 MV |
404 | scm_puts (" ?", port); |
405 | else | |
e11e83f3 | 406 | scm_intprint (scm_to_int (line) + 1, 10, port); |
fec097f0 MV |
407 | scm_puts (": ", port); |
408 | } | |
409 | ||
ab4f3efb | 410 | static void |
aa3f6951 AW |
411 | display_frame (SCM frame, int n, int nfield, int indentation, |
412 | SCM sport, SCM port, scm_print_state *pstate) | |
ab4f3efb | 413 | { |
aa3f6951 | 414 | int i, j; |
ab4f3efb | 415 | |
fec097f0 | 416 | /* display file name and line number */ |
7888309b | 417 | if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME))) |
fec097f0 MV |
418 | display_backtrace_file_and_line (frame, port, pstate); |
419 | ||
ab4f3efb | 420 | /* Check size of frame number. */ |
ab4f3efb MD |
421 | for (i = 0, j = n; j > 0; ++i) j /= 10; |
422 | ||
423 | /* Number indentation. */ | |
424 | indent (nfield - (i ? i : 1), port); | |
425 | ||
426 | /* Frame number. */ | |
93ccaef0 | 427 | scm_iprin1 (scm_from_int (n), port, pstate); |
ab4f3efb | 428 | |
ab4f3efb MD |
429 | /* Indentation. */ |
430 | indent (indentation, port); | |
431 | ||
aa3f6951 AW |
432 | /* Display an application. */ |
433 | display_application (frame, nfield + 1 + indentation, sport, port, pstate); | |
1ae6af28 | 434 | scm_putc ('\n', port); |
ab4f3efb MD |
435 | } |
436 | ||
bdf8afff MD |
437 | struct display_backtrace_args { |
438 | SCM stack; | |
439 | SCM port; | |
440 | SCM first; | |
441 | SCM depth; | |
2b55be75 | 442 | SCM highlight_objects; |
bdf8afff MD |
443 | }; |
444 | ||
bdf8afff | 445 | static SCM |
5843e5c9 | 446 | display_backtrace_body (struct display_backtrace_args *a) |
1bbd0b84 | 447 | #define FUNC_NAME "display_backtrace_body" |
ab4f3efb MD |
448 | { |
449 | int n_frames, beg, end, n, i, j; | |
03976fee | 450 | int nfield, indentation; |
ab4f3efb | 451 | SCM frame, sport, print_state; |
fec097f0 | 452 | SCM last_file; |
ab4f3efb MD |
453 | scm_print_state *pstate; |
454 | ||
78446828 MV |
455 | a->port = SCM_COERCE_OUTPORT (a->port); |
456 | ||
ab4f3efb | 457 | /* Argument checking and extraction. */ |
5843e5c9 DH |
458 | SCM_VALIDATE_STACK (1, a->stack); |
459 | SCM_VALIDATE_OPOUTPORT (2, a->port); | |
e11e83f3 MV |
460 | n_frames = scm_to_int (scm_stack_length (a->stack)); |
461 | n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH; | |
ab4f3efb MD |
462 | if (SCM_BACKWARDS_P) |
463 | { | |
e11e83f3 | 464 | beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0; |
ab4f3efb MD |
465 | end = beg + n - 1; |
466 | if (end >= n_frames) | |
467 | end = n_frames - 1; | |
468 | n = end - beg + 1; | |
469 | } | |
470 | else | |
471 | { | |
e11e83f3 | 472 | if (scm_is_integer (a->first)) |
ab4f3efb | 473 | { |
e11e83f3 | 474 | beg = scm_to_int (a->first); |
ab4f3efb MD |
475 | end = beg - n + 1; |
476 | if (end < 0) | |
477 | end = 0; | |
478 | } | |
479 | else | |
480 | { | |
481 | beg = n - 1; | |
482 | end = 0; | |
483 | if (beg >= n_frames) | |
484 | beg = n_frames - 1; | |
485 | } | |
486 | n = beg - end + 1; | |
487 | } | |
bdf8afff MD |
488 | SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace); |
489 | SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace); | |
ab4f3efb MD |
490 | |
491 | /* Create a string port used for adaptation of printing parameters. */ | |
0b2c2ba3 | 492 | sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
ab4f3efb | 493 | SCM_OPN | SCM_WRTNG, |
1bbd0b84 | 494 | FUNC_NAME); |
ab4f3efb MD |
495 | |
496 | /* Create a print state for printing of frames. */ | |
497 | print_state = scm_make_print_state (); | |
498 | pstate = SCM_PRINT_STATE (print_state); | |
499 | pstate->writingp = 1; | |
500 | pstate->fancyp = 1; | |
2b55be75 | 501 | pstate->highlight_objects = a->highlight_objects; |
ab4f3efb | 502 | |
ab4f3efb | 503 | /* Determine size of frame number field. */ |
aa3f6951 | 504 | j = end; |
ab4f3efb MD |
505 | for (i = 0; j > 0; ++i) j /= 10; |
506 | nfield = i ? i : 1; | |
507 | ||
ab4f3efb | 508 | /* Print frames. */ |
ab4f3efb | 509 | indentation = 1; |
fec097f0 | 510 | last_file = SCM_UNDEFINED; |
aa3f6951 AW |
511 | if (SCM_BACKWARDS_P) |
512 | end++; | |
513 | else | |
514 | end--; | |
515 | for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i) | |
ab4f3efb | 516 | { |
aa3f6951 | 517 | frame = scm_stack_ref (a->stack, scm_from_int (i)); |
bc36d050 | 518 | if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base)) |
fec097f0 | 519 | display_backtrace_file (frame, &last_file, a->port, pstate); |
aa3f6951 | 520 | display_frame (frame, i, nfield, indentation, sport, a->port, pstate); |
ab4f3efb | 521 | } |
bdf8afff | 522 | |
463b2219 ML |
523 | scm_remember_upto_here_1 (print_state); |
524 | ||
bdf8afff MD |
525 | return SCM_UNSPECIFIED; |
526 | } | |
1bbd0b84 | 527 | #undef FUNC_NAME |
bdf8afff | 528 | |
9ddf197e AW |
529 | static SCM |
530 | error_during_backtrace (void *data, SCM tag, SCM throw_args) | |
531 | { | |
532 | SCM port = PTR2SCM (data); | |
533 | ||
534 | scm_puts ("Exception thrown while printing backtrace:\n", port); | |
535 | scm_print_exception (port, SCM_BOOL_F, tag, throw_args); | |
536 | ||
537 | return SCM_UNSPECIFIED; | |
538 | } | |
539 | ||
540 | ||
09c6d80a | 541 | SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0, |
2b55be75 | 542 | (SCM stack, SCM port, SCM first, SCM depth, SCM highlights), |
b5944f66 | 543 | "Display a backtrace to the output port @var{port}. @var{stack}\n" |
c73bdd3a | 544 | "is the stack to take the backtrace from, @var{first} specifies\n" |
b5944f66 NJ |
545 | "where in the stack to start and @var{depth} how many frames\n" |
546 | "to display. @var{first} and @var{depth} can be @code{#f},\n" | |
c648c681 | 547 | "which means that default values will be used.\n" |
b5944f66 NJ |
548 | "If @var{highlights} is given it should be a list; the elements\n" |
549 | "of this list will be highlighted wherever they appear in the\n" | |
550 | "backtrace.") | |
2b55be75 | 551 | #define FUNC_NAME s_scm_display_backtrace_with_highlights |
bdf8afff | 552 | { |
0b2cb4ee | 553 | struct display_backtrace_args a; |
0b2cb4ee MD |
554 | a.stack = stack; |
555 | a.port = port; | |
556 | a.first = first; | |
557 | a.depth = depth; | |
2b55be75 MV |
558 | if (SCM_UNBNDP (highlights)) |
559 | a.highlight_objects = SCM_EOL; | |
560 | else | |
561 | a.highlight_objects = highlights; | |
9ddf197e | 562 | |
bdf8afff | 563 | scm_internal_catch (SCM_BOOL_T, |
92c2555f | 564 | (scm_t_catch_body) display_backtrace_body, &a, |
9ddf197e AW |
565 | (scm_t_catch_handler) error_during_backtrace, SCM2PTR (port)); |
566 | ||
ab4f3efb MD |
567 | return SCM_UNSPECIFIED; |
568 | } | |
1bbd0b84 | 569 | #undef FUNC_NAME |
ab4f3efb | 570 | |
2b55be75 MV |
571 | SCM |
572 | scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth) | |
573 | { | |
574 | return scm_display_backtrace_with_highlights (stack, port, first, depth, | |
575 | SCM_EOL); | |
576 | } | |
577 | ||
86d31dfe | 578 | SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?"); |
5aab5d96 | 579 | |
2b55be75 MV |
580 | SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0, |
581 | (SCM highlights), | |
ec16eb78 AW |
582 | "Display a backtrace of the current stack to the current\n" |
583 | "output port. If @var{highlights} is given, it should be\n" | |
584 | "a list; the elements of this list will be highlighted\n" | |
585 | "wherever they appear in the backtrace.") | |
2b55be75 | 586 | #define FUNC_NAME s_scm_backtrace_with_highlights |
5aab5d96 | 587 | { |
9de87eea | 588 | SCM port = scm_current_output_port (); |
ec16eb78 AW |
589 | SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL); |
590 | ||
2b55be75 MV |
591 | if (SCM_UNBNDP (highlights)) |
592 | highlights = SCM_EOL; | |
593 | ||
ec16eb78 AW |
594 | scm_newline (port); |
595 | scm_puts ("Backtrace:\n", port); | |
596 | scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F, | |
597 | highlights); | |
598 | scm_newline (port); | |
599 | ||
5aab5d96 MD |
600 | return SCM_UNSPECIFIED; |
601 | } | |
1bbd0b84 | 602 | #undef FUNC_NAME |
5aab5d96 | 603 | |
2b55be75 MV |
604 | SCM |
605 | scm_backtrace (void) | |
606 | { | |
607 | return scm_backtrace_with_highlights (SCM_EOL); | |
608 | } | |
609 | ||
ab4f3efb MD |
610 | \f |
611 | ||
612 | void | |
613 | scm_init_backtrace () | |
614 | { | |
e8df456a | 615 | scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception); |
a0599745 | 616 | #include "libguile/backtrace.x" |
ab4f3efb | 617 | } |
89e00824 ML |
618 | |
619 | /* | |
620 | Local Variables: | |
621 | c-file-style: "gnu" | |
622 | End: | |
623 | */ |