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 */
58 /* The ui_out structure */
59 /* Any change here requires a corresponding one in the initialization
60 of the default uiout, which is statically initialized */
65 /* specific implementation of ui-out */
66 struct ui_out_impl
*impl
;
67 struct ui_out_data
*data
;
69 /* if on, a table is being generated */
72 /* if on, the body of a table is being generated */
75 /* number of table columns (as specified in the table_begin call) */
78 /* strinf identifying the table (as specified in the table_begin call) */
81 /* Sub structure tracking the table depth. */
83 struct ui_out_level levels
[MAX_UI_OUT_LEVELS
];
85 /* points to the first header (if any) */
86 struct ui_out_hdr
*headerfirst
;
88 /* points to the last header (if any) */
89 struct ui_out_hdr
*headerlast
;
91 /* points to header of next column to format */
92 struct ui_out_hdr
*headercurr
;
96 /* The current (inner most) level. */
97 static struct ui_out_level
*
98 current_level (struct ui_out
*uiout
)
100 return &uiout
->levels
[uiout
->level
];
103 /* Create a new level, of TYPE. Return the new level's index. */
105 push_level (struct ui_out
*uiout
,
108 struct ui_out_level
*current
;
109 /* We had better not overflow the buffer. */
111 gdb_assert (uiout
->level
> 0 && uiout
->level
< MAX_UI_OUT_LEVELS
);
112 current
= current_level (uiout
);
113 current
->field_count
= 0;
117 /* Discard the current level, return the discarded level's index.
118 TYPE is the type of the level being discarded. */
120 pop_level (struct ui_out
*uiout
)
122 /* We had better not underflow the buffer. */
123 gdb_assert (uiout
->level
> 0 && uiout
->level
< MAX_UI_OUT_LEVELS
);
125 return uiout
->level
+ 1;
129 /* These are the default implementation functions */
131 static void default_table_begin (struct ui_out
*uiout
, int nbrofcols
,
133 static void default_table_body (struct ui_out
*uiout
);
134 static void default_table_end (struct ui_out
*uiout
);
135 static void default_table_header (struct ui_out
*uiout
, int width
,
136 enum ui_align alig
, char *colhdr
);
137 static void default_list_begin (struct ui_out
*uiout
, int level
,
139 static void default_list_end (struct ui_out
*uiout
, int level
);
140 static void default_field_int (struct ui_out
*uiout
, int fldno
, int width
,
141 enum ui_align alig
, char *fldname
, int value
);
142 static void default_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
143 enum ui_align alig
, char *fldname
);
144 static void default_field_string (struct ui_out
*uiout
, int fldno
, int width
,
145 enum ui_align align
, char *fldname
,
147 static void default_field_fmt (struct ui_out
*uiout
, int fldno
,
148 int width
, enum ui_align align
,
149 char *fldname
, char *format
, va_list args
);
150 static void default_spaces (struct ui_out
*uiout
, int numspaces
);
151 static void default_text (struct ui_out
*uiout
, char *string
);
152 static void default_message (struct ui_out
*uiout
, int verbosity
, char *format
,
154 static void default_wrap_hint (struct ui_out
*uiout
, char *identstring
);
155 static void default_flush (struct ui_out
*uiout
);
157 /* This is the default ui-out implementation functions vector */
159 struct ui_out_impl default_ui_out_impl
=
164 default_table_header
,
169 default_field_string
,
178 /* The default ui_out */
180 struct ui_out def_uiout
=
183 &default_ui_out_impl
, /* impl */
186 /* Pointer to current ui_out */
187 /* FIXME: This should not be a global, but something passed down from main.c
190 struct ui_out
*uiout
= &def_uiout
;
192 /* These are the interfaces to implementation functions */
194 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
);
195 static void uo_table_body (struct ui_out
*uiout
);
196 static void uo_table_end (struct ui_out
*uiout
);
197 static void uo_table_header (struct ui_out
*uiout
, int width
,
198 enum ui_align align
, char *colhdr
);
199 static void uo_list_begin (struct ui_out
*uiout
, int level
, char *lstid
);
200 static void uo_list_end (struct ui_out
*uiout
, int level
);
201 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
202 enum ui_align align
, char *fldname
, int value
);
203 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
204 enum ui_align align
, char *fldname
);
205 static void uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
206 enum ui_align align
, char *fldname
, const char *string
);
207 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
208 enum ui_align align
, char *fldname
,
209 char *format
, va_list args
);
210 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
211 static void uo_text (struct ui_out
*uiout
, char *string
);
212 static void uo_message (struct ui_out
*uiout
, int verbosity
,
213 char *format
, va_list args
);
214 static void uo_wrap_hint (struct ui_out
*uiout
, char *identstring
);
215 static void uo_flush (struct ui_out
*uiout
);
217 /* Prototypes for local functions */
219 extern void _initialize_ui_out (void);
220 static void append_header_to_list (struct ui_out
*uiout
, int width
, int alignment
, char *colhdr
);
221 static int get_curr_header (struct ui_out
*uiout
, int *colno
, int *width
,
222 int *alignment
, char **colhdr
);
223 static void clear_header_list (struct ui_out
*uiout
);
224 static void verify_field_proper_position (struct ui_out
*uiout
);
225 static void verify_field_alignment (struct ui_out
*uiout
, int fldno
, int *width
, int *alignment
);
227 static void init_ui_out_state (struct ui_out
*uiout
);
229 /* exported functions (ui_out API) */
231 /* Mark beginning of a table */
234 ui_out_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
236 if (uiout
->table_flag
)
237 internal_error (__FILE__
, __LINE__
,
238 "tables cannot be nested; table_begin found before \
239 previous table_end.");
241 uiout
->table_flag
= 1;
242 uiout
->table_columns
= nbrofcols
;
244 uiout
->table_id
= xstrdup (tblid
);
246 uiout
->table_id
= NULL
;
247 clear_header_list (uiout
);
249 uo_table_begin (uiout
, nbrofcols
, uiout
->table_id
);
253 ui_out_table_body (struct ui_out
*uiout
)
255 if (!uiout
->table_flag
)
256 internal_error (__FILE__
, __LINE__
,
257 "table_body outside a table is not valid; it must be \
258 after a table_begin and before a table_end.");
259 if (uiout
->body_flag
)
260 internal_error (__FILE__
, __LINE__
,
261 "extra table_body call not allowed; there must be \
262 only one table_body after a table_begin and before a table_end.");
263 if (uiout
->headercurr
->colno
!= uiout
->table_columns
)
264 internal_error (__FILE__
, __LINE__
,
265 "number of headers differ from number of table \
268 uiout
->body_flag
= 1;
269 uiout
->headercurr
= uiout
->headerfirst
;
271 uo_table_body (uiout
);
275 ui_out_table_end (struct ui_out
*uiout
)
277 if (!uiout
->table_flag
)
278 internal_error (__FILE__
, __LINE__
,
279 "misplaced table_end or missing table_begin.");
281 uiout
->body_flag
= 0;
282 uiout
->table_flag
= 0;
284 uo_table_end (uiout
);
287 xfree (uiout
->table_id
);
288 clear_header_list (uiout
);
292 ui_out_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
295 if (!uiout
->table_flag
|| uiout
->body_flag
)
296 internal_error (__FILE__
, __LINE__
,
297 "table header must be specified after table_begin \
298 and before table_body.");
300 append_header_to_list (uiout
, width
, alignment
, colhdr
);
302 uo_table_header (uiout
, width
, alignment
, colhdr
);
306 ui_out_list_begin (struct ui_out
*uiout
, char *lstid
)
309 if (uiout
->table_flag
&& !uiout
->body_flag
)
310 internal_error (__FILE__
, __LINE__
,
311 "table header or table_body expected; lists must be \
312 specified after table_body.");
313 new_level
= push_level (uiout
, lstid
);
314 if (uiout
->table_flag
&& (new_level
== 1))
315 uiout
->headercurr
= uiout
->headerfirst
;
316 uo_list_begin (uiout
, new_level
, lstid
);
320 ui_out_list_end (struct ui_out
*uiout
)
322 int old_level
= pop_level (uiout
);
323 uo_list_end (uiout
, old_level
);
327 do_list_end (void *uiout
)
329 ui_out_list_end (uiout
);
333 make_cleanup_ui_out_list_end (struct ui_out
*uiout
)
335 return make_cleanup (do_list_end
, uiout
);
339 ui_out_field_int (struct ui_out
*uiout
, char *fldname
, int value
)
344 struct ui_out_level
*current
= current_level (uiout
);
346 verify_field_proper_position (uiout
);
348 current
->field_count
+= 1;
349 fldno
= current
->field_count
;
351 verify_field_alignment (uiout
, fldno
, &width
, &align
);
353 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
357 ui_out_field_core_addr (struct ui_out
*uiout
, char *fldname
, CORE_ADDR address
)
361 /* FIXME-32x64: need a print_address_numeric with field width */
362 /* print_address_numeric (address, 1, local_stream); */
363 strcpy (addstr
, local_hex_string_custom ((unsigned long) address
, "08l"));
365 ui_out_field_string (uiout
, fldname
, addstr
);
369 ui_out_field_stream (struct ui_out
*uiout
, char *fldname
, struct ui_stream
*buf
)
372 char *buffer
= ui_file_xstrdup (buf
->stream
, &length
);
373 struct cleanup
*old_cleanup
= make_cleanup (xfree
, buffer
);
375 ui_out_field_string (uiout
, fldname
, buffer
);
377 ui_out_field_skip (uiout
, fldname
);
378 ui_file_rewind (buf
->stream
);
379 do_cleanups (old_cleanup
);
382 /* used to ommit a field */
385 ui_out_field_skip (struct ui_out
*uiout
, char *fldname
)
390 struct ui_out_level
*current
= current_level (uiout
);
392 verify_field_proper_position (uiout
);
394 current
->field_count
+= 1;
395 fldno
= current
->field_count
;
397 verify_field_alignment (uiout
, fldno
, &width
, &align
);
399 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
403 ui_out_field_string (struct ui_out
*uiout
,
410 struct ui_out_level
*current
= current_level (uiout
);
412 verify_field_proper_position (uiout
);
414 current
->field_count
+= 1;
415 fldno
= current
->field_count
;
417 verify_field_alignment (uiout
, fldno
, &width
, &align
);
419 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
424 ui_out_field_fmt (struct ui_out
*uiout
, char *fldname
, char *format
,...)
430 struct ui_out_level
*current
= current_level (uiout
);
432 verify_field_proper_position (uiout
);
434 current
->field_count
+= 1;
435 fldno
= current
->field_count
;
437 /* will not align, but has to call anyway */
438 verify_field_alignment (uiout
, fldno
, &width
, &align
);
440 va_start (args
, format
);
442 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
448 ui_out_spaces (struct ui_out
*uiout
, int numspaces
)
450 uo_spaces (uiout
, numspaces
);
454 ui_out_text (struct ui_out
*uiout
, char *string
)
456 uo_text (uiout
, string
);
460 ui_out_message (struct ui_out
*uiout
, int verbosity
, char *format
,...)
464 va_start (args
, format
);
466 uo_message (uiout
, verbosity
, format
, args
);
472 ui_out_stream_new (struct ui_out
*uiout
)
474 struct ui_stream
*tempbuf
;
476 tempbuf
= XMALLOC (struct ui_stream
);
477 tempbuf
->uiout
= uiout
;
478 tempbuf
->stream
= mem_fileopen ();
483 ui_out_stream_delete (struct ui_stream
*buf
)
485 ui_file_delete (buf
->stream
);
490 do_stream_delete (void *buf
)
492 ui_out_stream_delete (buf
);
496 make_cleanup_ui_out_stream_delete (struct ui_stream
*buf
)
498 return make_cleanup (do_stream_delete
, buf
);
503 ui_out_wrap_hint (struct ui_out
*uiout
, char *identstring
)
505 uo_wrap_hint (uiout
, identstring
);
509 ui_out_flush (struct ui_out
*uiout
)
514 /* set the flags specified by the mask given */
516 ui_out_set_flags (struct ui_out
*uiout
, int mask
)
518 int oldflags
= uiout
->flags
;
520 uiout
->flags
|= mask
;
525 /* clear the flags specified by the mask given */
527 ui_out_clear_flags (struct ui_out
*uiout
, int mask
)
529 int oldflags
= uiout
->flags
;
531 uiout
->flags
&= ~mask
;
536 /* test the flags against the mask given */
538 ui_out_test_flags (struct ui_out
*uiout
, int mask
)
540 return (uiout
->flags
& mask
);
543 /* obtain the current verbosity level (as stablished by the
544 'set verbositylevel' command */
547 ui_out_get_verblvl (struct ui_out
*uiout
)
549 /* FIXME: not implemented yet */
555 ui_out_result_begin (struct ui_out
*uiout
, char *class)
560 ui_out_result_end (struct ui_out
*uiout
)
565 ui_out_info_begin (struct ui_out
*uiout
, char *class)
570 ui_out_info_end (struct ui_out
*uiout
)
575 ui_out_notify_begin (struct ui_out
*uiout
, char *class)
580 ui_out_notify_end (struct ui_out
*uiout
)
585 ui_out_error_begin (struct ui_out
*uiout
, char *class)
590 ui_out_error_end (struct ui_out
*uiout
)
597 gdb_error (ui_out
* uiout
, int severity
, char *format
,...)
603 gdb_query (struct ui_out
*uiout
, int qflags
, char *qprompt
)
608 /* default gdb-out hook functions */
611 default_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
616 default_table_body (struct ui_out
*uiout
)
621 default_table_end (struct ui_out
*uiout
)
626 default_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
632 default_list_begin (struct ui_out
*uiout
, int level
, char *lstid
)
637 default_list_end (struct ui_out
*uiout
, int level
)
642 default_field_int (struct ui_out
*uiout
, int fldno
, int width
,
643 enum ui_align align
, char *fldname
, int value
)
648 default_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
649 enum ui_align align
, char *fldname
)
654 default_field_string (struct ui_out
*uiout
,
664 default_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
665 enum ui_align align
, char *fldname
, char *format
,
671 default_spaces (struct ui_out
*uiout
, int numspaces
)
676 default_text (struct ui_out
*uiout
, char *string
)
681 default_message (struct ui_out
*uiout
, int verbosity
, char *format
,
687 default_wrap_hint (struct ui_out
*uiout
, char *identstring
)
692 default_flush (struct ui_out
*uiout
)
696 /* Interface to the implementation functions */
699 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
701 if (!uiout
->impl
->table_begin
)
703 uiout
->impl
->table_begin (uiout
, nbrofcols
, tblid
);
707 uo_table_body (struct ui_out
*uiout
)
709 if (!uiout
->impl
->table_body
)
711 uiout
->impl
->table_body (uiout
);
715 uo_table_end (struct ui_out
*uiout
)
717 if (!uiout
->impl
->table_end
)
719 uiout
->impl
->table_end (uiout
);
723 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
, char *colhdr
)
725 if (!uiout
->impl
->table_header
)
727 uiout
->impl
->table_header (uiout
, width
, align
, colhdr
);
731 uo_list_begin (struct ui_out
*uiout
, int level
, char *lstid
)
733 if (!uiout
->impl
->list_begin
)
735 uiout
->impl
->list_begin (uiout
, level
, lstid
);
739 uo_list_end (struct ui_out
*uiout
, int level
)
741 if (!uiout
->impl
->list_end
)
743 uiout
->impl
->list_end (uiout
, level
);
747 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
, int value
)
749 if (!uiout
->impl
->field_int
)
751 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
755 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
)
757 if (!uiout
->impl
->field_skip
)
759 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
763 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
764 enum ui_align align
, char *fldname
, const char *string
)
766 if (!uiout
->impl
->field_string
)
768 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
772 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
, char *format
, va_list args
)
774 if (!uiout
->impl
->field_fmt
)
776 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
780 uo_spaces (struct ui_out
*uiout
, int numspaces
)
782 if (!uiout
->impl
->spaces
)
784 uiout
->impl
->spaces (uiout
, numspaces
);
788 uo_text (struct ui_out
*uiout
, char *string
)
790 if (!uiout
->impl
->text
)
792 uiout
->impl
->text (uiout
, string
);
796 uo_message (struct ui_out
*uiout
, int verbosity
, char *format
, va_list args
)
798 if (!uiout
->impl
->message
)
800 uiout
->impl
->message (uiout
, verbosity
, format
, args
);
804 uo_wrap_hint (struct ui_out
*uiout
, char *identstring
)
806 if (!uiout
->impl
->wrap_hint
)
808 uiout
->impl
->wrap_hint (uiout
, identstring
);
812 uo_flush (struct ui_out
*uiout
)
814 if (!uiout
->impl
->flush
)
816 uiout
->impl
->flush (uiout
);
819 /* local functions */
821 /* list of column headers manipulation routines */
824 clear_header_list (struct ui_out
*uiout
)
826 while (uiout
->headerfirst
!= NULL
)
828 uiout
->headercurr
= uiout
->headerfirst
;
829 uiout
->headerfirst
= uiout
->headerfirst
->next
;
830 if (uiout
->headercurr
->colhdr
!= NULL
)
831 xfree (uiout
->headercurr
->colhdr
);
832 xfree (uiout
->headercurr
);
834 uiout
->headerlast
= NULL
;
835 uiout
->headercurr
= NULL
;
839 append_header_to_list (struct ui_out
*uiout
,
844 struct ui_out_hdr
*temphdr
;
846 temphdr
= XMALLOC (struct ui_out_hdr
);
847 temphdr
->width
= width
;
848 temphdr
->alignment
= alignment
;
849 /* we have to copy the column title as the original may be an automatic */
852 temphdr
->colhdr
= xmalloc (strlen (colhdr
) + 1);
853 strcpy (temphdr
->colhdr
, colhdr
);
855 temphdr
->next
= NULL
;
856 if (uiout
->headerfirst
== NULL
)
859 uiout
->headerfirst
= temphdr
;
860 uiout
->headerlast
= temphdr
;
864 temphdr
->colno
= uiout
->headerlast
->colno
+ 1;
865 uiout
->headerlast
->next
= temphdr
;
866 uiout
->headerlast
= temphdr
;
868 uiout
->headercurr
= uiout
->headerlast
;
871 /* returns 0 if there is no more headers */
874 get_curr_header (struct ui_out
*uiout
,
880 /* There may be no headers at all or we may have used all columns */
881 if (uiout
->headercurr
== NULL
)
883 *colno
= uiout
->headercurr
->colno
;
884 *width
= uiout
->headercurr
->width
;
885 *alignment
= uiout
->headercurr
->alignment
;
886 *colhdr
= uiout
->headercurr
->colhdr
;
887 uiout
->headercurr
= uiout
->headercurr
->next
;
891 /* makes sure the field_* calls were properly placed */
894 verify_field_proper_position (struct ui_out
*uiout
)
896 if (uiout
->table_flag
)
898 if (!uiout
->body_flag
)
899 internal_error (__FILE__
, __LINE__
,
900 "table_body missing; table fields must be \
901 specified after table_body and inside a list.");
902 if (uiout
->level
== 0)
903 internal_error (__FILE__
, __LINE__
,
904 "list_begin missing; table fields must be \
905 specified after table_body and inside a list.");
909 /* determines what is the alignment policy */
912 verify_field_alignment (struct ui_out
*uiout
,
920 if (uiout
->table_flag
921 && get_curr_header (uiout
, &colno
, width
, align
, &text
))
924 internal_error (__FILE__
, __LINE__
,
925 "ui-out internal error in handling headers.");
934 /* access to ui_out format private members */
937 ui_out_get_field_separator (struct ui_out
*uiout
)
941 /* Access to ui-out members data */
944 ui_out_data (struct ui_out
*uiout
)
949 /* initalize private members at startup */
952 ui_out_new (struct ui_out_impl
*impl
,
953 struct ui_out_data
*data
,
956 struct ui_out
*uiout
= XMALLOC (struct ui_out
);
959 uiout
->flags
= flags
;
960 uiout
->table_flag
= 0;
961 uiout
->body_flag
= 0;
963 memset (uiout
->levels
, 0, sizeof (uiout
->levels
));
964 uiout
->headerfirst
= NULL
;
965 uiout
->headerlast
= NULL
;
966 uiout
->headercurr
= NULL
;
970 /* standard gdb initialization hook */
973 _initialize_ui_out (void)
975 /* nothing needs to be done */