1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2019 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" /* For language.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 (__FILE__
, __LINE__
,
216 _("extra table_body call not allowed; there must be only "
217 "one table_body after a table_begin and before a "
220 /* Check if the number of defined headers matches the number of expected
222 if (m_headers
.size () != m_nr_cols
)
223 internal_error (__FILE__
, __LINE__
,
224 _("number of headers differ from number of table "
227 m_state
= state::BODY
;
228 m_headers_iterator
= m_headers
.begin ();
233 void ui_out_table::append_header (int width
, ui_align alignment
,
234 const std::string
&col_name
,
235 const std::string
&col_hdr
)
237 if (m_state
!= state::HEADERS
)
238 internal_error (__FILE__
, __LINE__
,
239 _("table header must be specified after table_begin and "
240 "before table_body."));
242 std::unique_ptr
<ui_out_hdr
> header (new ui_out_hdr (m_headers
.size () + 1,
246 m_headers
.push_back (std::move (header
));
251 void ui_out_table::start_row ()
253 m_headers_iterator
= m_headers
.begin ();
258 bool ui_out_table::get_next_header (int *colno
, int *width
, ui_align
*alignment
,
259 const char **col_hdr
)
261 /* There may be no headers at all or we may have used all columns. */
262 if (m_headers_iterator
== m_headers
.end ())
265 ui_out_hdr
*hdr
= m_headers_iterator
->get ();
267 *colno
= hdr
->number ();
268 *width
= hdr
->min_width ();
269 *alignment
= hdr
->alignment ();
270 *col_hdr
= hdr
->header ().c_str ();
272 /* Advance the header pointer to the next entry. */
273 m_headers_iterator
++;
280 bool ui_out_table::query_field (int colno
, int *width
, int *alignment
,
281 const char **col_name
) const
283 /* Column numbers are 1-based, so convert to 0-based index. */
284 int index
= colno
- 1;
286 if (index
>= 0 && index
< m_headers
.size ())
288 ui_out_hdr
*hdr
= m_headers
[index
].get ();
290 gdb_assert (colno
== hdr
->number ());
292 *width
= hdr
->min_width ();
293 *alignment
= hdr
->alignment ();
294 *col_name
= hdr
->name ().c_str ();
304 ui_out_table::state
ui_out_table::current_state () const
311 int ui_out_table::entry_level () const
313 return m_entry_level
;
317 ui_out::level () const
319 return m_levels
.size ();
322 /* The current (inner most) level. */
325 ui_out::current_level () const
327 return m_levels
.back ().get ();
330 /* Create a new level, of TYPE. */
332 ui_out::push_level (ui_out_type type
)
334 std::unique_ptr
<ui_out_level
> level (new ui_out_level (type
));
336 m_levels
.push_back (std::move (level
));
339 /* Discard the current level. TYPE is the type of the level being
342 ui_out::pop_level (ui_out_type type
)
344 /* We had better not underflow the buffer. */
345 gdb_assert (m_levels
.size () > 0);
346 gdb_assert (current_level ()->type () == type
);
348 m_levels
.pop_back ();
351 /* Mark beginning of a table. */
354 ui_out::table_begin (int nr_cols
, int nr_rows
, const std::string
&tblid
)
356 if (m_table_up
!= nullptr)
357 internal_error (__FILE__
, __LINE__
,
358 _("tables cannot be nested; table_begin found before \
359 previous table_end."));
361 m_table_up
.reset (new ui_out_table (level () + 1, nr_cols
, tblid
));
363 do_table_begin (nr_cols
, nr_rows
, tblid
.c_str ());
367 ui_out::table_header (int width
, ui_align alignment
,
368 const std::string
&col_name
, const std::string
&col_hdr
)
370 if (m_table_up
== nullptr)
371 internal_error (__FILE__
, __LINE__
,
372 _("table_header outside a table is not valid; it must be \
373 after a table_begin and before a table_body."));
375 m_table_up
->append_header (width
, alignment
, col_name
, col_hdr
);
377 do_table_header (width
, alignment
, col_name
, col_hdr
);
381 ui_out::table_body ()
383 if (m_table_up
== nullptr)
384 internal_error (__FILE__
, __LINE__
,
385 _("table_body outside a table is not valid; it must be "
386 "after a table_begin and before a table_end."));
388 m_table_up
->start_body ();
396 if (m_table_up
== nullptr)
397 internal_error (__FILE__
, __LINE__
,
398 _("misplaced table_end or missing table_begin."));
402 m_table_up
= nullptr;
406 ui_out::begin (ui_out_type type
, const char *id
)
408 /* Be careful to verify the ``field'' before the new tuple/list is
409 pushed onto the stack. That way the containing list/table/row is
410 verified and not the newly created tuple/list. This verification
411 is needed (at least) for the case where a table row entry
412 contains either a tuple/list. For that case bookkeeping such as
413 updating the column count or advancing to the next heading still
414 needs to be performed. */
420 verify_field (&fldno
, &width
, &align
);
425 /* If the push puts us at the same level as a table row entry, we've
426 got a new table row. Put the header pointer back to the start. */
427 if (m_table_up
!= nullptr
428 && m_table_up
->current_state () == ui_out_table::state::BODY
429 && m_table_up
->entry_level () == level ())
430 m_table_up
->start_row ();
436 ui_out::end (ui_out_type type
)
444 ui_out::field_signed (const char *fldname
, LONGEST value
)
450 verify_field (&fldno
, &width
, &align
);
452 do_field_signed (fldno
, width
, align
, fldname
, value
);
456 ui_out::field_fmt_signed (int input_width
, ui_align input_align
,
457 const char *fldname
, LONGEST value
)
463 verify_field (&fldno
, &width
, &align
);
465 do_field_signed (fldno
, input_width
, input_align
, fldname
, value
);
471 ui_out::field_unsigned (const char *fldname
, ULONGEST value
)
477 verify_field (&fldno
, &width
, &align
);
479 do_field_unsigned (fldno
, width
, align
, fldname
, value
);
482 /* Documented in ui-out.h. */
485 ui_out::field_core_addr (const char *fldname
, struct gdbarch
*gdbarch
,
488 field_string (fldname
, print_core_address (gdbarch
, address
),
489 address_style
.style ());
493 ui_out::field_stream (const char *fldname
, string_file
&stream
,
494 const ui_file_style
&style
)
496 if (!stream
.empty ())
497 field_string (fldname
, stream
.c_str (), style
);
499 field_skip (fldname
);
503 /* Used to omit a field. */
506 ui_out::field_skip (const char *fldname
)
512 verify_field (&fldno
, &width
, &align
);
514 do_field_skip (fldno
, width
, align
, fldname
);
518 ui_out::field_string (const char *fldname
, const char *string
,
519 const ui_file_style
&style
)
525 verify_field (&fldno
, &width
, &align
);
527 do_field_string (fldno
, width
, align
, fldname
, string
, style
);
531 ui_out::field_string (const char *fldname
, const std::string
&string
)
533 field_string (fldname
, string
.c_str ());
538 ui_out::field_fmt (const char *fldname
, const char *format
, ...)
545 verify_field (&fldno
, &width
, &align
);
547 va_start (args
, format
);
549 do_field_fmt (fldno
, width
, align
, fldname
, ui_file_style (), format
, args
);
555 ui_out::field_fmt (const char *fldname
, const ui_file_style
&style
,
556 const char *format
, ...)
563 verify_field (&fldno
, &width
, &align
);
565 va_start (args
, format
);
567 do_field_fmt (fldno
, width
, align
, fldname
, style
, format
, args
);
573 ui_out::spaces (int numspaces
)
575 do_spaces (numspaces
);
579 ui_out::text (const char *string
)
585 ui_out::call_do_message (const ui_file_style
&style
, const char *format
,
590 va_start (args
, format
);
592 /* Since call_do_message is only used as a helper of vmessage, silence the
593 warning here once instead of at all call sites in vmessage, if we were
594 to put a "format" attribute on call_do_message. */
596 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
597 do_message (style
, format
, args
);
604 ui_out::vmessage (const ui_file_style
&in_style
, const char *format
,
607 format_pieces
fpieces (&format
, true);
609 ui_file_style style
= in_style
;
611 for (auto &&piece
: fpieces
)
613 const char *current_substring
= piece
.string
;
615 gdb_assert (piece
.n_int_args
>= 0 && piece
.n_int_args
<= 2);
616 int intvals
[2] = { 0, 0 };
617 for (int i
= 0; i
< piece
.n_int_args
; ++i
)
618 intvals
[i
] = va_arg (args
, int);
620 /* The only ones we support for now. */
621 gdb_assert (piece
.n_int_args
== 0
622 || piece
.argclass
== string_arg
623 || piece
.argclass
== int_arg
624 || piece
.argclass
== long_arg
);
626 switch (piece
.argclass
)
630 const char *str
= va_arg (args
, const char *);
631 switch (piece
.n_int_args
)
634 call_do_message (style
, current_substring
, str
);
637 call_do_message (style
, current_substring
, intvals
[0], str
);
640 call_do_message (style
, current_substring
,
641 intvals
[0], intvals
[1], str
);
646 case wide_string_arg
:
647 gdb_assert_not_reached (_("wide_string_arg not supported in vmessage"));
650 gdb_assert_not_reached (_("wide_char_arg not supported in vmessage"));
653 call_do_message (style
, current_substring
, va_arg (args
, long long));
657 int val
= va_arg (args
, int);
658 switch (piece
.n_int_args
)
661 call_do_message (style
, current_substring
, val
);
664 call_do_message (style
, current_substring
, intvals
[0], val
);
667 call_do_message (style
, current_substring
,
668 intvals
[0], intvals
[1], val
);
675 long val
= va_arg (args
, long);
676 switch (piece
.n_int_args
)
679 call_do_message (style
, current_substring
, val
);
682 call_do_message (style
, current_substring
, intvals
[0], val
);
685 call_do_message (style
, current_substring
,
686 intvals
[0], intvals
[1], val
);
693 size_t val
= va_arg (args
, size_t);
694 switch (piece
.n_int_args
)
697 call_do_message (style
, current_substring
, val
);
700 call_do_message (style
, current_substring
, intvals
[0], val
);
703 call_do_message (style
, current_substring
,
704 intvals
[0], intvals
[1], val
);
710 call_do_message (style
, current_substring
, va_arg (args
, double));
712 case long_double_arg
:
713 gdb_assert_not_reached (_("long_double_arg not supported in vmessage"));
716 gdb_assert_not_reached (_("dec32float_arg not supported in vmessage"));
719 gdb_assert_not_reached (_("dec64float_arg not supported in vmessage"));
721 case dec128float_arg
:
722 gdb_assert_not_reached (_("dec128float_arg not supported in vmessage"));
725 switch (current_substring
[2])
729 gdb_assert (!test_flags (disallow_ui_out_field
));
730 base_field_s
*bf
= va_arg (args
, base_field_s
*);
733 case field_kind::FIELD_SIGNED
:
735 auto *f
= (signed_field_s
*) bf
;
736 field_signed (f
->name
, f
->val
);
739 case field_kind::FIELD_STRING
:
741 auto *f
= (string_field_s
*) bf
;
742 field_string (f
->name
, f
->str
);
750 styled_string_s
*ss
= va_arg (args
, styled_string_s
*);
751 call_do_message (ss
->style
, "%s", ss
->str
);
755 style
= *va_arg (args
, const ui_file_style
*);
759 void *arg
= va_arg (args
, void *);
760 gdb_assert (arg
== nullptr);
766 call_do_message (style
, current_substring
, va_arg (args
, void *));
771 /* Print a portion of the format string that has no
772 directives. Note that this will not include any ordinary
773 %-specs, but it might include "%%". That is why we use
774 call_do_message here. Also, we pass a dummy argument
775 because some platforms have modified GCC to include
776 -Wformat-security by default, which will warn here if
777 there is no argument. */
778 call_do_message (style
, current_substring
, 0);
781 internal_error (__FILE__
, __LINE__
,
782 _("failed internal consistency check"));
788 ui_out::message (const char *format
, ...)
791 va_start (args
, format
);
793 vmessage (ui_file_style (), format
, args
);
799 ui_out::wrap_hint (const char *identstring
)
801 do_wrap_hint (identstring
);
811 ui_out::redirect (ui_file
*outstream
)
813 do_redirect (outstream
);
816 /* Test the flags against the mask given. */
818 ui_out::test_flags (ui_out_flags mask
)
820 return m_flags
& mask
;
824 ui_out::is_mi_like_p () const
826 return do_is_mi_like_p ();
829 /* Verify that the field/tuple/list is correctly positioned. Return
830 the field number and corresponding alignment (if
831 available/applicable). */
834 ui_out::verify_field (int *fldno
, int *width
, ui_align
*align
)
836 ui_out_level
*current
= current_level ();
839 if (m_table_up
!= nullptr
840 && m_table_up
->current_state () != ui_out_table::state::BODY
)
842 internal_error (__FILE__
, __LINE__
,
843 _("table_body missing; table fields must be \
844 specified after table_body and inside a list."));
847 current
->inc_field_count ();
849 if (m_table_up
!= nullptr
850 && m_table_up
->current_state () == ui_out_table::state::BODY
851 && m_table_up
->entry_level () == level ()
852 && m_table_up
->get_next_header (fldno
, width
, align
, &text
))
854 if (*fldno
!= current
->field_count ())
855 internal_error (__FILE__
, __LINE__
,
856 _("ui-out internal error in handling headers."));
862 *fldno
= current
->field_count ();
866 /* Access table field parameters. */
869 ui_out::query_table_field (int colno
, int *width
, int *alignment
,
870 const char **col_name
)
872 if (m_table_up
== nullptr)
875 return m_table_up
->query_field (colno
, width
, alignment
, col_name
);
878 /* The constructor. */
880 ui_out::ui_out (ui_out_flags flags
)
883 /* Create the ui-out level #1, the default level. */
884 push_level (ui_out_type_tuple
);