Commit | Line | Data |
---|---|---|
60250e8b AC |
1 | /* Exception (throw catch) mechanism, for GDB, the GNU debugger. |
2 | ||
618f726f | 3 | Copyright (C) 1986-2016 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" |
60250e8b | 29 | |
c25c4a8b | 30 | void |
ff55e1b5 | 31 | prepare_to_throw_exception (void) |
60250e8b | 32 | { |
60250e8b | 33 | immediate_quit = 0; |
2a78bfb5 AC |
34 | } |
35 | ||
6b1b7650 | 36 | static void |
c6da7a6d | 37 | print_flush (void) |
6b1b7650 | 38 | { |
e06e2353 | 39 | struct serial *gdb_stdout_serial; |
481ac8c9 | 40 | struct cleanup *old_chain = make_cleanup (null_cleanup, NULL); |
e06e2353 | 41 | |
c6da7a6d AC |
42 | if (deprecated_error_begin_hook) |
43 | deprecated_error_begin_hook (); | |
5df43998 GB |
44 | |
45 | if (target_supports_terminal_ours ()) | |
481ac8c9 PA |
46 | { |
47 | make_cleanup_restore_target_terminal (); | |
48 | target_terminal_ours_for_output (); | |
49 | } | |
e06e2353 AC |
50 | |
51 | /* We want all output to appear now, before we print the error. We | |
52 | have 3 levels of buffering we have to flush (it's possible that | |
53 | some of these should be changed to flush the lower-level ones | |
54 | too): */ | |
55 | ||
56 | /* 1. The _filtered buffer. */ | |
5df43998 GB |
57 | if (filtered_printing_initialized ()) |
58 | wrap_here (""); | |
e06e2353 AC |
59 | |
60 | /* 2. The stdio buffer. */ | |
c6da7a6d | 61 | gdb_flush (gdb_stdout); |
e06e2353 AC |
62 | gdb_flush (gdb_stderr); |
63 | ||
64 | /* 3. The system-level buffer. */ | |
65 | gdb_stdout_serial = serial_fdopen (1); | |
cade9e54 PB |
66 | if (gdb_stdout_serial) |
67 | { | |
68 | serial_drain_output (gdb_stdout_serial); | |
69 | serial_un_fdopen (gdb_stdout_serial); | |
70 | } | |
e06e2353 | 71 | |
c6da7a6d | 72 | annotate_error_begin (); |
481ac8c9 PA |
73 | |
74 | do_cleanups (old_chain); | |
6b1b7650 AC |
75 | } |
76 | ||
9cbc821d | 77 | static void |
71fff37b | 78 | print_exception (struct ui_file *file, struct gdb_exception e) |
9cbc821d AC |
79 | { |
80 | /* KLUGE: cagney/2005-01-13: Write the string out one line at a time | |
81 | as that way the MI's behavior is preserved. */ | |
82 | const char *start; | |
83 | const char *end; | |
d7f9d729 | 84 | |
9cbc821d AC |
85 | for (start = e.message; start != NULL; start = end) |
86 | { | |
87 | end = strchr (start, '\n'); | |
88 | if (end == NULL) | |
89 | fputs_filtered (start, file); | |
90 | else | |
91 | { | |
92 | end++; | |
93 | ui_file_write (file, start, end - start); | |
94 | } | |
95 | } | |
c6da7a6d | 96 | fprintf_filtered (file, "\n"); |
e48f5bee AC |
97 | |
98 | /* Now append the annotation. */ | |
99 | switch (e.reason) | |
100 | { | |
101 | case RETURN_QUIT: | |
102 | annotate_quit (); | |
103 | break; | |
104 | case RETURN_ERROR: | |
105 | /* Assume that these are all errors. */ | |
106 | annotate_error (); | |
107 | break; | |
108 | default: | |
109 | internal_error (__FILE__, __LINE__, _("Bad switch.")); | |
110 | } | |
9cbc821d AC |
111 | } |
112 | ||
8a076db9 | 113 | void |
71fff37b | 114 | exception_print (struct ui_file *file, struct gdb_exception e) |
8a076db9 AC |
115 | { |
116 | if (e.reason < 0 && e.message != NULL) | |
117 | { | |
c6da7a6d | 118 | print_flush (); |
9cbc821d | 119 | print_exception (file, e); |
9cbc821d AC |
120 | } |
121 | } | |
8a076db9 | 122 | |
9cbc821d | 123 | void |
71fff37b | 124 | exception_fprintf (struct ui_file *file, struct gdb_exception e, |
9cbc821d AC |
125 | const char *prefix, ...) |
126 | { | |
127 | if (e.reason < 0 && e.message != NULL) | |
128 | { | |
129 | va_list args; | |
c6da7a6d AC |
130 | |
131 | print_flush (); | |
9cbc821d AC |
132 | |
133 | /* Print the prefix. */ | |
134 | va_start (args, prefix); | |
135 | vfprintf_filtered (file, prefix, args); | |
136 | va_end (args); | |
137 | ||
138 | print_exception (file, e); | |
8a076db9 AC |
139 | } |
140 | } | |
141 | ||
787274f0 DE |
142 | /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception |
143 | handler. If an exception (enum return_reason) is thrown using | |
144 | throw_exception() than all cleanups installed since | |
145 | catch_exceptions() was entered are invoked, the (-ve) exception | |
146 | value is then returned by catch_exceptions. If FUNC() returns | |
147 | normally (with a positive or zero return value) then that value is | |
148 | returned by catch_exceptions(). It is an internal_error() for | |
149 | FUNC() to return a negative value. | |
150 | ||
151 | See exceptions.h for further usage details. | |
60250e8b AC |
152 | |
153 | Must not be called with immediate_quit in effect (bad things might | |
154 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
155 | This is an OK restriction; with very few exceptions immediate_quit can | |
787274f0 | 156 | be replaced by judicious use of QUIT. */ |
60250e8b AC |
157 | |
158 | /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with | |
7a9dd1b2 | 159 | error() et al. could maintain a set of flags that indicate the |
60250e8b AC |
160 | current state of each of the longjmp buffers. This would give the |
161 | longjmp code the chance to detect a longjmp botch (before it gets | |
162 | to longjmperror()). Prior to 1999-11-05 this wasn't possible as | |
163 | code also randomly used a SET_TOP_LEVEL macro that directly | |
0963b4bd | 164 | initialized the longjmp buffers. */ |
60250e8b | 165 | |
60250e8b AC |
166 | int |
167 | catch_exceptions (struct ui_out *uiout, | |
168 | catch_exceptions_ftype *func, | |
169 | void *func_args, | |
60250e8b AC |
170 | return_mask mask) |
171 | { | |
1c3c7ee7 | 172 | return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask); |
60250e8b AC |
173 | } |
174 | ||
175 | int | |
f9679975 | 176 | catch_exceptions_with_msg (struct ui_out *func_uiout, |
60250e8b AC |
177 | catch_exceptions_ftype *func, |
178 | void *func_args, | |
60250e8b AC |
179 | char **gdberrmsg, |
180 | return_mask mask) | |
181 | { | |
7556d4a4 | 182 | struct gdb_exception exception = exception_none; |
2a78bfb5 | 183 | volatile int val = 0; |
f9679975 | 184 | struct ui_out *saved_uiout; |
d7f9d729 | 185 | |
f9679975 | 186 | /* Save and override the global ``struct ui_out'' builder. */ |
79a45e25 PA |
187 | saved_uiout = current_uiout; |
188 | current_uiout = func_uiout; | |
f9679975 | 189 | |
492d29ea | 190 | TRY |
6941d02a | 191 | { |
79a45e25 | 192 | val = (*func) (current_uiout, func_args); |
6941d02a | 193 | } |
492d29ea | 194 | CATCH (ex, RETURN_MASK_ALL) |
7556d4a4 PA |
195 | { |
196 | exception = ex; | |
197 | } | |
492d29ea | 198 | END_CATCH |
f9679975 PA |
199 | |
200 | /* Restore the global builder. */ | |
79a45e25 | 201 | current_uiout = saved_uiout; |
f9679975 PA |
202 | |
203 | if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0) | |
204 | { | |
205 | /* The caller didn't request that the event be caught. | |
206 | Rethrow. */ | |
207 | throw_exception (exception); | |
208 | } | |
209 | ||
feefc97b | 210 | exception_print (gdb_stderr, exception); |
60250e8b | 211 | gdb_assert (val >= 0); |
2a78bfb5 AC |
212 | gdb_assert (exception.reason <= 0); |
213 | if (exception.reason < 0) | |
214 | { | |
215 | /* If caller wants a copy of the low-level error message, make | |
216 | one. This is used in the case of a silent error whereby the | |
217 | caller may optionally want to issue the message. */ | |
218 | if (gdberrmsg != NULL) | |
6b1b7650 AC |
219 | { |
220 | if (exception.message != NULL) | |
221 | *gdberrmsg = xstrdup (exception.message); | |
222 | else | |
223 | *gdberrmsg = NULL; | |
224 | } | |
2a78bfb5 AC |
225 | return exception.reason; |
226 | } | |
60250e8b AC |
227 | return val; |
228 | } | |
229 | ||
787274f0 DE |
230 | /* This function is superseded by catch_exceptions(). */ |
231 | ||
60250e8b AC |
232 | int |
233 | catch_errors (catch_errors_ftype *func, void *func_args, char *errstring, | |
234 | return_mask mask) | |
235 | { | |
492d29ea | 236 | struct gdb_exception exception = exception_none; |
2a78bfb5 | 237 | volatile int val = 0; |
f9679975 | 238 | struct ui_out *saved_uiout; |
d7f9d729 | 239 | |
f9679975 | 240 | /* Save the global ``struct ui_out'' builder. */ |
79a45e25 | 241 | saved_uiout = current_uiout; |
f9679975 | 242 | |
492d29ea | 243 | TRY |
6941d02a AC |
244 | { |
245 | val = func (func_args); | |
246 | } | |
492d29ea PA |
247 | CATCH (ex, RETURN_MASK_ALL) |
248 | { | |
249 | exception = ex; | |
250 | } | |
251 | END_CATCH | |
f9679975 PA |
252 | |
253 | /* Restore the global builder. */ | |
79a45e25 | 254 | current_uiout = saved_uiout; |
f9679975 PA |
255 | |
256 | if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0) | |
257 | { | |
258 | /* The caller didn't request that the event be caught. | |
259 | Rethrow. */ | |
260 | throw_exception (exception); | |
261 | } | |
262 | ||
feefc97b | 263 | exception_fprintf (gdb_stderr, exception, "%s", errstring); |
2a78bfb5 | 264 | if (exception.reason != 0) |
60250e8b AC |
265 | return 0; |
266 | return val; | |
267 | } |