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 | { | |
0607ebbf | 61 | scm_puts_unlocked ("Throw to key ", port); |
e8df456a | 62 | scm_write (key, port); |
0607ebbf | 63 | scm_puts_unlocked (" with args ", port); |
e8df456a AW |
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) | |
0607ebbf | 228 | scm_putc_unlocked (' ', 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; |
62bd5d66 | 235 | scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (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); | |
0607ebbf | 244 | scm_puts_unlocked (&tlr[1], sport); |
2b407f9b MD |
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 | { |
48192761 | 264 | display_frame_expr ("[", scm_frame_call_representation (frame), "]", |
e3c37929 MD |
265 | indentation, |
266 | sport, | |
267 | port, | |
268 | pstate); | |
269 | } | |
270 | ||
3b3b36dd | 271 | SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0, |
1bbd0b84 | 272 | (SCM frame, SCM port, SCM indent), |
c73bdd3a MG |
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.") | |
1bbd0b84 | 276 | #define FUNC_NAME s_scm_display_application |
e3c37929 | 277 | { |
34d19ef6 | 278 | SCM_VALIDATE_FRAME (1, frame); |
e3c37929 | 279 | if (SCM_UNBNDP (port)) |
9de87eea | 280 | port = scm_current_output_port (); |
2b407f9b | 281 | else |
34d19ef6 | 282 | SCM_VALIDATE_OPOUTPORT (2, port); |
2b407f9b MD |
283 | if (SCM_UNBNDP (indent)) |
284 | indent = SCM_INUM0; | |
2b407f9b | 285 | |
aa3f6951 AW |
286 | /* Display an application. */ |
287 | { | |
288 | SCM sport, print_state; | |
289 | scm_print_state *pstate; | |
e3c37929 | 290 | |
aa3f6951 | 291 | /* Create a string port used for adaptation of printing parameters. */ |
0b2c2ba3 | 292 | sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
aa3f6951 AW |
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; | |
e3c37929 | 301 | |
aa3f6951 AW |
302 | display_application (frame, scm_to_int (indent), sport, port, pstate); |
303 | return SCM_BOOL_T; | |
304 | } | |
e3c37929 | 305 | } |
1bbd0b84 | 306 | #undef FUNC_NAME |
e3c37929 | 307 | |
fec097f0 MV |
308 | SCM_SYMBOL (sym_base, "base"); |
309 | ||
310 | static void | |
311 | display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line) | |
312 | { | |
aa3f6951 | 313 | SCM source = scm_frame_source (frame); |
e052d296 | 314 | *file = *line = SCM_BOOL_F; |
b3f04491 AW |
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))) | |
e052d296 | 319 | { |
028e3d06 AW |
320 | /* (addr . (filename . (line . column))), from vm compilation */ |
321 | *file = scm_cadr (source); | |
322 | *line = scm_caddr (source); | |
e052d296 | 323 | } |
fec097f0 MV |
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 | ||
aa3f6951 | 337 | if (scm_is_true (scm_equal_p (file, *last_file))) |
fec097f0 MV |
338 | return; |
339 | ||
340 | *last_file = file; | |
341 | ||
0607ebbf | 342 | scm_puts_unlocked ("In ", port); |
7888309b MV |
343 | if (scm_is_false (file)) |
344 | if (scm_is_false (line)) | |
0607ebbf | 345 | scm_puts_unlocked ("unknown file", port); |
fec097f0 | 346 | else |
0607ebbf | 347 | scm_puts_unlocked ("current input", port); |
fec097f0 MV |
348 | else |
349 | { | |
350 | pstate->writingp = 0; | |
351 | scm_iprin1 (file, port, pstate); | |
352 | pstate->writingp = 1; | |
353 | } | |
0607ebbf | 354 | scm_puts_unlocked (":\n", port); |
fec097f0 MV |
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 | ||
bc36d050 | 364 | if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base)) |
fec097f0 | 365 | { |
7888309b | 366 | if (scm_is_false (file)) |
fec097f0 | 367 | { |
7888309b | 368 | if (scm_is_false (line)) |
0607ebbf | 369 | scm_putc_unlocked ('?', port); |
fec097f0 | 370 | else |
0607ebbf | 371 | scm_puts_unlocked ("<stdin>", port); |
fec097f0 MV |
372 | } |
373 | else | |
374 | { | |
375 | pstate -> writingp = 0; | |
4110fa69 | 376 | #ifdef HAVE_POSIX |
3b9ee0a4 MV |
377 | scm_iprin1 ((scm_is_string (file)? |
378 | scm_basename (file, SCM_UNDEFINED) : file), | |
fec097f0 | 379 | port, pstate); |
4110fa69 MV |
380 | #else |
381 | scm_iprin1 (file, port, pstate); | |
382 | #endif | |
fec097f0 MV |
383 | pstate -> writingp = 1; |
384 | } | |
385 | ||
0607ebbf | 386 | scm_putc_unlocked (':', port); |
fec097f0 | 387 | } |
7888309b | 388 | else if (scm_is_true (line)) |
fec097f0 MV |
389 | { |
390 | int i, j=0; | |
e11e83f3 | 391 | for (i = scm_to_int (line)+1; i > 0; i = i/10, j++) |
fec097f0 MV |
392 | ; |
393 | indent (4-j, port); | |
394 | } | |
395 | ||
7888309b | 396 | if (scm_is_false (line)) |
0607ebbf | 397 | scm_puts_unlocked (" ?", port); |
fec097f0 | 398 | else |
e11e83f3 | 399 | scm_intprint (scm_to_int (line) + 1, 10, port); |
0607ebbf | 400 | scm_puts_unlocked (": ", port); |
fec097f0 MV |
401 | } |
402 | ||
ab4f3efb | 403 | static void |
aa3f6951 AW |
404 | display_frame (SCM frame, int n, int nfield, int indentation, |
405 | SCM sport, SCM port, scm_print_state *pstate) | |
ab4f3efb | 406 | { |
aa3f6951 | 407 | int i, j; |
ab4f3efb | 408 | |
fec097f0 | 409 | /* display file name and line number */ |
7888309b | 410 | if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME))) |
fec097f0 MV |
411 | display_backtrace_file_and_line (frame, port, pstate); |
412 | ||
ab4f3efb | 413 | /* Check size of frame number. */ |
ab4f3efb MD |
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. */ | |
93ccaef0 | 420 | scm_iprin1 (scm_from_int (n), port, pstate); |
ab4f3efb | 421 | |
ab4f3efb MD |
422 | /* Indentation. */ |
423 | indent (indentation, port); | |
424 | ||
aa3f6951 AW |
425 | /* Display an application. */ |
426 | display_application (frame, nfield + 1 + indentation, sport, port, pstate); | |
0607ebbf | 427 | scm_putc_unlocked ('\n', port); |
ab4f3efb MD |
428 | } |
429 | ||
bdf8afff MD |
430 | struct display_backtrace_args { |
431 | SCM stack; | |
432 | SCM port; | |
433 | SCM first; | |
434 | SCM depth; | |
2b55be75 | 435 | SCM highlight_objects; |
bdf8afff MD |
436 | }; |
437 | ||
bdf8afff | 438 | static SCM |
5843e5c9 | 439 | display_backtrace_body (struct display_backtrace_args *a) |
1bbd0b84 | 440 | #define FUNC_NAME "display_backtrace_body" |
ab4f3efb MD |
441 | { |
442 | int n_frames, beg, end, n, i, j; | |
03976fee | 443 | int nfield, indentation; |
ab4f3efb | 444 | SCM frame, sport, print_state; |
fec097f0 | 445 | SCM last_file; |
ab4f3efb MD |
446 | scm_print_state *pstate; |
447 | ||
78446828 MV |
448 | a->port = SCM_COERCE_OUTPORT (a->port); |
449 | ||
ab4f3efb | 450 | /* Argument checking and extraction. */ |
5843e5c9 DH |
451 | SCM_VALIDATE_STACK (1, a->stack); |
452 | SCM_VALIDATE_OPOUTPORT (2, a->port); | |
e11e83f3 MV |
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; | |
ab4f3efb MD |
455 | if (SCM_BACKWARDS_P) |
456 | { | |
e11e83f3 | 457 | beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0; |
ab4f3efb MD |
458 | end = beg + n - 1; |
459 | if (end >= n_frames) | |
460 | end = n_frames - 1; | |
461 | n = end - beg + 1; | |
462 | } | |
463 | else | |
464 | { | |
e11e83f3 | 465 | if (scm_is_integer (a->first)) |
ab4f3efb | 466 | { |
e11e83f3 | 467 | beg = scm_to_int (a->first); |
ab4f3efb MD |
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 | } | |
bdf8afff MD |
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); | |
ab4f3efb MD |
483 | |
484 | /* Create a string port used for adaptation of printing parameters. */ | |
0b2c2ba3 | 485 | sport = scm_mkstrport (SCM_INUM0, SCM_BOOL_F, |
ab4f3efb | 486 | SCM_OPN | SCM_WRTNG, |
1bbd0b84 | 487 | FUNC_NAME); |
ab4f3efb MD |
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; | |
2b55be75 | 494 | pstate->highlight_objects = a->highlight_objects; |
ab4f3efb | 495 | |
ab4f3efb | 496 | /* Determine size of frame number field. */ |
aa3f6951 | 497 | j = end; |
ab4f3efb MD |
498 | for (i = 0; j > 0; ++i) j /= 10; |
499 | nfield = i ? i : 1; | |
500 | ||
ab4f3efb | 501 | /* Print frames. */ |
ab4f3efb | 502 | indentation = 1; |
fec097f0 | 503 | last_file = SCM_UNDEFINED; |
aa3f6951 AW |
504 | if (SCM_BACKWARDS_P) |
505 | end++; | |
506 | else | |
507 | end--; | |
508 | for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i) | |
ab4f3efb | 509 | { |
aa3f6951 | 510 | frame = scm_stack_ref (a->stack, scm_from_int (i)); |
bc36d050 | 511 | if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base)) |
fec097f0 | 512 | display_backtrace_file (frame, &last_file, a->port, pstate); |
aa3f6951 | 513 | display_frame (frame, i, nfield, indentation, sport, a->port, pstate); |
ab4f3efb | 514 | } |
bdf8afff | 515 | |
463b2219 ML |
516 | scm_remember_upto_here_1 (print_state); |
517 | ||
bdf8afff MD |
518 | return SCM_UNSPECIFIED; |
519 | } | |
1bbd0b84 | 520 | #undef FUNC_NAME |
bdf8afff | 521 | |
9ddf197e AW |
522 | static SCM |
523 | error_during_backtrace (void *data, SCM tag, SCM throw_args) | |
524 | { | |
21041372 | 525 | SCM port = SCM_PACK_POINTER (data); |
9ddf197e | 526 | |
0607ebbf | 527 | scm_puts_unlocked ("Exception thrown while printing backtrace:\n", port); |
9ddf197e AW |
528 | scm_print_exception (port, SCM_BOOL_F, tag, throw_args); |
529 | ||
530 | return SCM_UNSPECIFIED; | |
531 | } | |
532 | ||
533 | ||
09c6d80a | 534 | SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0, |
2b55be75 | 535 | (SCM stack, SCM port, SCM first, SCM depth, SCM highlights), |
b5944f66 | 536 | "Display a backtrace to the output port @var{port}. @var{stack}\n" |
c73bdd3a | 537 | "is the stack to take the backtrace from, @var{first} specifies\n" |
b5944f66 NJ |
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" | |
c648c681 | 540 | "which means that default values will be used.\n" |
b5944f66 NJ |
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.") | |
2b55be75 | 544 | #define FUNC_NAME s_scm_display_backtrace_with_highlights |
bdf8afff | 545 | { |
0b2cb4ee | 546 | struct display_backtrace_args a; |
0b2cb4ee MD |
547 | a.stack = stack; |
548 | a.port = port; | |
549 | a.first = first; | |
550 | a.depth = depth; | |
2b55be75 MV |
551 | if (SCM_UNBNDP (highlights)) |
552 | a.highlight_objects = SCM_EOL; | |
553 | else | |
554 | a.highlight_objects = highlights; | |
9ddf197e | 555 | |
bdf8afff | 556 | scm_internal_catch (SCM_BOOL_T, |
92c2555f | 557 | (scm_t_catch_body) display_backtrace_body, &a, |
21041372 | 558 | (scm_t_catch_handler) error_during_backtrace, SCM_UNPACK_POINTER (port)); |
9ddf197e | 559 | |
ab4f3efb MD |
560 | return SCM_UNSPECIFIED; |
561 | } | |
1bbd0b84 | 562 | #undef FUNC_NAME |
ab4f3efb | 563 | |
2b55be75 MV |
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 | ||
86d31dfe | 571 | SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?"); |
5aab5d96 | 572 | |
2b55be75 MV |
573 | SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0, |
574 | (SCM highlights), | |
ec16eb78 AW |
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.") | |
2b55be75 | 579 | #define FUNC_NAME s_scm_backtrace_with_highlights |
5aab5d96 | 580 | { |
9de87eea | 581 | SCM port = scm_current_output_port (); |
ec16eb78 AW |
582 | SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL); |
583 | ||
2b55be75 MV |
584 | if (SCM_UNBNDP (highlights)) |
585 | highlights = SCM_EOL; | |
586 | ||
ec16eb78 | 587 | scm_newline (port); |
0607ebbf | 588 | scm_puts_unlocked ("Backtrace:\n", port); |
ec16eb78 AW |
589 | scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F, |
590 | highlights); | |
591 | scm_newline (port); | |
592 | ||
5aab5d96 MD |
593 | return SCM_UNSPECIFIED; |
594 | } | |
1bbd0b84 | 595 | #undef FUNC_NAME |
5aab5d96 | 596 | |
2b55be75 MV |
597 | SCM |
598 | scm_backtrace (void) | |
599 | { | |
600 | return scm_backtrace_with_highlights (SCM_EOL); | |
601 | } | |
602 | ||
ab4f3efb MD |
603 | \f |
604 | ||
605 | void | |
606 | scm_init_backtrace () | |
607 | { | |
e8df456a | 608 | scm_c_define_gsubr ("print-exception", 4, 0, 0, boot_print_exception); |
a0599745 | 609 | #include "libguile/backtrace.x" |
ab4f3efb | 610 | } |
89e00824 ML |
611 | |
612 | /* | |
613 | Local Variables: | |
614 | c-file-style: "gnu" | |
615 | End: | |
616 | */ |