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,
194 0, /* Does not need MI hacks. */
197 /* The default ui_out */
199 struct ui_out def_uiout =
202 &default_ui_out_impl, /* impl */
205 /* Pointer to current ui_out */
206 /* FIXME: This should not be a global, but something passed down from main.c
209 struct ui_out *uiout = &def_uiout;
211 /* These are the interfaces to implementation functions */
213 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
214 int nr_rows, const char *tblid);
215 static void uo_table_body (struct ui_out *uiout);
216 static void uo_table_end (struct ui_out *uiout);
217 static void uo_table_header (struct ui_out *uiout, int width,
218 enum ui_align align, const char *col_name,
220 static void uo_begin (struct ui_out *uiout,
221 enum ui_out_type type,
222 int level, const char *id);
223 static void uo_end (struct ui_out *uiout,
224 enum ui_out_type type,
226 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
227 enum ui_align align, const char *fldname, int value);
228 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
229 enum ui_align align, const char *fldname);
230 static void uo_field_string (struct ui_out *uiout, int fldno, int width,
231 enum ui_align align, const char *fldname,
233 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
234 enum ui_align align, const char *fldname,
235 const char *format, va_list args);
236 static void uo_spaces (struct ui_out *uiout, int numspaces);
237 static void uo_text (struct ui_out *uiout, const char *string);
238 static void uo_message (struct ui_out *uiout, int verbosity,
239 const char *format, va_list args);
240 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
241 static void uo_flush (struct ui_out *uiout);
243 /* Prototypes for local functions */
245 extern void _initialize_ui_out (void);
246 static void append_header_to_list (struct ui_out *uiout, int width,
247 int alignment, const char *col_name,
249 static int get_curr_header (struct ui_out *uiout, int *colno, int *width,
250 int *alignment, char **colhdr);
251 static void clear_header_list (struct ui_out *uiout);
252 static void verify_field_proper_position (struct ui_out *uiout);
253 static void verify_field_alignment (struct ui_out *uiout, int fldno, int *width, int *alignment);
255 static void init_ui_out_state (struct ui_out *uiout);
257 /* exported functions (ui_out API) */
259 /* Mark beginning of a table */
262 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
266 if (uiout->table_flag)
267 internal_error (__FILE__, __LINE__,
268 "tables cannot be nested; table_begin found before \
269 previous table_end.");
271 uiout->table_flag = 1;
272 uiout->table_columns = nbrofcols;
274 uiout->table_id = xstrdup (tblid);
276 uiout->table_id = NULL;
277 clear_header_list (uiout);
279 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table_id);
283 ui_out_table_body (struct ui_out *uiout)
285 if (!uiout->table_flag)
286 internal_error (__FILE__, __LINE__,
287 "table_body outside a table is not valid; it must be \
288 after a table_begin and before a table_end.");
289 if (uiout->body_flag)
290 internal_error (__FILE__, __LINE__,
291 "extra table_body call not allowed; there must be \
292 only one table_body after a table_begin and before a table_end.");
293 if (uiout->headercurr->colno != uiout->table_columns)
294 internal_error (__FILE__, __LINE__,
295 "number of headers differ from number of table \
298 uiout->body_flag = 1;
299 uiout->headercurr = uiout->headerfirst;
301 uo_table_body (uiout);
305 ui_out_table_end (struct ui_out *uiout)
307 if (!uiout->table_flag)
308 internal_error (__FILE__, __LINE__,
309 "misplaced table_end or missing table_begin.");
311 uiout->body_flag = 0;
312 uiout->table_flag = 0;
314 uo_table_end (uiout);
317 xfree (uiout->table_id);
318 clear_header_list (uiout);
322 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
323 const char *col_name,
326 if (!uiout->table_flag || uiout->body_flag)
327 internal_error (__FILE__, __LINE__,
328 "table header must be specified after table_begin \
329 and before table_body.");
331 append_header_to_list (uiout, width, alignment, col_name, colhdr);
333 uo_table_header (uiout, width, alignment, col_name, colhdr);
337 ui_out_begin (struct ui_out *uiout,
338 enum ui_out_type type,
342 if (uiout->table_flag && !uiout->body_flag)
343 internal_error (__FILE__, __LINE__,
344 "table header or table_body expected; lists must be \
345 specified after table_body.");
346 new_level = push_level (uiout, type, id);
347 if (uiout->table_flag && (new_level == 1))
348 uiout->headercurr = uiout->headerfirst;
349 uo_begin (uiout, type, new_level, id);
353 ui_out_list_begin (struct ui_out *uiout,
356 ui_out_begin (uiout, ui_out_type_list, id);
360 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
362 ui_out_begin (uiout, ui_out_type_tuple, id);
366 ui_out_end (struct ui_out *uiout,
367 enum ui_out_type type)
369 int old_level = pop_level (uiout, type);
370 uo_end (uiout, type, old_level);
374 ui_out_list_end (struct ui_out *uiout)
376 ui_out_end (uiout, ui_out_type_list);
380 ui_out_tuple_end (struct ui_out *uiout)
382 ui_out_end (uiout, ui_out_type_tuple);
385 struct ui_out_end_cleanup_data
387 struct ui_out *uiout;
388 enum ui_out_type type;
392 do_cleanup_end (void *data)
394 struct ui_out_end_cleanup_data *end_cleanup_data = data;
395 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
396 xfree (end_cleanup_data);
399 static struct cleanup *
400 make_cleanup_ui_out_end (struct ui_out *uiout,
401 enum ui_out_type type)
403 struct ui_out_end_cleanup_data *end_cleanup_data;
404 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
405 end_cleanup_data->uiout = uiout;
406 end_cleanup_data->type = type;
407 return make_cleanup (do_cleanup_end, end_cleanup_data);
411 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
412 enum ui_out_type type,
415 ui_out_begin (uiout, type, id);
416 return make_cleanup_ui_out_end (uiout, type);
420 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
423 ui_out_tuple_begin (uiout, id);
424 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
428 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
431 ui_out_list_begin (uiout, id);
432 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
436 ui_out_field_int (struct ui_out *uiout,
443 struct ui_out_level *current = current_level (uiout);
445 verify_field_proper_position (uiout);
447 current->field_count += 1;
448 fldno = current->field_count;
450 verify_field_alignment (uiout, fldno, &width, &align);
452 uo_field_int (uiout, fldno, width, align, fldname, value);
456 ui_out_field_core_addr (struct ui_out *uiout,
462 /* FIXME-32x64: need a print_address_numeric with field width */
463 /* print_address_numeric (address, 1, local_stream); */
464 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
466 ui_out_field_string (uiout, fldname, addstr);
470 ui_out_field_stream (struct ui_out *uiout,
472 struct ui_stream *buf)
475 char *buffer = ui_file_xstrdup (buf->stream, &length);
476 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
478 ui_out_field_string (uiout, fldname, buffer);
480 ui_out_field_skip (uiout, fldname);
481 ui_file_rewind (buf->stream);
482 do_cleanups (old_cleanup);
485 /* used to ommit a field */
488 ui_out_field_skip (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_skip (uiout, fldno, width, align, fldname);
507 ui_out_field_string (struct ui_out *uiout,
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 verify_field_alignment (uiout, fldno, &width, &align);
523 uo_field_string (uiout, fldno, width, align, fldname, string);
528 ui_out_field_fmt (struct ui_out *uiout,
530 const char *format, ...)
536 struct ui_out_level *current = current_level (uiout);
538 verify_field_proper_position (uiout);
540 current->field_count += 1;
541 fldno = current->field_count;
543 /* will not align, but has to call anyway */
544 verify_field_alignment (uiout, fldno, &width, &align);
546 va_start (args, format);
548 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
554 ui_out_spaces (struct ui_out *uiout, int numspaces)
556 uo_spaces (uiout, numspaces);
560 ui_out_text (struct ui_out *uiout,
563 uo_text (uiout, string);
567 ui_out_message (struct ui_out *uiout, int verbosity,
568 const char *format,...)
572 va_start (args, format);
574 uo_message (uiout, verbosity, format, args);
580 ui_out_stream_new (struct ui_out *uiout)
582 struct ui_stream *tempbuf;
584 tempbuf = XMALLOC (struct ui_stream);
585 tempbuf->uiout = uiout;
586 tempbuf->stream = mem_fileopen ();
591 ui_out_stream_delete (struct ui_stream *buf)
593 ui_file_delete (buf->stream);
598 do_stream_delete (void *buf)
600 ui_out_stream_delete (buf);
604 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
606 return make_cleanup (do_stream_delete, buf);
611 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
613 uo_wrap_hint (uiout, identstring);
617 ui_out_flush (struct ui_out *uiout)
622 /* set the flags specified by the mask given */
624 ui_out_set_flags (struct ui_out *uiout, int mask)
626 int oldflags = uiout->flags;
628 uiout->flags |= mask;
633 /* clear the flags specified by the mask given */
635 ui_out_clear_flags (struct ui_out *uiout, int mask)
637 int oldflags = uiout->flags;
639 uiout->flags &= ~mask;
644 /* test the flags against the mask given */
646 ui_out_test_flags (struct ui_out *uiout, int mask)
648 return (uiout->flags & mask);
651 /* obtain the current verbosity level (as stablished by the
652 'set verbositylevel' command */
655 ui_out_get_verblvl (struct ui_out *uiout)
657 /* FIXME: not implemented yet */
663 ui_out_result_begin (struct ui_out *uiout, char *class)
668 ui_out_result_end (struct ui_out *uiout)
673 ui_out_info_begin (struct ui_out *uiout, char *class)
678 ui_out_info_end (struct ui_out *uiout)
683 ui_out_notify_begin (struct ui_out *uiout, char *class)
688 ui_out_notify_end (struct ui_out *uiout)
693 ui_out_error_begin (struct ui_out *uiout, char *class)
698 ui_out_error_end (struct ui_out *uiout)
705 gdb_error (ui_out * uiout, int severity, char *format,...)
711 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
717 ui_out_is_mi_like_p (struct ui_out *uiout)
719 return uiout->impl->is_mi_like_p;
722 /* default gdb-out hook functions */
725 default_table_begin (struct ui_out *uiout, int nbrofcols,
732 default_table_body (struct ui_out *uiout)
737 default_table_end (struct ui_out *uiout)
742 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
743 const char *col_name,
749 default_begin (struct ui_out *uiout,
750 enum ui_out_type type,
757 default_end (struct ui_out *uiout,
758 enum ui_out_type type,
764 default_field_int (struct ui_out *uiout, int fldno, int width,
766 const char *fldname, int value)
771 default_field_skip (struct ui_out *uiout, int fldno, int width,
772 enum ui_align align, const char *fldname)
777 default_field_string (struct ui_out *uiout,
787 default_field_fmt (struct ui_out *uiout, int fldno, int width,
796 default_spaces (struct ui_out *uiout, int numspaces)
801 default_text (struct ui_out *uiout, const char *string)
806 default_message (struct ui_out *uiout, int verbosity,
813 default_wrap_hint (struct ui_out *uiout, char *identstring)
818 default_flush (struct ui_out *uiout)
822 /* Interface to the implementation functions */
825 uo_table_begin (struct ui_out *uiout, int nbrofcols,
829 if (!uiout->impl->table_begin)
831 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
835 uo_table_body (struct ui_out *uiout)
837 if (!uiout->impl->table_body)
839 uiout->impl->table_body (uiout);
843 uo_table_end (struct ui_out *uiout)
845 if (!uiout->impl->table_end)
847 uiout->impl->table_end (uiout);
851 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
852 const char *col_name,
855 if (!uiout->impl->table_header)
857 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
861 uo_begin (struct ui_out *uiout,
862 enum ui_out_type type,
866 if (uiout->impl->begin == NULL)
868 uiout->impl->begin (uiout, type, level, id);
872 uo_end (struct ui_out *uiout,
873 enum ui_out_type type,
876 if (uiout->impl->end == NULL)
878 uiout->impl->end (uiout, type, level);
882 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
886 if (!uiout->impl->field_int)
888 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
892 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
895 if (!uiout->impl->field_skip)
897 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
901 uo_field_string (struct ui_out *uiout, int fldno, int width,
906 if (!uiout->impl->field_string)
908 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
912 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
917 if (!uiout->impl->field_fmt)
919 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
923 uo_spaces (struct ui_out *uiout, int numspaces)
925 if (!uiout->impl->spaces)
927 uiout->impl->spaces (uiout, numspaces);
931 uo_text (struct ui_out *uiout,
934 if (!uiout->impl->text)
936 uiout->impl->text (uiout, string);
940 uo_message (struct ui_out *uiout, int verbosity,
944 if (!uiout->impl->message)
946 uiout->impl->message (uiout, verbosity, format, args);
950 uo_wrap_hint (struct ui_out *uiout, char *identstring)
952 if (!uiout->impl->wrap_hint)
954 uiout->impl->wrap_hint (uiout, identstring);
958 uo_flush (struct ui_out *uiout)
960 if (!uiout->impl->flush)
962 uiout->impl->flush (uiout);
965 /* local functions */
967 /* list of column headers manipulation routines */
970 clear_header_list (struct ui_out *uiout)
972 while (uiout->headerfirst != NULL)
974 uiout->headercurr = uiout->headerfirst;
975 uiout->headerfirst = uiout->headerfirst->next;
976 if (uiout->headercurr->colhdr != NULL)
977 xfree (uiout->headercurr->colhdr);
978 xfree (uiout->headercurr);
980 uiout->headerlast = NULL;
981 uiout->headercurr = NULL;
985 append_header_to_list (struct ui_out *uiout,
988 const char *col_name,
991 struct ui_out_hdr *temphdr;
993 temphdr = XMALLOC (struct ui_out_hdr);
994 temphdr->width = width;
995 temphdr->alignment = alignment;
996 /* we have to copy the column title as the original may be an automatic */
998 temphdr->colhdr = xstrdup (colhdr);
1000 temphdr->colhdr = NULL;
1001 if (col_name != NULL)
1002 temphdr->col_name = xstrdup (colhdr);
1004 temphdr->col_name = xstrdup (colhdr);
1005 temphdr->next = NULL;
1006 if (uiout->headerfirst == NULL)
1009 uiout->headerfirst = temphdr;
1010 uiout->headerlast = temphdr;
1014 temphdr->colno = uiout->headerlast->colno + 1;
1015 uiout->headerlast->next = temphdr;
1016 uiout->headerlast = temphdr;
1018 uiout->headercurr = uiout->headerlast;
1021 /* returns 0 if there is no more headers */
1024 get_curr_header (struct ui_out *uiout,
1030 /* There may be no headers at all or we may have used all columns */
1031 if (uiout->headercurr == NULL)
1033 *colno = uiout->headercurr->colno;
1034 *width = uiout->headercurr->width;
1035 *alignment = uiout->headercurr->alignment;
1036 *colhdr = uiout->headercurr->colhdr;
1037 uiout->headercurr = uiout->headercurr->next;
1041 /* makes sure the field_* calls were properly placed */
1044 verify_field_proper_position (struct ui_out *uiout)
1046 if (uiout->table_flag)
1048 if (!uiout->body_flag)
1049 internal_error (__FILE__, __LINE__,
1050 "table_body missing; table fields must be \
1051 specified after table_body and inside a list.");
1052 if (uiout->level == 0)
1053 internal_error (__FILE__, __LINE__,
1054 "list_begin missing; table fields must be \
1055 specified after table_body and inside a list.");
1059 /* determines what is the alignment policy */
1062 verify_field_alignment (struct ui_out *uiout,
1070 if (uiout->table_flag
1071 && get_curr_header (uiout, &colno, width, align, &text))
1074 internal_error (__FILE__, __LINE__,
1075 "ui-out internal error in handling headers.");
1080 *align = ui_noalign;
1084 /* access to ui_out format private members */
1087 ui_out_get_field_separator (struct ui_out *uiout)
1091 /* Access to ui-out members data */
1093 struct ui_out_data *
1094 ui_out_data (struct ui_out *uiout)
1099 /* initalize private members at startup */
1102 ui_out_new (struct ui_out_impl *impl,
1103 struct ui_out_data *data,
1106 struct ui_out *uiout = XMALLOC (struct ui_out);
1109 uiout->flags = flags;
1110 uiout->table_flag = 0;
1111 uiout->body_flag = 0;
1113 memset (uiout->levels, 0, sizeof (uiout->levels));
1114 uiout->headerfirst = NULL;
1115 uiout->headerlast = NULL;
1116 uiout->headercurr = NULL;
1120 /* standard gdb initialization hook */
1123 _initialize_ui_out (void)
1125 /* nothing needs to be done */