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