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
,
142 enum ui_align alig
, char *colhdr
);
143 static void default_begin (struct ui_out
*uiout
,
144 enum ui_out_type type
,
145 int level
, const char *id
);
146 static void default_end (struct ui_out
*uiout
,
147 enum ui_out_type type
,
149 static void default_field_int (struct ui_out
*uiout
, int fldno
, int width
,
150 enum ui_align alig
, char *fldname
, int value
);
151 static void default_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
152 enum ui_align alig
, char *fldname
);
153 static void default_field_string (struct ui_out
*uiout
, int fldno
, int width
,
154 enum ui_align align
, char *fldname
,
156 static void default_field_fmt (struct ui_out
*uiout
, int fldno
,
157 int width
, enum ui_align align
,
158 char *fldname
, char *format
, va_list args
);
159 static void default_spaces (struct ui_out
*uiout
, int numspaces
);
160 static void default_text (struct ui_out
*uiout
, char *string
);
161 static void default_message (struct ui_out
*uiout
, int verbosity
, char *format
,
163 static void default_wrap_hint (struct ui_out
*uiout
, char *identstring
);
164 static void default_flush (struct ui_out
*uiout
);
166 /* This is the default ui-out implementation functions vector */
168 struct ui_out_impl default_ui_out_impl
=
173 default_table_header
,
178 default_field_string
,
187 /* The default ui_out */
189 struct ui_out def_uiout
=
192 &default_ui_out_impl
, /* impl */
195 /* Pointer to current ui_out */
196 /* FIXME: This should not be a global, but something passed down from main.c
199 struct ui_out
*uiout
= &def_uiout
;
201 /* These are the interfaces to implementation functions */
203 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
);
204 static void uo_table_body (struct ui_out
*uiout
);
205 static void uo_table_end (struct ui_out
*uiout
);
206 static void uo_table_header (struct ui_out
*uiout
, int width
,
207 enum ui_align align
, char *colhdr
);
208 static void uo_begin (struct ui_out
*uiout
,
209 enum ui_out_type type
,
210 int level
, const char *id
);
211 static void uo_end (struct ui_out
*uiout
,
212 enum ui_out_type type
,
214 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
215 enum ui_align align
, char *fldname
, int value
);
216 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
217 enum ui_align align
, char *fldname
);
218 static void uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
219 enum ui_align align
, char *fldname
, const char *string
);
220 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
221 enum ui_align align
, char *fldname
,
222 char *format
, va_list args
);
223 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
224 static void uo_text (struct ui_out
*uiout
, char *string
);
225 static void uo_message (struct ui_out
*uiout
, int verbosity
,
226 char *format
, va_list args
);
227 static void uo_wrap_hint (struct ui_out
*uiout
, char *identstring
);
228 static void uo_flush (struct ui_out
*uiout
);
230 /* Prototypes for local functions */
232 extern void _initialize_ui_out (void);
233 static void append_header_to_list (struct ui_out
*uiout
, int width
, int alignment
, char *colhdr
);
234 static int get_curr_header (struct ui_out
*uiout
, int *colno
, int *width
,
235 int *alignment
, char **colhdr
);
236 static void clear_header_list (struct ui_out
*uiout
);
237 static void verify_field_proper_position (struct ui_out
*uiout
);
238 static void verify_field_alignment (struct ui_out
*uiout
, int fldno
, int *width
, int *alignment
);
240 static void init_ui_out_state (struct ui_out
*uiout
);
242 /* exported functions (ui_out API) */
244 /* Mark beginning of a table */
247 ui_out_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
249 if (uiout
->table_flag
)
250 internal_error (__FILE__
, __LINE__
,
251 "tables cannot be nested; table_begin found before \
252 previous table_end.");
254 uiout
->table_flag
= 1;
255 uiout
->table_columns
= nbrofcols
;
257 uiout
->table_id
= xstrdup (tblid
);
259 uiout
->table_id
= NULL
;
260 clear_header_list (uiout
);
262 uo_table_begin (uiout
, nbrofcols
, uiout
->table_id
);
266 ui_out_table_body (struct ui_out
*uiout
)
268 if (!uiout
->table_flag
)
269 internal_error (__FILE__
, __LINE__
,
270 "table_body outside a table is not valid; it must be \
271 after a table_begin and before a table_end.");
272 if (uiout
->body_flag
)
273 internal_error (__FILE__
, __LINE__
,
274 "extra table_body call not allowed; there must be \
275 only one table_body after a table_begin and before a table_end.");
276 if (uiout
->headercurr
->colno
!= uiout
->table_columns
)
277 internal_error (__FILE__
, __LINE__
,
278 "number of headers differ from number of table \
281 uiout
->body_flag
= 1;
282 uiout
->headercurr
= uiout
->headerfirst
;
284 uo_table_body (uiout
);
288 ui_out_table_end (struct ui_out
*uiout
)
290 if (!uiout
->table_flag
)
291 internal_error (__FILE__
, __LINE__
,
292 "misplaced table_end or missing table_begin.");
294 uiout
->body_flag
= 0;
295 uiout
->table_flag
= 0;
297 uo_table_end (uiout
);
300 xfree (uiout
->table_id
);
301 clear_header_list (uiout
);
305 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
308 if (!uiout
->table_flag
|| uiout
->body_flag
)
309 internal_error (__FILE__
, __LINE__
,
310 "table header must be specified after table_begin \
311 and before table_body.");
313 append_header_to_list (uiout
, width
, alignment
, colhdr
);
315 uo_table_header (uiout
, width
, alignment
, colhdr
);
319 ui_out_begin (struct ui_out
*uiout
,
320 enum ui_out_type type
,
324 if (uiout
->table_flag
&& !uiout
->body_flag
)
325 internal_error (__FILE__
, __LINE__
,
326 "table header or table_body expected; lists must be \
327 specified after table_body.");
328 new_level
= push_level (uiout
, type
, id
);
329 if (uiout
->table_flag
&& (new_level
== 1))
330 uiout
->headercurr
= uiout
->headerfirst
;
331 uo_begin (uiout
, type
, new_level
, id
);
335 ui_out_list_begin (struct ui_out
*uiout
,
338 ui_out_begin (uiout
, ui_out_type_list
, id
);
342 ui_out_end (struct ui_out
*uiout
,
343 enum ui_out_type type
)
345 int old_level
= pop_level (uiout
, type
);
346 uo_end (uiout
, type
, old_level
);
350 ui_out_list_end (struct ui_out
*uiout
)
352 ui_out_end (uiout
, ui_out_type_list
);
356 do_list_end (void *uiout
)
358 ui_out_list_end (uiout
);
362 make_cleanup_ui_out_list_end (struct ui_out
*uiout
)
364 return make_cleanup (do_list_end
, uiout
);
368 ui_out_field_int (struct ui_out
*uiout
, char *fldname
, int value
)
373 struct ui_out_level
*current
= current_level (uiout
);
375 verify_field_proper_position (uiout
);
377 current
->field_count
+= 1;
378 fldno
= current
->field_count
;
380 verify_field_alignment (uiout
, fldno
, &width
, &align
);
382 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
386 ui_out_field_core_addr (struct ui_out
*uiout
, char *fldname
, CORE_ADDR address
)
390 /* FIXME-32x64: need a print_address_numeric with field width */
391 /* print_address_numeric (address, 1, local_stream); */
392 strcpy (addstr
, local_hex_string_custom ((unsigned long) address
, "08l"));
394 ui_out_field_string (uiout
, fldname
, addstr
);
398 ui_out_field_stream (struct ui_out
*uiout
, char *fldname
, struct ui_stream
*buf
)
401 char *buffer
= ui_file_xstrdup (buf
->stream
, &length
);
402 struct cleanup
*old_cleanup
= make_cleanup (xfree
, buffer
);
404 ui_out_field_string (uiout
, fldname
, buffer
);
406 ui_out_field_skip (uiout
, fldname
);
407 ui_file_rewind (buf
->stream
);
408 do_cleanups (old_cleanup
);
411 /* used to ommit a field */
414 ui_out_field_skip (struct ui_out
*uiout
, char *fldname
)
419 struct ui_out_level
*current
= current_level (uiout
);
421 verify_field_proper_position (uiout
);
423 current
->field_count
+= 1;
424 fldno
= current
->field_count
;
426 verify_field_alignment (uiout
, fldno
, &width
, &align
);
428 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
432 ui_out_field_string (struct ui_out
*uiout
,
439 struct ui_out_level
*current
= current_level (uiout
);
441 verify_field_proper_position (uiout
);
443 current
->field_count
+= 1;
444 fldno
= current
->field_count
;
446 verify_field_alignment (uiout
, fldno
, &width
, &align
);
448 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
453 ui_out_field_fmt (struct ui_out
*uiout
, char *fldname
, char *format
,...)
459 struct ui_out_level
*current
= current_level (uiout
);
461 verify_field_proper_position (uiout
);
463 current
->field_count
+= 1;
464 fldno
= current
->field_count
;
466 /* will not align, but has to call anyway */
467 verify_field_alignment (uiout
, fldno
, &width
, &align
);
469 va_start (args
, format
);
471 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
477 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
479 uo_spaces (uiout
, numspaces
);
483 ui_out_text (struct ui_out
*uiout
, char *string
)
485 uo_text (uiout
, string
);
489 ui_out_message (struct ui_out
*uiout
, int verbosity
, char *format
,...)
493 va_start (args
, format
);
495 uo_message (uiout
, verbosity
, format
, args
);
501 ui_out_stream_new (struct ui_out
*uiout
)
503 struct ui_stream
*tempbuf
;
505 tempbuf
= XMALLOC (struct ui_stream
);
506 tempbuf
->uiout
= uiout
;
507 tempbuf
->stream
= mem_fileopen ();
512 ui_out_stream_delete (struct ui_stream
*buf
)
514 ui_file_delete (buf
->stream
);
519 do_stream_delete (void *buf
)
521 ui_out_stream_delete (buf
);
525 make_cleanup_ui_out_stream_delete (struct ui_stream
*buf
)
527 return make_cleanup (do_stream_delete
, buf
);
532 ui_out_wrap_hint (struct ui_out
*uiout
, char *identstring
)
534 uo_wrap_hint (uiout
, identstring
);
538 ui_out_flush (struct ui_out
*uiout
)
543 /* set the flags specified by the mask given */
545 ui_out_set_flags (struct ui_out
*uiout
, int mask
)
547 int oldflags
= uiout
->flags
;
549 uiout
->flags
|= mask
;
554 /* clear the flags specified by the mask given */
556 ui_out_clear_flags (struct ui_out
*uiout
, int mask
)
558 int oldflags
= uiout
->flags
;
560 uiout
->flags
&= ~mask
;
565 /* test the flags against the mask given */
567 ui_out_test_flags (struct ui_out
*uiout
, int mask
)
569 return (uiout
->flags
& mask
);
572 /* obtain the current verbosity level (as stablished by the
573 'set verbositylevel' command */
576 ui_out_get_verblvl (struct ui_out
*uiout
)
578 /* FIXME: not implemented yet */
584 ui_out_result_begin (struct ui_out
*uiout
, char *class)
589 ui_out_result_end (struct ui_out
*uiout
)
594 ui_out_info_begin (struct ui_out
*uiout
, char *class)
599 ui_out_info_end (struct ui_out
*uiout
)
604 ui_out_notify_begin (struct ui_out
*uiout
, char *class)
609 ui_out_notify_end (struct ui_out
*uiout
)
614 ui_out_error_begin (struct ui_out
*uiout
, char *class)
619 ui_out_error_end (struct ui_out
*uiout
)
626 gdb_error (ui_out
* uiout
, int severity
, char *format
,...)
632 gdb_query (struct ui_out
*uiout
, int qflags
, char *qprompt
)
637 /* default gdb-out hook functions */
640 default_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
645 default_table_body (struct ui_out
*uiout
)
650 default_table_end (struct ui_out
*uiout
)
655 default_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
661 default_begin (struct ui_out
*uiout
,
662 enum ui_out_type type
,
669 default_end (struct ui_out
*uiout
,
670 enum ui_out_type type
,
676 default_field_int (struct ui_out
*uiout
, int fldno
, int width
,
677 enum ui_align align
, char *fldname
, int value
)
682 default_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
683 enum ui_align align
, char *fldname
)
688 default_field_string (struct ui_out
*uiout
,
698 default_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
699 enum ui_align align
, char *fldname
, char *format
,
705 default_spaces (struct ui_out
*uiout
, int numspaces
)
710 default_text (struct ui_out
*uiout
, char *string
)
715 default_message (struct ui_out
*uiout
, int verbosity
, char *format
,
721 default_wrap_hint (struct ui_out
*uiout
, char *identstring
)
726 default_flush (struct ui_out
*uiout
)
730 /* Interface to the implementation functions */
733 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
735 if (!uiout
->impl
->table_begin
)
737 uiout
->impl
->table_begin (uiout
, nbrofcols
, tblid
);
741 uo_table_body (struct ui_out
*uiout
)
743 if (!uiout
->impl
->table_body
)
745 uiout
->impl
->table_body (uiout
);
749 uo_table_end (struct ui_out
*uiout
)
751 if (!uiout
->impl
->table_end
)
753 uiout
->impl
->table_end (uiout
);
757 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
, char *colhdr
)
759 if (!uiout
->impl
->table_header
)
761 uiout
->impl
->table_header (uiout
, width
, align
, colhdr
);
765 uo_begin (struct ui_out
*uiout
,
766 enum ui_out_type type
,
770 if (uiout
->impl
->begin
== NULL
)
772 uiout
->impl
->begin (uiout
, type
, level
, id
);
776 uo_end (struct ui_out
*uiout
,
777 enum ui_out_type type
,
780 if (uiout
->impl
->end
== NULL
)
782 uiout
->impl
->end (uiout
, type
, level
);
786 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
, int value
)
788 if (!uiout
->impl
->field_int
)
790 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
794 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
)
796 if (!uiout
->impl
->field_skip
)
798 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
802 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
803 enum ui_align align
, char *fldname
, const char *string
)
805 if (!uiout
->impl
->field_string
)
807 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
811 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
, char *format
, va_list args
)
813 if (!uiout
->impl
->field_fmt
)
815 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
819 uo_spaces (struct ui_out
*uiout
, int numspaces
)
821 if (!uiout
->impl
->spaces
)
823 uiout
->impl
->spaces (uiout
, numspaces
);
827 uo_text (struct ui_out
*uiout
, char *string
)
829 if (!uiout
->impl
->text
)
831 uiout
->impl
->text (uiout
, string
);
835 uo_message (struct ui_out
*uiout
, int verbosity
, char *format
, va_list args
)
837 if (!uiout
->impl
->message
)
839 uiout
->impl
->message (uiout
, verbosity
, format
, args
);
843 uo_wrap_hint (struct ui_out
*uiout
, char *identstring
)
845 if (!uiout
->impl
->wrap_hint
)
847 uiout
->impl
->wrap_hint (uiout
, identstring
);
851 uo_flush (struct ui_out
*uiout
)
853 if (!uiout
->impl
->flush
)
855 uiout
->impl
->flush (uiout
);
858 /* local functions */
860 /* list of column headers manipulation routines */
863 clear_header_list (struct ui_out
*uiout
)
865 while (uiout
->headerfirst
!= NULL
)
867 uiout
->headercurr
= uiout
->headerfirst
;
868 uiout
->headerfirst
= uiout
->headerfirst
->next
;
869 if (uiout
->headercurr
->colhdr
!= NULL
)
870 xfree (uiout
->headercurr
->colhdr
);
871 xfree (uiout
->headercurr
);
873 uiout
->headerlast
= NULL
;
874 uiout
->headercurr
= NULL
;
878 append_header_to_list (struct ui_out
*uiout
,
883 struct ui_out_hdr
*temphdr
;
885 temphdr
= XMALLOC (struct ui_out_hdr
);
886 temphdr
->width
= width
;
887 temphdr
->alignment
= alignment
;
888 /* we have to copy the column title as the original may be an automatic */
891 temphdr
->colhdr
= xmalloc (strlen (colhdr
) + 1);
892 strcpy (temphdr
->colhdr
, colhdr
);
894 temphdr
->next
= NULL
;
895 if (uiout
->headerfirst
== NULL
)
898 uiout
->headerfirst
= temphdr
;
899 uiout
->headerlast
= temphdr
;
903 temphdr
->colno
= uiout
->headerlast
->colno
+ 1;
904 uiout
->headerlast
->next
= temphdr
;
905 uiout
->headerlast
= temphdr
;
907 uiout
->headercurr
= uiout
->headerlast
;
910 /* returns 0 if there is no more headers */
913 get_curr_header (struct ui_out
*uiout
,
919 /* There may be no headers at all or we may have used all columns */
920 if (uiout
->headercurr
== NULL
)
922 *colno
= uiout
->headercurr
->colno
;
923 *width
= uiout
->headercurr
->width
;
924 *alignment
= uiout
->headercurr
->alignment
;
925 *colhdr
= uiout
->headercurr
->colhdr
;
926 uiout
->headercurr
= uiout
->headercurr
->next
;
930 /* makes sure the field_* calls were properly placed */
933 verify_field_proper_position (struct ui_out
*uiout
)
935 if (uiout
->table_flag
)
937 if (!uiout
->body_flag
)
938 internal_error (__FILE__
, __LINE__
,
939 "table_body missing; table fields must be \
940 specified after table_body and inside a list.");
941 if (uiout
->level
== 0)
942 internal_error (__FILE__
, __LINE__
,
943 "list_begin missing; table fields must be \
944 specified after table_body and inside a list.");
948 /* determines what is the alignment policy */
951 verify_field_alignment (struct ui_out
*uiout
,
959 if (uiout
->table_flag
960 && get_curr_header (uiout
, &colno
, width
, align
, &text
))
963 internal_error (__FILE__
, __LINE__
,
964 "ui-out internal error in handling headers.");
973 /* access to ui_out format private members */
976 ui_out_get_field_separator (struct ui_out
*uiout
)
980 /* Access to ui-out members data */
983 ui_out_data (struct ui_out
*uiout
)
988 /* initalize private members at startup */
991 ui_out_new (struct ui_out_impl
*impl
,
992 struct ui_out_data
*data
,
995 struct ui_out
*uiout
= XMALLOC (struct ui_out
);
998 uiout
->flags
= flags
;
999 uiout
->table_flag
= 0;
1000 uiout
->body_flag
= 0;
1002 memset (uiout
->levels
, 0, sizeof (uiout
->levels
));
1003 uiout
->headerfirst
= NULL
;
1004 uiout
->headerlast
= NULL
;
1005 uiout
->headercurr
= NULL
;
1009 /* standard gdb initialization hook */
1012 _initialize_ui_out (void)
1014 /* nothing needs to be done */