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 /* table header structures */
38 enum ui_align alignment
;
45 /* Count each field; the first element is for non-list fields. */
47 /* The type of this level. */
48 enum ui_out_type type
;
52 /* Tables are special. Maintain a separate structure that tracks
53 their state. At present an output can only contain a single table
54 but that restriction might eventually be lifted. */
58 /* If on, a table is being generated. */
61 /* If on, the body of a table is being generated. If off, the table
62 header is being generated. */
65 /* The level at which each entry of the table is to be found. A row
66 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
67 above that of the table. */
70 /* Number of table columns (as specified in the table_begin call). */
73 /* String identifying the table (as specified in the table_begin
77 /* Pointers to the column headers. */
78 std::vector
<std::unique_ptr
<ui_out_hdr
>> headers
;
80 /* Iterator over the headers vector, used when printing successive fields. */
81 std::vector
<std::unique_ptr
<ui_out_hdr
>>::const_iterator headers_iterator
;
86 /* The ui_out structure */
91 /* Specific implementation of ui-out. */
92 const struct ui_out_impl
*impl
;
98 /* Vector to store and track the ui-out levels. */
99 std::vector
<std::unique_ptr
<ui_out_level
>> levels
;
101 /* A table, if any. At present only a single table is supported. */
102 struct ui_out_table table
;
105 /* The current (inner most) level. */
106 static struct ui_out_level
*
107 current_level (struct ui_out
*uiout
)
109 return uiout
->levels
[uiout
->level
].get ();
112 /* Create a new level, of TYPE. Return the new level's index. */
114 push_level (struct ui_out
*uiout
,
115 enum ui_out_type type
)
117 std::unique_ptr
<ui_out_level
> current (new ui_out_level ());
119 current
->field_count
= 0;
120 current
->type
= type
;
123 uiout
->levels
.push_back (std::move (current
));
128 /* Discard the current level, return the discarded level's index.
129 TYPE is the type of the level being discarded. */
131 pop_level (struct ui_out
*uiout
,
132 enum ui_out_type type
)
134 /* We had better not underflow the buffer. */
135 gdb_assert (uiout
->level
> 0);
136 gdb_assert (current_level (uiout
)->type
== type
);
138 uiout
->levels
.pop_back ();
141 return uiout
->level
+ 1;
144 /* These are the interfaces to implementation functions. */
146 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
147 int nr_rows
, const char *tblid
);
148 static void uo_table_body (struct ui_out
*uiout
);
149 static void uo_table_end (struct ui_out
*uiout
);
150 static void uo_table_header (struct ui_out
*uiout
, int width
,
151 enum ui_align align
, const char *col_name
,
153 static void uo_begin (struct ui_out
*uiout
,
154 enum ui_out_type type
,
155 int level
, const char *id
);
156 static void uo_end (struct ui_out
*uiout
,
157 enum ui_out_type type
,
159 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
160 enum ui_align align
, const char *fldname
, int value
);
161 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
162 enum ui_align align
, const char *fldname
);
163 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
164 enum ui_align align
, const char *fldname
,
165 const char *format
, va_list args
)
166 ATTRIBUTE_PRINTF (6, 0);
167 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
168 static void uo_text (struct ui_out
*uiout
, const char *string
);
169 static void uo_message (struct ui_out
*uiout
,
170 const char *format
, va_list args
)
171 ATTRIBUTE_PRINTF (2, 0);
172 static void uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
);
173 static void uo_flush (struct ui_out
*uiout
);
174 static int uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
176 /* Prototypes for local functions */
178 static void append_header_to_list (struct ui_out
*uiout
, int width
,
179 enum ui_align alignment
, const char *col_name
,
181 static int get_next_header (struct ui_out
*uiout
, int *colno
, int *width
,
182 enum ui_align
*alignment
, char **colhdr
);
183 static void clear_header_list (struct ui_out
*uiout
);
184 static void clear_table (struct ui_out
*uiout
);
185 static void verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
186 enum ui_align
*align
);
188 /* exported functions (ui_out API) */
190 /* Mark beginning of a table. */
193 ui_out_table_begin (struct ui_out
*uiout
, int nbrofcols
,
194 int nr_rows
, const std::string
&tblid
)
196 if (uiout
->table
.flag
)
197 internal_error (__FILE__
, __LINE__
,
198 _("tables cannot be nested; table_begin found before \
199 previous table_end."));
201 uiout
->table
.flag
= 1;
202 uiout
->table
.body_flag
= 0;
203 uiout
->table
.entry_level
= uiout
->level
+ 1;
204 uiout
->table
.columns
= nbrofcols
;
205 uiout
->table
.id
= tblid
;
207 clear_header_list (uiout
);
209 uo_table_begin (uiout
, nbrofcols
, nr_rows
, uiout
->table
.id
.c_str ());
213 ui_out_table_body (struct ui_out
*uiout
)
215 if (!uiout
->table
.flag
)
216 internal_error (__FILE__
, __LINE__
,
217 _("table_body outside a table is not valid; it must be \
218 after a table_begin and before a table_end."));
219 if (uiout
->table
.body_flag
)
220 internal_error (__FILE__
, __LINE__
,
221 _("extra table_body call not allowed; there must be \
222 only one table_body after a table_begin and before a table_end."));
223 if (uiout
->table
.headers
.size () != uiout
->table
.columns
)
224 internal_error (__FILE__
, __LINE__
,
225 _("number of headers differ from number of table \
228 uiout
->table
.body_flag
= 1;
230 uo_table_body (uiout
);
234 ui_out_table_end (struct ui_out
*uiout
)
236 if (!uiout
->table
.flag
)
237 internal_error (__FILE__
, __LINE__
,
238 _("misplaced table_end or missing table_begin."));
240 uiout
->table
.entry_level
= 0;
241 uiout
->table
.body_flag
= 0;
242 uiout
->table
.flag
= 0;
244 uo_table_end (uiout
);
249 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
250 const char *col_name
,
253 if (!uiout
->table
.flag
|| uiout
->table
.body_flag
)
254 internal_error (__FILE__
, __LINE__
,
255 _("table header must be specified after table_begin \
256 and before table_body."));
258 append_header_to_list (uiout
, width
, alignment
, col_name
, colhdr
);
260 uo_table_header (uiout
, width
, alignment
, col_name
, colhdr
);
264 do_cleanup_table_end (void *data
)
266 struct ui_out
*ui_out
= (struct ui_out
*) data
;
268 ui_out_table_end (ui_out
);
272 make_cleanup_ui_out_table_begin_end (struct ui_out
*ui_out
, int nr_cols
,
273 int nr_rows
, const char *tblid
)
275 ui_out_table_begin (ui_out
, nr_cols
, nr_rows
, tblid
);
276 return make_cleanup (do_cleanup_table_end
, ui_out
);
280 ui_out_begin (struct ui_out
*uiout
,
281 enum ui_out_type type
,
286 if (uiout
->table
.flag
&& !uiout
->table
.body_flag
)
287 internal_error (__FILE__
, __LINE__
,
288 _("table header or table_body expected; lists must be \
289 specified after table_body."));
291 /* Be careful to verify the ``field'' before the new tuple/list is
292 pushed onto the stack. That way the containing list/table/row is
293 verified and not the newly created tuple/list. This verification
294 is needed (at least) for the case where a table row entry
295 contains either a tuple/list. For that case bookkeeping such as
296 updating the column count or advancing to the next heading still
297 needs to be performed. */
303 verify_field (uiout
, &fldno
, &width
, &align
);
306 new_level
= push_level (uiout
, type
);
308 /* If the push puts us at the same level as a table row entry, we've
309 got a new table row. Put the header pointer back to the start. */
310 if (uiout
->table
.body_flag
311 && uiout
->table
.entry_level
== new_level
)
312 uiout
->table
.headers_iterator
= uiout
->table
.headers
.begin ();
314 uo_begin (uiout
, type
, new_level
, id
);
318 ui_out_end (struct ui_out
*uiout
,
319 enum ui_out_type type
)
321 int old_level
= pop_level (uiout
, type
);
323 uo_end (uiout
, type
, old_level
);
326 struct ui_out_end_cleanup_data
328 struct ui_out
*uiout
;
329 enum ui_out_type type
;
333 do_cleanup_end (void *data
)
335 struct ui_out_end_cleanup_data
*end_cleanup_data
336 = (struct ui_out_end_cleanup_data
*) data
;
338 ui_out_end (end_cleanup_data
->uiout
, end_cleanup_data
->type
);
339 xfree (end_cleanup_data
);
342 static struct cleanup
*
343 make_cleanup_ui_out_end (struct ui_out
*uiout
,
344 enum ui_out_type type
)
346 struct ui_out_end_cleanup_data
*end_cleanup_data
;
348 end_cleanup_data
= XNEW (struct ui_out_end_cleanup_data
);
349 end_cleanup_data
->uiout
= uiout
;
350 end_cleanup_data
->type
= type
;
351 return make_cleanup (do_cleanup_end
, end_cleanup_data
);
355 make_cleanup_ui_out_tuple_begin_end (struct ui_out
*uiout
,
358 ui_out_begin (uiout
, ui_out_type_tuple
, id
);
359 return make_cleanup_ui_out_end (uiout
, ui_out_type_tuple
);
363 make_cleanup_ui_out_list_begin_end (struct ui_out
*uiout
,
366 ui_out_begin (uiout
, ui_out_type_list
, id
);
367 return make_cleanup_ui_out_end (uiout
, ui_out_type_list
);
371 ui_out_field_int (struct ui_out
*uiout
,
379 verify_field (uiout
, &fldno
, &width
, &align
);
381 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
385 ui_out_field_fmt_int (struct ui_out
*uiout
,
387 enum ui_align input_align
,
395 verify_field (uiout
, &fldno
, &width
, &align
);
397 uo_field_int (uiout
, fldno
, input_width
, input_align
, fldname
, value
);
400 /* Documented in ui-out.h. */
403 ui_out_field_core_addr (struct ui_out
*uiout
,
405 struct gdbarch
*gdbarch
,
408 ui_out_field_string (uiout
, fldname
,
409 print_core_address (gdbarch
, address
));
413 ui_out_field_stream (struct ui_out
*uiout
,
415 struct ui_file
*stream
)
417 std::string buffer
= ui_file_as_string (stream
);
419 if (!buffer
.empty ())
420 ui_out_field_string (uiout
, fldname
, buffer
.c_str ());
422 ui_out_field_skip (uiout
, fldname
);
423 ui_file_rewind (stream
);
426 /* Used to omit a field. */
429 ui_out_field_skip (struct ui_out
*uiout
,
436 verify_field (uiout
, &fldno
, &width
, &align
);
438 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
442 ui_out_field_string (struct ui_out
*uiout
,
450 verify_field (uiout
, &fldno
, &width
, &align
);
452 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
457 ui_out_field_fmt (struct ui_out
*uiout
,
459 const char *format
, ...)
466 /* Will not align, but has to call anyway. */
467 verify_field (uiout
, &fldno
, &width
, &align
);
469 va_start (args
, format
);
471 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
477 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
479 uo_spaces (uiout
, numspaces
);
483 ui_out_text (struct ui_out
*uiout
,
486 uo_text (uiout
, string
);
490 ui_out_message (struct ui_out
*uiout
, const char *format
, ...)
494 va_start (args
, format
);
495 uo_message (uiout
, format
, args
);
500 ui_out_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
502 uo_wrap_hint (uiout
, identstring
);
506 ui_out_flush (struct ui_out
*uiout
)
512 ui_out_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
514 return uo_redirect (uiout
, outstream
);
517 /* Test the flags against the mask given. */
519 ui_out_test_flags (struct ui_out
*uiout
, int mask
)
521 return (uiout
->flags
& mask
);
525 ui_out_is_mi_like_p (struct ui_out
*uiout
)
527 return uiout
->impl
->is_mi_like_p
;
530 /* Interface to the implementation functions. */
533 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
537 if (!uiout
->impl
->table_begin
)
539 uiout
->impl
->table_begin (uiout
, nbrofcols
, nr_rows
, tblid
);
543 uo_table_body (struct ui_out
*uiout
)
545 if (!uiout
->impl
->table_body
)
547 uiout
->impl
->table_body (uiout
);
551 uo_table_end (struct ui_out
*uiout
)
553 if (!uiout
->impl
->table_end
)
555 uiout
->impl
->table_end (uiout
);
559 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
,
560 const char *col_name
,
563 if (!uiout
->impl
->table_header
)
565 uiout
->impl
->table_header (uiout
, width
, align
, col_name
, colhdr
);
568 /* Clear the table associated with UIOUT. */
571 clear_table (struct ui_out
*uiout
)
573 uiout
->table
.id
.clear ();
574 clear_header_list (uiout
);
578 uo_begin (struct ui_out
*uiout
,
579 enum ui_out_type type
,
583 if (uiout
->impl
->begin
== NULL
)
585 uiout
->impl
->begin (uiout
, type
, level
, id
);
589 uo_end (struct ui_out
*uiout
,
590 enum ui_out_type type
,
593 if (uiout
->impl
->end
== NULL
)
595 uiout
->impl
->end (uiout
, type
, level
);
599 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
603 if (!uiout
->impl
->field_int
)
605 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
609 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
612 if (!uiout
->impl
->field_skip
)
614 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
618 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
623 if (!uiout
->impl
->field_string
)
625 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
629 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
634 if (!uiout
->impl
->field_fmt
)
636 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
640 uo_spaces (struct ui_out
*uiout
, int numspaces
)
642 if (!uiout
->impl
->spaces
)
644 uiout
->impl
->spaces (uiout
, numspaces
);
648 uo_text (struct ui_out
*uiout
,
651 if (!uiout
->impl
->text
)
653 uiout
->impl
->text (uiout
, string
);
657 uo_message (struct ui_out
*uiout
,
661 if (!uiout
->impl
->message
)
663 uiout
->impl
->message (uiout
, format
, args
);
667 uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
669 if (!uiout
->impl
->wrap_hint
)
671 uiout
->impl
->wrap_hint (uiout
, identstring
);
675 uo_flush (struct ui_out
*uiout
)
677 if (!uiout
->impl
->flush
)
679 uiout
->impl
->flush (uiout
);
683 uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
685 if (!uiout
->impl
->redirect
)
687 return uiout
->impl
->redirect (uiout
, outstream
);
690 /* local functions */
692 /* List of column headers manipulation routines. */
695 clear_header_list (struct ui_out
*uiout
)
697 for (auto &it
: uiout
->table
.headers
)
700 xfree (it
->col_name
);
703 uiout
->table
.headers
.clear ();
704 uiout
->table
.headers_iterator
= uiout
->table
.headers
.end ();
708 append_header_to_list (struct ui_out
*uiout
,
710 enum ui_align alignment
,
711 const char *col_name
,
714 std::unique_ptr
<ui_out_hdr
> temphdr (new ui_out_hdr ());
716 temphdr
->width
= width
;
717 temphdr
->alignment
= alignment
;
718 /* We have to copy the column title as the original may be an
721 temphdr
->colhdr
= xstrdup (colhdr
);
723 temphdr
->colhdr
= NULL
;
725 if (col_name
!= NULL
)
726 temphdr
->col_name
= xstrdup (col_name
);
727 else if (colhdr
!= NULL
)
728 temphdr
->col_name
= xstrdup (colhdr
);
730 temphdr
->col_name
= NULL
;
732 temphdr
->colno
= uiout
->table
.headers
.size () + 1;
734 uiout
->table
.headers
.push_back (std::move (temphdr
));
737 /* Extract the format information for the NEXT header and advance
738 the header pointer. Return 0 if there was no next header. */
741 get_next_header (struct ui_out
*uiout
,
744 enum ui_align
*alignment
,
747 /* There may be no headers at all or we may have used all columns. */
748 if (uiout
->table
.headers_iterator
== uiout
->table
.headers
.end ())
751 ui_out_hdr
*hdr
= uiout
->table
.headers_iterator
->get ();
755 *alignment
= hdr
->alignment
;
756 *colhdr
= hdr
->colhdr
;
758 /* Advance the header pointer to the next entry. */
759 uiout
->table
.headers_iterator
++;
765 /* Verify that the field/tuple/list is correctly positioned. Return
766 the field number and corresponding alignment (if
767 available/applicable). */
770 verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
771 enum ui_align
*align
)
773 struct ui_out_level
*current
= current_level (uiout
);
776 if (uiout
->table
.flag
)
778 if (!uiout
->table
.body_flag
)
779 internal_error (__FILE__
, __LINE__
,
780 _("table_body missing; table fields must be \
781 specified after table_body and inside a list."));
782 /* NOTE: cagney/2001-12-08: There was a check here to ensure
783 that this code was only executed when uiout->level was
784 greater than zero. That no longer applies - this code is run
785 before each table row tuple is started and at that point the
789 current
->field_count
+= 1;
791 if (uiout
->table
.body_flag
792 && uiout
->table
.entry_level
== uiout
->level
793 && get_next_header (uiout
, fldno
, width
, align
, &text
))
795 if (*fldno
!= current
->field_count
)
796 internal_error (__FILE__
, __LINE__
,
797 _("ui-out internal error in handling headers."));
803 *fldno
= current
->field_count
;
808 /* Access to ui-out members data. */
811 ui_out_data (struct ui_out
*uiout
)
816 /* Access table field parameters. */
818 ui_out_query_field (struct ui_out
*uiout
, int colno
,
819 int *width
, int *alignment
, char **col_name
)
821 if (!uiout
->table
.flag
)
824 /* Column numbers are 1-based, so convert to 0-based index. */
825 int index
= colno
- 1;
827 if (index
>= 0 && index
< uiout
->table
.headers
.size ())
829 ui_out_hdr
*hdr
= uiout
->table
.headers
[index
].get ();
831 gdb_assert (colno
== hdr
->colno
);
834 *alignment
= hdr
->alignment
;
835 *col_name
= hdr
->col_name
;
843 /* Initialize private members at startup. */
846 ui_out_new (const struct ui_out_impl
*impl
, void *data
,
849 struct ui_out
*uiout
= new ui_out ();
850 std::unique_ptr
<ui_out_level
> current (new ui_out_level ());
854 uiout
->flags
= flags
;
855 uiout
->table
.flag
= 0;
856 uiout
->table
.body_flag
= 0;
859 /* Create uiout->level 0, the default level. */
860 current
->type
= ui_out_type_tuple
;
861 current
->field_count
= 0;
862 uiout
->levels
.push_back (std::move (current
));
864 uiout
->table
.headers_iterator
= uiout
->table
.headers
.end ();