1.0.25.21: handling of potential corruptions
[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     sigset_t oldset;
99     thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset);
100     fprintf(stderr, "CORRUPTION WARNING");
101     va_start(ap, fmt);
102     print_message(fmt, ap);
103     va_end(ap);
104     fprintf(stderr, "The integrity of this image is possibly compromised.\n");
105     if (lose_on_corruption_p)
106         fprintf(stderr, "Exiting.\n");
107     else
108         fprintf(stderr, "Continuing with fingers crossed.\n");
109     fflush(stderr);
110     if (lose_on_corruption_p)
111         call_lossage_handler();
112     else
113         thread_sigmask(SIG_SETMASK,&oldset,0);
114 }
115 \f
116 /* internal error handler for when the Lisp error system doesn't exist
117  *
118  * FIXME: Shouldn't error output go to stderr instead of stdout? (Alas,
119  * this'd require changes in a number of things like brief_print(..),
120  * or I'd have changed it immediately.) */
121 void
122 describe_internal_error(os_context_t *context)
123 {
124     unsigned char *ptr = arch_internal_error_arguments(context);
125     int len, scoffset, sc, offset, ch;
126
127     len = *ptr++;
128     printf("internal error #%d\n", *ptr++);
129     len--;
130     while (len > 0) {
131         scoffset = *ptr++;
132         len--;
133         if (scoffset == 253) {
134             scoffset = *ptr++;
135             len--;
136         }
137         else if (scoffset == 254) {
138             scoffset = ptr[0] + ptr[1]*256;
139             ptr += 2;
140             len -= 2;
141         }
142         else if (scoffset == 255) {
143             scoffset = ptr[0] + (ptr[1]<<8) + (ptr[2]<<16) + (ptr[3]<<24);
144             ptr += 4;
145             len -= 4;
146         }
147         sc = scoffset & 0x1f;
148         offset = scoffset >> 5;
149
150         printf("    SC: %d, Offset: %d", sc, offset);
151         switch (sc) {
152         case sc_AnyReg:
153         case sc_DescriptorReg:
154             putchar('\t');
155             brief_print(*os_context_register_addr(context, offset));
156             break;
157
158         case sc_CharacterReg:
159             ch = *os_context_register_addr(context, offset);
160 #ifdef LISP_FEATURE_X86
161             if (offset&1)
162                 ch = ch>>8;
163             ch = ch & 0xff;
164 #endif
165             switch (ch) {
166             case '\n': printf("\t'\\n'\n"); break;
167             case '\b': printf("\t'\\b'\n"); break;
168             case '\t': printf("\t'\\t'\n"); break;
169             case '\r': printf("\t'\\r'\n"); break;
170             default:
171                 if (ch < 32 || ch > 127)
172                     printf("\\%03o", ch);
173                 else
174                     printf("\t'%c'\n", ch);
175                 break;
176             }
177             break;
178         case sc_SapReg:
179 #ifdef sc_WordPointerReg
180         case sc_WordPointerReg:
181 #endif
182             printf("\t0x%08lx\n", (unsigned long) *os_context_register_addr(context, offset));
183             break;
184         case sc_SignedReg:
185             printf("\t%ld\n", (long) *os_context_register_addr(context, offset));
186             break;
187         case sc_UnsignedReg:
188             printf("\t%lu\n", (unsigned long) *os_context_register_addr(context, offset));
189             break;
190 #ifdef sc_SingleFloatReg
191         case sc_SingleFloatReg:
192             printf("\t%g\n", *(float *)&context->sc_fpregs[offset]);
193             break;
194 #endif
195 #ifdef sc_DoubleFloatReg
196         case sc_DoubleFloatReg:
197             printf("\t%g\n", *(double *)&context->sc_fpregs[offset]);
198             break;
199 #endif
200         default:
201             printf("\t???\n");
202             break;
203         }
204     }
205 }
206 \f
207 /* utility routines used by miscellaneous pieces of code */
208
209 lispobj debug_print(lispobj string)
210 {
211     /* This is a kludge.  It's not actually safe - in general - to use
212        %primitive print on the alpha, because it skips half of the
213        number stack setup that should usually be done on a function
214        call, so the called routine (i.e. this one) ends up being able
215        to overwrite local variables in the caller.  Rather than fix
216        this everywhere that %primitive print is used (it's only a
217        debugging aid anyway) we just guarantee our safety by putting
218        an unused buffer on the stack before doing anything else
219        here */
220     char untouched[32];
221     fprintf(stderr, "%s\n",
222             (char *)(((struct vector *)native_pointer(string))->data));
223     /* shut GCC up about not using this, because that's the point.. */
224     (void)untouched;
225     return NIL;
226 }