GDB (xrefs)
Loading...
Searching...
No Matches
exceptions.c
Go to the documentation of this file.
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2023 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 "defs.h"
21#include "exceptions.h"
22#include "breakpoint.h"
23#include "target.h"
24#include "inferior.h"
25#include "annotate.h"
26#include "ui-out.h"
27#include "serial.h"
28#include "gdbthread.h"
29#include "ui.h"
30#include "gdbsupport/gdb_optional.h"
31
32static void
34{
35 struct ui *ui = current_ui;
36 struct serial *gdb_stdout_serial;
37
40
41 gdb::optional<target_terminal::scoped_restore_terminal_state> term_state;
43 {
44 term_state.emplace ();
46 }
47
48 /* We want all output to appear now, before we print the error. We
49 have 2 levels of buffering we have to flush (it's possible that
50 some of these should be changed to flush the lower-level ones
51 too): */
52
53 /* 1. The stdio buffer. */
56
57 /* 2. The system-level buffer. */
58 gdb_stdout_serial = serial_fdopen (fileno (ui->outstream));
59 if (gdb_stdout_serial)
60 {
61 serial_drain_output (gdb_stdout_serial);
62 serial_un_fdopen (gdb_stdout_serial);
63 }
64
66}
67
68static void
69print_exception (struct ui_file *file, const struct gdb_exception &e)
70{
71 /* KLUDGE: cagney/2005-01-13: Write the string out one line at a time
72 as that way the MI's behavior is preserved. */
73 const char *start;
74 const char *end;
75
76 for (start = e.what (); start != NULL; start = end)
77 {
78 end = strchr (start, '\n');
79 if (end == NULL)
80 gdb_puts (start, file);
81 else
82 {
83 end++;
84 file->write (start, end - start);
85 }
86 }
87 gdb_printf (file, "\n");
88
89 /* Now append the annotation. */
90 switch (e.reason)
91 {
92 case RETURN_QUIT:
93 case RETURN_FORCED_QUIT:
95 break;
96 case RETURN_ERROR:
97 /* Assume that these are all errors. */
99 break;
100 default:
101 internal_error (_("Bad switch."));
102 }
103}
104
105void
106exception_print (struct ui_file *file, const struct gdb_exception &e)
107{
108 if (e.reason < 0 && e.message != NULL)
109 {
110 print_flush ();
111 print_exception (file, e);
112 }
113}
114
115void
116exception_fprintf (struct ui_file *file, const struct gdb_exception &e,
117 const char *prefix, ...)
118{
119 if (e.reason < 0 && e.message != NULL)
120 {
121 va_list args;
122
123 print_flush ();
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}
void annotate_error_begin(void)
Definition annotate.c:294
void annotate_quit(void)
Definition annotate.c:280
void annotate_error(void)
Definition annotate.c:287
static void ours_for_output()
Definition target.c:1088
void exception_fprintf(struct ui_file *file, const struct gdb_exception &e, const char *prefix,...)
Definition exceptions.c:116
void exception_print(struct ui_file *file, const struct gdb_exception &e)
Definition exceptions.c:106
static void print_flush(void)
Definition exceptions.c:33
static void print_exception(struct ui_file *file, const struct gdb_exception &e)
Definition exceptions.c:69
#define prefix(a, b, R, do)
Definition ppc64-tdep.c:52
void serial_un_fdopen(struct serial *scb)
Definition serial.c:357
int serial_drain_output(struct serial *scb)
Definition serial.c:457
struct serial * serial_fdopen(const int fd)
Definition serial.c:303
Definition ui.h:55
FILE * outstream
Definition ui.h:121
bool target_supports_terminal_ours(void)
Definition target.c:1114
struct ui * current_ui
Definition ui.c:35
void gdb_vprintf(struct ui_file *stream, const char *format, va_list args)
Definition utils.c:1874
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void(* deprecated_error_begin_hook)(void)
Definition utils.c:85
void gdb_flush(struct ui_file *stream)
Definition utils.c:1498
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition utils.c:1809
#define gdb_stderr
Definition utils.h:187
#define gdb_stdout
Definition utils.h:182