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