1 /* Copyright (C) 2002-2018 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"
39 #ifdef HAVE_SYS_TIME_H
43 /* <sys/time.h> has to be included before <sys/resource.h> to work
44 around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
63 /* Termination of a program: F2008 2.3.5 talks about "normal
64 termination" and "error termination". Normal termination occurs as
65 a result of e.g. executing the end program statement, and executing
66 the STOP statement. It includes the effect of the C exit()
69 Error termination is initiated when the ERROR STOP statement is
70 executed, when ALLOCATE/DEALLOCATE fails without STAT= being
71 specified, when some of the co-array synchronization statements
72 fail without STAT= being specified, and some I/O errors if
73 ERR/IOSTAT/END/EOR is not present, and finally EXECUTE_COMMAND_LINE
74 failure without CMDSTAT=.
76 2.3.5 also explains how co-images synchronize during termination.
78 In libgfortran we have three ways of ending a program. exit(code)
79 is a normal exit; calling exit() also causes open units to be
80 closed. No backtrace or core dump is needed here. For error
81 termination, we have exit_error(status), which prints a backtrace
82 if backtracing is enabled, then exits. Finally, when something
83 goes terribly wrong, we have sys_abort() which tries to print the
84 backtrace if -fbacktrace is enabled, and then dumps core; whether a
85 core file is generated is system dependent. When aborting, we don't
86 flush and close open units, as program memory might be corrupted
87 and we'd rather risk losing dirty data in the buffers rather than
88 corrupting files on disk.
92 /* Error conditions. The tricky part here is printing a message when
93 * it is the I/O subsystem that is severely wounded. Our goal is to
94 * try and print something making the fewest assumptions possible,
95 * then try to clean up before actually exiting.
97 * The following exit conditions are defined:
98 * 0 Normal program exit.
99 * 1 Terminated because of operating system error.
100 * 2 Error in the runtime library
101 * 3 Internal error in runtime library
103 * Other error returns are reserved for the STOP statement with a numeric code.
107 /* Write a null-terminated C string to standard error. This function
108 is async-signal-safe. */
111 estr_write (const char *str
)
113 return write (STDERR_FILENO
, str
, strlen (str
));
117 /* Write a vector of strings to standard error. This function is
118 async-signal-safe. */
121 estr_writev (const struct iovec
*iov
, int iovcnt
)
124 return writev (STDERR_FILENO
, iov
, iovcnt
);
127 for (int i
= 0; i
< iovcnt
; i
++)
129 ssize_t r
= write (STDERR_FILENO
, iov
[i
].iov_base
, iov
[i
].iov_len
);
139 #ifndef HAVE_VSNPRINTF
141 gf_vsnprintf (char *str
, size_t size
, const char *format
, va_list ap
)
145 written
= vsprintf(buffer
, format
, ap
);
147 if (written
>= size
- 1)
149 /* The error message was longer than our buffer. Ouch. Because
150 we may have messed up things badly, report the error and
152 #define ERROR_MESSAGE "Internal error: buffer overrun in gf_vsnprintf()\n"
153 write (STDERR_FILENO
, buffer
, size
- 1);
154 write (STDERR_FILENO
, ERROR_MESSAGE
, strlen (ERROR_MESSAGE
));
162 #define vsnprintf gf_vsnprintf
166 /* printf() like function for for printing to stderr. Uses a stack
167 allocated buffer and doesn't lock stderr, so it should be safe to
168 use from within a signal handler. */
170 #define ST_ERRBUF_SIZE 512
173 st_printf (const char * format
, ...)
175 char buffer
[ST_ERRBUF_SIZE
];
178 va_start (ap
, format
);
179 written
= vsnprintf (buffer
, ST_ERRBUF_SIZE
, format
, ap
);
181 written
= write (STDERR_FILENO
, buffer
, written
);
186 /* sys_abort()-- Terminate the program showing backtrace and dumping
192 /* If backtracing is enabled, print backtrace and disable signal
194 if (options
.backtrace
== 1
195 || (options
.backtrace
== -1 && compile_options
.backtrace
== 1))
197 estr_write ("\nProgram aborted. Backtrace:\n");
198 show_backtrace (false);
199 signal (SIGABRT
, SIG_DFL
);
206 /* Exit in case of error termination. If backtracing is enabled, print
207 backtrace, then exit. */
210 exit_error (int status
)
212 if (options
.backtrace
== 1
213 || (options
.backtrace
== -1 && compile_options
.backtrace
== 1))
215 estr_write ("\nError termination. Backtrace:\n");
216 show_backtrace (false);
223 /* gfc_xtoa()-- Integer to hexadecimal conversion. */
226 gfc_xtoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
231 assert (len
>= GFC_XTOA_BUF_SIZE
);
236 p
= buffer
+ GFC_XTOA_BUF_SIZE
- 1;
243 digit
+= 'A' - '0' - 10;
253 /* Hopefully thread-safe wrapper for a strerror() style function. */
256 gf_strerror (int errnum
,
257 char * buf
__attribute__((unused
)),
258 size_t buflen
__attribute__((unused
)))
260 #ifdef HAVE_STRERROR_L
261 locale_t myloc
= newlocale (LC_CTYPE_MASK
| LC_MESSAGES_MASK
, "",
266 p
= strerror_l (errnum
, myloc
);
270 /* newlocale might fail e.g. due to running out of memory, fall
271 back to the simpler strerror. */
272 p
= strerror (errnum
);
274 #elif defined(HAVE_STRERROR_R)
275 #ifdef HAVE_USELOCALE
276 /* Some targets (Darwin at least) have the POSIX 2008 extended
277 locale functions, but not strerror_l. So reset the per-thread
279 uselocale (LC_GLOBAL_LOCALE
);
281 /* POSIX returns an "int", GNU a "char*". */
283 __builtin_choose_expr (__builtin_classify_type (strerror_r (0, buf
, 0))
285 /* GNU strerror_r() */
286 strerror_r (errnum
, buf
, buflen
),
287 /* POSIX strerror_r () */
288 (strerror_r (errnum
, buf
, buflen
), buf
));
289 #elif defined(HAVE_STRERROR_R_2ARGS)
290 strerror_r (errnum
, buf
);
293 /* strerror () is not necessarily thread-safe, but should at least
294 be available everywhere. */
295 return strerror (errnum
);
300 /* show_locus()-- Print a line number and filename describing where
301 * something went wrong */
304 show_locus (st_parameter_common
*cmp
)
308 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
313 filename
= filename_from_unit (cmp
->unit
);
315 if (filename
!= NULL
)
317 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
318 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
, filename
);
323 st_printf ("At line %d of file %s (unit = %d)\n",
324 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
);
329 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
333 /* recursion_check()-- It's possible for additional errors to occur
334 * during fatal error processing. We detect this condition here and
335 * exit with code 4 immediately. */
337 #define MAGIC 0x20DE8101
340 recursion_check (void)
342 static int magic
= 0;
344 /* Don't even try to print something at this point */
352 #define STRERR_MAXSZ 256
354 /* os_error()-- Operating system error. We get a message from the
355 * operating system, show it and leave. Some operating system errors
356 * are caught and processed by the library. If not, we come here. */
359 os_error (const char *message
)
361 char errmsg
[STRERR_MAXSZ
];
364 iov
[0].iov_base
= (char*) "Operating system error: ";
365 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
366 iov
[1].iov_base
= gf_strerror (errno
, errmsg
, STRERR_MAXSZ
);
367 iov
[1].iov_len
= strlen (iov
[1].iov_base
);
368 iov
[2].iov_base
= (char*) "\n";
370 iov
[3].iov_base
= (char*) message
;
371 iov
[3].iov_len
= strlen (message
);
372 iov
[4].iov_base
= (char*) "\n";
374 estr_writev (iov
, 5);
380 /* void runtime_error()-- These are errors associated with an
381 * invalid fortran program. */
384 runtime_error (const char *message
, ...)
386 char buffer
[ST_ERRBUF_SIZE
];
392 iov
[0].iov_base
= (char*) "Fortran runtime error: ";
393 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
394 va_start (ap
, message
);
395 written
= vsnprintf (buffer
, ST_ERRBUF_SIZE
, message
, ap
);
399 iov
[1].iov_base
= buffer
;
400 iov
[1].iov_len
= written
;
401 iov
[2].iov_base
= (char*) "\n";
403 estr_writev (iov
, 3);
407 iexport(runtime_error
);
409 /* void runtime_error_at()-- These are errors associated with a
410 * run time error generated by the front end compiler. */
413 runtime_error_at (const char *where
, const char *message
, ...)
415 char buffer
[ST_ERRBUF_SIZE
];
421 iov
[0].iov_base
= (char*) where
;
422 iov
[0].iov_len
= strlen (where
);
423 iov
[1].iov_base
= (char*) "\nFortran runtime error: ";
424 iov
[1].iov_len
= strlen (iov
[1].iov_base
);
425 va_start (ap
, message
);
426 written
= vsnprintf (buffer
, ST_ERRBUF_SIZE
, message
, ap
);
430 iov
[2].iov_base
= buffer
;
431 iov
[2].iov_len
= written
;
432 iov
[3].iov_base
= (char*) "\n";
434 estr_writev (iov
, 4);
438 iexport(runtime_error_at
);
442 runtime_warning_at (const char *where
, const char *message
, ...)
444 char buffer
[ST_ERRBUF_SIZE
];
449 iov
[0].iov_base
= (char*) where
;
450 iov
[0].iov_len
= strlen (where
);
451 iov
[1].iov_base
= (char*) "\nFortran runtime warning: ";
452 iov
[1].iov_len
= strlen (iov
[1].iov_base
);
453 va_start (ap
, message
);
454 written
= vsnprintf (buffer
, ST_ERRBUF_SIZE
, message
, ap
);
458 iov
[2].iov_base
= buffer
;
459 iov
[2].iov_len
= written
;
460 iov
[3].iov_base
= (char*) "\n";
462 estr_writev (iov
, 4);
465 iexport(runtime_warning_at
);
468 /* void internal_error()-- These are this-can't-happen errors
469 * that indicate something deeply wrong. */
472 internal_error (st_parameter_common
*cmp
, const char *message
)
478 iov
[0].iov_base
= (char*) "Internal Error: ";
479 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
480 iov
[1].iov_base
= (char*) message
;
481 iov
[1].iov_len
= strlen (message
);
482 iov
[2].iov_base
= (char*) "\n";
484 estr_writev (iov
, 3);
486 /* This function call is here to get the main.o object file included
487 when linking statically. This works because error.o is supposed to
488 be always linked in (and the function call is in internal_error
489 because hopefully it doesn't happen too often). */
490 stupid_function_name_for_static_linking();
496 /* translate_error()-- Given an integer error code, return a string
497 * describing the error. */
500 translate_error (int code
)
515 p
= "Successful return";
519 p
= "Operating system error";
522 case LIBERROR_BAD_OPTION
:
523 p
= "Bad statement option";
526 case LIBERROR_MISSING_OPTION
:
527 p
= "Missing statement option";
530 case LIBERROR_OPTION_CONFLICT
:
531 p
= "Conflicting statement options";
534 case LIBERROR_ALREADY_OPEN
:
535 p
= "File already opened in another unit";
538 case LIBERROR_BAD_UNIT
:
539 p
= "Unattached unit";
542 case LIBERROR_FORMAT
:
546 case LIBERROR_BAD_ACTION
:
547 p
= "Incorrect ACTION specified";
550 case LIBERROR_ENDFILE
:
551 p
= "Read past ENDFILE record";
554 case LIBERROR_BAD_US
:
555 p
= "Corrupt unformatted sequential file";
558 case LIBERROR_READ_VALUE
:
559 p
= "Bad value during read";
562 case LIBERROR_READ_OVERFLOW
:
563 p
= "Numeric overflow on read";
566 case LIBERROR_INTERNAL
:
567 p
= "Internal error in run-time library";
570 case LIBERROR_INTERNAL_UNIT
:
571 p
= "Internal unit I/O error";
574 case LIBERROR_DIRECT_EOR
:
575 p
= "Write exceeds length of DIRECT access record";
578 case LIBERROR_SHORT_RECORD
:
579 p
= "I/O past end of record on unformatted file";
582 case LIBERROR_CORRUPT_FILE
:
583 p
= "Unformatted file structure has been corrupted";
586 case LIBERROR_INQUIRE_INTERNAL_UNIT
:
587 p
= "Inquire statement identifies an internal file";
591 p
= "Unknown error code";
599 /* Worker function for generate_error and generate_error_async. Return true
600 if a straight return is to be done, zero if the program should abort. */
603 generate_error_common (st_parameter_common
*cmp
, int family
, const char *message
)
605 char errmsg
[STRERR_MAXSZ
];
610 NOTE ("Entering generate_error_common");
615 if (u
->au
->error
.has_error
)
618 if (__gthread_equal (u
->au
->thread
, __gthread_self ()))
620 u
->au
->error
.has_error
= 1;
621 u
->au
->error
.cmp
= cmp
;
622 u
->au
->error
.family
= family
;
623 u
->au
->error
.message
= message
;
629 /* If there was a previous error, don't mask it with another
630 error message, EOF or EOR condition. */
632 if ((cmp
->flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_ERROR
)
635 /* Set the error status. */
636 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
637 *cmp
->iostat
= (family
== LIBERROR_OS
) ? errno
: family
;
641 (family
== LIBERROR_OS
) ? gf_strerror (errno
, errmsg
, STRERR_MAXSZ
) :
642 translate_error (family
);
644 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
645 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
647 /* Report status back to the compiler. */
648 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
652 cmp
->flags
|= IOPARM_LIBRETURN_EOR
; NOTE("EOR");
653 if ((cmp
->flags
& IOPARM_EOR
))
658 cmp
->flags
|= IOPARM_LIBRETURN_END
; NOTE("END");
659 if ((cmp
->flags
& IOPARM_END
))
664 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
; NOTE("ERROR");
665 if ((cmp
->flags
& IOPARM_ERR
))
670 /* Return if the user supplied an iostat variable. */
671 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
674 /* Return code, caller is responsible for terminating
675 the program if necessary. */
680 iov
[0].iov_base
= (char*) "Fortran runtime error: ";
681 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
682 iov
[1].iov_base
= (char*) message
;
683 iov
[1].iov_len
= strlen (message
);
684 iov
[2].iov_base
= (char*) "\n";
686 estr_writev (iov
, 3);
690 /* generate_error()-- Come here when an error happens. This
691 * subroutine is called if it is possible to continue on after the error.
692 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
693 * ERR labels are present, we return, otherwise we terminate the program
694 * after printing a message. The error code is always required but the
695 * message parameter can be NULL, in which case a string describing
696 * the most recent operating system error is used.
697 * If the error is for an asynchronous unit and if the program is currently
698 * executing the asynchronous thread, just mark the error and return. */
701 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
703 if (generate_error_common (cmp
, family
, message
))
708 iexport(generate_error
);
711 /* generate_warning()-- Similar to generate_error but just give a warning. */
714 generate_warning (st_parameter_common
*cmp
, const char *message
)
721 iov
[0].iov_base
= (char*) "Fortran runtime warning: ";
722 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
723 iov
[1].iov_base
= (char*) message
;
724 iov
[1].iov_len
= strlen (message
);
725 iov
[2].iov_base
= (char*) "\n";
727 estr_writev (iov
, 3);
731 /* Whether, for a feature included in a given standard set (GFC_STD_*),
732 we should issue an error or a warning, or be quiet. */
735 notification_std (int std
)
739 if (!compile_options
.pedantic
)
740 return NOTIFICATION_SILENT
;
742 warning
= compile_options
.warn_std
& std
;
743 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
744 return NOTIFICATION_SILENT
;
746 return warning
? NOTIFICATION_WARNING
: NOTIFICATION_ERROR
;
750 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
751 feature. An error/warning will be issued if the currently selected
752 standard does not contain the requested bits. */
755 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
760 if (!compile_options
.pedantic
)
763 warning
= compile_options
.warn_std
& std
;
764 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
771 iov
[0].iov_base
= (char*) "Fortran runtime error: ";
772 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
773 iov
[1].iov_base
= (char*) message
;
774 iov
[1].iov_len
= strlen (message
);
775 iov
[2].iov_base
= (char*) "\n";
777 estr_writev (iov
, 3);
783 iov
[0].iov_base
= (char*) "Fortran runtime warning: ";
784 iov
[0].iov_len
= strlen (iov
[0].iov_base
);
785 iov
[1].iov_base
= (char*) message
;
786 iov
[1].iov_len
= strlen (message
);
787 iov
[2].iov_base
= (char*) "\n";
789 estr_writev (iov
, 3);