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 */
43 struct ui_out_hdr *next;
46 /* Maintain a stack so that the info applicable to the inner most list
47 is always available. Stack/nested level 0 is reserved for the
50 enum { MAX_UI_OUT_LEVELS = 5 };
54 /* Count each field; the first element is for non-list fields */
56 /* The type of this level. */
57 enum ui_out_type type;
60 /* The ui_out structure */
61 /* Any change here requires a corresponding one in the initialization
62 of the default uiout, which is statically initialized */
67 /* specific implementation of ui-out */
68 struct ui_out_impl *impl;
69 struct ui_out_data *data;
71 /* if on, a table is being generated */
74 /* if on, the body of a table is being generated */
77 /* number of table columns (as specified in the table_begin call) */
80 /* strinf identifying the table (as specified in the table_begin call) */
83 /* Sub structure tracking the table depth. */
85 struct ui_out_level levels[MAX_UI_OUT_LEVELS];
87 /* points to the first header (if any) */
88 struct ui_out_hdr *headerfirst;
90 /* points to the last header (if any) */
91 struct ui_out_hdr *headerlast;
93 /* points to header of next column to format */
94 struct ui_out_hdr *headercurr;
98 /* The current (inner most) level. */
99 static struct ui_out_level *
100 current_level (struct ui_out *uiout)
102 return &uiout->levels[uiout->level];
105 /* Create a new level, of TYPE. Return the new level's index. */
107 push_level (struct ui_out *uiout,
108 enum ui_out_type type,
111 struct ui_out_level *current;
112 /* We had better not overflow the buffer. */
114 gdb_assert (uiout->level >= 0 && uiout->level < MAX_UI_OUT_LEVELS);
115 current = current_level (uiout);
116 current->field_count = 0;
117 current->type = type;
121 /* Discard the current level, return the discarded level's index.
122 TYPE is the type of the level being discarded. */
124 pop_level (struct ui_out *uiout,
125 enum ui_out_type type)
127 /* We had better not underflow the buffer. */
128 gdb_assert (uiout->level > 0 && uiout->level < MAX_UI_OUT_LEVELS);
129 gdb_assert (current_level (uiout)->type == type);
131 return uiout->level + 1;
135 /* These are the default implementation functions */
137 static void default_table_begin (struct ui_out *uiout, int nbrofcols,
139 static void default_table_body (struct ui_out *uiout);
140 static void default_table_end (struct ui_out *uiout);
141 static void default_table_header (struct ui_out *uiout, int width,
144 static void default_begin (struct ui_out *uiout,
145 enum ui_out_type type,
146 int level, const char *id);
147 static void default_end (struct ui_out *uiout,
148 enum ui_out_type type,
150 static void default_field_int (struct ui_out *uiout, int fldno, int width,
154 static void default_field_skip (struct ui_out *uiout, int fldno, int width,
156 const char *fldname);
157 static void default_field_string (struct ui_out *uiout, int fldno, int width,
161 static void default_field_fmt (struct ui_out *uiout, int fldno,
162 int width, enum ui_align align,
166 static void default_spaces (struct ui_out *uiout, int numspaces);
167 static void default_text (struct ui_out *uiout, const char *string);
168 static void default_message (struct ui_out *uiout, int verbosity,
171 static void default_wrap_hint (struct ui_out *uiout, char *identstring);
172 static void default_flush (struct ui_out *uiout);
174 /* This is the default ui-out implementation functions vector */
176 struct ui_out_impl default_ui_out_impl =
181 default_table_header,
186 default_field_string,
195 /* The default ui_out */
197 struct ui_out def_uiout =
200 &default_ui_out_impl, /* impl */
203 /* Pointer to current ui_out */
204 /* FIXME: This should not be a global, but something passed down from main.c
207 struct ui_out *uiout = &def_uiout;
209 /* These are the interfaces to implementation functions */
211 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
213 static void uo_table_body (struct ui_out *uiout);
214 static void uo_table_end (struct ui_out *uiout);
215 static void uo_table_header (struct ui_out *uiout, int width,
216 enum ui_align align, const char *colhdr);
217 static void uo_begin (struct ui_out *uiout,
218 enum ui_out_type type,
219 int level, const char *id);
220 static void uo_end (struct ui_out *uiout,
221 enum ui_out_type type,
223 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
224 enum ui_align align, const char *fldname, int value);
225 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
226 enum ui_align align, const char *fldname);
227 static void uo_field_string (struct ui_out *uiout, int fldno, int width,
228 enum ui_align align, const char *fldname,
230 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
231 enum ui_align align, const char *fldname,
232 const char *format, va_list args);
233 static void uo_spaces (struct ui_out *uiout, int numspaces);
234 static void uo_text (struct ui_out *uiout, const char *string);
235 static void uo_message (struct ui_out *uiout, int verbosity,
236 const char *format, va_list args);
237 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
238 static void uo_flush (struct ui_out *uiout);
240 /* Prototypes for local functions */
242 extern void _initialize_ui_out (void);
243 static void append_header_to_list (struct ui_out *uiout, int width,
244 int alignment, const char *colhdr);
245 static int get_curr_header (struct ui_out *uiout, int *colno, int *width,
246 int *alignment, char **colhdr);
247 static void clear_header_list (struct ui_out *uiout);
248 static void verify_field_proper_position (struct ui_out *uiout);
249 static void verify_field_alignment (struct ui_out *uiout, int fldno, int *width, int *alignment);
251 static void init_ui_out_state (struct ui_out *uiout);
253 /* exported functions (ui_out API) */
255 /* Mark beginning of a table */
258 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
261 if (uiout->table_flag)
262 internal_error (__FILE__, __LINE__,
263 "tables cannot be nested; table_begin found before \
264 previous table_end.");
266 uiout->table_flag = 1;
267 uiout->table_columns = nbrofcols;
269 uiout->table_id = xstrdup (tblid);
271 uiout->table_id = NULL;
272 clear_header_list (uiout);
274 uo_table_begin (uiout, nbrofcols, uiout->table_id);
278 ui_out_table_body (struct ui_out *uiout)
280 if (!uiout->table_flag)
281 internal_error (__FILE__, __LINE__,
282 "table_body outside a table is not valid; it must be \
283 after a table_begin and before a table_end.");
284 if (uiout->body_flag)
285 internal_error (__FILE__, __LINE__,
286 "extra table_body call not allowed; there must be \
287 only one table_body after a table_begin and before a table_end.");
288 if (uiout->headercurr->colno != uiout->table_columns)
289 internal_error (__FILE__, __LINE__,
290 "number of headers differ from number of table \
293 uiout->body_flag = 1;
294 uiout->headercurr = uiout->headerfirst;
296 uo_table_body (uiout);
300 ui_out_table_end (struct ui_out *uiout)
302 if (!uiout->table_flag)
303 internal_error (__FILE__, __LINE__,
304 "misplaced table_end or missing table_begin.");
306 uiout->body_flag = 0;
307 uiout->table_flag = 0;
309 uo_table_end (uiout);
312 xfree (uiout->table_id);
313 clear_header_list (uiout);
317 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
320 if (!uiout->table_flag || uiout->body_flag)
321 internal_error (__FILE__, __LINE__,
322 "table header must be specified after table_begin \
323 and before table_body.");
325 append_header_to_list (uiout, width, alignment, colhdr);
327 uo_table_header (uiout, width, alignment, colhdr);
331 ui_out_begin (struct ui_out *uiout,
332 enum ui_out_type type,
336 if (uiout->table_flag && !uiout->body_flag)
337 internal_error (__FILE__, __LINE__,
338 "table header or table_body expected; lists must be \
339 specified after table_body.");
340 new_level = push_level (uiout, type, id);
341 if (uiout->table_flag && (new_level == 1))
342 uiout->headercurr = uiout->headerfirst;
343 uo_begin (uiout, type, new_level, id);
347 ui_out_list_begin (struct ui_out *uiout)
349 ui_out_begin (uiout, ui_out_type_list, NULL);
353 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
355 ui_out_begin (uiout, ui_out_type_tuple, id);
359 ui_out_end (struct ui_out *uiout,
360 enum ui_out_type type)
362 int old_level = pop_level (uiout, type);
363 uo_end (uiout, type, old_level);
367 ui_out_list_end (struct ui_out *uiout)
369 ui_out_end (uiout, ui_out_type_list);
373 ui_out_tuple_end (struct ui_out *uiout)
375 ui_out_end (uiout, ui_out_type_tuple);
378 struct ui_out_end_cleanup_data
380 struct ui_out *uiout;
381 enum ui_out_type type;
385 do_cleanup_end (void *data)
387 struct ui_out_end_cleanup_data *end_cleanup_data = data;
388 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
389 xfree (end_cleanup_data);
392 static struct cleanup *
393 make_cleanup_ui_out_end (struct ui_out *uiout,
394 enum ui_out_type type)
396 struct ui_out_end_cleanup_data *end_cleanup_data;
397 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
398 end_cleanup_data->uiout = uiout;
399 end_cleanup_data->type = type;
400 return make_cleanup (do_cleanup_end, end_cleanup_data);
404 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
405 enum ui_out_type type,
408 ui_out_begin (uiout, type, id);
409 return make_cleanup_ui_out_end (uiout, type);
413 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
416 ui_out_tuple_begin (uiout, id);
417 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
421 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout)
423 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
427 ui_out_field_int (struct ui_out *uiout,
434 struct ui_out_level *current = current_level (uiout);
436 verify_field_proper_position (uiout);
438 current->field_count += 1;
439 fldno = current->field_count;
441 verify_field_alignment (uiout, fldno, &width, &align);
443 uo_field_int (uiout, fldno, width, align, fldname, value);
447 ui_out_field_core_addr (struct ui_out *uiout,
453 /* FIXME-32x64: need a print_address_numeric with field width */
454 /* print_address_numeric (address, 1, local_stream); */
455 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
457 ui_out_field_string (uiout, fldname, addstr);
461 ui_out_field_stream (struct ui_out *uiout,
463 struct ui_stream *buf)
466 char *buffer = ui_file_xstrdup (buf->stream, &length);
467 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
469 ui_out_field_string (uiout, fldname, buffer);
471 ui_out_field_skip (uiout, fldname);
472 ui_file_rewind (buf->stream);
473 do_cleanups (old_cleanup);
476 /* used to ommit a field */
479 ui_out_field_skip (struct ui_out *uiout,
485 struct ui_out_level *current = current_level (uiout);
487 verify_field_proper_position (uiout);
489 current->field_count += 1;
490 fldno = current->field_count;
492 verify_field_alignment (uiout, fldno, &width, &align);
494 uo_field_skip (uiout, fldno, width, align, fldname);
498 ui_out_field_string (struct ui_out *uiout,
505 struct ui_out_level *current = current_level (uiout);
507 verify_field_proper_position (uiout);
509 current->field_count += 1;
510 fldno = current->field_count;
512 verify_field_alignment (uiout, fldno, &width, &align);
514 uo_field_string (uiout, fldno, width, align, fldname, string);
519 ui_out_field_fmt (struct ui_out *uiout,
521 const char *format, ...)
527 struct ui_out_level *current = current_level (uiout);
529 verify_field_proper_position (uiout);
531 current->field_count += 1;
532 fldno = current->field_count;
534 /* will not align, but has to call anyway */
535 verify_field_alignment (uiout, fldno, &width, &align);
537 va_start (args, format);
539 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
545 ui_out_spaces (struct ui_out *uiout, int numspaces)
547 uo_spaces (uiout, numspaces);
551 ui_out_text (struct ui_out *uiout,
554 uo_text (uiout, string);
558 ui_out_message (struct ui_out *uiout, int verbosity,
559 const char *format,...)
563 va_start (args, format);
565 uo_message (uiout, verbosity, format, args);
571 ui_out_stream_new (struct ui_out *uiout)
573 struct ui_stream *tempbuf;
575 tempbuf = XMALLOC (struct ui_stream);
576 tempbuf->uiout = uiout;
577 tempbuf->stream = mem_fileopen ();
582 ui_out_stream_delete (struct ui_stream *buf)
584 ui_file_delete (buf->stream);
589 do_stream_delete (void *buf)
591 ui_out_stream_delete (buf);
595 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
597 return make_cleanup (do_stream_delete, buf);
602 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
604 uo_wrap_hint (uiout, identstring);
608 ui_out_flush (struct ui_out *uiout)
613 /* set the flags specified by the mask given */
615 ui_out_set_flags (struct ui_out *uiout, int mask)
617 int oldflags = uiout->flags;
619 uiout->flags |= mask;
624 /* clear the flags specified by the mask given */
626 ui_out_clear_flags (struct ui_out *uiout, int mask)
628 int oldflags = uiout->flags;
630 uiout->flags &= ~mask;
635 /* test the flags against the mask given */
637 ui_out_test_flags (struct ui_out *uiout, int mask)
639 return (uiout->flags & mask);
642 /* obtain the current verbosity level (as stablished by the
643 'set verbositylevel' command */
646 ui_out_get_verblvl (struct ui_out *uiout)
648 /* FIXME: not implemented yet */
654 ui_out_result_begin (struct ui_out *uiout, char *class)
659 ui_out_result_end (struct ui_out *uiout)
664 ui_out_info_begin (struct ui_out *uiout, char *class)
669 ui_out_info_end (struct ui_out *uiout)
674 ui_out_notify_begin (struct ui_out *uiout, char *class)
679 ui_out_notify_end (struct ui_out *uiout)
684 ui_out_error_begin (struct ui_out *uiout, char *class)
689 ui_out_error_end (struct ui_out *uiout)
696 gdb_error (ui_out * uiout, int severity, char *format,...)
702 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
707 /* default gdb-out hook functions */
710 default_table_begin (struct ui_out *uiout, int nbrofcols, const char *tblid)
715 default_table_body (struct ui_out *uiout)
720 default_table_end (struct ui_out *uiout)
725 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
731 default_begin (struct ui_out *uiout,
732 enum ui_out_type type,
739 default_end (struct ui_out *uiout,
740 enum ui_out_type type,
746 default_field_int (struct ui_out *uiout, int fldno, int width,
748 const char *fldname, int value)
753 default_field_skip (struct ui_out *uiout, int fldno, int width,
754 enum ui_align align, const char *fldname)
759 default_field_string (struct ui_out *uiout,
769 default_field_fmt (struct ui_out *uiout, int fldno, int width,
778 default_spaces (struct ui_out *uiout, int numspaces)
783 default_text (struct ui_out *uiout, const char *string)
788 default_message (struct ui_out *uiout, int verbosity,
795 default_wrap_hint (struct ui_out *uiout, char *identstring)
800 default_flush (struct ui_out *uiout)
804 /* Interface to the implementation functions */
807 uo_table_begin (struct ui_out *uiout, int nbrofcols,
810 if (!uiout->impl->table_begin)
812 uiout->impl->table_begin (uiout, nbrofcols, tblid);
816 uo_table_body (struct ui_out *uiout)
818 if (!uiout->impl->table_body)
820 uiout->impl->table_body (uiout);
824 uo_table_end (struct ui_out *uiout)
826 if (!uiout->impl->table_end)
828 uiout->impl->table_end (uiout);
832 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
835 if (!uiout->impl->table_header)
837 uiout->impl->table_header (uiout, width, align, colhdr);
841 uo_begin (struct ui_out *uiout,
842 enum ui_out_type type,
846 if (uiout->impl->begin == NULL)
848 uiout->impl->begin (uiout, type, level, id);
852 uo_end (struct ui_out *uiout,
853 enum ui_out_type type,
856 if (uiout->impl->end == NULL)
858 uiout->impl->end (uiout, type, level);
862 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
866 if (!uiout->impl->field_int)
868 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
872 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
875 if (!uiout->impl->field_skip)
877 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
881 uo_field_string (struct ui_out *uiout, int fldno, int width,
886 if (!uiout->impl->field_string)
888 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
892 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
897 if (!uiout->impl->field_fmt)
899 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
903 uo_spaces (struct ui_out *uiout, int numspaces)
905 if (!uiout->impl->spaces)
907 uiout->impl->spaces (uiout, numspaces);
911 uo_text (struct ui_out *uiout,
914 if (!uiout->impl->text)
916 uiout->impl->text (uiout, string);
920 uo_message (struct ui_out *uiout, int verbosity,
924 if (!uiout->impl->message)
926 uiout->impl->message (uiout, verbosity, format, args);
930 uo_wrap_hint (struct ui_out *uiout, char *identstring)
932 if (!uiout->impl->wrap_hint)
934 uiout->impl->wrap_hint (uiout, identstring);
938 uo_flush (struct ui_out *uiout)
940 if (!uiout->impl->flush)
942 uiout->impl->flush (uiout);
945 /* local functions */
947 /* list of column headers manipulation routines */
950 clear_header_list (struct ui_out *uiout)
952 while (uiout->headerfirst != NULL)
954 uiout->headercurr = uiout->headerfirst;
955 uiout->headerfirst = uiout->headerfirst->next;
956 if (uiout->headercurr->colhdr != NULL)
957 xfree (uiout->headercurr->colhdr);
958 xfree (uiout->headercurr);
960 uiout->headerlast = NULL;
961 uiout->headercurr = NULL;
965 append_header_to_list (struct ui_out *uiout,
970 struct ui_out_hdr *temphdr;
972 temphdr = XMALLOC (struct ui_out_hdr);
973 temphdr->width = width;
974 temphdr->alignment = alignment;
975 /* we have to copy the column title as the original may be an automatic */
978 temphdr->colhdr = xmalloc (strlen (colhdr) + 1);
979 strcpy (temphdr->colhdr, colhdr);
981 temphdr->next = NULL;
982 if (uiout->headerfirst == NULL)
985 uiout->headerfirst = temphdr;
986 uiout->headerlast = temphdr;
990 temphdr->colno = uiout->headerlast->colno + 1;
991 uiout->headerlast->next = temphdr;
992 uiout->headerlast = temphdr;
994 uiout->headercurr = uiout->headerlast;
997 /* returns 0 if there is no more headers */
1000 get_curr_header (struct ui_out *uiout,
1006 /* There may be no headers at all or we may have used all columns */
1007 if (uiout->headercurr == NULL)
1009 *colno = uiout->headercurr->colno;
1010 *width = uiout->headercurr->width;
1011 *alignment = uiout->headercurr->alignment;
1012 *colhdr = uiout->headercurr->colhdr;
1013 uiout->headercurr = uiout->headercurr->next;
1017 /* makes sure the field_* calls were properly placed */
1020 verify_field_proper_position (struct ui_out *uiout)
1022 if (uiout->table_flag)
1024 if (!uiout->body_flag)
1025 internal_error (__FILE__, __LINE__,
1026 "table_body missing; table fields must be \
1027 specified after table_body and inside a list.");
1028 if (uiout->level == 0)
1029 internal_error (__FILE__, __LINE__,
1030 "list_begin missing; table fields must be \
1031 specified after table_body and inside a list.");
1035 /* determines what is the alignment policy */
1038 verify_field_alignment (struct ui_out *uiout,
1046 if (uiout->table_flag
1047 && get_curr_header (uiout, &colno, width, align, &text))
1050 internal_error (__FILE__, __LINE__,
1051 "ui-out internal error in handling headers.");
1056 *align = ui_noalign;
1060 /* access to ui_out format private members */
1063 ui_out_get_field_separator (struct ui_out *uiout)
1067 /* Access to ui-out members data */
1069 struct ui_out_data *
1070 ui_out_data (struct ui_out *uiout)
1075 /* initalize private members at startup */
1078 ui_out_new (struct ui_out_impl *impl,
1079 struct ui_out_data *data,
1082 struct ui_out *uiout = XMALLOC (struct ui_out);
1085 uiout->flags = flags;
1086 uiout->table_flag = 0;
1087 uiout->body_flag = 0;
1089 memset (uiout->levels, 0, sizeof (uiout->levels));
1090 uiout->headerfirst = NULL;
1091 uiout->headerlast = NULL;
1092 uiout->headercurr = NULL;
1096 /* standard gdb initialization hook */
1099 _initialize_ui_out (void)
1101 /* nothing needs to be done */