Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/binutils-gdb.git] / gdb / exceptions.c
CommitLineData
60250e8b
AC
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
42a4f53d 3 Copyright (C) 1986-2019 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"
d55e5aa6
TT
21
22/* Local non-gdb includes. */
23#include "annotate.h"
60250e8b 24#include "breakpoint.h"
d55e5aa6
TT
25#include "common/gdb_optional.h"
26#include "exceptions.h"
27#include "gdbthread.h"
60250e8b 28#include "inferior.h"
e06e2353 29#include "serial.h"
d55e5aa6 30#include "target.h"
694ec099 31#include "top.h"
d55e5aa6 32#include "ui-out.h"
60250e8b 33
6b1b7650 34static void
c6da7a6d 35print_flush (void)
6b1b7650 36{
694ec099 37 struct ui *ui = current_ui;
e06e2353
AC
38 struct serial *gdb_stdout_serial;
39
c6da7a6d
AC
40 if (deprecated_error_begin_hook)
41 deprecated_error_begin_hook ();
5df43998 42
223ffa71 43 gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
f6ac5f3d
PA
44 /* While normally there's always something pushed on the target
45 stack, the NULL check is needed here because we can get here very
46 early during startup, before the target stack is first
47 initialized. */
8b88a78e 48 if (current_top_target () != NULL && target_supports_terminal_ours ())
481ac8c9 49 {
223ffa71
TT
50 term_state.emplace ();
51 target_terminal::ours_for_output ();
481ac8c9 52 }
e06e2353
AC
53
54 /* We want all output to appear now, before we print the error. We
55 have 3 levels of buffering we have to flush (it's possible that
56 some of these should be changed to flush the lower-level ones
57 too): */
58
59 /* 1. The _filtered buffer. */
5df43998
GB
60 if (filtered_printing_initialized ())
61 wrap_here ("");
e06e2353
AC
62
63 /* 2. The stdio buffer. */
c6da7a6d 64 gdb_flush (gdb_stdout);
e06e2353
AC
65 gdb_flush (gdb_stderr);
66
67 /* 3. The system-level buffer. */
694ec099 68 gdb_stdout_serial = serial_fdopen (fileno (ui->outstream));
cade9e54
PB
69 if (gdb_stdout_serial)
70 {
71 serial_drain_output (gdb_stdout_serial);
72 serial_un_fdopen (gdb_stdout_serial);
73 }
e06e2353 74
c6da7a6d 75 annotate_error_begin ();
6b1b7650
AC
76}
77
9cbc821d 78static void
71fff37b 79print_exception (struct ui_file *file, struct gdb_exception e)
9cbc821d
AC
80{
81 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
82 as that way the MI's behavior is preserved. */
83 const char *start;
84 const char *end;
d7f9d729 85
9cbc821d
AC
86 for (start = e.message; start != NULL; start = end)
87 {
88 end = strchr (start, '\n');
89 if (end == NULL)
90 fputs_filtered (start, file);
91 else
92 {
93 end++;
94 ui_file_write (file, start, end - start);
95 }
96 }
c6da7a6d 97 fprintf_filtered (file, "\n");
e48f5bee
AC
98
99 /* Now append the annotation. */
100 switch (e.reason)
101 {
102 case RETURN_QUIT:
103 annotate_quit ();
104 break;
105 case RETURN_ERROR:
106 /* Assume that these are all errors. */
107 annotate_error ();
108 break;
109 default:
110 internal_error (__FILE__, __LINE__, _("Bad switch."));
111 }
9cbc821d
AC
112}
113
8a076db9 114void
71fff37b 115exception_print (struct ui_file *file, struct gdb_exception e)
8a076db9
AC
116{
117 if (e.reason < 0 && e.message != NULL)
118 {
c6da7a6d 119 print_flush ();
9cbc821d 120 print_exception (file, e);
9cbc821d
AC
121 }
122}
8a076db9 123
9cbc821d 124void
71fff37b 125exception_fprintf (struct ui_file *file, struct gdb_exception e,
9cbc821d
AC
126 const char *prefix, ...)
127{
128 if (e.reason < 0 && e.message != NULL)
129 {
130 va_list args;
c6da7a6d
AC
131
132 print_flush ();
9cbc821d
AC
133
134 /* Print the prefix. */
135 va_start (args, prefix);
136 vfprintf_filtered (file, prefix, args);
137 va_end (args);
138
139 print_exception (file, e);
8a076db9
AC
140 }
141}
142
ecf45d2c
SL
143/* See exceptions.h. */
144
145int
146exception_print_same (struct gdb_exception e1, struct gdb_exception e2)
147{
148 const char *msg1 = e1.message;
149 const char *msg2 = e2.message;
150
151 if (msg1 == NULL)
152 msg1 = "";
153 if (msg2 == NULL)
154 msg2 = "";
155
156 return (e1.reason == e2.reason
157 && e1.error == e2.error
158 && strcmp (msg1, msg2) == 0);
159}
This page took 0.966641 seconds and 4 git commands to generate.