1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2013 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 "gdb_string.h"
25 #include "expression.h" /* For language.h */
28 #include "gdb_assert.h"
30 /* table header structures */
39 struct ui_out_hdr *next;
42 /* Maintain a stack so that the info applicable to the inner most list
43 is always available. Stack/nested level 0 is reserved for the
46 enum { MAX_UI_OUT_LEVELS = 8 };
50 /* Count each field; the first element is for non-list fields. */
52 /* The type of this level. */
53 enum ui_out_type type;
56 /* Tables are special. Maintain a separate structure that tracks
57 their state. At present an output can only contain a single table
58 but that restriction might eventually be lifted. */
62 /* If on, a table is being generated. */
65 /* If on, the body of a table is being generated. If off, the table
66 header is being generated. */
69 /* The level at which each entry of the table is to be found. A row
70 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
71 above that of the table. */
74 /* Number of table columns (as specified in the table_begin call). */
77 /* String identifying the table (as specified in the table_begin
81 /* Points to the first table header (if any). */
82 struct ui_out_hdr *header_first;
84 /* Points to the last table header (if any). */
85 struct ui_out_hdr *header_last;
87 /* Points to header of NEXT column to format. */
88 struct ui_out_hdr *header_next;
93 /* The ui_out structure */
94 /* Any change here requires a corresponding one in the initialization
95 of the default uiout, which is statically initialized. */
100 /* Specific implementation of ui-out. */
101 struct ui_out_impl *impl;
104 /* Sub structure tracking the ui-out depth. */
106 struct ui_out_level levels[MAX_UI_OUT_LEVELS];
108 /* A table, if any. At present only a single table is supported. */
109 struct ui_out_table table;
112 /* The current (inner most) level. */
113 static struct ui_out_level *
114 current_level (struct ui_out *uiout)
116 return &uiout->levels[uiout->level];
119 /* Create a new level, of TYPE. Return the new level's index. */
121 push_level (struct ui_out *uiout,
122 enum ui_out_type type,
125 struct ui_out_level *current;
127 /* We had better not overflow the buffer. */
129 gdb_assert (uiout->level >= 0 && uiout->level < MAX_UI_OUT_LEVELS);
130 current = current_level (uiout);
131 current->field_count = 0;
132 current->type = type;
136 /* Discard the current level, return the discarded level's index.
137 TYPE is the type of the level being discarded. */
139 pop_level (struct ui_out *uiout,
140 enum ui_out_type type)
142 /* We had better not underflow the buffer. */
143 gdb_assert (uiout->level > 0 && uiout->level < MAX_UI_OUT_LEVELS);
144 gdb_assert (current_level (uiout)->type == type);
146 return uiout->level + 1;
150 /* These are the default implementation functions. */
152 static void default_table_begin (struct ui_out *uiout, int nbrofcols,
153 int nr_rows, const char *tblid);
154 static void default_table_body (struct ui_out *uiout);
155 static void default_table_end (struct ui_out *uiout);
156 static void default_table_header (struct ui_out *uiout, int width,
157 enum ui_align alig, const char *col_name,
159 static void default_begin (struct ui_out *uiout,
160 enum ui_out_type type,
161 int level, const char *id);
162 static void default_end (struct ui_out *uiout,
163 enum ui_out_type type,
165 static void default_field_int (struct ui_out *uiout, int fldno, int width,
169 static void default_field_skip (struct ui_out *uiout, int fldno, int width,
171 const char *fldname);
172 static void default_field_string (struct ui_out *uiout, int fldno, int width,
176 static void default_field_fmt (struct ui_out *uiout, int fldno,
177 int width, enum ui_align align,
180 va_list args) ATTRIBUTE_PRINTF (6, 0);
181 static void default_spaces (struct ui_out *uiout, int numspaces);
182 static void default_text (struct ui_out *uiout, const char *string);
183 static void default_message (struct ui_out *uiout, int verbosity,
185 va_list args) ATTRIBUTE_PRINTF (3, 0);
186 static void default_wrap_hint (struct ui_out *uiout, char *identstring);
187 static void default_flush (struct ui_out *uiout);
189 /* This is the default ui-out implementation functions vector. */
191 struct ui_out_impl default_ui_out_impl =
196 default_table_header,
201 default_field_string,
209 0, /* Does not need MI hacks. */
212 /* The default ui_out */
214 struct ui_out def_uiout =
217 &default_ui_out_impl, /* impl */
220 /* Pointer to current ui_out */
221 /* FIXME: This should not be a global, but something passed down from main.c
224 struct ui_out *current_uiout = &def_uiout;
226 /* These are the interfaces to implementation functions. */
228 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
229 int nr_rows, const char *tblid);
230 static void uo_table_body (struct ui_out *uiout);
231 static void uo_table_end (struct ui_out *uiout);
232 static void uo_table_header (struct ui_out *uiout, int width,
233 enum ui_align align, const char *col_name,
235 static void uo_begin (struct ui_out *uiout,
236 enum ui_out_type type,
237 int level, const char *id);
238 static void uo_end (struct ui_out *uiout,
239 enum ui_out_type type,
241 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
242 enum ui_align align, const char *fldname, int value);
243 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
244 enum ui_align align, const char *fldname);
245 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
246 enum ui_align align, const char *fldname,
247 const char *format, va_list args)
248 ATTRIBUTE_PRINTF (6, 0);
249 static void uo_spaces (struct ui_out *uiout, int numspaces);
250 static void uo_text (struct ui_out *uiout, const char *string);
251 static void uo_message (struct ui_out *uiout, int verbosity,
252 const char *format, va_list args)
253 ATTRIBUTE_PRINTF (3, 0);
254 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
255 static void uo_flush (struct ui_out *uiout);
256 static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
258 /* Prototypes for local functions */
260 extern void _initialize_ui_out (void);
261 static void append_header_to_list (struct ui_out *uiout, int width,
262 int alignment, const char *col_name,
264 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
265 int *alignment, char **colhdr);
266 static void clear_header_list (struct ui_out *uiout);
267 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
270 /* exported functions (ui_out API) */
272 /* Mark beginning of a table. */
275 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
279 if (uiout->table.flag)
280 internal_error (__FILE__, __LINE__,
281 _("tables cannot be nested; table_begin found before \
282 previous table_end."));
284 uiout->table.flag = 1;
285 uiout->table.body_flag = 0;
286 uiout->table.entry_level = uiout->level + 1;
287 uiout->table.columns = nbrofcols;
289 uiout->table.id = xstrdup (tblid);
291 uiout->table.id = NULL;
292 clear_header_list (uiout);
294 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id);
298 ui_out_table_body (struct ui_out *uiout)
300 if (!uiout->table.flag)
301 internal_error (__FILE__, __LINE__,
302 _("table_body outside a table is not valid; it must be \
303 after a table_begin and before a table_end."));
304 if (uiout->table.body_flag)
305 internal_error (__FILE__, __LINE__,
306 _("extra table_body call not allowed; there must be \
307 only one table_body after a table_begin and before a table_end."));
308 if (uiout->table.header_next->colno != uiout->table.columns)
309 internal_error (__FILE__, __LINE__,
310 _("number of headers differ from number of table \
313 uiout->table.body_flag = 1;
314 uiout->table.header_next = uiout->table.header_first;
316 uo_table_body (uiout);
320 ui_out_table_end (struct ui_out *uiout)
322 if (!uiout->table.flag)
323 internal_error (__FILE__, __LINE__,
324 _("misplaced table_end or missing table_begin."));
326 uiout->table.entry_level = 0;
327 uiout->table.body_flag = 0;
328 uiout->table.flag = 0;
330 uo_table_end (uiout);
333 xfree (uiout->table.id);
334 clear_header_list (uiout);
338 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
339 const char *col_name,
342 if (!uiout->table.flag || uiout->table.body_flag)
343 internal_error (__FILE__, __LINE__,
344 _("table header must be specified after table_begin \
345 and before table_body."));
347 append_header_to_list (uiout, width, alignment, col_name, colhdr);
349 uo_table_header (uiout, width, alignment, col_name, colhdr);
353 do_cleanup_table_end (void *data)
355 struct ui_out *ui_out = data;
357 ui_out_table_end (ui_out);
361 make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
362 int nr_rows, const char *tblid)
364 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
365 return make_cleanup (do_cleanup_table_end, ui_out);
369 ui_out_begin (struct ui_out *uiout,
370 enum ui_out_type type,
375 if (uiout->table.flag && !uiout->table.body_flag)
376 internal_error (__FILE__, __LINE__,
377 _("table header or table_body expected; lists must be \
378 specified after table_body."));
380 /* Be careful to verify the ``field'' before the new tuple/list is
381 pushed onto the stack. That way the containing list/table/row is
382 verified and not the newly created tuple/list. This verification
383 is needed (at least) for the case where a table row entry
384 contains either a tuple/list. For that case bookkeeping such as
385 updating the column count or advancing to the next heading still
386 needs to be performed. */
392 verify_field (uiout, &fldno, &width, &align);
395 new_level = push_level (uiout, type, id);
397 /* If the push puts us at the same level as a table row entry, we've
398 got a new table row. Put the header pointer back to the start. */
399 if (uiout->table.body_flag
400 && uiout->table.entry_level == new_level)
401 uiout->table.header_next = uiout->table.header_first;
403 uo_begin (uiout, type, new_level, id);
407 ui_out_end (struct ui_out *uiout,
408 enum ui_out_type type)
410 int old_level = pop_level (uiout, type);
412 uo_end (uiout, type, old_level);
415 struct ui_out_end_cleanup_data
417 struct ui_out *uiout;
418 enum ui_out_type type;
422 do_cleanup_end (void *data)
424 struct ui_out_end_cleanup_data *end_cleanup_data = data;
426 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
427 xfree (end_cleanup_data);
430 static struct cleanup *
431 make_cleanup_ui_out_end (struct ui_out *uiout,
432 enum ui_out_type type)
434 struct ui_out_end_cleanup_data *end_cleanup_data;
436 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
437 end_cleanup_data->uiout = uiout;
438 end_cleanup_data->type = type;
439 return make_cleanup (do_cleanup_end, end_cleanup_data);
443 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
446 ui_out_begin (uiout, ui_out_type_tuple, id);
447 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
451 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
454 ui_out_begin (uiout, ui_out_type_list, id);
455 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
459 ui_out_field_int (struct ui_out *uiout,
467 verify_field (uiout, &fldno, &width, &align);
469 uo_field_int (uiout, fldno, width, align, fldname, value);
473 ui_out_field_fmt_int (struct ui_out *uiout,
475 enum ui_align input_align,
483 verify_field (uiout, &fldno, &width, &align);
485 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
488 /* Documented in ui-out.h. */
491 ui_out_field_core_addr (struct ui_out *uiout,
493 struct gdbarch *gdbarch,
496 ui_out_field_string (uiout, fldname,
497 print_core_address (gdbarch, address));
501 ui_out_field_stream (struct ui_out *uiout,
503 struct ui_file *stream)
506 char *buffer = ui_file_xstrdup (stream, &length);
507 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
510 ui_out_field_string (uiout, fldname, buffer);
512 ui_out_field_skip (uiout, fldname);
513 ui_file_rewind (stream);
514 do_cleanups (old_cleanup);
517 /* Used to omit a field. */
520 ui_out_field_skip (struct ui_out *uiout,
527 verify_field (uiout, &fldno, &width, &align);
529 uo_field_skip (uiout, fldno, width, align, fldname);
533 ui_out_field_string (struct ui_out *uiout,
541 verify_field (uiout, &fldno, &width, &align);
543 uo_field_string (uiout, fldno, width, align, fldname, string);
548 ui_out_field_fmt (struct ui_out *uiout,
550 const char *format, ...)
557 /* Will not align, but has to call anyway. */
558 verify_field (uiout, &fldno, &width, &align);
560 va_start (args, format);
562 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
568 ui_out_spaces (struct ui_out *uiout, int numspaces)
570 uo_spaces (uiout, numspaces);
574 ui_out_text (struct ui_out *uiout,
577 uo_text (uiout, string);
581 ui_out_message (struct ui_out *uiout, int verbosity,
582 const char *format,...)
586 va_start (args, format);
587 uo_message (uiout, verbosity, format, args);
592 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
594 uo_wrap_hint (uiout, identstring);
598 ui_out_flush (struct ui_out *uiout)
604 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
606 return uo_redirect (uiout, outstream);
609 /* Set the flags specified by the mask given. */
611 ui_out_set_flags (struct ui_out *uiout, int mask)
613 int oldflags = uiout->flags;
615 uiout->flags |= mask;
619 /* Clear the flags specified by the mask given. */
621 ui_out_clear_flags (struct ui_out *uiout, int mask)
623 int oldflags = uiout->flags;
625 uiout->flags &= ~mask;
629 /* Test the flags against the mask given. */
631 ui_out_test_flags (struct ui_out *uiout, int mask)
633 return (uiout->flags & mask);
636 /* Obtain the current verbosity level (as stablished by the
637 'set verbositylevel' command. */
640 ui_out_get_verblvl (struct ui_out *uiout)
642 /* FIXME: not implemented yet. */
647 ui_out_is_mi_like_p (struct ui_out *uiout)
649 return uiout->impl->is_mi_like_p;
652 /* Default gdb-out hook functions. */
655 default_table_begin (struct ui_out *uiout, int nbrofcols,
662 default_table_body (struct ui_out *uiout)
667 default_table_end (struct ui_out *uiout)
672 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
673 const char *col_name,
679 default_begin (struct ui_out *uiout,
680 enum ui_out_type type,
687 default_end (struct ui_out *uiout,
688 enum ui_out_type type,
694 default_field_int (struct ui_out *uiout, int fldno, int width,
696 const char *fldname, int value)
701 default_field_skip (struct ui_out *uiout, int fldno, int width,
702 enum ui_align align, const char *fldname)
707 default_field_string (struct ui_out *uiout,
717 default_field_fmt (struct ui_out *uiout, int fldno, int width,
726 default_spaces (struct ui_out *uiout, int numspaces)
731 default_text (struct ui_out *uiout, const char *string)
736 default_message (struct ui_out *uiout, int verbosity,
743 default_wrap_hint (struct ui_out *uiout, char *identstring)
748 default_flush (struct ui_out *uiout)
752 /* Interface to the implementation functions. */
755 uo_table_begin (struct ui_out *uiout, int nbrofcols,
759 if (!uiout->impl->table_begin)
761 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
765 uo_table_body (struct ui_out *uiout)
767 if (!uiout->impl->table_body)
769 uiout->impl->table_body (uiout);
773 uo_table_end (struct ui_out *uiout)
775 if (!uiout->impl->table_end)
777 uiout->impl->table_end (uiout);
781 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
782 const char *col_name,
785 if (!uiout->impl->table_header)
787 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
791 uo_begin (struct ui_out *uiout,
792 enum ui_out_type type,
796 if (uiout->impl->begin == NULL)
798 uiout->impl->begin (uiout, type, level, id);
802 uo_end (struct ui_out *uiout,
803 enum ui_out_type type,
806 if (uiout->impl->end == NULL)
808 uiout->impl->end (uiout, type, level);
812 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
816 if (!uiout->impl->field_int)
818 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
822 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
825 if (!uiout->impl->field_skip)
827 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
831 uo_field_string (struct ui_out *uiout, int fldno, int width,
836 if (!uiout->impl->field_string)
838 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
842 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
847 if (!uiout->impl->field_fmt)
849 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
853 uo_spaces (struct ui_out *uiout, int numspaces)
855 if (!uiout->impl->spaces)
857 uiout->impl->spaces (uiout, numspaces);
861 uo_text (struct ui_out *uiout,
864 if (!uiout->impl->text)
866 uiout->impl->text (uiout, string);
870 uo_message (struct ui_out *uiout, int verbosity,
874 if (!uiout->impl->message)
876 uiout->impl->message (uiout, verbosity, format, args);
880 uo_wrap_hint (struct ui_out *uiout, char *identstring)
882 if (!uiout->impl->wrap_hint)
884 uiout->impl->wrap_hint (uiout, identstring);
888 uo_flush (struct ui_out *uiout)
890 if (!uiout->impl->flush)
892 uiout->impl->flush (uiout);
896 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
898 if (!uiout->impl->redirect)
900 uiout->impl->redirect (uiout, outstream);
904 /* local functions */
906 /* List of column headers manipulation routines. */
909 clear_header_list (struct ui_out *uiout)
911 while (uiout->table.header_first != NULL)
913 uiout->table.header_next = uiout->table.header_first;
914 uiout->table.header_first = uiout->table.header_first->next;
915 xfree (uiout->table.header_next->colhdr);
916 xfree (uiout->table.header_next->col_name);
917 xfree (uiout->table.header_next);
919 gdb_assert (uiout->table.header_first == NULL);
920 uiout->table.header_last = NULL;
921 uiout->table.header_next = NULL;
925 append_header_to_list (struct ui_out *uiout,
928 const char *col_name,
931 struct ui_out_hdr *temphdr;
933 temphdr = XMALLOC (struct ui_out_hdr);
934 temphdr->width = width;
935 temphdr->alignment = alignment;
936 /* We have to copy the column title as the original may be an
939 temphdr->colhdr = xstrdup (colhdr);
941 temphdr->colhdr = NULL;
943 if (col_name != NULL)
944 temphdr->col_name = xstrdup (col_name);
945 else if (colhdr != NULL)
946 temphdr->col_name = xstrdup (colhdr);
948 temphdr->col_name = NULL;
950 temphdr->next = NULL;
951 if (uiout->table.header_first == NULL)
954 uiout->table.header_first = temphdr;
955 uiout->table.header_last = temphdr;
959 temphdr->colno = uiout->table.header_last->colno + 1;
960 uiout->table.header_last->next = temphdr;
961 uiout->table.header_last = temphdr;
963 uiout->table.header_next = uiout->table.header_last;
966 /* Extract the format information for the NEXT header and advance
967 the header pointer. Return 0 if there was no next header. */
970 get_next_header (struct ui_out *uiout,
976 /* There may be no headers at all or we may have used all columns. */
977 if (uiout->table.header_next == NULL)
979 *colno = uiout->table.header_next->colno;
980 *width = uiout->table.header_next->width;
981 *alignment = uiout->table.header_next->alignment;
982 *colhdr = uiout->table.header_next->colhdr;
983 /* Advance the header pointer to the next entry. */
984 uiout->table.header_next = uiout->table.header_next->next;
989 /* Verify that the field/tuple/list is correctly positioned. Return
990 the field number and corresponding alignment (if
991 available/applicable). */
994 verify_field (struct ui_out *uiout, int *fldno, int *width, int *align)
996 struct ui_out_level *current = current_level (uiout);
999 if (uiout->table.flag)
1001 if (!uiout->table.body_flag)
1002 internal_error (__FILE__, __LINE__,
1003 _("table_body missing; table fields must be \
1004 specified after table_body and inside a list."));
1005 /* NOTE: cagney/2001-12-08: There was a check here to ensure
1006 that this code was only executed when uiout->level was
1007 greater than zero. That no longer applies - this code is run
1008 before each table row tuple is started and at that point the
1012 current->field_count += 1;
1014 if (uiout->table.body_flag
1015 && uiout->table.entry_level == uiout->level
1016 && get_next_header (uiout, fldno, width, align, &text))
1018 if (*fldno != current->field_count)
1019 internal_error (__FILE__, __LINE__,
1020 _("ui-out internal error in handling headers."));
1025 *align = ui_noalign;
1026 *fldno = current->field_count;
1031 /* Access to ui-out members data. */
1034 ui_out_data (struct ui_out *uiout)
1039 /* Access table field parameters. */
1041 ui_out_query_field (struct ui_out *uiout, int colno,
1042 int *width, int *alignment, char **col_name)
1044 struct ui_out_hdr *hdr;
1046 if (!uiout->table.flag)
1049 for (hdr = uiout->table.header_first; hdr; hdr = hdr->next)
1050 if (hdr->colno == colno)
1052 *width = hdr->width;
1053 *alignment = hdr->alignment;
1054 *col_name = hdr->col_name;
1061 /* Initalize private members at startup. */
1064 ui_out_new (struct ui_out_impl *impl, void *data,
1067 struct ui_out *uiout = XMALLOC (struct ui_out);
1071 uiout->flags = flags;
1072 uiout->table.flag = 0;
1073 uiout->table.body_flag = 0;
1075 memset (uiout->levels, 0, sizeof (uiout->levels));
1076 uiout->table.header_first = NULL;
1077 uiout->table.header_last = NULL;
1078 uiout->table.header_next = NULL;
1082 /* Standard gdb initialization hook. */
1085 _initialize_ui_out (void)
1087 /* nothing needs to be done */