a40ac0a47f6395f2c1f142c57f9ddcc908c4c141
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
;
41 struct ui_out_hdr
*next
;
46 /* Count each field; the first element is for non-list fields. */
48 /* The type of this level. */
49 enum ui_out_type type
;
53 /* Tables are special. Maintain a separate structure that tracks
54 their state. At present an output can only contain a single table
55 but that restriction might eventually be lifted. */
59 /* If on, a table is being generated. */
62 /* If on, the body of a table is being generated. If off, the table
63 header is being generated. */
66 /* The level at which each entry of the table is to be found. A row
67 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
68 above that of the table. */
71 /* Number of table columns (as specified in the table_begin call). */
74 /* String identifying the table (as specified in the table_begin
78 /* Points to the first table header (if any). */
79 struct ui_out_hdr
*header_first
;
81 /* Points to the last table header (if any). */
82 struct ui_out_hdr
*header_last
;
84 /* Points to header of NEXT column to format. */
85 struct ui_out_hdr
*header_next
;
90 /* The ui_out structure */
95 /* Specific implementation of ui-out. */
96 const struct ui_out_impl
*impl
;
102 /* Vector to store and track the ui-out levels. */
103 std::vector
<std::unique_ptr
<ui_out_level
>> levels
;
105 /* A table, if any. At present only a single table is supported. */
106 struct ui_out_table table
;
109 /* The current (inner most) level. */
110 static struct ui_out_level
*
111 current_level (struct ui_out
*uiout
)
113 return uiout
->levels
[uiout
->level
].get ();
116 /* Create a new level, of TYPE. Return the new level's index. */
118 push_level (struct ui_out
*uiout
,
119 enum ui_out_type type
)
121 std::unique_ptr
<ui_out_level
> current (new ui_out_level ());
123 current
->field_count
= 0;
124 current
->type
= type
;
127 uiout
->levels
.push_back (std::move (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 /* We had better not underflow the buffer. */
139 gdb_assert (uiout
->level
> 0);
140 gdb_assert (current_level (uiout
)->type
== type
);
142 uiout
->levels
.pop_back ();
145 return uiout
->level
+ 1;
148 /* These are the interfaces to implementation functions. */
150 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
151 int nr_rows
, const char *tblid
);
152 static void uo_table_body (struct ui_out
*uiout
);
153 static void uo_table_end (struct ui_out
*uiout
);
154 static void uo_table_header (struct ui_out
*uiout
, int width
,
155 enum ui_align align
, const char *col_name
,
157 static void uo_begin (struct ui_out
*uiout
,
158 enum ui_out_type type
,
159 int level
, const char *id
);
160 static void uo_end (struct ui_out
*uiout
,
161 enum ui_out_type type
,
163 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
164 enum ui_align align
, const char *fldname
, int value
);
165 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
166 enum ui_align align
, const char *fldname
);
167 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
168 enum ui_align align
, const char *fldname
,
169 const char *format
, va_list args
)
170 ATTRIBUTE_PRINTF (6, 0);
171 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
172 static void uo_text (struct ui_out
*uiout
, const char *string
);
173 static void uo_message (struct ui_out
*uiout
,
174 const char *format
, va_list args
)
175 ATTRIBUTE_PRINTF (2, 0);
176 static void uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
);
177 static void uo_flush (struct ui_out
*uiout
);
178 static int uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
180 /* Prototypes for local functions */
182 static void append_header_to_list (struct ui_out
*uiout
, int width
,
183 enum ui_align alignment
, const char *col_name
,
185 static int get_next_header (struct ui_out
*uiout
, int *colno
, int *width
,
186 enum ui_align
*alignment
, char **colhdr
);
187 static void clear_header_list (struct ui_out
*uiout
);
188 static void clear_table (struct ui_out
*uiout
);
189 static void verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
190 enum ui_align
*align
);
192 /* exported functions (ui_out API) */
194 /* Mark beginning of a table. */
197 ui_out_table_begin (struct ui_out
*uiout
, int nbrofcols
,
198 int nr_rows
, const std::string
&tblid
)
200 if (uiout
->table
.flag
)
201 internal_error (__FILE__
, __LINE__
,
202 _("tables cannot be nested; table_begin found before \
203 previous table_end."));
205 uiout
->table
.flag
= 1;
206 uiout
->table
.body_flag
= 0;
207 uiout
->table
.entry_level
= uiout
->level
+ 1;
208 uiout
->table
.columns
= nbrofcols
;
209 uiout
->table
.id
= tblid
;
211 clear_header_list (uiout
);
213 uo_table_begin (uiout
, nbrofcols
, nr_rows
, uiout
->table
.id
.c_str ());
217 ui_out_table_body (struct ui_out
*uiout
)
219 if (!uiout
->table
.flag
)
220 internal_error (__FILE__
, __LINE__
,
221 _("table_body outside a table is not valid; it must be \
222 after a table_begin and before a table_end."));
223 if (uiout
->table
.body_flag
)
224 internal_error (__FILE__
, __LINE__
,
225 _("extra table_body call not allowed; there must be \
226 only one table_body after a table_begin and before a table_end."));
227 if (uiout
->table
.header_next
->colno
!= uiout
->table
.columns
)
228 internal_error (__FILE__
, __LINE__
,
229 _("number of headers differ from number of table \
232 uiout
->table
.body_flag
= 1;
233 uiout
->table
.header_next
= uiout
->table
.header_first
;
235 uo_table_body (uiout
);
239 ui_out_table_end (struct ui_out
*uiout
)
241 if (!uiout
->table
.flag
)
242 internal_error (__FILE__
, __LINE__
,
243 _("misplaced table_end or missing table_begin."));
245 uiout
->table
.entry_level
= 0;
246 uiout
->table
.body_flag
= 0;
247 uiout
->table
.flag
= 0;
249 uo_table_end (uiout
);
254 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
255 const char *col_name
,
258 if (!uiout
->table
.flag
|| uiout
->table
.body_flag
)
259 internal_error (__FILE__
, __LINE__
,
260 _("table header must be specified after table_begin \
261 and before table_body."));
263 append_header_to_list (uiout
, width
, alignment
, col_name
, colhdr
);
265 uo_table_header (uiout
, width
, alignment
, col_name
, colhdr
);
269 do_cleanup_table_end (void *data
)
271 struct ui_out
*ui_out
= (struct ui_out
*) data
;
273 ui_out_table_end (ui_out
);
277 make_cleanup_ui_out_table_begin_end (struct ui_out
*ui_out
, int nr_cols
,
278 int nr_rows
, const char *tblid
)
280 ui_out_table_begin (ui_out
, nr_cols
, nr_rows
, tblid
);
281 return make_cleanup (do_cleanup_table_end
, ui_out
);
285 ui_out_begin (struct ui_out
*uiout
,
286 enum ui_out_type type
,
291 if (uiout
->table
.flag
&& !uiout
->table
.body_flag
)
292 internal_error (__FILE__
, __LINE__
,
293 _("table header or table_body expected; lists must be \
294 specified after table_body."));
296 /* Be careful to verify the ``field'' before the new tuple/list is
297 pushed onto the stack. That way the containing list/table/row is
298 verified and not the newly created tuple/list. This verification
299 is needed (at least) for the case where a table row entry
300 contains either a tuple/list. For that case bookkeeping such as
301 updating the column count or advancing to the next heading still
302 needs to be performed. */
308 verify_field (uiout
, &fldno
, &width
, &align
);
311 new_level
= push_level (uiout
, type
);
313 /* If the push puts us at the same level as a table row entry, we've
314 got a new table row. Put the header pointer back to the start. */
315 if (uiout
->table
.body_flag
316 && uiout
->table
.entry_level
== new_level
)
317 uiout
->table
.header_next
= uiout
->table
.header_first
;
319 uo_begin (uiout
, type
, new_level
, id
);
323 ui_out_end (struct ui_out
*uiout
,
324 enum ui_out_type type
)
326 int old_level
= pop_level (uiout
, type
);
328 uo_end (uiout
, type
, old_level
);
331 struct ui_out_end_cleanup_data
333 struct ui_out
*uiout
;
334 enum ui_out_type type
;
338 do_cleanup_end (void *data
)
340 struct ui_out_end_cleanup_data
*end_cleanup_data
341 = (struct ui_out_end_cleanup_data
*) data
;
343 ui_out_end (end_cleanup_data
->uiout
, end_cleanup_data
->type
);
344 xfree (end_cleanup_data
);
347 static struct cleanup
*
348 make_cleanup_ui_out_end (struct ui_out
*uiout
,
349 enum ui_out_type type
)
351 struct ui_out_end_cleanup_data
*end_cleanup_data
;
353 end_cleanup_data
= XNEW (struct ui_out_end_cleanup_data
);
354 end_cleanup_data
->uiout
= uiout
;
355 end_cleanup_data
->type
= type
;
356 return make_cleanup (do_cleanup_end
, end_cleanup_data
);
360 make_cleanup_ui_out_tuple_begin_end (struct ui_out
*uiout
,
363 ui_out_begin (uiout
, ui_out_type_tuple
, id
);
364 return make_cleanup_ui_out_end (uiout
, ui_out_type_tuple
);
368 make_cleanup_ui_out_list_begin_end (struct ui_out
*uiout
,
371 ui_out_begin (uiout
, ui_out_type_list
, id
);
372 return make_cleanup_ui_out_end (uiout
, ui_out_type_list
);
376 ui_out_field_int (struct ui_out
*uiout
,
384 verify_field (uiout
, &fldno
, &width
, &align
);
386 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
390 ui_out_field_fmt_int (struct ui_out
*uiout
,
392 enum ui_align input_align
,
400 verify_field (uiout
, &fldno
, &width
, &align
);
402 uo_field_int (uiout
, fldno
, input_width
, input_align
, fldname
, value
);
405 /* Documented in ui-out.h. */
408 ui_out_field_core_addr (struct ui_out
*uiout
,
410 struct gdbarch
*gdbarch
,
413 ui_out_field_string (uiout
, fldname
,
414 print_core_address (gdbarch
, address
));
418 ui_out_field_stream (struct ui_out
*uiout
,
420 struct ui_file
*stream
)
422 std::string buffer
= ui_file_as_string (stream
);
424 if (!buffer
.empty ())
425 ui_out_field_string (uiout
, fldname
, buffer
.c_str ());
427 ui_out_field_skip (uiout
, fldname
);
428 ui_file_rewind (stream
);
431 /* Used to omit a field. */
434 ui_out_field_skip (struct ui_out
*uiout
,
441 verify_field (uiout
, &fldno
, &width
, &align
);
443 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
447 ui_out_field_string (struct ui_out
*uiout
,
455 verify_field (uiout
, &fldno
, &width
, &align
);
457 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
462 ui_out_field_fmt (struct ui_out
*uiout
,
464 const char *format
, ...)
471 /* Will not align, but has to call anyway. */
472 verify_field (uiout
, &fldno
, &width
, &align
);
474 va_start (args
, format
);
476 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
482 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
484 uo_spaces (uiout
, numspaces
);
488 ui_out_text (struct ui_out
*uiout
,
491 uo_text (uiout
, string
);
495 ui_out_message (struct ui_out
*uiout
, const char *format
, ...)
499 va_start (args
, format
);
500 uo_message (uiout
, format
, args
);
505 ui_out_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
507 uo_wrap_hint (uiout
, identstring
);
511 ui_out_flush (struct ui_out
*uiout
)
517 ui_out_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
519 return uo_redirect (uiout
, outstream
);
522 /* Test the flags against the mask given. */
524 ui_out_test_flags (struct ui_out
*uiout
, int mask
)
526 return (uiout
->flags
& mask
);
530 ui_out_is_mi_like_p (struct ui_out
*uiout
)
532 return uiout
->impl
->is_mi_like_p
;
535 /* Interface to the implementation functions. */
538 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
,
542 if (!uiout
->impl
->table_begin
)
544 uiout
->impl
->table_begin (uiout
, nbrofcols
, nr_rows
, tblid
);
548 uo_table_body (struct ui_out
*uiout
)
550 if (!uiout
->impl
->table_body
)
552 uiout
->impl
->table_body (uiout
);
556 uo_table_end (struct ui_out
*uiout
)
558 if (!uiout
->impl
->table_end
)
560 uiout
->impl
->table_end (uiout
);
564 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
,
565 const char *col_name
,
568 if (!uiout
->impl
->table_header
)
570 uiout
->impl
->table_header (uiout
, width
, align
, col_name
, colhdr
);
573 /* Clear the table associated with UIOUT. */
576 clear_table (struct ui_out
*uiout
)
578 uiout
->table
.id
.clear ();
579 clear_header_list (uiout
);
583 uo_begin (struct ui_out
*uiout
,
584 enum ui_out_type type
,
588 if (uiout
->impl
->begin
== NULL
)
590 uiout
->impl
->begin (uiout
, type
, level
, id
);
594 uo_end (struct ui_out
*uiout
,
595 enum ui_out_type type
,
598 if (uiout
->impl
->end
== NULL
)
600 uiout
->impl
->end (uiout
, type
, level
);
604 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
608 if (!uiout
->impl
->field_int
)
610 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
614 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
617 if (!uiout
->impl
->field_skip
)
619 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
623 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
628 if (!uiout
->impl
->field_string
)
630 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
634 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
,
639 if (!uiout
->impl
->field_fmt
)
641 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
645 uo_spaces (struct ui_out
*uiout
, int numspaces
)
647 if (!uiout
->impl
->spaces
)
649 uiout
->impl
->spaces (uiout
, numspaces
);
653 uo_text (struct ui_out
*uiout
,
656 if (!uiout
->impl
->text
)
658 uiout
->impl
->text (uiout
, string
);
662 uo_message (struct ui_out
*uiout
,
666 if (!uiout
->impl
->message
)
668 uiout
->impl
->message (uiout
, format
, args
);
672 uo_wrap_hint (struct ui_out
*uiout
, const char *identstring
)
674 if (!uiout
->impl
->wrap_hint
)
676 uiout
->impl
->wrap_hint (uiout
, identstring
);
680 uo_flush (struct ui_out
*uiout
)
682 if (!uiout
->impl
->flush
)
684 uiout
->impl
->flush (uiout
);
688 uo_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
690 if (!uiout
->impl
->redirect
)
692 return uiout
->impl
->redirect (uiout
, outstream
);
695 /* local functions */
697 /* List of column headers manipulation routines. */
700 clear_header_list (struct ui_out
*uiout
)
702 while (uiout
->table
.header_first
!= NULL
)
704 uiout
->table
.header_next
= uiout
->table
.header_first
;
705 uiout
->table
.header_first
= uiout
->table
.header_first
->next
;
706 xfree (uiout
->table
.header_next
->colhdr
);
707 xfree (uiout
->table
.header_next
->col_name
);
708 delete uiout
->table
.header_next
;
711 gdb_assert (uiout
->table
.header_first
== NULL
);
712 uiout
->table
.header_last
= NULL
;
713 uiout
->table
.header_next
= NULL
;
717 append_header_to_list (struct ui_out
*uiout
,
719 enum ui_align alignment
,
720 const char *col_name
,
723 struct ui_out_hdr
*temphdr
;
725 temphdr
= new ui_out_hdr ();
726 temphdr
->width
= width
;
727 temphdr
->alignment
= alignment
;
728 /* We have to copy the column title as the original may be an
731 temphdr
->colhdr
= xstrdup (colhdr
);
733 temphdr
->colhdr
= NULL
;
735 if (col_name
!= NULL
)
736 temphdr
->col_name
= xstrdup (col_name
);
737 else if (colhdr
!= NULL
)
738 temphdr
->col_name
= xstrdup (colhdr
);
740 temphdr
->col_name
= NULL
;
742 temphdr
->next
= NULL
;
743 if (uiout
->table
.header_first
== NULL
)
746 uiout
->table
.header_first
= temphdr
;
747 uiout
->table
.header_last
= temphdr
;
751 temphdr
->colno
= uiout
->table
.header_last
->colno
+ 1;
752 uiout
->table
.header_last
->next
= temphdr
;
753 uiout
->table
.header_last
= temphdr
;
755 uiout
->table
.header_next
= uiout
->table
.header_last
;
758 /* Extract the format information for the NEXT header and advance
759 the header pointer. Return 0 if there was no next header. */
762 get_next_header (struct ui_out
*uiout
,
765 enum ui_align
*alignment
,
768 /* There may be no headers at all or we may have used all columns. */
769 if (uiout
->table
.header_next
== NULL
)
771 *colno
= uiout
->table
.header_next
->colno
;
772 *width
= uiout
->table
.header_next
->width
;
773 *alignment
= uiout
->table
.header_next
->alignment
;
774 *colhdr
= uiout
->table
.header_next
->colhdr
;
775 /* Advance the header pointer to the next entry. */
776 uiout
->table
.header_next
= uiout
->table
.header_next
->next
;
781 /* Verify that the field/tuple/list is correctly positioned. Return
782 the field number and corresponding alignment (if
783 available/applicable). */
786 verify_field (struct ui_out
*uiout
, int *fldno
, int *width
,
787 enum ui_align
*align
)
789 struct ui_out_level
*current
= current_level (uiout
);
792 if (uiout
->table
.flag
)
794 if (!uiout
->table
.body_flag
)
795 internal_error (__FILE__
, __LINE__
,
796 _("table_body missing; table fields must be \
797 specified after table_body and inside a list."));
798 /* NOTE: cagney/2001-12-08: There was a check here to ensure
799 that this code was only executed when uiout->level was
800 greater than zero. That no longer applies - this code is run
801 before each table row tuple is started and at that point the
805 current
->field_count
+= 1;
807 if (uiout
->table
.body_flag
808 && uiout
->table
.entry_level
== uiout
->level
809 && get_next_header (uiout
, fldno
, width
, align
, &text
))
811 if (*fldno
!= current
->field_count
)
812 internal_error (__FILE__
, __LINE__
,
813 _("ui-out internal error in handling headers."));
819 *fldno
= current
->field_count
;
824 /* Access to ui-out members data. */
827 ui_out_data (struct ui_out
*uiout
)
832 /* Access table field parameters. */
834 ui_out_query_field (struct ui_out
*uiout
, int colno
,
835 int *width
, int *alignment
, char **col_name
)
837 struct ui_out_hdr
*hdr
;
839 if (!uiout
->table
.flag
)
842 for (hdr
= uiout
->table
.header_first
; hdr
; hdr
= hdr
->next
)
843 if (hdr
->colno
== colno
)
846 *alignment
= hdr
->alignment
;
847 *col_name
= hdr
->col_name
;
854 /* Initialize private members at startup. */
857 ui_out_new (const struct ui_out_impl
*impl
, void *data
,
860 struct ui_out
*uiout
= new ui_out ();
861 std::unique_ptr
<ui_out_level
> current (new ui_out_level ());
865 uiout
->flags
= flags
;
866 uiout
->table
.flag
= 0;
867 uiout
->table
.body_flag
= 0;
870 /* Create uiout->level 0, the default level. */
871 current
->type
= ui_out_type_tuple
;
872 current
->field_count
= 0;
873 uiout
->levels
.push_back (std::move (current
));
875 uiout
->table
.header_first
= NULL
;
876 uiout
->table
.header_last
= NULL
;
877 uiout
->table
.header_next
= NULL
;
This page took 0.048463 seconds and 4 git commands to generate.