Automatic date update in version.in
[binutils-gdb.git] / gdb / exceptions.c
blob368999829fce8217938ae1d243f6e270bc220578
1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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/>. */
20 #include "exceptions.h"
21 #include "breakpoint.h"
22 #include "target.h"
23 #include "inferior.h"
24 #include "annotate.h"
25 #include "ui-out.h"
26 #include "serial.h"
27 #include "gdbthread.h"
28 #include "ui.h"
29 #include <optional>
31 static void
32 print_flush (void)
34 struct ui *ui = current_ui;
35 struct serial *gdb_stdout_serial;
37 if (deprecated_error_begin_hook)
38 deprecated_error_begin_hook ();
40 std::optional<target_terminal::scoped_restore_terminal_state> term_state;
41 if (target_supports_terminal_ours ())
43 term_state.emplace ();
44 target_terminal::ours_for_output ();
47 /* We want all output to appear now, before we print the error. We
48 have 2 levels of buffering we have to flush (it's possible that
49 some of these should be changed to flush the lower-level ones
50 too): */
52 /* 1. The stdio buffer. */
53 gdb_flush (gdb_stdout);
54 gdb_flush (gdb_stderr);
56 /* 2. The system-level buffer. */
57 gdb_stdout_serial = serial_fdopen (fileno (ui->outstream));
58 if (gdb_stdout_serial)
60 serial_drain_output (gdb_stdout_serial);
61 serial_un_fdopen (gdb_stdout_serial);
64 annotate_error_begin ();
67 static void
68 print_exception (struct ui_file *file, const struct gdb_exception &e)
70 /* KLUDGE: cagney/2005-01-13: Write the string out one line at a time
71 as that way the MI's behavior is preserved. */
72 const char *start;
73 const char *end;
75 for (start = e.what (); start != NULL; start = end)
77 end = strchr (start, '\n');
78 if (end == NULL)
79 gdb_puts (start, file);
80 else
82 end++;
83 file->write (start, end - start);
86 gdb_printf (file, "\n");
88 /* Now append the annotation. */
89 switch (e.reason)
91 case RETURN_QUIT:
92 case RETURN_FORCED_QUIT:
93 annotate_quit ();
94 break;
95 case RETURN_ERROR:
96 /* Assume that these are all errors. */
97 annotate_error ();
98 break;
99 default:
100 internal_error (_("Bad switch."));
104 void
105 exception_print (struct ui_file *file, const struct gdb_exception &e)
107 if (e.reason < 0 && e.message != NULL)
109 print_flush ();
110 print_exception (file, e);
114 void
115 exception_fprintf (struct ui_file *file, const struct gdb_exception &e,
116 const char *prefix, ...)
118 if (e.reason < 0 && e.message != NULL)
120 va_list args;
122 print_flush ();
124 /* Print the prefix. */
125 va_start (args, prefix);
126 gdb_vprintf (file, prefix, args);
127 va_end (args);
129 print_exception (file, e);