1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2016 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 */
32 /* A header of a ui_out_table. */
38 explicit ui_out_hdr (int number
, int min_width
, ui_align alignment
,
39 const std::string
&name
, const std::string
&header
)
41 m_min_width (min_width
),
42 m_alignment (alignment
),
53 int min_width () const
58 ui_align
alignment () const
63 const std::string
&header () const
68 const std::string
&name () const
75 /* The number of the table column this header represents, 1-based. */
78 /* Minimal column width in characters. May or may not be applicable,
79 depending on the actual implementation of ui_out. */
82 /* Alignment of the content in the column. May or may not be applicable,
83 depending on the actual implementation of ui_out. */
86 /* Internal column name, used to internally refer to the column. */
89 /* Printed header text of the column. */
93 /* A level of nesting (either a list or a tuple) in a ui_out output. */
99 explicit ui_out_level (ui_out_type type
)
105 ui_out_type
type () const
110 int field_count () const
112 return m_field_count
;
115 void inc_field_count ()
122 /* The type of this level. */
125 /* Count each field; the first element is for non-list fields. */
129 /* Tables are special. Maintain a separate structure that tracks
130 their state. At present an output can only contain a single table
131 but that restriction might eventually be lifted. */
137 /* States (steps) of a table generation. */
141 /* We are generating the table headers. */
144 /* We are generating the table body. */
148 explicit ui_out_table (int entry_level
, int nr_cols
, const std::string
&id
)
149 : m_state (state::HEADERS
),
150 m_entry_level (entry_level
),
156 /* Start building the body of the table. */
160 /* Add a new header to the table. */
162 void append_header (int width
, ui_align alignment
,
163 const std::string
&col_name
, const std::string
&col_hdr
);
167 /* Extract the format information for the next header and advance
168 the header iterator. Return false if there was no next header. */
170 bool get_next_header (int *colno
, int *width
, ui_align
*alignment
,
171 const char **col_hdr
);
173 bool query_field (int colno
, int *width
, int *alignment
,
174 const char **col_name
) const;
176 state
current_state () const;
178 int entry_level () const;
184 /* The level at which each entry of the table is to be found. A row
185 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
186 above that of the table. */
189 /* Number of table columns (as specified in the table_begin call). */
192 /* String identifying the table (as specified in the table_begin
196 /* Pointers to the column headers. */
197 std::vector
<std::unique_ptr
<ui_out_hdr
>> m_headers
;
199 /* Iterator over the headers vector, used when printing successive fields. */
200 std::vector
<std::unique_ptr
<ui_out_hdr
>>::const_iterator m_headers_iterator
;
205 void ui_out_table::start_body ()
207 if (m_state
!= state::HEADERS
)
208 internal_error (__FILE__
, __LINE__
,
209 _("extra table_body call not allowed; there must be only "
210 "one table_body after a table_begin and before a "
213 /* Check if the number of defined headers matches the number of expected
215 if (m_headers
.size () != m_nr_cols
)
216 internal_error (__FILE__
, __LINE__
,
217 _("number of headers differ from number of table "
220 m_state
= state::BODY
;
221 m_headers_iterator
= m_headers
.begin ();
226 void ui_out_table::append_header (int width
, ui_align alignment
,
227 const std::string
&col_name
,
228 const std::string
&col_hdr
)
230 if (m_state
!= state::HEADERS
)
231 internal_error (__FILE__
, __LINE__
,
232 _("table header must be specified after table_begin and "
233 "before table_body."));
235 std::unique_ptr
<ui_out_hdr
> header (new ui_out_hdr (m_headers
.size () + 1,
239 m_headers
.push_back (std::move (header
));
244 void ui_out_table::start_row ()
246 m_headers_iterator
= m_headers
.begin ();
251 bool ui_out_table::get_next_header (int *colno
, int *width
, ui_align
*alignment
,
252 const char **col_hdr
)
254 /* There may be no headers at all or we may have used all columns. */
255 if (m_headers_iterator
== m_headers
.end ())
258 ui_out_hdr
*hdr
= m_headers_iterator
->get ();
260 *colno
= hdr
->number ();
261 *width
= hdr
->min_width ();
262 *alignment
= hdr
->alignment ();
263 *col_hdr
= hdr
->header ().c_str ();
265 /* Advance the header pointer to the next entry. */
266 m_headers_iterator
++;
273 bool ui_out_table::query_field (int colno
, int *width
, int *alignment
,
274 const char **col_name
) const
276 /* Column numbers are 1-based, so convert to 0-based index. */
277 int index
= colno
- 1;
279 if (index
>= 0 && index
< m_headers
.size ())
281 ui_out_hdr
*hdr
= m_headers
[index
].get ();
283 gdb_assert (colno
== hdr
->number ());
285 *width
= hdr
->min_width ();
286 *alignment
= hdr
->alignment ();
287 *col_name
= hdr
->name ().c_str ();
297 ui_out_table::state
ui_out_table::current_state () const
304 int ui_out_table::entry_level () const
306 return m_entry_level
;
309 /* The ui_out structure */
314 /* Specific implementation of ui-out. */
315 const struct ui_out_impl
*impl
;
318 /* Vector to store and track the ui-out levels. */
319 std::vector
<std::unique_ptr
<ui_out_level
>> levels
;
323 return this->levels
.size ();
326 /* A table, if any. At present only a single table is supported. */
327 std::unique_ptr
<ui_out_table
> table
;
330 /* The current (inner most) level. */
331 static ui_out_level
*
332 current_level (struct ui_out
*uiout
)
334 return uiout
->levels
.back ().get ();
337 /* Create a new level, of TYPE. */
339 push_level (struct ui_out
*uiout
,
340 enum ui_out_type type
)
342 std::unique_ptr
<ui_out_level
> level (new ui_out_level (type
));
344 uiout
->levels
.push_back (std::move (level
));
347 /* Discard the current level. TYPE is the type of the level being
350 pop_level (struct ui_out
*uiout
,
351 enum ui_out_type type
)
353 /* We had better not underflow the buffer. */
354 gdb_assert (uiout
->level () > 0);
355 gdb_assert (current_level (uiout
)->type () == type
);
357 uiout
->levels
.pop_back ();
360 /* These are the interfaces to implementation functions. */
362 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
363 int nr_rows
, const char *tblid
);
364 static void uo_table_body (struct ui_out
*uiout
);
365 static void uo_table_end (struct ui_out
*uiout
);
366 static void uo_table_header (struct ui_out
*uiout
, int width
,
368 const std::string
&col_name
,
369 const std::string
&col_hdr
);
370 static void uo_begin (struct ui_out
*uiout
,
371 enum ui_out_type type
,
373 static void uo_end (struct ui_out
*uiout
,
374 enum ui_out_type type
);
375 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
376 enum ui_align align
, const char *fldname
, int value
);
377 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
378 enum ui_align align
, const char *fldname
);
379 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
380 enum ui_align align
, const char *fldname
,
381 const char *format
, va_list args
)
382 ATTRIBUTE_PRINTF (6, 0);
383 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
384 static void uo_text (struct ui_out
*uiout
, const char *string
);
385 static void uo_message (struct ui_out
*uiout
,
386 const char *format
, va_list args
)
387 ATTRIBUTE_PRINTF (2, 0);
388 static void uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
);
389 static void uo_flush (struct ui_out
*uiout
);
390 static int uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
392 /* Prototypes for local functions */
394 static void verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
395 enum ui_align
*align
);
397 /* exported functions (ui_out API) */
399 /* Mark beginning of a table. */
402 ui_out_table_begin (struct ui_out
*uiout
, int nr_cols
,
403 int nr_rows
, const std::string
&tblid
)
405 if (uiout
->table
!= nullptr)
406 internal_error (__FILE__
, __LINE__
,
407 _("tables cannot be nested; table_begin found before \
408 previous table_end."));
411 new ui_out_table (uiout
->level () + 1, nr_cols
, tblid
));
413 uo_table_begin (uiout
, nr_cols
, nr_rows
, tblid
.c_str ());
417 ui_out_table_body (struct ui_out
*uiout
)
419 if (uiout
->table
== nullptr)
420 internal_error (__FILE__
, __LINE__
,
421 _("table_body outside a table is not valid; it must be \
422 after a table_begin and before a table_end."));
424 uiout
->table
->start_body ();
426 uo_table_body (uiout
);
430 ui_out_table_end (struct ui_out
*uiout
)
432 if (uiout
->table
== nullptr)
433 internal_error (__FILE__
, __LINE__
,
434 _("misplaced table_end or missing table_begin."));
436 uo_table_end (uiout
);
438 uiout
->table
= nullptr;
442 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
443 const std::string
&col_name
, const std::string
&col_hdr
)
445 if (uiout
->table
== nullptr)
446 internal_error (__FILE__
, __LINE__
,
447 _("table_header outside a table is not valid; it must be "
448 "after a table_begin and before a table_body."));
450 uiout
->table
->append_header (width
, alignment
, col_name
, col_hdr
);
452 uo_table_header (uiout
, width
, alignment
, col_name
, col_hdr
);
456 do_cleanup_table_end (void *data
)
458 struct ui_out
*ui_out
= (struct ui_out
*) data
;
460 ui_out_table_end (ui_out
);
464 make_cleanup_ui_out_table_begin_end (struct ui_out
*ui_out
, int nr_cols
,
465 int nr_rows
, const char *tblid
)
467 ui_out_table_begin (ui_out
, nr_cols
, nr_rows
, tblid
);
468 return make_cleanup (do_cleanup_table_end
, ui_out
);
472 ui_out_begin (struct ui_out
*uiout
,
473 enum ui_out_type type
,
476 /* Be careful to verify the ``field'' before the new tuple/list is
477 pushed onto the stack. That way the containing list/table/row is
478 verified and not the newly created tuple/list. This verification
479 is needed (at least) for the case where a table row entry
480 contains either a tuple/list. For that case bookkeeping such as
481 updating the column count or advancing to the next heading still
482 needs to be performed. */
488 verify_field (uiout
, &fldno
, &width
, &align
);
491 push_level (uiout
, type
);
493 /* If the push puts us at the same level as a table row entry, we've
494 got a new table row. Put the header pointer back to the start. */
495 if (uiout
->table
!= nullptr
496 && uiout
->table
->current_state () == ui_out_table::state::BODY
497 && uiout
->table
->entry_level () == uiout
->level ())
498 uiout
->table
->start_row ();
500 uo_begin (uiout
, type
, id
);
504 ui_out_end (struct ui_out
*uiout
,
505 enum ui_out_type type
)
507 pop_level (uiout
, type
);
509 uo_end (uiout
, type
);
512 struct ui_out_end_cleanup_data
514 struct ui_out
*uiout
;
515 enum ui_out_type type
;
519 do_cleanup_end (void *data
)
521 struct ui_out_end_cleanup_data
*end_cleanup_data
522 = (struct ui_out_end_cleanup_data
*) data
;
524 ui_out_end (end_cleanup_data
->uiout
, end_cleanup_data
->type
);
525 xfree (end_cleanup_data
);
528 static struct cleanup
*
529 make_cleanup_ui_out_end (struct ui_out
*uiout
,
530 enum ui_out_type type
)
532 struct ui_out_end_cleanup_data
*end_cleanup_data
;
534 end_cleanup_data
= XNEW (struct ui_out_end_cleanup_data
);
535 end_cleanup_data
->uiout
= uiout
;
536 end_cleanup_data
->type
= type
;
537 return make_cleanup (do_cleanup_end
, end_cleanup_data
);
541 make_cleanup_ui_out_tuple_begin_end (struct ui_out
*uiout
,
544 ui_out_begin (uiout
, ui_out_type_tuple
, id
);
545 return make_cleanup_ui_out_end (uiout
, ui_out_type_tuple
);
549 make_cleanup_ui_out_list_begin_end (struct ui_out
*uiout
,
552 ui_out_begin (uiout
, ui_out_type_list
, id
);
553 return make_cleanup_ui_out_end (uiout
, ui_out_type_list
);
557 ui_out_field_int (struct ui_out
*uiout
,
565 verify_field (uiout
, &fldno
, &width
, &align
);
567 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
571 ui_out_field_fmt_int (struct ui_out
*uiout
,
573 enum ui_align input_align
,
581 verify_field (uiout
, &fldno
, &width
, &align
);
583 uo_field_int (uiout
, fldno
, input_width
, input_align
, fldname
, value
);
586 /* Documented in ui-out.h. */
589 ui_out_field_core_addr (struct ui_out
*uiout
,
591 struct gdbarch
*gdbarch
,
594 ui_out_field_string (uiout
, fldname
,
595 print_core_address (gdbarch
, address
));
599 ui_out_field_stream (struct ui_out
*uiout
,
601 struct ui_file
*stream
)
603 std::string buffer
= ui_file_as_string (stream
);
605 if (!buffer
.empty ())
606 ui_out_field_string (uiout
, fldname
, buffer
.c_str ());
608 ui_out_field_skip (uiout
, fldname
);
609 ui_file_rewind (stream
);
612 /* Used to omit a field. */
615 ui_out_field_skip (struct ui_out
*uiout
,
622 verify_field (uiout
, &fldno
, &width
, &align
);
624 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
628 ui_out_field_string (struct ui_out
*uiout
,
636 verify_field (uiout
, &fldno
, &width
, &align
);
638 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
643 ui_out_field_fmt (struct ui_out
*uiout
,
645 const char *format
, ...)
652 /* Will not align, but has to call anyway. */
653 verify_field (uiout
, &fldno
, &width
, &align
);
655 va_start (args
, format
);
657 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
663 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
665 uo_spaces (uiout
, numspaces
);
669 ui_out_text (struct ui_out
*uiout
,
672 uo_text (uiout
, string
);
676 ui_out_message (struct ui_out
*uiout
, const char *format
, ...)
680 va_start (args
, format
);
681 uo_message (uiout
, format
, args
);
686 ui_out_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
688 uo_wrap_hint (uiout
, identstring
);
692 ui_out_flush (struct ui_out
*uiout
)
698 ui_out_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
700 return uo_redirect (uiout
, outstream
);
703 /* Test the flags against the mask given. */
705 ui_out_test_flags (struct ui_out
*uiout
, ui_out_flags mask
)
707 return (uiout
->flags
& mask
);
711 ui_out_is_mi_like_p (struct ui_out
*uiout
)
713 return uiout
->impl
->is_mi_like_p
;
716 /* Interface to the implementation functions. */
719 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
723 if (!uiout
->impl
->table_begin
)
725 uiout
->impl
->table_begin (uiout
, nbrofcols
, nr_rows
, tblid
);
729 uo_table_body (struct ui_out
*uiout
)
731 if (!uiout
->impl
->table_body
)
733 uiout
->impl
->table_body (uiout
);
737 uo_table_end (struct ui_out
*uiout
)
739 if (!uiout
->impl
->table_end
)
741 uiout
->impl
->table_end (uiout
);
745 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
,
746 const std::string
&col_name
, const std::string
&col_hdr
)
748 if (!uiout
->impl
->table_header
)
750 uiout
->impl
->table_header (uiout
, width
, align
, col_name
, col_hdr
);
754 uo_begin (struct ui_out
*uiout
,
755 enum ui_out_type type
,
758 if (uiout
->impl
->begin
== NULL
)
760 uiout
->impl
->begin (uiout
, type
, id
);
764 uo_end (struct ui_out
*uiout
,
765 enum ui_out_type type
)
767 if (uiout
->impl
->end
== NULL
)
769 uiout
->impl
->end (uiout
, type
);
773 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
777 if (!uiout
->impl
->field_int
)
779 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
783 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
786 if (!uiout
->impl
->field_skip
)
788 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
792 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
797 if (!uiout
->impl
->field_string
)
799 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
803 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
808 if (!uiout
->impl
->field_fmt
)
810 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
814 uo_spaces (struct ui_out
*uiout
, int numspaces
)
816 if (!uiout
->impl
->spaces
)
818 uiout
->impl
->spaces (uiout
, numspaces
);
822 uo_text (struct ui_out
*uiout
,
825 if (!uiout
->impl
->text
)
827 uiout
->impl
->text (uiout
, string
);
831 uo_message (struct ui_out
*uiout
,
835 if (!uiout
->impl
->message
)
837 uiout
->impl
->message (uiout
, format
, args
);
841 uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
843 if (!uiout
->impl
->wrap_hint
)
845 uiout
->impl
->wrap_hint (uiout
, identstring
);
849 uo_flush (struct ui_out
*uiout
)
851 if (!uiout
->impl
->flush
)
853 uiout
->impl
->flush (uiout
);
857 uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
859 if (!uiout
->impl
->redirect
)
861 return uiout
->impl
->redirect (uiout
, outstream
);
864 /* Verify that the field/tuple/list is correctly positioned. Return
865 the field number and corresponding alignment (if
866 available/applicable). */
869 verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
870 enum ui_align
*align
)
872 ui_out_level
*current
= current_level (uiout
);
875 if (uiout
->table
!= nullptr
876 && uiout
->table
->current_state () != ui_out_table::state::BODY
)
878 internal_error (__FILE__
, __LINE__
,
879 _("table_body missing; table fields must be \
880 specified after table_body and inside a list."));
883 current
->inc_field_count ();
885 if (uiout
->table
!= nullptr
886 && uiout
->table
->current_state () == ui_out_table::state::BODY
887 && uiout
->table
->entry_level () == uiout
->level ()
888 && uiout
->table
->get_next_header (fldno
, width
, align
, &text
))
890 if (*fldno
!= current
->field_count ())
891 internal_error (__FILE__
, __LINE__
,
892 _("ui-out internal error in handling headers."));
898 *fldno
= current
->field_count ();
903 /* Access to ui-out members data. */
906 ui_out_data (struct ui_out
*uiout
)
911 /* Access table field parameters. */
913 ui_out_query_field (struct ui_out
*uiout
, int colno
,
914 int *width
, int *alignment
, const char **col_name
)
916 if (uiout
->table
== nullptr)
919 return uiout
->table
->query_field (colno
, width
, alignment
, col_name
);
922 /* Initialize private members at startup. */
925 ui_out_new (const struct ui_out_impl
*impl
, void *data
,
928 struct ui_out
*uiout
= new ui_out ();
932 uiout
->flags
= flags
;
934 /* Create the ui-out level #1, the default level. */
935 push_level (uiout
, ui_out_type_tuple
);