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