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,
138 int nr_rows, const char *tblid);
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,
212 int nr_rows, const char *tblid);
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,
262 if (uiout->table_flag)
263 internal_error (__FILE__, __LINE__,
264 "tables cannot be nested; table_begin found before \
265 previous table_end.");
267 uiout->table_flag = 1;
268 uiout->table_columns = nbrofcols;
270 uiout->table_id = xstrdup (tblid);
272 uiout->table_id = NULL;
273 clear_header_list (uiout);
275 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table_id);
279 ui_out_table_body (struct ui_out *uiout)
281 if (!uiout->table_flag)
282 internal_error (__FILE__, __LINE__,
283 "table_body outside a table is not valid; it must be \
284 after a table_begin and before a table_end.");
285 if (uiout->body_flag)
286 internal_error (__FILE__, __LINE__,
287 "extra table_body call not allowed; there must be \
288 only one table_body after a table_begin and before a table_end.");
289 if (uiout->headercurr->colno != uiout->table_columns)
290 internal_error (__FILE__, __LINE__,
291 "number of headers differ from number of table \
294 uiout->body_flag = 1;
295 uiout->headercurr = uiout->headerfirst;
297 uo_table_body (uiout);
301 ui_out_table_end (struct ui_out *uiout)
303 if (!uiout->table_flag)
304 internal_error (__FILE__, __LINE__,
305 "misplaced table_end or missing table_begin.");
307 uiout->body_flag = 0;
308 uiout->table_flag = 0;
310 uo_table_end (uiout);
313 xfree (uiout->table_id);
314 clear_header_list (uiout);
318 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
321 if (!uiout->table_flag || uiout->body_flag)
322 internal_error (__FILE__, __LINE__,
323 "table header must be specified after table_begin \
324 and before table_body.");
326 append_header_to_list (uiout, width, alignment, colhdr);
328 uo_table_header (uiout, width, alignment, colhdr);
332 ui_out_begin (struct ui_out *uiout,
333 enum ui_out_type type,
337 if (uiout->table_flag && !uiout->body_flag)
338 internal_error (__FILE__, __LINE__,
339 "table header or table_body expected; lists must be \
340 specified after table_body.");
341 new_level = push_level (uiout, type, id);
342 if (uiout->table_flag && (new_level == 1))
343 uiout->headercurr = uiout->headerfirst;
344 uo_begin (uiout, type, new_level, id);
348 ui_out_list_begin (struct ui_out *uiout,
351 ui_out_begin (uiout, ui_out_type_list, id);
355 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
357 ui_out_begin (uiout, ui_out_type_tuple, id);
361 ui_out_end (struct ui_out *uiout,
362 enum ui_out_type type)
364 int old_level = pop_level (uiout, type);
365 uo_end (uiout, type, old_level);
369 ui_out_list_end (struct ui_out *uiout)
371 ui_out_end (uiout, ui_out_type_list);
375 ui_out_tuple_end (struct ui_out *uiout)
377 ui_out_end (uiout, ui_out_type_tuple);
380 struct ui_out_end_cleanup_data
382 struct ui_out *uiout;
383 enum ui_out_type type;
387 do_cleanup_end (void *data)
389 struct ui_out_end_cleanup_data *end_cleanup_data = data;
390 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
391 xfree (end_cleanup_data);
394 static struct cleanup *
395 make_cleanup_ui_out_end (struct ui_out *uiout,
396 enum ui_out_type type)
398 struct ui_out_end_cleanup_data *end_cleanup_data;
399 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
400 end_cleanup_data->uiout = uiout;
401 end_cleanup_data->type = type;
402 return make_cleanup (do_cleanup_end, end_cleanup_data);
406 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
407 enum ui_out_type type,
410 ui_out_begin (uiout, type, id);
411 return make_cleanup_ui_out_end (uiout, type);
415 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
418 ui_out_tuple_begin (uiout, id);
419 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
423 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
426 ui_out_list_begin (uiout, id);
427 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
431 ui_out_field_int (struct ui_out *uiout,
438 struct ui_out_level *current = current_level (uiout);
440 verify_field_proper_position (uiout);
442 current->field_count += 1;
443 fldno = current->field_count;
445 verify_field_alignment (uiout, fldno, &width, &align);
447 uo_field_int (uiout, fldno, width, align, fldname, value);
451 ui_out_field_core_addr (struct ui_out *uiout,
457 /* FIXME-32x64: need a print_address_numeric with field width */
458 /* print_address_numeric (address, 1, local_stream); */
459 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
461 ui_out_field_string (uiout, fldname, addstr);
465 ui_out_field_stream (struct ui_out *uiout,
467 struct ui_stream *buf)
470 char *buffer = ui_file_xstrdup (buf->stream, &length);
471 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
473 ui_out_field_string (uiout, fldname, buffer);
475 ui_out_field_skip (uiout, fldname);
476 ui_file_rewind (buf->stream);
477 do_cleanups (old_cleanup);
480 /* used to ommit a field */
483 ui_out_field_skip (struct ui_out *uiout,
489 struct ui_out_level *current = current_level (uiout);
491 verify_field_proper_position (uiout);
493 current->field_count += 1;
494 fldno = current->field_count;
496 verify_field_alignment (uiout, fldno, &width, &align);
498 uo_field_skip (uiout, fldno, width, align, fldname);
502 ui_out_field_string (struct ui_out *uiout,
509 struct ui_out_level *current = current_level (uiout);
511 verify_field_proper_position (uiout);
513 current->field_count += 1;
514 fldno = current->field_count;
516 verify_field_alignment (uiout, fldno, &width, &align);
518 uo_field_string (uiout, fldno, width, align, fldname, string);
523 ui_out_field_fmt (struct ui_out *uiout,
525 const char *format, ...)
531 struct ui_out_level *current = current_level (uiout);
533 verify_field_proper_position (uiout);
535 current->field_count += 1;
536 fldno = current->field_count;
538 /* will not align, but has to call anyway */
539 verify_field_alignment (uiout, fldno, &width, &align);
541 va_start (args, format);
543 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
549 ui_out_spaces (struct ui_out *uiout, int numspaces)
551 uo_spaces (uiout, numspaces);
555 ui_out_text (struct ui_out *uiout,
558 uo_text (uiout, string);
562 ui_out_message (struct ui_out *uiout, int verbosity,
563 const char *format,...)
567 va_start (args, format);
569 uo_message (uiout, verbosity, format, args);
575 ui_out_stream_new (struct ui_out *uiout)
577 struct ui_stream *tempbuf;
579 tempbuf = XMALLOC (struct ui_stream);
580 tempbuf->uiout = uiout;
581 tempbuf->stream = mem_fileopen ();
586 ui_out_stream_delete (struct ui_stream *buf)
588 ui_file_delete (buf->stream);
593 do_stream_delete (void *buf)
595 ui_out_stream_delete (buf);
599 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
601 return make_cleanup (do_stream_delete, buf);
606 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
608 uo_wrap_hint (uiout, identstring);
612 ui_out_flush (struct ui_out *uiout)
617 /* set the flags specified by the mask given */
619 ui_out_set_flags (struct ui_out *uiout, int mask)
621 int oldflags = uiout->flags;
623 uiout->flags |= mask;
628 /* clear the flags specified by the mask given */
630 ui_out_clear_flags (struct ui_out *uiout, int mask)
632 int oldflags = uiout->flags;
634 uiout->flags &= ~mask;
639 /* test the flags against the mask given */
641 ui_out_test_flags (struct ui_out *uiout, int mask)
643 return (uiout->flags & mask);
646 /* obtain the current verbosity level (as stablished by the
647 'set verbositylevel' command */
650 ui_out_get_verblvl (struct ui_out *uiout)
652 /* FIXME: not implemented yet */
658 ui_out_result_begin (struct ui_out *uiout, char *class)
663 ui_out_result_end (struct ui_out *uiout)
668 ui_out_info_begin (struct ui_out *uiout, char *class)
673 ui_out_info_end (struct ui_out *uiout)
678 ui_out_notify_begin (struct ui_out *uiout, char *class)
683 ui_out_notify_end (struct ui_out *uiout)
688 ui_out_error_begin (struct ui_out *uiout, char *class)
693 ui_out_error_end (struct ui_out *uiout)
700 gdb_error (ui_out * uiout, int severity, char *format,...)
706 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
711 /* default gdb-out hook functions */
714 default_table_begin (struct ui_out *uiout, int nbrofcols,
721 default_table_body (struct ui_out *uiout)
726 default_table_end (struct ui_out *uiout)
731 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
737 default_begin (struct ui_out *uiout,
738 enum ui_out_type type,
745 default_end (struct ui_out *uiout,
746 enum ui_out_type type,
752 default_field_int (struct ui_out *uiout, int fldno, int width,
754 const char *fldname, int value)
759 default_field_skip (struct ui_out *uiout, int fldno, int width,
760 enum ui_align align, const char *fldname)
765 default_field_string (struct ui_out *uiout,
775 default_field_fmt (struct ui_out *uiout, int fldno, int width,
784 default_spaces (struct ui_out *uiout, int numspaces)
789 default_text (struct ui_out *uiout, const char *string)
794 default_message (struct ui_out *uiout, int verbosity,
801 default_wrap_hint (struct ui_out *uiout, char *identstring)
806 default_flush (struct ui_out *uiout)
810 /* Interface to the implementation functions */
813 uo_table_begin (struct ui_out *uiout, int nbrofcols,
817 if (!uiout->impl->table_begin)
819 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
823 uo_table_body (struct ui_out *uiout)
825 if (!uiout->impl->table_body)
827 uiout->impl->table_body (uiout);
831 uo_table_end (struct ui_out *uiout)
833 if (!uiout->impl->table_end)
835 uiout->impl->table_end (uiout);
839 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
842 if (!uiout->impl->table_header)
844 uiout->impl->table_header (uiout, width, align, colhdr);
848 uo_begin (struct ui_out *uiout,
849 enum ui_out_type type,
853 if (uiout->impl->begin == NULL)
855 uiout->impl->begin (uiout, type, level, id);
859 uo_end (struct ui_out *uiout,
860 enum ui_out_type type,
863 if (uiout->impl->end == NULL)
865 uiout->impl->end (uiout, type, level);
869 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
873 if (!uiout->impl->field_int)
875 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
879 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
882 if (!uiout->impl->field_skip)
884 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
888 uo_field_string (struct ui_out *uiout, int fldno, int width,
893 if (!uiout->impl->field_string)
895 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
899 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
904 if (!uiout->impl->field_fmt)
906 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
910 uo_spaces (struct ui_out *uiout, int numspaces)
912 if (!uiout->impl->spaces)
914 uiout->impl->spaces (uiout, numspaces);
918 uo_text (struct ui_out *uiout,
921 if (!uiout->impl->text)
923 uiout->impl->text (uiout, string);
927 uo_message (struct ui_out *uiout, int verbosity,
931 if (!uiout->impl->message)
933 uiout->impl->message (uiout, verbosity, format, args);
937 uo_wrap_hint (struct ui_out *uiout, char *identstring)
939 if (!uiout->impl->wrap_hint)
941 uiout->impl->wrap_hint (uiout, identstring);
945 uo_flush (struct ui_out *uiout)
947 if (!uiout->impl->flush)
949 uiout->impl->flush (uiout);
952 /* local functions */
954 /* list of column headers manipulation routines */
957 clear_header_list (struct ui_out *uiout)
959 while (uiout->headerfirst != NULL)
961 uiout->headercurr = uiout->headerfirst;
962 uiout->headerfirst = uiout->headerfirst->next;
963 if (uiout->headercurr->colhdr != NULL)
964 xfree (uiout->headercurr->colhdr);
965 xfree (uiout->headercurr);
967 uiout->headerlast = NULL;
968 uiout->headercurr = NULL;
972 append_header_to_list (struct ui_out *uiout,
977 struct ui_out_hdr *temphdr;
979 temphdr = XMALLOC (struct ui_out_hdr);
980 temphdr->width = width;
981 temphdr->alignment = alignment;
982 /* we have to copy the column title as the original may be an automatic */
985 temphdr->colhdr = xmalloc (strlen (colhdr) + 1);
986 strcpy (temphdr->colhdr, colhdr);
988 temphdr->next = NULL;
989 if (uiout->headerfirst == NULL)
992 uiout->headerfirst = temphdr;
993 uiout->headerlast = temphdr;
997 temphdr->colno = uiout->headerlast->colno + 1;
998 uiout->headerlast->next = temphdr;
999 uiout->headerlast = temphdr;
1001 uiout->headercurr = uiout->headerlast;
1004 /* returns 0 if there is no more headers */
1007 get_curr_header (struct ui_out *uiout,
1013 /* There may be no headers at all or we may have used all columns */
1014 if (uiout->headercurr == NULL)
1016 *colno = uiout->headercurr->colno;
1017 *width = uiout->headercurr->width;
1018 *alignment = uiout->headercurr->alignment;
1019 *colhdr = uiout->headercurr->colhdr;
1020 uiout->headercurr = uiout->headercurr->next;
1024 /* makes sure the field_* calls were properly placed */
1027 verify_field_proper_position (struct ui_out *uiout)
1029 if (uiout->table_flag)
1031 if (!uiout->body_flag)
1032 internal_error (__FILE__, __LINE__,
1033 "table_body missing; table fields must be \
1034 specified after table_body and inside a list.");
1035 if (uiout->level == 0)
1036 internal_error (__FILE__, __LINE__,
1037 "list_begin missing; table fields must be \
1038 specified after table_body and inside a list.");
1042 /* determines what is the alignment policy */
1045 verify_field_alignment (struct ui_out *uiout,
1053 if (uiout->table_flag
1054 && get_curr_header (uiout, &colno, width, align, &text))
1057 internal_error (__FILE__, __LINE__,
1058 "ui-out internal error in handling headers.");
1063 *align = ui_noalign;
1067 /* access to ui_out format private members */
1070 ui_out_get_field_separator (struct ui_out *uiout)
1074 /* Access to ui-out members data */
1076 struct ui_out_data *
1077 ui_out_data (struct ui_out *uiout)
1082 /* initalize private members at startup */
1085 ui_out_new (struct ui_out_impl *impl,
1086 struct ui_out_data *data,
1089 struct ui_out *uiout = XMALLOC (struct ui_out);
1092 uiout->flags = flags;
1093 uiout->table_flag = 0;
1094 uiout->body_flag = 0;
1096 memset (uiout->levels, 0, sizeof (uiout->levels));
1097 uiout->headerfirst = NULL;
1098 uiout->headerlast = NULL;
1099 uiout->headercurr = NULL;
1103 /* standard gdb initialization hook */
1106 _initialize_ui_out (void)
1108 /* nothing needs to be done */