f7da2d3670b32bf839fa579e796f55cfc6b7b349
[sbcl.git] / src / runtime / interr.c
1 /*
2  * stuff to handle internal errors
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <stdlib.h>
19
20 #include "sbcl.h"
21 #include "arch.h"
22 #include "signal.h"
23
24 #include "runtime.h"
25 #include "interr.h"
26 #include "print.h"
27 #include "lispregs.h"
28 #include "genesis/static-symbols.h"
29 #include "genesis/vector.h"
30 #include "thread.h"
31 #include "monitor.h"
32 \f
33 /* the way that we shut down the system on a fatal error */
34
35 static void
36 default_lossage_handler(void)
37 {
38     exit(1);
39 }
40 static void (*lossage_handler)(void) = default_lossage_handler;
41
42 void enable_lossage_handler(void)
43 {
44     lossage_handler = monitor_or_something;
45 }
46 void disable_lossage_handler(void)
47 {
48     lossage_handler = default_lossage_handler;
49 }
50
51 static
52 void print_message(char *fmt, va_list ap)
53 {
54     fprintf(stderr, " in SBCL pid %d",getpid());
55 #if defined(LISP_FEATURE_SB_THREAD)
56     fprintf(stderr, "(tid %lu)", (unsigned long) thread_self());
57 #endif
58     if (fmt) {
59         fprintf(stderr, ":\n");
60         vfprintf(stderr, fmt, ap);
61     }
62     fprintf(stderr, "\n");
63 }
64
65 static inline void
66 call_lossage_handler() never_returns;
67
68 static inline void
69 call_lossage_handler()
70 {
71     lossage_handler();
72     fprintf(stderr, "Argh! lossage_handler() returned, total confusion..\n");
73     exit(1);
74 }
75
76 void
77 lose(char *fmt, ...)
78 {
79     va_list ap;
80     /* Block signals to prevent other threads, timers and such from
81      * interfering. If only all threads could be stopped somehow. */
82     block_blockable_signals();
83     fprintf(stderr, "fatal error encountered");
84     va_start(ap, fmt);
85     print_message(fmt, ap);
86     va_end(ap);
87     fprintf(stderr, "\n");
88     fflush(stderr);
89     call_lossage_handler();
90 }
91
92 boolean lose_on_corruption_p = 0;
93
94 void
95 corruption_warning_and_maybe_lose(char *fmt, ...)
96 {
97     va_list ap;
98 #ifndef LISP_FEATURE_WIN32
99     sigset_t oldset;
100     thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset);
101 #endif
102     fprintf(stderr, "CORRUPTION WARNING");
103     va_start(ap, fmt);
104     print_message(fmt, ap);
105     va_end(ap);
106     fprintf(stderr, "The integrity of this image is possibly compromised.\n");
107     if (lose_on_corruption_p)
108         fprintf(stderr, "Exiting.\n");
109     else
110         fprintf(stderr, "Continuing with fingers crossed.\n");
111     fflush(stderr);
112     if (lose_on_corruption_p)
113         call_lossage_handler();
114 #ifndef LISP_FEATURE_WIN32
115     else
116         thread_sigmask(SIG_SETMASK,&oldset,0);
117 #endif
118 }
119 \f
120 /* internal error handler for when the Lisp error system doesn't exist
121  *
122  * FIXME: Shouldn't error output go to stderr instead of stdout? (Alas,
123  * this'd require changes in a number of things like brief_print(..),
124  * or I'd have changed it immediately.) */
125 void
126 describe_internal_error(os_context_t *context)
127 {
128     unsigned char *ptr = arch_internal_error_arguments(context);
129     int len, scoffset, sc, offset, ch;
130
131     len = *ptr++;
132     printf("internal error #%d\n", *ptr++);
133     len--;
134     while (len > 0) {
135         scoffset = *ptr++;
136         len--;
137         if (scoffset == 253) {
138             scoffset = *ptr++;
139             len--;
140         }
141         else if (scoffset == 254) {
142             scoffset = ptr[0] + ptr[1]*256;
143             ptr += 2;
144             len -= 2;
145         }
146         else if (scoffset == 255) {
147             scoffset = ptr[0] + (ptr[1]<<8) + (ptr[2]<<16) + (ptr[3]<<24);
148             ptr += 4;
149             len -= 4;
150         }
151         sc = scoffset & 0x1f;
152         offset = scoffset >> 5;
153
154         printf("    SC: %d, Offset: %d", sc, offset);
155         switch (sc) {
156         case sc_AnyReg:
157         case sc_DescriptorReg:
158             putchar('\t');
159             brief_print(*os_context_register_addr(context, offset));
160             break;
161
162         case sc_CharacterReg:
163             ch = *os_context_register_addr(context, offset);
164 #ifdef LISP_FEATURE_X86
165             if (offset&1)
166                 ch = ch>>8;
167             ch = ch & 0xff;
168 #endif
169             switch (ch) {
170             case '\n': printf("\t'\\n'\n"); break;
171             case '\b': printf("\t'\\b'\n"); break;
172             case '\t': printf("\t'\\t'\n"); break;
173             case '\r': printf("\t'\\r'\n"); break;
174             default:
175                 if (ch < 32 || ch > 127)
176                     printf("\\%03o", ch);
177                 else
178                     printf("\t'%c'\n", ch);
179                 break;
180             }
181             break;
182         case sc_SapReg:
183 #ifdef sc_WordPointerReg
184         case sc_WordPointerReg:
185 #endif
186             printf("\t0x%08lx\n", (unsigned long) *os_context_register_addr(context, offset));
187             break;
188         case sc_SignedReg:
189             printf("\t%ld\n", (long) *os_context_register_addr(context, offset));
190             break;
191         case sc_UnsignedReg:
192             printf("\t%lu\n", (unsigned long) *os_context_register_addr(context, offset));
193             break;
194 #ifdef sc_SingleFloatReg
195         case sc_SingleFloatReg:
196             printf("\t%g\n", *(float *)&context->sc_fpregs[offset]);
197             break;
198 #endif
199 #ifdef sc_DoubleFloatReg
200         case sc_DoubleFloatReg:
201             printf("\t%g\n", *(double *)&context->sc_fpregs[offset]);
202             break;
203 #endif
204         default:
205             printf("\t???\n");
206             break;
207         }
208     }
209 }
210 \f
211 /* utility routines used by miscellaneous pieces of code */
212
213 lispobj debug_print(lispobj string)
214 {
215     /* This is a kludge.  It's not actually safe - in general - to use
216        %primitive print on the alpha, because it skips half of the
217        number stack setup that should usually be done on a function
218        call, so the called routine (i.e. this one) ends up being able
219        to overwrite local variables in the caller.  Rather than fix
220        this everywhere that %primitive print is used (it's only a
221        debugging aid anyway) we just guarantee our safety by putting
222        an unused buffer on the stack before doing anything else
223        here */
224     char untouched[32];
225     fprintf(stderr, "%s\n",
226             (char *)(((struct vector *)native_pointer(string))->data));
227     /* shut GCC up about not using this, because that's the point.. */
228     (void)untouched;
229     return NIL;
230 }