Change len's type to ULONGEST: remote_write_bytes_aux
[deliverable/binutils-gdb.git] / gdb / exceptions.c
1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2014 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 "gdb_assert.h"
28 #include <string.h>
29 #include "serial.h"
30 #include "gdbthread.h"
31
32 const struct gdb_exception exception_none = { 0, GDB_NO_ERROR, NULL };
33
34 /* Possible catcher states. */
35 enum catcher_state {
36 /* Initial state, a new catcher has just been created. */
37 CATCHER_CREATED,
38 /* The catch code is running. */
39 CATCHER_RUNNING,
40 CATCHER_RUNNING_1,
41 /* The catch code threw an exception. */
42 CATCHER_ABORTING
43 };
44
45 /* Possible catcher actions. */
46 enum catcher_action {
47 CATCH_ITER,
48 CATCH_ITER_1,
49 CATCH_THROWING
50 };
51
52 struct catcher
53 {
54 enum catcher_state state;
55 /* Jump buffer pointing back at the exception handler. */
56 EXCEPTIONS_SIGJMP_BUF buf;
57 /* Status buffer belonging to the exception handler. */
58 volatile struct gdb_exception *exception;
59 /* Saved/current state. */
60 int mask;
61 struct cleanup *saved_cleanup_chain;
62 /* Back link. */
63 struct catcher *prev;
64 };
65
66 /* Where to go for throw_exception(). */
67 static struct catcher *current_catcher;
68
69 /* Return length of current_catcher list. */
70
71 static int
72 catcher_list_size (void)
73 {
74 int size;
75 struct catcher *catcher;
76
77 for (size = 0, catcher = current_catcher;
78 catcher != NULL;
79 catcher = catcher->prev)
80 ++size;
81
82 return size;
83 }
84
85 EXCEPTIONS_SIGJMP_BUF *
86 exceptions_state_mc_init (volatile struct gdb_exception *exception,
87 return_mask mask)
88 {
89 struct catcher *new_catcher = XCNEW (struct catcher);
90
91 /* Start with no exception, save it's address. */
92 exception->reason = 0;
93 exception->error = GDB_NO_ERROR;
94 exception->message = NULL;
95 new_catcher->exception = exception;
96
97 new_catcher->mask = mask;
98
99 /* Prevent error/quit during FUNC from calling cleanups established
100 prior to here. */
101 new_catcher->saved_cleanup_chain = save_cleanups ();
102
103 /* Push this new catcher on the top. */
104 new_catcher->prev = current_catcher;
105 current_catcher = new_catcher;
106 new_catcher->state = CATCHER_CREATED;
107
108 return &new_catcher->buf;
109 }
110
111 static void
112 catcher_pop (void)
113 {
114 struct catcher *old_catcher = current_catcher;
115
116 current_catcher = old_catcher->prev;
117
118 /* Restore the cleanup chain, the error/quit messages, and the uiout
119 builder, to their original states. */
120
121 restore_cleanups (old_catcher->saved_cleanup_chain);
122
123 xfree (old_catcher);
124 }
125
126 /* Catcher state machine. Returns non-zero if the m/c should be run
127 again, zero if it should abort. */
128
129 static int
130 exceptions_state_mc (enum catcher_action action)
131 {
132 switch (current_catcher->state)
133 {
134 case CATCHER_CREATED:
135 switch (action)
136 {
137 case CATCH_ITER:
138 /* Allow the code to run the catcher. */
139 current_catcher->state = CATCHER_RUNNING;
140 return 1;
141 default:
142 internal_error (__FILE__, __LINE__, _("bad state"));
143 }
144 case CATCHER_RUNNING:
145 switch (action)
146 {
147 case CATCH_ITER:
148 /* No error/quit has occured. Just clean up. */
149 catcher_pop ();
150 return 0;
151 case CATCH_ITER_1:
152 current_catcher->state = CATCHER_RUNNING_1;
153 return 1;
154 case CATCH_THROWING:
155 current_catcher->state = CATCHER_ABORTING;
156 /* See also throw_exception. */
157 return 1;
158 default:
159 internal_error (__FILE__, __LINE__, _("bad switch"));
160 }
161 case CATCHER_RUNNING_1:
162 switch (action)
163 {
164 case CATCH_ITER:
165 /* The did a "break" from the inner while loop. */
166 catcher_pop ();
167 return 0;
168 case CATCH_ITER_1:
169 current_catcher->state = CATCHER_RUNNING;
170 return 0;
171 case CATCH_THROWING:
172 current_catcher->state = CATCHER_ABORTING;
173 /* See also throw_exception. */
174 return 1;
175 default:
176 internal_error (__FILE__, __LINE__, _("bad switch"));
177 }
178 case CATCHER_ABORTING:
179 switch (action)
180 {
181 case CATCH_ITER:
182 {
183 struct gdb_exception exception = *current_catcher->exception;
184
185 if (current_catcher->mask & RETURN_MASK (exception.reason))
186 {
187 /* Exit normally if this catcher can handle this
188 exception. The caller analyses the func return
189 values. */
190 catcher_pop ();
191 return 0;
192 }
193 /* The caller didn't request that the event be caught,
194 relay the event to the next containing
195 catch_errors(). */
196 catcher_pop ();
197 throw_exception (exception);
198 }
199 default:
200 internal_error (__FILE__, __LINE__, _("bad state"));
201 }
202 default:
203 internal_error (__FILE__, __LINE__, _("bad switch"));
204 }
205 }
206
207 int
208 exceptions_state_mc_action_iter (void)
209 {
210 return exceptions_state_mc (CATCH_ITER);
211 }
212
213 int
214 exceptions_state_mc_action_iter_1 (void)
215 {
216 return exceptions_state_mc (CATCH_ITER_1);
217 }
218
219 /* Return EXCEPTION to the nearest containing catch_errors(). */
220
221 void
222 throw_exception (struct gdb_exception exception)
223 {
224 clear_quit_flag ();
225 immediate_quit = 0;
226
227 do_cleanups (all_cleanups ());
228
229 /* Jump to the containing catch_errors() call, communicating REASON
230 to that call via setjmp's return value. Note that REASON can't
231 be zero, by definition in defs.h. */
232 exceptions_state_mc (CATCH_THROWING);
233 *current_catcher->exception = exception;
234 EXCEPTIONS_SIGLONGJMP (current_catcher->buf, exception.reason);
235 }
236
237 static void
238 print_flush (void)
239 {
240 struct serial *gdb_stdout_serial;
241
242 if (deprecated_error_begin_hook)
243 deprecated_error_begin_hook ();
244 target_terminal_ours ();
245
246 /* We want all output to appear now, before we print the error. We
247 have 3 levels of buffering we have to flush (it's possible that
248 some of these should be changed to flush the lower-level ones
249 too): */
250
251 /* 1. The _filtered buffer. */
252 wrap_here ("");
253
254 /* 2. The stdio buffer. */
255 gdb_flush (gdb_stdout);
256 gdb_flush (gdb_stderr);
257
258 /* 3. The system-level buffer. */
259 gdb_stdout_serial = serial_fdopen (1);
260 if (gdb_stdout_serial)
261 {
262 serial_drain_output (gdb_stdout_serial);
263 serial_un_fdopen (gdb_stdout_serial);
264 }
265
266 annotate_error_begin ();
267 }
268
269 static void
270 print_exception (struct ui_file *file, struct gdb_exception e)
271 {
272 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
273 as that way the MI's behavior is preserved. */
274 const char *start;
275 const char *end;
276
277 for (start = e.message; start != NULL; start = end)
278 {
279 end = strchr (start, '\n');
280 if (end == NULL)
281 fputs_filtered (start, file);
282 else
283 {
284 end++;
285 ui_file_write (file, start, end - start);
286 }
287 }
288 fprintf_filtered (file, "\n");
289
290 /* Now append the annotation. */
291 switch (e.reason)
292 {
293 case RETURN_QUIT:
294 annotate_quit ();
295 break;
296 case RETURN_ERROR:
297 /* Assume that these are all errors. */
298 annotate_error ();
299 break;
300 default:
301 internal_error (__FILE__, __LINE__, _("Bad switch."));
302 }
303 }
304
305 void
306 exception_print (struct ui_file *file, struct gdb_exception e)
307 {
308 if (e.reason < 0 && e.message != NULL)
309 {
310 print_flush ();
311 print_exception (file, e);
312 }
313 }
314
315 void
316 exception_fprintf (struct ui_file *file, struct gdb_exception e,
317 const char *prefix, ...)
318 {
319 if (e.reason < 0 && e.message != NULL)
320 {
321 va_list args;
322
323 print_flush ();
324
325 /* Print the prefix. */
326 va_start (args, prefix);
327 vfprintf_filtered (file, prefix, args);
328 va_end (args);
329
330 print_exception (file, e);
331 }
332 }
333
334 static void
335 print_any_exception (struct ui_file *file, const char *prefix,
336 struct gdb_exception e)
337 {
338 if (e.reason < 0 && e.message != NULL)
339 {
340 target_terminal_ours ();
341 wrap_here (""); /* Force out any buffered output. */
342 gdb_flush (gdb_stdout);
343 annotate_error_begin ();
344
345 /* Print the prefix. */
346 if (prefix != NULL && prefix[0] != '\0')
347 fputs_filtered (prefix, file);
348 print_exception (file, e);
349 }
350 }
351
352 /* A stack of exception messages.
353 This is needed to handle nested calls to throw_it: we don't want to
354 xfree space for a message before it's used.
355 This can happen if we throw an exception during a cleanup:
356 An outer TRY_CATCH may have an exception message it wants to print,
357 but while doing cleanups further calls to throw_it are made.
358
359 This is indexed by the size of the current_catcher list.
360 It is a dynamically allocated array so that we don't care how deeply
361 GDB nests its TRY_CATCHs. */
362 static char **exception_messages;
363
364 /* The number of currently allocated entries in exception_messages. */
365 static int exception_messages_size;
366
367 static void ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (3, 0)
368 throw_it (enum return_reason reason, enum errors error, const char *fmt,
369 va_list ap)
370 {
371 struct gdb_exception e;
372 char *new_message;
373 int depth = catcher_list_size ();
374
375 gdb_assert (depth > 0);
376
377 /* Note: The new message may use an old message's text. */
378 new_message = xstrvprintf (fmt, ap);
379
380 if (depth > exception_messages_size)
381 {
382 int old_size = exception_messages_size;
383
384 exception_messages_size = depth + 10;
385 exception_messages = (char **) xrealloc (exception_messages,
386 exception_messages_size
387 * sizeof (char *));
388 memset (exception_messages + old_size, 0,
389 (exception_messages_size - old_size) * sizeof (char *));
390 }
391
392 xfree (exception_messages[depth - 1]);
393 exception_messages[depth - 1] = new_message;
394
395 /* Create the exception. */
396 e.reason = reason;
397 e.error = error;
398 e.message = new_message;
399
400 /* Throw the exception. */
401 throw_exception (e);
402 }
403
404 void
405 throw_verror (enum errors error, const char *fmt, va_list ap)
406 {
407 throw_it (RETURN_ERROR, error, fmt, ap);
408 }
409
410 void
411 throw_vfatal (const char *fmt, va_list ap)
412 {
413 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
414 }
415
416 void
417 throw_error (enum errors error, const char *fmt, ...)
418 {
419 va_list args;
420
421 va_start (args, fmt);
422 throw_it (RETURN_ERROR, error, fmt, args);
423 va_end (args);
424 }
425
426 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
427 handler. If an exception (enum return_reason) is thrown using
428 throw_exception() than all cleanups installed since
429 catch_exceptions() was entered are invoked, the (-ve) exception
430 value is then returned by catch_exceptions. If FUNC() returns
431 normally (with a positive or zero return value) then that value is
432 returned by catch_exceptions(). It is an internal_error() for
433 FUNC() to return a negative value.
434
435 See exceptions.h for further usage details.
436
437 Must not be called with immediate_quit in effect (bad things might
438 happen, say we got a signal in the middle of a memcpy to quit_return).
439 This is an OK restriction; with very few exceptions immediate_quit can
440 be replaced by judicious use of QUIT. */
441
442 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
443 error() et al. could maintain a set of flags that indicate the
444 current state of each of the longjmp buffers. This would give the
445 longjmp code the chance to detect a longjmp botch (before it gets
446 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
447 code also randomly used a SET_TOP_LEVEL macro that directly
448 initialized the longjmp buffers. */
449
450 int
451 catch_exceptions (struct ui_out *uiout,
452 catch_exceptions_ftype *func,
453 void *func_args,
454 return_mask mask)
455 {
456 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
457 }
458
459 int
460 catch_exceptions_with_msg (struct ui_out *func_uiout,
461 catch_exceptions_ftype *func,
462 void *func_args,
463 char **gdberrmsg,
464 return_mask mask)
465 {
466 volatile struct gdb_exception exception;
467 volatile int val = 0;
468 struct ui_out *saved_uiout;
469
470 /* Save and override the global ``struct ui_out'' builder. */
471 saved_uiout = current_uiout;
472 current_uiout = func_uiout;
473
474 TRY_CATCH (exception, RETURN_MASK_ALL)
475 {
476 val = (*func) (current_uiout, func_args);
477 }
478
479 /* Restore the global builder. */
480 current_uiout = saved_uiout;
481
482 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
483 {
484 /* The caller didn't request that the event be caught.
485 Rethrow. */
486 throw_exception (exception);
487 }
488
489 print_any_exception (gdb_stderr, NULL, exception);
490 gdb_assert (val >= 0);
491 gdb_assert (exception.reason <= 0);
492 if (exception.reason < 0)
493 {
494 /* If caller wants a copy of the low-level error message, make
495 one. This is used in the case of a silent error whereby the
496 caller may optionally want to issue the message. */
497 if (gdberrmsg != NULL)
498 {
499 if (exception.message != NULL)
500 *gdberrmsg = xstrdup (exception.message);
501 else
502 *gdberrmsg = NULL;
503 }
504 return exception.reason;
505 }
506 return val;
507 }
508
509 /* This function is superseded by catch_exceptions(). */
510
511 int
512 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
513 return_mask mask)
514 {
515 volatile int val = 0;
516 volatile struct gdb_exception exception;
517 struct ui_out *saved_uiout;
518
519 /* Save the global ``struct ui_out'' builder. */
520 saved_uiout = current_uiout;
521
522 TRY_CATCH (exception, RETURN_MASK_ALL)
523 {
524 val = func (func_args);
525 }
526
527 /* Restore the global builder. */
528 current_uiout = saved_uiout;
529
530 if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
531 {
532 /* The caller didn't request that the event be caught.
533 Rethrow. */
534 throw_exception (exception);
535 }
536
537 print_any_exception (gdb_stderr, errstring, exception);
538 if (exception.reason != 0)
539 return 0;
540 return val;
541 }
542
543 int
544 catch_command_errors (catch_command_errors_ftype *command,
545 char *arg, int from_tty, return_mask mask)
546 {
547 volatile struct gdb_exception e;
548
549 TRY_CATCH (e, mask)
550 {
551 command (arg, from_tty);
552 }
553 print_any_exception (gdb_stderr, NULL, e);
554 if (e.reason < 0)
555 return 0;
556 return 1;
557 }
558
559 int
560 catch_command_errors_const (catch_command_errors_const_ftype *command,
561 const char *arg, int from_tty, return_mask mask)
562 {
563 volatile struct gdb_exception e;
564
565 TRY_CATCH (e, mask)
566 {
567 command (arg, from_tty);
568 }
569 print_any_exception (gdb_stderr, NULL, e);
570 if (e.reason < 0)
571 return 0;
572 return 1;
573 }
This page took 0.041453 seconds and 4 git commands to generate.