PR 53379 Print backtrace on error termination.
[gcc.git] / libgfortran / runtime / stop.c
1 /* Implementation of the STOP statement.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
11
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.
16
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.
20
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/>. */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <string.h>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34
35 /* Fortran 2008 demands: If any exception (14) is signaling on that image, the
36 processor shall issue a warning indicating which exceptions are signaling;
37 this warning shall be on the unit identified by the named constant
38 ERROR_UNIT (13.8.2.8). In line with other compilers, we do not report
39 inexact - and we optionally ignore underflow, cf. thread starting at
40 http://mailman.j3-fortran.org/pipermail/j3/2013-June/006452.html. */
41
42 static void
43 report_exception (void)
44 {
45 int set_excepts;
46
47 if (!compile_options.fpe_summary)
48 return;
49
50 set_excepts = get_fpu_except_flags ();
51 if ((set_excepts & compile_options.fpe_summary) == 0)
52 return;
53
54 estr_write ("Note: The following floating-point exceptions are signalling:");
55
56 if ((compile_options.fpe_summary & GFC_FPE_INVALID)
57 && (set_excepts & GFC_FPE_INVALID))
58 estr_write (" IEEE_INVALID_FLAG");
59
60 if ((compile_options.fpe_summary & GFC_FPE_ZERO)
61 && (set_excepts & GFC_FPE_ZERO))
62 estr_write (" IEEE_DIVIDE_BY_ZERO");
63
64 if ((compile_options.fpe_summary & GFC_FPE_OVERFLOW)
65 && (set_excepts & GFC_FPE_OVERFLOW))
66 estr_write (" IEEE_OVERFLOW_FLAG");
67
68 if ((compile_options.fpe_summary & GFC_FPE_UNDERFLOW)
69 && (set_excepts & GFC_FPE_UNDERFLOW))
70 estr_write (" IEEE_UNDERFLOW_FLAG");
71
72 if ((compile_options.fpe_summary & GFC_FPE_DENORMAL)
73 && (set_excepts & GFC_FPE_DENORMAL))
74 estr_write (" IEEE_DENORMAL");
75
76 if ((compile_options.fpe_summary & GFC_FPE_INEXACT)
77 && (set_excepts & GFC_FPE_INEXACT))
78 estr_write (" IEEE_INEXACT_FLAG");
79
80 estr_write ("\n");
81 }
82
83
84 /* A numeric STOP statement. */
85
86 extern _Noreturn void stop_numeric (GFC_INTEGER_4);
87 export_proto(stop_numeric);
88
89 void
90 stop_numeric (GFC_INTEGER_4 code)
91 {
92 report_exception ();
93 if (code == -1)
94 code = 0;
95 else
96 st_printf ("STOP %d\n", (int)code);
97
98 exit (code);
99 }
100
101
102 /* A Fortran 2008 numeric STOP statement. */
103
104 extern _Noreturn void stop_numeric_f08 (GFC_INTEGER_4);
105 export_proto(stop_numeric_f08);
106
107 void
108 stop_numeric_f08 (GFC_INTEGER_4 code)
109 {
110 report_exception ();
111 st_printf ("STOP %d\n", (int)code);
112 exit (code);
113 }
114
115
116 /* A character string or blank STOP statement. */
117
118 void
119 stop_string (const char *string, GFC_INTEGER_4 len)
120 {
121 report_exception ();
122 if (string)
123 {
124 estr_write ("STOP ");
125 (void) write (STDERR_FILENO, string, len);
126 estr_write ("\n");
127 }
128 exit (0);
129 }
130
131
132 /* Per Fortran 2008, section 8.4: "Execution of a STOP statement initiates
133 normal termination of execution. Execution of an ERROR STOP statement
134 initiates error termination of execution." Thus, error_stop_string returns
135 a nonzero exit status code. */
136
137 extern _Noreturn void error_stop_string (const char *, GFC_INTEGER_4);
138 export_proto(error_stop_string);
139
140 void
141 error_stop_string (const char *string, GFC_INTEGER_4 len)
142 {
143 report_exception ();
144 estr_write ("ERROR STOP ");
145 (void) write (STDERR_FILENO, string, len);
146 estr_write ("\n");
147
148 exit_error (1);
149 }
150
151
152 /* A numeric ERROR STOP statement. */
153
154 extern _Noreturn void error_stop_numeric (GFC_INTEGER_4);
155 export_proto(error_stop_numeric);
156
157 void
158 error_stop_numeric (GFC_INTEGER_4 code)
159 {
160 report_exception ();
161 st_printf ("ERROR STOP %d\n", (int) code);
162 exit_error (code);
163 }