GDB (xrefs)
/tmp/gdb-7.10/gdb/exceptions.c
Go to the documentation of this file.
1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2 
3  Copyright (C) 1986-2015 Free Software Foundation, Inc.
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 #include "defs.h"
21 #include "exceptions.h"
22 #include "breakpoint.h"
23 #include "target.h"
24 #include "inferior.h"
25 #include "annotate.h"
26 #include "ui-out.h"
27 #include "serial.h"
28 #include "gdbthread.h"
29 
30 void
32 {
33  clear_quit_flag ();
34  immediate_quit = 0;
35 }
36 
37 static void
39 {
40  struct serial *gdb_stdout_serial;
41 
44 
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. */
55  wrap_here ("");
56 
57  /* 2. The stdio buffer. */
60 
61  /* 3. The system-level buffer. */
62  gdb_stdout_serial = serial_fdopen (1);
63  if (gdb_stdout_serial)
64  {
65  serial_drain_output (gdb_stdout_serial);
66  serial_un_fdopen (gdb_stdout_serial);
67  }
68 
70 }
71 
72 static void
73 print_exception (struct ui_file *file, struct gdb_exception e)
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;
79 
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  }
91  fprintf_filtered (file, "\n");
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  }
106 }
107 
108 void
109 exception_print (struct ui_file *file, struct gdb_exception e)
110 {
111  if (e.reason < 0 && e.message != NULL)
112  {
113  print_flush ();
114  print_exception (file, e);
115  }
116 }
117 
118 void
119 exception_fprintf (struct ui_file *file, struct gdb_exception e,
120  const char *prefix, ...)
121 {
122  if (e.reason < 0 && e.message != NULL)
123  {
124  va_list args;
125 
126  print_flush ();
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);
134  }
135 }
136 
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 
146  See exceptions.h for further usage details.
147 
148  Must not be called with immediate_quit in effect (bad things might
149  happen, say we got a signal in the middle of a memcpy to quit_return).
150  This is an OK restriction; with very few exceptions immediate_quit can
151  be replaced by judicious use of QUIT. */
152 
153 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
154  error() et al. could maintain a set of flags that indicate the
155  current state of each of the longjmp buffers. This would give the
156  longjmp code the chance to detect a longjmp botch (before it gets
157  to longjmperror()). Prior to 1999-11-05 this wasn't possible as
158  code also randomly used a SET_TOP_LEVEL macro that directly
159  initialized the longjmp buffers. */
160 
161 int
162 catch_exceptions (struct ui_out *uiout,
164  void *func_args,
165  return_mask mask)
166 {
167  return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
168 }
169 
170 int
171 catch_exceptions_with_msg (struct ui_out *func_uiout,
173  void *func_args,
174  char **gdberrmsg,
175  return_mask mask)
176 {
177  struct gdb_exception exception = exception_none;
178  volatile int val = 0;
179  struct ui_out *saved_uiout;
180 
181  /* Save and override the global ``struct ui_out'' builder. */
182  saved_uiout = current_uiout;
183  current_uiout = func_uiout;
184 
185  TRY
186  {
187  val = (*func) (current_uiout, func_args);
188  }
189  CATCH (ex, RETURN_MASK_ALL)
190  {
191  exception = ex;
192  }
193  END_CATCH
194 
195  /* Restore the global builder. */
196  current_uiout = saved_uiout;
197 
198  if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
199  {
200  /* The caller didn't request that the event be caught.
201  Rethrow. */
202  throw_exception (exception);
203  }
204 
205  exception_print (gdb_stderr, exception);
206  gdb_assert (val >= 0);
207  gdb_assert (exception.reason <= 0);
208  if (exception.reason < 0)
209  {
210  /* If caller wants a copy of the low-level error message, make
211  one. This is used in the case of a silent error whereby the
212  caller may optionally want to issue the message. */
213  if (gdberrmsg != NULL)
214  {
215  if (exception.message != NULL)
216  *gdberrmsg = xstrdup (exception.message);
217  else
218  *gdberrmsg = NULL;
219  }
220  return exception.reason;
221  }
222  return val;
223 }
224 
225 /* This function is superseded by catch_exceptions(). */
226 
227 int
228 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
229  return_mask mask)
230 {
231  struct gdb_exception exception = exception_none;
232  volatile int val = 0;
233  struct ui_out *saved_uiout;
234 
235  /* Save the global ``struct ui_out'' builder. */
236  saved_uiout = current_uiout;
237 
238  TRY
239  {
240  val = func (func_args);
241  }
242  CATCH (ex, RETURN_MASK_ALL)
243  {
244  exception = ex;
245  }
246  END_CATCH
247 
248  /* Restore the global builder. */
249  current_uiout = saved_uiout;
250 
251  if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
252  {
253  /* The caller didn't request that the event be caught.
254  Rethrow. */
255  throw_exception (exception);
256  }
257 
258  exception_fprintf (gdb_stderr, exception, "%s", errstring);
259  if (exception.reason != 0)
260  return 0;
261  return val;
262 }
void target_terminal_ours(void)
Definition: target.c:491
int serial_drain_output(struct serial *scb)
Definition: serial.c:449
int target_supports_terminal_ours(void)
Definition: target.c:514
static void print_flush(void)
Definition: exceptions.c:38
int( catch_exceptions_ftype)(struct ui_out *ui_out, void *args)
Definition: exceptions.h:68
void clear_quit_flag(void)
Definition: extension.c:793
void(* func)(char *)
int catch_exceptions(struct ui_out *uiout, catch_exceptions_ftype *func, void *func_args, return_mask mask)
Definition: exceptions.c:162
int( catch_errors_ftype)(void *)
Definition: exceptions.h:88
#define RETURN_MASK(reason)
struct ui_file * gdb_stdout
Definition: main.c:71
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
const struct gdb_exception exception_none
int catch_errors(catch_errors_ftype *func, void *func_args, char *errstring, return_mask mask)
Definition: exceptions.c:228
#define _(String)
Definition: gdb_locale.h:40
#define END_CATCH
Definition: ui-out.c:99
#define TRY
struct serial * serial_fdopen(const int fd)
Definition: serial.c:291
#define CATCH(EXCEPTION, MASK)
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2351
void serial_un_fdopen(struct serial *scb)
Definition: serial.c:346
void vfprintf_filtered(struct ui_file *stream, const char *format, va_list args)
Definition: utils.c:2302
void exception_print(struct ui_file *file, struct gdb_exception e)
Definition: exceptions.c:109
void fputs_filtered(const char *linebuffer, struct ui_file *stream)
Definition: utils.c:2145
static void print_exception(struct ui_file *file, struct gdb_exception e)
Definition: exceptions.c:73
#define gdb_assert(expr)
Definition: gdb_assert.h:33
void exception_fprintf(struct ui_file *file, struct gdb_exception e, const char *prefix,...)
Definition: exceptions.c:119
Definition: serial.h:227
int filtered_printing_initialized(void)
Definition: utils.c:1703
void annotate_error_begin(void)
Definition: annotate.c:302
void wrap_here(char *indent)
Definition: utils.c:1930
void prepare_to_throw_exception(void)
Definition: exceptions.c:31
void(* deprecated_error_begin_hook)(void)
Definition: utils.c:82
return_mask
void throw_exception(struct gdb_exception exception)
int catch_exceptions_with_msg(struct ui_out *func_uiout, catch_exceptions_ftype *func, void *func_args, char **gdberrmsg, return_mask mask)
Definition: exceptions.c:171
int immediate_quit
struct ui_file * gdb_stderr
Definition: main.c:72
void ui_file_write(struct ui_file *file, const char *buf, long length_buf)
Definition: ui-file.c:218
void annotate_quit(void)
Definition: annotate.c:288
const char * message
void annotate_error(void)
Definition: annotate.c:295
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:192
struct ui_out * current_uiout
Definition: ui-out.c:233
enum return_reason reason