Debian Codegraph @

gcc/m2/gm2-libs-ch/SysExceptions.c

raw
1 /* SysExceptions.c connect signal handlers to exceptions.
2
3 Copyright (C) 2010-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #include "config.h"
28 #include "system.h"
29
30 #include "gm2-libs-host.h"
31
32 #ifdef MC_M2
33 #include "GSysExceptions.h"
34 #define DECL_PROC_T(X) SysExceptions_PROCEXCEPTION X
35 #define PROC_FUNC(X) X.proc
36 #else
37 #define DECL_PROC_T(X) void (*X) (void *)
38 #define PROC_FUNC(X) X
39 #endif
40
41 #undef EXTERN
42 #if defined(__cplusplus)
43 #define EXTERN extern "C"
44 #else
45 #define EXTERN
46 #endif
47
48 #if 0
49 /* Signals. */
50 #define SIGHUP 1 /* Hangup (POSIX). */
51 #define SIGINT 2 /* Interrupt (ANSI). */
52 #define SIGQUIT 3 /* Quit (POSIX). */
53 #define SIGILL 4 /* Illegal instruction (ANSI). */
54 #define SIGTRAP 5 /* Trace trap (POSIX). */
55 #define SIGABRT 6 /* Abort (ANSI). */
56 #define SIGIOT 6 /* IOT trap (4.2 BSD). */
57 #define SIGBUS 7 /* BUS error (4.2 BSD). */
58 #define SIGFPE 8 /* Floating-point exception (ANSI). */
59 #define SIGKILL 9 /* Kill, unblockable (POSIX). */
60 #define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
61 #define SIGSEGV 11 /* Segmentation violation (ANSI). */
62 #define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
63 #define SIGPIPE 13 /* Broken pipe (POSIX). */
64 #define SIGALRM 14 /* Alarm clock (POSIX). */
65 #define SIGTERM 15 /* Termination (ANSI). */
66 #define SIGSTKFLT 16 /* Stack fault. */
67 #define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */
68 #define SIGCHLD 17 /* Child status has changed (POSIX). */
69 #define SIGCONT 18 /* Continue (POSIX). */
70 #define SIGSTOP 19 /* Stop, unblockable (POSIX). */
71 #define SIGTSTP 20 /* Keyboard stop (POSIX). */
72 #define SIGTTIN 21 /* Background read from tty (POSIX). */
73 #define SIGTTOU 22 /* Background write to tty (POSIX). */
74 #define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */
75 #define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */
76 #define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */
77 #define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */
78 #define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */
79 #define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */
80 #define SIGPOLL SIGIO /* Pollable event occurred (System V). */
81 #define SIGIO 29 /* I/O now possible (4.2 BSD). */
82 #define SIGPWR 30 /* Power failure restart (System V). */
83 #define SIGSYS 31 /* Bad system call. */
84 #define SIGUNUSED 31
85
86 (indexException, rangeException, caseSelectException, invalidLocation,
87 functionException, wholeValueException, wholeDivException, realValueException,
88 realDivException, complexValueException, complexDivException, protException,
89 sysException, coException, exException
90 );
91 #endif
92
93 /* wholeDivException and realDivException are caught by SIGFPE
94 and depatched to the appropriate Modula-2 runtime routine upon
95 testing FPE_INTDIV or FPE_FLTDIV. realValueException is also
96 caught by SIGFPE and dispatched by testing FFE_FLTOVF or
97 FPE_FLTUND or FPE_FLTRES or FPE_FLTINV. indexException is
98 caught by SIGFPE and dispatched by FPE_FLTSUB. */
99
100 #if defined(HAVE_SIGNAL_H)
101 static struct sigaction sigbus;
102 static struct sigaction sigfpe_;
103 static struct sigaction sigsegv;
104
105 static void (*indexProc) (void *);
106 static void (*rangeProc) (void *);
107 static void (*assignmentrangeProc) (void *);
108 static void (*caseProc) (void *);
109 static void (*invalidlocProc) (void *);
110 static void (*functionProc) (void *);
111 static void (*wholevalueProc) (void *);
112 static void (*wholedivProc) (void *);
113 static void (*realvalueProc) (void *);
114 static void (*realdivProc) (void *);
115 static void (*complexvalueProc) (void *);
116 static void (*complexdivProc) (void *);
117 static void (*protectionProc) (void *);
118 static void (*systemProc) (void *);
119 static void (*coroutineProc) (void *);
120 static void (*exceptionProc) (void *);
121
122
123 static void
124 sigbusDespatcher (int signum, siginfo_t *info, void *ucontext)
125 {
126 switch (signum)
127 {
128
129 case SIGSEGV:
130 case SIGBUS:
131 if (info)
132 (*invalidlocProc) (info->si_addr);
133 break;
134 default:
135 perror ("not expecting to arrive here with this signal");
136 }
137 }
138
139 static void
140 sigfpeDespatcher (int signum, siginfo_t *info, void *ucontext)
141 {
142 switch (signum)
143 {
144
145 case SIGFPE:
146 if (info)
147 {
148 if (info->si_code | FPE_INTDIV)
149 (*wholedivProc) (info->si_addr); /* integer divide by zero. */
150 if (info->si_code | FPE_INTOVF)
151 (*wholevalueProc) (info->si_addr); /* integer overflow. */
152 if (info->si_code | FPE_FLTDIV)
153 (*realdivProc) (
154 info->si_addr); /* floating-point divide by zero. */
155 if (info->si_code | FPE_FLTOVF)
156 (*realvalueProc) (info->si_addr); /* floating-point overflow. */
157 if (info->si_code | FPE_FLTUND)
158 (*realvalueProc) (info->si_addr); /* floating-point underflow. */
159 if (info->si_code | FPE_FLTRES)
160 (*realvalueProc) (
161 info->si_addr); /* floating-point inexact result. */
162 if (info->si_code | FPE_FLTINV)
163 (*realvalueProc) (
164 info->si_addr); /* floating-point invalid result. */
165 if (info->si_code | FPE_FLTSUB)
166 (*indexProc) (info->si_addr); /* subscript out of range. */
167 }
168 break;
169 default:
170 perror ("not expecting to arrive here with this signal");
171 }
172 }
173
174 EXTERN
175 void
176 SysExceptions_InitExceptionHandlers (DECL_PROC_T(indexf),
177 DECL_PROC_T(range),
178 DECL_PROC_T(casef),
179 DECL_PROC_T(invalidloc),
180 DECL_PROC_T(function),
181 DECL_PROC_T(wholevalue),
182 DECL_PROC_T(wholediv),
183 DECL_PROC_T(realvalue),
184 DECL_PROC_T(realdiv),
185 DECL_PROC_T(complexvalue),
186 DECL_PROC_T(complexdiv),
187 DECL_PROC_T(protection),
188 DECL_PROC_T(systemf),
189 DECL_PROC_T(coroutine),
190 DECL_PROC_T(exception))
191 {
192 struct sigaction old;
193
194 indexProc = PROC_FUNC (indexf);
195 rangeProc = PROC_FUNC (range);
196 caseProc = PROC_FUNC (casef);
197 invalidlocProc = PROC_FUNC (invalidloc);
198 functionProc = PROC_FUNC (function);
199 wholevalueProc = PROC_FUNC (wholevalue);
200 wholedivProc = PROC_FUNC (wholediv);
201 realvalueProc = PROC_FUNC (realvalue);
202 realdivProc = PROC_FUNC (realdiv);
203 complexvalueProc = PROC_FUNC (complexvalue);
204 complexdivProc = PROC_FUNC (complexdiv);
205 protectionProc = PROC_FUNC (protection);
206 systemProc = PROC_FUNC (systemf);
207 coroutineProc = PROC_FUNC (coroutine);
208 exceptionProc = PROC_FUNC (exception);
209
210 sigbus.sa_sigaction = sigbusDespatcher;
211 sigbus.sa_flags = (SA_SIGINFO);
212 sigemptyset (&sigbus.sa_mask);
213
214 if (sigaction (SIGBUS, &sigbus, &old) != 0)
215 perror ("unable to install the sigbus signal handler");
216
217 sigsegv.sa_sigaction = sigbusDespatcher;
218 sigsegv.sa_flags = (SA_SIGINFO);
219 sigemptyset (&sigsegv.sa_mask);
220
221 if (sigaction (SIGSEGV, &sigsegv, &old) != 0)
222 perror ("unable to install the sigsegv signal handler");
223
224 sigfpe_.sa_sigaction = sigfpeDespatcher;
225 sigfpe_.sa_flags = (SA_SIGINFO);
226 sigemptyset (&sigfpe_.sa_mask);
227
228 if (sigaction (SIGFPE, &sigfpe_, &old) != 0)
229 perror ("unable to install the sigfpe signal handler");
230 }
231
232 #else
233 EXTERN
234 void
235 SysExceptions_InitExceptionHandlers (DECL_PROC_T(indexf),
236 DECL_PROC_T(range),
237 DECL_PROC_T(casef),
238 DECL_PROC_T(invalidloc),
239 DECL_PROC_T(function),
240 DECL_PROC_T(wholevalue),
241 DECL_PROC_T(wholediv),
242 DECL_PROC_T(realvalue),
243 DECL_PROC_T(realdiv),
244 DECL_PROC_T(complexvalue),
245 DECL_PROC_T(complexdiv),
246 DECL_PROC_T(protection),
247 DECL_PROC_T(systemf),
248 DECL_PROC_T(coroutine),
249 DECL_PROC_T(exception))
250 {
251 }
252 #endif
253
254 /* GNU Modula-2 linking fodder. */
255
256 EXTERN
257 void
258 _M2_SysExceptions_init (int argc, char *argv[], char *envp[])
259 {
260 }
261
262 EXTERN
263 void
264 _M2_SysExceptions_fini (int argc, char *argv[], char *envp[])
265 {
266 }