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,
350 ui_out_begin (uiout, ui_out_type_list, id);
354 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
356 ui_out_begin (uiout, ui_out_type_tuple, id);
360 ui_out_end (struct ui_out *uiout,
361 enum ui_out_type type)
363 int old_level = pop_level (uiout, type);
364 uo_end (uiout, type, old_level);
368 ui_out_list_end (struct ui_out *uiout)
370 ui_out_end (uiout, ui_out_type_list);
374 ui_out_tuple_end (struct ui_out *uiout)
376 ui_out_end (uiout, ui_out_type_tuple);
379 struct ui_out_end_cleanup_data
381 struct ui_out *uiout;
382 enum ui_out_type type;
386 do_cleanup_end (void *data)
388 struct ui_out_end_cleanup_data *end_cleanup_data = data;
389 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
390 xfree (end_cleanup_data);
393 static struct cleanup *
394 make_cleanup_ui_out_end (struct ui_out *uiout,
395 enum ui_out_type type)
397 struct ui_out_end_cleanup_data *end_cleanup_data;
398 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
399 end_cleanup_data->uiout = uiout;
400 end_cleanup_data->type = type;
401 return make_cleanup (do_cleanup_end, end_cleanup_data);
405 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
406 enum ui_out_type type,
409 ui_out_begin (uiout, type, id);
410 return make_cleanup_ui_out_end (uiout, type);
414 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
417 ui_out_tuple_begin (uiout, id);
418 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
422 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
425 ui_out_list_begin (uiout, id);
426 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
430 ui_out_field_int (struct ui_out *uiout,
437 struct ui_out_level *current = current_level (uiout);
439 verify_field_proper_position (uiout);
441 current->field_count += 1;
442 fldno = current->field_count;
444 verify_field_alignment (uiout, fldno, &width, &align);
446 uo_field_int (uiout, fldno, width, align, fldname, value);
450 ui_out_field_core_addr (struct ui_out *uiout,
456 /* FIXME-32x64: need a print_address_numeric with field width */
457 /* print_address_numeric (address, 1, local_stream); */
458 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
460 ui_out_field_string (uiout, fldname, addstr);
464 ui_out_field_stream (struct ui_out *uiout,
466 struct ui_stream *buf)
469 char *buffer = ui_file_xstrdup (buf->stream, &length);
470 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
472 ui_out_field_string (uiout, fldname, buffer);
474 ui_out_field_skip (uiout, fldname);
475 ui_file_rewind (buf->stream);
476 do_cleanups (old_cleanup);
479 /* used to ommit a field */
482 ui_out_field_skip (struct ui_out *uiout,
488 struct ui_out_level *current = current_level (uiout);
490 verify_field_proper_position (uiout);
492 current->field_count += 1;
493 fldno = current->field_count;
495 verify_field_alignment (uiout, fldno, &width, &align);
497 uo_field_skip (uiout, fldno, width, align, fldname);
501 ui_out_field_string (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_string (uiout, fldno, width, align, fldname, string);
522 ui_out_field_fmt (struct ui_out *uiout,
524 const char *format, ...)
530 struct ui_out_level *current = current_level (uiout);
532 verify_field_proper_position (uiout);
534 current->field_count += 1;
535 fldno = current->field_count;
537 /* will not align, but has to call anyway */
538 verify_field_alignment (uiout, fldno, &width, &align);
540 va_start (args, format);
542 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
548 ui_out_spaces (struct ui_out *uiout, int numspaces)
550 uo_spaces (uiout, numspaces);
554 ui_out_text (struct ui_out *uiout,
557 uo_text (uiout, string);
561 ui_out_message (struct ui_out *uiout, int verbosity,
562 const char *format,...)
566 va_start (args, format);
568 uo_message (uiout, verbosity, format, args);
574 ui_out_stream_new (struct ui_out *uiout)
576 struct ui_stream *tempbuf;
578 tempbuf = XMALLOC (struct ui_stream);
579 tempbuf->uiout = uiout;
580 tempbuf->stream = mem_fileopen ();
585 ui_out_stream_delete (struct ui_stream *buf)
587 ui_file_delete (buf->stream);
592 do_stream_delete (void *buf)
594 ui_out_stream_delete (buf);
598 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
600 return make_cleanup (do_stream_delete, buf);
605 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
607 uo_wrap_hint (uiout, identstring);
611 ui_out_flush (struct ui_out *uiout)
616 /* set the flags specified by the mask given */
618 ui_out_set_flags (struct ui_out *uiout, int mask)
620 int oldflags = uiout->flags;
622 uiout->flags |= mask;
627 /* clear the flags specified by the mask given */
629 ui_out_clear_flags (struct ui_out *uiout, int mask)
631 int oldflags = uiout->flags;
633 uiout->flags &= ~mask;
638 /* test the flags against the mask given */
640 ui_out_test_flags (struct ui_out *uiout, int mask)
642 return (uiout->flags & mask);
645 /* obtain the current verbosity level (as stablished by the
646 'set verbositylevel' command */
649 ui_out_get_verblvl (struct ui_out *uiout)
651 /* FIXME: not implemented yet */
657 ui_out_result_begin (struct ui_out *uiout, char *class)
662 ui_out_result_end (struct ui_out *uiout)
667 ui_out_info_begin (struct ui_out *uiout, char *class)
672 ui_out_info_end (struct ui_out *uiout)
677 ui_out_notify_begin (struct ui_out *uiout, char *class)
682 ui_out_notify_end (struct ui_out *uiout)
687 ui_out_error_begin (struct ui_out *uiout, char *class)
692 ui_out_error_end (struct ui_out *uiout)
699 gdb_error (ui_out * uiout, int severity, char *format,...)
705 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
710 /* default gdb-out hook functions */
713 default_table_begin (struct ui_out *uiout, int nbrofcols, const char *tblid)
718 default_table_body (struct ui_out *uiout)
723 default_table_end (struct ui_out *uiout)
728 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
734 default_begin (struct ui_out *uiout,
735 enum ui_out_type type,
742 default_end (struct ui_out *uiout,
743 enum ui_out_type type,
749 default_field_int (struct ui_out *uiout, int fldno, int width,
751 const char *fldname, int value)
756 default_field_skip (struct ui_out *uiout, int fldno, int width,
757 enum ui_align align, const char *fldname)
762 default_field_string (struct ui_out *uiout,
772 default_field_fmt (struct ui_out *uiout, int fldno, int width,
781 default_spaces (struct ui_out *uiout, int numspaces)
786 default_text (struct ui_out *uiout, const char *string)
791 default_message (struct ui_out *uiout, int verbosity,
798 default_wrap_hint (struct ui_out *uiout, char *identstring)
803 default_flush (struct ui_out *uiout)
807 /* Interface to the implementation functions */
810 uo_table_begin (struct ui_out *uiout, int nbrofcols,
813 if (!uiout->impl->table_begin)
815 uiout->impl->table_begin (uiout, nbrofcols, tblid);
819 uo_table_body (struct ui_out *uiout)
821 if (!uiout->impl->table_body)
823 uiout->impl->table_body (uiout);
827 uo_table_end (struct ui_out *uiout)
829 if (!uiout->impl->table_end)
831 uiout->impl->table_end (uiout);
835 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
838 if (!uiout->impl->table_header)
840 uiout->impl->table_header (uiout, width, align, colhdr);
844 uo_begin (struct ui_out *uiout,
845 enum ui_out_type type,
849 if (uiout->impl->begin == NULL)
851 uiout->impl->begin (uiout, type, level, id);
855 uo_end (struct ui_out *uiout,
856 enum ui_out_type type,
859 if (uiout->impl->end == NULL)
861 uiout->impl->end (uiout, type, level);
865 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
869 if (!uiout->impl->field_int)
871 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
875 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
878 if (!uiout->impl->field_skip)
880 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
884 uo_field_string (struct ui_out *uiout, int fldno, int width,
889 if (!uiout->impl->field_string)
891 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
895 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
900 if (!uiout->impl->field_fmt)
902 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
906 uo_spaces (struct ui_out *uiout, int numspaces)
908 if (!uiout->impl->spaces)
910 uiout->impl->spaces (uiout, numspaces);
914 uo_text (struct ui_out *uiout,
917 if (!uiout->impl->text)
919 uiout->impl->text (uiout, string);
923 uo_message (struct ui_out *uiout, int verbosity,
927 if (!uiout->impl->message)
929 uiout->impl->message (uiout, verbosity, format, args);
933 uo_wrap_hint (struct ui_out *uiout, char *identstring)
935 if (!uiout->impl->wrap_hint)
937 uiout->impl->wrap_hint (uiout, identstring);
941 uo_flush (struct ui_out *uiout)
943 if (!uiout->impl->flush)
945 uiout->impl->flush (uiout);
948 /* local functions */
950 /* list of column headers manipulation routines */
953 clear_header_list (struct ui_out *uiout)
955 while (uiout->headerfirst != NULL)
957 uiout->headercurr = uiout->headerfirst;
958 uiout->headerfirst = uiout->headerfirst->next;
959 if (uiout->headercurr->colhdr != NULL)
960 xfree (uiout->headercurr->colhdr);
961 xfree (uiout->headercurr);
963 uiout->headerlast = NULL;
964 uiout->headercurr = NULL;
968 append_header_to_list (struct ui_out *uiout,
973 struct ui_out_hdr *temphdr;
975 temphdr = XMALLOC (struct ui_out_hdr);
976 temphdr->width = width;
977 temphdr->alignment = alignment;
978 /* we have to copy the column title as the original may be an automatic */
981 temphdr->colhdr = xmalloc (strlen (colhdr) + 1);
982 strcpy (temphdr->colhdr, colhdr);
984 temphdr->next = NULL;
985 if (uiout->headerfirst == NULL)
988 uiout->headerfirst = temphdr;
989 uiout->headerlast = temphdr;
993 temphdr->colno = uiout->headerlast->colno + 1;
994 uiout->headerlast->next = temphdr;
995 uiout->headerlast = temphdr;
997 uiout->headercurr = uiout->headerlast;
1000 /* returns 0 if there is no more headers */
1003 get_curr_header (struct ui_out *uiout,
1009 /* There may be no headers at all or we may have used all columns */
1010 if (uiout->headercurr == NULL)
1012 *colno = uiout->headercurr->colno;
1013 *width = uiout->headercurr->width;
1014 *alignment = uiout->headercurr->alignment;
1015 *colhdr = uiout->headercurr->colhdr;
1016 uiout->headercurr = uiout->headercurr->next;
1020 /* makes sure the field_* calls were properly placed */
1023 verify_field_proper_position (struct ui_out *uiout)
1025 if (uiout->table_flag)
1027 if (!uiout->body_flag)
1028 internal_error (__FILE__, __LINE__,
1029 "table_body missing; table fields must be \
1030 specified after table_body and inside a list.");
1031 if (uiout->level == 0)
1032 internal_error (__FILE__, __LINE__,
1033 "list_begin missing; table fields must be \
1034 specified after table_body and inside a list.");
1038 /* determines what is the alignment policy */
1041 verify_field_alignment (struct ui_out *uiout,
1049 if (uiout->table_flag
1050 && get_curr_header (uiout, &colno, width, align, &text))
1053 internal_error (__FILE__, __LINE__,
1054 "ui-out internal error in handling headers.");
1059 *align = ui_noalign;
1063 /* access to ui_out format private members */
1066 ui_out_get_field_separator (struct ui_out *uiout)
1070 /* Access to ui-out members data */
1072 struct ui_out_data *
1073 ui_out_data (struct ui_out *uiout)
1078 /* initalize private members at startup */
1081 ui_out_new (struct ui_out_impl *impl,
1082 struct ui_out_data *data,
1085 struct ui_out *uiout = XMALLOC (struct ui_out);
1088 uiout->flags = flags;
1089 uiout->table_flag = 0;
1090 uiout->body_flag = 0;
1092 memset (uiout->levels, 0, sizeof (uiout->levels));
1093 uiout->headerfirst = NULL;
1094 uiout->headerlast = NULL;
1095 uiout->headercurr = NULL;
1099 /* standard gdb initialization hook */
1102 _initialize_ui_out (void)
1104 /* nothing needs to be done */