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