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