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, char *tblid);
212 static void uo_table_body (struct ui_out *uiout);
213 static void uo_table_end (struct ui_out *uiout);
214 static void uo_table_header (struct ui_out *uiout, int width,
215 enum ui_align align, char *colhdr);
216 static void uo_begin (struct ui_out *uiout,
217 enum ui_out_type type,
218 int level, const char *id);
219 static void uo_end (struct ui_out *uiout,
220 enum ui_out_type type,
222 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
223 enum ui_align align, char *fldname, int value);
224 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
225 enum ui_align align, char *fldname);
226 static void uo_field_string (struct ui_out *uiout, int fldno, int width,
227 enum ui_align align, char *fldname, const char *string);
228 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
229 enum ui_align align, char *fldname,
230 char *format, va_list args);
231 static void uo_spaces (struct ui_out *uiout, int numspaces);
232 static void uo_text (struct ui_out *uiout, char *string);
233 static void uo_message (struct ui_out *uiout, int verbosity,
234 char *format, va_list args);
235 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
236 static void uo_flush (struct ui_out *uiout);
238 /* Prototypes for local functions */
240 extern void _initialize_ui_out (void);
241 static void append_header_to_list (struct ui_out *uiout, int width, int alignment, char *colhdr);
242 static int get_curr_header (struct ui_out *uiout, int *colno, int *width,
243 int *alignment, char **colhdr);
244 static void clear_header_list (struct ui_out *uiout);
245 static void verify_field_proper_position (struct ui_out *uiout);
246 static void verify_field_alignment (struct ui_out *uiout, int fldno, int *width, int *alignment);
248 static void init_ui_out_state (struct ui_out *uiout);
250 /* exported functions (ui_out API) */
252 /* Mark beginning of a table */
255 ui_out_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid)
257 if (uiout->table_flag)
258 internal_error (__FILE__, __LINE__,
259 "tables cannot be nested; table_begin found before \
260 previous table_end.");
262 uiout->table_flag = 1;
263 uiout->table_columns = nbrofcols;
265 uiout->table_id = xstrdup (tblid);
267 uiout->table_id = NULL;
268 clear_header_list (uiout);
270 uo_table_begin (uiout, nbrofcols, uiout->table_id);
274 ui_out_table_body (struct ui_out *uiout)
276 if (!uiout->table_flag)
277 internal_error (__FILE__, __LINE__,
278 "table_body outside a table is not valid; it must be \
279 after a table_begin and before a table_end.");
280 if (uiout->body_flag)
281 internal_error (__FILE__, __LINE__,
282 "extra table_body call not allowed; there must be \
283 only one table_body after a table_begin and before a table_end.");
284 if (uiout->headercurr->colno != uiout->table_columns)
285 internal_error (__FILE__, __LINE__,
286 "number of headers differ from number of table \
289 uiout->body_flag = 1;
290 uiout->headercurr = uiout->headerfirst;
292 uo_table_body (uiout);
296 ui_out_table_end (struct ui_out *uiout)
298 if (!uiout->table_flag)
299 internal_error (__FILE__, __LINE__,
300 "misplaced table_end or missing table_begin.");
302 uiout->body_flag = 0;
303 uiout->table_flag = 0;
305 uo_table_end (uiout);
308 xfree (uiout->table_id);
309 clear_header_list (uiout);
313 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
316 if (!uiout->table_flag || uiout->body_flag)
317 internal_error (__FILE__, __LINE__,
318 "table header must be specified after table_begin \
319 and before table_body.");
321 append_header_to_list (uiout, width, alignment, colhdr);
323 uo_table_header (uiout, width, alignment, colhdr);
327 ui_out_begin (struct ui_out *uiout,
328 enum ui_out_type type,
332 if (uiout->table_flag && !uiout->body_flag)
333 internal_error (__FILE__, __LINE__,
334 "table header or table_body expected; lists must be \
335 specified after table_body.");
336 new_level = push_level (uiout, type, id);
337 if (uiout->table_flag && (new_level == 1))
338 uiout->headercurr = uiout->headerfirst;
339 uo_begin (uiout, type, new_level, id);
343 ui_out_list_begin (struct ui_out *uiout)
345 ui_out_begin (uiout, ui_out_type_list, NULL);
349 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
351 ui_out_begin (uiout, ui_out_type_tuple, id);
355 ui_out_end (struct ui_out *uiout,
356 enum ui_out_type type)
358 int old_level = pop_level (uiout, type);
359 uo_end (uiout, type, old_level);
363 ui_out_list_end (struct ui_out *uiout)
365 ui_out_end (uiout, ui_out_type_list);
369 ui_out_tuple_end (struct ui_out *uiout)
371 ui_out_end (uiout, ui_out_type_tuple);
374 struct ui_out_end_cleanup_data
376 struct ui_out *uiout;
377 enum ui_out_type type;
381 do_cleanup_end (void *data)
383 struct ui_out_end_cleanup_data *end_cleanup_data = data;
384 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
385 xfree (end_cleanup_data);
388 static struct cleanup *
389 make_cleanup_ui_out_end (struct ui_out *uiout,
390 enum ui_out_type type)
392 struct ui_out_end_cleanup_data *end_cleanup_data;
393 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
394 end_cleanup_data->uiout = uiout;
395 end_cleanup_data->type = type;
396 return make_cleanup (do_cleanup_end, end_cleanup_data);
400 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
401 enum ui_out_type type,
404 ui_out_begin (uiout, type, id);
405 return make_cleanup_ui_out_end (uiout, type);
409 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
412 ui_out_tuple_begin (uiout, id);
413 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
417 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout)
419 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
423 ui_out_field_int (struct ui_out *uiout, char *fldname, int value)
428 struct ui_out_level *current = current_level (uiout);
430 verify_field_proper_position (uiout);
432 current->field_count += 1;
433 fldno = current->field_count;
435 verify_field_alignment (uiout, fldno, &width, &align);
437 uo_field_int (uiout, fldno, width, align, fldname, value);
441 ui_out_field_core_addr (struct ui_out *uiout, char *fldname, CORE_ADDR address)
445 /* FIXME-32x64: need a print_address_numeric with field width */
446 /* print_address_numeric (address, 1, local_stream); */
447 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
449 ui_out_field_string (uiout, fldname, addstr);
453 ui_out_field_stream (struct ui_out *uiout, char *fldname, struct ui_stream *buf)
456 char *buffer = ui_file_xstrdup (buf->stream, &length);
457 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
459 ui_out_field_string (uiout, fldname, buffer);
461 ui_out_field_skip (uiout, fldname);
462 ui_file_rewind (buf->stream);
463 do_cleanups (old_cleanup);
466 /* used to ommit a field */
469 ui_out_field_skip (struct ui_out *uiout, char *fldname)
474 struct ui_out_level *current = current_level (uiout);
476 verify_field_proper_position (uiout);
478 current->field_count += 1;
479 fldno = current->field_count;
481 verify_field_alignment (uiout, fldno, &width, &align);
483 uo_field_skip (uiout, fldno, width, align, fldname);
487 ui_out_field_string (struct ui_out *uiout,
494 struct ui_out_level *current = current_level (uiout);
496 verify_field_proper_position (uiout);
498 current->field_count += 1;
499 fldno = current->field_count;
501 verify_field_alignment (uiout, fldno, &width, &align);
503 uo_field_string (uiout, fldno, width, align, fldname, string);
508 ui_out_field_fmt (struct ui_out *uiout, char *fldname, char *format,...)
514 struct ui_out_level *current = current_level (uiout);
516 verify_field_proper_position (uiout);
518 current->field_count += 1;
519 fldno = current->field_count;
521 /* will not align, but has to call anyway */
522 verify_field_alignment (uiout, fldno, &width, &align);
524 va_start (args, format);
526 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
532 ui_out_spaces (struct ui_out *uiout, int numspaces)
534 uo_spaces (uiout, numspaces);
538 ui_out_text (struct ui_out *uiout, char *string)
540 uo_text (uiout, string);
544 ui_out_message (struct ui_out *uiout, int verbosity, char *format,...)
548 va_start (args, format);
550 uo_message (uiout, verbosity, format, args);
556 ui_out_stream_new (struct ui_out *uiout)
558 struct ui_stream *tempbuf;
560 tempbuf = XMALLOC (struct ui_stream);
561 tempbuf->uiout = uiout;
562 tempbuf->stream = mem_fileopen ();
567 ui_out_stream_delete (struct ui_stream *buf)
569 ui_file_delete (buf->stream);
574 do_stream_delete (void *buf)
576 ui_out_stream_delete (buf);
580 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
582 return make_cleanup (do_stream_delete, buf);
587 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
589 uo_wrap_hint (uiout, identstring);
593 ui_out_flush (struct ui_out *uiout)
598 /* set the flags specified by the mask given */
600 ui_out_set_flags (struct ui_out *uiout, int mask)
602 int oldflags = uiout->flags;
604 uiout->flags |= mask;
609 /* clear the flags specified by the mask given */
611 ui_out_clear_flags (struct ui_out *uiout, int mask)
613 int oldflags = uiout->flags;
615 uiout->flags &= ~mask;
620 /* test the flags against the mask given */
622 ui_out_test_flags (struct ui_out *uiout, int mask)
624 return (uiout->flags & mask);
627 /* obtain the current verbosity level (as stablished by the
628 'set verbositylevel' command */
631 ui_out_get_verblvl (struct ui_out *uiout)
633 /* FIXME: not implemented yet */
639 ui_out_result_begin (struct ui_out *uiout, char *class)
644 ui_out_result_end (struct ui_out *uiout)
649 ui_out_info_begin (struct ui_out *uiout, char *class)
654 ui_out_info_end (struct ui_out *uiout)
659 ui_out_notify_begin (struct ui_out *uiout, char *class)
664 ui_out_notify_end (struct ui_out *uiout)
669 ui_out_error_begin (struct ui_out *uiout, char *class)
674 ui_out_error_end (struct ui_out *uiout)
681 gdb_error (ui_out * uiout, int severity, char *format,...)
687 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
692 /* default gdb-out hook functions */
695 default_table_begin (struct ui_out *uiout, int nbrofcols, const char *tblid)
700 default_table_body (struct ui_out *uiout)
705 default_table_end (struct ui_out *uiout)
710 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
716 default_begin (struct ui_out *uiout,
717 enum ui_out_type type,
724 default_end (struct ui_out *uiout,
725 enum ui_out_type type,
731 default_field_int (struct ui_out *uiout, int fldno, int width,
733 const char *fldname, int value)
738 default_field_skip (struct ui_out *uiout, int fldno, int width,
739 enum ui_align align, const char *fldname)
744 default_field_string (struct ui_out *uiout,
754 default_field_fmt (struct ui_out *uiout, int fldno, int width,
763 default_spaces (struct ui_out *uiout, int numspaces)
768 default_text (struct ui_out *uiout, const char *string)
773 default_message (struct ui_out *uiout, int verbosity,
780 default_wrap_hint (struct ui_out *uiout, char *identstring)
785 default_flush (struct ui_out *uiout)
789 /* Interface to the implementation functions */
792 uo_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid)
794 if (!uiout->impl->table_begin)
796 uiout->impl->table_begin (uiout, nbrofcols, tblid);
800 uo_table_body (struct ui_out *uiout)
802 if (!uiout->impl->table_body)
804 uiout->impl->table_body (uiout);
808 uo_table_end (struct ui_out *uiout)
810 if (!uiout->impl->table_end)
812 uiout->impl->table_end (uiout);
816 uo_table_header (struct ui_out *uiout, int width, enum ui_align align, char *colhdr)
818 if (!uiout->impl->table_header)
820 uiout->impl->table_header (uiout, width, align, colhdr);
824 uo_begin (struct ui_out *uiout,
825 enum ui_out_type type,
829 if (uiout->impl->begin == NULL)
831 uiout->impl->begin (uiout, type, level, id);
835 uo_end (struct ui_out *uiout,
836 enum ui_out_type type,
839 if (uiout->impl->end == NULL)
841 uiout->impl->end (uiout, type, level);
845 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname, int value)
847 if (!uiout->impl->field_int)
849 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
853 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname)
855 if (!uiout->impl->field_skip)
857 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
861 uo_field_string (struct ui_out *uiout, int fldno, int width,
862 enum ui_align align, char *fldname, const char *string)
864 if (!uiout->impl->field_string)
866 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
870 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname, char *format, va_list args)
872 if (!uiout->impl->field_fmt)
874 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
878 uo_spaces (struct ui_out *uiout, int numspaces)
880 if (!uiout->impl->spaces)
882 uiout->impl->spaces (uiout, numspaces);
886 uo_text (struct ui_out *uiout, char *string)
888 if (!uiout->impl->text)
890 uiout->impl->text (uiout, string);
894 uo_message (struct ui_out *uiout, int verbosity, char *format, va_list args)
896 if (!uiout->impl->message)
898 uiout->impl->message (uiout, verbosity, format, args);
902 uo_wrap_hint (struct ui_out *uiout, char *identstring)
904 if (!uiout->impl->wrap_hint)
906 uiout->impl->wrap_hint (uiout, identstring);
910 uo_flush (struct ui_out *uiout)
912 if (!uiout->impl->flush)
914 uiout->impl->flush (uiout);
917 /* local functions */
919 /* list of column headers manipulation routines */
922 clear_header_list (struct ui_out *uiout)
924 while (uiout->headerfirst != NULL)
926 uiout->headercurr = uiout->headerfirst;
927 uiout->headerfirst = uiout->headerfirst->next;
928 if (uiout->headercurr->colhdr != NULL)
929 xfree (uiout->headercurr->colhdr);
930 xfree (uiout->headercurr);
932 uiout->headerlast = NULL;
933 uiout->headercurr = NULL;
937 append_header_to_list (struct ui_out *uiout,
942 struct ui_out_hdr *temphdr;
944 temphdr = XMALLOC (struct ui_out_hdr);
945 temphdr->width = width;
946 temphdr->alignment = alignment;
947 /* we have to copy the column title as the original may be an automatic */
950 temphdr->colhdr = xmalloc (strlen (colhdr) + 1);
951 strcpy (temphdr->colhdr, colhdr);
953 temphdr->next = NULL;
954 if (uiout->headerfirst == NULL)
957 uiout->headerfirst = temphdr;
958 uiout->headerlast = temphdr;
962 temphdr->colno = uiout->headerlast->colno + 1;
963 uiout->headerlast->next = temphdr;
964 uiout->headerlast = temphdr;
966 uiout->headercurr = uiout->headerlast;
969 /* returns 0 if there is no more headers */
972 get_curr_header (struct ui_out *uiout,
978 /* There may be no headers at all or we may have used all columns */
979 if (uiout->headercurr == NULL)
981 *colno = uiout->headercurr->colno;
982 *width = uiout->headercurr->width;
983 *alignment = uiout->headercurr->alignment;
984 *colhdr = uiout->headercurr->colhdr;
985 uiout->headercurr = uiout->headercurr->next;
989 /* makes sure the field_* calls were properly placed */
992 verify_field_proper_position (struct ui_out *uiout)
994 if (uiout->table_flag)
996 if (!uiout->body_flag)
997 internal_error (__FILE__, __LINE__,
998 "table_body missing; table fields must be \
999 specified after table_body and inside a list.");
1000 if (uiout->level == 0)
1001 internal_error (__FILE__, __LINE__,
1002 "list_begin missing; table fields must be \
1003 specified after table_body and inside a list.");
1007 /* determines what is the alignment policy */
1010 verify_field_alignment (struct ui_out *uiout,
1018 if (uiout->table_flag
1019 && get_curr_header (uiout, &colno, width, align, &text))
1022 internal_error (__FILE__, __LINE__,
1023 "ui-out internal error in handling headers.");
1028 *align = ui_noalign;
1032 /* access to ui_out format private members */
1035 ui_out_get_field_separator (struct ui_out *uiout)
1039 /* Access to ui-out members data */
1041 struct ui_out_data *
1042 ui_out_data (struct ui_out *uiout)
1047 /* initalize private members at startup */
1050 ui_out_new (struct ui_out_impl *impl,
1051 struct ui_out_data *data,
1054 struct ui_out *uiout = XMALLOC (struct ui_out);
1057 uiout->flags = flags;
1058 uiout->table_flag = 0;
1059 uiout->body_flag = 0;
1061 memset (uiout->levels, 0, sizeof (uiout->levels));
1062 uiout->headerfirst = NULL;
1063 uiout->headerlast = NULL;
1064 uiout->headercurr = NULL;
1068 /* standard gdb initialization hook */
1071 _initialize_ui_out (void)
1073 /* nothing needs to be done */