Commit | Line | Data |
---|---|---|
ab4f3efb | 1 | /* Printing of backtraces and error messages |
f2c9fcb0 | 2 | * Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation |
ab4f3efb MD |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2, or (at your option) | |
7 | * any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
16 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
17 | * Boston, MA 02111-1307 USA | |
ab4f3efb MD |
18 | * |
19 | * As a special exception, the Free Software Foundation gives permission | |
20 | * for additional uses of the text contained in its release of GUILE. | |
21 | * | |
22 | * The exception is that, if you link the GUILE library with other files | |
23 | * to produce an executable, this does not by itself cause the | |
24 | * resulting executable to be covered by the GNU General Public License. | |
25 | * Your use of that executable is in no way restricted on account of | |
26 | * linking the GUILE library code into it. | |
27 | * | |
28 | * This exception does not however invalidate any other reasons why | |
29 | * the executable file might be covered by the GNU General Public License. | |
30 | * | |
31 | * This exception applies only to the code released by the | |
32 | * Free Software Foundation under the name GUILE. If you copy | |
33 | * code from other Free Software Foundation releases into a copy of | |
34 | * GUILE, as the General Public License permits, the exception does | |
35 | * not apply to the code that you add in this way. To avoid misleading | |
36 | * anyone as to the status of such modified files, you must delete | |
37 | * this exception notice from them. | |
38 | * | |
39 | * If you write modifications of your own for GUILE, it is your choice | |
40 | * whether to permit this exception to apply to your modifications. | |
41 | * If you do not wish that, delete this exception notice. | |
42 | * | |
43 | * The author can be reached at djurfeldt@nada.kth.se | |
82892bed | 44 | * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */ |
ab4f3efb | 45 | |
1bbd0b84 GB |
46 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
47 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
48 | ||
49 | ||
2b407f9b MD |
50 | #include <ctype.h> |
51 | ||
a0599745 | 52 | #include "libguile/_scm.h" |
2fb36297 | 53 | |
b1508ade MD |
54 | #ifdef HAVE_UNISTD_H |
55 | #include <unistd.h> | |
56 | #endif | |
57 | ||
a0599745 MD |
58 | #include "libguile/stacks.h" |
59 | #include "libguile/srcprop.h" | |
60 | #include "libguile/struct.h" | |
61 | #include "libguile/strports.h" | |
62 | #include "libguile/throw.h" | |
63 | #include "libguile/fluids.h" | |
64 | #include "libguile/ports.h" | |
65 | #include "libguile/strings.h" | |
ab4f3efb | 66 | |
a0599745 MD |
67 | #include "libguile/validate.h" |
68 | #include "libguile/backtrace.h" | |
ab4f3efb MD |
69 | |
70 | /* {Error reporting and backtraces} | |
71 | * (A first approximation.) | |
72 | * | |
73 | * Note that these functions shouldn't generate errors themselves. | |
74 | */ | |
75 | ||
76 | #ifndef SCM_RECKLESS | |
77 | #undef SCM_ASSERT | |
78 | #define SCM_ASSERT(_cond, _arg, _pos, _subr) \ | |
79 | if (!(_cond)) \ | |
80 | return SCM_BOOL_F; | |
81 | #endif | |
82 | ||
86d31dfe | 83 | SCM scm_the_last_stack_fluid_var; |
b6609fc7 | 84 | |
ab4f3efb | 85 | static void |
1bbd0b84 | 86 | display_header (SCM source, SCM port) |
ab4f3efb | 87 | { |
2f2b390c | 88 | if (SCM_MEMOIZEDP (source)) |
ab4f3efb | 89 | { |
2f2b390c DH |
90 | SCM fname = scm_source_property (source, scm_sym_filename); |
91 | SCM line = scm_source_property (source, scm_sym_line); | |
92 | SCM col = scm_source_property (source, scm_sym_column); | |
93 | ||
94 | /* Dirk:FIXME:: Maybe we should store the _port_ rather than the | |
95 | * filename with the source properties? Then we could in case of | |
96 | * non-file ports give at least some more details than just | |
97 | * "<unnamed port>". */ | |
98 | if (SCM_STRINGP (fname)) | |
99 | scm_prin1 (fname, port, 0); | |
100 | else | |
101 | scm_puts ("<unnamed port>", port); | |
102 | ||
103 | if (!SCM_FALSEP (line) && !SCM_FALSEP (col)) | |
104 | { | |
105 | scm_putc (':', port); | |
106 | scm_intprint (SCM_INUM (line) + 1, 10, port); | |
107 | scm_putc (':', port); | |
108 | scm_intprint (SCM_INUM (col) + 1, 10, port); | |
109 | } | |
ab4f3efb MD |
110 | } |
111 | else | |
b7f3516f TT |
112 | scm_puts ("ERROR", port); |
113 | scm_puts (": ", port); | |
ab4f3efb MD |
114 | } |
115 | ||
f3acc5c1 JB |
116 | |
117 | void | |
6e8d25a6 | 118 | scm_display_error_message (SCM message, SCM args, SCM port) |
ab4f3efb | 119 | { |
b24b5e13 | 120 | if (SCM_STRINGP (message) && !SCM_FALSEP (scm_list_p (args))) |
3fceef59 MD |
121 | { |
122 | scm_simple_format (port, message, args); | |
123 | scm_newline (port); | |
124 | } | |
125 | else | |
ab4f3efb | 126 | { |
b24b5e13 DH |
127 | scm_display (message, port); |
128 | scm_newline (port); | |
ab4f3efb | 129 | } |
ab4f3efb MD |
130 | } |
131 | ||
ab4f3efb | 132 | static void |
1bbd0b84 | 133 | display_expression (SCM frame,SCM pname,SCM source,SCM port) |
ab4f3efb MD |
134 | { |
135 | SCM print_state = scm_make_print_state (); | |
136 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
137 | pstate->writingp = 0; | |
138 | pstate->fancyp = 1; | |
139 | pstate->level = 2; | |
140 | pstate->length = 3; | |
b24b5e13 | 141 | if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname)) |
ab4f3efb | 142 | { |
cabe682c | 143 | if (SCM_FRAMEP (frame) |
ab4f3efb | 144 | && SCM_FRAME_EVAL_ARGS_P (frame)) |
b7f3516f | 145 | scm_puts ("While evaluating arguments to ", port); |
ab4f3efb | 146 | else |
b7f3516f | 147 | scm_puts ("In procedure ", port); |
ab4f3efb | 148 | scm_iprin1 (pname, port, pstate); |
0c95b57d | 149 | if (SCM_MEMOIZEDP (source)) |
ab4f3efb | 150 | { |
b7f3516f | 151 | scm_puts (" in expression ", port); |
ab4f3efb MD |
152 | pstate->writingp = 1; |
153 | scm_iprin1 (scm_unmemoize (source), port, pstate); | |
154 | } | |
155 | } | |
156 | else if (SCM_NIMP (source)) | |
157 | { | |
b7f3516f | 158 | scm_puts ("In expression ", port); |
ab4f3efb MD |
159 | pstate->writingp = 1; |
160 | scm_iprin1 (scm_unmemoize (source), port, pstate); | |
161 | } | |
b7f3516f | 162 | scm_puts (":\n", port); |
ab4f3efb MD |
163 | scm_free_print_state (print_state); |
164 | } | |
165 | ||
bdf8afff MD |
166 | struct display_error_args { |
167 | SCM stack; | |
168 | SCM port; | |
169 | SCM subr; | |
170 | SCM message; | |
171 | SCM args; | |
172 | SCM rest; | |
173 | }; | |
174 | ||
175 | static SCM | |
39752bec | 176 | display_error_body (struct display_error_args *a) |
ab4f3efb MD |
177 | { |
178 | SCM current_frame = SCM_BOOL_F; | |
179 | SCM source = SCM_BOOL_F; | |
a88a4c8a | 180 | SCM prev_frame = SCM_BOOL_F; |
b24b5e13 | 181 | SCM pname = a->subr; |
a88a4c8a | 182 | |
841076ac | 183 | if (SCM_DEBUGGINGP |
bdf8afff MD |
184 | && SCM_STACKP (a->stack) |
185 | && SCM_STACK_LENGTH (a->stack) > 0) | |
ab4f3efb | 186 | { |
bdf8afff | 187 | current_frame = scm_stack_ref (a->stack, SCM_INUM0); |
ab4f3efb | 188 | source = SCM_FRAME_SOURCE (current_frame); |
a88a4c8a | 189 | prev_frame = SCM_FRAME_PREV (current_frame); |
cffcab30 | 190 | if (!SCM_MEMOIZEDP (source) && !SCM_FALSEP (prev_frame)) |
a88a4c8a | 191 | source = SCM_FRAME_SOURCE (prev_frame); |
b24b5e13 | 192 | if (!SCM_SYMBOLP (pname) && !SCM_STRINGP (pname) && SCM_FRAME_PROC_P (current_frame) |
9a09deb1 | 193 | && SCM_EQ_P (scm_procedure_p (SCM_FRAME_PROC (current_frame)), SCM_BOOL_T)) |
ab4f3efb MD |
194 | pname = scm_procedure_name (SCM_FRAME_PROC (current_frame)); |
195 | } | |
b24b5e13 | 196 | if (SCM_SYMBOLP (pname) || SCM_STRINGP (pname) || SCM_MEMOIZEDP (source)) |
ab4f3efb | 197 | { |
bdf8afff MD |
198 | display_header (source, a->port); |
199 | display_expression (current_frame, pname, source, a->port); | |
ab4f3efb | 200 | } |
bdf8afff MD |
201 | display_header (source, a->port); |
202 | scm_display_error_message (a->message, a->args, a->port); | |
203 | return SCM_UNSPECIFIED; | |
204 | } | |
205 | ||
206 | struct display_error_handler_data { | |
207 | char *mode; | |
208 | SCM port; | |
209 | }; | |
210 | ||
211 | /* This is the exception handler for error reporting routines. | |
212 | Note that it is very important that this handler *doesn't* try to | |
213 | print more than the error tag, since the error very probably is | |
214 | caused by an erroneous print call-back routine. If we would | |
2fdcf8bd | 215 | try to print all objects, we would enter an infinite loop. */ |
bdf8afff MD |
216 | static SCM |
217 | display_error_handler (struct display_error_handler_data *data, | |
218 | SCM tag, SCM args) | |
219 | { | |
220 | SCM print_state = scm_make_print_state (); | |
b7f3516f TT |
221 | scm_puts ("\nException during displaying of ", data->port); |
222 | scm_puts (data->mode, data->port); | |
223 | scm_puts (": ", data->port); | |
bdf8afff | 224 | scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state)); |
b7f3516f | 225 | scm_putc ('\n', data->port); |
bdf8afff MD |
226 | return SCM_UNSPECIFIED; |
227 | } | |
228 | ||
e40a4095 DH |
229 | |
230 | /* The function scm_i_display_error prints out a detailed error message. This | |
231 | * function will be called directly within libguile to signal error messages. | |
232 | * No parameter checks will be performed by scm_i_display_error. Thus, User | |
233 | * code should rather use the function scm_display_error. | |
234 | */ | |
235 | void | |
236 | scm_i_display_error (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest) | |
bdf8afff | 237 | { |
0b2cb4ee MD |
238 | struct display_error_args a; |
239 | struct display_error_handler_data data; | |
240 | a.stack = stack; | |
241 | a.port = port; | |
242 | a.subr = subr; | |
243 | a.message = message; | |
244 | a.args = args; | |
245 | a.rest = rest; | |
246 | data.mode = "error"; | |
247 | data.port = port; | |
bdf8afff MD |
248 | scm_internal_catch (SCM_BOOL_T, |
249 | (scm_catch_body_t) display_error_body, &a, | |
250 | (scm_catch_handler_t) display_error_handler, &data); | |
e40a4095 DH |
251 | } |
252 | ||
253 | ||
254 | SCM_DEFINE (scm_display_error, "display-error", 6, 0, 0, | |
255 | (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest), | |
c73bdd3a MG |
256 | "Display an error message to the output port @var{port}.\n" |
257 | "@var{stack} is the saved stack for the error, @var{subr} is\n" | |
258 | "the name of the procedure in which the error occured and\n" | |
259 | "@var{message} is the actual error message, which may contain\n" | |
260 | "formatting instructions. These will format the arguments in\n" | |
261 | "the list @var{args} accordingly. @var{rest} is currently\n" | |
262 | "ignored.") | |
e40a4095 DH |
263 | #define FUNC_NAME s_scm_display_error |
264 | { | |
265 | SCM_VALIDATE_OUTPUT_PORT (2, port); | |
266 | ||
267 | scm_i_display_error (stack, port, subr, message, args, rest); | |
268 | ||
ab4f3efb MD |
269 | return SCM_UNSPECIFIED; |
270 | } | |
1bbd0b84 | 271 | #undef FUNC_NAME |
ab4f3efb | 272 | |
e40a4095 | 273 | |
2b407f9b MD |
274 | typedef struct { |
275 | int level; | |
276 | int length; | |
277 | } print_params_t; | |
278 | ||
279 | static int n_print_params = 9; | |
280 | static print_params_t default_print_params[] = { | |
281 | { 4, 9 }, { 4, 3 }, | |
282 | { 3, 4 }, { 3, 3 }, | |
283 | { 2, 4 }, { 2, 3 }, | |
284 | { 1, 4 }, { 1, 3 }, { 1, 2 } | |
285 | }; | |
286 | static print_params_t *print_params = default_print_params; | |
287 | ||
288 | #ifdef GUILE_DEBUG | |
3b3b36dd | 289 | SCM_DEFINE (scm_set_print_params_x, "set-print-params!", 1, 0, 0, |
5623a9b4 | 290 | (SCM params), |
c73bdd3a MG |
291 | "Set the print parameters to the values from @var{params}.\n" |
292 | "@var{params} must be a list of two-element lists which must\n" | |
293 | "hold two integer values.") | |
e8e9b690 | 294 | #define FUNC_NAME s_scm_set_print_params_x |
2b407f9b | 295 | { |
3fceef59 MD |
296 | int i; |
297 | int n; | |
2b407f9b MD |
298 | SCM ls; |
299 | print_params_t *new_params; | |
3fceef59 MD |
300 | |
301 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, params, n); | |
302 | for (ls = params; SCM_NNULLP (ls); ls = SCM_CDR (ls)) | |
2b407f9b MD |
303 | SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2 |
304 | && SCM_INUMP (SCM_CAAR (ls)) | |
305 | && SCM_INUM (SCM_CAAR (ls)) >= 0 | |
306 | && SCM_INUMP (SCM_CADAR (ls)) | |
307 | && SCM_INUM (SCM_CADAR (ls)) >= 0, | |
308 | params, | |
309 | SCM_ARG2, | |
e8e9b690 | 310 | s_scm_set_print_params_x); |
2b407f9b | 311 | new_params = scm_must_malloc (n * sizeof (print_params_t), |
1bbd0b84 | 312 | FUNC_NAME); |
2b407f9b MD |
313 | if (print_params != default_print_params) |
314 | scm_must_free (print_params); | |
315 | print_params = new_params; | |
316 | for (i = 0; i < n; ++i) | |
317 | { | |
318 | print_params[i].level = SCM_INUM (SCM_CAAR (params)); | |
319 | print_params[i].length = SCM_INUM (SCM_CADAR (params)); | |
320 | params = SCM_CDR (params); | |
321 | } | |
322 | n_print_params = n; | |
323 | return SCM_UNSPECIFIED; | |
324 | } | |
1bbd0b84 | 325 | #undef FUNC_NAME |
2b407f9b MD |
326 | #endif |
327 | ||
ab4f3efb | 328 | static void |
1bbd0b84 | 329 | indent (int n, SCM port) |
ab4f3efb MD |
330 | { |
331 | int i; | |
332 | for (i = 0; i < n; ++i) | |
b7f3516f | 333 | scm_putc (' ', port); |
ab4f3efb MD |
334 | } |
335 | ||
ab4f3efb | 336 | static void |
1bbd0b84 | 337 | display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
ab4f3efb | 338 | { |
2b407f9b MD |
339 | SCM string; |
340 | int i = 0, n; | |
341 | scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport); | |
342 | do | |
ab4f3efb | 343 | { |
2b407f9b MD |
344 | pstate->length = print_params[i].length; |
345 | ptob->seek (sport, 0, SEEK_SET); | |
0c95b57d | 346 | if (SCM_CONSP (exp)) |
2b407f9b MD |
347 | { |
348 | pstate->level = print_params[i].level - 1; | |
349 | scm_iprlist (hdr, exp, tlr[0], sport, pstate); | |
350 | scm_puts (&tlr[1], sport); | |
351 | } | |
352 | else | |
353 | { | |
354 | pstate->level = print_params[i].level; | |
355 | scm_iprin1 (exp, sport, pstate); | |
356 | } | |
357 | ptob->flush (sport); | |
358 | n = ptob->seek (sport, 0, SEEK_CUR); | |
359 | ++i; | |
ab4f3efb | 360 | } |
2b407f9b MD |
361 | while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params); |
362 | ptob->truncate (sport, n); | |
363 | string = scm_strport_to_string (sport); | |
364 | /* Remove control characters */ | |
365 | for (i = 0; i < n; ++i) | |
86c991c2 DH |
366 | if (iscntrl (SCM_STRING_CHARS (string)[i])) |
367 | SCM_STRING_CHARS (string)[i] = ' '; | |
2b407f9b MD |
368 | /* Truncate */ |
369 | if (indentation + n > SCM_BACKTRACE_WIDTH) | |
370 | { | |
371 | n = SCM_BACKTRACE_WIDTH - indentation; | |
86c991c2 | 372 | SCM_STRING_CHARS (string)[n - 1] = '$'; |
2b407f9b MD |
373 | } |
374 | ||
86c991c2 | 375 | scm_lfwrite (SCM_STRING_CHARS (string), n, port); |
ab4f3efb MD |
376 | } |
377 | ||
e3c37929 | 378 | static void |
1bbd0b84 | 379 | display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
e3c37929 MD |
380 | { |
381 | SCM proc = SCM_FRAME_PROC (frame); | |
382 | SCM name = (SCM_NFALSEP (scm_procedure_p (proc)) | |
383 | ? scm_procedure_name (proc) | |
384 | : SCM_BOOL_F); | |
385 | display_frame_expr ("[", | |
386 | scm_cons (SCM_NFALSEP (name) ? name : proc, | |
387 | SCM_FRAME_ARGS (frame)), | |
388 | SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]", | |
389 | indentation, | |
390 | sport, | |
391 | port, | |
392 | pstate); | |
393 | } | |
394 | ||
3b3b36dd | 395 | SCM_DEFINE (scm_display_application, "display-application", 1, 2, 0, |
1bbd0b84 | 396 | (SCM frame, SCM port, SCM indent), |
c73bdd3a MG |
397 | "Display a procedure application @var{frame} to the output port\n" |
398 | "@var{port}. @var{indent} specifies the indentation of the\n" | |
399 | "output.") | |
1bbd0b84 | 400 | #define FUNC_NAME s_scm_display_application |
e3c37929 | 401 | { |
3b3b36dd | 402 | SCM_VALIDATE_FRAME (1,frame); |
e3c37929 MD |
403 | if (SCM_UNBNDP (port)) |
404 | port = scm_cur_outp; | |
2b407f9b | 405 | else |
3b3b36dd | 406 | SCM_VALIDATE_OPOUTPORT (2,port); |
2b407f9b MD |
407 | if (SCM_UNBNDP (indent)) |
408 | indent = SCM_INUM0; | |
409 | else | |
3b3b36dd | 410 | SCM_VALIDATE_INUM (3,indent); |
2b407f9b | 411 | |
e3c37929 MD |
412 | if (SCM_FRAME_PROC_P (frame)) |
413 | /* Display an application. */ | |
414 | { | |
2b407f9b | 415 | SCM sport, print_state; |
e3c37929 MD |
416 | scm_print_state *pstate; |
417 | ||
2b407f9b MD |
418 | /* Create a string port used for adaptation of printing parameters. */ |
419 | sport = scm_mkstrport (SCM_INUM0, | |
420 | scm_make_string (SCM_MAKINUM (240), | |
421 | SCM_UNDEFINED), | |
422 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 423 | FUNC_NAME); |
2b407f9b | 424 | |
e3c37929 MD |
425 | /* Create a print state for printing of frames. */ |
426 | print_state = scm_make_print_state (); | |
427 | pstate = SCM_PRINT_STATE (print_state); | |
428 | pstate->writingp = 1; | |
429 | pstate->fancyp = 1; | |
e3c37929 | 430 | |
2b407f9b | 431 | display_application (frame, SCM_INUM (indent), sport, port, pstate); |
e3c37929 MD |
432 | return SCM_BOOL_T; |
433 | } | |
434 | else | |
435 | return SCM_BOOL_F; | |
436 | } | |
1bbd0b84 | 437 | #undef FUNC_NAME |
e3c37929 | 438 | |
ab4f3efb | 439 | static void |
1bbd0b84 | 440 | display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
ab4f3efb MD |
441 | { |
442 | int n, i, j; | |
443 | ||
444 | /* Announce missing frames? */ | |
445 | if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame)) | |
446 | { | |
447 | indent (nfield + 1 + indentation, port); | |
b7f3516f | 448 | scm_puts ("...\n", port); |
ab4f3efb MD |
449 | } |
450 | ||
451 | /* Check size of frame number. */ | |
452 | n = SCM_FRAME_NUMBER (frame); | |
453 | for (i = 0, j = n; j > 0; ++i) j /= 10; | |
454 | ||
455 | /* Number indentation. */ | |
456 | indent (nfield - (i ? i : 1), port); | |
457 | ||
458 | /* Frame number. */ | |
459 | scm_iprin1 (SCM_MAKINUM (n), port, pstate); | |
460 | ||
461 | /* Real frame marker */ | |
b7f3516f | 462 | scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port); |
ab4f3efb MD |
463 | |
464 | /* Indentation. */ | |
465 | indent (indentation, port); | |
466 | ||
467 | if (SCM_FRAME_PROC_P (frame)) | |
468 | /* Display an application. */ | |
e3c37929 | 469 | display_application (frame, nfield + 1 + indentation, sport, port, pstate); |
ab4f3efb MD |
470 | else |
471 | /* Display a special form. */ | |
472 | { | |
473 | SCM source = SCM_FRAME_SOURCE (frame); | |
0c95b57d | 474 | SCM copy = (SCM_CONSP (source) |
7f2d92b1 | 475 | ? scm_source_property (source, scm_sym_copy) |
2e9d6c6d | 476 | : SCM_BOOL_F); |
0c95b57d | 477 | SCM umcopy = (SCM_MEMOIZEDP (source) |
2e9d6c6d MD |
478 | ? scm_unmemoize (source) |
479 | : SCM_BOOL_F); | |
ab4f3efb | 480 | display_frame_expr ("(", |
0c95b57d | 481 | SCM_CONSP (copy) ? copy : umcopy, |
ab4f3efb MD |
482 | ")", |
483 | nfield + 1 + indentation, | |
484 | sport, | |
485 | port, | |
486 | pstate); | |
487 | } | |
1ae6af28 | 488 | scm_putc ('\n', port); |
ab4f3efb MD |
489 | |
490 | /* Announce missing frames? */ | |
491 | if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame)) | |
492 | { | |
493 | indent (nfield + 1 + indentation, port); | |
b7f3516f | 494 | scm_puts ("...\n", port); |
ab4f3efb MD |
495 | } |
496 | } | |
497 | ||
bdf8afff MD |
498 | struct display_backtrace_args { |
499 | SCM stack; | |
500 | SCM port; | |
501 | SCM first; | |
502 | SCM depth; | |
503 | }; | |
504 | ||
bdf8afff | 505 | static SCM |
1bbd0b84 GB |
506 | display_backtrace_body(struct display_backtrace_args *a) |
507 | #define FUNC_NAME "display_backtrace_body" | |
ab4f3efb MD |
508 | { |
509 | int n_frames, beg, end, n, i, j; | |
510 | int nfield, indent_p, indentation; | |
511 | SCM frame, sport, print_state; | |
512 | scm_print_state *pstate; | |
513 | ||
78446828 MV |
514 | a->port = SCM_COERCE_OUTPORT (a->port); |
515 | ||
ab4f3efb | 516 | /* Argument checking and extraction. */ |
0c95b57d | 517 | SCM_ASSERT (SCM_STACKP (a->stack), |
bdf8afff | 518 | a->stack, |
ab4f3efb MD |
519 | SCM_ARG1, |
520 | s_display_backtrace); | |
0c95b57d | 521 | SCM_ASSERT (SCM_OPOUTPORTP (a->port), |
bdf8afff | 522 | a->port, |
ab4f3efb MD |
523 | SCM_ARG2, |
524 | s_display_backtrace); | |
bdf8afff MD |
525 | n_frames = SCM_INUM (scm_stack_length (a->stack)); |
526 | n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH; | |
ab4f3efb MD |
527 | if (SCM_BACKWARDS_P) |
528 | { | |
bdf8afff | 529 | beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0; |
ab4f3efb MD |
530 | end = beg + n - 1; |
531 | if (end >= n_frames) | |
532 | end = n_frames - 1; | |
533 | n = end - beg + 1; | |
534 | } | |
535 | else | |
536 | { | |
bdf8afff | 537 | if (SCM_INUMP (a->first)) |
ab4f3efb | 538 | { |
bdf8afff | 539 | beg = SCM_INUM (a->first); |
ab4f3efb MD |
540 | end = beg - n + 1; |
541 | if (end < 0) | |
542 | end = 0; | |
543 | } | |
544 | else | |
545 | { | |
546 | beg = n - 1; | |
547 | end = 0; | |
548 | if (beg >= n_frames) | |
549 | beg = n_frames - 1; | |
550 | } | |
551 | n = beg - end + 1; | |
552 | } | |
bdf8afff MD |
553 | SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace); |
554 | SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace); | |
ab4f3efb MD |
555 | |
556 | /* Create a string port used for adaptation of printing parameters. */ | |
557 | sport = scm_mkstrport (SCM_INUM0, | |
558 | scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED), | |
559 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 560 | FUNC_NAME); |
ab4f3efb MD |
561 | |
562 | /* Create a print state for printing of frames. */ | |
563 | print_state = scm_make_print_state (); | |
564 | pstate = SCM_PRINT_STATE (print_state); | |
565 | pstate->writingp = 1; | |
566 | pstate->fancyp = 1; | |
567 | ||
568 | /* First find out if it's reasonable to do indentation. */ | |
569 | if (SCM_BACKWARDS_P) | |
570 | indent_p = 0; | |
571 | else | |
572 | { | |
573 | indent_p = 1; | |
bdf8afff | 574 | frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg)); |
ab4f3efb MD |
575 | for (i = 0, j = 0; i < n; ++i) |
576 | { | |
577 | if (SCM_FRAME_REAL_P (frame)) | |
578 | ++j; | |
579 | if (j > SCM_BACKTRACE_INDENT) | |
580 | { | |
581 | indent_p = 0; | |
582 | break; | |
583 | } | |
584 | frame = (SCM_BACKWARDS_P | |
585 | ? SCM_FRAME_PREV (frame) | |
586 | : SCM_FRAME_NEXT (frame)); | |
587 | } | |
588 | } | |
589 | ||
590 | /* Determine size of frame number field. */ | |
bdf8afff | 591 | j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end))); |
ab4f3efb MD |
592 | for (i = 0; j > 0; ++i) j /= 10; |
593 | nfield = i ? i : 1; | |
594 | ||
ab4f3efb | 595 | /* Print frames. */ |
bdf8afff | 596 | frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg)); |
ab4f3efb | 597 | indentation = 1; |
bdf8afff | 598 | display_frame (frame, nfield, indentation, sport, a->port, pstate); |
ab4f3efb MD |
599 | for (i = 1; i < n; ++i) |
600 | { | |
601 | if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame)) | |
602 | ++indentation; | |
603 | frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame); | |
bdf8afff | 604 | display_frame (frame, nfield, indentation, sport, a->port, pstate); |
ab4f3efb | 605 | } |
bdf8afff | 606 | |
463b2219 ML |
607 | scm_remember_upto_here_1 (print_state); |
608 | ||
bdf8afff MD |
609 | return SCM_UNSPECIFIED; |
610 | } | |
1bbd0b84 | 611 | #undef FUNC_NAME |
bdf8afff | 612 | |
3b3b36dd | 613 | SCM_DEFINE (scm_display_backtrace, "display-backtrace", 2, 2, 0, |
c73bdd3a MG |
614 | (SCM stack, SCM port, SCM first, SCM depth), |
615 | "Display a backtrace to the output port @var{port}. @var{stack}\n" | |
616 | "is the stack to take the backtrace from, @var{first} specifies\n" | |
617 | "where in the stack to start and @var{depth} how much frames\n" | |
618 | "to display. Both @var{first} and @var{depth} can be @code{#f},\n" | |
619 | "which means that default values will be used.") | |
1bbd0b84 | 620 | #define FUNC_NAME s_scm_display_backtrace |
bdf8afff | 621 | { |
0b2cb4ee MD |
622 | struct display_backtrace_args a; |
623 | struct display_error_handler_data data; | |
624 | a.stack = stack; | |
625 | a.port = port; | |
626 | a.first = first; | |
627 | a.depth = depth; | |
628 | data.mode = "backtrace"; | |
629 | data.port = port; | |
bdf8afff MD |
630 | scm_internal_catch (SCM_BOOL_T, |
631 | (scm_catch_body_t) display_backtrace_body, &a, | |
632 | (scm_catch_handler_t) display_error_handler, &data); | |
ab4f3efb MD |
633 | return SCM_UNSPECIFIED; |
634 | } | |
1bbd0b84 | 635 | #undef FUNC_NAME |
ab4f3efb | 636 | |
86d31dfe | 637 | SCM_VARIABLE (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?"); |
5aab5d96 | 638 | |
3b3b36dd | 639 | SCM_DEFINE (scm_backtrace, "backtrace", 0, 0, 0, |
c73bdd3a MG |
640 | (), |
641 | "Display a backtrace of the stack saved by the last error\n" | |
642 | "to the current output port.") | |
1bbd0b84 | 643 | #define FUNC_NAME s_scm_backtrace |
5aab5d96 | 644 | { |
86d31dfe MV |
645 | SCM the_last_stack = |
646 | scm_fluid_ref (SCM_VARIABLE_REF (scm_the_last_stack_fluid_var)); | |
a5d6d578 | 647 | if (SCM_NFALSEP (the_last_stack)) |
5aab5d96 MD |
648 | { |
649 | scm_newline (scm_cur_outp); | |
3a1f8447 | 650 | scm_puts ("Backtrace:\n", scm_cur_outp); |
a5d6d578 | 651 | scm_display_backtrace (the_last_stack, |
5aab5d96 MD |
652 | scm_cur_outp, |
653 | SCM_UNDEFINED, | |
654 | SCM_UNDEFINED); | |
655 | scm_newline (scm_cur_outp); | |
86d31dfe | 656 | if (SCM_FALSEP (SCM_VARIABLE_REF (scm_has_shown_backtrace_hint_p_var)) |
5aab5d96 MD |
657 | && !SCM_BACKTRACE_P) |
658 | { | |
b7f3516f TT |
659 | scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like " |
660 | "a backtrace\n" | |
661 | "automatically if an error occurs in the future.\n", | |
662 | scm_cur_outp); | |
86d31dfe | 663 | SCM_VARIABLE_SET (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T); |
5aab5d96 MD |
664 | } |
665 | } | |
666 | else | |
667 | { | |
b7f3516f | 668 | scm_puts ("No backtrace available.\n", scm_cur_outp); |
5aab5d96 MD |
669 | } |
670 | return SCM_UNSPECIFIED; | |
671 | } | |
1bbd0b84 | 672 | #undef FUNC_NAME |
5aab5d96 | 673 | |
ab4f3efb MD |
674 | \f |
675 | ||
676 | void | |
677 | scm_init_backtrace () | |
678 | { | |
a5d6d578 | 679 | SCM f = scm_make_fluid (); |
86d31dfe | 680 | scm_the_last_stack_fluid_var = scm_c_define ("the-last-stack", f); |
5aab5d96 | 681 | |
8dc9439f | 682 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 683 | #include "libguile/backtrace.x" |
8dc9439f | 684 | #endif |
ab4f3efb | 685 | } |
89e00824 ML |
686 | |
687 | /* | |
688 | Local Variables: | |
689 | c-file-style: "gnu" | |
690 | End: | |
691 | */ |