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,
123 struct ui_out_level *current;
126 current = XNEW (struct ui_out_level);
127 current->field_count = 0;
128 current->type = type;
129 VEC_safe_push (ui_out_level_p, uiout->levels, current);
133 /* Discard the current level, return the discarded level's index.
134 TYPE is the type of the level being discarded. */
136 pop_level (struct ui_out *uiout,
137 enum ui_out_type type)
139 struct ui_out_level *current;
141 /* We had better not underflow the buffer. */
142 gdb_assert (uiout->level > 0);
143 gdb_assert (current_level (uiout)->type == type);
144 current = VEC_pop (ui_out_level_p, uiout->levels);
147 return uiout->level + 1;
150 /* These are the interfaces to implementation functions. */
152 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
153 int nr_rows, const char *tblid);
154 static void uo_table_body (struct ui_out *uiout);
155 static void uo_table_end (struct ui_out *uiout);
156 static void uo_table_header (struct ui_out *uiout, int width,
157 enum ui_align align, const char *col_name,
159 static void uo_begin (struct ui_out *uiout,
160 enum ui_out_type type,
161 int level, const char *id);
162 static void uo_end (struct ui_out *uiout,
163 enum ui_out_type type,
165 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
166 enum ui_align align, const char *fldname, int value);
167 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
168 enum ui_align align, const char *fldname);
169 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
170 enum ui_align align, const char *fldname,
171 const char *format, va_list args)
172 ATTRIBUTE_PRINTF (6, 0);
173 static void uo_spaces (struct ui_out *uiout, int numspaces);
174 static void uo_text (struct ui_out *uiout, const char *string);
175 static void uo_message (struct ui_out *uiout, int verbosity,
176 const char *format, va_list args)
177 ATTRIBUTE_PRINTF (3, 0);
178 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
179 static void uo_flush (struct ui_out *uiout);
180 static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
181 static void uo_data_destroy (struct ui_out *uiout);
183 /* Prototypes for local functions */
185 extern void _initialize_ui_out (void);
186 static void append_header_to_list (struct ui_out *uiout, int width,
187 enum ui_align alignment, const char *col_name,
189 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
190 enum ui_align *alignment, char **colhdr);
191 static void clear_header_list (struct ui_out *uiout);
192 static void clear_table (struct ui_out *uiout);
193 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
194 enum ui_align *align);
196 /* exported functions (ui_out API) */
198 /* Mark beginning of a table. */
201 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
205 if (uiout->table.flag)
206 internal_error (__FILE__, __LINE__,
207 _("tables cannot be nested; table_begin found before \
208 previous table_end."));
210 uiout->table.flag = 1;
211 uiout->table.body_flag = 0;
212 uiout->table.entry_level = uiout->level + 1;
213 uiout->table.columns = nbrofcols;
215 uiout->table.id = xstrdup (tblid);
217 uiout->table.id = NULL;
218 clear_header_list (uiout);
220 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id);
224 ui_out_table_body (struct ui_out *uiout)
226 if (!uiout->table.flag)
227 internal_error (__FILE__, __LINE__,
228 _("table_body outside a table is not valid; it must be \
229 after a table_begin and before a table_end."));
230 if (uiout->table.body_flag)
231 internal_error (__FILE__, __LINE__,
232 _("extra table_body call not allowed; there must be \
233 only one table_body after a table_begin and before a table_end."));
234 if (uiout->table.header_next->colno != uiout->table.columns)
235 internal_error (__FILE__, __LINE__,
236 _("number of headers differ from number of table \
239 uiout->table.body_flag = 1;
240 uiout->table.header_next = uiout->table.header_first;
242 uo_table_body (uiout);
246 ui_out_table_end (struct ui_out *uiout)
248 if (!uiout->table.flag)
249 internal_error (__FILE__, __LINE__,
250 _("misplaced table_end or missing table_begin."));
252 uiout->table.entry_level = 0;
253 uiout->table.body_flag = 0;
254 uiout->table.flag = 0;
256 uo_table_end (uiout);
261 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
262 const char *col_name,
265 if (!uiout->table.flag || uiout->table.body_flag)
266 internal_error (__FILE__, __LINE__,
267 _("table header must be specified after table_begin \
268 and before table_body."));
270 append_header_to_list (uiout, width, alignment, col_name, colhdr);
272 uo_table_header (uiout, width, alignment, col_name, colhdr);
276 do_cleanup_table_end (void *data)
278 struct ui_out *ui_out = (struct ui_out *) data;
280 ui_out_table_end (ui_out);
284 make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
285 int nr_rows, const char *tblid)
287 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
288 return make_cleanup (do_cleanup_table_end, ui_out);
292 ui_out_begin (struct ui_out *uiout,
293 enum ui_out_type type,
298 if (uiout->table.flag && !uiout->table.body_flag)
299 internal_error (__FILE__, __LINE__,
300 _("table header or table_body expected; lists must be \
301 specified after table_body."));
303 /* Be careful to verify the ``field'' before the new tuple/list is
304 pushed onto the stack. That way the containing list/table/row is
305 verified and not the newly created tuple/list. This verification
306 is needed (at least) for the case where a table row entry
307 contains either a tuple/list. For that case bookkeeping such as
308 updating the column count or advancing to the next heading still
309 needs to be performed. */
315 verify_field (uiout, &fldno, &width, &align);
318 new_level = push_level (uiout, type, id);
320 /* If the push puts us at the same level as a table row entry, we've
321 got a new table row. Put the header pointer back to the start. */
322 if (uiout->table.body_flag
323 && uiout->table.entry_level == new_level)
324 uiout->table.header_next = uiout->table.header_first;
326 uo_begin (uiout, type, new_level, id);
330 ui_out_end (struct ui_out *uiout,
331 enum ui_out_type type)
333 int old_level = pop_level (uiout, type);
335 uo_end (uiout, type, old_level);
338 struct ui_out_end_cleanup_data
340 struct ui_out *uiout;
341 enum ui_out_type type;
345 do_cleanup_end (void *data)
347 struct ui_out_end_cleanup_data *end_cleanup_data
348 = (struct ui_out_end_cleanup_data *) data;
350 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
351 xfree (end_cleanup_data);
354 static struct cleanup *
355 make_cleanup_ui_out_end (struct ui_out *uiout,
356 enum ui_out_type type)
358 struct ui_out_end_cleanup_data *end_cleanup_data;
360 end_cleanup_data = XNEW (struct ui_out_end_cleanup_data);
361 end_cleanup_data->uiout = uiout;
362 end_cleanup_data->type = type;
363 return make_cleanup (do_cleanup_end, end_cleanup_data);
367 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
370 ui_out_begin (uiout, ui_out_type_tuple, id);
371 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
375 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
378 ui_out_begin (uiout, ui_out_type_list, id);
379 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
383 ui_out_field_int (struct ui_out *uiout,
391 verify_field (uiout, &fldno, &width, &align);
393 uo_field_int (uiout, fldno, width, align, fldname, value);
397 ui_out_field_fmt_int (struct ui_out *uiout,
399 enum ui_align input_align,
407 verify_field (uiout, &fldno, &width, &align);
409 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
412 /* Documented in ui-out.h. */
415 ui_out_field_core_addr (struct ui_out *uiout,
417 struct gdbarch *gdbarch,
420 ui_out_field_string (uiout, fldname,
421 print_core_address (gdbarch, address));
425 ui_out_field_stream (struct ui_out *uiout,
427 struct ui_file *stream)
430 char *buffer = ui_file_xstrdup (stream, &length);
431 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
434 ui_out_field_string (uiout, fldname, buffer);
436 ui_out_field_skip (uiout, fldname);
437 ui_file_rewind (stream);
438 do_cleanups (old_cleanup);
441 /* Used to omit a field. */
444 ui_out_field_skip (struct ui_out *uiout,
451 verify_field (uiout, &fldno, &width, &align);
453 uo_field_skip (uiout, fldno, width, align, fldname);
457 ui_out_field_string (struct ui_out *uiout,
465 verify_field (uiout, &fldno, &width, &align);
467 uo_field_string (uiout, fldno, width, align, fldname, string);
472 ui_out_field_fmt (struct ui_out *uiout,
474 const char *format, ...)
481 /* Will not align, but has to call anyway. */
482 verify_field (uiout, &fldno, &width, &align);
484 va_start (args, format);
486 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
492 ui_out_spaces (struct ui_out *uiout, int numspaces)
494 uo_spaces (uiout, numspaces);
498 ui_out_text (struct ui_out *uiout,
501 uo_text (uiout, string);
505 ui_out_message (struct ui_out *uiout, int verbosity,
506 const char *format,...)
510 va_start (args, format);
511 uo_message (uiout, verbosity, format, args);
516 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
518 uo_wrap_hint (uiout, identstring);
522 ui_out_flush (struct ui_out *uiout)
528 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
530 return uo_redirect (uiout, outstream);
533 /* Set the flags specified by the mask given. */
535 ui_out_set_flags (struct ui_out *uiout, int mask)
537 int oldflags = uiout->flags;
539 uiout->flags |= mask;
543 /* Clear the flags specified by the mask given. */
545 ui_out_clear_flags (struct ui_out *uiout, int mask)
547 int oldflags = uiout->flags;
549 uiout->flags &= ~mask;
553 /* Test the flags against the mask given. */
555 ui_out_test_flags (struct ui_out *uiout, int mask)
557 return (uiout->flags & mask);
560 /* Obtain the current verbosity level (as stablished by the
561 'set verbositylevel' command. */
564 ui_out_get_verblvl (struct ui_out *uiout)
566 /* FIXME: not implemented yet. */
571 ui_out_is_mi_like_p (struct ui_out *uiout)
573 return uiout->impl->is_mi_like_p;
576 /* Interface to the implementation functions. */
579 uo_table_begin (struct ui_out *uiout, int nbrofcols,
583 if (!uiout->impl->table_begin)
585 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
589 uo_table_body (struct ui_out *uiout)
591 if (!uiout->impl->table_body)
593 uiout->impl->table_body (uiout);
597 uo_table_end (struct ui_out *uiout)
599 if (!uiout->impl->table_end)
601 uiout->impl->table_end (uiout);
605 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
606 const char *col_name,
609 if (!uiout->impl->table_header)
611 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
614 /* Clear the table associated with UIOUT. */
617 clear_table (struct ui_out *uiout)
619 xfree (uiout->table.id);
620 uiout->table.id = NULL;
621 clear_header_list (uiout);
625 uo_begin (struct ui_out *uiout,
626 enum ui_out_type type,
630 if (uiout->impl->begin == NULL)
632 uiout->impl->begin (uiout, type, level, id);
636 uo_end (struct ui_out *uiout,
637 enum ui_out_type type,
640 if (uiout->impl->end == NULL)
642 uiout->impl->end (uiout, type, level);
646 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
650 if (!uiout->impl->field_int)
652 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
656 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
659 if (!uiout->impl->field_skip)
661 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
665 uo_field_string (struct ui_out *uiout, int fldno, int width,
670 if (!uiout->impl->field_string)
672 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
676 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
681 if (!uiout->impl->field_fmt)
683 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
687 uo_spaces (struct ui_out *uiout, int numspaces)
689 if (!uiout->impl->spaces)
691 uiout->impl->spaces (uiout, numspaces);
695 uo_text (struct ui_out *uiout,
698 if (!uiout->impl->text)
700 uiout->impl->text (uiout, string);
704 uo_message (struct ui_out *uiout, int verbosity,
708 if (!uiout->impl->message)
710 uiout->impl->message (uiout, verbosity, format, args);
714 uo_wrap_hint (struct ui_out *uiout, char *identstring)
716 if (!uiout->impl->wrap_hint)
718 uiout->impl->wrap_hint (uiout, identstring);
722 uo_flush (struct ui_out *uiout)
724 if (!uiout->impl->flush)
726 uiout->impl->flush (uiout);
730 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
732 if (!uiout->impl->redirect)
734 uiout->impl->redirect (uiout, outstream);
739 uo_data_destroy (struct ui_out *uiout)
741 if (!uiout->impl->data_destroy)
744 uiout->impl->data_destroy (uiout);
747 /* local functions */
749 /* List of column headers manipulation routines. */
752 clear_header_list (struct ui_out *uiout)
754 while (uiout->table.header_first != NULL)
756 uiout->table.header_next = uiout->table.header_first;
757 uiout->table.header_first = uiout->table.header_first->next;
758 xfree (uiout->table.header_next->colhdr);
759 xfree (uiout->table.header_next->col_name);
760 xfree (uiout->table.header_next);
762 gdb_assert (uiout->table.header_first == NULL);
763 uiout->table.header_last = NULL;
764 uiout->table.header_next = NULL;
768 append_header_to_list (struct ui_out *uiout,
770 enum ui_align alignment,
771 const char *col_name,
774 struct ui_out_hdr *temphdr;
776 temphdr = XNEW (struct ui_out_hdr);
777 temphdr->width = width;
778 temphdr->alignment = alignment;
779 /* We have to copy the column title as the original may be an
782 temphdr->colhdr = xstrdup (colhdr);
784 temphdr->colhdr = NULL;
786 if (col_name != NULL)
787 temphdr->col_name = xstrdup (col_name);
788 else if (colhdr != NULL)
789 temphdr->col_name = xstrdup (colhdr);
791 temphdr->col_name = NULL;
793 temphdr->next = NULL;
794 if (uiout->table.header_first == NULL)
797 uiout->table.header_first = temphdr;
798 uiout->table.header_last = temphdr;
802 temphdr->colno = uiout->table.header_last->colno + 1;
803 uiout->table.header_last->next = temphdr;
804 uiout->table.header_last = temphdr;
806 uiout->table.header_next = uiout->table.header_last;
809 /* Extract the format information for the NEXT header and advance
810 the header pointer. Return 0 if there was no next header. */
813 get_next_header (struct ui_out *uiout,
816 enum ui_align *alignment,
819 /* There may be no headers at all or we may have used all columns. */
820 if (uiout->table.header_next == NULL)
822 *colno = uiout->table.header_next->colno;
823 *width = uiout->table.header_next->width;
824 *alignment = uiout->table.header_next->alignment;
825 *colhdr = uiout->table.header_next->colhdr;
826 /* Advance the header pointer to the next entry. */
827 uiout->table.header_next = uiout->table.header_next->next;
832 /* Verify that the field/tuple/list is correctly positioned. Return
833 the field number and corresponding alignment (if
834 available/applicable). */
837 verify_field (struct ui_out *uiout, int *fldno, int *width,
838 enum ui_align *align)
840 struct ui_out_level *current = current_level (uiout);
843 if (uiout->table.flag)
845 if (!uiout->table.body_flag)
846 internal_error (__FILE__, __LINE__,
847 _("table_body missing; table fields must be \
848 specified after table_body and inside a list."));
849 /* NOTE: cagney/2001-12-08: There was a check here to ensure
850 that this code was only executed when uiout->level was
851 greater than zero. That no longer applies - this code is run
852 before each table row tuple is started and at that point the
856 current->field_count += 1;
858 if (uiout->table.body_flag
859 && uiout->table.entry_level == uiout->level
860 && get_next_header (uiout, fldno, width, align, &text))
862 if (*fldno != current->field_count)
863 internal_error (__FILE__, __LINE__,
864 _("ui-out internal error in handling headers."));
870 *fldno = current->field_count;
875 /* Access to ui-out members data. */
878 ui_out_data (struct ui_out *uiout)
883 /* Access table field parameters. */
885 ui_out_query_field (struct ui_out *uiout, int colno,
886 int *width, int *alignment, char **col_name)
888 struct ui_out_hdr *hdr;
890 if (!uiout->table.flag)
893 for (hdr = uiout->table.header_first; hdr; hdr = hdr->next)
894 if (hdr->colno == colno)
897 *alignment = hdr->alignment;
898 *col_name = hdr->col_name;
905 /* Initalize private members at startup. */
908 ui_out_new (const struct ui_out_impl *impl, void *data,
911 struct ui_out *uiout = XNEW (struct ui_out);
912 struct ui_out_level *current = XNEW (struct ui_out_level);
916 uiout->flags = flags;
917 uiout->table.flag = 0;
918 uiout->table.body_flag = 0;
920 uiout->levels = NULL;
922 /* Create uiout->level 0, the default level. */
923 current->type = ui_out_type_tuple;
924 current->field_count = 0;
925 VEC_safe_push (ui_out_level_p, uiout->levels, current);
927 uiout->table.id = NULL;
928 uiout->table.header_first = NULL;
929 uiout->table.header_last = NULL;
930 uiout->table.header_next = NULL;
934 /* Free UIOUT and the memory areas it references. */
937 ui_out_destroy (struct ui_out *uiout)
940 struct ui_out_level *current;
942 /* Make sure that all levels are freed in the case where levels have
943 been pushed, but not popped before the ui_out object is
946 VEC_iterate (ui_out_level_p, uiout->levels, i, current);
950 VEC_free (ui_out_level_p, uiout->levels);
951 uo_data_destroy (uiout);
956 /* Standard gdb initialization hook. */
959 _initialize_ui_out (void)
961 /* nothing needs to be done */