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 /* The ui_out structure */
62 /* Any change here requires a corresponding one in the initialization
63 of the default uiout, which is statically initialized */
68 /* specific implementation of ui-out */
69 struct ui_out_impl *impl;
70 struct ui_out_data *data;
72 /* if on, a table is being generated */
75 /* if on, the body of a table is being generated */
78 /* number of table columns (as specified in the table_begin call) */
81 /* strinf identifying the table (as specified in the table_begin call) */
84 /* Sub structure tracking the table depth. */
86 struct ui_out_level levels[MAX_UI_OUT_LEVELS];
88 /* points to the first header (if any) */
89 struct ui_out_hdr *headerfirst;
91 /* points to the last header (if any) */
92 struct ui_out_hdr *headerlast;
94 /* points to header of next column to format */
95 struct ui_out_hdr *headercurr;
99 /* The current (inner most) level. */
100 static struct ui_out_level *
101 current_level (struct ui_out *uiout)
103 return &uiout->levels[uiout->level];
106 /* Create a new level, of TYPE. Return the new level's index. */
108 push_level (struct ui_out *uiout,
109 enum ui_out_type type,
112 struct ui_out_level *current;
113 /* We had better not overflow the buffer. */
115 gdb_assert (uiout->level >= 0 && uiout->level < MAX_UI_OUT_LEVELS);
116 current = current_level (uiout);
117 current->field_count = 0;
118 current->type = type;
122 /* Discard the current level, return the discarded level's index.
123 TYPE is the type of the level being discarded. */
125 pop_level (struct ui_out *uiout,
126 enum ui_out_type type)
128 /* We had better not underflow the buffer. */
129 gdb_assert (uiout->level > 0 && uiout->level < MAX_UI_OUT_LEVELS);
130 gdb_assert (current_level (uiout)->type == type);
132 return uiout->level + 1;
136 /* These are the default implementation functions */
138 static void default_table_begin (struct ui_out *uiout, int nbrofcols,
139 int nr_rows, const char *tblid);
140 static void default_table_body (struct ui_out *uiout);
141 static void default_table_end (struct ui_out *uiout);
142 static void default_table_header (struct ui_out *uiout, int width,
143 enum ui_align alig, const char *col_name,
145 static void default_begin (struct ui_out *uiout,
146 enum ui_out_type type,
147 int level, const char *id);
148 static void default_end (struct ui_out *uiout,
149 enum ui_out_type type,
151 static void default_field_int (struct ui_out *uiout, int fldno, int width,
155 static void default_field_skip (struct ui_out *uiout, int fldno, int width,
157 const char *fldname);
158 static void default_field_string (struct ui_out *uiout, int fldno, int width,
162 static void default_field_fmt (struct ui_out *uiout, int fldno,
163 int width, enum ui_align align,
167 static void default_spaces (struct ui_out *uiout, int numspaces);
168 static void default_text (struct ui_out *uiout, const char *string);
169 static void default_message (struct ui_out *uiout, int verbosity,
172 static void default_wrap_hint (struct ui_out *uiout, char *identstring);
173 static void default_flush (struct ui_out *uiout);
175 /* This is the default ui-out implementation functions vector */
177 struct ui_out_impl default_ui_out_impl =
182 default_table_header,
187 default_field_string,
196 /* The default ui_out */
198 struct ui_out def_uiout =
201 &default_ui_out_impl, /* impl */
204 /* Pointer to current ui_out */
205 /* FIXME: This should not be a global, but something passed down from main.c
208 struct ui_out *uiout = &def_uiout;
210 /* These are the interfaces to implementation functions */
212 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
213 int nr_rows, const char *tblid);
214 static void uo_table_body (struct ui_out *uiout);
215 static void uo_table_end (struct ui_out *uiout);
216 static void uo_table_header (struct ui_out *uiout, int width,
217 enum ui_align align, const char *col_name,
219 static void uo_begin (struct ui_out *uiout,
220 enum ui_out_type type,
221 int level, const char *id);
222 static void uo_end (struct ui_out *uiout,
223 enum ui_out_type type,
225 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
226 enum ui_align align, const char *fldname, int value);
227 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
228 enum ui_align align, const char *fldname);
229 static void uo_field_string (struct ui_out *uiout, int fldno, int width,
230 enum ui_align align, const char *fldname,
232 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
233 enum ui_align align, const char *fldname,
234 const char *format, va_list args);
235 static void uo_spaces (struct ui_out *uiout, int numspaces);
236 static void uo_text (struct ui_out *uiout, const char *string);
237 static void uo_message (struct ui_out *uiout, int verbosity,
238 const char *format, va_list args);
239 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
240 static void uo_flush (struct ui_out *uiout);
242 /* Prototypes for local functions */
244 extern void _initialize_ui_out (void);
245 static void append_header_to_list (struct ui_out *uiout, int width,
246 int alignment, const char *col_name,
248 static int get_curr_header (struct ui_out *uiout, int *colno, int *width,
249 int *alignment, char **colhdr);
250 static void clear_header_list (struct ui_out *uiout);
251 static void verify_field_proper_position (struct ui_out *uiout);
252 static void verify_field_alignment (struct ui_out *uiout, int fldno, int *width, int *alignment);
254 static void init_ui_out_state (struct ui_out *uiout);
256 /* exported functions (ui_out API) */
258 /* Mark beginning of a table */
261 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
265 if (uiout->table_flag)
266 internal_error (__FILE__, __LINE__,
267 "tables cannot be nested; table_begin found before \
268 previous table_end.");
270 uiout->table_flag = 1;
271 uiout->table_columns = nbrofcols;
273 uiout->table_id = xstrdup (tblid);
275 uiout->table_id = NULL;
276 clear_header_list (uiout);
278 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table_id);
282 ui_out_table_body (struct ui_out *uiout)
284 if (!uiout->table_flag)
285 internal_error (__FILE__, __LINE__,
286 "table_body outside a table is not valid; it must be \
287 after a table_begin and before a table_end.");
288 if (uiout->body_flag)
289 internal_error (__FILE__, __LINE__,
290 "extra table_body call not allowed; there must be \
291 only one table_body after a table_begin and before a table_end.");
292 if (uiout->headercurr->colno != uiout->table_columns)
293 internal_error (__FILE__, __LINE__,
294 "number of headers differ from number of table \
297 uiout->body_flag = 1;
298 uiout->headercurr = uiout->headerfirst;
300 uo_table_body (uiout);
304 ui_out_table_end (struct ui_out *uiout)
306 if (!uiout->table_flag)
307 internal_error (__FILE__, __LINE__,
308 "misplaced table_end or missing table_begin.");
310 uiout->body_flag = 0;
311 uiout->table_flag = 0;
313 uo_table_end (uiout);
316 xfree (uiout->table_id);
317 clear_header_list (uiout);
321 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
322 const char *col_name,
325 if (!uiout->table_flag || uiout->body_flag)
326 internal_error (__FILE__, __LINE__,
327 "table header must be specified after table_begin \
328 and before table_body.");
330 append_header_to_list (uiout, width, alignment, col_name, colhdr);
332 uo_table_header (uiout, width, alignment, col_name, colhdr);
336 ui_out_begin (struct ui_out *uiout,
337 enum ui_out_type type,
341 if (uiout->table_flag && !uiout->body_flag)
342 internal_error (__FILE__, __LINE__,
343 "table header or table_body expected; lists must be \
344 specified after table_body.");
345 new_level = push_level (uiout, type, id);
346 if (uiout->table_flag && (new_level == 1))
347 uiout->headercurr = uiout->headerfirst;
348 uo_begin (uiout, type, new_level, id);
352 ui_out_list_begin (struct ui_out *uiout,
355 ui_out_begin (uiout, ui_out_type_list, id);
359 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
361 ui_out_begin (uiout, ui_out_type_tuple, id);
365 ui_out_end (struct ui_out *uiout,
366 enum ui_out_type type)
368 int old_level = pop_level (uiout, type);
369 uo_end (uiout, type, old_level);
373 ui_out_list_end (struct ui_out *uiout)
375 ui_out_end (uiout, ui_out_type_list);
379 ui_out_tuple_end (struct ui_out *uiout)
381 ui_out_end (uiout, ui_out_type_tuple);
384 struct ui_out_end_cleanup_data
386 struct ui_out *uiout;
387 enum ui_out_type type;
391 do_cleanup_end (void *data)
393 struct ui_out_end_cleanup_data *end_cleanup_data = data;
394 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
395 xfree (end_cleanup_data);
398 static struct cleanup *
399 make_cleanup_ui_out_end (struct ui_out *uiout,
400 enum ui_out_type type)
402 struct ui_out_end_cleanup_data *end_cleanup_data;
403 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
404 end_cleanup_data->uiout = uiout;
405 end_cleanup_data->type = type;
406 return make_cleanup (do_cleanup_end, end_cleanup_data);
410 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
411 enum ui_out_type type,
414 ui_out_begin (uiout, type, id);
415 return make_cleanup_ui_out_end (uiout, type);
419 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
422 ui_out_tuple_begin (uiout, id);
423 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
427 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
430 ui_out_list_begin (uiout, id);
431 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
435 ui_out_field_int (struct ui_out *uiout,
442 struct ui_out_level *current = current_level (uiout);
444 verify_field_proper_position (uiout);
446 current->field_count += 1;
447 fldno = current->field_count;
449 verify_field_alignment (uiout, fldno, &width, &align);
451 uo_field_int (uiout, fldno, width, align, fldname, value);
455 ui_out_field_core_addr (struct ui_out *uiout,
461 /* FIXME-32x64: need a print_address_numeric with field width */
462 /* print_address_numeric (address, 1, local_stream); */
463 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
465 ui_out_field_string (uiout, fldname, addstr);
469 ui_out_field_stream (struct ui_out *uiout,
471 struct ui_stream *buf)
474 char *buffer = ui_file_xstrdup (buf->stream, &length);
475 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
477 ui_out_field_string (uiout, fldname, buffer);
479 ui_out_field_skip (uiout, fldname);
480 ui_file_rewind (buf->stream);
481 do_cleanups (old_cleanup);
484 /* used to ommit a field */
487 ui_out_field_skip (struct ui_out *uiout,
493 struct ui_out_level *current = current_level (uiout);
495 verify_field_proper_position (uiout);
497 current->field_count += 1;
498 fldno = current->field_count;
500 verify_field_alignment (uiout, fldno, &width, &align);
502 uo_field_skip (uiout, fldno, width, align, fldname);
506 ui_out_field_string (struct ui_out *uiout,
513 struct ui_out_level *current = current_level (uiout);
515 verify_field_proper_position (uiout);
517 current->field_count += 1;
518 fldno = current->field_count;
520 verify_field_alignment (uiout, fldno, &width, &align);
522 uo_field_string (uiout, fldno, width, align, fldname, string);
527 ui_out_field_fmt (struct ui_out *uiout,
529 const char *format, ...)
535 struct ui_out_level *current = current_level (uiout);
537 verify_field_proper_position (uiout);
539 current->field_count += 1;
540 fldno = current->field_count;
542 /* will not align, but has to call anyway */
543 verify_field_alignment (uiout, fldno, &width, &align);
545 va_start (args, format);
547 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
553 ui_out_spaces (struct ui_out *uiout, int numspaces)
555 uo_spaces (uiout, numspaces);
559 ui_out_text (struct ui_out *uiout,
562 uo_text (uiout, string);
566 ui_out_message (struct ui_out *uiout, int verbosity,
567 const char *format,...)
571 va_start (args, format);
573 uo_message (uiout, verbosity, format, args);
579 ui_out_stream_new (struct ui_out *uiout)
581 struct ui_stream *tempbuf;
583 tempbuf = XMALLOC (struct ui_stream);
584 tempbuf->uiout = uiout;
585 tempbuf->stream = mem_fileopen ();
590 ui_out_stream_delete (struct ui_stream *buf)
592 ui_file_delete (buf->stream);
597 do_stream_delete (void *buf)
599 ui_out_stream_delete (buf);
603 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
605 return make_cleanup (do_stream_delete, buf);
610 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
612 uo_wrap_hint (uiout, identstring);
616 ui_out_flush (struct ui_out *uiout)
621 /* set the flags specified by the mask given */
623 ui_out_set_flags (struct ui_out *uiout, int mask)
625 int oldflags = uiout->flags;
627 uiout->flags |= mask;
632 /* clear the flags specified by the mask given */
634 ui_out_clear_flags (struct ui_out *uiout, int mask)
636 int oldflags = uiout->flags;
638 uiout->flags &= ~mask;
643 /* test the flags against the mask given */
645 ui_out_test_flags (struct ui_out *uiout, int mask)
647 return (uiout->flags & mask);
650 /* obtain the current verbosity level (as stablished by the
651 'set verbositylevel' command */
654 ui_out_get_verblvl (struct ui_out *uiout)
656 /* FIXME: not implemented yet */
662 ui_out_result_begin (struct ui_out *uiout, char *class)
667 ui_out_result_end (struct ui_out *uiout)
672 ui_out_info_begin (struct ui_out *uiout, char *class)
677 ui_out_info_end (struct ui_out *uiout)
682 ui_out_notify_begin (struct ui_out *uiout, char *class)
687 ui_out_notify_end (struct ui_out *uiout)
692 ui_out_error_begin (struct ui_out *uiout, char *class)
697 ui_out_error_end (struct ui_out *uiout)
704 gdb_error (ui_out * uiout, int severity, char *format,...)
710 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
715 /* default gdb-out hook functions */
718 default_table_begin (struct ui_out *uiout, int nbrofcols,
725 default_table_body (struct ui_out *uiout)
730 default_table_end (struct ui_out *uiout)
735 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
736 const char *col_name,
742 default_begin (struct ui_out *uiout,
743 enum ui_out_type type,
750 default_end (struct ui_out *uiout,
751 enum ui_out_type type,
757 default_field_int (struct ui_out *uiout, int fldno, int width,
759 const char *fldname, int value)
764 default_field_skip (struct ui_out *uiout, int fldno, int width,
765 enum ui_align align, const char *fldname)
770 default_field_string (struct ui_out *uiout,
780 default_field_fmt (struct ui_out *uiout, int fldno, int width,
789 default_spaces (struct ui_out *uiout, int numspaces)
794 default_text (struct ui_out *uiout, const char *string)
799 default_message (struct ui_out *uiout, int verbosity,
806 default_wrap_hint (struct ui_out *uiout, char *identstring)
811 default_flush (struct ui_out *uiout)
815 /* Interface to the implementation functions */
818 uo_table_begin (struct ui_out *uiout, int nbrofcols,
822 if (!uiout->impl->table_begin)
824 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
828 uo_table_body (struct ui_out *uiout)
830 if (!uiout->impl->table_body)
832 uiout->impl->table_body (uiout);
836 uo_table_end (struct ui_out *uiout)
838 if (!uiout->impl->table_end)
840 uiout->impl->table_end (uiout);
844 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
845 const char *col_name,
848 if (!uiout->impl->table_header)
850 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
854 uo_begin (struct ui_out *uiout,
855 enum ui_out_type type,
859 if (uiout->impl->begin == NULL)
861 uiout->impl->begin (uiout, type, level, id);
865 uo_end (struct ui_out *uiout,
866 enum ui_out_type type,
869 if (uiout->impl->end == NULL)
871 uiout->impl->end (uiout, type, level);
875 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
879 if (!uiout->impl->field_int)
881 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
885 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
888 if (!uiout->impl->field_skip)
890 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
894 uo_field_string (struct ui_out *uiout, int fldno, int width,
899 if (!uiout->impl->field_string)
901 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
905 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
910 if (!uiout->impl->field_fmt)
912 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
916 uo_spaces (struct ui_out *uiout, int numspaces)
918 if (!uiout->impl->spaces)
920 uiout->impl->spaces (uiout, numspaces);
924 uo_text (struct ui_out *uiout,
927 if (!uiout->impl->text)
929 uiout->impl->text (uiout, string);
933 uo_message (struct ui_out *uiout, int verbosity,
937 if (!uiout->impl->message)
939 uiout->impl->message (uiout, verbosity, format, args);
943 uo_wrap_hint (struct ui_out *uiout, char *identstring)
945 if (!uiout->impl->wrap_hint)
947 uiout->impl->wrap_hint (uiout, identstring);
951 uo_flush (struct ui_out *uiout)
953 if (!uiout->impl->flush)
955 uiout->impl->flush (uiout);
958 /* local functions */
960 /* list of column headers manipulation routines */
963 clear_header_list (struct ui_out *uiout)
965 while (uiout->headerfirst != NULL)
967 uiout->headercurr = uiout->headerfirst;
968 uiout->headerfirst = uiout->headerfirst->next;
969 if (uiout->headercurr->colhdr != NULL)
970 xfree (uiout->headercurr->colhdr);
971 xfree (uiout->headercurr);
973 uiout->headerlast = NULL;
974 uiout->headercurr = NULL;
978 append_header_to_list (struct ui_out *uiout,
981 const char *col_name,
984 struct ui_out_hdr *temphdr;
986 temphdr = XMALLOC (struct ui_out_hdr);
987 temphdr->width = width;
988 temphdr->alignment = alignment;
989 /* we have to copy the column title as the original may be an automatic */
991 temphdr->colhdr = xstrdup (colhdr);
993 temphdr->colhdr = NULL;
994 if (col_name != NULL)
995 temphdr->col_name = xstrdup (colhdr);
997 temphdr->col_name = xstrdup (colhdr);
998 temphdr->next = NULL;
999 if (uiout->headerfirst == NULL)
1002 uiout->headerfirst = temphdr;
1003 uiout->headerlast = temphdr;
1007 temphdr->colno = uiout->headerlast->colno + 1;
1008 uiout->headerlast->next = temphdr;
1009 uiout->headerlast = temphdr;
1011 uiout->headercurr = uiout->headerlast;
1014 /* returns 0 if there is no more headers */
1017 get_curr_header (struct ui_out *uiout,
1023 /* There may be no headers at all or we may have used all columns */
1024 if (uiout->headercurr == NULL)
1026 *colno = uiout->headercurr->colno;
1027 *width = uiout->headercurr->width;
1028 *alignment = uiout->headercurr->alignment;
1029 *colhdr = uiout->headercurr->colhdr;
1030 uiout->headercurr = uiout->headercurr->next;
1034 /* makes sure the field_* calls were properly placed */
1037 verify_field_proper_position (struct ui_out *uiout)
1039 if (uiout->table_flag)
1041 if (!uiout->body_flag)
1042 internal_error (__FILE__, __LINE__,
1043 "table_body missing; table fields must be \
1044 specified after table_body and inside a list.");
1045 if (uiout->level == 0)
1046 internal_error (__FILE__, __LINE__,
1047 "list_begin missing; table fields must be \
1048 specified after table_body and inside a list.");
1052 /* determines what is the alignment policy */
1055 verify_field_alignment (struct ui_out *uiout,
1063 if (uiout->table_flag
1064 && get_curr_header (uiout, &colno, width, align, &text))
1067 internal_error (__FILE__, __LINE__,
1068 "ui-out internal error in handling headers.");
1073 *align = ui_noalign;
1077 /* access to ui_out format private members */
1080 ui_out_get_field_separator (struct ui_out *uiout)
1084 /* Access to ui-out members data */
1086 struct ui_out_data *
1087 ui_out_data (struct ui_out *uiout)
1092 /* initalize private members at startup */
1095 ui_out_new (struct ui_out_impl *impl,
1096 struct ui_out_data *data,
1099 struct ui_out *uiout = XMALLOC (struct ui_out);
1102 uiout->flags = flags;
1103 uiout->table_flag = 0;
1104 uiout->body_flag = 0;
1106 memset (uiout->levels, 0, sizeof (uiout->levels));
1107 uiout->headerfirst = NULL;
1108 uiout->headerlast = NULL;
1109 uiout->headercurr = NULL;
1113 /* standard gdb initialization hook */
1116 _initialize_ui_out (void)
1118 /* nothing needs to be done */