1 /* Handling of compile-time options that influence the library.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran runtime library (libgfortran).
5
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "libgfortran.h"
26 #include <signal.h>
27
28
29 /* Useful compile-time options will be stored in here. */
30 compile_options_t compile_options;
31
32
33 volatile sig_atomic_t fatal_error_in_progress = 0;
34
35
36 /* Helper function for backtrace_handler to write information about the
37 received signal to stderr before actually giving the backtrace. */
38 static void
show_signal(int signum)39 show_signal (int signum)
40 {
41 const char * name = NULL, * desc = NULL;
42
43 switch (signum)
44 {
45 #if defined(SIGQUIT)
46 case SIGQUIT:
47 name = "SIGQUIT";
48 desc = "Terminal quit signal";
49 break;
50 #endif
51
52 /* The following 4 signals are defined by C89. */
53 case SIGILL:
54 name = "SIGILL";
55 desc = "Illegal instruction";
56 break;
57
58 case SIGABRT:
59 name = "SIGABRT";
60 desc = "Process abort signal";
61 break;
62
63 case SIGFPE:
64 name = "SIGFPE";
65 desc = "Floating-point exception - erroneous arithmetic operation";
66 break;
67
68 case SIGSEGV:
69 name = "SIGSEGV";
70 desc = "Segmentation fault - invalid memory reference";
71 break;
72
73 #if defined(SIGBUS)
74 case SIGBUS:
75 name = "SIGBUS";
76 desc = "Access to an undefined portion of a memory object";
77 break;
78 #endif
79
80 #if defined(SIGSYS)
81 case SIGSYS:
82 name = "SIGSYS";
83 desc = "Bad system call";
84 break;
85 #endif
86
87 #if defined(SIGTRAP)
88 case SIGTRAP:
89 name = "SIGTRAP";
90 desc = "Trace/breakpoint trap";
91 break;
92 #endif
93
94 #if defined(SIGXCPU)
95 case SIGXCPU:
96 name = "SIGXCPU";
97 desc = "CPU time limit exceeded";
98 break;
99 #endif
100
101 #if defined(SIGXFSZ)
102 case SIGXFSZ:
103 name = "SIGXFSZ";
104 desc = "File size limit exceeded";
105 break;
106 #endif
107 }
108
109 if (name)
110 st_printf ("\nProgram received signal %s: %s.\n", name, desc);
111 else
112 st_printf ("\nProgram received signal %d.\n", signum);
113 }
114
115
116 /* A signal handler to allow us to output a backtrace. */
117 void
backtrace_handler(int signum)118 backtrace_handler (int signum)
119 {
120 /* Since this handler is established for more than one kind of signal,
121 it might still get invoked recursively by delivery of some other kind
122 of signal. Use a static variable to keep track of that. */
123 if (fatal_error_in_progress)
124 raise (signum);
125 fatal_error_in_progress = 1;
126
127 show_signal (signum);
128 estr_write ("\nBacktrace for this error:\n");
129 backtrace ();
130
131 /* Now reraise the signal. We reactivate the signal's
132 default handling, which is to terminate the process.
133 We could just call exit or abort,
134 but reraising the signal sets the return status
135 from the process correctly. */
136 signal (signum, SIG_DFL);
137 raise (signum);
138 }
139
140
141 /* Helper function for set_options because we need to access the
142 global variable options which is not seen in set_options. */
143 static void
maybe_find_addr2line(void)144 maybe_find_addr2line (void)
145 {
146 if (options.backtrace == -1)
147 find_addr2line ();
148 }
149
150 /* Set the usual compile-time options. */
151 extern void set_options (int , int []);
152 export_proto(set_options);
153
154 void
set_options(int num,int options[])155 set_options (int num, int options[])
156 {
157 if (num >= 1)
158 compile_options.warn_std = options[0];
159 if (num >= 2)
160 compile_options.allow_std = options[1];
161 if (num >= 3)
162 compile_options.pedantic = options[2];
163 /* options[3] is the removed -fdump-core option. It's place in the
164 options array is retained due to ABI compatibility. Remove when
165 bumping the library ABI. */
166 if (num >= 5)
167 compile_options.backtrace = options[4];
168 if (num >= 6)
169 compile_options.sign_zero = options[5];
170 if (num >= 7)
171 compile_options.bounds_check = options[6];
172 /* options[7] is the -frange-check option, which no longer affects
173 the library behavior; range checking is now always done when
174 parsing integers. It's place in the options array is retained due
175 to ABI compatibility. Remove when bumping the library ABI. */
176 if (num >= 9)
177 compile_options.fpe_summary = options[8];
178
179 /* If backtrace is required, we set signal handlers on the POSIX
180 2001 signals with core action. */
181 if (compile_options.backtrace)
182 {
183 #if defined(SIGQUIT)
184 signal (SIGQUIT, backtrace_handler);
185 #endif
186
187 /* The following 4 signals are defined by C89. */
188 signal (SIGILL, backtrace_handler);
189 signal (SIGABRT, backtrace_handler);
190 signal (SIGFPE, backtrace_handler);
191 signal (SIGSEGV, backtrace_handler);
192
193 #if defined(SIGBUS)
194 signal (SIGBUS, backtrace_handler);
195 #endif
196
197 #if defined(SIGSYS)
198 signal (SIGSYS, backtrace_handler);
199 #endif
200
201 #if defined(SIGTRAP)
202 signal (SIGTRAP, backtrace_handler);
203 #endif
204
205 #if defined(SIGXCPU)
206 signal (SIGXCPU, backtrace_handler);
207 #endif
208
209 #if defined(SIGXFSZ)
210 signal (SIGXFSZ, backtrace_handler);
211 #endif
212
213 maybe_find_addr2line ();
214 }
215 }
216
217
218 /* Default values for the compile-time options. Keep in sync with
219 gcc/fortran/options.c (gfc_init_options). */
220 void
init_compile_options(void)221 init_compile_options (void)
222 {
223 compile_options.warn_std = GFC_STD_F95_DEL | GFC_STD_LEGACY;
224 compile_options.allow_std = GFC_STD_F95_OBS | GFC_STD_F95_DEL
225 | GFC_STD_F2003 | GFC_STD_F2008 | GFC_STD_F95 | GFC_STD_F77
226 | GFC_STD_F2008_OBS | GFC_STD_GNU | GFC_STD_LEGACY;
227 compile_options.pedantic = 0;
228 compile_options.backtrace = 0;
229 compile_options.sign_zero = 1;
230 compile_options.fpe_summary = 0;
231 }
232
233 /* Function called by the front-end to tell us the
234 default for unformatted data conversion. */
235
236 extern void set_convert (int);
237 export_proto (set_convert);
238
239 void
set_convert(int conv)240 set_convert (int conv)
241 {
242 compile_options.convert = conv;
243 }
244
245 extern void set_record_marker (int);
246 export_proto (set_record_marker);
247
248
249 void
set_record_marker(int val)250 set_record_marker (int val)
251 {
252
253 switch(val)
254 {
255 case 4:
256 compile_options.record_marker = sizeof (GFC_INTEGER_4);
257 break;
258
259 case 8:
260 compile_options.record_marker = sizeof (GFC_INTEGER_8);
261 break;
262
263 default:
264 runtime_error ("Invalid value for record marker");
265 break;
266 }
267 }
268
269 extern void set_max_subrecord_length (int);
270 export_proto (set_max_subrecord_length);
271
set_max_subrecord_length(int val)272 void set_max_subrecord_length(int val)
273 {
274 if (val > GFC_MAX_SUBRECORD_LENGTH || val < 1)
275 {
276 runtime_error ("Invalid value for maximum subrecord length");
277 return;
278 }
279
280 compile_options.max_subrecord_length = val;
281 }
282