Fix fork-related regressions on GNU/Linux
[deliverable/binutils-gdb.git] / gdb / exceptions.c
CommitLineData
60250e8b
AC
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
60250e8b
AC
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
60250e8b
AC
10 (at your option) any later version.
11
12 This program 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 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
60250e8b
AC
19
20#include "defs.h"
21#include "exceptions.h"
60250e8b
AC
22#include "breakpoint.h"
23#include "target.h"
24#include "inferior.h"
25#include "annotate.h"
26#include "ui-out.h"
e06e2353 27#include "serial.h"
347bddb7 28#include "gdbthread.h"
694ec099 29#include "top.h"
223ffa71 30#include "common/gdb_optional.h"
60250e8b 31
6b1b7650 32static void
c6da7a6d 33print_flush (void)
6b1b7650 34{
694ec099 35 struct ui *ui = current_ui;
e06e2353
AC
36 struct serial *gdb_stdout_serial;
37
c6da7a6d
AC
38 if (deprecated_error_begin_hook)
39 deprecated_error_begin_hook ();
5df43998 40
223ffa71 41 gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
5df43998 42 if (target_supports_terminal_ours ())
481ac8c9 43 {
223ffa71
TT
44 term_state.emplace ();
45 target_terminal::ours_for_output ();
481ac8c9 46 }
e06e2353
AC
47
48 /* We want all output to appear now, before we print the error. We
49 have 3 levels of buffering we have to flush (it's possible that
50 some of these should be changed to flush the lower-level ones
51 too): */
52
53 /* 1. The _filtered buffer. */
5df43998
GB
54 if (filtered_printing_initialized ())
55 wrap_here ("");
e06e2353
AC
56
57 /* 2. The stdio buffer. */
c6da7a6d 58 gdb_flush (gdb_stdout);
e06e2353
AC
59 gdb_flush (gdb_stderr);
60
61 /* 3. The system-level buffer. */
694ec099 62 gdb_stdout_serial = serial_fdopen (fileno (ui->outstream));
cade9e54
PB
63 if (gdb_stdout_serial)
64 {
65 serial_drain_output (gdb_stdout_serial);
66 serial_un_fdopen (gdb_stdout_serial);
67 }
e06e2353 68
c6da7a6d 69 annotate_error_begin ();
6b1b7650
AC
70}
71
9cbc821d 72static void
71fff37b 73print_exception (struct ui_file *file, struct gdb_exception e)
9cbc821d
AC
74{
75 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
76 as that way the MI's behavior is preserved. */
77 const char *start;
78 const char *end;
d7f9d729 79
9cbc821d
AC
80 for (start = e.message; start != NULL; start = end)
81 {
82 end = strchr (start, '\n');
83 if (end == NULL)
84 fputs_filtered (start, file);
85 else
86 {
87 end++;
88 ui_file_write (file, start, end - start);
89 }
90 }
c6da7a6d 91 fprintf_filtered (file, "\n");
e48f5bee
AC
92
93 /* Now append the annotation. */
94 switch (e.reason)
95 {
96 case RETURN_QUIT:
97 annotate_quit ();
98 break;
99 case RETURN_ERROR:
100 /* Assume that these are all errors. */
101 annotate_error ();
102 break;
103 default:
104 internal_error (__FILE__, __LINE__, _("Bad switch."));
105 }
9cbc821d
AC
106}
107
8a076db9 108void
71fff37b 109exception_print (struct ui_file *file, struct gdb_exception e)
8a076db9
AC
110{
111 if (e.reason < 0 && e.message != NULL)
112 {
c6da7a6d 113 print_flush ();
9cbc821d 114 print_exception (file, e);
9cbc821d
AC
115 }
116}
8a076db9 117
9cbc821d 118void
71fff37b 119exception_fprintf (struct ui_file *file, struct gdb_exception e,
9cbc821d
AC
120 const char *prefix, ...)
121{
122 if (e.reason < 0 && e.message != NULL)
123 {
124 va_list args;
c6da7a6d
AC
125
126 print_flush ();
9cbc821d
AC
127
128 /* Print the prefix. */
129 va_start (args, prefix);
130 vfprintf_filtered (file, prefix, args);
131 va_end (args);
132
133 print_exception (file, e);
8a076db9
AC
134 }
135}
136
787274f0
DE
137/* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
138 handler. If an exception (enum return_reason) is thrown using
139 throw_exception() than all cleanups installed since
140 catch_exceptions() was entered are invoked, the (-ve) exception
141 value is then returned by catch_exceptions. If FUNC() returns
142 normally (with a positive or zero return value) then that value is
143 returned by catch_exceptions(). It is an internal_error() for
144 FUNC() to return a negative value.
145
585a46a2 146 See exceptions.h for further usage details. */
60250e8b
AC
147
148/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
7a9dd1b2 149 error() et al. could maintain a set of flags that indicate the
60250e8b
AC
150 current state of each of the longjmp buffers. This would give the
151 longjmp code the chance to detect a longjmp botch (before it gets
152 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
153 code also randomly used a SET_TOP_LEVEL macro that directly
0963b4bd 154 initialized the longjmp buffers. */
60250e8b 155
60250e8b
AC
156int
157catch_exceptions (struct ui_out *uiout,
158 catch_exceptions_ftype *func,
159 void *func_args,
60250e8b
AC
160 return_mask mask)
161{
1c3c7ee7 162 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
60250e8b
AC
163}
164
165int
f9679975 166catch_exceptions_with_msg (struct ui_out *func_uiout,
60250e8b
AC
167 catch_exceptions_ftype *func,
168 void *func_args,
60250e8b
AC
169 char **gdberrmsg,
170 return_mask mask)
171{
7556d4a4 172 struct gdb_exception exception = exception_none;
2a78bfb5 173 volatile int val = 0;
f9679975 174 struct ui_out *saved_uiout;
d7f9d729 175
f9679975 176 /* Save and override the global ``struct ui_out'' builder. */
79a45e25
PA
177 saved_uiout = current_uiout;
178 current_uiout = func_uiout;
f9679975 179
492d29ea 180 TRY
6941d02a 181 {
79a45e25 182 val = (*func) (current_uiout, func_args);
6941d02a 183 }
492d29ea 184 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
185 {
186 exception = ex;
187 }
492d29ea 188 END_CATCH
f9679975
PA
189
190 /* Restore the global builder. */
79a45e25 191 current_uiout = saved_uiout;
f9679975
PA
192
193 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
194 {
195 /* The caller didn't request that the event be caught.
196 Rethrow. */
197 throw_exception (exception);
198 }
199
feefc97b 200 exception_print (gdb_stderr, exception);
60250e8b 201 gdb_assert (val >= 0);
2a78bfb5
AC
202 gdb_assert (exception.reason <= 0);
203 if (exception.reason < 0)
204 {
205 /* If caller wants a copy of the low-level error message, make
206 one. This is used in the case of a silent error whereby the
207 caller may optionally want to issue the message. */
208 if (gdberrmsg != NULL)
6b1b7650
AC
209 {
210 if (exception.message != NULL)
211 *gdberrmsg = xstrdup (exception.message);
212 else
213 *gdberrmsg = NULL;
214 }
2a78bfb5
AC
215 return exception.reason;
216 }
60250e8b
AC
217 return val;
218}
219
787274f0
DE
220/* This function is superseded by catch_exceptions(). */
221
60250e8b 222int
a121b7c1
PA
223catch_errors (catch_errors_ftype *func, void *func_args,
224 const char *errstring, return_mask mask)
60250e8b 225{
492d29ea 226 struct gdb_exception exception = exception_none;
2a78bfb5 227 volatile int val = 0;
f9679975 228 struct ui_out *saved_uiout;
d7f9d729 229
f9679975 230 /* Save the global ``struct ui_out'' builder. */
79a45e25 231 saved_uiout = current_uiout;
f9679975 232
492d29ea 233 TRY
6941d02a
AC
234 {
235 val = func (func_args);
236 }
492d29ea
PA
237 CATCH (ex, RETURN_MASK_ALL)
238 {
239 exception = ex;
240 }
241 END_CATCH
f9679975
PA
242
243 /* Restore the global builder. */
79a45e25 244 current_uiout = saved_uiout;
f9679975
PA
245
246 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
247 {
248 /* The caller didn't request that the event be caught.
249 Rethrow. */
250 throw_exception (exception);
251 }
252
feefc97b 253 exception_fprintf (gdb_stderr, exception, "%s", errstring);
2a78bfb5 254 if (exception.reason != 0)
60250e8b
AC
255 return 0;
256 return val;
257}
ecf45d2c
SL
258
259/* See exceptions.h. */
260
261int
262exception_print_same (struct gdb_exception e1, struct gdb_exception e2)
263{
264 const char *msg1 = e1.message;
265 const char *msg2 = e2.message;
266
267 if (msg1 == NULL)
268 msg1 = "";
269 if (msg2 == NULL)
270 msg2 = "";
271
272 return (e1.reason == e2.reason
273 && e1.error == e2.error
274 && strcmp (msg1, msg2) == 0);
275}
This page took 0.861647 seconds and 4 git commands to generate.