gdb: Replace operator new / operator new[]
[deliverable/binutils-gdb.git] / gdb / common / common-exceptions.h
CommitLineData
ff55e1b5
GB
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
ff55e1b5
GB
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#ifndef COMMON_EXCEPTIONS_H
21#define COMMON_EXCEPTIONS_H
22
173981bc 23#include <setjmp.h>
503b1c39 24#include <new>
ff55e1b5
GB
25
26/* Reasons for calling throw_exceptions(). NOTE: all reason values
27 must be less than zero. enum value 0 is reserved for internal use
28 as the return value from an initial setjmp(). The function
29 catch_exceptions() reserves values >= 0 as legal results from its
30 wrapped function. */
31
32enum return_reason
33 {
34 /* User interrupt. */
35 RETURN_QUIT = -2,
36 /* Any other error. */
37 RETURN_ERROR
38 };
39
40#define RETURN_MASK(reason) (1 << (int)(-reason))
41
42typedef enum
43{
44 RETURN_MASK_QUIT = RETURN_MASK (RETURN_QUIT),
45 RETURN_MASK_ERROR = RETURN_MASK (RETURN_ERROR),
46 RETURN_MASK_ALL = (RETURN_MASK_QUIT | RETURN_MASK_ERROR)
47} return_mask;
48
49/* Describe all exceptions. */
50
51enum errors {
52 GDB_NO_ERROR,
53
54 /* Any generic error, the corresponding text is in
55 exception.message. */
56 GENERIC_ERROR,
57
58 /* Something requested was not found. */
59 NOT_FOUND_ERROR,
60
61 /* Thread library lacks support necessary for finding thread local
62 storage. */
63 TLS_NO_LIBRARY_SUPPORT_ERROR,
64
65 /* Load module not found while attempting to find thread local storage. */
66 TLS_LOAD_MODULE_NOT_FOUND_ERROR,
67
68 /* Thread local storage has not been allocated yet. */
69 TLS_NOT_ALLOCATED_YET_ERROR,
70
71 /* Something else went wrong while attempting to find thread local
72 storage. The ``struct gdb_exception'' message field provides
73 more detail. */
74 TLS_GENERIC_ERROR,
75
76 /* Problem parsing an XML document. */
77 XML_PARSE_ERROR,
78
79 /* Error accessing memory. */
80 MEMORY_ERROR,
81
82 /* Value not available. E.g., a register was not collected in a
83 traceframe. */
84 NOT_AVAILABLE_ERROR,
85
86 /* Value was optimized out. Note: if the value was a register, this
87 means the register was not saved in the frame. */
88 OPTIMIZED_OUT_ERROR,
89
90 /* DW_OP_GNU_entry_value resolving failed. */
91 NO_ENTRY_VALUE_ERROR,
92
93 /* Target throwing an error has been closed. Current command should be
94 aborted as the inferior state is no longer valid. */
95 TARGET_CLOSE_ERROR,
96
97 /* An undefined command was executed. */
98 UNDEFINED_COMMAND_ERROR,
99
100 /* Requested feature, method, mechanism, etc. is not supported. */
101 NOT_SUPPORTED_ERROR,
102
ef0b411a
GB
103 /* The number of candidates generated during line completion has
104 reached the user's specified limit. This isn't an error, this exception
105 is used to halt searching for more completions, but for consistency
106 "_ERROR" is appended to the name. */
107 MAX_COMPLETIONS_REACHED_ERROR,
108
ff55e1b5
GB
109 /* Add more errors here. */
110 NR_ERRORS
111};
112
113struct gdb_exception
114{
115 enum return_reason reason;
116 enum errors error;
117 const char *message;
118};
119
eec461d0
PA
120/* The different exception mechanisms that TRY/CATCH can map to. */
121
122/* Make GDB exceptions use setjmp/longjmp behind the scenes. This is
123 the only mode supported when GDB is built as a C program. */
124#define GDB_XCPT_SJMP 1
125
6290672f 126/* Make GDB exceptions use try/catch behind the scenes. */
eec461d0
PA
127#define GDB_XCPT_TRY 2
128
129/* Specify this mode to build with TRY/CATCH mapped directly to raw
130 try/catch. GDB won't work correctly, but building that way catches
131 code tryin to break/continue out of the try block, along with
132 spurious code between the TRY and the CATCH block. */
133#define GDB_XCPT_RAW_TRY 3
134
6290672f
PA
135#ifdef __cplusplus
136# define GDB_XCPT GDB_XCPT_TRY
137#else
138# define GDB_XCPT GDB_XCPT_SJMP
139#endif
eec461d0 140
89525768
PA
141/* Functions to drive the sjlj-based exceptions state machine. Though
142 declared here by necessity, these functions should be considered
143 internal to the exceptions subsystem and not used other than via
144 the TRY/CATCH (or TRY_SJLJ/CATCH_SJLJ) macros defined below. */
ff55e1b5 145
173981bc 146extern jmp_buf *exceptions_state_mc_init (void);
ff55e1b5
GB
147extern int exceptions_state_mc_action_iter (void);
148extern int exceptions_state_mc_action_iter_1 (void);
492d29ea 149extern int exceptions_state_mc_catch (struct gdb_exception *, int);
89525768
PA
150
151/* Same, but for the C++ try/catch-based TRY/CATCH mechanism. */
152
153#if GDB_XCPT != GDB_XCPT_SJMP
72df25b2
PA
154extern void *exception_try_scope_entry (void);
155extern void exception_try_scope_exit (void *saved_state);
156extern void exception_rethrow (void);
157#endif
ff55e1b5
GB
158
159/* Macro to wrap up standard try/catch behavior.
160
161 The double loop lets us correctly handle code "break"ing out of the
162 try catch block. (It works as the "break" only exits the inner
163 "while" loop, the outer for loop detects this handling it
164 correctly.) Of course "return" and "goto" are not so lucky.
165
166 For instance:
167
168 *INDENT-OFF*
169
492d29ea 170 TRY
ff55e1b5
GB
171 {
172 }
492d29ea 173 CATCH (e, RETURN_MASK_ERROR)
ff55e1b5 174 {
492d29ea
PA
175 switch (e.reason)
176 {
177 case RETURN_ERROR: ...
178 }
ff55e1b5 179 }
492d29ea 180 END_CATCH
ff55e1b5 181
89525768
PA
182 Note that the SJLJ version of the macros are actually named
183 TRY_SJLJ/CATCH_SJLJ in order to make it possible to call them even
184 when TRY/CATCH are mapped to C++ try/catch. The SJLJ variants are
185 needed in some cases where gdb exceptions need to cross third-party
186 library code compiled without exceptions support (e.g.,
187 readline). */
ff55e1b5 188
89525768 189#define TRY_SJLJ \
ff55e1b5 190 { \
173981bc 191 jmp_buf *buf = \
492d29ea 192 exceptions_state_mc_init (); \
173981bc 193 setjmp (*buf); \
ff55e1b5
GB
194 } \
195 while (exceptions_state_mc_action_iter ()) \
196 while (exceptions_state_mc_action_iter_1 ())
197
89525768 198#define CATCH_SJLJ(EXCEPTION, MASK) \
492d29ea
PA
199 { \
200 struct gdb_exception EXCEPTION; \
201 if (exceptions_state_mc_catch (&(EXCEPTION), MASK))
202
89525768 203#define END_CATCH_SJLJ \
492d29ea
PA
204 }
205
89525768
PA
206#if GDB_XCPT == GDB_XCPT_SJMP
207
208/* If using SJLJ-based exceptions for all exceptions, then provide
209 standard aliases. */
210
211#define TRY TRY_SJLJ
212#define CATCH CATCH_SJLJ
213#define END_CATCH END_CATCH_SJLJ
214
eec461d0
PA
215#endif /* GDB_XCPT_SJMP */
216
217#if GDB_XCPT == GDB_XCPT_TRY || GDB_XCPT == GDB_XCPT_RAW_TRY
72df25b2
PA
218
219/* Prevent error/quit during TRY from calling cleanups established
220 prior to here. This pops out the scope in either case of normal
221 exit or exception exit. */
222struct exception_try_scope
223{
224 exception_try_scope ()
225 {
226 saved_state = exception_try_scope_entry ();
227 }
228 ~exception_try_scope ()
229 {
230 exception_try_scope_exit (saved_state);
231 }
232
233 void *saved_state;
234};
235
eec461d0 236#if GDB_XCPT == GDB_XCPT_TRY
9c6595ab 237
72df25b2
PA
238/* We still need to wrap TRY/CATCH in C++ so that cleanups and C++
239 exceptions can coexist. The TRY blocked is wrapped in a
240 do/while(0) so that break/continue within the block works the same
241 as in C. */
242#define TRY \
243 try \
244 { \
245 exception_try_scope exception_try_scope_instance; \
246 do \
247 {
248
249#define CATCH(EXCEPTION, MASK) \
250 } while (0); \
251 } \
252 catch (struct gdb_exception ## _ ## MASK &EXCEPTION)
253
254#define END_CATCH \
255 catch (...) \
256 { \
257 exception_rethrow (); \
258 }
eec461d0 259
9c6595ab
PA
260#else
261
262#define TRY try
263#define CATCH(EXCEPTION, MASK) \
264 catch (struct gdb_exception ## _ ## MASK &EXCEPTION)
265#define END_CATCH
266
267#endif
72df25b2
PA
268
269/* The exception types client code may catch. They're just shims
270 around gdb_exception that add nothing but type info. Which is used
271 is selected depending on the MASK argument passed to CATCH. */
272
273struct gdb_exception_RETURN_MASK_ALL : public gdb_exception
274{
275};
276
277struct gdb_exception_RETURN_MASK_ERROR : public gdb_exception_RETURN_MASK_ALL
278{
279};
280
281struct gdb_exception_RETURN_MASK_QUIT : public gdb_exception_RETURN_MASK_ALL
282{
283};
284
eec461d0 285#endif /* GDB_XCPT_TRY || GDB_XCPT_RAW_TRY */
72df25b2 286
503b1c39
PA
287/* An exception type that inherits from both std::bad_alloc and a gdb
288 exception. This is necessary because operator new can only throw
289 std::bad_alloc, and OTOH, we want exceptions thrown due to memory
290 allocation error to be caught by all the CATCH/RETURN_MASK_ALL
291 spread around the codebase. */
292
293struct gdb_quit_bad_alloc
294 : public gdb_exception_RETURN_MASK_QUIT,
295 public std::bad_alloc
296{
297 explicit gdb_quit_bad_alloc (gdb_exception ex)
298 : std::bad_alloc ()
299 {
300 gdb_exception *self = this;
301
302 *self = ex;
303 }
304};
305
ff55e1b5
GB
306/* *INDENT-ON* */
307
89525768
PA
308/* Throw an exception (as described by "struct gdb_exception"). When
309 GDB is built as a C program, executes a LONG JUMP to the inner most
310 containing exception handler established using TRY/CATCH. When
311 built as a C++ program, throws a C++ exception, using "throw". */
ff55e1b5
GB
312extern void throw_exception (struct gdb_exception exception)
313 ATTRIBUTE_NORETURN;
89525768
PA
314
315/* Throw an exception by executing a LONG JUMP to the inner most
316 containing exception handler established using TRY_SJLJ. Works the
317 same regardless of whether GDB is built as a C program or a C++
318 program. Necessary in some cases where we need to throw GDB
319 exceptions across third-party library code (e.g., readline). */
320extern void throw_exception_sjlj (struct gdb_exception exception)
321 ATTRIBUTE_NORETURN;
322
323/* Convenience wrappers around throw_exception that throw GDB
324 errors. */
ff55e1b5
GB
325extern void throw_verror (enum errors, const char *fmt, va_list ap)
326 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 0);
327extern void throw_vquit (const char *fmt, va_list ap)
328 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 0);
329extern void throw_error (enum errors error, const char *fmt, ...)
330 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (2, 3);
331extern void throw_quit (const char *fmt, ...)
332 ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
333
ad6aff7d
PA
334/* A pre-defined non-exception. */
335extern const struct gdb_exception exception_none;
336
ff55e1b5 337#endif /* COMMON_EXCEPTIONS_H */
This page took 0.165805 seconds and 4 git commands to generate.