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
223 __builtin_choose_expr (__builtin_classify_type (strerror_r (0, buf
, 0))
225 /* GNU strerror_r() */
226 strerror_r (errnum
, buf
, buflen
),
227 /* POSIX strerror_r () */
228 (strerror_r (errnum
, buf
, buflen
), buf
));
230 /* strerror () is not necessarily thread-safe, but should at least
231 be available everywhere. */
232 return strerror (errnum
);
237 /* show_locus()-- Print a line number and filename describing where
238 * something went wrong */
241 show_locus (st_parameter_common
*cmp
)
245 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
250 filename
= filename_from_unit (cmp
->unit
);
252 if (filename
!= NULL
)
254 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
255 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
, filename
);
260 st_printf ("At line %d of file %s (unit = %d)\n",
261 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
);
266 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
270 /* recursion_check()-- It's possible for additional errors to occur
271 * during fatal error processing. We detect this condition here and
272 * exit with code 4 immediately. */
274 #define MAGIC 0x20DE8101
277 recursion_check (void)
279 static int magic
= 0;
281 /* Don't even try to print something at this point */
289 #define STRERR_MAXSZ 256
291 /* os_error()-- Operating system error. We get a message from the
292 * operating system, show it and leave. Some operating system errors
293 * are caught and processed by the library. If not, we come here. */
296 os_error (const char *message
)
298 char errmsg
[STRERR_MAXSZ
];
300 estr_write ("Operating system error: ");
301 estr_write (gf_strerror (errno
, errmsg
, STRERR_MAXSZ
));
303 estr_write (message
);
310 /* void runtime_error()-- These are errors associated with an
311 * invalid fortran program. */
314 runtime_error (const char *message
, ...)
319 estr_write ("Fortran runtime error: ");
320 va_start (ap
, message
);
321 st_vprintf (message
, ap
);
326 iexport(runtime_error
);
328 /* void runtime_error_at()-- These are errors associated with a
329 * run time error generated by the front end compiler. */
332 runtime_error_at (const char *where
, const char *message
, ...)
338 estr_write ("\nFortran runtime error: ");
339 va_start (ap
, message
);
340 st_vprintf (message
, ap
);
345 iexport(runtime_error_at
);
349 runtime_warning_at (const char *where
, const char *message
, ...)
354 estr_write ("\nFortran runtime warning: ");
355 va_start (ap
, message
);
356 st_vprintf (message
, ap
);
360 iexport(runtime_warning_at
);
363 /* void internal_error()-- These are this-can't-happen errors
364 * that indicate something deeply wrong. */
367 internal_error (st_parameter_common
*cmp
, const char *message
)
371 estr_write ("Internal Error: ");
372 estr_write (message
);
375 /* This function call is here to get the main.o object file included
376 when linking statically. This works because error.o is supposed to
377 be always linked in (and the function call is in internal_error
378 because hopefully it doesn't happen too often). */
379 stupid_function_name_for_static_linking();
385 /* translate_error()-- Given an integer error code, return a string
386 * describing the error. */
389 translate_error (int code
)
404 p
= "Successful return";
408 p
= "Operating system error";
411 case LIBERROR_BAD_OPTION
:
412 p
= "Bad statement option";
415 case LIBERROR_MISSING_OPTION
:
416 p
= "Missing statement option";
419 case LIBERROR_OPTION_CONFLICT
:
420 p
= "Conflicting statement options";
423 case LIBERROR_ALREADY_OPEN
:
424 p
= "File already opened in another unit";
427 case LIBERROR_BAD_UNIT
:
428 p
= "Unattached unit";
431 case LIBERROR_FORMAT
:
435 case LIBERROR_BAD_ACTION
:
436 p
= "Incorrect ACTION specified";
439 case LIBERROR_ENDFILE
:
440 p
= "Read past ENDFILE record";
443 case LIBERROR_BAD_US
:
444 p
= "Corrupt unformatted sequential file";
447 case LIBERROR_READ_VALUE
:
448 p
= "Bad value during read";
451 case LIBERROR_READ_OVERFLOW
:
452 p
= "Numeric overflow on read";
455 case LIBERROR_INTERNAL
:
456 p
= "Internal error in run-time library";
459 case LIBERROR_INTERNAL_UNIT
:
460 p
= "Internal unit I/O error";
463 case LIBERROR_DIRECT_EOR
:
464 p
= "Write exceeds length of DIRECT access record";
467 case LIBERROR_SHORT_RECORD
:
468 p
= "I/O past end of record on unformatted file";
471 case LIBERROR_CORRUPT_FILE
:
472 p
= "Unformatted file structure has been corrupted";
476 p
= "Unknown error code";
484 /* generate_error()-- Come here when an error happens. This
485 * subroutine is called if it is possible to continue on after the error.
486 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
487 * ERR labels are present, we return, otherwise we terminate the program
488 * after printing a message. The error code is always required but the
489 * message parameter can be NULL, in which case a string describing
490 * the most recent operating system error is used. */
493 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
495 char errmsg
[STRERR_MAXSZ
];
497 /* If there was a previous error, don't mask it with another
498 error message, EOF or EOR condition. */
500 if ((cmp
->flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_ERROR
)
503 /* Set the error status. */
504 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
505 *cmp
->iostat
= (family
== LIBERROR_OS
) ? errno
: family
;
509 (family
== LIBERROR_OS
) ? gf_strerror (errno
, errmsg
, STRERR_MAXSZ
) :
510 translate_error (family
);
512 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
513 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
515 /* Report status back to the compiler. */
516 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
520 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
521 if ((cmp
->flags
& IOPARM_EOR
))
526 cmp
->flags
|= IOPARM_LIBRETURN_END
;
527 if ((cmp
->flags
& IOPARM_END
))
532 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
533 if ((cmp
->flags
& IOPARM_ERR
))
538 /* Return if the user supplied an iostat variable. */
539 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
542 /* Terminate the program */
546 estr_write ("Fortran runtime error: ");
547 estr_write (message
);
551 iexport(generate_error
);
554 /* generate_warning()-- Similar to generate_error but just give a warning. */
557 generate_warning (st_parameter_common
*cmp
, const char *message
)
563 estr_write ("Fortran runtime warning: ");
564 estr_write (message
);
569 /* Whether, for a feature included in a given standard set (GFC_STD_*),
570 we should issue an error or a warning, or be quiet. */
573 notification_std (int std
)
577 if (!compile_options
.pedantic
)
578 return NOTIFICATION_SILENT
;
580 warning
= compile_options
.warn_std
& std
;
581 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
582 return NOTIFICATION_SILENT
;
584 return warning
? NOTIFICATION_WARNING
: NOTIFICATION_ERROR
;
588 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
589 feature. An error/warning will be issued if the currently selected
590 standard does not contain the requested bits. */
593 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
597 if (!compile_options
.pedantic
)
600 warning
= compile_options
.warn_std
& std
;
601 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
608 estr_write ("Fortran runtime error: ");
609 estr_write (message
);
616 estr_write ("Fortran runtime warning: ");
617 estr_write (message
);