Automatic date update in version.in
[binutils-gdb.git] / gdb / ui-out.c
blobb3a2fb7f4e51c763a8a109fee9d0f6c295cd9056
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/>. */
23 #include "expression.h"
24 #include "language.h"
25 #include "ui-out.h"
26 #include "gdbsupport/format.h"
27 #include "cli/cli-style.h"
28 #include "diagnostics.h"
30 #include <vector>
31 #include <memory>
32 #include <string>
34 namespace {
36 /* A header of a ui_out_table. */
38 class ui_out_hdr
40 public:
42 explicit ui_out_hdr (int number, int min_width, ui_align alignment,
43 const std::string &name, const std::string &header)
44 : m_number (number),
45 m_min_width (min_width),
46 m_alignment (alignment),
47 m_name (name),
48 m_header (header)
52 int number () const
54 return m_number;
57 int min_width () const
59 return m_min_width;
62 ui_align alignment () const
64 return m_alignment;
67 const std::string &header () const
69 return m_header;
72 const std::string &name () const
74 return m_name;
77 private:
79 /* The number of the table column this header represents, 1-based. */
80 int m_number;
82 /* Minimal column width in characters. May or may not be applicable,
83 depending on the actual implementation of ui_out. */
84 int m_min_width;
86 /* Alignment of the content in the column. May or may not be applicable,
87 depending on the actual implementation of ui_out. */
88 ui_align m_alignment;
90 /* Internal column name, used to internally refer to the column. */
91 std::string m_name;
93 /* Printed header text of the column. */
94 std::string m_header;
97 } // namespace
99 /* A level of nesting (either a list or a tuple) in a ui_out output. */
101 class ui_out_level
103 public:
105 explicit ui_out_level (ui_out_type type)
106 : m_type (type),
107 m_field_count (0)
111 ui_out_type type () const
113 return m_type;
116 int field_count () const
118 return m_field_count;
121 void inc_field_count ()
123 m_field_count++;
126 private:
128 /* The type of this level. */
129 ui_out_type m_type;
131 /* Count each field; the first element is for non-list fields. */
132 int m_field_count;
135 /* Tables are special. Maintain a separate structure that tracks
136 their state. At present an output can only contain a single table
137 but that restriction might eventually be lifted. */
139 class ui_out_table
141 public:
143 /* States (steps) of a table generation. */
145 enum class state
147 /* We are generating the table headers. */
148 HEADERS,
150 /* We are generating the table body. */
151 BODY,
154 explicit ui_out_table (int entry_level, int nr_cols, const std::string &id)
155 : m_state (state::HEADERS),
156 m_entry_level (entry_level),
157 m_nr_cols (nr_cols),
158 m_id (id)
162 /* Start building the body of the table. */
164 void start_body ();
166 /* Add a new header to the table. */
168 void append_header (int width, ui_align alignment,
169 const std::string &col_name, const std::string &col_hdr);
171 void start_row ();
173 /* Extract the format information for the next header and advance
174 the header iterator. Return false if there was no next header. */
176 bool get_next_header (int *colno, int *width, ui_align *alignment,
177 const char **col_hdr);
179 bool query_field (int colno, int *width, int *alignment,
180 const char **col_name) const;
182 state current_state () const;
184 int entry_level () const;
186 private:
188 state m_state;
190 /* The level at which each entry of the table is to be found. A row
191 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
192 above that of the table. */
193 int m_entry_level;
195 /* Number of table columns (as specified in the table_begin call). */
196 int m_nr_cols;
198 /* String identifying the table (as specified in the table_begin
199 call). */
200 std::string m_id;
202 /* Pointers to the column headers. */
203 std::vector<std::unique_ptr<ui_out_hdr>> m_headers;
205 /* Iterator over the headers vector, used when printing successive fields. */
206 std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator m_headers_iterator;
209 /* See ui-out.h. */
211 void ui_out_table::start_body ()
213 if (m_state != state::HEADERS)
214 internal_error (_("extra table_body call not allowed; there must be only "
215 "one table_body after a table_begin and before a "
216 "table_end."));
218 /* Check if the number of defined headers matches the number of expected
219 columns. */
220 if (m_headers.size () != m_nr_cols)
221 internal_error (_("number of headers differ from number of table "
222 "columns."));
224 m_state = state::BODY;
225 m_headers_iterator = m_headers.begin ();
228 /* See ui-out.h. */
230 void ui_out_table::append_header (int width, ui_align alignment,
231 const std::string &col_name,
232 const std::string &col_hdr)
234 if (m_state != state::HEADERS)
235 internal_error (_("table header must be specified after table_begin and "
236 "before table_body."));
238 auto header = std::make_unique<ui_out_hdr> (m_headers.size () + 1,
239 width, alignment,
240 col_name, col_hdr);
242 m_headers.push_back (std::move (header));
245 /* See ui-out.h. */
247 void ui_out_table::start_row ()
249 m_headers_iterator = m_headers.begin ();
252 /* See ui-out.h. */
254 bool ui_out_table::get_next_header (int *colno, int *width, ui_align *alignment,
255 const char **col_hdr)
257 /* There may be no headers at all or we may have used all columns. */
258 if (m_headers_iterator == m_headers.end ())
259 return false;
261 ui_out_hdr *hdr = m_headers_iterator->get ();
263 *colno = hdr->number ();
264 *width = hdr->min_width ();
265 *alignment = hdr->alignment ();
266 *col_hdr = hdr->header ().c_str ();
268 /* Advance the header pointer to the next entry. */
269 m_headers_iterator++;
271 return true;
274 /* See ui-out.h. */
276 bool ui_out_table::query_field (int colno, int *width, int *alignment,
277 const char **col_name) const
279 /* Column numbers are 1-based, so convert to 0-based index. */
280 int index = colno - 1;
282 if (index >= 0 && index < m_headers.size ())
284 ui_out_hdr *hdr = m_headers[index].get ();
286 gdb_assert (colno == hdr->number ());
288 *width = hdr->min_width ();
289 *alignment = hdr->alignment ();
290 *col_name = hdr->name ().c_str ();
292 return true;
294 else
295 return false;
298 /* See ui-out.h. */
300 ui_out_table::state ui_out_table::current_state () const
302 return m_state;
305 /* See ui-out.h. */
307 int ui_out_table::entry_level () const
309 return m_entry_level;
313 ui_out::level () const
315 return m_levels.size ();
318 /* The current (inner most) level. */
320 ui_out_level *
321 ui_out::current_level () const
323 return m_levels.back ().get ();
326 /* Create a new level, of TYPE. */
327 void
328 ui_out::push_level (ui_out_type type)
330 auto level = std::make_unique<ui_out_level> (type);
332 m_levels.push_back (std::move (level));
335 /* Discard the current level. TYPE is the type of the level being
336 discarded. */
337 void
338 ui_out::pop_level (ui_out_type type)
340 /* We had better not underflow the buffer. */
341 gdb_assert (m_levels.size () > 0);
342 gdb_assert (current_level ()->type () == type);
344 m_levels.pop_back ();
347 /* Mark beginning of a table. */
349 void
350 ui_out::table_begin (int nr_cols, int nr_rows, const std::string &tblid)
352 if (m_table_up != nullptr)
353 internal_error (_("tables cannot be nested; table_begin found before \
354 previous table_end."));
356 m_table_up.reset (new ui_out_table (level () + 1, nr_cols, tblid));
358 do_table_begin (nr_cols, nr_rows, tblid.c_str ());
361 void
362 ui_out::table_header (int width, ui_align alignment,
363 const std::string &col_name, const std::string &col_hdr)
365 if (m_table_up == nullptr)
366 internal_error (_("table_header outside a table is not valid; it must be \
367 after a table_begin and before a table_body."));
369 m_table_up->append_header (width, alignment, col_name, col_hdr);
371 do_table_header (width, alignment, col_name, col_hdr);
374 void
375 ui_out::table_body ()
377 if (m_table_up == nullptr)
378 internal_error (_("table_body outside a table is not valid; it must be "
379 "after a table_begin and before a table_end."));
381 m_table_up->start_body ();
383 do_table_body ();
386 void
387 ui_out::table_end ()
389 if (m_table_up == nullptr)
390 internal_error (_("misplaced table_end or missing table_begin."));
392 do_table_end ();
394 m_table_up = nullptr;
397 void
398 ui_out::begin (ui_out_type type, const char *id)
400 /* Be careful to verify the ``field'' before the new tuple/list is
401 pushed onto the stack. That way the containing list/table/row is
402 verified and not the newly created tuple/list. This verification
403 is needed (at least) for the case where a table row entry
404 contains either a tuple/list. For that case bookkeeping such as
405 updating the column count or advancing to the next heading still
406 needs to be performed. */
408 int fldno;
409 int width;
410 ui_align align;
412 verify_field (&fldno, &width, &align);
415 push_level (type);
417 /* If the push puts us at the same level as a table row entry, we've
418 got a new table row. Put the header pointer back to the start. */
419 if (m_table_up != nullptr
420 && m_table_up->current_state () == ui_out_table::state::BODY
421 && m_table_up->entry_level () == level ())
422 m_table_up->start_row ();
424 do_begin (type, id);
427 void
428 ui_out::end (ui_out_type type)
430 pop_level (type);
432 do_end (type);
435 void
436 ui_out::field_signed (const char *fldname, LONGEST value)
438 int fldno;
439 int width;
440 ui_align align;
442 verify_field (&fldno, &width, &align);
444 do_field_signed (fldno, width, align, fldname, value);
447 void
448 ui_out::field_fmt_signed (int input_width, ui_align input_align,
449 const char *fldname, LONGEST value)
451 int fldno;
452 int width;
453 ui_align align;
455 verify_field (&fldno, &width, &align);
457 do_field_signed (fldno, input_width, input_align, fldname, value);
460 /* See ui-out.h. */
462 void
463 ui_out::field_unsigned (const char *fldname, ULONGEST value)
465 int fldno;
466 int width;
467 ui_align align;
469 verify_field (&fldno, &width, &align);
471 do_field_unsigned (fldno, width, align, fldname, value);
474 /* Documented in ui-out.h. */
476 void
477 ui_out::field_core_addr (const char *fldname, struct gdbarch *gdbarch,
478 CORE_ADDR address)
480 field_string (fldname, print_core_address (gdbarch, address),
481 address_style.style ());
484 void
485 ui_out::field_stream (const char *fldname, string_file &stream,
486 const ui_file_style &style)
488 if (!stream.empty ())
489 field_string (fldname, stream.c_str (), style);
490 else
491 field_skip (fldname);
492 stream.clear ();
495 /* Used to omit a field. */
497 void
498 ui_out::field_skip (const char *fldname)
500 int fldno;
501 int width;
502 ui_align align;
504 verify_field (&fldno, &width, &align);
506 do_field_skip (fldno, width, align, fldname);
509 void
510 ui_out::field_string (const char *fldname, const char *string,
511 const ui_file_style &style)
513 int fldno;
514 int width;
515 ui_align align;
517 verify_field (&fldno, &width, &align);
519 do_field_string (fldno, width, align, fldname, string, style);
522 /* VARARGS */
523 void
524 ui_out::field_fmt (const char *fldname, const char *format, ...)
526 va_list args;
527 int fldno;
528 int width;
529 ui_align align;
531 verify_field (&fldno, &width, &align);
533 va_start (args, format);
535 do_field_fmt (fldno, width, align, fldname, ui_file_style (), format, args);
537 va_end (args);
540 void
541 ui_out::field_fmt (const char *fldname, const ui_file_style &style,
542 const char *format, ...)
544 va_list args;
545 int fldno;
546 int width;
547 ui_align align;
549 verify_field (&fldno, &width, &align);
551 va_start (args, format);
553 do_field_fmt (fldno, width, align, fldname, style, format, args);
555 va_end (args);
558 void
559 ui_out::spaces (int numspaces)
561 do_spaces (numspaces);
564 void
565 ui_out::text (const char *string)
567 do_text (string);
570 void
571 ui_out::call_do_message (const ui_file_style &style, const char *format,
572 ...)
574 va_list args;
576 va_start (args, format);
578 /* Since call_do_message is only used as a helper of vmessage, silence the
579 warning here once instead of at all call sites in vmessage, if we were
580 to put a "format" attribute on call_do_message. */
581 DIAGNOSTIC_PUSH
582 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
583 do_message (style, format, args);
584 DIAGNOSTIC_POP
586 va_end (args);
589 void
590 ui_out::vmessage (const ui_file_style &in_style, const char *format,
591 va_list args)
593 format_pieces fpieces (&format, true);
595 ui_file_style style = in_style;
597 for (auto &&piece : fpieces)
599 const char *current_substring = piece.string;
601 gdb_assert (piece.n_int_args >= 0 && piece.n_int_args <= 2);
602 int intvals[2] = { 0, 0 };
603 for (int i = 0; i < piece.n_int_args; ++i)
604 intvals[i] = va_arg (args, int);
606 /* The only ones we support for now. */
607 gdb_assert (piece.n_int_args == 0
608 || piece.argclass == string_arg
609 || piece.argclass == int_arg
610 || piece.argclass == long_arg);
612 switch (piece.argclass)
614 case string_arg:
616 const char *str = va_arg (args, const char *);
617 switch (piece.n_int_args)
619 case 0:
620 call_do_message (style, current_substring, str);
621 break;
622 case 1:
623 call_do_message (style, current_substring, intvals[0], str);
624 break;
625 case 2:
626 call_do_message (style, current_substring,
627 intvals[0], intvals[1], str);
628 break;
631 break;
632 case wide_string_arg:
633 gdb_assert_not_reached ("wide_string_arg not supported in vmessage");
634 break;
635 case wide_char_arg:
636 gdb_assert_not_reached ("wide_char_arg not supported in vmessage");
637 break;
638 case long_long_arg:
639 call_do_message (style, current_substring, va_arg (args, long long));
640 break;
641 case int_arg:
643 int val = va_arg (args, int);
644 switch (piece.n_int_args)
646 case 0:
647 call_do_message (style, current_substring, val);
648 break;
649 case 1:
650 call_do_message (style, current_substring, intvals[0], val);
651 break;
652 case 2:
653 call_do_message (style, current_substring,
654 intvals[0], intvals[1], val);
655 break;
658 break;
659 case long_arg:
661 long val = va_arg (args, long);
662 switch (piece.n_int_args)
664 case 0:
665 call_do_message (style, current_substring, val);
666 break;
667 case 1:
668 call_do_message (style, current_substring, intvals[0], val);
669 break;
670 case 2:
671 call_do_message (style, current_substring,
672 intvals[0], intvals[1], val);
673 break;
676 break;
677 case size_t_arg:
679 size_t val = va_arg (args, size_t);
680 switch (piece.n_int_args)
682 case 0:
683 call_do_message (style, current_substring, val);
684 break;
685 case 1:
686 call_do_message (style, current_substring, intvals[0], val);
687 break;
688 case 2:
689 call_do_message (style, current_substring,
690 intvals[0], intvals[1], val);
691 break;
694 break;
695 case double_arg:
696 call_do_message (style, current_substring, va_arg (args, double));
697 break;
698 case long_double_arg:
699 gdb_assert_not_reached ("long_double_arg not supported in vmessage");
700 break;
701 case dec32float_arg:
702 gdb_assert_not_reached ("dec32float_arg not supported in vmessage");
703 break;
704 case dec64float_arg:
705 gdb_assert_not_reached ("dec64float_arg not supported in vmessage");
706 break;
707 case dec128float_arg:
708 gdb_assert_not_reached ("dec128float_arg not supported in vmessage");
709 break;
710 case ptr_arg:
711 switch (current_substring[2])
713 case 'F':
715 gdb_assert (!test_flags (disallow_ui_out_field));
716 base_field_s *bf = va_arg (args, base_field_s *);
717 switch (bf->kind)
719 case field_kind::FIELD_SIGNED:
721 auto *f = (signed_field_s *) bf;
722 field_signed (f->name, f->val);
724 break;
725 case field_kind::FIELD_STRING:
727 auto *f = (string_field_s *) bf;
728 field_string (f->name, f->str);
730 break;
733 break;
734 case 's':
736 styled_string_s *ss = va_arg (args, styled_string_s *);
737 call_do_message (ss->style, "%s", ss->str);
739 break;
740 case '[':
741 style = *va_arg (args, const ui_file_style *);
742 break;
743 case ']':
745 void *arg = va_arg (args, void *);
746 gdb_assert (arg == nullptr);
748 style = {};
750 break;
751 default:
752 call_do_message (style, current_substring, va_arg (args, void *));
753 break;
755 break;
756 case literal_piece:
757 /* Print a portion of the format string that has no
758 directives. Note that this will not include any ordinary
759 %-specs, but it might include "%%". That is why we use
760 call_do_message here. Also, we pass a dummy argument
761 because some platforms have modified GCC to include
762 -Wformat-security by default, which will warn here if
763 there is no argument. */
764 call_do_message (style, current_substring, 0);
765 break;
766 default:
767 internal_error (_("failed internal consistency check"));
772 void
773 ui_out::message (const char *format, ...)
775 va_list args;
776 va_start (args, format);
778 vmessage (ui_file_style (), format, args);
780 va_end (args);
783 void
784 ui_out::wrap_hint (int indent)
786 do_wrap_hint (indent);
789 void
790 ui_out::flush ()
792 do_flush ();
795 void
796 ui_out::redirect (ui_file *outstream)
798 do_redirect (outstream);
801 /* Test the flags against the mask given. */
802 ui_out_flags
803 ui_out::test_flags (ui_out_flags mask)
805 return m_flags & mask;
808 bool
809 ui_out::is_mi_like_p () const
811 return do_is_mi_like_p ();
814 /* Verify that the field/tuple/list is correctly positioned. Return
815 the field number and corresponding alignment (if
816 available/applicable). */
818 void
819 ui_out::verify_field (int *fldno, int *width, ui_align *align)
821 ui_out_level *current = current_level ();
822 const char *text;
824 if (m_table_up != nullptr
825 && m_table_up->current_state () != ui_out_table::state::BODY)
827 internal_error (_("table_body missing; table fields must be \
828 specified after table_body and inside a list."));
831 current->inc_field_count ();
833 if (m_table_up != nullptr
834 && m_table_up->current_state () == ui_out_table::state::BODY
835 && m_table_up->entry_level () == level ()
836 && m_table_up->get_next_header (fldno, width, align, &text))
838 if (*fldno != current->field_count ())
839 internal_error (_("ui-out internal error in handling headers."));
841 else
843 *width = 0;
844 *align = ui_noalign;
845 *fldno = current->field_count ();
849 /* Access table field parameters. */
851 bool
852 ui_out::query_table_field (int colno, int *width, int *alignment,
853 const char **col_name)
855 if (m_table_up == nullptr)
856 return false;
858 return m_table_up->query_field (colno, width, alignment, col_name);
861 /* The constructor. */
863 ui_out::ui_out (ui_out_flags flags)
864 : m_flags (flags)
866 /* Create the ui-out level #1, the default level. */
867 push_level (ui_out_type_tuple);
870 ui_out::~ui_out ()
874 /* See ui-out.h. */
876 void
877 buffer_group::output_unit::flush () const
879 if (!m_msg.empty ())
880 m_stream->puts (m_msg.c_str ());
882 if (m_wrap_hint >= 0)
883 m_stream->wrap_here (m_wrap_hint);
885 if (m_flush)
886 m_stream->flush ();
889 /* See ui-out.h. */
891 void
892 buffer_group::write (const char *buf, long length_buf, ui_file *stream)
894 /* Record each line separately. */
895 for (size_t prev = 0, cur = 0; cur < length_buf; ++cur)
896 if (buf[cur] == '\n' || cur == length_buf - 1)
898 std::string msg (buf + prev, cur - prev + 1);
900 if (m_buffered_output.size () > 0
901 && m_buffered_output.back ().m_wrap_hint == -1
902 && m_buffered_output.back ().m_stream == stream
903 && m_buffered_output.back ().m_msg.size () > 0
904 && m_buffered_output.back ().m_msg.back () != '\n')
905 m_buffered_output.back ().m_msg.append (msg);
906 else
907 m_buffered_output.emplace_back (msg).m_stream = stream;
908 prev = cur + 1;
912 /* See ui-out.h. */
914 void
915 buffer_group::wrap_here (int indent, ui_file *stream)
917 m_buffered_output.emplace_back ("", indent).m_stream = stream;
920 /* See ui-out.h. */
922 void
923 buffer_group::flush_here (ui_file *stream)
925 m_buffered_output.emplace_back ("", -1, true).m_stream = stream;
928 /* See ui-out.h. */
930 ui_file *
931 get_unbuffered (ui_file *stream)
933 buffering_file *buf = dynamic_cast<buffering_file *> (stream);
935 if (buf == nullptr)
936 return stream;
938 return get_unbuffered (buf->stream ());
941 buffered_streams::buffered_streams (buffer_group *group, ui_out *uiout)
942 : m_buffered_stdout (group, gdb_stdout),
943 m_buffered_stderr (group, gdb_stderr),
944 m_buffered_stdlog (group, gdb_stdlog),
945 m_buffered_stdtarg (group, gdb_stdtarg),
946 m_buffered_stdtargerr (group, gdb_stdtargerr),
947 m_uiout (uiout)
949 gdb_stdout = &m_buffered_stdout;
950 gdb_stderr = &m_buffered_stderr;
951 gdb_stdlog = &m_buffered_stdlog;
952 gdb_stdtarg = &m_buffered_stdtarg;
953 gdb_stdtargerr = &m_buffered_stdtargerr;
955 ui_file *stream = current_uiout->current_stream ();
956 if (stream != nullptr)
958 m_buffered_current_uiout.emplace (group, stream);
959 current_uiout->redirect (&(*m_buffered_current_uiout));
962 stream = m_uiout->current_stream ();
963 if (stream != nullptr && current_uiout != m_uiout)
965 m_buffered_uiout.emplace (group, stream);
966 m_uiout->redirect (&(*m_buffered_uiout));
969 m_buffers_in_place = true;
972 /* See ui-out.h. */
974 void
975 buffered_streams::remove_buffers ()
977 if (!m_buffers_in_place)
978 return;
980 m_buffers_in_place = false;
982 gdb_stdout = m_buffered_stdout.stream ();
983 gdb_stderr = m_buffered_stderr.stream ();
984 gdb_stdlog = m_buffered_stdlog.stream ();
985 gdb_stdtarg = m_buffered_stdtarg.stream ();
986 gdb_stdtargerr = m_buffered_stdtargerr.stream ();
988 if (m_buffered_current_uiout.has_value ())
989 current_uiout->redirect (nullptr);
991 if (m_buffered_uiout.has_value ())
992 m_uiout->redirect (nullptr);
995 buffer_group::buffer_group (ui_out *uiout)
996 : m_buffered_streams (new buffered_streams (this, uiout))
997 { /* Nothing. */ }
999 /* See ui-out.h. */
1001 void
1002 buffer_group::flush () const
1004 m_buffered_streams->remove_buffers ();
1006 for (const output_unit &ou : m_buffered_output)
1007 ou.flush ();