1 /* Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
2 Contributed by Andy Vaught
4 This file is part of the GNU Fortran 95 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 2, or (at your option)
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA. */
50 #ifdef HAVE_SYS_TIME_H
54 /* <sys/time.h> has to be included before <sys/resource.h> to work
55 around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
56 #ifdef HAVE_SYS_RESOURCE_H
57 #include <sys/resource.h>
60 #include "libgfortran.h"
68 /* sys_exit()-- Terminate the program with an exit code. */
73 /* Show error backtrace if possible. */
74 if (code
!= 0 && code
!= 4
75 && (options
.backtrace
== 1
76 || (options
.backtrace
== -1 && compile_options
.backtrace
== 1)))
79 /* Dump core if requested. */
81 && (options
.dump_core
== 1
82 || (options
.dump_core
== -1 && compile_options
.dump_core
== 1)))
84 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
85 /* Warn if a core file cannot be produced because
86 of core size limit. */
88 struct rlimit core_limit
;
90 if (getrlimit (RLIMIT_CORE
, &core_limit
) == 0 && core_limit
.rlim_cur
== 0)
91 st_printf ("** Warning: a core dump was requested, but the core size"
92 "limit\n** is currently zero.\n\n");
96 #if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
97 kill (getpid (), SIGQUIT
);
99 st_printf ("Core dump not possible, sorry.");
107 /* Error conditions. The tricky part here is printing a message when
108 * it is the I/O subsystem that is severely wounded. Our goal is to
109 * try and print something making the fewest assumptions possible,
110 * then try to clean up before actually exiting.
112 * The following exit conditions are defined:
113 * 0 Normal program exit.
114 * 1 Terminated because of operating system error.
115 * 2 Error in the runtime library
116 * 3 Internal error in runtime library
117 * 4 Error during error processing (very bad)
119 * Other error returns are reserved for the STOP statement with a numeric code.
122 /* gfc_itoa()-- Integer to decimal conversion. */
125 gfc_itoa (GFC_INTEGER_LARGEST n
, char *buffer
, size_t len
)
129 GFC_UINTEGER_LARGEST t
;
131 assert (len
>= GFC_ITOA_BUF_SIZE
);
141 t
= -n
; /*must use unsigned to protect from overflow*/
144 p
= buffer
+ GFC_ITOA_BUF_SIZE
- 1;
149 *--p
= '0' + (t
% 10);
159 /* xtoa()-- Integer to hexadecimal conversion. */
162 xtoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
167 assert (len
>= GFC_XTOA_BUF_SIZE
);
172 p
= buffer
+ GFC_XTOA_BUF_SIZE
- 1;
179 digit
+= 'A' - '0' - 10;
188 /* show_locus()-- Print a line number and filename describing where
189 * something went wrong */
192 show_locus (st_parameter_common
*cmp
)
194 static char *filename
;
196 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
201 filename
= filename_from_unit (cmp
->unit
);
202 if (filename
!= NULL
)
204 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
205 (int) cmp
->line
, cmp
->filename
, cmp
->unit
, filename
);
211 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
215 /* recursion_check()-- It's possible for additional errors to occur
216 * during fatal error processing. We detect this condition here and
217 * exit with code 4 immediately. */
219 #define MAGIC 0x20DE8101
222 recursion_check (void)
224 static int magic
= 0;
226 /* Don't even try to print something at this point */
234 /* os_error()-- Operating system error. We get a message from the
235 * operating system, show it and leave. Some operating system errors
236 * are caught and processed by the library. If not, we come here. */
239 os_error (const char *message
)
242 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message
);
248 /* void runtime_error()-- These are errors associated with an
249 * invalid fortran program. */
252 runtime_error (const char *message
, ...)
257 st_printf ("Fortran runtime error: ");
258 va_start (ap
, message
);
259 st_vprintf (message
, ap
);
264 iexport(runtime_error
);
266 /* void runtime_error_at()-- These are errors associated with a
267 * run time error generated by the front end compiler. */
270 runtime_error_at (const char *where
, const char *message
, ...)
275 st_printf ("%s\n", where
);
276 st_printf ("Fortran runtime error: ");
277 va_start (ap
, message
);
278 st_vprintf (message
, ap
);
283 iexport(runtime_error_at
);
286 /* void internal_error()-- These are this-can't-happen errors
287 * that indicate something deeply wrong. */
290 internal_error (st_parameter_common
*cmp
, const char *message
)
294 st_printf ("Internal Error: %s\n", message
);
296 /* This function call is here to get the main.o object file included
297 when linking statically. This works because error.o is supposed to
298 be always linked in (and the function call is in internal_error
299 because hopefully it doesn't happen too often). */
300 stupid_function_name_for_static_linking();
306 /* translate_error()-- Given an integer error code, return a string
307 * describing the error. */
310 translate_error (int code
)
325 p
= "Successful return";
329 p
= "Operating system error";
332 case ERROR_BAD_OPTION
:
333 p
= "Bad statement option";
336 case ERROR_MISSING_OPTION
:
337 p
= "Missing statement option";
340 case ERROR_OPTION_CONFLICT
:
341 p
= "Conflicting statement options";
344 case ERROR_ALREADY_OPEN
:
345 p
= "File already opened in another unit";
349 p
= "Unattached unit";
356 case ERROR_BAD_ACTION
:
357 p
= "Incorrect ACTION specified";
361 p
= "Read past ENDFILE record";
365 p
= "Corrupt unformatted sequential file";
368 case ERROR_READ_VALUE
:
369 p
= "Bad value during read";
372 case ERROR_READ_OVERFLOW
:
373 p
= "Numeric overflow on read";
377 p
= "Internal error in run-time library";
380 case ERROR_INTERNAL_UNIT
:
381 p
= "Internal unit I/O error";
384 case ERROR_DIRECT_EOR
:
385 p
= "Write exceeds length of DIRECT access record";
388 case ERROR_SHORT_RECORD
:
389 p
= "I/O past end of record on unformatted file";
392 case ERROR_CORRUPT_FILE
:
393 p
= "Unformatted file structure has been corrupted";
397 p
= "Unknown error code";
405 /* generate_error()-- Come here when an error happens. This
406 * subroutine is called if it is possible to continue on after the error.
407 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
408 * ERR labels are present, we return, otherwise we terminate the program
409 * after printing a message. The error code is always required but the
410 * message parameter can be NULL, in which case a string describing
411 * the most recent operating system error is used. */
414 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
416 /* Set the error status. */
417 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
418 *cmp
->iostat
= (family
== ERROR_OS
) ? errno
: family
;
422 (family
== ERROR_OS
) ? get_oserror () : translate_error (family
);
424 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
425 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
427 /* Report status back to the compiler. */
428 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
432 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
433 if ((cmp
->flags
& IOPARM_EOR
))
438 cmp
->flags
|= IOPARM_LIBRETURN_END
;
439 if ((cmp
->flags
& IOPARM_END
))
444 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
445 if ((cmp
->flags
& IOPARM_ERR
))
450 /* Return if the user supplied an iostat variable. */
451 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
454 /* Terminate the program */
458 st_printf ("Fortran runtime error: %s\n", message
);
461 iexport(generate_error
);
463 /* Whether, for a feature included in a given standard set (GFC_STD_*),
464 we should issue an error or a warning, or be quiet. */
467 notification_std (int std
)
471 if (!compile_options
.pedantic
)
474 warning
= compile_options
.warn_std
& std
;
475 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
478 return warning
? WARNING
: ERROR
;
483 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
484 feature. An error/warning will be issued if the currently selected
485 standard does not contain the requested bits. */
488 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
492 if (!compile_options
.pedantic
)
495 warning
= compile_options
.warn_std
& std
;
496 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
503 st_printf ("Fortran runtime error: %s\n", message
);
509 st_printf ("Fortran runtime warning: %s\n", message
);