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. */
130 /* Tables are special. Maintain a separate structure that tracks
131 their state. At present an output can only contain a single table
132 but that restriction might eventually be lifted. */
136 /* If on, a table is being generated. */
139 /* If on, the body of a table is being generated. If off, the table
140 header is being generated. */
143 /* The level at which each entry of the table is to be found. A row
144 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
145 above that of the table. */
148 /* Number of table columns (as specified in the table_begin call). */
151 /* String identifying the table (as specified in the table_begin
155 /* Pointers to the column headers. */
156 std::vector
<std::unique_ptr
<ui_out_hdr
>> headers
;
158 /* Iterator over the headers vector, used when printing successive fields. */
159 std::vector
<std::unique_ptr
<ui_out_hdr
>>::const_iterator headers_iterator
;
164 /* The ui_out structure */
169 /* Specific implementation of ui-out. */
170 const struct ui_out_impl
*impl
;
176 /* Vector to store and track the ui-out levels. */
177 std::vector
<std::unique_ptr
<ui_out_level
>> levels
;
179 /* A table, if any. At present only a single table is supported. */
180 struct ui_out_table table
;
183 /* The current (inner most) level. */
184 static ui_out_level
*
185 current_level (struct ui_out
*uiout
)
187 return uiout
->levels
[uiout
->level
].get ();
190 /* Create a new level, of TYPE. Return the new level's index. */
192 push_level (struct ui_out
*uiout
,
193 enum ui_out_type type
)
195 std::unique_ptr
<ui_out_level
> level (new ui_out_level (type
));
197 uiout
->levels
.push_back (std::move (level
));
203 /* Discard the current level, return the discarded level's index.
204 TYPE is the type of the level being discarded. */
206 pop_level (struct ui_out
*uiout
,
207 enum ui_out_type type
)
209 /* We had better not underflow the buffer. */
210 gdb_assert (uiout
->level
> 0);
211 gdb_assert (current_level (uiout
)->type () == type
);
213 uiout
->levels
.pop_back ();
216 return uiout
->level
+ 1;
219 /* These are the interfaces to implementation functions. */
221 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
222 int nr_rows
, const char *tblid
);
223 static void uo_table_body (struct ui_out
*uiout
);
224 static void uo_table_end (struct ui_out
*uiout
);
225 static void uo_table_header (struct ui_out
*uiout
, int width
,
227 const std::string
&col_name
,
228 const std::string
&col_hdr
);
229 static void uo_begin (struct ui_out
*uiout
,
230 enum ui_out_type type
,
231 int level
, const char *id
);
232 static void uo_end (struct ui_out
*uiout
,
233 enum ui_out_type type
,
235 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
236 enum ui_align align
, const char *fldname
, int value
);
237 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
238 enum ui_align align
, const char *fldname
);
239 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
240 enum ui_align align
, const char *fldname
,
241 const char *format
, va_list args
)
242 ATTRIBUTE_PRINTF (6, 0);
243 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
244 static void uo_text (struct ui_out
*uiout
, const char *string
);
245 static void uo_message (struct ui_out
*uiout
,
246 const char *format
, va_list args
)
247 ATTRIBUTE_PRINTF (2, 0);
248 static void uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
);
249 static void uo_flush (struct ui_out
*uiout
);
250 static int uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
252 /* Prototypes for local functions */
254 static void append_header_to_list (struct ui_out
*uiout
, int width
,
255 enum ui_align alignment
,
256 const std::string
&col_name
,
257 const std::string
&col_hdr
);
258 static int get_next_header (struct ui_out
*uiout
, int *colno
, int *width
,
259 enum ui_align
*alignment
, const char **col_hdr
);
260 static void clear_header_list (struct ui_out
*uiout
);
261 static void clear_table (struct ui_out
*uiout
);
262 static void verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
263 enum ui_align
*align
);
265 /* exported functions (ui_out API) */
267 /* Mark beginning of a table. */
270 ui_out_table_begin (struct ui_out
*uiout
, int nbrofcols
,
271 int nr_rows
, const std::string
&tblid
)
273 if (uiout
->table
.flag
)
274 internal_error (__FILE__
, __LINE__
,
275 _("tables cannot be nested; table_begin found before \
276 previous table_end."));
278 uiout
->table
.flag
= 1;
279 uiout
->table
.body_flag
= 0;
280 uiout
->table
.entry_level
= uiout
->level
+ 1;
281 uiout
->table
.columns
= nbrofcols
;
282 uiout
->table
.id
= tblid
;
284 clear_header_list (uiout
);
286 uo_table_begin (uiout
, nbrofcols
, nr_rows
, uiout
->table
.id
.c_str ());
290 ui_out_table_body (struct ui_out
*uiout
)
292 if (!uiout
->table
.flag
)
293 internal_error (__FILE__
, __LINE__
,
294 _("table_body outside a table is not valid; it must be \
295 after a table_begin and before a table_end."));
296 if (uiout
->table
.body_flag
)
297 internal_error (__FILE__
, __LINE__
,
298 _("extra table_body call not allowed; there must be \
299 only one table_body after a table_begin and before a table_end."));
300 if (uiout
->table
.headers
.size () != uiout
->table
.columns
)
301 internal_error (__FILE__
, __LINE__
,
302 _("number of headers differ from number of table \
305 uiout
->table
.body_flag
= 1;
307 uo_table_body (uiout
);
311 ui_out_table_end (struct ui_out
*uiout
)
313 if (!uiout
->table
.flag
)
314 internal_error (__FILE__
, __LINE__
,
315 _("misplaced table_end or missing table_begin."));
317 uiout
->table
.entry_level
= 0;
318 uiout
->table
.body_flag
= 0;
319 uiout
->table
.flag
= 0;
321 uo_table_end (uiout
);
326 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
327 const std::string
&col_name
, const std::string
&col_hdr
)
329 if (!uiout
->table
.flag
|| uiout
->table
.body_flag
)
330 internal_error (__FILE__
, __LINE__
,
331 _("table header must be specified after table_begin \
332 and before table_body."));
334 append_header_to_list (uiout
, width
, alignment
, col_name
, col_hdr
);
336 uo_table_header (uiout
, width
, alignment
, col_name
, col_hdr
);
340 do_cleanup_table_end (void *data
)
342 struct ui_out
*ui_out
= (struct ui_out
*) data
;
344 ui_out_table_end (ui_out
);
348 make_cleanup_ui_out_table_begin_end (struct ui_out
*ui_out
, int nr_cols
,
349 int nr_rows
, const char *tblid
)
351 ui_out_table_begin (ui_out
, nr_cols
, nr_rows
, tblid
);
352 return make_cleanup (do_cleanup_table_end
, ui_out
);
356 ui_out_begin (struct ui_out
*uiout
,
357 enum ui_out_type type
,
362 if (uiout
->table
.flag
&& !uiout
->table
.body_flag
)
363 internal_error (__FILE__
, __LINE__
,
364 _("table header or table_body expected; lists must be \
365 specified after table_body."));
367 /* Be careful to verify the ``field'' before the new tuple/list is
368 pushed onto the stack. That way the containing list/table/row is
369 verified and not the newly created tuple/list. This verification
370 is needed (at least) for the case where a table row entry
371 contains either a tuple/list. For that case bookkeeping such as
372 updating the column count or advancing to the next heading still
373 needs to be performed. */
379 verify_field (uiout
, &fldno
, &width
, &align
);
382 new_level
= push_level (uiout
, type
);
384 /* If the push puts us at the same level as a table row entry, we've
385 got a new table row. Put the header pointer back to the start. */
386 if (uiout
->table
.body_flag
387 && uiout
->table
.entry_level
== new_level
)
388 uiout
->table
.headers_iterator
= uiout
->table
.headers
.begin ();
390 uo_begin (uiout
, type
, new_level
, id
);
394 ui_out_end (struct ui_out
*uiout
,
395 enum ui_out_type type
)
397 int old_level
= pop_level (uiout
, type
);
399 uo_end (uiout
, type
, old_level
);
402 struct ui_out_end_cleanup_data
404 struct ui_out
*uiout
;
405 enum ui_out_type type
;
409 do_cleanup_end (void *data
)
411 struct ui_out_end_cleanup_data
*end_cleanup_data
412 = (struct ui_out_end_cleanup_data
*) data
;
414 ui_out_end (end_cleanup_data
->uiout
, end_cleanup_data
->type
);
415 xfree (end_cleanup_data
);
418 static struct cleanup
*
419 make_cleanup_ui_out_end (struct ui_out
*uiout
,
420 enum ui_out_type type
)
422 struct ui_out_end_cleanup_data
*end_cleanup_data
;
424 end_cleanup_data
= XNEW (struct ui_out_end_cleanup_data
);
425 end_cleanup_data
->uiout
= uiout
;
426 end_cleanup_data
->type
= type
;
427 return make_cleanup (do_cleanup_end
, end_cleanup_data
);
431 make_cleanup_ui_out_tuple_begin_end (struct ui_out
*uiout
,
434 ui_out_begin (uiout
, ui_out_type_tuple
, id
);
435 return make_cleanup_ui_out_end (uiout
, ui_out_type_tuple
);
439 make_cleanup_ui_out_list_begin_end (struct ui_out
*uiout
,
442 ui_out_begin (uiout
, ui_out_type_list
, id
);
443 return make_cleanup_ui_out_end (uiout
, ui_out_type_list
);
447 ui_out_field_int (struct ui_out
*uiout
,
455 verify_field (uiout
, &fldno
, &width
, &align
);
457 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
461 ui_out_field_fmt_int (struct ui_out
*uiout
,
463 enum ui_align input_align
,
471 verify_field (uiout
, &fldno
, &width
, &align
);
473 uo_field_int (uiout
, fldno
, input_width
, input_align
, fldname
, value
);
476 /* Documented in ui-out.h. */
479 ui_out_field_core_addr (struct ui_out
*uiout
,
481 struct gdbarch
*gdbarch
,
484 ui_out_field_string (uiout
, fldname
,
485 print_core_address (gdbarch
, address
));
489 ui_out_field_stream (struct ui_out
*uiout
,
491 struct ui_file
*stream
)
493 std::string buffer
= ui_file_as_string (stream
);
495 if (!buffer
.empty ())
496 ui_out_field_string (uiout
, fldname
, buffer
.c_str ());
498 ui_out_field_skip (uiout
, fldname
);
499 ui_file_rewind (stream
);
502 /* Used to omit a field. */
505 ui_out_field_skip (struct ui_out
*uiout
,
512 verify_field (uiout
, &fldno
, &width
, &align
);
514 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
518 ui_out_field_string (struct ui_out
*uiout
,
526 verify_field (uiout
, &fldno
, &width
, &align
);
528 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
533 ui_out_field_fmt (struct ui_out
*uiout
,
535 const char *format
, ...)
542 /* Will not align, but has to call anyway. */
543 verify_field (uiout
, &fldno
, &width
, &align
);
545 va_start (args
, format
);
547 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
553 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
555 uo_spaces (uiout
, numspaces
);
559 ui_out_text (struct ui_out
*uiout
,
562 uo_text (uiout
, string
);
566 ui_out_message (struct ui_out
*uiout
, const char *format
, ...)
570 va_start (args
, format
);
571 uo_message (uiout
, format
, args
);
576 ui_out_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
578 uo_wrap_hint (uiout
, identstring
);
582 ui_out_flush (struct ui_out
*uiout
)
588 ui_out_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
590 return uo_redirect (uiout
, outstream
);
593 /* Test the flags against the mask given. */
595 ui_out_test_flags (struct ui_out
*uiout
, int mask
)
597 return (uiout
->flags
& mask
);
601 ui_out_is_mi_like_p (struct ui_out
*uiout
)
603 return uiout
->impl
->is_mi_like_p
;
606 /* Interface to the implementation functions. */
609 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
613 if (!uiout
->impl
->table_begin
)
615 uiout
->impl
->table_begin (uiout
, nbrofcols
, nr_rows
, tblid
);
619 uo_table_body (struct ui_out
*uiout
)
621 if (!uiout
->impl
->table_body
)
623 uiout
->impl
->table_body (uiout
);
627 uo_table_end (struct ui_out
*uiout
)
629 if (!uiout
->impl
->table_end
)
631 uiout
->impl
->table_end (uiout
);
635 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
,
636 const std::string
&col_name
, const std::string
&col_hdr
)
638 if (!uiout
->impl
->table_header
)
640 uiout
->impl
->table_header (uiout
, width
, align
, col_name
, col_hdr
);
643 /* Clear the table associated with UIOUT. */
646 clear_table (struct ui_out
*uiout
)
648 uiout
->table
.id
.clear ();
649 clear_header_list (uiout
);
653 uo_begin (struct ui_out
*uiout
,
654 enum ui_out_type type
,
658 if (uiout
->impl
->begin
== NULL
)
660 uiout
->impl
->begin (uiout
, type
, level
, id
);
664 uo_end (struct ui_out
*uiout
,
665 enum ui_out_type type
,
668 if (uiout
->impl
->end
== NULL
)
670 uiout
->impl
->end (uiout
, type
, level
);
674 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
678 if (!uiout
->impl
->field_int
)
680 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
684 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
687 if (!uiout
->impl
->field_skip
)
689 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
693 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
698 if (!uiout
->impl
->field_string
)
700 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
704 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
709 if (!uiout
->impl
->field_fmt
)
711 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
715 uo_spaces (struct ui_out
*uiout
, int numspaces
)
717 if (!uiout
->impl
->spaces
)
719 uiout
->impl
->spaces (uiout
, numspaces
);
723 uo_text (struct ui_out
*uiout
,
726 if (!uiout
->impl
->text
)
728 uiout
->impl
->text (uiout
, string
);
732 uo_message (struct ui_out
*uiout
,
736 if (!uiout
->impl
->message
)
738 uiout
->impl
->message (uiout
, format
, args
);
742 uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
744 if (!uiout
->impl
->wrap_hint
)
746 uiout
->impl
->wrap_hint (uiout
, identstring
);
750 uo_flush (struct ui_out
*uiout
)
752 if (!uiout
->impl
->flush
)
754 uiout
->impl
->flush (uiout
);
758 uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
760 if (!uiout
->impl
->redirect
)
762 return uiout
->impl
->redirect (uiout
, outstream
);
765 /* local functions */
767 /* List of column headers manipulation routines. */
770 clear_header_list (struct ui_out
*uiout
)
772 uiout
->table
.headers
.clear ();
773 uiout
->table
.headers_iterator
= uiout
->table
.headers
.end ();
777 append_header_to_list (struct ui_out
*uiout
,
779 enum ui_align alignment
,
780 const std::string
&col_name
,
781 const std::string
&col_hdr
)
783 std::unique_ptr
<ui_out_hdr
> temphdr(
784 new ui_out_hdr (uiout
->table
.headers
.size () + 1, width
,
785 alignment
, col_name
, col_hdr
));
787 uiout
->table
.headers
.push_back (std::move (temphdr
));
790 /* Extract the format information for the NEXT header and advance
791 the header pointer. Return 0 if there was no next header. */
794 get_next_header (struct ui_out
*uiout
,
797 enum ui_align
*alignment
,
798 const char **col_hdr
)
800 /* There may be no headers at all or we may have used all columns. */
801 if (uiout
->table
.headers_iterator
== uiout
->table
.headers
.end ())
804 ui_out_hdr
*hdr
= uiout
->table
.headers_iterator
->get ();
806 *colno
= hdr
->number ();
807 *width
= hdr
->min_width ();
808 *alignment
= hdr
->alignment ();
809 *col_hdr
= hdr
->header ().c_str ();
811 /* Advance the header pointer to the next entry. */
812 uiout
->table
.headers_iterator
++;
818 /* Verify that the field/tuple/list is correctly positioned. Return
819 the field number and corresponding alignment (if
820 available/applicable). */
823 verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
824 enum ui_align
*align
)
826 ui_out_level
*current
= current_level (uiout
);
829 if (uiout
->table
.flag
)
831 if (!uiout
->table
.body_flag
)
832 internal_error (__FILE__
, __LINE__
,
833 _("table_body missing; table fields must be \
834 specified after table_body and inside a list."));
835 /* NOTE: cagney/2001-12-08: There was a check here to ensure
836 that this code was only executed when uiout->level was
837 greater than zero. That no longer applies - this code is run
838 before each table row tuple is started and at that point the
842 current
->inc_field_count ();
844 if (uiout
->table
.body_flag
845 && uiout
->table
.entry_level
== uiout
->level
846 && get_next_header (uiout
, fldno
, width
, align
, &text
))
848 if (*fldno
!= current
->field_count ())
849 internal_error (__FILE__
, __LINE__
,
850 _("ui-out internal error in handling headers."));
856 *fldno
= current
->field_count ();
861 /* Access to ui-out members data. */
864 ui_out_data (struct ui_out
*uiout
)
869 /* Access table field parameters. */
871 ui_out_query_field (struct ui_out
*uiout
, int colno
,
872 int *width
, int *alignment
, const char **col_name
)
874 if (!uiout
->table
.flag
)
877 /* Column numbers are 1-based, so convert to 0-based index. */
878 int index
= colno
- 1;
880 if (index
>= 0 && index
< uiout
->table
.headers
.size ())
882 ui_out_hdr
*hdr
= uiout
->table
.headers
[index
].get ();
884 gdb_assert (colno
== hdr
->number ());
886 *width
= hdr
->min_width ();
887 *alignment
= hdr
->alignment ();
888 *col_name
= hdr
->name ().c_str ();
896 /* Initialize private members at startup. */
899 ui_out_new (const struct ui_out_impl
*impl
, void *data
,
902 struct ui_out
*uiout
= new ui_out ();
906 uiout
->flags
= flags
;
907 uiout
->table
.flag
= 0;
908 uiout
->table
.body_flag
= 0;
911 /* Create uiout->level 0, the default level. */
912 std::unique_ptr
<ui_out_level
> level (new ui_out_level (ui_out_type_tuple
));
913 uiout
->levels
.push_back (std::move (level
));
915 uiout
->table
.headers_iterator
= uiout
->table
.headers
.end ();