Commit | Line | Data |
---|---|---|
ab4f3efb | 1 | /* Printing of backtraces and error messages |
78a0461a | 2 | * Copyright (C) 1996, 1997, 1998, 1999 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 | ||
ab4f3efb | 50 | #include <stdio.h> |
2b407f9b MD |
51 | #include <ctype.h> |
52 | ||
2fb36297 JB |
53 | #include "_scm.h" |
54 | ||
b1508ade MD |
55 | #ifdef HAVE_UNISTD_H |
56 | #include <unistd.h> | |
57 | #endif | |
58 | ||
ab4f3efb MD |
59 | #include "stacks.h" |
60 | #include "srcprop.h" | |
61 | #include "genio.h" | |
62 | #include "struct.h" | |
63 | #include "strports.h" | |
bdf8afff | 64 | #include "throw.h" |
a5d6d578 | 65 | #include "fluids.h" |
ab4f3efb MD |
66 | |
67 | #include "backtrace.h" | |
68 | ||
69 | /* {Error reporting and backtraces} | |
70 | * (A first approximation.) | |
71 | * | |
72 | * Note that these functions shouldn't generate errors themselves. | |
73 | */ | |
74 | ||
75 | #ifndef SCM_RECKLESS | |
76 | #undef SCM_ASSERT | |
77 | #define SCM_ASSERT(_cond, _arg, _pos, _subr) \ | |
78 | if (!(_cond)) \ | |
79 | return SCM_BOOL_F; | |
80 | #endif | |
81 | ||
b6609fc7 MD |
82 | SCM scm_the_last_stack_fluid; |
83 | ||
ab4f3efb | 84 | static void |
1bbd0b84 | 85 | display_header (SCM source, SCM port) |
ab4f3efb MD |
86 | { |
87 | SCM fname = (SCM_NIMP (source) && SCM_MEMOIZEDP (source) | |
7f2d92b1 | 88 | ? scm_source_property (source, scm_sym_filename) |
ab4f3efb MD |
89 | : SCM_BOOL_F); |
90 | if (SCM_NIMP (fname) && SCM_STRINGP (fname)) | |
91 | { | |
92 | scm_prin1 (fname, port, 0); | |
b7f3516f | 93 | scm_putc (':', port); |
7f2d92b1 | 94 | scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_line)) + 1, |
0e929db3 MD |
95 | 10, |
96 | port); | |
b7f3516f | 97 | scm_putc (':', port); |
7f2d92b1 | 98 | scm_intprint (SCM_INUM (scm_source_property (source, scm_sym_column)) + 1, |
0e929db3 MD |
99 | 10, |
100 | port); | |
ab4f3efb MD |
101 | } |
102 | else | |
b7f3516f TT |
103 | scm_puts ("ERROR", port); |
104 | scm_puts (": ", port); | |
ab4f3efb MD |
105 | } |
106 | ||
f3acc5c1 JB |
107 | |
108 | void | |
6e8d25a6 | 109 | scm_display_error_message (SCM message, SCM args, SCM port) |
ab4f3efb MD |
110 | { |
111 | int writingp; | |
112 | char *start; | |
113 | char *p; | |
114 | ||
89958ad0 | 115 | if (SCM_IMP (message) || !SCM_ROSTRINGP (message) || SCM_IMP (args) |
c37e0e55 | 116 | || !scm_list_p (args)) |
ab4f3efb MD |
117 | { |
118 | scm_prin1 (message, port, 0); | |
b7f3516f | 119 | scm_putc ('\n', port); |
ab4f3efb MD |
120 | return; |
121 | } | |
122 | ||
89958ad0 JB |
123 | SCM_COERCE_SUBSTR (message); |
124 | start = SCM_ROCHARS (message); | |
ab4f3efb MD |
125 | for (p = start; *p != '\0'; ++p) |
126 | if (*p == '%') | |
127 | { | |
128 | if (SCM_IMP (args) || SCM_NCONSP (args)) | |
129 | continue; | |
130 | ||
131 | ++p; | |
132 | if (*p == 's') | |
133 | writingp = 0; | |
134 | else if (*p == 'S') | |
135 | writingp = 1; | |
136 | else | |
137 | continue; | |
138 | ||
b7f3516f | 139 | scm_lfwrite (start, p - start - 1, port); |
ab4f3efb MD |
140 | scm_prin1 (SCM_CAR (args), port, writingp); |
141 | args = SCM_CDR (args); | |
142 | start = p + 1; | |
143 | } | |
b7f3516f TT |
144 | scm_lfwrite (start, p - start, port); |
145 | scm_putc ('\n', port); | |
ab4f3efb MD |
146 | } |
147 | ||
ab4f3efb | 148 | static void |
1bbd0b84 | 149 | display_expression (SCM frame,SCM pname,SCM source,SCM port) |
ab4f3efb MD |
150 | { |
151 | SCM print_state = scm_make_print_state (); | |
152 | scm_print_state *pstate = SCM_PRINT_STATE (print_state); | |
153 | pstate->writingp = 0; | |
154 | pstate->fancyp = 1; | |
155 | pstate->level = 2; | |
156 | pstate->length = 3; | |
157 | if (SCM_NIMP (pname) && SCM_ROSTRINGP (pname)) | |
158 | { | |
159 | if (SCM_NIMP (frame) | |
160 | && SCM_FRAMEP (frame) | |
161 | && SCM_FRAME_EVAL_ARGS_P (frame)) | |
b7f3516f | 162 | scm_puts ("While evaluating arguments to ", port); |
ab4f3efb | 163 | else |
b7f3516f | 164 | scm_puts ("In procedure ", port); |
ab4f3efb MD |
165 | scm_iprin1 (pname, port, pstate); |
166 | if (SCM_NIMP (source) && SCM_MEMOIZEDP (source)) | |
167 | { | |
b7f3516f | 168 | scm_puts (" in expression ", port); |
ab4f3efb MD |
169 | pstate->writingp = 1; |
170 | scm_iprin1 (scm_unmemoize (source), port, pstate); | |
171 | } | |
172 | } | |
173 | else if (SCM_NIMP (source)) | |
174 | { | |
b7f3516f | 175 | scm_puts ("In expression ", port); |
ab4f3efb MD |
176 | pstate->writingp = 1; |
177 | scm_iprin1 (scm_unmemoize (source), port, pstate); | |
178 | } | |
b7f3516f | 179 | scm_puts (":\n", port); |
ab4f3efb MD |
180 | scm_free_print_state (print_state); |
181 | } | |
182 | ||
bdf8afff MD |
183 | struct display_error_args { |
184 | SCM stack; | |
185 | SCM port; | |
186 | SCM subr; | |
187 | SCM message; | |
188 | SCM args; | |
189 | SCM rest; | |
190 | }; | |
191 | ||
192 | static SCM | |
39752bec | 193 | display_error_body (struct display_error_args *a) |
ab4f3efb MD |
194 | { |
195 | SCM current_frame = SCM_BOOL_F; | |
196 | SCM source = SCM_BOOL_F; | |
197 | SCM pname = SCM_BOOL_F; | |
a88a4c8a JB |
198 | SCM prev_frame = SCM_BOOL_F; |
199 | ||
841076ac | 200 | if (SCM_DEBUGGINGP |
bdf8afff MD |
201 | && SCM_NIMP (a->stack) |
202 | && SCM_STACKP (a->stack) | |
203 | && SCM_STACK_LENGTH (a->stack) > 0) | |
ab4f3efb | 204 | { |
bdf8afff | 205 | current_frame = scm_stack_ref (a->stack, SCM_INUM0); |
ab4f3efb | 206 | source = SCM_FRAME_SOURCE (current_frame); |
a88a4c8a JB |
207 | prev_frame = SCM_FRAME_PREV (current_frame); |
208 | if (!(SCM_NIMP (source) && SCM_MEMOIZEDP (source)) | |
209 | && prev_frame != SCM_BOOL_F) | |
210 | source = SCM_FRAME_SOURCE (prev_frame); | |
ab4f3efb | 211 | if (SCM_FRAME_PROC_P (current_frame) |
016e2ce1 | 212 | && scm_procedure_p (SCM_FRAME_PROC (current_frame)) == SCM_BOOL_T) |
ab4f3efb MD |
213 | pname = scm_procedure_name (SCM_FRAME_PROC (current_frame)); |
214 | } | |
215 | if (!(SCM_NIMP (pname) && SCM_ROSTRINGP (pname))) | |
bdf8afff | 216 | pname = a->subr; |
e40a5fc8 | 217 | if ((SCM_NIMP (pname) && SCM_ROSTRINGP (pname)) |
26e19854 | 218 | || (SCM_NIMP (source) && SCM_MEMOIZEDP (source))) |
ab4f3efb | 219 | { |
bdf8afff MD |
220 | display_header (source, a->port); |
221 | display_expression (current_frame, pname, source, a->port); | |
ab4f3efb | 222 | } |
bdf8afff MD |
223 | display_header (source, a->port); |
224 | scm_display_error_message (a->message, a->args, a->port); | |
225 | return SCM_UNSPECIFIED; | |
226 | } | |
227 | ||
228 | struct display_error_handler_data { | |
229 | char *mode; | |
230 | SCM port; | |
231 | }; | |
232 | ||
233 | /* This is the exception handler for error reporting routines. | |
234 | Note that it is very important that this handler *doesn't* try to | |
235 | print more than the error tag, since the error very probably is | |
236 | caused by an erroneous print call-back routine. If we would | |
237 | tru to print all objects, we would enter an infinite loop. */ | |
238 | static SCM | |
239 | display_error_handler (struct display_error_handler_data *data, | |
240 | SCM tag, SCM args) | |
241 | { | |
242 | SCM print_state = scm_make_print_state (); | |
b7f3516f TT |
243 | scm_puts ("\nException during displaying of ", data->port); |
244 | scm_puts (data->mode, data->port); | |
245 | scm_puts (": ", data->port); | |
bdf8afff | 246 | scm_iprin1 (tag, data->port, SCM_PRINT_STATE (print_state)); |
b7f3516f | 247 | scm_putc ('\n', data->port); |
bdf8afff MD |
248 | return SCM_UNSPECIFIED; |
249 | } | |
250 | ||
1bbd0b84 GB |
251 | GUILE_PROC(scm_display_error, "display-error", 6, 0, 0, |
252 | (SCM stack, SCM port, SCM subr, SCM message, SCM args, SCM rest), | |
253 | "") | |
254 | #define FUNC_NAME s_scm_display_error | |
bdf8afff | 255 | { |
0b2cb4ee MD |
256 | struct display_error_args a; |
257 | struct display_error_handler_data data; | |
258 | a.stack = stack; | |
259 | a.port = port; | |
260 | a.subr = subr; | |
261 | a.message = message; | |
262 | a.args = args; | |
263 | a.rest = rest; | |
264 | data.mode = "error"; | |
265 | data.port = port; | |
bdf8afff MD |
266 | scm_internal_catch (SCM_BOOL_T, |
267 | (scm_catch_body_t) display_error_body, &a, | |
268 | (scm_catch_handler_t) display_error_handler, &data); | |
ab4f3efb MD |
269 | return SCM_UNSPECIFIED; |
270 | } | |
1bbd0b84 | 271 | #undef FUNC_NAME |
ab4f3efb | 272 | |
2b407f9b MD |
273 | typedef struct { |
274 | int level; | |
275 | int length; | |
276 | } print_params_t; | |
277 | ||
278 | static int n_print_params = 9; | |
279 | static print_params_t default_print_params[] = { | |
280 | { 4, 9 }, { 4, 3 }, | |
281 | { 3, 4 }, { 3, 3 }, | |
282 | { 2, 4 }, { 2, 3 }, | |
283 | { 1, 4 }, { 1, 3 }, { 1, 2 } | |
284 | }; | |
285 | static print_params_t *print_params = default_print_params; | |
286 | ||
287 | #ifdef GUILE_DEBUG | |
1bbd0b84 GB |
288 | GUILE_PROC(set_print_params_x, "set-print-params!", 1, 0, 0, |
289 | (SCM params) | |
290 | #define FUNC_NAME s_set_print_params_x | |
2b407f9b MD |
291 | { |
292 | int i, n = scm_ilength (params); | |
293 | SCM ls; | |
294 | print_params_t *new_params; | |
1bbd0b84 | 295 | SCM_ASSERT (n >= 1, params, SCM_ARG2, FUNC_NAME); |
2b407f9b MD |
296 | for (ls = params; SCM_NIMP (ls); ls = SCM_CDR (ls)) |
297 | SCM_ASSERT (scm_ilength (SCM_CAR (params)) == 2 | |
298 | && SCM_INUMP (SCM_CAAR (ls)) | |
299 | && SCM_INUM (SCM_CAAR (ls)) >= 0 | |
300 | && SCM_INUMP (SCM_CADAR (ls)) | |
301 | && SCM_INUM (SCM_CADAR (ls)) >= 0, | |
302 | params, | |
303 | SCM_ARG2, | |
304 | s_set_print_params_x); | |
305 | new_params = scm_must_malloc (n * sizeof (print_params_t), | |
1bbd0b84 | 306 | FUNC_NAME); |
2b407f9b MD |
307 | if (print_params != default_print_params) |
308 | scm_must_free (print_params); | |
309 | print_params = new_params; | |
310 | for (i = 0; i < n; ++i) | |
311 | { | |
312 | print_params[i].level = SCM_INUM (SCM_CAAR (params)); | |
313 | print_params[i].length = SCM_INUM (SCM_CADAR (params)); | |
314 | params = SCM_CDR (params); | |
315 | } | |
316 | n_print_params = n; | |
317 | return SCM_UNSPECIFIED; | |
318 | } | |
1bbd0b84 | 319 | #undef FUNC_NAME |
2b407f9b MD |
320 | #endif |
321 | ||
ab4f3efb | 322 | static void |
1bbd0b84 | 323 | indent (int n, SCM port) |
ab4f3efb MD |
324 | { |
325 | int i; | |
326 | for (i = 0; i < n; ++i) | |
b7f3516f | 327 | scm_putc (' ', port); |
ab4f3efb MD |
328 | } |
329 | ||
ab4f3efb | 330 | static void |
1bbd0b84 | 331 | display_frame_expr (char *hdr,SCM exp,char *tlr,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
ab4f3efb | 332 | { |
2b407f9b MD |
333 | SCM string; |
334 | int i = 0, n; | |
335 | scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (sport); | |
336 | do | |
ab4f3efb | 337 | { |
2b407f9b MD |
338 | pstate->length = print_params[i].length; |
339 | ptob->seek (sport, 0, SEEK_SET); | |
340 | if (SCM_NIMP (exp) && SCM_CONSP (exp)) | |
341 | { | |
342 | pstate->level = print_params[i].level - 1; | |
343 | scm_iprlist (hdr, exp, tlr[0], sport, pstate); | |
344 | scm_puts (&tlr[1], sport); | |
345 | } | |
346 | else | |
347 | { | |
348 | pstate->level = print_params[i].level; | |
349 | scm_iprin1 (exp, sport, pstate); | |
350 | } | |
351 | ptob->flush (sport); | |
352 | n = ptob->seek (sport, 0, SEEK_CUR); | |
353 | ++i; | |
ab4f3efb | 354 | } |
2b407f9b MD |
355 | while (indentation + n > SCM_BACKTRACE_WIDTH && i < n_print_params); |
356 | ptob->truncate (sport, n); | |
357 | string = scm_strport_to_string (sport); | |
358 | /* Remove control characters */ | |
359 | for (i = 0; i < n; ++i) | |
360 | if (iscntrl (SCM_CHARS (string)[i])) | |
361 | SCM_CHARS (string)[i] = ' '; | |
362 | /* Truncate */ | |
363 | if (indentation + n > SCM_BACKTRACE_WIDTH) | |
364 | { | |
365 | n = SCM_BACKTRACE_WIDTH - indentation; | |
366 | SCM_CHARS (string)[n - 1] = '$'; | |
367 | } | |
368 | ||
369 | scm_lfwrite (SCM_CHARS (string), n, port); | |
ab4f3efb MD |
370 | } |
371 | ||
e3c37929 | 372 | static void |
1bbd0b84 | 373 | display_application (SCM frame,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
e3c37929 MD |
374 | { |
375 | SCM proc = SCM_FRAME_PROC (frame); | |
376 | SCM name = (SCM_NFALSEP (scm_procedure_p (proc)) | |
377 | ? scm_procedure_name (proc) | |
378 | : SCM_BOOL_F); | |
379 | display_frame_expr ("[", | |
380 | scm_cons (SCM_NFALSEP (name) ? name : proc, | |
381 | SCM_FRAME_ARGS (frame)), | |
382 | SCM_FRAME_EVAL_ARGS_P (frame) ? " ..." : "]", | |
383 | indentation, | |
384 | sport, | |
385 | port, | |
386 | pstate); | |
387 | } | |
388 | ||
1bbd0b84 GB |
389 | GUILE_PROC(scm_display_application, "display-application", 1, 2, 0, |
390 | (SCM frame, SCM port, SCM indent), | |
391 | "") | |
392 | #define FUNC_NAME s_scm_display_application | |
e3c37929 | 393 | { |
2b407f9b MD |
394 | SCM_ASSERT (SCM_NIMP (frame) && SCM_FRAMEP (frame), |
395 | frame, SCM_ARG1, s_display_application); | |
e3c37929 MD |
396 | if (SCM_UNBNDP (port)) |
397 | port = scm_cur_outp; | |
2b407f9b MD |
398 | else |
399 | SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), | |
400 | port, SCM_ARG2, s_display_application); | |
401 | if (SCM_UNBNDP (indent)) | |
402 | indent = SCM_INUM0; | |
403 | else | |
404 | SCM_ASSERT (SCM_INUMP (indent), indent, SCM_ARG3, s_display_application); | |
405 | ||
e3c37929 MD |
406 | if (SCM_FRAME_PROC_P (frame)) |
407 | /* Display an application. */ | |
408 | { | |
2b407f9b | 409 | SCM sport, print_state; |
e3c37929 MD |
410 | scm_print_state *pstate; |
411 | ||
2b407f9b MD |
412 | /* Create a string port used for adaptation of printing parameters. */ |
413 | sport = scm_mkstrport (SCM_INUM0, | |
414 | scm_make_string (SCM_MAKINUM (240), | |
415 | SCM_UNDEFINED), | |
416 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 417 | FUNC_NAME); |
2b407f9b | 418 | |
e3c37929 MD |
419 | /* Create a print state for printing of frames. */ |
420 | print_state = scm_make_print_state (); | |
421 | pstate = SCM_PRINT_STATE (print_state); | |
422 | pstate->writingp = 1; | |
423 | pstate->fancyp = 1; | |
e3c37929 | 424 | |
2b407f9b | 425 | display_application (frame, SCM_INUM (indent), sport, port, pstate); |
e3c37929 MD |
426 | return SCM_BOOL_T; |
427 | } | |
428 | else | |
429 | return SCM_BOOL_F; | |
430 | } | |
1bbd0b84 | 431 | #undef FUNC_NAME |
e3c37929 | 432 | |
ab4f3efb | 433 | static void |
1bbd0b84 | 434 | display_frame (SCM frame,int nfield,int indentation,SCM sport,SCM port,scm_print_state *pstate) |
ab4f3efb MD |
435 | { |
436 | int n, i, j; | |
437 | ||
438 | /* Announce missing frames? */ | |
439 | if (!SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame)) | |
440 | { | |
441 | indent (nfield + 1 + indentation, port); | |
b7f3516f | 442 | scm_puts ("...\n", port); |
ab4f3efb MD |
443 | } |
444 | ||
445 | /* Check size of frame number. */ | |
446 | n = SCM_FRAME_NUMBER (frame); | |
447 | for (i = 0, j = n; j > 0; ++i) j /= 10; | |
448 | ||
449 | /* Number indentation. */ | |
450 | indent (nfield - (i ? i : 1), port); | |
451 | ||
452 | /* Frame number. */ | |
453 | scm_iprin1 (SCM_MAKINUM (n), port, pstate); | |
454 | ||
455 | /* Real frame marker */ | |
b7f3516f | 456 | scm_putc (SCM_FRAME_REAL_P (frame) ? '*' : ' ', port); |
ab4f3efb MD |
457 | |
458 | /* Indentation. */ | |
459 | indent (indentation, port); | |
460 | ||
461 | if (SCM_FRAME_PROC_P (frame)) | |
462 | /* Display an application. */ | |
e3c37929 | 463 | display_application (frame, nfield + 1 + indentation, sport, port, pstate); |
ab4f3efb MD |
464 | else |
465 | /* Display a special form. */ | |
466 | { | |
467 | SCM source = SCM_FRAME_SOURCE (frame); | |
2e9d6c6d | 468 | SCM copy = (SCM_NIMP (source) && SCM_CONSP (source) |
7f2d92b1 | 469 | ? scm_source_property (source, scm_sym_copy) |
2e9d6c6d MD |
470 | : SCM_BOOL_F); |
471 | SCM umcopy = (SCM_NIMP (source) && SCM_MEMOIZEDP (source) | |
472 | ? scm_unmemoize (source) | |
473 | : SCM_BOOL_F); | |
ab4f3efb | 474 | display_frame_expr ("(", |
2e9d6c6d | 475 | SCM_NIMP (copy) && SCM_CONSP (copy) ? copy : umcopy, |
ab4f3efb MD |
476 | ")", |
477 | nfield + 1 + indentation, | |
478 | sport, | |
479 | port, | |
480 | pstate); | |
481 | } | |
1ae6af28 | 482 | scm_putc ('\n', port); |
ab4f3efb MD |
483 | |
484 | /* Announce missing frames? */ | |
485 | if (SCM_BACKWARDS_P && SCM_FRAME_OVERFLOW_P (frame)) | |
486 | { | |
487 | indent (nfield + 1 + indentation, port); | |
b7f3516f | 488 | scm_puts ("...\n", port); |
ab4f3efb MD |
489 | } |
490 | } | |
491 | ||
bdf8afff MD |
492 | struct display_backtrace_args { |
493 | SCM stack; | |
494 | SCM port; | |
495 | SCM first; | |
496 | SCM depth; | |
497 | }; | |
498 | ||
bdf8afff | 499 | static SCM |
1bbd0b84 GB |
500 | display_backtrace_body(struct display_backtrace_args *a) |
501 | #define FUNC_NAME "display_backtrace_body" | |
ab4f3efb MD |
502 | { |
503 | int n_frames, beg, end, n, i, j; | |
504 | int nfield, indent_p, indentation; | |
505 | SCM frame, sport, print_state; | |
506 | scm_print_state *pstate; | |
507 | ||
78446828 MV |
508 | a->port = SCM_COERCE_OUTPORT (a->port); |
509 | ||
ab4f3efb | 510 | /* Argument checking and extraction. */ |
bdf8afff MD |
511 | SCM_ASSERT (SCM_NIMP (a->stack) && SCM_STACKP (a->stack), |
512 | a->stack, | |
ab4f3efb MD |
513 | SCM_ARG1, |
514 | s_display_backtrace); | |
bdf8afff MD |
515 | SCM_ASSERT (SCM_NIMP (a->port) && SCM_OPOUTPORTP (a->port), |
516 | a->port, | |
ab4f3efb MD |
517 | SCM_ARG2, |
518 | s_display_backtrace); | |
bdf8afff MD |
519 | n_frames = SCM_INUM (scm_stack_length (a->stack)); |
520 | n = SCM_INUMP (a->depth) ? SCM_INUM (a->depth) : SCM_BACKTRACE_DEPTH; | |
ab4f3efb MD |
521 | if (SCM_BACKWARDS_P) |
522 | { | |
bdf8afff | 523 | beg = SCM_INUMP (a->first) ? SCM_INUM (a->first) : 0; |
ab4f3efb MD |
524 | end = beg + n - 1; |
525 | if (end >= n_frames) | |
526 | end = n_frames - 1; | |
527 | n = end - beg + 1; | |
528 | } | |
529 | else | |
530 | { | |
bdf8afff | 531 | if (SCM_INUMP (a->first)) |
ab4f3efb | 532 | { |
bdf8afff | 533 | beg = SCM_INUM (a->first); |
ab4f3efb MD |
534 | end = beg - n + 1; |
535 | if (end < 0) | |
536 | end = 0; | |
537 | } | |
538 | else | |
539 | { | |
540 | beg = n - 1; | |
541 | end = 0; | |
542 | if (beg >= n_frames) | |
543 | beg = n_frames - 1; | |
544 | } | |
545 | n = beg - end + 1; | |
546 | } | |
bdf8afff MD |
547 | SCM_ASSERT (beg >= 0 && beg < n_frames, a->first, SCM_ARG3, s_display_backtrace); |
548 | SCM_ASSERT (n > 0, a->depth, SCM_ARG4, s_display_backtrace); | |
ab4f3efb MD |
549 | |
550 | /* Create a string port used for adaptation of printing parameters. */ | |
551 | sport = scm_mkstrport (SCM_INUM0, | |
552 | scm_make_string (SCM_MAKINUM (240), SCM_UNDEFINED), | |
553 | SCM_OPN | SCM_WRTNG, | |
1bbd0b84 | 554 | FUNC_NAME); |
ab4f3efb MD |
555 | |
556 | /* Create a print state for printing of frames. */ | |
557 | print_state = scm_make_print_state (); | |
558 | pstate = SCM_PRINT_STATE (print_state); | |
559 | pstate->writingp = 1; | |
560 | pstate->fancyp = 1; | |
561 | ||
562 | /* First find out if it's reasonable to do indentation. */ | |
563 | if (SCM_BACKWARDS_P) | |
564 | indent_p = 0; | |
565 | else | |
566 | { | |
567 | indent_p = 1; | |
bdf8afff | 568 | frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg)); |
ab4f3efb MD |
569 | for (i = 0, j = 0; i < n; ++i) |
570 | { | |
571 | if (SCM_FRAME_REAL_P (frame)) | |
572 | ++j; | |
573 | if (j > SCM_BACKTRACE_INDENT) | |
574 | { | |
575 | indent_p = 0; | |
576 | break; | |
577 | } | |
578 | frame = (SCM_BACKWARDS_P | |
579 | ? SCM_FRAME_PREV (frame) | |
580 | : SCM_FRAME_NEXT (frame)); | |
581 | } | |
582 | } | |
583 | ||
584 | /* Determine size of frame number field. */ | |
bdf8afff | 585 | j = SCM_FRAME_NUMBER (scm_stack_ref (a->stack, SCM_MAKINUM (end))); |
ab4f3efb MD |
586 | for (i = 0; j > 0; ++i) j /= 10; |
587 | nfield = i ? i : 1; | |
588 | ||
ab4f3efb | 589 | /* Print frames. */ |
bdf8afff | 590 | frame = scm_stack_ref (a->stack, SCM_MAKINUM (beg)); |
ab4f3efb | 591 | indentation = 1; |
bdf8afff | 592 | display_frame (frame, nfield, indentation, sport, a->port, pstate); |
ab4f3efb MD |
593 | for (i = 1; i < n; ++i) |
594 | { | |
595 | if (indent_p && SCM_FRAME_EVAL_ARGS_P (frame)) | |
596 | ++indentation; | |
597 | frame = SCM_BACKWARDS_P ? SCM_FRAME_PREV (frame) : SCM_FRAME_NEXT (frame); | |
bdf8afff | 598 | display_frame (frame, nfield, indentation, sport, a->port, pstate); |
ab4f3efb | 599 | } |
bdf8afff MD |
600 | |
601 | return SCM_UNSPECIFIED; | |
602 | } | |
1bbd0b84 | 603 | #undef FUNC_NAME |
bdf8afff | 604 | |
1bbd0b84 GB |
605 | GUILE_PROC(scm_display_backtrace, "display-backtrace", 2, 2, 0, |
606 | (SCM stack, SCM port, SCM first, SCM depth), | |
607 | "") | |
608 | #define FUNC_NAME s_scm_display_backtrace | |
bdf8afff | 609 | { |
0b2cb4ee MD |
610 | struct display_backtrace_args a; |
611 | struct display_error_handler_data data; | |
612 | a.stack = stack; | |
613 | a.port = port; | |
614 | a.first = first; | |
615 | a.depth = depth; | |
616 | data.mode = "backtrace"; | |
617 | data.port = port; | |
bdf8afff MD |
618 | scm_internal_catch (SCM_BOOL_T, |
619 | (scm_catch_body_t) display_backtrace_body, &a, | |
620 | (scm_catch_handler_t) display_error_handler, &data); | |
ab4f3efb MD |
621 | return SCM_UNSPECIFIED; |
622 | } | |
1bbd0b84 | 623 | #undef FUNC_NAME |
ab4f3efb | 624 | |
78f9f47b | 625 | SCM_VCELL (scm_has_shown_backtrace_hint_p_var, "has-shown-backtrace-hint?"); |
5aab5d96 | 626 | |
1bbd0b84 GB |
627 | GUILE_PROC(scm_backtrace, "backtrace", 0, 0, 0, |
628 | (), | |
629 | "") | |
630 | #define FUNC_NAME s_scm_backtrace | |
5aab5d96 | 631 | { |
b6609fc7 | 632 | SCM the_last_stack = scm_fluid_ref (SCM_CDR (scm_the_last_stack_fluid)); |
a5d6d578 | 633 | if (SCM_NFALSEP (the_last_stack)) |
5aab5d96 MD |
634 | { |
635 | scm_newline (scm_cur_outp); | |
3a1f8447 | 636 | scm_puts ("Backtrace:\n", scm_cur_outp); |
a5d6d578 | 637 | scm_display_backtrace (the_last_stack, |
5aab5d96 MD |
638 | scm_cur_outp, |
639 | SCM_UNDEFINED, | |
640 | SCM_UNDEFINED); | |
641 | scm_newline (scm_cur_outp); | |
642 | if (SCM_FALSEP (SCM_CDR (scm_has_shown_backtrace_hint_p_var)) | |
643 | && !SCM_BACKTRACE_P) | |
644 | { | |
b7f3516f TT |
645 | scm_puts ("Type \"(debug-enable 'backtrace)\" if you would like " |
646 | "a backtrace\n" | |
647 | "automatically if an error occurs in the future.\n", | |
648 | scm_cur_outp); | |
5aab5d96 MD |
649 | SCM_SETCDR (scm_has_shown_backtrace_hint_p_var, SCM_BOOL_T); |
650 | } | |
651 | } | |
652 | else | |
653 | { | |
b7f3516f | 654 | scm_puts ("No backtrace available.\n", scm_cur_outp); |
5aab5d96 MD |
655 | } |
656 | return SCM_UNSPECIFIED; | |
657 | } | |
1bbd0b84 | 658 | #undef FUNC_NAME |
5aab5d96 | 659 | |
ab4f3efb MD |
660 | \f |
661 | ||
662 | void | |
663 | scm_init_backtrace () | |
664 | { | |
a5d6d578 | 665 | SCM f = scm_make_fluid (); |
b6609fc7 | 666 | scm_the_last_stack_fluid = scm_sysintern ("the-last-stack", f); |
5aab5d96 | 667 | |
ab4f3efb MD |
668 | #include "backtrace.x" |
669 | } |