1 /* Output generating routines for GDB.
2 Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
4 Written by Fernando Nasser for Cygnus.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
25 #include "expression.h" /* For language.h */
28 #include "gdb_assert.h"
30 /* Convenience macro for allocting typesafe memory. */
33 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
35 /* table header structures */
44 struct ui_out_hdr *next;
47 /* Maintain a stack so that the info applicable to the inner most list
48 is always available. Stack/nested level 0 is reserved for the
51 enum { MAX_UI_OUT_LEVELS = 5 };
55 /* Count each field; the first element is for non-list fields */
57 /* The type of this level. */
58 enum ui_out_type type;
61 /* Tables are special. Maintain a separate structure that tracks
62 their state. At present an output can only contain a single table
63 but that restriction might eventually be lifted. */
67 /* If on, a table is being generated. */
70 /* If on, the body of a table is being generated. If off, the table
71 header is being generated. */
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;
102 struct ui_out_data *data;
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;
126 /* We had better not overflow the buffer. */
128 gdb_assert (uiout->level >= 0 && uiout->level < MAX_UI_OUT_LEVELS);
129 current = current_level (uiout);
130 current->field_count = 0;
131 current->type = type;
135 /* Discard the current level, return the discarded level's index.
136 TYPE is the type of the level being discarded. */
138 pop_level (struct ui_out *uiout,
139 enum ui_out_type type)
141 /* We had better not underflow the buffer. */
142 gdb_assert (uiout->level > 0 && uiout->level < MAX_UI_OUT_LEVELS);
143 gdb_assert (current_level (uiout)->type == type);
145 return uiout->level + 1;
149 /* These are the default implementation functions */
151 static void default_table_begin (struct ui_out *uiout, int nbrofcols,
152 int nr_rows, const char *tblid);
153 static void default_table_body (struct ui_out *uiout);
154 static void default_table_end (struct ui_out *uiout);
155 static void default_table_header (struct ui_out *uiout, int width,
156 enum ui_align alig, const char *col_name,
158 static void default_begin (struct ui_out *uiout,
159 enum ui_out_type type,
160 int level, const char *id);
161 static void default_end (struct ui_out *uiout,
162 enum ui_out_type type,
164 static void default_field_int (struct ui_out *uiout, int fldno, int width,
168 static void default_field_skip (struct ui_out *uiout, int fldno, int width,
170 const char *fldname);
171 static void default_field_string (struct ui_out *uiout, int fldno, int width,
175 static void default_field_fmt (struct ui_out *uiout, int fldno,
176 int width, enum ui_align align,
180 static void default_spaces (struct ui_out *uiout, int numspaces);
181 static void default_text (struct ui_out *uiout, const char *string);
182 static void default_message (struct ui_out *uiout, int verbosity,
185 static void default_wrap_hint (struct ui_out *uiout, char *identstring);
186 static void default_flush (struct ui_out *uiout);
188 /* This is the default ui-out implementation functions vector */
190 struct ui_out_impl default_ui_out_impl =
195 default_table_header,
200 default_field_string,
207 0, /* Does not need MI hacks. */
210 /* The default ui_out */
212 struct ui_out def_uiout =
215 &default_ui_out_impl, /* impl */
218 /* Pointer to current ui_out */
219 /* FIXME: This should not be a global, but something passed down from main.c
222 struct ui_out *uiout = &def_uiout;
224 /* These are the interfaces to implementation functions */
226 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
227 int nr_rows, const char *tblid);
228 static void uo_table_body (struct ui_out *uiout);
229 static void uo_table_end (struct ui_out *uiout);
230 static void uo_table_header (struct ui_out *uiout, int width,
231 enum ui_align align, const char *col_name,
233 static void uo_begin (struct ui_out *uiout,
234 enum ui_out_type type,
235 int level, const char *id);
236 static void uo_end (struct ui_out *uiout,
237 enum ui_out_type type,
239 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
240 enum ui_align align, const char *fldname, int value);
241 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
242 enum ui_align align, const char *fldname);
243 static void uo_field_string (struct ui_out *uiout, int fldno, int width,
244 enum ui_align align, const char *fldname,
246 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
247 enum ui_align align, const char *fldname,
248 const char *format, va_list args);
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 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
254 static void uo_flush (struct ui_out *uiout);
256 /* Prototypes for local functions */
258 extern void _initialize_ui_out (void);
259 static void append_header_to_list (struct ui_out *uiout, int width,
260 int alignment, const char *col_name,
262 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
263 int *alignment, char **colhdr);
264 static void clear_header_list (struct ui_out *uiout);
265 static void verify_field_proper_position (struct ui_out *uiout);
266 static void verify_field_alignment (struct ui_out *uiout, int fldno, int *width, int *alignment);
268 static void init_ui_out_state (struct ui_out *uiout);
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.columns = nbrofcols;
288 uiout->table.id = xstrdup (tblid);
290 uiout->table.id = NULL;
291 clear_header_list (uiout);
293 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id);
297 ui_out_table_body (struct ui_out *uiout)
299 if (!uiout->table.flag)
300 internal_error (__FILE__, __LINE__,
301 "table_body outside a table is not valid; it must be \
302 after a table_begin and before a table_end.");
303 if (uiout->table.body_flag)
304 internal_error (__FILE__, __LINE__,
305 "extra table_body call not allowed; there must be \
306 only one table_body after a table_begin and before a table_end.");
307 if (uiout->table.header_next->colno != uiout->table.columns)
308 internal_error (__FILE__, __LINE__,
309 "number of headers differ from number of table \
312 uiout->table.body_flag = 1;
313 uiout->table.header_next = uiout->table.header_first;
315 uo_table_body (uiout);
319 ui_out_table_end (struct ui_out *uiout)
321 if (!uiout->table.flag)
322 internal_error (__FILE__, __LINE__,
323 "misplaced table_end or missing table_begin.");
325 uiout->table.body_flag = 0;
326 uiout->table.flag = 0;
328 uo_table_end (uiout);
331 xfree (uiout->table.id);
332 clear_header_list (uiout);
336 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
337 const char *col_name,
340 if (!uiout->table.flag || uiout->table.body_flag)
341 internal_error (__FILE__, __LINE__,
342 "table header must be specified after table_begin \
343 and before table_body.");
345 append_header_to_list (uiout, width, alignment, col_name, colhdr);
347 uo_table_header (uiout, width, alignment, col_name, colhdr);
351 ui_out_begin (struct ui_out *uiout,
352 enum ui_out_type type,
356 if (uiout->table.flag && !uiout->table.body_flag)
357 internal_error (__FILE__, __LINE__,
358 "table header or table_body expected; lists must be \
359 specified after table_body.");
360 new_level = push_level (uiout, type, id);
361 if (uiout->table.flag && (new_level == 1))
362 uiout->table.header_next = uiout->table.header_first;
363 uo_begin (uiout, type, new_level, id);
367 ui_out_list_begin (struct ui_out *uiout,
370 ui_out_begin (uiout, ui_out_type_list, id);
374 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
376 ui_out_begin (uiout, ui_out_type_tuple, id);
380 ui_out_end (struct ui_out *uiout,
381 enum ui_out_type type)
383 int old_level = pop_level (uiout, type);
384 uo_end (uiout, type, old_level);
388 ui_out_list_end (struct ui_out *uiout)
390 ui_out_end (uiout, ui_out_type_list);
394 ui_out_tuple_end (struct ui_out *uiout)
396 ui_out_end (uiout, ui_out_type_tuple);
399 struct ui_out_end_cleanup_data
401 struct ui_out *uiout;
402 enum ui_out_type type;
406 do_cleanup_end (void *data)
408 struct ui_out_end_cleanup_data *end_cleanup_data = data;
409 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
410 xfree (end_cleanup_data);
413 static struct cleanup *
414 make_cleanup_ui_out_end (struct ui_out *uiout,
415 enum ui_out_type type)
417 struct ui_out_end_cleanup_data *end_cleanup_data;
418 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
419 end_cleanup_data->uiout = uiout;
420 end_cleanup_data->type = type;
421 return make_cleanup (do_cleanup_end, end_cleanup_data);
425 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
426 enum ui_out_type type,
429 ui_out_begin (uiout, type, id);
430 return make_cleanup_ui_out_end (uiout, type);
434 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
437 ui_out_tuple_begin (uiout, id);
438 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
442 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
445 ui_out_list_begin (uiout, id);
446 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
450 ui_out_field_int (struct ui_out *uiout,
457 struct ui_out_level *current = current_level (uiout);
459 verify_field_proper_position (uiout);
461 current->field_count += 1;
462 fldno = current->field_count;
464 verify_field_alignment (uiout, fldno, &width, &align);
466 uo_field_int (uiout, fldno, width, align, fldname, value);
470 ui_out_field_core_addr (struct ui_out *uiout,
476 /* FIXME-32x64: need a print_address_numeric with field width */
477 /* print_address_numeric (address, 1, local_stream); */
478 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
480 ui_out_field_string (uiout, fldname, addstr);
484 ui_out_field_stream (struct ui_out *uiout,
486 struct ui_stream *buf)
489 char *buffer = ui_file_xstrdup (buf->stream, &length);
490 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
492 ui_out_field_string (uiout, fldname, buffer);
494 ui_out_field_skip (uiout, fldname);
495 ui_file_rewind (buf->stream);
496 do_cleanups (old_cleanup);
499 /* used to ommit a field */
502 ui_out_field_skip (struct ui_out *uiout,
508 struct ui_out_level *current = current_level (uiout);
510 verify_field_proper_position (uiout);
512 current->field_count += 1;
513 fldno = current->field_count;
515 verify_field_alignment (uiout, fldno, &width, &align);
517 uo_field_skip (uiout, fldno, width, align, fldname);
521 ui_out_field_string (struct ui_out *uiout,
528 struct ui_out_level *current = current_level (uiout);
530 verify_field_proper_position (uiout);
532 current->field_count += 1;
533 fldno = current->field_count;
535 verify_field_alignment (uiout, fldno, &width, &align);
537 uo_field_string (uiout, fldno, width, align, fldname, string);
542 ui_out_field_fmt (struct ui_out *uiout,
544 const char *format, ...)
550 struct ui_out_level *current = current_level (uiout);
552 verify_field_proper_position (uiout);
554 current->field_count += 1;
555 fldno = current->field_count;
557 /* will not align, but has to call anyway */
558 verify_field_alignment (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);
588 uo_message (uiout, verbosity, format, args);
594 ui_out_stream_new (struct ui_out *uiout)
596 struct ui_stream *tempbuf;
598 tempbuf = XMALLOC (struct ui_stream);
599 tempbuf->uiout = uiout;
600 tempbuf->stream = mem_fileopen ();
605 ui_out_stream_delete (struct ui_stream *buf)
607 ui_file_delete (buf->stream);
612 do_stream_delete (void *buf)
614 ui_out_stream_delete (buf);
618 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
620 return make_cleanup (do_stream_delete, buf);
625 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
627 uo_wrap_hint (uiout, identstring);
631 ui_out_flush (struct ui_out *uiout)
636 /* set the flags specified by the mask given */
638 ui_out_set_flags (struct ui_out *uiout, int mask)
640 int oldflags = uiout->flags;
642 uiout->flags |= mask;
647 /* clear the flags specified by the mask given */
649 ui_out_clear_flags (struct ui_out *uiout, int mask)
651 int oldflags = uiout->flags;
653 uiout->flags &= ~mask;
658 /* test the flags against the mask given */
660 ui_out_test_flags (struct ui_out *uiout, int mask)
662 return (uiout->flags & mask);
665 /* obtain the current verbosity level (as stablished by the
666 'set verbositylevel' command */
669 ui_out_get_verblvl (struct ui_out *uiout)
671 /* FIXME: not implemented yet */
677 ui_out_result_begin (struct ui_out *uiout, char *class)
682 ui_out_result_end (struct ui_out *uiout)
687 ui_out_info_begin (struct ui_out *uiout, char *class)
692 ui_out_info_end (struct ui_out *uiout)
697 ui_out_notify_begin (struct ui_out *uiout, char *class)
702 ui_out_notify_end (struct ui_out *uiout)
707 ui_out_error_begin (struct ui_out *uiout, char *class)
712 ui_out_error_end (struct ui_out *uiout)
719 gdb_error (ui_out * uiout, int severity, char *format,...)
725 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
731 ui_out_is_mi_like_p (struct ui_out *uiout)
733 return uiout->impl->is_mi_like_p;
736 /* default gdb-out hook functions */
739 default_table_begin (struct ui_out *uiout, int nbrofcols,
746 default_table_body (struct ui_out *uiout)
751 default_table_end (struct ui_out *uiout)
756 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
757 const char *col_name,
763 default_begin (struct ui_out *uiout,
764 enum ui_out_type type,
771 default_end (struct ui_out *uiout,
772 enum ui_out_type type,
778 default_field_int (struct ui_out *uiout, int fldno, int width,
780 const char *fldname, int value)
785 default_field_skip (struct ui_out *uiout, int fldno, int width,
786 enum ui_align align, const char *fldname)
791 default_field_string (struct ui_out *uiout,
801 default_field_fmt (struct ui_out *uiout, int fldno, int width,
810 default_spaces (struct ui_out *uiout, int numspaces)
815 default_text (struct ui_out *uiout, const char *string)
820 default_message (struct ui_out *uiout, int verbosity,
827 default_wrap_hint (struct ui_out *uiout, char *identstring)
832 default_flush (struct ui_out *uiout)
836 /* Interface to the implementation functions */
839 uo_table_begin (struct ui_out *uiout, int nbrofcols,
843 if (!uiout->impl->table_begin)
845 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
849 uo_table_body (struct ui_out *uiout)
851 if (!uiout->impl->table_body)
853 uiout->impl->table_body (uiout);
857 uo_table_end (struct ui_out *uiout)
859 if (!uiout->impl->table_end)
861 uiout->impl->table_end (uiout);
865 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
866 const char *col_name,
869 if (!uiout->impl->table_header)
871 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
875 uo_begin (struct ui_out *uiout,
876 enum ui_out_type type,
880 if (uiout->impl->begin == NULL)
882 uiout->impl->begin (uiout, type, level, id);
886 uo_end (struct ui_out *uiout,
887 enum ui_out_type type,
890 if (uiout->impl->end == NULL)
892 uiout->impl->end (uiout, type, level);
896 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
900 if (!uiout->impl->field_int)
902 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
906 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
909 if (!uiout->impl->field_skip)
911 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
915 uo_field_string (struct ui_out *uiout, int fldno, int width,
920 if (!uiout->impl->field_string)
922 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
926 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
931 if (!uiout->impl->field_fmt)
933 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
937 uo_spaces (struct ui_out *uiout, int numspaces)
939 if (!uiout->impl->spaces)
941 uiout->impl->spaces (uiout, numspaces);
945 uo_text (struct ui_out *uiout,
948 if (!uiout->impl->text)
950 uiout->impl->text (uiout, string);
954 uo_message (struct ui_out *uiout, int verbosity,
958 if (!uiout->impl->message)
960 uiout->impl->message (uiout, verbosity, format, args);
964 uo_wrap_hint (struct ui_out *uiout, char *identstring)
966 if (!uiout->impl->wrap_hint)
968 uiout->impl->wrap_hint (uiout, identstring);
972 uo_flush (struct ui_out *uiout)
974 if (!uiout->impl->flush)
976 uiout->impl->flush (uiout);
979 /* local functions */
981 /* list of column headers manipulation routines */
984 clear_header_list (struct ui_out *uiout)
986 while (uiout->table.header_first != NULL)
988 uiout->table.header_next = uiout->table.header_first;
989 uiout->table.header_first = uiout->table.header_first->next;
990 if (uiout->table.header_next->colhdr != NULL)
991 xfree (uiout->table.header_next->colhdr);
992 xfree (uiout->table.header_next);
994 gdb_assert (uiout->table.header_first == NULL);
995 uiout->table.header_last = NULL;
996 uiout->table.header_next = NULL;
1000 append_header_to_list (struct ui_out *uiout,
1003 const char *col_name,
1006 struct ui_out_hdr *temphdr;
1008 temphdr = XMALLOC (struct ui_out_hdr);
1009 temphdr->width = width;
1010 temphdr->alignment = alignment;
1011 /* we have to copy the column title as the original may be an automatic */
1013 temphdr->colhdr = xstrdup (colhdr);
1015 temphdr->colhdr = NULL;
1016 if (col_name != NULL)
1017 temphdr->col_name = xstrdup (colhdr);
1019 temphdr->col_name = xstrdup (colhdr);
1020 temphdr->next = NULL;
1021 if (uiout->table.header_first == NULL)
1024 uiout->table.header_first = temphdr;
1025 uiout->table.header_last = temphdr;
1029 temphdr->colno = uiout->table.header_last->colno + 1;
1030 uiout->table.header_last->next = temphdr;
1031 uiout->table.header_last = temphdr;
1033 uiout->table.header_next = uiout->table.header_last;
1036 /* Extract the format information for the NEXT header and and advance
1037 the header pointer. Return 0 if there was no next header. */
1040 get_next_header (struct ui_out *uiout,
1046 /* There may be no headers at all or we may have used all columns. */
1047 if (uiout->table.header_next == NULL)
1049 *colno = uiout->table.header_next->colno;
1050 *width = uiout->table.header_next->width;
1051 *alignment = uiout->table.header_next->alignment;
1052 *colhdr = uiout->table.header_next->colhdr;
1053 /* Advance the header pointer to the next entry. */
1054 uiout->table.header_next = uiout->table.header_next->next;
1058 /* makes sure the field_* calls were properly placed */
1061 verify_field_proper_position (struct ui_out *uiout)
1063 if (uiout->table.flag)
1065 if (!uiout->table.body_flag)
1066 internal_error (__FILE__, __LINE__,
1067 "table_body missing; table fields must be \
1068 specified after table_body and inside a list.");
1069 if (uiout->level == 0)
1070 internal_error (__FILE__, __LINE__,
1071 "list_begin missing; table fields must be \
1072 specified after table_body and inside a list.");
1076 /* determines what is the alignment policy */
1079 verify_field_alignment (struct ui_out *uiout,
1087 if (uiout->table.flag
1088 && get_next_header (uiout, &colno, width, align, &text))
1091 internal_error (__FILE__, __LINE__,
1092 "ui-out internal error in handling headers.");
1097 *align = ui_noalign;
1101 /* access to ui_out format private members */
1104 ui_out_get_field_separator (struct ui_out *uiout)
1108 /* Access to ui-out members data */
1110 struct ui_out_data *
1111 ui_out_data (struct ui_out *uiout)
1116 /* initalize private members at startup */
1119 ui_out_new (struct ui_out_impl *impl,
1120 struct ui_out_data *data,
1123 struct ui_out *uiout = XMALLOC (struct ui_out);
1126 uiout->flags = flags;
1127 uiout->table.flag = 0;
1128 uiout->table.body_flag = 0;
1130 memset (uiout->levels, 0, sizeof (uiout->levels));
1131 uiout->table.header_first = NULL;
1132 uiout->table.header_last = NULL;
1133 uiout->table.header_next = NULL;
1137 /* standard gdb initialization hook */
1140 _initialize_ui_out (void)
1142 /* nothing needs to be done */