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 /* Tables are special. Maintain a separate structure that tracks
62 their state. At present an output can only contain a single table
63 but that restriction might eventually be lifted. */
67 /* If on, a table is being generated. */
70 /* If on, the body of a table is being generated. If off, the table
71 header is being generated. */
74 /* The level at which each entry of the table is to be found. A row
75 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
76 above that of the table. */
79 /* Number of table columns (as specified in the table_begin call). */
82 /* String identifying the table (as specified in the table_begin
86 /* Points to the first table header (if any). */
87 struct ui_out_hdr *header_first;
89 /* Points to the last table header (if any). */
90 struct ui_out_hdr *header_last;
92 /* Points to header of NEXT column to format. */
93 struct ui_out_hdr *header_next;
98 /* The ui_out structure */
99 /* Any change here requires a corresponding one in the initialization
100 of the default uiout, which is statically initialized */
105 /* specific implementation of ui-out */
106 struct ui_out_impl *impl;
107 struct ui_out_data *data;
109 /* Sub structure tracking the ui-out depth. */
111 struct ui_out_level levels[MAX_UI_OUT_LEVELS];
113 /* A table, if any. At present only a single table is supported. */
114 struct ui_out_table table;
117 /* The current (inner most) level. */
118 static struct ui_out_level *
119 current_level (struct ui_out *uiout)
121 return &uiout->levels[uiout->level];
124 /* Create a new level, of TYPE. Return the new level's index. */
126 push_level (struct ui_out *uiout,
127 enum ui_out_type type,
130 struct ui_out_level *current;
131 /* We had better not overflow the buffer. */
133 gdb_assert (uiout->level >= 0 && uiout->level < MAX_UI_OUT_LEVELS);
134 current = current_level (uiout);
135 current->field_count = 0;
136 current->type = type;
140 /* Discard the current level, return the discarded level's index.
141 TYPE is the type of the level being discarded. */
143 pop_level (struct ui_out *uiout,
144 enum ui_out_type type)
146 /* We had better not underflow the buffer. */
147 gdb_assert (uiout->level > 0 && uiout->level < MAX_UI_OUT_LEVELS);
148 gdb_assert (current_level (uiout)->type == type);
150 return uiout->level + 1;
154 /* These are the default implementation functions */
156 static void default_table_begin (struct ui_out *uiout, int nbrofcols,
157 int nr_rows, const char *tblid);
158 static void default_table_body (struct ui_out *uiout);
159 static void default_table_end (struct ui_out *uiout);
160 static void default_table_header (struct ui_out *uiout, int width,
161 enum ui_align alig, const char *col_name,
163 static void default_begin (struct ui_out *uiout,
164 enum ui_out_type type,
165 int level, const char *id);
166 static void default_end (struct ui_out *uiout,
167 enum ui_out_type type,
169 static void default_field_int (struct ui_out *uiout, int fldno, int width,
173 static void default_field_skip (struct ui_out *uiout, int fldno, int width,
175 const char *fldname);
176 static void default_field_string (struct ui_out *uiout, int fldno, int width,
180 static void default_field_fmt (struct ui_out *uiout, int fldno,
181 int width, enum ui_align align,
185 static void default_spaces (struct ui_out *uiout, int numspaces);
186 static void default_text (struct ui_out *uiout, const char *string);
187 static void default_message (struct ui_out *uiout, int verbosity,
190 static void default_wrap_hint (struct ui_out *uiout, char *identstring);
191 static void default_flush (struct ui_out *uiout);
193 /* This is the default ui-out implementation functions vector */
195 struct ui_out_impl default_ui_out_impl =
200 default_table_header,
205 default_field_string,
212 0, /* Does not need MI hacks. */
215 /* The default ui_out */
217 struct ui_out def_uiout =
220 &default_ui_out_impl, /* impl */
223 /* Pointer to current ui_out */
224 /* FIXME: This should not be a global, but something passed down from main.c
227 struct ui_out *uiout = &def_uiout;
229 /* These are the interfaces to implementation functions */
231 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
232 int nr_rows, const char *tblid);
233 static void uo_table_body (struct ui_out *uiout);
234 static void uo_table_end (struct ui_out *uiout);
235 static void uo_table_header (struct ui_out *uiout, int width,
236 enum ui_align align, const char *col_name,
238 static void uo_begin (struct ui_out *uiout,
239 enum ui_out_type type,
240 int level, const char *id);
241 static void uo_end (struct ui_out *uiout,
242 enum ui_out_type type,
244 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
245 enum ui_align align, const char *fldname, int value);
246 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
247 enum ui_align align, const char *fldname);
248 static void uo_field_string (struct ui_out *uiout, int fldno, int width,
249 enum ui_align align, const char *fldname,
251 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
252 enum ui_align align, const char *fldname,
253 const char *format, va_list args);
254 static void uo_spaces (struct ui_out *uiout, int numspaces);
255 static void uo_text (struct ui_out *uiout, const char *string);
256 static void uo_message (struct ui_out *uiout, int verbosity,
257 const char *format, va_list args);
258 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
259 static void uo_flush (struct ui_out *uiout);
261 /* Prototypes for local functions */
263 extern void _initialize_ui_out (void);
264 static void append_header_to_list (struct ui_out *uiout, int width,
265 int alignment, const char *col_name,
267 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
268 int *alignment, char **colhdr);
269 static void clear_header_list (struct ui_out *uiout);
270 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
273 static void init_ui_out_state (struct ui_out *uiout);
275 /* exported functions (ui_out API) */
277 /* Mark beginning of a table */
280 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
284 if (uiout->table.flag)
285 internal_error (__FILE__, __LINE__,
286 "tables cannot be nested; table_begin found before \
287 previous table_end.");
289 uiout->table.flag = 1;
290 uiout->table.body_flag = 0;
291 uiout->table.entry_level = uiout->level + 1;
292 uiout->table.columns = nbrofcols;
294 uiout->table.id = xstrdup (tblid);
296 uiout->table.id = NULL;
297 clear_header_list (uiout);
299 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id);
303 ui_out_table_body (struct ui_out *uiout)
305 if (!uiout->table.flag)
306 internal_error (__FILE__, __LINE__,
307 "table_body outside a table is not valid; it must be \
308 after a table_begin and before a table_end.");
309 if (uiout->table.body_flag)
310 internal_error (__FILE__, __LINE__,
311 "extra table_body call not allowed; there must be \
312 only one table_body after a table_begin and before a table_end.");
313 if (uiout->table.header_next->colno != uiout->table.columns)
314 internal_error (__FILE__, __LINE__,
315 "number of headers differ from number of table \
318 uiout->table.body_flag = 1;
319 uiout->table.header_next = uiout->table.header_first;
321 uo_table_body (uiout);
325 ui_out_table_end (struct ui_out *uiout)
327 if (!uiout->table.flag)
328 internal_error (__FILE__, __LINE__,
329 "misplaced table_end or missing table_begin.");
331 uiout->table.entry_level = 0;
332 uiout->table.body_flag = 0;
333 uiout->table.flag = 0;
335 uo_table_end (uiout);
338 xfree (uiout->table.id);
339 clear_header_list (uiout);
343 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
344 const char *col_name,
347 if (!uiout->table.flag || uiout->table.body_flag)
348 internal_error (__FILE__, __LINE__,
349 "table header must be specified after table_begin \
350 and before table_body.");
352 append_header_to_list (uiout, width, alignment, col_name, colhdr);
354 uo_table_header (uiout, width, alignment, col_name, colhdr);
358 ui_out_begin (struct ui_out *uiout,
359 enum ui_out_type type,
363 if (uiout->table.flag && !uiout->table.body_flag)
364 internal_error (__FILE__, __LINE__,
365 "table header or table_body expected; lists must be \
366 specified after table_body.");
368 /* Be careful to verify the ``field'' before the new tuple/list is
369 pushed onto the stack. That way the containing list/table/row is
370 verified and not the newly created tuple/list. This verification
371 is needed (at least) for the case where a table row entry
372 contains either a tuple/list. For that case bookkeeping such as
373 updating the column count or advancing to the next heading still
374 needs to be performed. */
379 verify_field (uiout, &fldno, &width, &align);
382 new_level = push_level (uiout, type, id);
384 /* If the push puts us at the same level as a table row entry, we've
385 got a new table row. Put the header pointer back to the start. */
386 if (uiout->table.body_flag
387 && uiout->table.entry_level == new_level)
388 uiout->table.header_next = uiout->table.header_first;
390 uo_begin (uiout, type, new_level, id);
394 ui_out_list_begin (struct ui_out *uiout,
397 ui_out_begin (uiout, ui_out_type_list, id);
401 ui_out_tuple_begin (struct ui_out *uiout, const char *id)
403 ui_out_begin (uiout, ui_out_type_tuple, id);
407 ui_out_end (struct ui_out *uiout,
408 enum ui_out_type type)
410 int old_level = pop_level (uiout, type);
411 uo_end (uiout, type, old_level);
415 ui_out_list_end (struct ui_out *uiout)
417 ui_out_end (uiout, ui_out_type_list);
421 ui_out_tuple_end (struct ui_out *uiout)
423 ui_out_end (uiout, ui_out_type_tuple);
426 struct ui_out_end_cleanup_data
428 struct ui_out *uiout;
429 enum ui_out_type type;
433 do_cleanup_end (void *data)
435 struct ui_out_end_cleanup_data *end_cleanup_data = data;
436 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
437 xfree (end_cleanup_data);
440 static struct cleanup *
441 make_cleanup_ui_out_end (struct ui_out *uiout,
442 enum ui_out_type type)
444 struct ui_out_end_cleanup_data *end_cleanup_data;
445 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
446 end_cleanup_data->uiout = uiout;
447 end_cleanup_data->type = type;
448 return make_cleanup (do_cleanup_end, end_cleanup_data);
452 make_cleanup_ui_out_begin_end (struct ui_out *uiout,
453 enum ui_out_type type,
456 ui_out_begin (uiout, type, id);
457 return make_cleanup_ui_out_end (uiout, type);
461 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
464 ui_out_tuple_begin (uiout, id);
465 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
469 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
472 ui_out_list_begin (uiout, id);
473 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
477 ui_out_field_int (struct ui_out *uiout,
484 struct ui_out_level *current = current_level (uiout);
486 verify_field (uiout, &fldno, &width, &align);
488 uo_field_int (uiout, fldno, width, align, fldname, value);
492 ui_out_field_core_addr (struct ui_out *uiout,
498 /* FIXME-32x64: need a print_address_numeric with field width */
499 /* print_address_numeric (address, 1, local_stream); */
500 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
502 ui_out_field_string (uiout, fldname, addstr);
506 ui_out_field_stream (struct ui_out *uiout,
508 struct ui_stream *buf)
511 char *buffer = ui_file_xstrdup (buf->stream, &length);
512 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
514 ui_out_field_string (uiout, fldname, buffer);
516 ui_out_field_skip (uiout, fldname);
517 ui_file_rewind (buf->stream);
518 do_cleanups (old_cleanup);
521 /* used to ommit a field */
524 ui_out_field_skip (struct ui_out *uiout,
531 verify_field (uiout, &fldno, &width, &align);
533 uo_field_skip (uiout, fldno, width, align, fldname);
537 ui_out_field_string (struct ui_out *uiout,
545 verify_field (uiout, &fldno, &width, &align);
547 uo_field_string (uiout, fldno, width, align, fldname, string);
552 ui_out_field_fmt (struct ui_out *uiout,
554 const char *format, ...)
561 /* will not align, but has to call anyway */
562 verify_field (uiout, &fldno, &width, &align);
564 va_start (args, format);
566 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
572 ui_out_spaces (struct ui_out *uiout, int numspaces)
574 uo_spaces (uiout, numspaces);
578 ui_out_text (struct ui_out *uiout,
581 uo_text (uiout, string);
585 ui_out_message (struct ui_out *uiout, int verbosity,
586 const char *format,...)
590 va_start (args, format);
592 uo_message (uiout, verbosity, format, args);
598 ui_out_stream_new (struct ui_out *uiout)
600 struct ui_stream *tempbuf;
602 tempbuf = XMALLOC (struct ui_stream);
603 tempbuf->uiout = uiout;
604 tempbuf->stream = mem_fileopen ();
609 ui_out_stream_delete (struct ui_stream *buf)
611 ui_file_delete (buf->stream);
616 do_stream_delete (void *buf)
618 ui_out_stream_delete (buf);
622 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
624 return make_cleanup (do_stream_delete, buf);
629 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
631 uo_wrap_hint (uiout, identstring);
635 ui_out_flush (struct ui_out *uiout)
640 /* set the flags specified by the mask given */
642 ui_out_set_flags (struct ui_out *uiout, int mask)
644 int oldflags = uiout->flags;
646 uiout->flags |= mask;
651 /* clear the flags specified by the mask given */
653 ui_out_clear_flags (struct ui_out *uiout, int mask)
655 int oldflags = uiout->flags;
657 uiout->flags &= ~mask;
662 /* test the flags against the mask given */
664 ui_out_test_flags (struct ui_out *uiout, int mask)
666 return (uiout->flags & mask);
669 /* obtain the current verbosity level (as stablished by the
670 'set verbositylevel' command */
673 ui_out_get_verblvl (struct ui_out *uiout)
675 /* FIXME: not implemented yet */
681 ui_out_result_begin (struct ui_out *uiout, char *class)
686 ui_out_result_end (struct ui_out *uiout)
691 ui_out_info_begin (struct ui_out *uiout, char *class)
696 ui_out_info_end (struct ui_out *uiout)
701 ui_out_notify_begin (struct ui_out *uiout, char *class)
706 ui_out_notify_end (struct ui_out *uiout)
711 ui_out_error_begin (struct ui_out *uiout, char *class)
716 ui_out_error_end (struct ui_out *uiout)
723 gdb_error (ui_out * uiout, int severity, char *format,...)
729 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
735 ui_out_is_mi_like_p (struct ui_out *uiout)
737 return uiout->impl->is_mi_like_p;
740 /* default gdb-out hook functions */
743 default_table_begin (struct ui_out *uiout, int nbrofcols,
750 default_table_body (struct ui_out *uiout)
755 default_table_end (struct ui_out *uiout)
760 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
761 const char *col_name,
767 default_begin (struct ui_out *uiout,
768 enum ui_out_type type,
775 default_end (struct ui_out *uiout,
776 enum ui_out_type type,
782 default_field_int (struct ui_out *uiout, int fldno, int width,
784 const char *fldname, int value)
789 default_field_skip (struct ui_out *uiout, int fldno, int width,
790 enum ui_align align, const char *fldname)
795 default_field_string (struct ui_out *uiout,
805 default_field_fmt (struct ui_out *uiout, int fldno, int width,
814 default_spaces (struct ui_out *uiout, int numspaces)
819 default_text (struct ui_out *uiout, const char *string)
824 default_message (struct ui_out *uiout, int verbosity,
831 default_wrap_hint (struct ui_out *uiout, char *identstring)
836 default_flush (struct ui_out *uiout)
840 /* Interface to the implementation functions */
843 uo_table_begin (struct ui_out *uiout, int nbrofcols,
847 if (!uiout->impl->table_begin)
849 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
853 uo_table_body (struct ui_out *uiout)
855 if (!uiout->impl->table_body)
857 uiout->impl->table_body (uiout);
861 uo_table_end (struct ui_out *uiout)
863 if (!uiout->impl->table_end)
865 uiout->impl->table_end (uiout);
869 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
870 const char *col_name,
873 if (!uiout->impl->table_header)
875 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
879 uo_begin (struct ui_out *uiout,
880 enum ui_out_type type,
884 if (uiout->impl->begin == NULL)
886 uiout->impl->begin (uiout, type, level, id);
890 uo_end (struct ui_out *uiout,
891 enum ui_out_type type,
894 if (uiout->impl->end == NULL)
896 uiout->impl->end (uiout, type, level);
900 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
904 if (!uiout->impl->field_int)
906 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
910 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
913 if (!uiout->impl->field_skip)
915 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
919 uo_field_string (struct ui_out *uiout, int fldno, int width,
924 if (!uiout->impl->field_string)
926 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
930 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
935 if (!uiout->impl->field_fmt)
937 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
941 uo_spaces (struct ui_out *uiout, int numspaces)
943 if (!uiout->impl->spaces)
945 uiout->impl->spaces (uiout, numspaces);
949 uo_text (struct ui_out *uiout,
952 if (!uiout->impl->text)
954 uiout->impl->text (uiout, string);
958 uo_message (struct ui_out *uiout, int verbosity,
962 if (!uiout->impl->message)
964 uiout->impl->message (uiout, verbosity, format, args);
968 uo_wrap_hint (struct ui_out *uiout, char *identstring)
970 if (!uiout->impl->wrap_hint)
972 uiout->impl->wrap_hint (uiout, identstring);
976 uo_flush (struct ui_out *uiout)
978 if (!uiout->impl->flush)
980 uiout->impl->flush (uiout);
983 /* local functions */
985 /* list of column headers manipulation routines */
988 clear_header_list (struct ui_out *uiout)
990 while (uiout->table.header_first != NULL)
992 uiout->table.header_next = uiout->table.header_first;
993 uiout->table.header_first = uiout->table.header_first->next;
994 if (uiout->table.header_next->colhdr != NULL)
995 xfree (uiout->table.header_next->colhdr);
996 xfree (uiout->table.header_next);
998 gdb_assert (uiout->table.header_first == NULL);
999 uiout->table.header_last = NULL;
1000 uiout->table.header_next = NULL;
1004 append_header_to_list (struct ui_out *uiout,
1007 const char *col_name,
1010 struct ui_out_hdr *temphdr;
1012 temphdr = XMALLOC (struct ui_out_hdr);
1013 temphdr->width = width;
1014 temphdr->alignment = alignment;
1015 /* we have to copy the column title as the original may be an automatic */
1017 temphdr->colhdr = xstrdup (colhdr);
1019 temphdr->colhdr = NULL;
1020 if (col_name != NULL)
1021 temphdr->col_name = xstrdup (colhdr);
1023 temphdr->col_name = xstrdup (colhdr);
1024 temphdr->next = NULL;
1025 if (uiout->table.header_first == NULL)
1028 uiout->table.header_first = temphdr;
1029 uiout->table.header_last = temphdr;
1033 temphdr->colno = uiout->table.header_last->colno + 1;
1034 uiout->table.header_last->next = temphdr;
1035 uiout->table.header_last = temphdr;
1037 uiout->table.header_next = uiout->table.header_last;
1040 /* Extract the format information for the NEXT header and and advance
1041 the header pointer. Return 0 if there was no next header. */
1044 get_next_header (struct ui_out *uiout,
1050 /* There may be no headers at all or we may have used all columns. */
1051 if (uiout->table.header_next == NULL)
1053 *colno = uiout->table.header_next->colno;
1054 *width = uiout->table.header_next->width;
1055 *alignment = uiout->table.header_next->alignment;
1056 *colhdr = uiout->table.header_next->colhdr;
1057 /* Advance the header pointer to the next entry. */
1058 uiout->table.header_next = uiout->table.header_next->next;
1063 /* Verify that the field/tuple/list is correctly positioned. Return
1064 the field number and corresponding alignment (if
1065 available/applicable). */
1068 verify_field (struct ui_out *uiout, int *fldno, int *width, int *align)
1070 struct ui_out_level *current = current_level (uiout);
1073 if (uiout->table.flag)
1075 if (!uiout->table.body_flag)
1076 internal_error (__FILE__, __LINE__,
1077 "table_body missing; table fields must be \
1078 specified after table_body and inside a list.");
1079 /* NOTE: cagney/2001-12-08: There was a check here to ensure
1080 that this code was only executed when uiout->level was
1081 greater than zero. That no longer applies - this code is run
1082 before each table row tuple is started and at that point the
1086 current->field_count += 1;
1088 if (uiout->table.body_flag
1089 && uiout->table.entry_level == uiout->level
1090 && get_next_header (uiout, fldno, width, align, &text))
1092 if (*fldno != current->field_count)
1093 internal_error (__FILE__, __LINE__,
1094 "ui-out internal error in handling headers.");
1099 *align = ui_noalign;
1100 *fldno = current->field_count;
1105 /* access to ui_out format private members */
1108 ui_out_get_field_separator (struct ui_out *uiout)
1112 /* Access to ui-out members data */
1114 struct ui_out_data *
1115 ui_out_data (struct ui_out *uiout)
1120 /* initalize private members at startup */
1123 ui_out_new (struct ui_out_impl *impl,
1124 struct ui_out_data *data,
1127 struct ui_out *uiout = XMALLOC (struct ui_out);
1130 uiout->flags = flags;
1131 uiout->table.flag = 0;
1132 uiout->table.body_flag = 0;
1134 memset (uiout->levels, 0, sizeof (uiout->levels));
1135 uiout->table.header_first = NULL;
1136 uiout->table.header_last = NULL;
1137 uiout->table.header_next = NULL;
1141 /* standard gdb initialization hook */
1144 _initialize_ui_out (void)
1146 /* nothing needs to be done */