GDB (xrefs)

mi-out.c
Go to the documentation of this file.
1 /* MI Command Set - output generating routines.
2
3 Copyright (C) 2000-2018 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Solutions (a Red Hat company).
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "mi-out.h"
25 #include <vector>
26
27 /* Mark beginning of a table. */
28
29 void
30 mi_ui_out::do_table_begin (int nr_cols, int nr_rows,
31 const char *tblid)
32 {
33 open (tblid, ui_out_type_tuple);
34 do_field_int (-1, -1, ui_left, "nr_rows", nr_rows);
35 do_field_int (-1, -1, ui_left, "nr_cols", nr_cols);
36 open ("hdr", ui_out_type_list);
37 }
38
39 /* Mark beginning of a table body. */
40
41 void
42 mi_ui_out::do_table_body ()
43 {
44 /* close the table header line if there were any headers */
45 close (ui_out_type_list);
46 open ("body", ui_out_type_list);
47 }
48
49 /* Mark end of a table. */
50
51 void
52 mi_ui_out::do_table_end ()
53 {
54 close (ui_out_type_list); /* body */
55 close (ui_out_type_tuple);
56 }
57
58 /* Specify table header. */
59
60 void
61 mi_ui_out::do_table_header (int width, ui_align alignment,
62 const std::string &col_name,
63 const std::string &col_hdr)
64 {
65 open (NULL, ui_out_type_tuple);
66 do_field_int (0, 0, ui_center, "width", width);
67 do_field_int (0, 0, ui_center, "alignment", alignment);
68 do_field_string (0, 0, ui_center, "col_name", col_name.c_str ());
69 do_field_string (0, width, alignment, "colhdr", col_hdr.c_str ());
70 close (ui_out_type_tuple);
71 }
72
73 /* Mark beginning of a list. */
74
75 void
76 mi_ui_out::do_begin (ui_out_type type, const char *id)
77 {
78 open (id, type);
79 }
80
81 /* Mark end of a list. */
82
83 void
84 mi_ui_out::do_end (ui_out_type type)
85 {
86 close (type);
87 }
88
89 /* Output an int field. */
90
91 void
92 mi_ui_out::do_field_int (int fldno, int width, ui_align alignment,
93 const char *fldname, int value)
94 {
95 char buffer[20]; /* FIXME: how many chars long a %d can become? */
96
97 xsnprintf (buffer, sizeof (buffer), "%d", value);
98 do_field_string (fldno, width, alignment, fldname, buffer);
99 }
100
101 /* Used to omit a field. */
102
103 void
104 mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
105 const char *fldname)
106 {
107 }
108
109 /* Other specific mi_field_* end up here so alignment and field
110 separators are both handled by mi_field_string. */
111
112 void
113 mi_ui_out::do_field_string (int fldno, int width, ui_align align,
114 const char *fldname, const char *string)
115 {
116 ui_file *stream = m_streams.back ();
117 field_separator ();
118
119 if (fldname)
120 fprintf_unfiltered (stream, "%s=", fldname);
121 fprintf_unfiltered (stream, "\"");
122 if (string)
123 fputstr_unfiltered (string, '"', stream);
124 fprintf_unfiltered (stream, "\"");
125 }
126
127 /* This is the only field function that does not align. */
128
129 void
130 mi_ui_out::do_field_fmt (int fldno, int width, ui_align align,
131 const char *fldname, const char *format,
132 va_list args)
133 {
134 ui_file *stream = m_streams.back ();
135 field_separator ();
136
137 if (fldname)
138 fprintf_unfiltered (stream, "%s=\"", fldname);
139 else
140 fputs_unfiltered ("\"", stream);
141 vfprintf_unfiltered (stream, format, args);
142 fputs_unfiltered ("\"", stream);
143 }
144
145 void
146 mi_ui_out::do_spaces (int numspaces)
147 {
148 }
149
150 void
151 mi_ui_out::do_text (const char *string)
152 {
153 }
154
155 void
156 mi_ui_out::do_message (const char *format, va_list args)
157 {
158 }
159
160 void
161 mi_ui_out::do_wrap_hint (const char *identstring)
162 {
163 wrap_here (identstring);
164 }
165
166 void
167 mi_ui_out::do_flush ()
168 {
169
170 gdb_flush (m_streams.back ());
171 }
172
173 void
174 mi_ui_out::do_redirect (ui_file *outstream)
175 {
176 if (outstream != NULL)
177 m_streams.push_back (outstream);
178 else
179 m_streams.pop_back ();
180 }
181
182 void
183 mi_ui_out::field_separator ()
184 {
185 if (m_suppress_field_separator)
186 m_suppress_field_separator = false;
187 else
188 fputc_unfiltered (',', m_streams.back ());
189 }
190
191 void
192 mi_ui_out::open (const char *name, ui_out_type type)
193 {
194 ui_file *stream = m_streams.back ();
195
196 field_separator ();
197 m_suppress_field_separator = true;
198
199 if (name)
200 fprintf_unfiltered (stream, "%s=", name);
201
202 switch (type)
203 {
204 case ui_out_type_tuple:
205 fputc_unfiltered ('{', stream);
206 break;
207
208 case ui_out_type_list:
209 fputc_unfiltered ('[', stream);
210 break;
211
212 default:
213 internal_error (__FILE__, __LINE__, _("bad switch"));
214 }
215 }
216
217 void
218 mi_ui_out::close (ui_out_type type)
219 {
220 ui_file *stream = m_streams.back ();
221
222 switch (type)
223 {
224 case ui_out_type_tuple:
225 fputc_unfiltered ('}', stream);
226 break;
227
228 case ui_out_type_list:
229 fputc_unfiltered (']', stream);
230 break;
231
232 default:
233 internal_error (__FILE__, __LINE__, _("bad switch"));
234 }
235
236 m_suppress_field_separator = false;
237 }
238
239 string_file *
240 mi_ui_out::main_stream ()
241 {
242 gdb_assert (m_streams.size () == 1);
243
244 return (string_file *) m_streams.back ();
245 }
246
247 /* Clear the buffer. */
248
249 void
250 mi_ui_out::rewind ()
251 {
252 main_stream ()->clear ();
253 }
254
255 /* Dump the buffer onto the specified stream. */
256
257 void
258 mi_ui_out::put (ui_file *where)
259 {
260 string_file *mi_stream = main_stream ();
261
262 where->write (mi_stream->data (), mi_stream->size ());
263 mi_stream->clear ();
264 }
265
266 /* Return the current MI version. */
267
268 int
269 mi_ui_out::version ()
270 {
271 return m_mi_version;
272 }
273
274 /* Constructor for an `mi_out_data' object. */
275
276 mi_ui_out::mi_ui_out (int mi_version)
277 : m_suppress_field_separator (false),
278 m_suppress_output (false),
279 m_mi_version (mi_version)
280 {
281 string_file *stream = new string_file ();
282 m_streams.push_back (stream);
283 }
284
285 mi_ui_out::~mi_ui_out ()
286 {
287 }
288
289 /* Initialize private members at startup. */
290
291 mi_ui_out *
292 mi_out_new (int mi_version)
293 {
294 return new mi_ui_out (mi_version);
295 }
296
297 /* Helper function to return the given UIOUT as an mi_ui_out. It is an error
298 to call this function with an ui_out that is not an MI. */
299
300 static mi_ui_out *
301 as_mi_ui_out (ui_out *uiout)
302 {
303 mi_ui_out *mi_uiout = dynamic_cast<mi_ui_out *> (uiout);
304
305 gdb_assert (mi_uiout != NULL);
306
307 return mi_uiout;
308 }
309
310 int
311 mi_version (ui_out *uiout)
312 {
313 return as_mi_ui_out (uiout)->version ();
314 }
315
316 void
317 mi_out_put (ui_out *uiout, struct ui_file *stream)
318 {
319 return as_mi_ui_out (uiout)->put (stream);
320 }
321
322 void
323 mi_out_rewind (ui_out *uiout)
324 {
325 return as_mi_ui_out (uiout)->rewind ();
326 }
mi_ui_out::close
void close(ui_out_type type)
Definition: mi-out.c:218
ui_file::write
void virtual void write(const char *buf, long length_buf)=0
string
const char * string
Definition: signals.c:50
mi_ui_out::mi_ui_out
mi_ui_out(int mi_version)
Definition: mi-out.c:276
mi_ui_out::do_field_fmt
virtual void do_field_fmt(int fldno, int width, ui_align align, const char *fldname, const char *format, va_list args) override ATTRIBUTE_PRINTF(6
Definition: mi-out.c:130
mi_ui_out::field_separator
void field_separator()
Definition: mi-out.c:183
fputs_unfiltered
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:127
mi_ui_out::put
void put(struct ui_file *stream)
Definition: mi-out.c:258
mi_ui_out::do_table_end
virtual void do_table_end() override
Definition: mi-out.c:52
mi_ui_out::main_stream
string_file * main_stream()
Definition: mi-out.c:240
internal_error
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
mi_ui_out::do_flush
virtual void do_flush() override
Definition: mi-out.c:167
as_mi_ui_out
static mi_ui_out * as_mi_ui_out(ui_out *uiout)
Definition: mi-out.c:301
mi_ui_out::do_spaces
virtual void virtual void do_spaces(int numspaces) override
Definition: mi-out.c:146
mi_ui_out::do_text
virtual void do_text(const char *string) override
Definition: mi-out.c:151
_
#define _(String)
Definition: gdb_locale.h:35
mi_ui_out::rewind
void rewind()
Definition: mi-out.c:250
ui_file
Definition: ui-file.h:26
mi_out_new
mi_ui_out * mi_out_new(int mi_version)
Definition: mi-out.c:292
ui_left
Definition: ui-out.h:44
defs.h
mi_ui_out::m_mi_version
int m_mi_version
Definition: mi-out.h:88
name
const char *const name
Definition: aarch64-tdep.c:76
fputstr_unfiltered
void fputstr_unfiltered(const char *str, int quoter, struct ui_file *stream)
Definition: utils.c:1261
mi_ui_out::version
int version()
Definition: mi-out.c:269
fputc_unfiltered
int fputc_unfiltered(int c, struct ui_file *stream)
Definition: utils.c:1835
mi_ui_out::do_message
virtual void do_message(const char *format, va_list args) override ATTRIBUTE_PRINTF(2
Definition: mi-out.c:156
ui_out_type_tuple
Definition: ui-out.h:65
ui_out_type_list
Definition: ui-out.h:66
fprintf_unfiltered
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
vfprintf_unfiltered
void vfprintf_unfiltered(struct ui_file *stream, const char *format, va_list args)
Definition: utils.c:1969
mi_version
int mi_version(ui_out *uiout)
Definition: mi-out.c:311
mi_ui_out::do_field_skip
virtual void do_field_skip(int fldno, int width, ui_align align, const char *fldname) override
Definition: mi-out.c:104
mi_ui_out::~mi_ui_out
virtual ~mi_ui_out()
Definition: mi-out.c:285
mi_ui_out::do_end
virtual void do_end(ui_out_type type) override
Definition: mi-out.c:84
type
Definition: gdbtypes.h:749
string_file::clear
void clear()
Definition: ui-file.h:140
string_file::size
size_t size() const
Definition: ui-file.h:138
gdb_assert
#define gdb_assert(expr)
Definition: gdb_assert.h:32
value
Definition: value.c:169
mi_ui_out::m_streams
std::vector< ui_file * > m_streams
Definition: mi-out.h:89
mi_ui_out::do_wrap_hint
virtual void virtual void do_wrap_hint(const char *identstring) override
Definition: mi-out.c:161
wrap_here
void wrap_here(const char *indent)
Definition: utils.c:1596
ui_out
Definition: ui-out.h:77
ui_center
Definition: ui-out.h:45
mi_ui_out::do_redirect
virtual void do_redirect(struct ui_file *outstream) override
Definition: mi-out.c:174
xsnprintf
int xsnprintf(char *str, size_t size, const char *format,...)
Definition: common-utils.c:134
ui_align
ui_align
Definition: ui-out.h:42
mi_ui_out::do_field_string
virtual void do_field_string(int fldno, int width, ui_align align, const char *fldname, const char *string) override
Definition: mi-out.c:113
mi_ui_out::do_field_int
virtual void do_field_int(int fldno, int width, ui_align align, const char *fldname, int value) override
Definition: mi-out.c:92
mi_ui_out::do_table_body
virtual void do_table_body() override
Definition: mi-out.c:42
buffer
Definition: buffer.h:23
mi_ui_out::do_table_begin
virtual void do_table_begin(int nbrofcols, int nr_rows, const char *tblid) override
Definition: mi-out.c:30
mi_ui_out
Definition: mi-out.h:29
string_file::data
const char * data() const
Definition: ui-file.h:136
ui_out_type
ui_out_type
Definition: ui-out.h:63
gdb_flush
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:93
mi_ui_out::do_begin
virtual void do_begin(ui_out_type type, const char *id) override
Definition: mi-out.c:76
mi_out_rewind
void mi_out_rewind(ui_out *uiout)
Definition: mi-out.c:323
mi_out_put
void mi_out_put(ui_out *uiout, struct ui_file *stream)
Definition: mi-out.c:317
mi_ui_out::do_table_header
virtual void do_table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr) override
Definition: mi-out.c:61
mi_ui_out::m_suppress_field_separator
bool m_suppress_field_separator
Definition: mi-out.h:86
mi-out.h
ui-out.h
mi_ui_out::open
void open(const char *name, ui_out_type type)
Definition: mi-out.c:192
string_file
Definition: ui-file.h:106