1 /* Copyright (C) 2002, 2003, 2005, 2006, 2007, 2009, 2010, 2011
2 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
44 #ifdef HAVE_SYS_TIME_H
48 /* <sys/time.h> has to be included before <sys/resource.h> to work
49 around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
50 #ifdef HAVE_SYS_RESOURCE_H
51 #include <sys/resource.h>
61 /* Termination of a program: F2008 2.3.5 talks about "normal
62 termination" and "error termination". Normal termination occurs as
63 a result of e.g. executing the end program statement, and executing
64 the STOP statement. It includes the effect of the C exit()
67 Error termination is initiated when the ERROR STOP statement is
68 executed, when ALLOCATE/DEALLOCATE fails without STAT= being
69 specified, when some of the co-array synchronization statements
70 fail without STAT= being specified, and some I/O errors if
71 ERR/IOSTAT/END/EOR is not present, and finally EXECUTE_COMMAND_LINE
72 failure without CMDSTAT=.
74 2.3.5 also explains how co-images synchronize during termination.
76 In libgfortran we have two ways of ending a program. exit(code) is
77 a normal exit; calling exit() also causes open units to be
78 closed. No backtrace or core dump is needed here. When something
79 goes wrong, we have sys_abort() which tries to print the backtrace
80 if -fbacktrace is enabled, and then dumps core; whether a core file
81 is generated is system dependent. When aborting, we don't flush and
82 close open units, as program memory might be corrupted and we'd
83 rather risk losing dirty data in the buffers rather than corrupting
88 /* Error conditions. The tricky part here is printing a message when
89 * it is the I/O subsystem that is severely wounded. Our goal is to
90 * try and print something making the fewest assumptions possible,
91 * then try to clean up before actually exiting.
93 * The following exit conditions are defined:
94 * 0 Normal program exit.
95 * 1 Terminated because of operating system error.
96 * 2 Error in the runtime library
97 * 3 Internal error in runtime library
99 * Other error returns are reserved for the STOP statement with a numeric code.
103 /* Write a null-terminated C string to standard error. This function
104 is async-signal-safe. */
107 estr_write (const char *str
)
109 return write (STDERR_FILENO
, str
, strlen (str
));
113 /* st_vprintf()-- vsnprintf-like function for error output. We use a
114 stack allocated buffer for formatting; since this function might be
115 called from within a signal handler, printing directly to stderr
116 with vfprintf is not safe since the stderr locking might lead to a
119 #define ST_VPRINTF_SIZE 512
122 st_vprintf (const char *format
, va_list ap
)
125 char buffer
[ST_VPRINTF_SIZE
];
127 #ifdef HAVE_VSNPRINTF
128 written
= vsnprintf(buffer
, ST_VPRINTF_SIZE
, format
, ap
);
130 written
= vsprintf(buffer
, format
, ap
);
132 if (written
>= ST_VPRINTF_SIZE
- 1)
134 /* The error message was longer than our buffer. Ouch. Because
135 we may have messed up things badly, report the error and
137 #define ERROR_MESSAGE "Internal error: buffer overrun in st_vprintf()\n"
138 write (STDERR_FILENO
, buffer
, ST_VPRINTF_SIZE
- 1);
139 write (STDERR_FILENO
, ERROR_MESSAGE
, strlen(ERROR_MESSAGE
));
146 written
= write (STDERR_FILENO
, buffer
, written
);
152 st_printf (const char * format
, ...)
156 va_start (ap
, format
);
157 written
= st_vprintf (format
, ap
);
163 /* sys_abort()-- Terminate the program showing backtrace and dumping
169 /* If backtracing is enabled, print backtrace and disable signal
171 if (options
.backtrace
== 1
172 || (options
.backtrace
== -1 && compile_options
.backtrace
== 1))
175 #if defined(HAVE_SIGNAL) && defined(SIGABRT)
176 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_r() style function. */
217 gf_strerror (int errnum
,
218 char * buf
__attribute__((unused
)),
219 size_t buflen
__attribute__((unused
)))
221 #ifdef HAVE_STRERROR_R
222 /* TODO: How to prevent the compiler warning due to strerror_r of
223 the untaken branch having the wrong return type? */
224 if (__builtin_classify_type (strerror_r (0, buf
, 0)) == 5)
226 /* GNU strerror_r() */
227 return strerror_r (errnum
, buf
, buflen
);
231 /* POSIX strerror_r () */
232 strerror_r (errnum
, buf
, buflen
);
236 /* strerror () is not necessarily thread-safe, but should at least
237 be available everywhere. */
238 return strerror (errnum
);
243 /* show_locus()-- Print a line number and filename describing where
244 * something went wrong */
247 show_locus (st_parameter_common
*cmp
)
251 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
256 filename
= filename_from_unit (cmp
->unit
);
258 if (filename
!= NULL
)
260 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
261 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
, filename
);
266 st_printf ("At line %d of file %s (unit = %d)\n",
267 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
);
272 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
276 /* recursion_check()-- It's possible for additional errors to occur
277 * during fatal error processing. We detect this condition here and
278 * exit with code 4 immediately. */
280 #define MAGIC 0x20DE8101
283 recursion_check (void)
285 static int magic
= 0;
287 /* Don't even try to print something at this point */
295 #define STRERR_MAXSZ 256
297 /* os_error()-- Operating system error. We get a message from the
298 * operating system, show it and leave. Some operating system errors
299 * are caught and processed by the library. If not, we come here. */
302 os_error (const char *message
)
304 char errmsg
[STRERR_MAXSZ
];
306 estr_write ("Operating system error: ");
307 estr_write (gf_strerror (errno
, errmsg
, STRERR_MAXSZ
));
309 estr_write (message
);
316 /* void runtime_error()-- These are errors associated with an
317 * invalid fortran program. */
320 runtime_error (const char *message
, ...)
325 estr_write ("Fortran runtime error: ");
326 va_start (ap
, message
);
327 st_vprintf (message
, ap
);
332 iexport(runtime_error
);
334 /* void runtime_error_at()-- These are errors associated with a
335 * run time error generated by the front end compiler. */
338 runtime_error_at (const char *where
, const char *message
, ...)
344 estr_write ("\nFortran runtime error: ");
345 va_start (ap
, message
);
346 st_vprintf (message
, ap
);
351 iexport(runtime_error_at
);
355 runtime_warning_at (const char *where
, const char *message
, ...)
360 estr_write ("\nFortran runtime warning: ");
361 va_start (ap
, message
);
362 st_vprintf (message
, ap
);
366 iexport(runtime_warning_at
);
369 /* void internal_error()-- These are this-can't-happen errors
370 * that indicate something deeply wrong. */
373 internal_error (st_parameter_common
*cmp
, const char *message
)
377 estr_write ("Internal Error: ");
378 estr_write (message
);
381 /* This function call is here to get the main.o object file included
382 when linking statically. This works because error.o is supposed to
383 be always linked in (and the function call is in internal_error
384 because hopefully it doesn't happen too often). */
385 stupid_function_name_for_static_linking();
391 /* translate_error()-- Given an integer error code, return a string
392 * describing the error. */
395 translate_error (int code
)
410 p
= "Successful return";
414 p
= "Operating system error";
417 case LIBERROR_BAD_OPTION
:
418 p
= "Bad statement option";
421 case LIBERROR_MISSING_OPTION
:
422 p
= "Missing statement option";
425 case LIBERROR_OPTION_CONFLICT
:
426 p
= "Conflicting statement options";
429 case LIBERROR_ALREADY_OPEN
:
430 p
= "File already opened in another unit";
433 case LIBERROR_BAD_UNIT
:
434 p
= "Unattached unit";
437 case LIBERROR_FORMAT
:
441 case LIBERROR_BAD_ACTION
:
442 p
= "Incorrect ACTION specified";
445 case LIBERROR_ENDFILE
:
446 p
= "Read past ENDFILE record";
449 case LIBERROR_BAD_US
:
450 p
= "Corrupt unformatted sequential file";
453 case LIBERROR_READ_VALUE
:
454 p
= "Bad value during read";
457 case LIBERROR_READ_OVERFLOW
:
458 p
= "Numeric overflow on read";
461 case LIBERROR_INTERNAL
:
462 p
= "Internal error in run-time library";
465 case LIBERROR_INTERNAL_UNIT
:
466 p
= "Internal unit I/O error";
469 case LIBERROR_DIRECT_EOR
:
470 p
= "Write exceeds length of DIRECT access record";
473 case LIBERROR_SHORT_RECORD
:
474 p
= "I/O past end of record on unformatted file";
477 case LIBERROR_CORRUPT_FILE
:
478 p
= "Unformatted file structure has been corrupted";
482 p
= "Unknown error code";
490 /* generate_error()-- Come here when an error happens. This
491 * subroutine is called if it is possible to continue on after the error.
492 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
493 * ERR labels are present, we return, otherwise we terminate the program
494 * after printing a message. The error code is always required but the
495 * message parameter can be NULL, in which case a string describing
496 * the most recent operating system error is used. */
499 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
501 char errmsg
[STRERR_MAXSZ
];
503 /* If there was a previous error, don't mask it with another
504 error message, EOF or EOR condition. */
506 if ((cmp
->flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_ERROR
)
509 /* Set the error status. */
510 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
511 *cmp
->iostat
= (family
== LIBERROR_OS
) ? errno
: family
;
515 (family
== LIBERROR_OS
) ? gf_strerror (errno
, errmsg
, STRERR_MAXSZ
) :
516 translate_error (family
);
518 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
519 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
521 /* Report status back to the compiler. */
522 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
526 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
527 if ((cmp
->flags
& IOPARM_EOR
))
532 cmp
->flags
|= IOPARM_LIBRETURN_END
;
533 if ((cmp
->flags
& IOPARM_END
))
538 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
539 if ((cmp
->flags
& IOPARM_ERR
))
544 /* Return if the user supplied an iostat variable. */
545 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
548 /* Terminate the program */
552 estr_write ("Fortran runtime error: ");
553 estr_write (message
);
557 iexport(generate_error
);
560 /* generate_warning()-- Similar to generate_error but just give a warning. */
563 generate_warning (st_parameter_common
*cmp
, const char *message
)
569 estr_write ("Fortran runtime warning: ");
570 estr_write (message
);
575 /* Whether, for a feature included in a given standard set (GFC_STD_*),
576 we should issue an error or a warning, or be quiet. */
579 notification_std (int std
)
583 if (!compile_options
.pedantic
)
584 return NOTIFICATION_SILENT
;
586 warning
= compile_options
.warn_std
& std
;
587 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
588 return NOTIFICATION_SILENT
;
590 return warning
? NOTIFICATION_WARNING
: NOTIFICATION_ERROR
;
594 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
595 feature. An error/warning will be issued if the currently selected
596 standard does not contain the requested bits. */
599 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
603 if (!compile_options
.pedantic
)
606 warning
= compile_options
.warn_std
& std
;
607 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
614 estr_write ("Fortran runtime error: ");
615 estr_write (message
);
622 estr_write ("Fortran runtime warning: ");
623 estr_write (message
);