1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
22 /* Local non-gdb includes. */
24 #include "breakpoint.h"
25 #include "common/gdb_optional.h"
26 #include "exceptions.h"
27 #include "gdbthread.h"
37 struct ui
*ui
= current_ui
;
38 struct serial
*gdb_stdout_serial
;
40 if (deprecated_error_begin_hook
)
41 deprecated_error_begin_hook ();
43 gdb::optional
<target_terminal::scoped_restore_terminal_state
> term_state
;
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
48 if (current_top_target () != NULL
&& target_supports_terminal_ours ())
50 term_state
.emplace ();
51 target_terminal::ours_for_output ();
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
59 /* 1. The _filtered buffer. */
60 if (filtered_printing_initialized ())
63 /* 2. The stdio buffer. */
64 gdb_flush (gdb_stdout
);
65 gdb_flush (gdb_stderr
);
67 /* 3. The system-level buffer. */
68 gdb_stdout_serial
= serial_fdopen (fileno (ui
->outstream
));
69 if (gdb_stdout_serial
)
71 serial_drain_output (gdb_stdout_serial
);
72 serial_un_fdopen (gdb_stdout_serial
);
75 annotate_error_begin ();
79 print_exception (struct ui_file
*file
, struct gdb_exception e
)
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. */
86 for (start
= e
.message
; start
!= NULL
; start
= end
)
88 end
= strchr (start
, '\n');
90 fputs_filtered (start
, file
);
94 ui_file_write (file
, start
, end
- start
);
97 fprintf_filtered (file
, "\n");
99 /* Now append the annotation. */
106 /* Assume that these are all errors. */
110 internal_error (__FILE__
, __LINE__
, _("Bad switch."));
115 exception_print (struct ui_file
*file
, struct gdb_exception e
)
117 if (e
.reason
< 0 && e
.message
!= NULL
)
120 print_exception (file
, e
);
125 exception_fprintf (struct ui_file
*file
, struct gdb_exception e
,
126 const char *prefix
, ...)
128 if (e
.reason
< 0 && e
.message
!= NULL
)
134 /* Print the prefix. */
135 va_start (args
, prefix
);
136 vfprintf_filtered (file
, prefix
, args
);
139 print_exception (file
, e
);
143 /* See exceptions.h. */
146 exception_print_same (struct gdb_exception e1
, struct gdb_exception e2
)
148 const char *msg1
= e1
.message
;
149 const char *msg2
= e2
.message
;
156 return (e1
.reason
== e2
.reason
157 && e1
.error
== e2
.error
158 && strcmp (msg1
, msg2
) == 0);