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)
429 char *buffer = ui_file_xstrdup (stream, &length);
430 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
433 ui_out_field_string (uiout, fldname, buffer);
435 ui_out_field_skip (uiout, fldname);
436 ui_file_rewind (stream);
437 do_cleanups (old_cleanup);
440 /* Used to omit a field. */
443 ui_out_field_skip (struct ui_out *uiout,
450 verify_field (uiout, &fldno, &width, &align);
452 uo_field_skip (uiout, fldno, width, align, fldname);
456 ui_out_field_string (struct ui_out *uiout,
464 verify_field (uiout, &fldno, &width, &align);
466 uo_field_string (uiout, fldno, width, align, fldname, string);
471 ui_out_field_fmt (struct ui_out *uiout,
473 const char *format, ...)
480 /* Will not align, but has to call anyway. */
481 verify_field (uiout, &fldno, &width, &align);
483 va_start (args, format);
485 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
491 ui_out_spaces (struct ui_out *uiout, int numspaces)
493 uo_spaces (uiout, numspaces);
497 ui_out_text (struct ui_out *uiout,
500 uo_text (uiout, string);
504 ui_out_message (struct ui_out *uiout, int verbosity,
505 const char *format,...)
509 va_start (args, format);
510 uo_message (uiout, verbosity, format, args);
515 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
517 uo_wrap_hint (uiout, identstring);
521 ui_out_flush (struct ui_out *uiout)
527 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
529 return uo_redirect (uiout, outstream);
532 /* Set the flags specified by the mask given. */
534 ui_out_set_flags (struct ui_out *uiout, int mask)
536 int oldflags = uiout->flags;
538 uiout->flags |= mask;
542 /* Clear the flags specified by the mask given. */
544 ui_out_clear_flags (struct ui_out *uiout, int mask)
546 int oldflags = uiout->flags;
548 uiout->flags &= ~mask;
552 /* Test the flags against the mask given. */
554 ui_out_test_flags (struct ui_out *uiout, int mask)
556 return (uiout->flags & mask);
559 /* Obtain the current verbosity level (as stablished by the
560 'set verbositylevel' command. */
563 ui_out_get_verblvl (struct ui_out *uiout)
565 /* FIXME: not implemented yet. */
570 ui_out_is_mi_like_p (struct ui_out *uiout)
572 return uiout->impl->is_mi_like_p;
575 /* Interface to the implementation functions. */
578 uo_table_begin (struct ui_out *uiout, int nbrofcols,
582 if (!uiout->impl->table_begin)
584 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
588 uo_table_body (struct ui_out *uiout)
590 if (!uiout->impl->table_body)
592 uiout->impl->table_body (uiout);
596 uo_table_end (struct ui_out *uiout)
598 if (!uiout->impl->table_end)
600 uiout->impl->table_end (uiout);
604 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
605 const char *col_name,
608 if (!uiout->impl->table_header)
610 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
613 /* Clear the table associated with UIOUT. */
616 clear_table (struct ui_out *uiout)
618 xfree (uiout->table.id);
619 uiout->table.id = NULL;
620 clear_header_list (uiout);
624 uo_begin (struct ui_out *uiout,
625 enum ui_out_type type,
629 if (uiout->impl->begin == NULL)
631 uiout->impl->begin (uiout, type, level, id);
635 uo_end (struct ui_out *uiout,
636 enum ui_out_type type,
639 if (uiout->impl->end == NULL)
641 uiout->impl->end (uiout, type, level);
645 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
649 if (!uiout->impl->field_int)
651 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
655 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
658 if (!uiout->impl->field_skip)
660 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
664 uo_field_string (struct ui_out *uiout, int fldno, int width,
669 if (!uiout->impl->field_string)
671 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
675 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
680 if (!uiout->impl->field_fmt)
682 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
686 uo_spaces (struct ui_out *uiout, int numspaces)
688 if (!uiout->impl->spaces)
690 uiout->impl->spaces (uiout, numspaces);
694 uo_text (struct ui_out *uiout,
697 if (!uiout->impl->text)
699 uiout->impl->text (uiout, string);
703 uo_message (struct ui_out *uiout, int verbosity,
707 if (!uiout->impl->message)
709 uiout->impl->message (uiout, verbosity, format, args);
713 uo_wrap_hint (struct ui_out *uiout, char *identstring)
715 if (!uiout->impl->wrap_hint)
717 uiout->impl->wrap_hint (uiout, identstring);
721 uo_flush (struct ui_out *uiout)
723 if (!uiout->impl->flush)
725 uiout->impl->flush (uiout);
729 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
731 if (!uiout->impl->redirect)
733 uiout->impl->redirect (uiout, outstream);
738 uo_data_destroy (struct ui_out *uiout)
740 if (!uiout->impl->data_destroy)
743 uiout->impl->data_destroy (uiout);
746 /* local functions */
748 /* List of column headers manipulation routines. */
751 clear_header_list (struct ui_out *uiout)
753 while (uiout->table.header_first != NULL)
755 uiout->table.header_next = uiout->table.header_first;
756 uiout->table.header_first = uiout->table.header_first->next;
757 xfree (uiout->table.header_next->colhdr);
758 xfree (uiout->table.header_next->col_name);
759 xfree (uiout->table.header_next);
761 gdb_assert (uiout->table.header_first == NULL);
762 uiout->table.header_last = NULL;
763 uiout->table.header_next = NULL;
767 append_header_to_list (struct ui_out *uiout,
769 enum ui_align alignment,
770 const char *col_name,
773 struct ui_out_hdr *temphdr;
775 temphdr = XNEW (struct ui_out_hdr);
776 temphdr->width = width;
777 temphdr->alignment = alignment;
778 /* We have to copy the column title as the original may be an
781 temphdr->colhdr = xstrdup (colhdr);
783 temphdr->colhdr = NULL;
785 if (col_name != NULL)
786 temphdr->col_name = xstrdup (col_name);
787 else if (colhdr != NULL)
788 temphdr->col_name = xstrdup (colhdr);
790 temphdr->col_name = NULL;
792 temphdr->next = NULL;
793 if (uiout->table.header_first == NULL)
796 uiout->table.header_first = temphdr;
797 uiout->table.header_last = temphdr;
801 temphdr->colno = uiout->table.header_last->colno + 1;
802 uiout->table.header_last->next = temphdr;
803 uiout->table.header_last = temphdr;
805 uiout->table.header_next = uiout->table.header_last;
808 /* Extract the format information for the NEXT header and advance
809 the header pointer. Return 0 if there was no next header. */
812 get_next_header (struct ui_out *uiout,
815 enum ui_align *alignment,
818 /* There may be no headers at all or we may have used all columns. */
819 if (uiout->table.header_next == NULL)
821 *colno = uiout->table.header_next->colno;
822 *width = uiout->table.header_next->width;
823 *alignment = uiout->table.header_next->alignment;
824 *colhdr = uiout->table.header_next->colhdr;
825 /* Advance the header pointer to the next entry. */
826 uiout->table.header_next = uiout->table.header_next->next;
831 /* Verify that the field/tuple/list is correctly positioned. Return
832 the field number and corresponding alignment (if
833 available/applicable). */
836 verify_field (struct ui_out *uiout, int *fldno, int *width,
837 enum ui_align *align)
839 struct ui_out_level *current = current_level (uiout);
842 if (uiout->table.flag)
844 if (!uiout->table.body_flag)
845 internal_error (__FILE__, __LINE__,
846 _("table_body missing; table fields must be \
847 specified after table_body and inside a list."));
848 /* NOTE: cagney/2001-12-08: There was a check here to ensure
849 that this code was only executed when uiout->level was
850 greater than zero. That no longer applies - this code is run
851 before each table row tuple is started and at that point the
855 current->field_count += 1;
857 if (uiout->table.body_flag
858 && uiout->table.entry_level == uiout->level
859 && get_next_header (uiout, fldno, width, align, &text))
861 if (*fldno != current->field_count)
862 internal_error (__FILE__, __LINE__,
863 _("ui-out internal error in handling headers."));
869 *fldno = current->field_count;
874 /* Access to ui-out members data. */
877 ui_out_data (struct ui_out *uiout)
882 /* Access table field parameters. */
884 ui_out_query_field (struct ui_out *uiout, int colno,
885 int *width, int *alignment, char **col_name)
887 struct ui_out_hdr *hdr;
889 if (!uiout->table.flag)
892 for (hdr = uiout->table.header_first; hdr; hdr = hdr->next)
893 if (hdr->colno == colno)
896 *alignment = hdr->alignment;
897 *col_name = hdr->col_name;
904 /* Initalize private members at startup. */
907 ui_out_new (const struct ui_out_impl *impl, void *data,
910 struct ui_out *uiout = XNEW (struct ui_out);
911 struct ui_out_level *current = XNEW (struct ui_out_level);
915 uiout->flags = flags;
916 uiout->table.flag = 0;
917 uiout->table.body_flag = 0;
919 uiout->levels = NULL;
921 /* Create uiout->level 0, the default level. */
922 current->type = ui_out_type_tuple;
923 current->field_count = 0;
924 VEC_safe_push (ui_out_level_p, uiout->levels, current);
926 uiout->table.id = NULL;
927 uiout->table.header_first = NULL;
928 uiout->table.header_last = NULL;
929 uiout->table.header_next = NULL;
933 /* Free UIOUT and the memory areas it references. */
936 ui_out_destroy (struct ui_out *uiout)
939 struct ui_out_level *current;
941 /* Make sure that all levels are freed in the case where levels have
942 been pushed, but not popped before the ui_out object is
945 VEC_iterate (ui_out_level_p, uiout->levels, i, current);
949 VEC_free (ui_out_level_p, uiout->levels);
950 uo_data_destroy (uiout);
955 /* Cleanup that restores a previous current uiout. */
958 restore_current_uiout_cleanup (void *arg)
960 struct ui_out *saved_uiout = (struct ui_out *) arg;
962 current_uiout = saved_uiout;
968 make_cleanup_restore_current_uiout (void)
970 return make_cleanup (restore_current_uiout_cleanup, current_uiout);
973 /* Standard gdb initialization hook. */
976 _initialize_ui_out (void)
978 /* nothing needs to be done */