1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
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.
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.
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/>. */
29 #include "gdbsupport/gdb-checked-static-cast.h"
31 /* Mark beginning of a table. */
34 mi_ui_out::do_table_begin (int nr_cols
, int nr_rows
,
37 open (tblid
, ui_out_type_tuple
);
38 do_field_signed (-1, -1, ui_left
, "nr_rows", nr_rows
, ui_file_style ());
39 do_field_signed (-1, -1, ui_left
, "nr_cols", nr_cols
, ui_file_style ());
40 open ("hdr", ui_out_type_list
);
43 /* Mark beginning of a table body. */
46 mi_ui_out::do_table_body ()
48 /* close the table header line if there were any headers */
49 close (ui_out_type_list
);
50 open ("body", ui_out_type_list
);
53 /* Mark end of a table. */
56 mi_ui_out::do_table_end ()
58 close (ui_out_type_list
); /* body */
59 close (ui_out_type_tuple
);
62 /* Specify table header. */
65 mi_ui_out::do_table_header (int width
, ui_align alignment
,
66 const std::string
&col_name
,
67 const std::string
&col_hdr
)
69 open (NULL
, ui_out_type_tuple
);
70 do_field_signed (0, 0, ui_center
, "width", width
, ui_file_style ());
71 do_field_signed (0, 0, ui_center
, "alignment", alignment
, ui_file_style ());
72 do_field_string (0, 0, ui_center
, "col_name", col_name
.c_str (),
74 do_field_string (0, width
, alignment
, "colhdr", col_hdr
.c_str (),
76 close (ui_out_type_tuple
);
79 /* Mark beginning of a list. */
82 mi_ui_out::do_begin (ui_out_type type
, const char *id
)
87 /* Mark end of a list. */
90 mi_ui_out::do_end (ui_out_type type
)
95 /* Output an int field. */
98 mi_ui_out::do_field_signed (int fldno
, int width
, ui_align alignment
,
99 const char *fldname
, LONGEST value
,
100 const ui_file_style
&style
)
102 do_field_string (fldno
, width
, alignment
, fldname
, plongest (value
),
106 /* Output an unsigned field. */
109 mi_ui_out::do_field_unsigned (int fldno
, int width
, ui_align alignment
,
110 const char *fldname
, ULONGEST value
)
112 do_field_string (fldno
, width
, alignment
, fldname
, pulongest (value
),
116 /* Used to omit a field. */
119 mi_ui_out::do_field_skip (int fldno
, int width
, ui_align alignment
,
124 /* Other specific mi_field_* end up here so alignment and field
125 separators are both handled by mi_field_string. */
128 mi_ui_out::do_field_string (int fldno
, int width
, ui_align align
,
129 const char *fldname
, const char *string
,
130 const ui_file_style
&style
)
132 ui_file
*stream
= m_streams
.back ();
136 gdb_printf (stream
, "%s=", fldname
);
137 gdb_printf (stream
, "\"");
139 stream
->putstr (string
, '"');
140 gdb_printf (stream
, "\"");
144 mi_ui_out::do_field_fmt (int fldno
, int width
, ui_align align
,
145 const char *fldname
, const ui_file_style
&style
,
146 const char *format
, va_list args
)
148 ui_file
*stream
= m_streams
.back ();
152 gdb_printf (stream
, "%s=\"", fldname
);
154 gdb_puts ("\"", stream
);
155 gdb_vprintf (stream
, format
, args
);
156 gdb_puts ("\"", stream
);
160 mi_ui_out::do_spaces (int numspaces
)
165 mi_ui_out::do_text (const char *string
)
170 mi_ui_out::do_message (const ui_file_style
&style
,
171 const char *format
, va_list args
)
176 mi_ui_out::do_wrap_hint (int indent
)
178 m_streams
.back ()->wrap_here (indent
);
182 mi_ui_out::do_flush ()
185 gdb_flush (m_streams
.back ());
189 mi_ui_out::do_redirect (ui_file
*outstream
)
191 if (outstream
!= NULL
)
192 m_streams
.push_back (outstream
);
194 m_streams
.pop_back ();
198 mi_ui_out::field_separator ()
200 if (m_suppress_field_separator
)
201 m_suppress_field_separator
= false;
203 gdb_putc (',', m_streams
.back ());
207 mi_ui_out::open (const char *name
, ui_out_type type
)
209 ui_file
*stream
= m_streams
.back ();
212 m_suppress_field_separator
= true;
215 gdb_printf (stream
, "%s=", name
);
219 case ui_out_type_tuple
:
220 gdb_putc ('{', stream
);
223 case ui_out_type_list
:
224 gdb_putc ('[', stream
);
228 internal_error (_("bad switch"));
233 mi_ui_out::close (ui_out_type type
)
235 ui_file
*stream
= m_streams
.back ();
239 case ui_out_type_tuple
:
240 gdb_putc ('}', stream
);
243 case ui_out_type_list
:
244 gdb_putc (']', stream
);
248 internal_error (_("bad switch"));
251 m_suppress_field_separator
= false;
255 mi_ui_out::main_stream ()
257 gdb_assert (m_streams
.size () == 1);
259 return (string_file
*) m_streams
.back ();
262 /* Initialize a progress update to be displayed with
263 mi_ui_out::do_progress_notify. */
266 mi_ui_out::do_progress_start ()
268 m_progress_info
.emplace_back ();
271 /* Indicate that a task described by MSG is in progress. */
274 mi_ui_out::do_progress_notify (const std::string
&msg
, const char *unit
,
275 double cur
, double total
)
277 mi_progress_info
&info (m_progress_info
.back ());
279 if (info
.state
== progress_update::START
)
281 gdb_printf ("%s...\n", msg
.c_str ());
282 info
.state
= progress_update::WORKING
;
286 /* Remove the most recent progress update from the progress_info stack. */
289 mi_ui_out::do_progress_end ()
291 m_progress_info
.pop_back ();
294 /* Clear the buffer. */
299 main_stream ()->clear ();
302 /* Dump the buffer onto the specified stream. */
305 mi_ui_out::put (ui_file
*where
)
307 string_file
*mi_stream
= main_stream ();
309 where
->write (mi_stream
->data (), mi_stream
->size ());
313 /* Return the current MI version. */
316 mi_ui_out::version ()
321 /* Constructor for an `mi_out_data' object. */
323 mi_ui_out::mi_ui_out (int mi_version
)
324 : ui_out (make_flags (mi_version
)),
325 m_suppress_field_separator (false),
326 m_suppress_output (false),
327 m_mi_version (mi_version
)
329 string_file
*stream
= new string_file ();
330 m_streams
.push_back (stream
);
333 mi_ui_out::~mi_ui_out ()
337 /* See mi/mi-out.h. */
339 std::unique_ptr
<mi_ui_out
>
340 mi_out_new (const char *mi_version
)
342 if (streq (mi_version
, INTERP_MI4
) || streq (mi_version
, INTERP_MI
))
343 return std::make_unique
<mi_ui_out
> (4);
345 if (streq (mi_version
, INTERP_MI3
))
346 return std::make_unique
<mi_ui_out
> (3);
348 if (streq (mi_version
, INTERP_MI2
))
349 return std::make_unique
<mi_ui_out
> (2);
354 /* Helper function to return the given UIOUT as an mi_ui_out. It is an error
355 to call this function with an ui_out that is not an MI. */
358 as_mi_ui_out (ui_out
*uiout
)
360 return gdb::checked_static_cast
<mi_ui_out
*> (uiout
);
364 mi_out_put (ui_out
*uiout
, struct ui_file
*stream
)
366 return as_mi_ui_out (uiout
)->put (stream
);
370 mi_out_rewind (ui_out
*uiout
)
372 return as_mi_ui_out (uiout
)->rewind ();