| 1 |
/* Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 2 |
Contributed by Andy Vaught and Paul Brook <[email protected]> |
| 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 |
|
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#ifdef HAVE_UNISTD_H |
| 30 |
#include <unistd.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
|
| 34 |
#if __nvptx__ |
| 35 |
/* 'printf' is all we have. */ |
| 36 |
# undef estr_vprintf |
| 37 |
# define estr_vprintf vprintf |
| 38 |
#else |
| 39 |
# error TODO |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
/* runtime/environ.c */ |
| 44 |
|
| 45 |
options_t options; |
| 46 |
|
| 47 |
|
| 48 |
/* runtime/main.c */ |
| 49 |
|
| 50 |
/* Stupid function to be sure the constructor is always linked in, even |
| 51 |
in the case of static linking. See PR libfortran/22298 for details. */ |
| 52 |
void |
| 53 |
stupid_function_name_for_static_linking (void) |
| 54 |
{ |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
static int argc_save; |
| 60 |
static char **argv_save; |
| 61 |
|
| 62 |
|
| 63 |
/* Set the saved values of the command line arguments. */ |
| 64 |
|
| 65 |
void |
| 66 |
set_args (int argc, char **argv) |
| 67 |
{ |
| 68 |
argc_save = argc; |
| 69 |
argv_save = argv; |
| 70 |
} |
| 71 |
iexport(set_args); |
| 72 |
|
| 73 |
|
| 74 |
/* Retrieve the saved values of the command line arguments. */ |
| 75 |
|
| 76 |
void |
| 77 |
get_args (int *argc, char ***argv) |
| 78 |
{ |
| 79 |
*argc = argc_save; |
| 80 |
*argv = argv_save; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/* runtime/error.c */ |
| 85 |
|
| 86 |
/* Write a null-terminated C string to standard error. This function |
| 87 |
is async-signal-safe. */ |
| 88 |
|
| 89 |
ssize_t |
| 90 |
estr_write (const char *str) |
| 91 |
{ |
| 92 |
return write (STDERR_FILENO, str, strlen (str)); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/* printf() like function for for printing to stderr. Uses a stack |
| 97 |
allocated buffer and doesn't lock stderr, so it should be safe to |
| 98 |
use from within a signal handler. */ |
| 99 |
|
| 100 |
int |
| 101 |
st_printf (const char * format, ...) |
| 102 |
{ |
| 103 |
int written; |
| 104 |
va_list ap; |
| 105 |
va_start (ap, format); |
| 106 |
written = estr_vprintf (format, ap); |
| 107 |
va_end (ap); |
| 108 |
return written; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/* sys_abort()-- Terminate the program showing backtrace and dumping |
| 113 |
core. */ |
| 114 |
|
| 115 |
void |
| 116 |
sys_abort (void) |
| 117 |
{ |
| 118 |
/* If backtracing is enabled, print backtrace and disable signal |
| 119 |
handler for ABRT. */ |
| 120 |
if (options.backtrace == 1 |
| 121 |
|| (options.backtrace == -1 && compile_options.backtrace == 1)) |
| 122 |
{ |
| 123 |
estr_write ("\nProgram aborted.\n"); |
| 124 |
} |
| 125 |
|
| 126 |
abort(); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/* Exit in case of error termination. If backtracing is enabled, print |
| 131 |
backtrace, then exit. */ |
| 132 |
|
| 133 |
void |
| 134 |
exit_error (int status) |
| 135 |
{ |
| 136 |
if (options.backtrace == 1 |
| 137 |
|| (options.backtrace == -1 && compile_options.backtrace == 1)) |
| 138 |
{ |
| 139 |
estr_write ("\nError termination.\n"); |
| 140 |
} |
| 141 |
exit (status); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/* show_locus()-- Print a line number and filename describing where |
| 146 |
* something went wrong */ |
| 147 |
|
| 148 |
void |
| 149 |
show_locus (st_parameter_common *cmp) |
| 150 |
{ |
| 151 |
char *filename; |
| 152 |
|
| 153 |
if (!options.locus || cmp == NULL || cmp->filename == NULL) |
| 154 |
return; |
| 155 |
|
| 156 |
if (cmp->unit > 0) |
| 157 |
{ |
| 158 |
filename = /* TODO filename_from_unit (cmp->unit) */ NULL; |
| 159 |
|
| 160 |
if (filename != NULL) |
| 161 |
{ |
| 162 |
st_printf ("At line %d of file %s (unit = %d, file = '%s')\n", |
| 163 |
(int) cmp->line, cmp->filename, (int) cmp->unit, filename); |
| 164 |
free (filename); |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
st_printf ("At line %d of file %s (unit = %d)\n", |
| 169 |
(int) cmp->line, cmp->filename, (int) cmp->unit); |
| 170 |
} |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
st_printf ("At line %d of file %s\n", (int) cmp->line, cmp->filename); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/* recursion_check()-- It's possible for additional errors to occur |
| 179 |
* during fatal error processing. We detect this condition here and |
| 180 |
* exit with code 4 immediately. */ |
| 181 |
|
| 182 |
#define MAGIC 0x20DE8101 |
| 183 |
|
| 184 |
static void |
| 185 |
recursion_check (void) |
| 186 |
{ |
| 187 |
static int magic = 0; |
| 188 |
|
| 189 |
/* Don't even try to print something at this point */ |
| 190 |
if (magic == MAGIC) |
| 191 |
sys_abort (); |
| 192 |
|
| 193 |
magic = MAGIC; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/* os_error()-- Operating system error. We get a message from the |
| 198 |
* operating system, show it and leave. Some operating system errors |
| 199 |
* are caught and processed by the library. If not, we come here. */ |
| 200 |
|
| 201 |
void |
| 202 |
os_error (const char *message) |
| 203 |
{ |
| 204 |
recursion_check (); |
| 205 |
estr_write ("Operating system error: "); |
| 206 |
estr_write (message); |
| 207 |
estr_write ("\n"); |
| 208 |
exit_error (1); |
| 209 |
} |
| 210 |
iexport(os_error); /* TODO, DEPRECATED, ABI: Should not be exported |
| 211 |
anymore when bumping so version. */ |
| 212 |
|
| 213 |
|
| 214 |
/* Improved version of os_error with a printf style format string and |
| 215 |
a locus. */ |
| 216 |
|
| 217 |
void |
| 218 |
os_error_at (const char *where, const char *message, ...) |
| 219 |
{ |
| 220 |
va_list ap; |
| 221 |
|
| 222 |
recursion_check (); |
| 223 |
estr_write (where); |
| 224 |
estr_write (": "); |
| 225 |
va_start (ap, message); |
| 226 |
estr_vprintf (message, ap); |
| 227 |
va_end (ap); |
| 228 |
estr_write ("\n"); |
| 229 |
exit_error (1); |
| 230 |
} |
| 231 |
iexport(os_error_at); |
| 232 |
|
| 233 |
|
| 234 |
/* void runtime_error()-- These are errors associated with an |
| 235 |
* invalid fortran program. */ |
| 236 |
|
| 237 |
void |
| 238 |
runtime_error (const char *message, ...) |
| 239 |
{ |
| 240 |
va_list ap; |
| 241 |
|
| 242 |
recursion_check (); |
| 243 |
estr_write ("Fortran runtime error: "); |
| 244 |
va_start (ap, message); |
| 245 |
estr_vprintf (message, ap); |
| 246 |
va_end (ap); |
| 247 |
estr_write ("\n"); |
| 248 |
exit_error (2); |
| 249 |
} |
| 250 |
iexport(runtime_error); |
| 251 |
|
| 252 |
/* void runtime_error_at()-- These are errors associated with a |
| 253 |
* run time error generated by the front end compiler. */ |
| 254 |
|
| 255 |
void |
| 256 |
runtime_error_at (const char *where, const char *message, ...) |
| 257 |
{ |
| 258 |
va_list ap; |
| 259 |
|
| 260 |
recursion_check (); |
| 261 |
estr_write (where); |
| 262 |
estr_write ("\nFortran runtime error: "); |
| 263 |
va_start (ap, message); |
| 264 |
estr_vprintf (message, ap); |
| 265 |
va_end (ap); |
| 266 |
estr_write ("\n"); |
| 267 |
exit_error (2); |
| 268 |
} |
| 269 |
iexport(runtime_error_at); |
| 270 |
|
| 271 |
|
| 272 |
void |
| 273 |
runtime_warning_at (const char *where, const char *message, ...) |
| 274 |
{ |
| 275 |
va_list ap; |
| 276 |
|
| 277 |
estr_write (where); |
| 278 |
estr_write ("\nFortran runtime warning: "); |
| 279 |
va_start (ap, message); |
| 280 |
estr_vprintf (message, ap); |
| 281 |
va_end (ap); |
| 282 |
estr_write ("\n"); |
| 283 |
} |
| 284 |
iexport(runtime_warning_at); |
| 285 |
|
| 286 |
|
| 287 |
/* void internal_error()-- These are this-can't-happen errors |
| 288 |
* that indicate something deeply wrong. */ |
| 289 |
|
| 290 |
void |
| 291 |
internal_error (st_parameter_common *cmp, const char *message) |
| 292 |
{ |
| 293 |
recursion_check (); |
| 294 |
show_locus (cmp); |
| 295 |
estr_write ("Internal Error: "); |
| 296 |
estr_write (message); |
| 297 |
estr_write ("\n"); |
| 298 |
|
| 299 |
/* This function call is here to get the main.o object file included |
| 300 |
when linking statically. This works because error.o is supposed to |
| 301 |
be always linked in (and the function call is in internal_error |
| 302 |
because hopefully it doesn't happen too often). */ |
| 303 |
stupid_function_name_for_static_linking(); |
| 304 |
|
| 305 |
exit_error (3); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
/* runtime/stop.c */ |
| 310 |
|
| 311 |
#undef report_exception |
| 312 |
#define report_exception() do {} while (0) |
| 313 |
|
| 314 |
|
| 315 |
/* A numeric STOP statement. */ |
| 316 |
|
| 317 |
extern _Noreturn void stop_numeric (int, bool); |
| 318 |
export_proto(stop_numeric); |
| 319 |
|
| 320 |
void |
| 321 |
stop_numeric (int code, bool quiet) |
| 322 |
{ |
| 323 |
if (!quiet) |
| 324 |
{ |
| 325 |
report_exception (); |
| 326 |
st_printf ("STOP %d\n", code); |
| 327 |
} |
| 328 |
exit (code); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
/* A character string or blank STOP statement. */ |
| 333 |
|
| 334 |
void |
| 335 |
stop_string (const char *string, size_t len, bool quiet) |
| 336 |
{ |
| 337 |
if (!quiet) |
| 338 |
{ |
| 339 |
report_exception (); |
| 340 |
if (string) |
| 341 |
{ |
| 342 |
estr_write ("STOP "); |
| 343 |
(void) write (STDERR_FILENO, string, len); |
| 344 |
estr_write ("\n"); |
| 345 |
} |
| 346 |
} |
| 347 |
exit (0); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/* Per Fortran 2008, section 8.4: "Execution of a STOP statement initiates |
| 352 |
normal termination of execution. Execution of an ERROR STOP statement |
| 353 |
initiates error termination of execution." Thus, error_stop_string returns |
| 354 |
a nonzero exit status code. */ |
| 355 |
|
| 356 |
extern _Noreturn void error_stop_string (const char *, size_t, bool); |
| 357 |
export_proto(error_stop_string); |
| 358 |
|
| 359 |
void |
| 360 |
error_stop_string (const char *string, size_t len, bool quiet) |
| 361 |
{ |
| 362 |
if (!quiet) |
| 363 |
{ |
| 364 |
report_exception (); |
| 365 |
estr_write ("ERROR STOP "); |
| 366 |
(void) write (STDERR_FILENO, string, len); |
| 367 |
estr_write ("\n"); |
| 368 |
} |
| 369 |
exit_error (1); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
/* A numeric ERROR STOP statement. */ |
| 374 |
|
| 375 |
extern _Noreturn void error_stop_numeric (int, bool); |
| 376 |
export_proto(error_stop_numeric); |
| 377 |
|
| 378 |
void |
| 379 |
error_stop_numeric (int code, bool quiet) |
| 380 |
{ |
| 381 |
if (!quiet) |
| 382 |
{ |
| 383 |
report_exception (); |
| 384 |
st_printf ("ERROR STOP %d\n", code); |
| 385 |
} |
| 386 |
exit_error (code); |
| 387 |
} |