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