1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2024 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h"
27 #include "gdbsupport/format.h"
28 #include "cli/cli-style.h"
29 #include "diagnostics.h"
37 /* A header of a ui_out_table. */
43 explicit ui_out_hdr (int number
, int min_width
, ui_align alignment
,
44 const std::string
&name
, const std::string
&header
)
46 m_min_width (min_width
),
47 m_alignment (alignment
),
58 int min_width () const
63 ui_align
alignment () const
68 const std::string
&header () const
73 const std::string
&name () const
80 /* The number of the table column this header represents, 1-based. */
83 /* Minimal column width in characters. May or may not be applicable,
84 depending on the actual implementation of ui_out. */
87 /* Alignment of the content in the column. May or may not be applicable,
88 depending on the actual implementation of ui_out. */
91 /* Internal column name, used to internally refer to the column. */
94 /* Printed header text of the column. */
100 /* A level of nesting (either a list or a tuple) in a ui_out output. */
106 explicit ui_out_level (ui_out_type type
)
112 ui_out_type
type () const
117 int field_count () const
119 return m_field_count
;
122 void inc_field_count ()
129 /* The type of this level. */
132 /* Count each field; the first element is for non-list fields. */
136 /* Tables are special. Maintain a separate structure that tracks
137 their state. At present an output can only contain a single table
138 but that restriction might eventually be lifted. */
144 /* States (steps) of a table generation. */
148 /* We are generating the table headers. */
151 /* We are generating the table body. */
155 explicit ui_out_table (int entry_level
, int nr_cols
, const std::string
&id
)
156 : m_state (state::HEADERS
),
157 m_entry_level (entry_level
),
163 /* Start building the body of the table. */
167 /* Add a new header to the table. */
169 void append_header (int width
, ui_align alignment
,
170 const std::string
&col_name
, const std::string
&col_hdr
);
174 /* Extract the format information for the next header and advance
175 the header iterator. Return false if there was no next header. */
177 bool get_next_header (int *colno
, int *width
, ui_align
*alignment
,
178 const char **col_hdr
);
180 bool query_field (int colno
, int *width
, int *alignment
,
181 const char **col_name
) const;
183 state
current_state () const;
185 int entry_level () const;
191 /* The level at which each entry of the table is to be found. A row
192 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
193 above that of the table. */
196 /* Number of table columns (as specified in the table_begin call). */
199 /* String identifying the table (as specified in the table_begin
203 /* Pointers to the column headers. */
204 std::vector
<std::unique_ptr
<ui_out_hdr
>> m_headers
;
206 /* Iterator over the headers vector, used when printing successive fields. */
207 std::vector
<std::unique_ptr
<ui_out_hdr
>>::const_iterator m_headers_iterator
;
212 void ui_out_table::start_body ()
214 if (m_state
!= state::HEADERS
)
215 internal_error (_("extra table_body call not allowed; there must be only "
216 "one table_body after a table_begin and before a "
219 /* Check if the number of defined headers matches the number of expected
221 if (m_headers
.size () != m_nr_cols
)
222 internal_error (_("number of headers differ from number of table "
225 m_state
= state::BODY
;
226 m_headers_iterator
= m_headers
.begin ();
231 void ui_out_table::append_header (int width
, ui_align alignment
,
232 const std::string
&col_name
,
233 const std::string
&col_hdr
)
235 if (m_state
!= state::HEADERS
)
236 internal_error (_("table header must be specified after table_begin and "
237 "before table_body."));
239 auto header
= std::make_unique
<ui_out_hdr
> (m_headers
.size () + 1,
243 m_headers
.push_back (std::move (header
));
248 void ui_out_table::start_row ()
250 m_headers_iterator
= m_headers
.begin ();
255 bool ui_out_table::get_next_header (int *colno
, int *width
, ui_align
*alignment
,
256 const char **col_hdr
)
258 /* There may be no headers at all or we may have used all columns. */
259 if (m_headers_iterator
== m_headers
.end ())
262 ui_out_hdr
*hdr
= m_headers_iterator
->get ();
264 *colno
= hdr
->number ();
265 *width
= hdr
->min_width ();
266 *alignment
= hdr
->alignment ();
267 *col_hdr
= hdr
->header ().c_str ();
269 /* Advance the header pointer to the next entry. */
270 m_headers_iterator
++;
277 bool ui_out_table::query_field (int colno
, int *width
, int *alignment
,
278 const char **col_name
) const
280 /* Column numbers are 1-based, so convert to 0-based index. */
281 int index
= colno
- 1;
283 if (index
>= 0 && index
< m_headers
.size ())
285 ui_out_hdr
*hdr
= m_headers
[index
].get ();
287 gdb_assert (colno
== hdr
->number ());
289 *width
= hdr
->min_width ();
290 *alignment
= hdr
->alignment ();
291 *col_name
= hdr
->name ().c_str ();
301 ui_out_table::state
ui_out_table::current_state () const
308 int ui_out_table::entry_level () const
310 return m_entry_level
;
314 ui_out::level () const
316 return m_levels
.size ();
319 /* The current (inner most) level. */
322 ui_out::current_level () const
324 return m_levels
.back ().get ();
327 /* Create a new level, of TYPE. */
329 ui_out::push_level (ui_out_type type
)
331 auto level
= std::make_unique
<ui_out_level
> (type
);
333 m_levels
.push_back (std::move (level
));
336 /* Discard the current level. TYPE is the type of the level being
339 ui_out::pop_level (ui_out_type type
)
341 /* We had better not underflow the buffer. */
342 gdb_assert (m_levels
.size () > 0);
343 gdb_assert (current_level ()->type () == type
);
345 m_levels
.pop_back ();
348 /* Mark beginning of a table. */
351 ui_out::table_begin (int nr_cols
, int nr_rows
, const std::string
&tblid
)
353 if (m_table_up
!= nullptr)
354 internal_error (_("tables cannot be nested; table_begin found before \
355 previous table_end."));
357 m_table_up
.reset (new ui_out_table (level () + 1, nr_cols
, tblid
));
359 do_table_begin (nr_cols
, nr_rows
, tblid
.c_str ());
363 ui_out::table_header (int width
, ui_align alignment
,
364 const std::string
&col_name
, const std::string
&col_hdr
)
366 if (m_table_up
== nullptr)
367 internal_error (_("table_header outside a table is not valid; it must be \
368 after a table_begin and before a table_body."));
370 m_table_up
->append_header (width
, alignment
, col_name
, col_hdr
);
372 do_table_header (width
, alignment
, col_name
, col_hdr
);
376 ui_out::table_body ()
378 if (m_table_up
== nullptr)
379 internal_error (_("table_body outside a table is not valid; it must be "
380 "after a table_begin and before a table_end."));
382 m_table_up
->start_body ();
390 if (m_table_up
== nullptr)
391 internal_error (_("misplaced table_end or missing table_begin."));
395 m_table_up
= nullptr;
399 ui_out::begin (ui_out_type type
, const char *id
)
401 /* Be careful to verify the ``field'' before the new tuple/list is
402 pushed onto the stack. That way the containing list/table/row is
403 verified and not the newly created tuple/list. This verification
404 is needed (at least) for the case where a table row entry
405 contains either a tuple/list. For that case bookkeeping such as
406 updating the column count or advancing to the next heading still
407 needs to be performed. */
413 verify_field (&fldno
, &width
, &align
);
418 /* If the push puts us at the same level as a table row entry, we've
419 got a new table row. Put the header pointer back to the start. */
420 if (m_table_up
!= nullptr
421 && m_table_up
->current_state () == ui_out_table::state::BODY
422 && m_table_up
->entry_level () == level ())
423 m_table_up
->start_row ();
429 ui_out::end (ui_out_type type
)
437 ui_out::field_signed (const char *fldname
, LONGEST value
)
443 verify_field (&fldno
, &width
, &align
);
445 do_field_signed (fldno
, width
, align
, fldname
, value
);
449 ui_out::field_fmt_signed (int input_width
, ui_align input_align
,
450 const char *fldname
, LONGEST value
)
456 verify_field (&fldno
, &width
, &align
);
458 do_field_signed (fldno
, input_width
, input_align
, fldname
, value
);
464 ui_out::field_unsigned (const char *fldname
, ULONGEST value
)
470 verify_field (&fldno
, &width
, &align
);
472 do_field_unsigned (fldno
, width
, align
, fldname
, value
);
475 /* Documented in ui-out.h. */
478 ui_out::field_core_addr (const char *fldname
, struct gdbarch
*gdbarch
,
481 field_string (fldname
, print_core_address (gdbarch
, address
),
482 address_style
.style ());
486 ui_out::field_stream (const char *fldname
, string_file
&stream
,
487 const ui_file_style
&style
)
489 if (!stream
.empty ())
490 field_string (fldname
, stream
.c_str (), style
);
492 field_skip (fldname
);
496 /* Used to omit a field. */
499 ui_out::field_skip (const char *fldname
)
505 verify_field (&fldno
, &width
, &align
);
507 do_field_skip (fldno
, width
, align
, fldname
);
511 ui_out::field_string (const char *fldname
, const char *string
,
512 const ui_file_style
&style
)
518 verify_field (&fldno
, &width
, &align
);
520 do_field_string (fldno
, width
, align
, fldname
, string
, style
);
525 ui_out::field_fmt (const char *fldname
, const char *format
, ...)
532 verify_field (&fldno
, &width
, &align
);
534 va_start (args
, format
);
536 do_field_fmt (fldno
, width
, align
, fldname
, ui_file_style (), format
, args
);
542 ui_out::field_fmt (const char *fldname
, const ui_file_style
&style
,
543 const char *format
, ...)
550 verify_field (&fldno
, &width
, &align
);
552 va_start (args
, format
);
554 do_field_fmt (fldno
, width
, align
, fldname
, style
, format
, args
);
560 ui_out::spaces (int numspaces
)
562 do_spaces (numspaces
);
566 ui_out::text (const char *string
)
572 ui_out::call_do_message (const ui_file_style
&style
, const char *format
,
577 va_start (args
, format
);
579 /* Since call_do_message is only used as a helper of vmessage, silence the
580 warning here once instead of at all call sites in vmessage, if we were
581 to put a "format" attribute on call_do_message. */
583 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
584 do_message (style
, format
, args
);
591 ui_out::vmessage (const ui_file_style
&in_style
, const char *format
,
594 format_pieces
fpieces (&format
, true);
596 ui_file_style style
= in_style
;
598 for (auto &&piece
: fpieces
)
600 const char *current_substring
= piece
.string
;
602 gdb_assert (piece
.n_int_args
>= 0 && piece
.n_int_args
<= 2);
603 int intvals
[2] = { 0, 0 };
604 for (int i
= 0; i
< piece
.n_int_args
; ++i
)
605 intvals
[i
] = va_arg (args
, int);
607 /* The only ones we support for now. */
608 gdb_assert (piece
.n_int_args
== 0
609 || piece
.argclass
== string_arg
610 || piece
.argclass
== int_arg
611 || piece
.argclass
== long_arg
);
613 switch (piece
.argclass
)
617 const char *str
= va_arg (args
, const char *);
618 switch (piece
.n_int_args
)
621 call_do_message (style
, current_substring
, str
);
624 call_do_message (style
, current_substring
, intvals
[0], str
);
627 call_do_message (style
, current_substring
,
628 intvals
[0], intvals
[1], str
);
633 case wide_string_arg
:
634 gdb_assert_not_reached ("wide_string_arg not supported in vmessage");
637 gdb_assert_not_reached ("wide_char_arg not supported in vmessage");
640 call_do_message (style
, current_substring
, va_arg (args
, long long));
644 int val
= va_arg (args
, int);
645 switch (piece
.n_int_args
)
648 call_do_message (style
, current_substring
, val
);
651 call_do_message (style
, current_substring
, intvals
[0], val
);
654 call_do_message (style
, current_substring
,
655 intvals
[0], intvals
[1], val
);
662 long val
= va_arg (args
, long);
663 switch (piece
.n_int_args
)
666 call_do_message (style
, current_substring
, val
);
669 call_do_message (style
, current_substring
, intvals
[0], val
);
672 call_do_message (style
, current_substring
,
673 intvals
[0], intvals
[1], val
);
680 size_t val
= va_arg (args
, size_t);
681 switch (piece
.n_int_args
)
684 call_do_message (style
, current_substring
, val
);
687 call_do_message (style
, current_substring
, intvals
[0], val
);
690 call_do_message (style
, current_substring
,
691 intvals
[0], intvals
[1], val
);
697 call_do_message (style
, current_substring
, va_arg (args
, double));
699 case long_double_arg
:
700 gdb_assert_not_reached ("long_double_arg not supported in vmessage");
703 gdb_assert_not_reached ("dec32float_arg not supported in vmessage");
706 gdb_assert_not_reached ("dec64float_arg not supported in vmessage");
708 case dec128float_arg
:
709 gdb_assert_not_reached ("dec128float_arg not supported in vmessage");
712 switch (current_substring
[2])
716 gdb_assert (!test_flags (disallow_ui_out_field
));
717 base_field_s
*bf
= va_arg (args
, base_field_s
*);
720 case field_kind::FIELD_SIGNED
:
722 auto *f
= (signed_field_s
*) bf
;
723 field_signed (f
->name
, f
->val
);
726 case field_kind::FIELD_STRING
:
728 auto *f
= (string_field_s
*) bf
;
729 field_string (f
->name
, f
->str
);
737 styled_string_s
*ss
= va_arg (args
, styled_string_s
*);
738 call_do_message (ss
->style
, "%s", ss
->str
);
742 style
= *va_arg (args
, const ui_file_style
*);
746 void *arg
= va_arg (args
, void *);
747 gdb_assert (arg
== nullptr);
753 call_do_message (style
, current_substring
, va_arg (args
, void *));
758 /* Print a portion of the format string that has no
759 directives. Note that this will not include any ordinary
760 %-specs, but it might include "%%". That is why we use
761 call_do_message here. Also, we pass a dummy argument
762 because some platforms have modified GCC to include
763 -Wformat-security by default, which will warn here if
764 there is no argument. */
765 call_do_message (style
, current_substring
, 0);
768 internal_error (_("failed internal consistency check"));
774 ui_out::message (const char *format
, ...)
777 va_start (args
, format
);
779 vmessage (ui_file_style (), format
, args
);
785 ui_out::wrap_hint (int indent
)
787 do_wrap_hint (indent
);
797 ui_out::redirect (ui_file
*outstream
)
799 do_redirect (outstream
);
802 /* Test the flags against the mask given. */
804 ui_out::test_flags (ui_out_flags mask
)
806 return m_flags
& mask
;
810 ui_out::is_mi_like_p () const
812 return do_is_mi_like_p ();
815 /* Verify that the field/tuple/list is correctly positioned. Return
816 the field number and corresponding alignment (if
817 available/applicable). */
820 ui_out::verify_field (int *fldno
, int *width
, ui_align
*align
)
822 ui_out_level
*current
= current_level ();
825 if (m_table_up
!= nullptr
826 && m_table_up
->current_state () != ui_out_table::state::BODY
)
828 internal_error (_("table_body missing; table fields must be \
829 specified after table_body and inside a list."));
832 current
->inc_field_count ();
834 if (m_table_up
!= nullptr
835 && m_table_up
->current_state () == ui_out_table::state::BODY
836 && m_table_up
->entry_level () == level ()
837 && m_table_up
->get_next_header (fldno
, width
, align
, &text
))
839 if (*fldno
!= current
->field_count ())
840 internal_error (_("ui-out internal error in handling headers."));
846 *fldno
= current
->field_count ();
850 /* Access table field parameters. */
853 ui_out::query_table_field (int colno
, int *width
, int *alignment
,
854 const char **col_name
)
856 if (m_table_up
== nullptr)
859 return m_table_up
->query_field (colno
, width
, alignment
, col_name
);
862 /* The constructor. */
864 ui_out::ui_out (ui_out_flags flags
)
867 /* Create the ui-out level #1, the default level. */
868 push_level (ui_out_type_tuple
);
878 buffer_group::output_unit::flush () const
881 m_stream
->puts (m_msg
.c_str ());
883 if (m_wrap_hint
>= 0)
884 m_stream
->wrap_here (m_wrap_hint
);
893 buffer_group::write (const char *buf
, long length_buf
, ui_file
*stream
)
895 /* Record each line separately. */
896 for (size_t prev
= 0, cur
= 0; cur
< length_buf
; ++cur
)
897 if (buf
[cur
] == '\n' || cur
== length_buf
- 1)
899 std::string
msg (buf
+ prev
, cur
- prev
+ 1);
901 if (m_buffered_output
.size () > 0
902 && m_buffered_output
.back ().m_wrap_hint
== -1
903 && m_buffered_output
.back ().m_stream
== stream
904 && m_buffered_output
.back ().m_msg
.size () > 0
905 && m_buffered_output
.back ().m_msg
.back () != '\n')
906 m_buffered_output
.back ().m_msg
.append (msg
);
909 m_buffered_output
.emplace_back (msg
);
910 m_buffered_output
.back ().m_stream
= stream
;
919 buffer_group::wrap_here (int indent
, ui_file
*stream
)
921 m_buffered_output
.emplace_back ("", indent
);
922 m_buffered_output
.back ().m_stream
= stream
;
928 buffer_group::flush_here (ui_file
*stream
)
930 m_buffered_output
.emplace_back ("", -1, true);
931 m_buffered_output
.back ().m_stream
= stream
;
937 get_unbuffered (ui_file
*stream
)
939 buffering_file
*buf
= dynamic_cast<buffering_file
*> (stream
);
944 return get_unbuffered (buf
->stream ());
947 buffered_streams::buffered_streams (buffer_group
*group
, ui_out
*uiout
)
948 : m_buffered_stdout (group
, gdb_stdout
),
949 m_buffered_stderr (group
, gdb_stderr
),
950 m_buffered_stdlog (group
, gdb_stdlog
),
951 m_buffered_stdtarg (group
, gdb_stdtarg
),
952 m_buffered_stdtargerr (group
, gdb_stdtargerr
),
955 gdb_stdout
= &m_buffered_stdout
;
956 gdb_stderr
= &m_buffered_stderr
;
957 gdb_stdlog
= &m_buffered_stdlog
;
958 gdb_stdtarg
= &m_buffered_stdtarg
;
959 gdb_stdtargerr
= &m_buffered_stdtargerr
;
961 ui_file
*stream
= current_uiout
->current_stream ();
962 if (stream
!= nullptr)
964 m_buffered_current_uiout
.emplace (group
, stream
);
965 current_uiout
->redirect (&(*m_buffered_current_uiout
));
968 stream
= m_uiout
->current_stream ();
969 if (stream
!= nullptr && current_uiout
!= m_uiout
)
971 m_buffered_uiout
.emplace (group
, stream
);
972 m_uiout
->redirect (&(*m_buffered_uiout
));
975 m_buffers_in_place
= true;
981 buffered_streams::remove_buffers ()
983 if (!m_buffers_in_place
)
986 m_buffers_in_place
= false;
988 gdb_stdout
= m_buffered_stdout
.stream ();
989 gdb_stderr
= m_buffered_stderr
.stream ();
990 gdb_stdlog
= m_buffered_stdlog
.stream ();
991 gdb_stdtarg
= m_buffered_stdtarg
.stream ();
992 gdb_stdtargerr
= m_buffered_stdtargerr
.stream ();
994 if (m_buffered_current_uiout
.has_value ())
995 current_uiout
->redirect (nullptr);
997 if (m_buffered_uiout
.has_value ())
998 m_uiout
->redirect (nullptr);
1001 buffer_group::buffer_group (ui_out
*uiout
)
1002 : m_buffered_streams (new buffered_streams (this, uiout
))
1008 buffer_group::flush () const
1010 m_buffered_streams
->remove_buffers ();
1012 for (const output_unit
&ou
: m_buffered_output
)