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. */
51 #ifdef HAVE_SYS_TIME_H
55 /* <sys/time.h> has to be included before <sys/resource.h> to work
56 around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
57 #ifdef HAVE_SYS_RESOURCE_H
58 #include <sys/resource.h>
61 #include "libgfortran.h"
69 /* sys_exit()-- Terminate the program with an exit code. */
74 /* Dump core if requested. */
76 && (options.dump_core == 1
77 || (options.dump_core == -1 && compile_options.dump_core == 1)))
79 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
80 /* Warn if a core file cannot be produced because
81 of core size limit. */
83 struct rlimit core_limit;
85 if (getrlimit (RLIMIT_CORE, &core_limit) == 0 && core_limit.rlim_cur == 0)
86 st_printf ("** Warning: a core dump was requested, but the core size"
87 "limit\n** is currently zero.\n\n");
91 #if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
92 kill (getpid (), SIGQUIT);
94 st_printf ("Core dump not possible, sorry.");
102 /* Error conditions. The tricky part here is printing a message when
103 * it is the I/O subsystem that is severely wounded. Our goal is to
104 * try and print something making the fewest assumptions possible,
105 * then try to clean up before actually exiting.
107 * The following exit conditions are defined:
108 * 0 Normal program exit.
109 * 1 Terminated because of operating system error.
110 * 2 Error in the runtime library
111 * 3 Internal error in runtime library
112 * 4 Error during error processing (very bad)
114 * Other error returns are reserved for the STOP statement with a numeric code.
117 /* gfc_itoa()-- Integer to decimal conversion. */
120 gfc_itoa (GFC_INTEGER_LARGEST n, char *buffer, size_t len)
124 GFC_UINTEGER_LARGEST t;
126 assert (len >= GFC_ITOA_BUF_SIZE);
136 t = -n; /*must use unsigned to protect from overflow*/
139 p = buffer + GFC_ITOA_BUF_SIZE - 1;
144 *--p = '0' + (t % 10);
154 /* xtoa()-- Integer to hexadecimal conversion. */
157 xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
162 assert (len >= GFC_XTOA_BUF_SIZE);
167 p = buffer + GFC_XTOA_BUF_SIZE - 1;
174 digit += 'A' - '0' - 10;
184 /* st_sprintf()-- Simple sprintf() for formatting memory buffers. */
187 st_sprintf (char *buffer, const char *format, ...)
193 char itoa_buf[GFC_ITOA_BUF_SIZE];
195 va_start (arg, format);
212 *buffer++ = (char) va_arg (arg, int);
216 p = gfc_itoa (va_arg (arg, int), itoa_buf, sizeof (itoa_buf));
219 memcpy (buffer, p, count);
224 p = va_arg (arg, char *);
227 memcpy (buffer, p, count);
240 /* show_locus()-- Print a line number and filename describing where
241 * something went wrong */
244 show_locus (st_parameter_common *cmp)
246 if (!options.locus || cmp == NULL || cmp->filename == NULL)
249 st_printf ("At line %d of file %s\n", (int) cmp->line, cmp->filename);
253 /* recursion_check()-- It's possible for additional errors to occur
254 * during fatal error processing. We detect this condition here and
255 * exit with code 4 immediately. */
257 #define MAGIC 0x20DE8101
260 recursion_check (void)
262 static int magic = 0;
264 /* Don't even try to print something at this point */
272 /* os_error()-- Operating system error. We get a message from the
273 * operating system, show it and leave. Some operating system errors
274 * are caught and processed by the library. If not, we come here. */
277 os_error (const char *message)
280 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
285 /* void runtime_error()-- These are errors associated with an
286 * invalid fortran program. */
289 runtime_error (const char *message)
292 st_printf ("Fortran runtime error: %s\n", message);
295 iexport(runtime_error);
298 /* void internal_error()-- These are this-can't-happen errors
299 * that indicate something deeply wrong. */
302 internal_error (st_parameter_common *cmp, const char *message)
306 st_printf ("Internal Error: %s\n", message);
308 /* This function call is here to get the main.o object file included
309 when linking statically. This works because error.o is supposed to
310 be always linked in (and the function call is in internal_error
311 because hopefully it doesn't happen too often). */
312 stupid_function_name_for_static_linking();
318 /* translate_error()-- Given an integer error code, return a string
319 * describing the error. */
322 translate_error (int code)
337 p = "Successful return";
341 p = "Operating system error";
344 case ERROR_BAD_OPTION:
345 p = "Bad statement option";
348 case ERROR_MISSING_OPTION:
349 p = "Missing statement option";
352 case ERROR_OPTION_CONFLICT:
353 p = "Conflicting statement options";
356 case ERROR_ALREADY_OPEN:
357 p = "File already opened in another unit";
361 p = "Unattached unit";
368 case ERROR_BAD_ACTION:
369 p = "Incorrect ACTION specified";
373 p = "Read past ENDFILE record";
377 p = "Corrupt unformatted sequential file";
380 case ERROR_READ_VALUE:
381 p = "Bad value during read";
384 case ERROR_READ_OVERFLOW:
385 p = "Numeric overflow on read";
389 p = "Internal error in run-time library";
392 case ERROR_INTERNAL_UNIT:
393 p = "Internal unit I/O error";
396 case ERROR_DIRECT_EOR:
397 p = "Write exceeds length of DIRECT access record";
400 case ERROR_SHORT_RECORD:
401 p = "I/O past end of record on unformatted file";
404 case ERROR_CORRUPT_FILE:
405 p = "Unformatted file structure has been corrupted";
409 p = "Unknown error code";
417 /* generate_error()-- Come here when an error happens. This
418 * subroutine is called if it is possible to continue on after the error.
419 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
420 * ERR labels are present, we return, otherwise we terminate the program
421 * after printing a message. The error code is always required but the
422 * message parameter can be NULL, in which case a string describing
423 * the most recent operating system error is used. */
426 generate_error (st_parameter_common *cmp, int family, const char *message)
428 /* Set the error status. */
429 if ((cmp->flags & IOPARM_HAS_IOSTAT))
430 *cmp->iostat = (family == ERROR_OS) ? errno : family;
434 (family == ERROR_OS) ? get_oserror () : translate_error (family);
436 if (cmp->flags & IOPARM_HAS_IOMSG)
437 cf_strcpy (cmp->iomsg, cmp->iomsg_len, message);
439 /* Report status back to the compiler. */
440 cmp->flags &= ~IOPARM_LIBRETURN_MASK;
444 cmp->flags |= IOPARM_LIBRETURN_EOR;
445 if ((cmp->flags & IOPARM_EOR))
450 cmp->flags |= IOPARM_LIBRETURN_END;
451 if ((cmp->flags & IOPARM_END))
456 cmp->flags |= IOPARM_LIBRETURN_ERROR;
457 if ((cmp->flags & IOPARM_ERR))
462 /* Return if the user supplied an iostat variable. */
463 if ((cmp->flags & IOPARM_HAS_IOSTAT))
466 /* Terminate the program */
470 st_printf ("Fortran runtime error: %s\n", message);
475 /* Whether, for a feature included in a given standard set (GFC_STD_*),
476 we should issue an error or a warning, or be quiet. */
479 notification_std (int std)
483 if (!compile_options.pedantic)
486 warning = compile_options.warn_std & std;
487 if ((compile_options.allow_std & std) != 0 && !warning)
490 return warning ? WARNING : ERROR;
495 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
496 feature. An error/warning will be issued if the currently selected
497 standard does not contain the requested bits. */
500 notify_std (st_parameter_common *cmp, int std, const char * message)
504 if (!compile_options.pedantic)
507 warning = compile_options.warn_std & std;
508 if ((compile_options.allow_std & std) != 0 && !warning)
515 st_printf ("Fortran runtime error: %s\n", message);
521 st_printf ("Fortran runtime warning: %s\n", message);