1 /* Copyright (C) 2002, 2003, 2005, 2006, 2007, 2009 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. */
31 #include "libgfortran.h"
48 #ifdef HAVE_SYS_TIME_H
52 /* <sys/time.h> has to be included before <sys/resource.h> to work
53 around PR 30518; otherwise, MacOS 10.3.9 headers are just broken. */
54 #ifdef HAVE_SYS_RESOURCE_H
55 #include <sys/resource.h>
65 /* sys_exit()-- Terminate the program with an exit code. */
70 /* Show error backtrace if possible. */
71 if (code != 0 && code != 4
72 && (options.backtrace == 1
73 || (options.backtrace == -1 && compile_options.backtrace == 1)))
76 /* Dump core if requested. */
78 && (options.dump_core == 1
79 || (options.dump_core == -1 && compile_options.dump_core == 1)))
81 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
82 /* Warn if a core file cannot be produced because
83 of core size limit. */
85 struct rlimit core_limit;
87 if (getrlimit (RLIMIT_CORE, &core_limit) == 0 && core_limit.rlim_cur == 0)
88 st_printf ("** Warning: a core dump was requested, but the core size"
89 "limit\n** is currently zero.\n\n");
93 #if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
94 kill (getpid (), SIGQUIT);
96 st_printf ("Core dump not possible, sorry.");
104 /* Error conditions. The tricky part here is printing a message when
105 * it is the I/O subsystem that is severely wounded. Our goal is to
106 * try and print something making the fewest assumptions possible,
107 * then try to clean up before actually exiting.
109 * The following exit conditions are defined:
110 * 0 Normal program exit.
111 * 1 Terminated because of operating system error.
112 * 2 Error in the runtime library
113 * 3 Internal error in runtime library
114 * 4 Error during error processing (very bad)
116 * Other error returns are reserved for the STOP statement with a numeric code.
119 /* gfc_xtoa()-- Integer to hexadecimal conversion. */
122 gfc_xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
127 assert (len >= GFC_XTOA_BUF_SIZE);
132 p = buffer + GFC_XTOA_BUF_SIZE - 1;
139 digit += 'A' - '0' - 10;
148 /* show_locus()-- Print a line number and filename describing where
149 * something went wrong */
152 show_locus (st_parameter_common *cmp)
154 static char *filename;
156 if (!options.locus || cmp == NULL || cmp->filename == NULL)
161 filename = filename_from_unit (cmp->unit);
162 if (filename != NULL)
164 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
165 (int) cmp->line, cmp->filename, (int) cmp->unit, filename);
170 st_printf ("At line %d of file %s (unit = %d)\n",
171 (int) cmp->line, cmp->filename, (int) cmp->unit);
176 st_printf ("At line %d of file %s\n", (int) cmp->line, cmp->filename);
180 /* recursion_check()-- It's possible for additional errors to occur
181 * during fatal error processing. We detect this condition here and
182 * exit with code 4 immediately. */
184 #define MAGIC 0x20DE8101
187 recursion_check (void)
189 static int magic = 0;
191 /* Don't even try to print something at this point */
199 /* os_error()-- Operating system error. We get a message from the
200 * operating system, show it and leave. Some operating system errors
201 * are caught and processed by the library. If not, we come here. */
204 os_error (const char *message)
207 st_printf ("Operating system error: %s\n%s\n", get_oserror (), message);
213 /* void runtime_error()-- These are errors associated with an
214 * invalid fortran program. */
217 runtime_error (const char *message, ...)
222 st_printf ("Fortran runtime error: ");
223 va_start (ap, message);
224 st_vprintf (message, ap);
229 iexport(runtime_error);
231 /* void runtime_error_at()-- These are errors associated with a
232 * run time error generated by the front end compiler. */
235 runtime_error_at (const char *where, const char *message, ...)
240 st_printf ("%s\n", where);
241 st_printf ("Fortran runtime error: ");
242 va_start (ap, message);
243 st_vprintf (message, ap);
248 iexport(runtime_error_at);
252 runtime_warning_at (const char *where, const char *message, ...)
256 st_printf ("%s\n", where);
257 st_printf ("Fortran runtime warning: ");
258 va_start (ap, message);
259 st_vprintf (message, ap);
263 iexport(runtime_warning_at);
266 /* void internal_error()-- These are this-can't-happen errors
267 * that indicate something deeply wrong. */
270 internal_error (st_parameter_common *cmp, const char *message)
274 st_printf ("Internal Error: %s\n", message);
276 /* This function call is here to get the main.o object file included
277 when linking statically. This works because error.o is supposed to
278 be always linked in (and the function call is in internal_error
279 because hopefully it doesn't happen too often). */
280 stupid_function_name_for_static_linking();
286 /* translate_error()-- Given an integer error code, return a string
287 * describing the error. */
290 translate_error (int code)
305 p = "Successful return";
309 p = "Operating system error";
312 case LIBERROR_BAD_OPTION:
313 p = "Bad statement option";
316 case LIBERROR_MISSING_OPTION:
317 p = "Missing statement option";
320 case LIBERROR_OPTION_CONFLICT:
321 p = "Conflicting statement options";
324 case LIBERROR_ALREADY_OPEN:
325 p = "File already opened in another unit";
328 case LIBERROR_BAD_UNIT:
329 p = "Unattached unit";
332 case LIBERROR_FORMAT:
336 case LIBERROR_BAD_ACTION:
337 p = "Incorrect ACTION specified";
340 case LIBERROR_ENDFILE:
341 p = "Read past ENDFILE record";
344 case LIBERROR_BAD_US:
345 p = "Corrupt unformatted sequential file";
348 case LIBERROR_READ_VALUE:
349 p = "Bad value during read";
352 case LIBERROR_READ_OVERFLOW:
353 p = "Numeric overflow on read";
356 case LIBERROR_INTERNAL:
357 p = "Internal error in run-time library";
360 case LIBERROR_INTERNAL_UNIT:
361 p = "Internal unit I/O error";
364 case LIBERROR_DIRECT_EOR:
365 p = "Write exceeds length of DIRECT access record";
368 case LIBERROR_SHORT_RECORD:
369 p = "I/O past end of record on unformatted file";
372 case LIBERROR_CORRUPT_FILE:
373 p = "Unformatted file structure has been corrupted";
377 p = "Unknown error code";
385 /* generate_error()-- Come here when an error happens. This
386 * subroutine is called if it is possible to continue on after the error.
387 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
388 * ERR labels are present, we return, otherwise we terminate the program
389 * after printing a message. The error code is always required but the
390 * message parameter can be NULL, in which case a string describing
391 * the most recent operating system error is used. */
394 generate_error (st_parameter_common *cmp, int family, const char *message)
397 /* If there was a previous error, don't mask it with another
398 error message, EOF or EOR condition. */
400 if ((cmp->flags & IOPARM_LIBRETURN_MASK) == IOPARM_LIBRETURN_ERROR)
403 /* Set the error status. */
404 if ((cmp->flags & IOPARM_HAS_IOSTAT))
405 *cmp->iostat = (family == LIBERROR_OS) ? errno : family;
409 (family == LIBERROR_OS) ? get_oserror () : translate_error (family);
411 if (cmp->flags & IOPARM_HAS_IOMSG)
412 cf_strcpy (cmp->iomsg, cmp->iomsg_len, message);
414 /* Report status back to the compiler. */
415 cmp->flags &= ~IOPARM_LIBRETURN_MASK;
419 cmp->flags |= IOPARM_LIBRETURN_EOR;
420 if ((cmp->flags & IOPARM_EOR))
425 cmp->flags |= IOPARM_LIBRETURN_END;
426 if ((cmp->flags & IOPARM_END))
431 cmp->flags |= IOPARM_LIBRETURN_ERROR;
432 if ((cmp->flags & IOPARM_ERR))
437 /* Return if the user supplied an iostat variable. */
438 if ((cmp->flags & IOPARM_HAS_IOSTAT))
441 /* Terminate the program */
445 st_printf ("Fortran runtime error: %s\n", message);
448 iexport(generate_error);
450 /* Whether, for a feature included in a given standard set (GFC_STD_*),
451 we should issue an error or a warning, or be quiet. */
454 notification_std (int std)
458 if (!compile_options.pedantic)
461 warning = compile_options.warn_std & std;
462 if ((compile_options.allow_std & std) != 0 && !warning)
465 return warning ? WARNING : ERROR;
470 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
471 feature. An error/warning will be issued if the currently selected
472 standard does not contain the requested bits. */
475 notify_std (st_parameter_common *cmp, int std, const char * message)
479 if (!compile_options.pedantic)
482 warning = compile_options.warn_std & std;
483 if ((compile_options.allow_std & std) != 0 && !warning)
490 st_printf ("Fortran runtime error: %s\n", message);
496 st_printf ("Fortran runtime warning: %s\n", message);