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 */
28 /* table header structures */
34 enum ui_align alignment
;
37 struct ui_out_hdr
*next
;
42 /* Count each field; the first element is for non-list fields. */
44 /* The type of this level. */
45 enum ui_out_type type
;
48 /* Define uiout->level vector types and operations. */
49 typedef struct ui_out_level
*ui_out_level_p
;
50 DEF_VEC_P (ui_out_level_p
);
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 /* Points to the first table header (if any). */
78 struct ui_out_hdr
*header_first
;
80 /* Points to the last table header (if any). */
81 struct ui_out_hdr
*header_last
;
83 /* Points to header of NEXT column to format. */
84 struct ui_out_hdr
*header_next
;
89 /* The ui_out structure */
90 /* Any change here requires a corresponding one in the initialization
91 of the default uiout, which is statically initialized. */
96 /* Specific implementation of ui-out. */
97 const struct ui_out_impl
*impl
;
103 /* Vector to store and track the ui-out levels. */
104 VEC (ui_out_level_p
) *levels
;
106 /* A table, if any. At present only a single table is supported. */
107 struct ui_out_table table
;
110 /* The current (inner most) level. */
111 static struct ui_out_level
*
112 current_level (struct ui_out
*uiout
)
114 return VEC_index (ui_out_level_p
, uiout
->levels
, uiout
->level
);
117 /* Create a new level, of TYPE. Return the new level's index. */
119 push_level (struct ui_out
*uiout
,
120 enum ui_out_type type
)
122 struct ui_out_level
*current
;
125 current
= XNEW (struct ui_out_level
);
126 current
->field_count
= 0;
127 current
->type
= type
;
128 VEC_safe_push (ui_out_level_p
, uiout
->levels
, current
);
132 /* Discard the current level, return the discarded level's index.
133 TYPE is the type of the level being discarded. */
135 pop_level (struct ui_out
*uiout
,
136 enum ui_out_type type
)
138 struct ui_out_level
*current
;
140 /* We had better not underflow the buffer. */
141 gdb_assert (uiout
->level
> 0);
142 gdb_assert (current_level (uiout
)->type
== type
);
143 current
= VEC_pop (ui_out_level_p
, uiout
->levels
);
146 return uiout
->level
+ 1;
149 /* These are the interfaces to implementation functions. */
151 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
152 int nr_rows
, const char *tblid
);
153 static void uo_table_body (struct ui_out
*uiout
);
154 static void uo_table_end (struct ui_out
*uiout
);
155 static void uo_table_header (struct ui_out
*uiout
, int width
,
156 enum ui_align align
, const char *col_name
,
158 static void uo_begin (struct ui_out
*uiout
,
159 enum ui_out_type type
,
160 int level
, const char *id
);
161 static void uo_end (struct ui_out
*uiout
,
162 enum ui_out_type type
,
164 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
165 enum ui_align align
, const char *fldname
, int value
);
166 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
167 enum ui_align align
, const char *fldname
);
168 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
169 enum ui_align align
, const char *fldname
,
170 const char *format
, va_list args
)
171 ATTRIBUTE_PRINTF (6, 0);
172 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
173 static void uo_text (struct ui_out
*uiout
, const char *string
);
174 static void uo_message (struct ui_out
*uiout
, int verbosity
,
175 const char *format
, va_list args
)
176 ATTRIBUTE_PRINTF (3, 0);
177 static void uo_wrap_hint (struct ui_out
*uiout
, char *identstring
);
178 static void uo_flush (struct ui_out
*uiout
);
179 static int uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
180 static void uo_data_destroy (struct ui_out
*uiout
);
182 /* Prototypes for local functions */
184 extern void _initialize_ui_out (void);
185 static void append_header_to_list (struct ui_out
*uiout
, int width
,
186 enum ui_align alignment
, const char *col_name
,
188 static int get_next_header (struct ui_out
*uiout
, int *colno
, int *width
,
189 enum ui_align
*alignment
, char **colhdr
);
190 static void clear_header_list (struct ui_out
*uiout
);
191 static void clear_table (struct ui_out
*uiout
);
192 static void verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
193 enum ui_align
*align
);
195 /* exported functions (ui_out API) */
197 /* Mark beginning of a table. */
200 ui_out_table_begin (struct ui_out
*uiout
, int nbrofcols
,
204 if (uiout
->table
.flag
)
205 internal_error (__FILE__
, __LINE__
,
206 _("tables cannot be nested; table_begin found before \
207 previous table_end."));
209 uiout
->table
.flag
= 1;
210 uiout
->table
.body_flag
= 0;
211 uiout
->table
.entry_level
= uiout
->level
+ 1;
212 uiout
->table
.columns
= nbrofcols
;
214 uiout
->table
.id
= xstrdup (tblid
);
216 uiout
->table
.id
= NULL
;
217 clear_header_list (uiout
);
219 uo_table_begin (uiout
, nbrofcols
, nr_rows
, uiout
->table
.id
);
223 ui_out_table_body (struct ui_out
*uiout
)
225 if (!uiout
->table
.flag
)
226 internal_error (__FILE__
, __LINE__
,
227 _("table_body outside a table is not valid; it must be \
228 after a table_begin and before a table_end."));
229 if (uiout
->table
.body_flag
)
230 internal_error (__FILE__
, __LINE__
,
231 _("extra table_body call not allowed; there must be \
232 only one table_body after a table_begin and before a table_end."));
233 if (uiout
->table
.header_next
->colno
!= uiout
->table
.columns
)
234 internal_error (__FILE__
, __LINE__
,
235 _("number of headers differ from number of table \
238 uiout
->table
.body_flag
= 1;
239 uiout
->table
.header_next
= uiout
->table
.header_first
;
241 uo_table_body (uiout
);
245 ui_out_table_end (struct ui_out
*uiout
)
247 if (!uiout
->table
.flag
)
248 internal_error (__FILE__
, __LINE__
,
249 _("misplaced table_end or missing table_begin."));
251 uiout
->table
.entry_level
= 0;
252 uiout
->table
.body_flag
= 0;
253 uiout
->table
.flag
= 0;
255 uo_table_end (uiout
);
260 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
261 const char *col_name
,
264 if (!uiout
->table
.flag
|| uiout
->table
.body_flag
)
265 internal_error (__FILE__
, __LINE__
,
266 _("table header must be specified after table_begin \
267 and before table_body."));
269 append_header_to_list (uiout
, width
, alignment
, col_name
, colhdr
);
271 uo_table_header (uiout
, width
, alignment
, col_name
, colhdr
);
275 do_cleanup_table_end (void *data
)
277 struct ui_out
*ui_out
= (struct ui_out
*) data
;
279 ui_out_table_end (ui_out
);
283 make_cleanup_ui_out_table_begin_end (struct ui_out
*ui_out
, int nr_cols
,
284 int nr_rows
, const char *tblid
)
286 ui_out_table_begin (ui_out
, nr_cols
, nr_rows
, tblid
);
287 return make_cleanup (do_cleanup_table_end
, ui_out
);
291 ui_out_begin (struct ui_out
*uiout
,
292 enum ui_out_type type
,
297 if (uiout
->table
.flag
&& !uiout
->table
.body_flag
)
298 internal_error (__FILE__
, __LINE__
,
299 _("table header or table_body expected; lists must be \
300 specified after table_body."));
302 /* Be careful to verify the ``field'' before the new tuple/list is
303 pushed onto the stack. That way the containing list/table/row is
304 verified and not the newly created tuple/list. This verification
305 is needed (at least) for the case where a table row entry
306 contains either a tuple/list. For that case bookkeeping such as
307 updating the column count or advancing to the next heading still
308 needs to be performed. */
314 verify_field (uiout
, &fldno
, &width
, &align
);
317 new_level
= push_level (uiout
, type
);
319 /* If the push puts us at the same level as a table row entry, we've
320 got a new table row. Put the header pointer back to the start. */
321 if (uiout
->table
.body_flag
322 && uiout
->table
.entry_level
== new_level
)
323 uiout
->table
.header_next
= uiout
->table
.header_first
;
325 uo_begin (uiout
, type
, new_level
, id
);
329 ui_out_end (struct ui_out
*uiout
,
330 enum ui_out_type type
)
332 int old_level
= pop_level (uiout
, type
);
334 uo_end (uiout
, type
, old_level
);
337 struct ui_out_end_cleanup_data
339 struct ui_out
*uiout
;
340 enum ui_out_type type
;
344 do_cleanup_end (void *data
)
346 struct ui_out_end_cleanup_data
*end_cleanup_data
347 = (struct ui_out_end_cleanup_data
*) data
;
349 ui_out_end (end_cleanup_data
->uiout
, end_cleanup_data
->type
);
350 xfree (end_cleanup_data
);
353 static struct cleanup
*
354 make_cleanup_ui_out_end (struct ui_out
*uiout
,
355 enum ui_out_type type
)
357 struct ui_out_end_cleanup_data
*end_cleanup_data
;
359 end_cleanup_data
= XNEW (struct ui_out_end_cleanup_data
);
360 end_cleanup_data
->uiout
= uiout
;
361 end_cleanup_data
->type
= type
;
362 return make_cleanup (do_cleanup_end
, end_cleanup_data
);
366 make_cleanup_ui_out_tuple_begin_end (struct ui_out
*uiout
,
369 ui_out_begin (uiout
, ui_out_type_tuple
, id
);
370 return make_cleanup_ui_out_end (uiout
, ui_out_type_tuple
);
374 make_cleanup_ui_out_list_begin_end (struct ui_out
*uiout
,
377 ui_out_begin (uiout
, ui_out_type_list
, id
);
378 return make_cleanup_ui_out_end (uiout
, ui_out_type_list
);
382 ui_out_field_int (struct ui_out
*uiout
,
390 verify_field (uiout
, &fldno
, &width
, &align
);
392 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
396 ui_out_field_fmt_int (struct ui_out
*uiout
,
398 enum ui_align input_align
,
406 verify_field (uiout
, &fldno
, &width
, &align
);
408 uo_field_int (uiout
, fldno
, input_width
, input_align
, fldname
, value
);
411 /* Documented in ui-out.h. */
414 ui_out_field_core_addr (struct ui_out
*uiout
,
416 struct gdbarch
*gdbarch
,
419 ui_out_field_string (uiout
, fldname
,
420 print_core_address (gdbarch
, address
));
424 ui_out_field_stream (struct ui_out
*uiout
,
426 struct ui_file
*stream
)
428 std::string buffer
= ui_file_as_string (stream
);
430 if (!buffer
.empty ())
431 ui_out_field_string (uiout
, fldname
, buffer
.c_str ());
433 ui_out_field_skip (uiout
, fldname
);
434 ui_file_rewind (stream
);
437 /* Used to omit a field. */
440 ui_out_field_skip (struct ui_out
*uiout
,
447 verify_field (uiout
, &fldno
, &width
, &align
);
449 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
453 ui_out_field_string (struct ui_out
*uiout
,
461 verify_field (uiout
, &fldno
, &width
, &align
);
463 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
468 ui_out_field_fmt (struct ui_out
*uiout
,
470 const char *format
, ...)
477 /* Will not align, but has to call anyway. */
478 verify_field (uiout
, &fldno
, &width
, &align
);
480 va_start (args
, format
);
482 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
488 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
490 uo_spaces (uiout
, numspaces
);
494 ui_out_text (struct ui_out
*uiout
,
497 uo_text (uiout
, string
);
501 ui_out_message (struct ui_out
*uiout
, int verbosity
,
502 const char *format
,...)
506 va_start (args
, format
);
507 uo_message (uiout
, verbosity
, format
, args
);
512 ui_out_wrap_hint (struct ui_out
*uiout
, char *identstring
)
514 uo_wrap_hint (uiout
, identstring
);
518 ui_out_flush (struct ui_out
*uiout
)
524 ui_out_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
526 return uo_redirect (uiout
, outstream
);
529 /* Set the flags specified by the mask given. */
531 ui_out_set_flags (struct ui_out
*uiout
, int mask
)
533 int oldflags
= uiout
->flags
;
535 uiout
->flags
|= mask
;
539 /* Clear the flags specified by the mask given. */
541 ui_out_clear_flags (struct ui_out
*uiout
, int mask
)
543 int oldflags
= uiout
->flags
;
545 uiout
->flags
&= ~mask
;
549 /* Test the flags against the mask given. */
551 ui_out_test_flags (struct ui_out
*uiout
, int mask
)
553 return (uiout
->flags
& mask
);
556 /* Obtain the current verbosity level (as stablished by the
557 'set verbositylevel' command. */
560 ui_out_get_verblvl (struct ui_out
*uiout
)
562 /* FIXME: not implemented yet. */
567 ui_out_is_mi_like_p (struct ui_out
*uiout
)
569 return uiout
->impl
->is_mi_like_p
;
572 /* Interface to the implementation functions. */
575 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
579 if (!uiout
->impl
->table_begin
)
581 uiout
->impl
->table_begin (uiout
, nbrofcols
, nr_rows
, tblid
);
585 uo_table_body (struct ui_out
*uiout
)
587 if (!uiout
->impl
->table_body
)
589 uiout
->impl
->table_body (uiout
);
593 uo_table_end (struct ui_out
*uiout
)
595 if (!uiout
->impl
->table_end
)
597 uiout
->impl
->table_end (uiout
);
601 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
,
602 const char *col_name
,
605 if (!uiout
->impl
->table_header
)
607 uiout
->impl
->table_header (uiout
, width
, align
, col_name
, colhdr
);
610 /* Clear the table associated with UIOUT. */
613 clear_table (struct ui_out
*uiout
)
615 xfree (uiout
->table
.id
);
616 uiout
->table
.id
= NULL
;
617 clear_header_list (uiout
);
621 uo_begin (struct ui_out
*uiout
,
622 enum ui_out_type type
,
626 if (uiout
->impl
->begin
== NULL
)
628 uiout
->impl
->begin (uiout
, type
, level
, id
);
632 uo_end (struct ui_out
*uiout
,
633 enum ui_out_type type
,
636 if (uiout
->impl
->end
== NULL
)
638 uiout
->impl
->end (uiout
, type
, level
);
642 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
646 if (!uiout
->impl
->field_int
)
648 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
652 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
655 if (!uiout
->impl
->field_skip
)
657 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
661 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
666 if (!uiout
->impl
->field_string
)
668 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
672 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
677 if (!uiout
->impl
->field_fmt
)
679 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
683 uo_spaces (struct ui_out
*uiout
, int numspaces
)
685 if (!uiout
->impl
->spaces
)
687 uiout
->impl
->spaces (uiout
, numspaces
);
691 uo_text (struct ui_out
*uiout
,
694 if (!uiout
->impl
->text
)
696 uiout
->impl
->text (uiout
, string
);
700 uo_message (struct ui_out
*uiout
, int verbosity
,
704 if (!uiout
->impl
->message
)
706 uiout
->impl
->message (uiout
, verbosity
, format
, args
);
710 uo_wrap_hint (struct ui_out
*uiout
, char *identstring
)
712 if (!uiout
->impl
->wrap_hint
)
714 uiout
->impl
->wrap_hint (uiout
, identstring
);
718 uo_flush (struct ui_out
*uiout
)
720 if (!uiout
->impl
->flush
)
722 uiout
->impl
->flush (uiout
);
726 uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
728 if (!uiout
->impl
->redirect
)
730 uiout
->impl
->redirect (uiout
, outstream
);
735 uo_data_destroy (struct ui_out
*uiout
)
737 if (!uiout
->impl
->data_destroy
)
740 uiout
->impl
->data_destroy (uiout
);
743 /* local functions */
745 /* List of column headers manipulation routines. */
748 clear_header_list (struct ui_out
*uiout
)
750 while (uiout
->table
.header_first
!= NULL
)
752 uiout
->table
.header_next
= uiout
->table
.header_first
;
753 uiout
->table
.header_first
= uiout
->table
.header_first
->next
;
754 xfree (uiout
->table
.header_next
->colhdr
);
755 xfree (uiout
->table
.header_next
->col_name
);
756 xfree (uiout
->table
.header_next
);
758 gdb_assert (uiout
->table
.header_first
== NULL
);
759 uiout
->table
.header_last
= NULL
;
760 uiout
->table
.header_next
= NULL
;
764 append_header_to_list (struct ui_out
*uiout
,
766 enum ui_align alignment
,
767 const char *col_name
,
770 struct ui_out_hdr
*temphdr
;
772 temphdr
= XNEW (struct ui_out_hdr
);
773 temphdr
->width
= width
;
774 temphdr
->alignment
= alignment
;
775 /* We have to copy the column title as the original may be an
778 temphdr
->colhdr
= xstrdup (colhdr
);
780 temphdr
->colhdr
= NULL
;
782 if (col_name
!= NULL
)
783 temphdr
->col_name
= xstrdup (col_name
);
784 else if (colhdr
!= NULL
)
785 temphdr
->col_name
= xstrdup (colhdr
);
787 temphdr
->col_name
= NULL
;
789 temphdr
->next
= NULL
;
790 if (uiout
->table
.header_first
== NULL
)
793 uiout
->table
.header_first
= temphdr
;
794 uiout
->table
.header_last
= temphdr
;
798 temphdr
->colno
= uiout
->table
.header_last
->colno
+ 1;
799 uiout
->table
.header_last
->next
= temphdr
;
800 uiout
->table
.header_last
= temphdr
;
802 uiout
->table
.header_next
= uiout
->table
.header_last
;
805 /* Extract the format information for the NEXT header and advance
806 the header pointer. Return 0 if there was no next header. */
809 get_next_header (struct ui_out
*uiout
,
812 enum ui_align
*alignment
,
815 /* There may be no headers at all or we may have used all columns. */
816 if (uiout
->table
.header_next
== NULL
)
818 *colno
= uiout
->table
.header_next
->colno
;
819 *width
= uiout
->table
.header_next
->width
;
820 *alignment
= uiout
->table
.header_next
->alignment
;
821 *colhdr
= uiout
->table
.header_next
->colhdr
;
822 /* Advance the header pointer to the next entry. */
823 uiout
->table
.header_next
= uiout
->table
.header_next
->next
;
828 /* Verify that the field/tuple/list is correctly positioned. Return
829 the field number and corresponding alignment (if
830 available/applicable). */
833 verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
834 enum ui_align
*align
)
836 struct ui_out_level
*current
= current_level (uiout
);
839 if (uiout
->table
.flag
)
841 if (!uiout
->table
.body_flag
)
842 internal_error (__FILE__
, __LINE__
,
843 _("table_body missing; table fields must be \
844 specified after table_body and inside a list."));
845 /* NOTE: cagney/2001-12-08: There was a check here to ensure
846 that this code was only executed when uiout->level was
847 greater than zero. That no longer applies - this code is run
848 before each table row tuple is started and at that point the
852 current
->field_count
+= 1;
854 if (uiout
->table
.body_flag
855 && uiout
->table
.entry_level
== uiout
->level
856 && get_next_header (uiout
, fldno
, width
, align
, &text
))
858 if (*fldno
!= current
->field_count
)
859 internal_error (__FILE__
, __LINE__
,
860 _("ui-out internal error in handling headers."));
866 *fldno
= current
->field_count
;
871 /* Access to ui-out members data. */
874 ui_out_data (struct ui_out
*uiout
)
879 /* Access table field parameters. */
881 ui_out_query_field (struct ui_out
*uiout
, int colno
,
882 int *width
, int *alignment
, char **col_name
)
884 struct ui_out_hdr
*hdr
;
886 if (!uiout
->table
.flag
)
889 for (hdr
= uiout
->table
.header_first
; hdr
; hdr
= hdr
->next
)
890 if (hdr
->colno
== colno
)
893 *alignment
= hdr
->alignment
;
894 *col_name
= hdr
->col_name
;
901 /* Initialize private members at startup. */
904 ui_out_new (const struct ui_out_impl
*impl
, void *data
,
907 struct ui_out
*uiout
= XNEW (struct ui_out
);
908 struct ui_out_level
*current
= XNEW (struct ui_out_level
);
912 uiout
->flags
= flags
;
913 uiout
->table
.flag
= 0;
914 uiout
->table
.body_flag
= 0;
916 uiout
->levels
= NULL
;
918 /* Create uiout->level 0, the default level. */
919 current
->type
= ui_out_type_tuple
;
920 current
->field_count
= 0;
921 VEC_safe_push (ui_out_level_p
, uiout
->levels
, current
);
923 uiout
->table
.id
= NULL
;
924 uiout
->table
.header_first
= NULL
;
925 uiout
->table
.header_last
= NULL
;
926 uiout
->table
.header_next
= NULL
;
930 /* Free UIOUT and the memory areas it references. */
933 ui_out_destroy (struct ui_out
*uiout
)
936 struct ui_out_level
*current
;
938 /* Make sure that all levels are freed in the case where levels have
939 been pushed, but not popped before the ui_out object is
942 VEC_iterate (ui_out_level_p
, uiout
->levels
, i
, current
);
946 VEC_free (ui_out_level_p
, uiout
->levels
);
947 uo_data_destroy (uiout
);
952 /* Standard gdb initialization hook. */
955 _initialize_ui_out (void)
957 /* nothing needs to be done */
This page took 0.049751 seconds and 4 git commands to generate.