]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/runtime/error.c
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 /* sys_exit()-- Terminate the program with an exit code. */
66 /* Show error backtrace if possible. */
67 if (code
!= 0 && code
!= 4
68 && (options
.backtrace
== 1
69 || (options
.backtrace
== -1 && compile_options
.backtrace
== 1)))
72 /* Dump core if requested. */
74 && (options
.dump_core
== 1
75 || (options
.dump_core
== -1 && compile_options
.dump_core
== 1)))
77 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
78 /* Warn if a core file cannot be produced because
79 of core size limit. */
81 struct rlimit core_limit
;
83 if (getrlimit (RLIMIT_CORE
, &core_limit
) == 0 && core_limit
.rlim_cur
== 0)
84 estr_write ("** Warning: a core dump was requested, but the core size"
85 "limit\n** is currently zero.\n\n");
89 #if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
90 kill (getpid (), SIGQUIT
);
92 estr_write ("Core dump not possible, sorry.");
100 /* Error conditions. The tricky part here is printing a message when
101 * it is the I/O subsystem that is severely wounded. Our goal is to
102 * try and print something making the fewest assumptions possible,
103 * then try to clean up before actually exiting.
105 * The following exit conditions are defined:
106 * 0 Normal program exit.
107 * 1 Terminated because of operating system error.
108 * 2 Error in the runtime library
109 * 3 Internal error in runtime library
110 * 4 Error during error processing (very bad)
112 * Other error returns are reserved for the STOP statement with a numeric code.
116 /* Write a null-terminated C string to standard error. This function
117 is async-signal-safe. */
120 estr_write (const char *str
)
122 return write (STDERR_FILENO
, str
, strlen (str
));
126 /* st_vprintf()-- vsnprintf-like function for error output. We use a
127 stack allocated buffer for formatting; since this function might be
128 called from within a signal handler, printing directly to stderr
129 with vfprintf is not safe since the stderr locking might lead to a
132 #define ST_VPRINTF_SIZE 512
135 st_vprintf (const char *format
, va_list ap
)
138 char buffer
[ST_VPRINTF_SIZE
];
140 #ifdef HAVE_VSNPRINTF
141 written
= vsnprintf(buffer
, ST_VPRINTF_SIZE
, format
, ap
);
143 written
= vsprintf(buffer
, format
, ap
);
145 if (written
>= ST_VPRINTF_SIZE
- 1)
147 /* The error message was longer than our buffer. Ouch. Because
148 we may have messed up things badly, report the error and
150 #define ERROR_MESSAGE "Internal error: buffer overrun in st_vprintf()\n"
151 write (STDERR_FILENO
, buffer
, ST_VPRINTF_SIZE
- 1);
152 write (STDERR_FILENO
, ERROR_MESSAGE
, strlen(ERROR_MESSAGE
));
159 written
= write (STDERR_FILENO
, buffer
, written
);
165 st_printf (const char * format
, ...)
169 va_start (ap
, format
);
170 written
= st_vprintf (format
, ap
);
176 /* gfc_xtoa()-- Integer to hexadecimal conversion. */
179 gfc_xtoa (GFC_UINTEGER_LARGEST n
, char *buffer
, size_t len
)
184 assert (len
>= GFC_XTOA_BUF_SIZE
);
189 p
= buffer
+ GFC_XTOA_BUF_SIZE
- 1;
196 digit
+= 'A' - '0' - 10;
206 /* Hopefully thread-safe wrapper for a strerror_r() style function. */
209 gf_strerror (int errnum
,
210 char * buf
__attribute__((unused
)),
211 size_t buflen
__attribute__((unused
)))
213 #ifdef HAVE_STRERROR_R
214 /* TODO: How to prevent the compiler warning due to strerror_r of
215 the untaken branch having the wrong return type? */
216 if (__builtin_classify_type (strerror_r (0, buf
, 0)) == 5)
218 /* GNU strerror_r() */
219 return strerror_r (errnum
, buf
, buflen
);
223 /* POSIX strerror_r () */
224 strerror_r (errnum
, buf
, buflen
);
228 /* strerror () is not necessarily thread-safe, but should at least
229 be available everywhere. */
230 return strerror (errnum
);
235 /* show_locus()-- Print a line number and filename describing where
236 * something went wrong */
239 show_locus (st_parameter_common
*cmp
)
243 if (!options
.locus
|| cmp
== NULL
|| cmp
->filename
== NULL
)
248 filename
= filename_from_unit (cmp
->unit
);
250 if (filename
!= NULL
)
252 st_printf ("At line %d of file %s (unit = %d, file = '%s')\n",
253 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
, filename
);
258 st_printf ("At line %d of file %s (unit = %d)\n",
259 (int) cmp
->line
, cmp
->filename
, (int) cmp
->unit
);
264 st_printf ("At line %d of file %s\n", (int) cmp
->line
, cmp
->filename
);
268 /* recursion_check()-- It's possible for additional errors to occur
269 * during fatal error processing. We detect this condition here and
270 * exit with code 4 immediately. */
272 #define MAGIC 0x20DE8101
275 recursion_check (void)
277 static int magic
= 0;
279 /* Don't even try to print something at this point */
287 #define STRERR_MAXSZ 256
289 /* os_error()-- Operating system error. We get a message from the
290 * operating system, show it and leave. Some operating system errors
291 * are caught and processed by the library. If not, we come here. */
294 os_error (const char *message
)
296 char errmsg
[STRERR_MAXSZ
];
298 estr_write ("Operating system error: ");
299 estr_write (gf_strerror (errno
, errmsg
, STRERR_MAXSZ
));
301 estr_write (message
);
308 /* void runtime_error()-- These are errors associated with an
309 * invalid fortran program. */
312 runtime_error (const char *message
, ...)
317 estr_write ("Fortran runtime error: ");
318 va_start (ap
, message
);
319 st_vprintf (message
, ap
);
324 iexport(runtime_error
);
326 /* void runtime_error_at()-- These are errors associated with a
327 * run time error generated by the front end compiler. */
330 runtime_error_at (const char *where
, const char *message
, ...)
336 estr_write ("\nFortran runtime error: ");
337 va_start (ap
, message
);
338 st_vprintf (message
, ap
);
343 iexport(runtime_error_at
);
347 runtime_warning_at (const char *where
, const char *message
, ...)
352 estr_write ("\nFortran runtime warning: ");
353 va_start (ap
, message
);
354 st_vprintf (message
, ap
);
358 iexport(runtime_warning_at
);
361 /* void internal_error()-- These are this-can't-happen errors
362 * that indicate something deeply wrong. */
365 internal_error (st_parameter_common
*cmp
, const char *message
)
369 estr_write ("Internal Error: ");
370 estr_write (message
);
373 /* This function call is here to get the main.o object file included
374 when linking statically. This works because error.o is supposed to
375 be always linked in (and the function call is in internal_error
376 because hopefully it doesn't happen too often). */
377 stupid_function_name_for_static_linking();
383 /* translate_error()-- Given an integer error code, return a string
384 * describing the error. */
387 translate_error (int code
)
402 p
= "Successful return";
406 p
= "Operating system error";
409 case LIBERROR_BAD_OPTION
:
410 p
= "Bad statement option";
413 case LIBERROR_MISSING_OPTION
:
414 p
= "Missing statement option";
417 case LIBERROR_OPTION_CONFLICT
:
418 p
= "Conflicting statement options";
421 case LIBERROR_ALREADY_OPEN
:
422 p
= "File already opened in another unit";
425 case LIBERROR_BAD_UNIT
:
426 p
= "Unattached unit";
429 case LIBERROR_FORMAT
:
433 case LIBERROR_BAD_ACTION
:
434 p
= "Incorrect ACTION specified";
437 case LIBERROR_ENDFILE
:
438 p
= "Read past ENDFILE record";
441 case LIBERROR_BAD_US
:
442 p
= "Corrupt unformatted sequential file";
445 case LIBERROR_READ_VALUE
:
446 p
= "Bad value during read";
449 case LIBERROR_READ_OVERFLOW
:
450 p
= "Numeric overflow on read";
453 case LIBERROR_INTERNAL
:
454 p
= "Internal error in run-time library";
457 case LIBERROR_INTERNAL_UNIT
:
458 p
= "Internal unit I/O error";
461 case LIBERROR_DIRECT_EOR
:
462 p
= "Write exceeds length of DIRECT access record";
465 case LIBERROR_SHORT_RECORD
:
466 p
= "I/O past end of record on unformatted file";
469 case LIBERROR_CORRUPT_FILE
:
470 p
= "Unformatted file structure has been corrupted";
474 p
= "Unknown error code";
482 /* generate_error()-- Come here when an error happens. This
483 * subroutine is called if it is possible to continue on after the error.
484 * If an IOSTAT or IOMSG variable exists, we set it. If IOSTAT or
485 * ERR labels are present, we return, otherwise we terminate the program
486 * after printing a message. The error code is always required but the
487 * message parameter can be NULL, in which case a string describing
488 * the most recent operating system error is used. */
491 generate_error (st_parameter_common
*cmp
, int family
, const char *message
)
493 char errmsg
[STRERR_MAXSZ
];
495 /* If there was a previous error, don't mask it with another
496 error message, EOF or EOR condition. */
498 if ((cmp
->flags
& IOPARM_LIBRETURN_MASK
) == IOPARM_LIBRETURN_ERROR
)
501 /* Set the error status. */
502 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
503 *cmp
->iostat
= (family
== LIBERROR_OS
) ? errno
: family
;
507 (family
== LIBERROR_OS
) ? gf_strerror (errno
, errmsg
, STRERR_MAXSZ
) :
508 translate_error (family
);
510 if (cmp
->flags
& IOPARM_HAS_IOMSG
)
511 cf_strcpy (cmp
->iomsg
, cmp
->iomsg_len
, message
);
513 /* Report status back to the compiler. */
514 cmp
->flags
&= ~IOPARM_LIBRETURN_MASK
;
518 cmp
->flags
|= IOPARM_LIBRETURN_EOR
;
519 if ((cmp
->flags
& IOPARM_EOR
))
524 cmp
->flags
|= IOPARM_LIBRETURN_END
;
525 if ((cmp
->flags
& IOPARM_END
))
530 cmp
->flags
|= IOPARM_LIBRETURN_ERROR
;
531 if ((cmp
->flags
& IOPARM_ERR
))
536 /* Return if the user supplied an iostat variable. */
537 if ((cmp
->flags
& IOPARM_HAS_IOSTAT
))
540 /* Terminate the program */
544 estr_write ("Fortran runtime error: ");
545 estr_write (message
);
549 iexport(generate_error
);
552 /* generate_warning()-- Similar to generate_error but just give a warning. */
555 generate_warning (st_parameter_common
*cmp
, const char *message
)
561 estr_write ("Fortran runtime warning: ");
562 estr_write (message
);
567 /* Whether, for a feature included in a given standard set (GFC_STD_*),
568 we should issue an error or a warning, or be quiet. */
571 notification_std (int std
)
575 if (!compile_options
.pedantic
)
576 return NOTIFICATION_SILENT
;
578 warning
= compile_options
.warn_std
& std
;
579 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
580 return NOTIFICATION_SILENT
;
582 return warning
? NOTIFICATION_WARNING
: NOTIFICATION_ERROR
;
586 /* Possibly issue a warning/error about use of a nonstandard (or deleted)
587 feature. An error/warning will be issued if the currently selected
588 standard does not contain the requested bits. */
591 notify_std (st_parameter_common
*cmp
, int std
, const char * message
)
595 if (!compile_options
.pedantic
)
598 warning
= compile_options
.warn_std
& std
;
599 if ((compile_options
.allow_std
& std
) != 0 && !warning
)
606 estr_write ("Fortran runtime error: ");
607 estr_write (message
);
614 estr_write ("Fortran runtime warning: ");
615 estr_write (message
);