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