1 /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran runtime library (libgfortran).
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)
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.
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.
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/>. */
26 #include "libgfortran.h"
38 #ifdef HAVE_SYS_TIME_H
42 /* <sys/time.h> has to be included before <sys/resource.h> to work
43 around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
44 #ifdef HAVE_SYS_RESOURCE_H
45 #include <sys/resource.h>
62 /* Termination of a program: F2008 2.3.5 talks about "normal
63 termination" and "error termination". Normal termination occurs as
64 a result of e.g. executing the end program statement, and executing
65 the STOP statement. It includes the effect of the C exit()
68 Error termination is initiated when the ERROR STOP statement is
69 executed, when ALLOCATE/DEALLOCATE fails without STAT= being
70 specified, when some of the co-array synchronization statements
71 fail without STAT= being specified, and some I/O errors if
72 ERR/IOSTAT/END/EOR is not present, and finally EXECUTE_COMMAND_LINE
73 failure without CMDSTAT=.
75 2.3.5 also explains how co-images synchronize during termination.
77 In libgfortran we have two ways of ending a program. exit(code) is
78 a normal exit; calling exit() also causes open units to be
79 closed. No backtrace or core dump is needed here. When something
80 goes wrong, we have sys_abort() which tries to print the backtrace
81 if -fbacktrace is enabled, and then dumps core; whether a core file
82 is generated is system dependent. When aborting, we don't flush and
83 close open units, as program memory might be corrupted and we'd
84 rather risk losing dirty data in the buffers rather than corrupting
89 /* Error conditions. The tricky part here is printing a message when
90 * it is the I/O subsystem that is severely wounded. Our goal is to
91 * try and print something making the fewest assumptions possible,
92 * then try to clean up before actually exiting.
94 * The following exit conditions are defined:
95 * 0 Normal program exit.
96 * 1 Terminated because of operating system error.
97 * 2 Error in the runtime library
98 * 3 Internal error in runtime library
100 * Other error returns are reserved for the STOP statement with a numeric code.
104 /* Write a null-terminated C string to standard error. This function
105 is async-signal-safe. */
108 estr_write (const char *str
)
110 return write (STDERR_FILENO
, str
, strlen (str
));
114 /* st_vprintf()-- vsnprintf-like function for error output. We use a
115 stack allocated buffer for formatting; since this function might be
116 called from within a signal handler, printing directly to stderr
117 with vfprintf is not safe since the stderr locking might lead to a
120 #define ST_VPRINTF_SIZE 512
123 st_vprintf (const char *format
, va_list ap
)
126 char buffer
[ST_VPRINTF_SIZE
];
128 #ifdef HAVE_VSNPRINTF
129 written
= vsnprintf(buffer
, ST_VPRINTF_SIZE
, format
, ap
);
131 written
= vsprintf(buffer
, format
, ap
);
133 if (written
>= ST_VPRINTF_SIZE
- 1)
135 /* The error message was longer than our buffer. Ouch. Because
136 we may have messed up things badly, report the error and
138 #define ERROR_MESSAGE "Internal error: buffer overrun in st_vprintf()\n"
139 write (STDERR_FILENO
, buffer
, ST_VPRINTF_SIZE
- 1);
140 write (STDERR_FILENO
, ERROR_MESSAGE
, strlen(ERROR_MESSAGE
));
147 written
= write (STDERR_FILENO
, buffer
, written
);
153 st_printf (const char * format
, ...)
157 va_start (ap
, format
);
158 written
= st_vprintf (format
, ap
);
164 /* sys_abort()-- Terminate the program showing backtrace and dumping
170 /* If backtracing is enabled, print backtrace and disable signal
172 if (options
.backtrace
== 1
173 || (options
.backtrace
== -1 && compile_options
.backtrace
== 1))
175 estr_write ("\nProgram aborted. Backtrace:\n");
176 show_backtrace (false);
177 signal (SIGABRT
, SIG_DFL
);
184 /* gfc_xtoa()-- Integer to hexadecimal conversion. */
187 gfc_xtoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
192 assert (len
>= GFC_XTOA_BUF_SIZE
);
197 p
= buffer
+ GFC_XTOA_BUF_SIZE
- 1;
204 digit
+= 'A' - '0' - 10;
214 /* Hopefully thread-safe wrapper for a strerror() style function. */
217 gf_strerror (int errnum
,
218 char * buf
__attribute__((unused
)),
219 size_t buflen
__attribute__((unused
)))
221 #ifdef HAVE_STRERROR_L
222 locale_t myloc
= newlocale (LC_CTYPE_MASK
| LC_MESSAGES_MASK
, "",
227 p
= strerror_l (errnum
, myloc
);
231 /* newlocale might fail e.g. due to running out of memory, fall
232 back to the simpler strerror. */
233 p
= strerror (errnum
);
235 #elif defined(HAVE_STRERROR_R)
236 #ifdef HAVE_USELOCALE
237 /* Some targets (Darwin at least) have the POSIX 2008 extended
238 locale functions, but not strerror_l. So reset the per-thread
240 uselocale (LC_GLOBAL_LOCALE
);
242 /* POSIX returns an "int", GNU a "char*". */
244 __builtin_choose_expr (__builtin_classify_type (strerror_r (0, buf
, 0))
246 /* GNU strerror_r() */
247 strerror_r (errnum
, buf
, buflen
),
248 /* POSIX strerror_r () */
249 (strerror_r (errnum
, buf
, buflen
), buf
));
250 #elif defined(HAVE_STRERROR_R_2ARGS)
251 strerror_r (errnum
, buf
);
254 /* strerror () is not necessarily thread-safe, but should at least
255 be available everywhere. */
256 return strerror (errnum
);
261 /* show_locus()-- Print a line number and filename describing where
262 * something went wrong */
265 show_locus (st_parameter_common
*cmp
)
269 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
274 filename
= filename_from_unit (cmp
->unit
);
276 if (filename
!= NULL
)
278 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
279 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
, filename
);
284 st_printf ("At line %d of file %s (unit = %d)\n",
285 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
);
290 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
294 /* recursion_check()-- It's possible for additional errors to occur
295 * during fatal error processing. We detect this condition here and
296 * exit with code 4 immediately. */
298 #define MAGIC 0x20DE8101
301 recursion_check (void)
303 static int magic
= 0;
305 /* Don't even try to print something at this point */
313 #define STRERR_MAXSZ 256
315 /* os_error()-- Operating system error. We get a message from the
316 * operating system, show it and leave. Some operating system errors
317 * are caught and processed by the library. If not, we come here. */
320 os_error (const char *message
)
322 char errmsg
[STRERR_MAXSZ
];
324 estr_write ("Operating system error: ");
325 estr_write (gf_strerror (errno
, errmsg
, STRERR_MAXSZ
));
327 estr_write (message
);
334 /* void runtime_error()-- These are errors associated with an
335 * invalid fortran program. */
338 runtime_error (const char *message
, ...)
343 estr_write ("Fortran runtime error: ");
344 va_start (ap
, message
);
345 st_vprintf (message
, ap
);
350 iexport(runtime_error
);
352 /* void runtime_error_at()-- These are errors associated with a
353 * run time error generated by the front end compiler. */
356 runtime_error_at (const char *where
, const char *message
, ...)
362 estr_write ("\nFortran runtime error: ");
363 va_start (ap
, message
);
364 st_vprintf (message
, ap
);
369 iexport(runtime_error_at
);
373 runtime_warning_at (const char *where
, const char *message
, ...)
378 estr_write ("\nFortran runtime warning: ");
379 va_start (ap
, message
);
380 st_vprintf (message
, ap
);
384 iexport(runtime_warning_at
);
387 /* void internal_error()-- These are this-can't-happen errors
388 * that indicate something deeply wrong. */
391 internal_error (st_parameter_common
*cmp
, const char *message
)
395 estr_write ("Internal Error: ");
396 estr_write (message
);
399 /* This function call is here to get the main.o object file included
400 when linking statically. This works because error.o is supposed to
401 be always linked in (and the function call is in internal_error
402 because hopefully it doesn't happen too often). */
403 stupid_function_name_for_static_linking();
409 /* translate_error()-- Given an integer error code, return a string
410 * describing the error. */
413 translate_error (int code
)
428 p
= "Successful return";
432 p
= "Operating system error";
435 case LIBERROR_BAD_OPTION
:
436 p
= "Bad statement option";
439 case LIBERROR_MISSING_OPTION
:
440 p
= "Missing statement option";
443 case LIBERROR_OPTION_CONFLICT
:
444 p
= "Conflicting statement options";
447 case LIBERROR_ALREADY_OPEN
:
448 p
= "File already opened in another unit";
451 case LIBERROR_BAD_UNIT
:
452 p
= "Unattached unit";
455 case LIBERROR_FORMAT
:
459 case LIBERROR_BAD_ACTION
:
460 p
= "Incorrect ACTION specified";
463 case LIBERROR_ENDFILE
:
464 p
= "Read past ENDFILE record";
467 case LIBERROR_BAD_US
:
468 p
= "Corrupt unformatted sequential file";
471 case LIBERROR_READ_VALUE
:
472 p
= "Bad value during read";
475 case LIBERROR_READ_OVERFLOW
:
476 p
= "Numeric overflow on read";
479 case LIBERROR_INTERNAL
:
480 p
= "Internal error in run-time library";
483 case LIBERROR_INTERNAL_UNIT
:
484 p
= "Internal unit I/O error";
487 case LIBERROR_DIRECT_EOR
:
488 p
= "Write exceeds length of DIRECT access record";
491 case LIBERROR_SHORT_RECORD
:
492 p
= "I/O past end of record on unformatted file";
495 case LIBERROR_CORRUPT_FILE
:
496 p
= "Unformatted file structure has been corrupted";
499 case LIBERROR_INQUIRE_INTERNAL_UNIT
:
500 p
= "Inquire statement identifies an internal file";
504 p
= "Unknown error code";
512 /* generate_error()-- Come here when an error happens. This
513 * subroutine is called if it is possible to continue on after the error.
514 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
515 * ERR labels are present, we return, otherwise we terminate the program
516 * after printing a message. The error code is always required but the
517 * message parameter can be NULL, in which case a string describing
518 * the most recent operating system error is used. */
521 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
523 char errmsg
[STRERR_MAXSZ
];
525 /* If there was a previous error, don't mask it with another
526 error message, EOF or EOR condition. */
528 if ((cmp
->flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_ERROR
)
531 /* Set the error status. */
532 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
533 *cmp
->iostat
= (family
== LIBERROR_OS
) ? errno
: family
;
537 (family
== LIBERROR_OS
) ? gf_strerror (errno
, errmsg
, STRERR_MAXSZ
) :
538 translate_error (family
);
540 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
541 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
543 /* Report status back to the compiler. */
544 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
548 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
549 if ((cmp
->flags
& IOPARM_EOR
))
554 cmp
->flags
|= IOPARM_LIBRETURN_END
;
555 if ((cmp
->flags
& IOPARM_END
))
560 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
561 if ((cmp
->flags
& IOPARM_ERR
))
566 /* Return if the user supplied an iostat variable. */
567 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
570 /* Terminate the program */
574 estr_write ("Fortran runtime error: ");
575 estr_write (message
);
579 iexport(generate_error
);
582 /* generate_warning()-- Similar to generate_error but just give a warning. */
585 generate_warning (st_parameter_common
*cmp
, const char *message
)
591 estr_write ("Fortran runtime warning: ");
592 estr_write (message
);
597 /* Whether, for a feature included in a given standard set (GFC_STD_*),
598 we should issue an error or a warning, or be quiet. */
601 notification_std (int std
)
605 if (!compile_options
.pedantic
)
606 return NOTIFICATION_SILENT
;
608 warning
= compile_options
.warn_std
& std
;
609 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
610 return NOTIFICATION_SILENT
;
612 return warning
? NOTIFICATION_WARNING
: NOTIFICATION_ERROR
;
616 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
617 feature. An error/warning will be issued if the currently selected
618 standard does not contain the requested bits. */
621 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
625 if (!compile_options
.pedantic
)
628 warning
= compile_options
.warn_std
& std
;
629 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
636 estr_write ("Fortran runtime error: ");
637 estr_write (message
);
644 estr_write ("Fortran runtime warning: ");
645 estr_write (message
);