gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdb / ui-file.h
CommitLineData
d9fcf2fb 1/* UI_FILE - a generic STDIO like output stream.
b811d2c2 2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
d9fcf2fb
JM
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
d9fcf2fb
JM
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d9fcf2fb
JM
18
19#ifndef UI_FILE_H
20#define UI_FILE_H
21
8de00631 22#include <string>
eedeedd2 23#include "ui-style.h"
8de00631 24
d7e74731
PA
25/* The abstract ui_file base class. */
26
27class ui_file
28{
29public:
30 ui_file ();
31 virtual ~ui_file () = 0;
d9fcf2fb 32
d7e74731 33 /* Public non-virtual API. */
d9fcf2fb 34
d7e74731 35 void printf (const char *, ...) ATTRIBUTE_PRINTF (2, 3);
d9fcf2fb 36
d7e74731
PA
37 /* Print a string whose delimiter is QUOTER. Note that these
38 routines should only be called for printing things which are
39 independent of the language of the program being debugged. */
40 void putstr (const char *str, int quoter);
d9fcf2fb 41
d7e74731 42 void putstrn (const char *str, int n, int quoter);
3e43a32a 43
d7e74731 44 int putc (int c);
3e43a32a 45
d7e74731 46 void vprintf (const char *, va_list) ATTRIBUTE_PRINTF (2, 0);
01124a23 47
d7e74731
PA
48 /* Methods below are both public, and overridable by ui_file
49 subclasses. */
3e43a32a 50
d7e74731 51 virtual void write (const char *buf, long length_buf) = 0;
3e43a32a 52
d7e74731
PA
53 /* This version of "write" is safe for use in signal handlers. It's
54 not guaranteed that all existing output will have been flushed
55 first. Implementations are also free to ignore some or all of
56 the request. puts_async is not provided as the async versions
57 are rarely used, no point in having both for a rarely used
58 interface. */
59 virtual void write_async_safe (const char *buf, long length_buf)
60 { gdb_assert_not_reached ("write_async_safe"); }
3e43a32a 61
d7e74731
PA
62 /* Some ui_files override this to provide a efficient implementation
63 that avoids a strlen. */
64 virtual void puts (const char *str)
65 { this->write (str, strlen (str)); }
d9fcf2fb 66
d7e74731
PA
67 virtual long read (char *buf, long length_buf)
68 { gdb_assert_not_reached ("can't read from this file type"); }
d9fcf2fb 69
d7e74731
PA
70 virtual bool isatty ()
71 { return false; }
2a9d5ccf 72
8a522c6c
PW
73 /* true indicates terminal output behaviour such as cli_styling.
74 This default implementation indicates to do terminal output
75 behaviour if the UI_FILE is a tty. A derived class can override
76 TERM_OUT to have cli_styling behaviour without being a tty. */
77 virtual bool term_out ()
78 { return isatty (); }
79
80 /* true if ANSI escapes can be used on STREAM. */
81 virtual bool can_emit_style_escape ()
82 { return false; }
83
d7e74731
PA
84 virtual void flush ()
85 {}
86};
d9fcf2fb 87
d7e74731 88typedef std::unique_ptr<ui_file> ui_file_up;
d9fcf2fb 89
d7e74731 90/* A ui_file that writes to nowhere. */
d9fcf2fb 91
d7e74731
PA
92class null_file : public ui_file
93{
94public:
95 void write (const char *buf, long length_buf) override;
96 void write_async_safe (const char *buf, long sizeof_buf) override;
97 void puts (const char *str) override;
98};
d9fcf2fb 99
d7e74731
PA
100/* A preallocated null_file stream. */
101extern null_file null_stream;
102
e4adb939
EZ
103extern int gdb_console_fputs (const char *, FILE *);
104
d7e74731
PA
105/* A std::string-based ui_file. Can be used as a scratch buffer for
106 collecting output. */
d9fcf2fb 107
d7e74731
PA
108class string_file : public ui_file
109{
110public:
8a522c6c
PW
111 /* Construct a string_file to collect 'raw' output, i.e. without
112 'terminal' behaviour such as cli_styling. */
113 string_file () : m_term_out (false) {};
114 /* If TERM_OUT, construct a string_file with terminal output behaviour
115 such as cli_styling)
116 else collect 'raw' output like the previous constructor. */
117 explicit string_file (bool term_out) : m_term_out (term_out) {};
d7e74731 118 ~string_file () override;
d9fcf2fb 119
d7e74731 120 /* Override ui_file methods. */
8de00631 121
d7e74731 122 void write (const char *buf, long length_buf) override;
d9fcf2fb 123
d7e74731
PA
124 long read (char *buf, long length_buf) override
125 { gdb_assert_not_reached ("a string_file is not readable"); }
126
8a522c6c
PW
127 bool term_out () override;
128 bool can_emit_style_escape () override;
129
d7e74731
PA
130 /* string_file-specific public API. */
131
132 /* Accesses the std::string containing the entire output collected
133 so far.
134
135 Returns a non-const reference so that it's easy to move the
136 string contents out of the string_file. E.g.:
137
138 string_file buf;
139 buf.printf (....);
140 buf.printf (....);
141 return std::move (buf.string ());
142 */
143 std::string &string () { return m_string; }
144
145 /* Provide a few convenience methods with the same API as the
146 underlying std::string. */
147 const char *data () const { return m_string.data (); }
148 const char *c_str () const { return m_string.c_str (); }
149 size_t size () const { return m_string.size (); }
150 bool empty () const { return m_string.empty (); }
151 void clear () { return m_string.clear (); }
152
153private:
154 /* The internal buffer. */
155 std::string m_string;
8a522c6c
PW
156
157 bool m_term_out;
d7e74731
PA
158};
159
160/* A ui_file implementation that maps directly onto <stdio.h>'s FILE.
161 A stdio_file can either own its underlying file, or not. If it
162 owns the file, then destroying the stdio_file closes the underlying
163 file, otherwise it is left open. */
164
165class stdio_file : public ui_file
166{
167public:
168 /* Create a ui_file from a previously opened FILE. CLOSE_P
169 indicates whether the underlying file should be closed when the
170 stdio_file is destroyed. */
171 explicit stdio_file (FILE *file, bool close_p = false);
172
173 /* Create an stdio_file that is not managing any file yet. Call
174 open to actually open something. */
175 stdio_file ();
176
177 ~stdio_file () override;
178
179 /* Open NAME in mode MODE, and own the resulting file. Returns true
180 on success, false otherwise. If the stdio_file previously owned
181 a file, it is closed. */
182 bool open (const char *name, const char *mode);
183
184 void flush () override;
185
186 void write (const char *buf, long length_buf) override;
187
188 void write_async_safe (const char *buf, long length_buf) override;
189
190 void puts (const char *) override;
191
192 long read (char *buf, long length_buf) override;
193
194 bool isatty () override;
195
8a522c6c
PW
196 bool can_emit_style_escape () override;
197
d7e74731
PA
198private:
199 /* Sets the internal stream to FILE, and saves the FILE's file
200 descriptor in M_FD. */
201 void set_stream (FILE *file);
202
203 /* The file. */
204 FILE *m_file;
205
206 /* The associated file descriptor is extracted ahead of time for
207 stdio_file::write_async_safe's benefit, in case fileno isn't
208 async-safe. */
209 int m_fd;
210
211 /* If true, M_FILE is closed on destruction. */
212 bool m_close_p;
213};
214
215typedef std::unique_ptr<stdio_file> stdio_file_up;
216
217/* Like stdio_file, but specifically for stderr.
218
219 This exists because there is no real line-buffering on Windows, see
220 <http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx>
221 so the stdout is either fully-buffered or non-buffered. We can't
222 make stdout non-buffered, because of two concerns:
449092f6 223
d7e74731
PA
224 1. Non-buffering hurts performance.
225 2. Non-buffering may change GDB's behavior when it is interacting
226 with a front-end, such as Emacs.
2a9d5ccf 227
d7e74731
PA
228 We leave stdout as fully buffered, but flush it first when
229 something is written to stderr.
d9fcf2fb 230
d7e74731
PA
231 Note that the 'write_async_safe' method is not overridden, because
232 there's no way to flush a stream in an async-safe manner.
233 Fortunately, it doesn't really matter, because:
234
235 1. That method is only used for printing internal debug output
236 from signal handlers.
237
238 2. Windows hosts don't have a concept of async-safeness. Signal
239 handlers run in a separate thread, so they can call the regular
240 non-async-safe output routines freely.
241*/
242class stderr_file : public stdio_file
243{
244public:
245 explicit stderr_file (FILE *stream);
246
247 /* Override the output routines to flush gdb_stdout before deferring
248 to stdio_file for the actual outputting. */
249 void write (const char *buf, long length_buf) override;
250 void puts (const char *linebuffer) override;
251};
d9fcf2fb 252
d7e74731 253/* A ui_file implementation that maps onto two ui-file objects. */
d9fcf2fb 254
d7e74731
PA
255class tee_file : public ui_file
256{
257public:
f3a09c80
AH
258 /* Create a file which writes to both ONE and TWO. ONE will remain
259 open when this object is destroyed; but TWO will be closed. */
260 tee_file (ui_file *one, ui_file_up &&two);
d7e74731 261 ~tee_file () override;
d9fcf2fb 262
d7e74731
PA
263 void write (const char *buf, long length_buf) override;
264 void write_async_safe (const char *buf, long length_buf) override;
265 void puts (const char *) override;
ffa4ac95 266
d7e74731 267 bool isatty () override;
8a522c6c
PW
268 bool term_out () override;
269 bool can_emit_style_escape () override;
d7e74731 270 void flush () override;
ffa4ac95 271
d7e74731 272private:
f3a09c80
AH
273 /* The two underlying ui_files. */
274 ui_file *m_one;
275 ui_file_up m_two;
d7e74731 276};
d9fcf2fb 277
0735b091
TT
278/* A ui_file implementation that filters out terminal escape
279 sequences. */
280
281class no_terminal_escape_file : public stdio_file
282{
283public:
284 no_terminal_escape_file ()
285 {
286 }
287
288 /* Like the stdio_file methods, but these filter out terminal escape
289 sequences. */
290 void write (const char *buf, long length_buf) override;
291 void puts (const char *linebuffer) override;
292};
293
d9fcf2fb 294#endif
This page took 2.14161 seconds and 4 git commands to generate.