Commit | Line | Data |
---|---|---|
ab4f3efb | 1 | /* Printing of backtraces and error messages |
cd038da5 | 2 | * Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2009, 2010 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 | ||
dfd03fb9 MD |
58 | /* Print parameters for error messages. */ |
59 | ||
60 | #define DISPLAY_ERROR_MESSAGE_MAX_LEVEL 7 | |
61 | #define DISPLAY_ERROR_MESSAGE_MAX_LENGTH 10 | |
62 | ||
63 | /* Print parameters for failing expressions in error messages. | |
64 | * (See also `print_params' below for backtrace print parameters.) | |
65 | */ | |
66 | ||
67 | #define DISPLAY_EXPRESSION_MAX_LEVEL 2 | |
68 | #define DISPLAY_EXPRESSION_MAX_LENGTH 3 | |
69 | ||
ab4f3efb MD |
70 | #undef SCM_ASSERT |
71 | #define SCM_ASSERT(_cond, _arg, _pos, _subr) \ | |
72 | if (!(_cond)) \ | |
73 | return SCM_BOOL_F; | |
ab4f3efb | 74 | |
ab4f3efb | 75 | static void |
1bbd0b84 | 76 | display_header (SCM source, SCM port) |
ab4f3efb | 77 | { |
218d580a AW |
78 | if (scm_is_true (source)) |
79 | { | |
80 | /* source := (addr . (filename . (line . column))) */ | |
81 | SCM fname = scm_cadr (source); | |
82 | SCM line = scm_caddr (source); | |
83 | SCM col = scm_cdddr (source); | |
84 | ||
85 | if (scm_is_true (fname)) | |
86 | scm_prin1 (fname, port, 0); | |
87 | else | |
88 | scm_puts ("<unnamed port>", port); | |
89 | ||
90 | if (scm_is_true (line) && scm_is_true (col)) | |
91 | { | |
92 | scm_putc (':', port); | |
93 | scm_intprint (scm_to_long (line) + 1, 10, port); | |
94 | scm_putc (':', port); | |
95 | scm_intprint (scm_to_long (col) + 1, 10, port); | |
96 | } | |
97 | } | |
98 | else | |
99 | scm_puts ("ERROR", port); | |
b7f3516f | 100 | scm_puts (": ", port); |
ab4f3efb MD |
101 | } |
102 | ||
f3acc5c1 | 103 | |
dfd03fb9 MD |
104 | struct display_error_message_data { |
105 | SCM message; | |
106 | SCM args; | |
107 | SCM port; | |
108 | scm_print_state *pstate; | |
109 | int old_fancyp; | |
110 | int old_level; | |
111 | int old_length; | |
112 | }; | |
113 | ||
114 | static SCM | |
115 | display_error_message (struct display_error_message_data *d) | |
116 | { | |
3b9ee0a4 | 117 | if (scm_is_string (d->message) && scm_is_true (scm_list_p (d->args))) |
dfd03fb9 MD |
118 | scm_simple_format (d->port, d->message, d->args); |
119 | else | |
120 | scm_display (d->message, d->port); | |
121 | scm_newline (d->port); | |
122 | return SCM_UNSPECIFIED; | |
123 | } | |
124 | ||
125 | static void | |
126 | before_display_error_message (struct display_error_message_data *d) | |
127 | { | |
128 | scm_print_state *pstate = d->pstate; | |
129 | d->old_fancyp = pstate->fancyp; | |
130 | d->old_level = pstate->level; | |
131 | d->old_length = pstate->length; | |
132 | pstate->fancyp = 1; | |
133 | pstate->level = DISPLAY_ERROR_MESSAGE_MAX_LEVEL; | |
134 | pstate->length = DISPLAY_ERROR_MESSAGE_MAX_LENGTH; | |
135 | } | |
136 | ||
137 | static void | |
138 | after_display_error_message (struct display_error_message_data *d) | |
139 | { | |
140 | scm_print_state *pstate = d->pstate; | |
141 | pstate->fancyp = d->old_fancyp; | |
142 | pstate->level = d->old_level; | |
143 | pstate->length = d->old_length; | |
144 | } | |
145 | ||
f3acc5c1 | 146 | void |
6e8d25a6 | 147 | scm_display_error_message (SCM message, SCM args, SCM port) |
ab4f3efb | 148 | { |
dfd03fb9 MD |
149 | struct display_error_message_data d; |
150 | SCM print_state; | |
151 | scm_print_state *pstate; | |
152 | ||
153 | port = scm_i_port_with_print_state (port, SCM_UNDEFINED); | |
154 | print_state = SCM_PORT_WITH_PS_PS (port); | |
155 | pstate = SCM_PRINT_STATE (print_state); | |
156 | ||
157 | d.message = message; | |
158 | d.args = args; | |
159 | d.port = port; | |
160 | d.pstate = pstate; | |
161 | scm_internal_dynamic_wind ((scm_t_guard) before_display_error_message, | |
162 | (scm_t_inner) display_error_message, | |
163 | (scm_t_guard) after_display_error_message, | |
164 | &d, | |
165 | &d); | |
ab4f3efb MD |
166 | } |
167 | ||
ab4f3efb | 168 | static void |
34d19ef6 | 169 | display_expression (SCM frame, SCM pname, SCM source, SCM port) |
ab4f3efb MD |
170 | { |
171 | SCM print_state = scm_make_print_state (); | |
172 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
173 | pstate->writingp = 0; | |
174 | pstate->fancyp = 1; | |
dfd03fb9 MD |
175 | pstate->level = DISPLAY_EXPRESSION_MAX_LEVEL; |
176 | pstate->length = DISPLAY_EXPRESSION_MAX_LENGTH; | |
cc95e00a | 177 | if (scm_is_symbol (pname) || scm_is_string (pname)) |
ab4f3efb | 178 | { |
aa3f6951 | 179 | scm_puts ("In procedure ", port); |
ab4f3efb | 180 | scm_iprin1 (pname, port, pstate); |
ab4f3efb | 181 | } |
b7f3516f | 182 | scm_puts (":\n", port); |
ab4f3efb MD |
183 | scm_free_print_state (print_state); |
184 | } | |
185 | ||
bdf8afff | 186 | struct display_error_args { |
218d580a | 187 | SCM frame; |
bdf8afff MD |
188 | SCM port; |
189 | SCM subr; | |
190 | SCM message; | |
191 | SCM args; | |
192 | SCM rest; | |
193 | }; | |
194 | ||
195 | static SCM | |
39752bec | 196 | display_error_body (struct display_error_args *a) |
ab4f3efb | 197 | { |
ab4f3efb | 198 | SCM source = SCM_BOOL_F; |
b24b5e13 | 199 | SCM pname = a->subr; |
a88a4c8a | 200 | |
218d580a AW |
201 | if (SCM_FRAMEP (a->frame)) |
202 | { | |
203 | source = scm_frame_source (a->frame); | |
204 | if (!scm_is_symbol (pname) && !scm_is_string (pname)) | |
205 | pname = scm_procedure_name (scm_frame_procedure (a->frame)); | |
206 | } | |
207 | ||
b3f04491 | 208 | if (scm_is_symbol (pname) || scm_is_string (pname)) |
ab4f3efb | 209 | { |
bdf8afff | 210 | display_header (source, a->port); |
218d580a | 211 | display_expression (a->frame, pname, source, a->port); |
ab4f3efb | 212 | } |
bdf8afff MD |
213 | display_header (source, a->port); |
214 | scm_display_error_message (a->message, a->args, a->port); | |
215 | return SCM_UNSPECIFIED; | |
216 | } | |
217 | ||
218 | struct display_error_handler_data { | |
219 | char *mode; | |
220 | SCM port; | |
221 | }; | |
222 | ||
223 | /* This is the exception handler for error reporting routines. | |
224 | Note that it is very important that this handler *doesn't* try to | |
225 | print more than the error tag, since the error very probably is | |
226 | caused by an erroneous print call-back routine. If we would | |
2fdcf8bd | 227 | try to print all objects, we would enter an infinite loop. */ |
bdf8afff MD |
228 | static SCM |
229 | display_error_handler (struct display_error_handler_data *data, | |
e81d98ec | 230 | SCM tag, SCM args SCM_UNUSED) |
bdf8afff MD |
231 | { |
232 | SCM print_state = scm_make_print_state (); | |
b7f3516f TT |
233 | scm_puts ("\nException during displaying of ", data->port); |
234 | scm_puts (data->mode, data->port); | |
235 | scm_puts (": ", data->port); | |
bdf8afff | 236 | scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state)); |
b7f3516f | 237 | scm_putc ('\n', data->port); |
bdf8afff MD |
238 | return SCM_UNSPECIFIED; |
239 | } | |
240 | ||
e40a4095 DH |
241 | |
242 | /* The function scm_i_display_error prints out a detailed error message. This | |
243 | * function will be called directly within libguile to signal error messages. | |
244 | * No parameter checks will be performed by scm_i_display_error. Thus, User | |
245 | * code should rather use the function scm_display_error. | |
246 | */ | |
247 | void | |
218d580a | 248 | scm_i_display_error (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest) |
bdf8afff | 249 | { |
0b2cb4ee MD |
250 | struct display_error_args a; |
251 | struct display_error_handler_data data; | |
218d580a AW |
252 | |
253 | if (SCM_FRAMEP (frame)) | |
254 | a.frame = frame; | |
255 | #if SCM_ENABLE_DEPRECATED | |
256 | else if (SCM_STACKP (frame)) | |
257 | { | |
258 | scm_c_issue_deprecation_warning | |
259 | ("Passing a stack to display-error is deprecated. Pass a frame instead."); | |
260 | a.frame = scm_stack_ref (frame, SCM_INUM0); | |
261 | } | |
262 | #endif | |
263 | else | |
264 | a.frame = SCM_BOOL_F; | |
0b2cb4ee MD |
265 | a.port = port; |
266 | a.subr = subr; | |
267 | a.message = message; | |
268 | a.args = args; | |
269 | a.rest = rest; | |
270 | data.mode = "error"; | |
271 | data.port = port; | |
bdf8afff | 272 | scm_internal_catch (SCM_BOOL_T, |
92c2555f MV |
273 | (scm_t_catch_body) display_error_body, &a, |
274 | (scm_t_catch_handler) display_error_handler, &data); | |
e40a4095 DH |
275 | } |
276 | ||
277 | ||
278 | SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0, | |
218d580a | 279 | (SCM frame, SCM port, SCM subr, SCM message, SCM args, SCM rest), |
c73bdd3a | 280 | "Display an error message to the output port @var{port}.\n" |
218d580a | 281 | "@var{frame} is the frame in which the error occurred, @var{subr} is\n" |
bb2c02f2 | 282 | "the name of the procedure in which the error occurred and\n" |
c73bdd3a MG |
283 | "@var{message} is the actual error message, which may contain\n" |
284 | "formatting instructions. These will format the arguments in\n" | |
285 | "the list @var{args} accordingly. @var{rest} is currently\n" | |
286 | "ignored.") | |
e40a4095 DH |
287 | #define FUNC_NAME s_scm_display_error |
288 | { | |
289 | SCM_VALIDATE_OUTPUT_PORT (2, port); | |
290 | ||
218d580a | 291 | scm_i_display_error (frame, port, subr, message, args, rest); |
e40a4095 | 292 | |
ab4f3efb MD |
293 | return SCM_UNSPECIFIED; |
294 | } | |
1bbd0b84 | 295 | #undef FUNC_NAME |
ab4f3efb | 296 | |
e40a4095 | 297 | |
2b407f9b MD |
298 | typedef struct { |
299 | int level; | |
300 | int length; | |
301 | } print_params_t; | |
302 | ||
303 | static int n_print_params = 9; | |
304 | static print_params_t default_print_params[] = { | |
305 | { 4, 9 }, { 4, 3 }, | |
306 | { 3, 4 }, { 3, 3 }, | |
307 | { 2, 4 }, { 2, 3 }, | |
308 | { 1, 4 }, { 1, 3 }, { 1, 2 } | |
309 | }; | |
310 | static print_params_t *print_params = default_print_params; | |
311 | ||
312 | #ifdef GUILE_DEBUG | |
3b3b36dd | 313 | SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0, |
5623a9b4 | 314 | (SCM params), |
c73bdd3a MG |
315 | "Set the print parameters to the values from @var{params}.\n" |
316 | "@var{params} must be a list of two-element lists which must\n" | |
317 | "hold two integer values.") | |
e8e9b690 | 318 | #define FUNC_NAME s_scm_set_print_params_x |
2b407f9b | 319 | { |
3fceef59 MD |
320 | int i; |
321 | int n; | |
2b407f9b MD |
322 | SCM ls; |
323 | print_params_t *new_params; | |
3fceef59 MD |
324 | |
325 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n); | |
c96d76b8 | 326 | for (ls = params; !SCM_NULL_OR_NIL_P (ls); ls = SCM_CDR (ls)) |
2b407f9b | 327 | SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2 |
e11e83f3 MV |
328 | && scm_is_unsigned_integer (SCM_CAAR (ls), 0, INT_MAX) |
329 | && scm_is_unsigned_integer (SCM_CADAR (ls), 0, INT_MAX), | |
2b407f9b MD |
330 | params, |
331 | SCM_ARG2, | |
e8e9b690 | 332 | s_scm_set_print_params_x); |
4c9419ac | 333 | new_params = scm_malloc (n * sizeof (print_params_t)); |
2b407f9b | 334 | if (print_params != default_print_params) |
4c9419ac | 335 | free (print_params); |
2b407f9b MD |
336 | print_params = new_params; |
337 | for (i = 0; i < n; ++i) | |
338 | { | |
e11e83f3 MV |
339 | print_params[i].level = scm_to_int (SCM_CAAR (params)); |
340 | print_params[i].length = scm_to_int (SCM_CADAR (params)); | |
2b407f9b MD |
341 | params = SCM_CDR (params); |
342 | } | |
343 | n_print_params = n; | |
344 | return SCM_UNSPECIFIED; | |
345 | } | |
1bbd0b84 | 346 | #undef FUNC_NAME |
2b407f9b MD |
347 | #endif |
348 | ||
ab4f3efb | 349 | static void |
1bbd0b84 | 350 | indent (int n, SCM port) |
ab4f3efb MD |
351 | { |
352 | int i; | |
353 | for (i = 0; i < n; ++i) | |
b7f3516f | 354 | scm_putc (' ', port); |
ab4f3efb MD |
355 | } |
356 | ||
ab4f3efb | 357 | static void |
34d19ef6 | 358 | display_frame_expr (char *hdr, SCM exp, char *tlr, int indentation, SCM sport, SCM port, scm_print_state *pstate) |
ab4f3efb | 359 | { |
2b407f9b | 360 | int i = 0, n; |
92c2555f | 361 | scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport); |
2b407f9b | 362 | do |
ab4f3efb | 363 | { |
2b407f9b MD |
364 | pstate->length = print_params[i].length; |
365 | ptob->seek (sport, 0, SEEK_SET); | |
d2e53ed6 | 366 | if (scm_is_pair (exp)) |
2b407f9b MD |
367 | { |
368 | pstate->level = print_params[i].level - 1; | |
369 | scm_iprlist (hdr, exp, tlr[0], sport, pstate); | |
370 | scm_puts (&tlr[1], sport); | |
371 | } | |
372 | else | |
373 | { | |
374 | pstate->level = print_params[i].level; | |
375 | scm_iprin1 (exp, sport, pstate); | |
376 | } | |
377 | ptob->flush (sport); | |
378 | n = ptob->seek (sport, 0, SEEK_CUR); | |
379 | ++i; | |
ab4f3efb | 380 | } |
2b407f9b MD |
381 | while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params); |
382 | ptob->truncate (sport, n); | |
2b407f9b | 383 | |
22e47f69 | 384 | scm_display (scm_strport_to_string (sport), port); |
ab4f3efb MD |
385 | } |
386 | ||
e3c37929 | 387 | static void |
34d19ef6 | 388 | display_application (SCM frame, int indentation, SCM sport, SCM port, scm_print_state *pstate) |
e3c37929 | 389 | { |
aa3f6951 | 390 | SCM proc = scm_frame_procedure (frame); |
7888309b | 391 | SCM name = (scm_is_true (scm_procedure_p (proc)) |
e3c37929 MD |
392 | ? scm_procedure_name (proc) |
393 | : SCM_BOOL_F); | |
394 | display_frame_expr ("[", | |
7888309b | 395 | scm_cons (scm_is_true (name) ? name : proc, |
aa3f6951 AW |
396 | scm_frame_arguments (frame)), |
397 | "]", | |
e3c37929 MD |
398 | indentation, |
399 | sport, | |
400 | port, | |
401 | pstate); | |
402 | } | |
403 | ||
3b3b36dd | 404 | SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0, |
1bbd0b84 | 405 | (SCM frame, SCM port, SCM indent), |
c73bdd3a MG |
406 | "Display a procedure application @var{frame} to the output port\n" |
407 | "@var{port}. @var{indent} specifies the indentation of the\n" | |
408 | "output.") | |
1bbd0b84 | 409 | #define FUNC_NAME s_scm_display_application |
e3c37929 | 410 | { |
34d19ef6 | 411 | SCM_VALIDATE_FRAME (1, frame); |
e3c37929 | 412 | if (SCM_UNBNDP (port)) |
9de87eea | 413 | port = scm_current_output_port (); |
2b407f9b | 414 | else |
34d19ef6 | 415 | SCM_VALIDATE_OPOUTPORT (2, port); |
2b407f9b MD |
416 | if (SCM_UNBNDP (indent)) |
417 | indent = SCM_INUM0; | |
2b407f9b | 418 | |
aa3f6951 AW |
419 | /* Display an application. */ |
420 | { | |
421 | SCM sport, print_state; | |
422 | scm_print_state *pstate; | |
e3c37929 | 423 | |
aa3f6951 AW |
424 | /* Create a string port used for adaptation of printing parameters. */ |
425 | sport = scm_mkstrport (SCM_INUM0, | |
426 | scm_make_string (scm_from_int (240), | |
427 | SCM_UNDEFINED), | |
428 | SCM_OPN | SCM_WRTNG, | |
429 | FUNC_NAME); | |
430 | ||
431 | /* Create a print state for printing of frames. */ | |
432 | print_state = scm_make_print_state (); | |
433 | pstate = SCM_PRINT_STATE (print_state); | |
434 | pstate->writingp = 1; | |
435 | pstate->fancyp = 1; | |
e3c37929 | 436 | |
aa3f6951 AW |
437 | display_application (frame, scm_to_int (indent), sport, port, pstate); |
438 | return SCM_BOOL_T; | |
439 | } | |
e3c37929 | 440 | } |
1bbd0b84 | 441 | #undef FUNC_NAME |
e3c37929 | 442 | |
fec097f0 MV |
443 | SCM_SYMBOL (sym_base, "base"); |
444 | ||
445 | static void | |
446 | display_backtrace_get_file_line (SCM frame, SCM *file, SCM *line) | |
447 | { | |
aa3f6951 | 448 | SCM source = scm_frame_source (frame); |
e052d296 | 449 | *file = *line = SCM_BOOL_F; |
b3f04491 AW |
450 | if (scm_is_pair (source) |
451 | && scm_is_pair (scm_cdr (source)) | |
452 | && scm_is_pair (scm_cddr (source)) | |
453 | && !scm_is_pair (scm_cdddr (source))) | |
e052d296 | 454 | { |
028e3d06 AW |
455 | /* (addr . (filename . (line . column))), from vm compilation */ |
456 | *file = scm_cadr (source); | |
457 | *line = scm_caddr (source); | |
e052d296 | 458 | } |
fec097f0 MV |
459 | } |
460 | ||
461 | static void | |
462 | display_backtrace_file (frame, last_file, port, pstate) | |
463 | SCM frame; | |
464 | SCM *last_file; | |
465 | SCM port; | |
466 | scm_print_state *pstate; | |
467 | { | |
468 | SCM file, line; | |
469 | ||
470 | display_backtrace_get_file_line (frame, &file, &line); | |
471 | ||
aa3f6951 | 472 | if (scm_is_true (scm_equal_p (file, *last_file))) |
fec097f0 MV |
473 | return; |
474 | ||
475 | *last_file = file; | |
476 | ||
477 | scm_puts ("In ", port); | |
7888309b MV |
478 | if (scm_is_false (file)) |
479 | if (scm_is_false (line)) | |
fec097f0 MV |
480 | scm_puts ("unknown file", port); |
481 | else | |
482 | scm_puts ("current input", port); | |
483 | else | |
484 | { | |
485 | pstate->writingp = 0; | |
486 | scm_iprin1 (file, port, pstate); | |
487 | pstate->writingp = 1; | |
488 | } | |
489 | scm_puts (":\n", port); | |
490 | } | |
491 | ||
492 | static void | |
493 | display_backtrace_file_and_line (SCM frame, SCM port, scm_print_state *pstate) | |
494 | { | |
495 | SCM file, line; | |
496 | ||
497 | display_backtrace_get_file_line (frame, &file, &line); | |
498 | ||
bc36d050 | 499 | if (scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base)) |
fec097f0 | 500 | { |
7888309b | 501 | if (scm_is_false (file)) |
fec097f0 | 502 | { |
7888309b | 503 | if (scm_is_false (line)) |
fec097f0 MV |
504 | scm_putc ('?', port); |
505 | else | |
506 | scm_puts ("<stdin>", port); | |
507 | } | |
508 | else | |
509 | { | |
510 | pstate -> writingp = 0; | |
4110fa69 | 511 | #ifdef HAVE_POSIX |
3b9ee0a4 MV |
512 | scm_iprin1 ((scm_is_string (file)? |
513 | scm_basename (file, SCM_UNDEFINED) : file), | |
fec097f0 | 514 | port, pstate); |
4110fa69 MV |
515 | #else |
516 | scm_iprin1 (file, port, pstate); | |
517 | #endif | |
fec097f0 MV |
518 | pstate -> writingp = 1; |
519 | } | |
520 | ||
521 | scm_putc (':', port); | |
522 | } | |
7888309b | 523 | else if (scm_is_true (line)) |
fec097f0 MV |
524 | { |
525 | int i, j=0; | |
e11e83f3 | 526 | for (i = scm_to_int (line)+1; i > 0; i = i/10, j++) |
fec097f0 MV |
527 | ; |
528 | indent (4-j, port); | |
529 | } | |
530 | ||
7888309b | 531 | if (scm_is_false (line)) |
fec097f0 MV |
532 | scm_puts (" ?", port); |
533 | else | |
e11e83f3 | 534 | scm_intprint (scm_to_int (line) + 1, 10, port); |
fec097f0 MV |
535 | scm_puts (": ", port); |
536 | } | |
537 | ||
ab4f3efb | 538 | static void |
aa3f6951 AW |
539 | display_frame (SCM frame, int n, int nfield, int indentation, |
540 | SCM sport, SCM port, scm_print_state *pstate) | |
ab4f3efb | 541 | { |
aa3f6951 | 542 | int i, j; |
ab4f3efb | 543 | |
fec097f0 | 544 | /* display file name and line number */ |
7888309b | 545 | if (scm_is_true (SCM_PACK (SCM_SHOW_FILE_NAME))) |
fec097f0 MV |
546 | display_backtrace_file_and_line (frame, port, pstate); |
547 | ||
ab4f3efb | 548 | /* Check size of frame number. */ |
ab4f3efb MD |
549 | for (i = 0, j = n; j > 0; ++i) j /= 10; |
550 | ||
551 | /* Number indentation. */ | |
552 | indent (nfield - (i ? i : 1), port); | |
553 | ||
554 | /* Frame number. */ | |
93ccaef0 | 555 | scm_iprin1 (scm_from_int (n), port, pstate); |
ab4f3efb | 556 | |
ab4f3efb MD |
557 | /* Indentation. */ |
558 | indent (indentation, port); | |
559 | ||
aa3f6951 AW |
560 | /* Display an application. */ |
561 | display_application (frame, nfield + 1 + indentation, sport, port, pstate); | |
1ae6af28 | 562 | scm_putc ('\n', port); |
ab4f3efb MD |
563 | } |
564 | ||
bdf8afff MD |
565 | struct display_backtrace_args { |
566 | SCM stack; | |
567 | SCM port; | |
568 | SCM first; | |
569 | SCM depth; | |
2b55be75 | 570 | SCM highlight_objects; |
bdf8afff MD |
571 | }; |
572 | ||
bdf8afff | 573 | static SCM |
5843e5c9 | 574 | display_backtrace_body (struct display_backtrace_args *a) |
1bbd0b84 | 575 | #define FUNC_NAME "display_backtrace_body" |
ab4f3efb MD |
576 | { |
577 | int n_frames, beg, end, n, i, j; | |
578 | int nfield, indent_p, indentation; | |
579 | SCM frame, sport, print_state; | |
fec097f0 | 580 | SCM last_file; |
ab4f3efb MD |
581 | scm_print_state *pstate; |
582 | ||
78446828 MV |
583 | a->port = SCM_COERCE_OUTPORT (a->port); |
584 | ||
ab4f3efb | 585 | /* Argument checking and extraction. */ |
5843e5c9 DH |
586 | SCM_VALIDATE_STACK (1, a->stack); |
587 | SCM_VALIDATE_OPOUTPORT (2, a->port); | |
e11e83f3 MV |
588 | n_frames = scm_to_int (scm_stack_length (a->stack)); |
589 | n = scm_is_integer (a->depth) ? scm_to_int (a->depth) : SCM_BACKTRACE_DEPTH; | |
ab4f3efb MD |
590 | if (SCM_BACKWARDS_P) |
591 | { | |
e11e83f3 | 592 | beg = scm_is_integer (a->first) ? scm_to_int (a->first) : 0; |
ab4f3efb MD |
593 | end = beg + n - 1; |
594 | if (end >= n_frames) | |
595 | end = n_frames - 1; | |
596 | n = end - beg + 1; | |
597 | } | |
598 | else | |
599 | { | |
e11e83f3 | 600 | if (scm_is_integer (a->first)) |
ab4f3efb | 601 | { |
e11e83f3 | 602 | beg = scm_to_int (a->first); |
ab4f3efb MD |
603 | end = beg - n + 1; |
604 | if (end < 0) | |
605 | end = 0; | |
606 | } | |
607 | else | |
608 | { | |
609 | beg = n - 1; | |
610 | end = 0; | |
611 | if (beg >= n_frames) | |
612 | beg = n_frames - 1; | |
613 | } | |
614 | n = beg - end + 1; | |
615 | } | |
bdf8afff MD |
616 | SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace); |
617 | SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace); | |
ab4f3efb MD |
618 | |
619 | /* Create a string port used for adaptation of printing parameters. */ | |
620 | sport = scm_mkstrport (SCM_INUM0, | |
93ccaef0 | 621 | scm_make_string (scm_from_int (240), SCM_UNDEFINED), |
ab4f3efb | 622 | SCM_OPN | SCM_WRTNG, |
1bbd0b84 | 623 | FUNC_NAME); |
ab4f3efb MD |
624 | |
625 | /* Create a print state for printing of frames. */ | |
626 | print_state = scm_make_print_state (); | |
627 | pstate = SCM_PRINT_STATE (print_state); | |
628 | pstate->writingp = 1; | |
629 | pstate->fancyp = 1; | |
2b55be75 | 630 | pstate->highlight_objects = a->highlight_objects; |
ab4f3efb MD |
631 | |
632 | /* First find out if it's reasonable to do indentation. */ | |
aa3f6951 | 633 | indent_p = 0; |
ab4f3efb MD |
634 | |
635 | /* Determine size of frame number field. */ | |
aa3f6951 | 636 | j = end; |
ab4f3efb MD |
637 | for (i = 0; j > 0; ++i) j /= 10; |
638 | nfield = i ? i : 1; | |
639 | ||
ab4f3efb | 640 | /* Print frames. */ |
ab4f3efb | 641 | indentation = 1; |
fec097f0 | 642 | last_file = SCM_UNDEFINED; |
aa3f6951 AW |
643 | if (SCM_BACKWARDS_P) |
644 | end++; | |
645 | else | |
646 | end--; | |
647 | for (i = beg; i != end; SCM_BACKWARDS_P ? ++i : --i) | |
ab4f3efb | 648 | { |
aa3f6951 | 649 | frame = scm_stack_ref (a->stack, scm_from_int (i)); |
bc36d050 | 650 | if (!scm_is_eq (SCM_PACK (SCM_SHOW_FILE_NAME), sym_base)) |
fec097f0 | 651 | display_backtrace_file (frame, &last_file, a->port, pstate); |
aa3f6951 | 652 | display_frame (frame, i, nfield, indentation, sport, a->port, pstate); |
ab4f3efb | 653 | } |
bdf8afff | 654 | |
463b2219 ML |
655 | scm_remember_upto_here_1 (print_state); |
656 | ||
bdf8afff MD |
657 | return SCM_UNSPECIFIED; |
658 | } | |
1bbd0b84 | 659 | #undef FUNC_NAME |
bdf8afff | 660 | |
09c6d80a | 661 | SCM_DEFINE (scm_display_backtrace_with_highlights, "display-backtrace", 2, 3, 0, |
2b55be75 | 662 | (SCM stack, SCM port, SCM first, SCM depth, SCM highlights), |
b5944f66 | 663 | "Display a backtrace to the output port @var{port}. @var{stack}\n" |
c73bdd3a | 664 | "is the stack to take the backtrace from, @var{first} specifies\n" |
b5944f66 NJ |
665 | "where in the stack to start and @var{depth} how many frames\n" |
666 | "to display. @var{first} and @var{depth} can be @code{#f},\n" | |
c648c681 | 667 | "which means that default values will be used.\n" |
b5944f66 NJ |
668 | "If @var{highlights} is given it should be a list; the elements\n" |
669 | "of this list will be highlighted wherever they appear in the\n" | |
670 | "backtrace.") | |
2b55be75 | 671 | #define FUNC_NAME s_scm_display_backtrace_with_highlights |
bdf8afff | 672 | { |
0b2cb4ee MD |
673 | struct display_backtrace_args a; |
674 | struct display_error_handler_data data; | |
675 | a.stack = stack; | |
676 | a.port = port; | |
677 | a.first = first; | |
678 | a.depth = depth; | |
2b55be75 MV |
679 | if (SCM_UNBNDP (highlights)) |
680 | a.highlight_objects = SCM_EOL; | |
681 | else | |
682 | a.highlight_objects = highlights; | |
0b2cb4ee MD |
683 | data.mode = "backtrace"; |
684 | data.port = port; | |
bdf8afff | 685 | scm_internal_catch (SCM_BOOL_T, |
92c2555f MV |
686 | (scm_t_catch_body) display_backtrace_body, &a, |
687 | (scm_t_catch_handler) display_error_handler, &data); | |
ab4f3efb MD |
688 | return SCM_UNSPECIFIED; |
689 | } | |
1bbd0b84 | 690 | #undef FUNC_NAME |
ab4f3efb | 691 | |
2b55be75 MV |
692 | SCM |
693 | scm_display_backtrace (SCM stack, SCM port, SCM first, SCM depth) | |
694 | { | |
695 | return scm_display_backtrace_with_highlights (stack, port, first, depth, | |
696 | SCM_EOL); | |
697 | } | |
698 | ||
86d31dfe | 699 | SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?"); |
5aab5d96 | 700 | |
2b55be75 MV |
701 | SCM_DEFINE (scm_backtrace_with_highlights, "backtrace", 0, 1, 0, |
702 | (SCM highlights), | |
ec16eb78 AW |
703 | "Display a backtrace of the current stack to the current\n" |
704 | "output port. If @var{highlights} is given, it should be\n" | |
705 | "a list; the elements of this list will be highlighted\n" | |
706 | "wherever they appear in the backtrace.") | |
2b55be75 | 707 | #define FUNC_NAME s_scm_backtrace_with_highlights |
5aab5d96 | 708 | { |
9de87eea | 709 | SCM port = scm_current_output_port (); |
ec16eb78 AW |
710 | SCM stack = scm_make_stack (SCM_BOOL_T, SCM_EOL); |
711 | ||
2b55be75 MV |
712 | if (SCM_UNBNDP (highlights)) |
713 | highlights = SCM_EOL; | |
714 | ||
ec16eb78 AW |
715 | scm_newline (port); |
716 | scm_puts ("Backtrace:\n", port); | |
717 | scm_display_backtrace_with_highlights (stack, port, SCM_BOOL_F, SCM_BOOL_F, | |
718 | highlights); | |
719 | scm_newline (port); | |
720 | ||
5aab5d96 MD |
721 | return SCM_UNSPECIFIED; |
722 | } | |
1bbd0b84 | 723 | #undef FUNC_NAME |
5aab5d96 | 724 | |
2b55be75 MV |
725 | SCM |
726 | scm_backtrace (void) | |
727 | { | |
728 | return scm_backtrace_with_highlights (SCM_EOL); | |
729 | } | |
730 | ||
ab4f3efb MD |
731 | \f |
732 | ||
733 | void | |
734 | scm_init_backtrace () | |
735 | { | |
a0599745 | 736 | #include "libguile/backtrace.x" |
ab4f3efb | 737 | } |
89e00824 ML |
738 | |
739 | /* | |
740 | Local Variables: | |
741 | c-file-style: "gnu" | |
742 | End: | |
743 | */ |