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