1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2016 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "expression.h" /* For language.h */
32 /* table header structures */
38 enum ui_align alignment;
45 /* Count each field; the first element is for non-list fields. */
47 /* The type of this level. */
48 enum ui_out_type type;
52 /* Tables are special. Maintain a separate structure that tracks
53 their state. At present an output can only contain a single table
54 but that restriction might eventually be lifted. */
58 /* If on, a table is being generated. */
61 /* If on, the body of a table is being generated. If off, the table
62 header is being generated. */
65 /* The level at which each entry of the table is to be found. A row
66 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
67 above that of the table. */
70 /* Number of table columns (as specified in the table_begin call). */
73 /* String identifying the table (as specified in the table_begin
77 /* Pointers to the column headers. */
78 std::vector<std::unique_ptr<ui_out_hdr>> headers;
80 /* Iterator over the headers vector, used when printing successive fields. */
81 std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator headers_iterator;
86 /* The ui_out structure */
91 /* Specific implementation of ui-out. */
92 const struct ui_out_impl *impl;
98 /* Vector to store and track the ui-out levels. */
99 std::vector<std::unique_ptr<ui_out_level>> levels;
101 /* A table, if any. At present only a single table is supported. */
102 struct ui_out_table table;
105 /* The current (inner most) level. */
106 static struct ui_out_level *
107 current_level (struct ui_out *uiout)
109 return uiout->levels[uiout->level].get ();
112 /* Create a new level, of TYPE. Return the new level's index. */
114 push_level (struct ui_out *uiout,
115 enum ui_out_type type)
117 std::unique_ptr<ui_out_level> current (new ui_out_level ());
119 current->field_count = 0;
120 current->type = type;
123 uiout->levels.push_back (std::move (current));
128 /* Discard the current level, return the discarded level's index.
129 TYPE is the type of the level being discarded. */
131 pop_level (struct ui_out *uiout,
132 enum ui_out_type type)
134 /* We had better not underflow the buffer. */
135 gdb_assert (uiout->level > 0);
136 gdb_assert (current_level (uiout)->type == type);
138 uiout->levels.pop_back ();
141 return uiout->level + 1;
144 /* These are the interfaces to implementation functions. */
146 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
147 int nr_rows, const char *tblid);
148 static void uo_table_body (struct ui_out *uiout);
149 static void uo_table_end (struct ui_out *uiout);
150 static void uo_table_header (struct ui_out *uiout, int width,
152 const std::string &col_name,
153 const std::string &col_hdr);
154 static void uo_begin (struct ui_out *uiout,
155 enum ui_out_type type,
156 int level, const char *id);
157 static void uo_end (struct ui_out *uiout,
158 enum ui_out_type type,
160 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
161 enum ui_align align, const char *fldname, int value);
162 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
163 enum ui_align align, const char *fldname);
164 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
165 enum ui_align align, const char *fldname,
166 const char *format, va_list args)
167 ATTRIBUTE_PRINTF (6, 0);
168 static void uo_spaces (struct ui_out *uiout, int numspaces);
169 static void uo_text (struct ui_out *uiout, const char *string);
170 static void uo_message (struct ui_out *uiout,
171 const char *format, va_list args)
172 ATTRIBUTE_PRINTF (2, 0);
173 static void uo_wrap_hint (struct ui_out *uiout, const char *identstring);
174 static void uo_flush (struct ui_out *uiout);
175 static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
177 /* Prototypes for local functions */
179 static void append_header_to_list (struct ui_out *uiout, int width,
180 enum ui_align alignment,
181 const std::string &col_name,
182 const std::string &col_hdr);
183 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
184 enum ui_align *alignment, const char **col_hdr);
185 static void clear_header_list (struct ui_out *uiout);
186 static void clear_table (struct ui_out *uiout);
187 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
188 enum ui_align *align);
190 /* exported functions (ui_out API) */
192 /* Mark beginning of a table. */
195 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
196 int nr_rows, const std::string &tblid)
198 if (uiout->table.flag)
199 internal_error (__FILE__, __LINE__,
200 _("tables cannot be nested; table_begin found before \
201 previous table_end."));
203 uiout->table.flag = 1;
204 uiout->table.body_flag = 0;
205 uiout->table.entry_level = uiout->level + 1;
206 uiout->table.columns = nbrofcols;
207 uiout->table.id = tblid;
209 clear_header_list (uiout);
211 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id.c_str ());
215 ui_out_table_body (struct ui_out *uiout)
217 if (!uiout->table.flag)
218 internal_error (__FILE__, __LINE__,
219 _("table_body outside a table is not valid; it must be \
220 after a table_begin and before a table_end."));
221 if (uiout->table.body_flag)
222 internal_error (__FILE__, __LINE__,
223 _("extra table_body call not allowed; there must be \
224 only one table_body after a table_begin and before a table_end."));
225 if (uiout->table.headers.size () != uiout->table.columns)
226 internal_error (__FILE__, __LINE__,
227 _("number of headers differ from number of table \
230 uiout->table.body_flag = 1;
232 uo_table_body (uiout);
236 ui_out_table_end (struct ui_out *uiout)
238 if (!uiout->table.flag)
239 internal_error (__FILE__, __LINE__,
240 _("misplaced table_end or missing table_begin."));
242 uiout->table.entry_level = 0;
243 uiout->table.body_flag = 0;
244 uiout->table.flag = 0;
246 uo_table_end (uiout);
251 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
252 const std::string &col_name, const std::string &col_hdr)
254 if (!uiout->table.flag || uiout->table.body_flag)
255 internal_error (__FILE__, __LINE__,
256 _("table header must be specified after table_begin \
257 and before table_body."));
259 append_header_to_list (uiout, width, alignment, col_name, col_hdr);
261 uo_table_header (uiout, width, alignment, col_name, col_hdr);
265 do_cleanup_table_end (void *data)
267 struct ui_out *ui_out = (struct ui_out *) data;
269 ui_out_table_end (ui_out);
273 make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
274 int nr_rows, const char *tblid)
276 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
277 return make_cleanup (do_cleanup_table_end, ui_out);
281 ui_out_begin (struct ui_out *uiout,
282 enum ui_out_type type,
287 if (uiout->table.flag && !uiout->table.body_flag)
288 internal_error (__FILE__, __LINE__,
289 _("table header or table_body expected; lists must be \
290 specified after table_body."));
292 /* Be careful to verify the ``field'' before the new tuple/list is
293 pushed onto the stack. That way the containing list/table/row is
294 verified and not the newly created tuple/list. This verification
295 is needed (at least) for the case where a table row entry
296 contains either a tuple/list. For that case bookkeeping such as
297 updating the column count or advancing to the next heading still
298 needs to be performed. */
304 verify_field (uiout, &fldno, &width, &align);
307 new_level = push_level (uiout, type);
309 /* If the push puts us at the same level as a table row entry, we've
310 got a new table row. Put the header pointer back to the start. */
311 if (uiout->table.body_flag
312 && uiout->table.entry_level == new_level)
313 uiout->table.headers_iterator = uiout->table.headers.begin ();
315 uo_begin (uiout, type, new_level, id);
319 ui_out_end (struct ui_out *uiout,
320 enum ui_out_type type)
322 int old_level = pop_level (uiout, type);
324 uo_end (uiout, type, old_level);
327 struct ui_out_end_cleanup_data
329 struct ui_out *uiout;
330 enum ui_out_type type;
334 do_cleanup_end (void *data)
336 struct ui_out_end_cleanup_data *end_cleanup_data
337 = (struct ui_out_end_cleanup_data *) data;
339 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
340 xfree (end_cleanup_data);
343 static struct cleanup *
344 make_cleanup_ui_out_end (struct ui_out *uiout,
345 enum ui_out_type type)
347 struct ui_out_end_cleanup_data *end_cleanup_data;
349 end_cleanup_data = XNEW (struct ui_out_end_cleanup_data);
350 end_cleanup_data->uiout = uiout;
351 end_cleanup_data->type = type;
352 return make_cleanup (do_cleanup_end, end_cleanup_data);
356 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
359 ui_out_begin (uiout, ui_out_type_tuple, id);
360 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
364 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
367 ui_out_begin (uiout, ui_out_type_list, id);
368 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
372 ui_out_field_int (struct ui_out *uiout,
380 verify_field (uiout, &fldno, &width, &align);
382 uo_field_int (uiout, fldno, width, align, fldname, value);
386 ui_out_field_fmt_int (struct ui_out *uiout,
388 enum ui_align input_align,
396 verify_field (uiout, &fldno, &width, &align);
398 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
401 /* Documented in ui-out.h. */
404 ui_out_field_core_addr (struct ui_out *uiout,
406 struct gdbarch *gdbarch,
409 ui_out_field_string (uiout, fldname,
410 print_core_address (gdbarch, address));
414 ui_out_field_stream (struct ui_out *uiout,
416 struct ui_file *stream)
418 std::string buffer = ui_file_as_string (stream);
420 if (!buffer.empty ())
421 ui_out_field_string (uiout, fldname, buffer.c_str ());
423 ui_out_field_skip (uiout, fldname);
424 ui_file_rewind (stream);
427 /* Used to omit a field. */
430 ui_out_field_skip (struct ui_out *uiout,
437 verify_field (uiout, &fldno, &width, &align);
439 uo_field_skip (uiout, fldno, width, align, fldname);
443 ui_out_field_string (struct ui_out *uiout,
451 verify_field (uiout, &fldno, &width, &align);
453 uo_field_string (uiout, fldno, width, align, fldname, string);
458 ui_out_field_fmt (struct ui_out *uiout,
460 const char *format, ...)
467 /* Will not align, but has to call anyway. */
468 verify_field (uiout, &fldno, &width, &align);
470 va_start (args, format);
472 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
478 ui_out_spaces (struct ui_out *uiout, int numspaces)
480 uo_spaces (uiout, numspaces);
484 ui_out_text (struct ui_out *uiout,
487 uo_text (uiout, string);
491 ui_out_message (struct ui_out *uiout, const char *format, ...)
495 va_start (args, format);
496 uo_message (uiout, format, args);
501 ui_out_wrap_hint (struct ui_out *uiout, const char *identstring)
503 uo_wrap_hint (uiout, identstring);
507 ui_out_flush (struct ui_out *uiout)
513 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
515 return uo_redirect (uiout, outstream);
518 /* Test the flags against the mask given. */
520 ui_out_test_flags (struct ui_out *uiout, int mask)
522 return (uiout->flags & mask);
526 ui_out_is_mi_like_p (struct ui_out *uiout)
528 return uiout->impl->is_mi_like_p;
531 /* Interface to the implementation functions. */
534 uo_table_begin (struct ui_out *uiout, int nbrofcols,
538 if (!uiout->impl->table_begin)
540 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
544 uo_table_body (struct ui_out *uiout)
546 if (!uiout->impl->table_body)
548 uiout->impl->table_body (uiout);
552 uo_table_end (struct ui_out *uiout)
554 if (!uiout->impl->table_end)
556 uiout->impl->table_end (uiout);
560 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
561 const std::string &col_name, const std::string &col_hdr)
563 if (!uiout->impl->table_header)
565 uiout->impl->table_header (uiout, width, align, col_name, col_hdr);
568 /* Clear the table associated with UIOUT. */
571 clear_table (struct ui_out *uiout)
573 uiout->table.id.clear ();
574 clear_header_list (uiout);
578 uo_begin (struct ui_out *uiout,
579 enum ui_out_type type,
583 if (uiout->impl->begin == NULL)
585 uiout->impl->begin (uiout, type, level, id);
589 uo_end (struct ui_out *uiout,
590 enum ui_out_type type,
593 if (uiout->impl->end == NULL)
595 uiout->impl->end (uiout, type, level);
599 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
603 if (!uiout->impl->field_int)
605 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
609 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
612 if (!uiout->impl->field_skip)
614 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
618 uo_field_string (struct ui_out *uiout, int fldno, int width,
623 if (!uiout->impl->field_string)
625 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
629 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
634 if (!uiout->impl->field_fmt)
636 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
640 uo_spaces (struct ui_out *uiout, int numspaces)
642 if (!uiout->impl->spaces)
644 uiout->impl->spaces (uiout, numspaces);
648 uo_text (struct ui_out *uiout,
651 if (!uiout->impl->text)
653 uiout->impl->text (uiout, string);
657 uo_message (struct ui_out *uiout,
661 if (!uiout->impl->message)
663 uiout->impl->message (uiout, format, args);
667 uo_wrap_hint (struct ui_out *uiout, const char *identstring)
669 if (!uiout->impl->wrap_hint)
671 uiout->impl->wrap_hint (uiout, identstring);
675 uo_flush (struct ui_out *uiout)
677 if (!uiout->impl->flush)
679 uiout->impl->flush (uiout);
683 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
685 if (!uiout->impl->redirect)
687 return uiout->impl->redirect (uiout, outstream);
690 /* local functions */
692 /* List of column headers manipulation routines. */
695 clear_header_list (struct ui_out *uiout)
697 uiout->table.headers.clear ();
698 uiout->table.headers_iterator = uiout->table.headers.end ();
702 append_header_to_list (struct ui_out *uiout,
704 enum ui_align alignment,
705 const std::string &col_name,
706 const std::string &col_hdr)
708 std::unique_ptr<ui_out_hdr> temphdr (new ui_out_hdr ());
710 temphdr->width = width;
711 temphdr->alignment = alignment;
713 /* Make our own copy of the strings, since the lifetime of the original
714 versions may be too short. */
715 temphdr->col_hdr = col_hdr;
716 temphdr->col_name = col_name;
718 temphdr->colno = uiout->table.headers.size () + 1;
720 uiout->table.headers.push_back (std::move (temphdr));
723 /* Extract the format information for the NEXT header and advance
724 the header pointer. Return 0 if there was no next header. */
727 get_next_header (struct ui_out *uiout,
730 enum ui_align *alignment,
731 const char **col_hdr)
733 /* There may be no headers at all or we may have used all columns. */
734 if (uiout->table.headers_iterator == uiout->table.headers.end ())
737 ui_out_hdr *hdr = uiout->table.headers_iterator->get ();
741 *alignment = hdr->alignment;
742 *col_hdr = hdr->col_hdr.c_str ();
744 /* Advance the header pointer to the next entry. */
745 uiout->table.headers_iterator++;
751 /* Verify that the field/tuple/list is correctly positioned. Return
752 the field number and corresponding alignment (if
753 available/applicable). */
756 verify_field (struct ui_out *uiout, int *fldno, int *width,
757 enum ui_align *align)
759 struct ui_out_level *current = current_level (uiout);
762 if (uiout->table.flag)
764 if (!uiout->table.body_flag)
765 internal_error (__FILE__, __LINE__,
766 _("table_body missing; table fields must be \
767 specified after table_body and inside a list."));
768 /* NOTE: cagney/2001-12-08: There was a check here to ensure
769 that this code was only executed when uiout->level was
770 greater than zero. That no longer applies - this code is run
771 before each table row tuple is started and at that point the
775 current->field_count += 1;
777 if (uiout->table.body_flag
778 && uiout->table.entry_level == uiout->level
779 && get_next_header (uiout, fldno, width, align, &text))
781 if (*fldno != current->field_count)
782 internal_error (__FILE__, __LINE__,
783 _("ui-out internal error in handling headers."));
789 *fldno = current->field_count;
794 /* Access to ui-out members data. */
797 ui_out_data (struct ui_out *uiout)
802 /* Access table field parameters. */
804 ui_out_query_field (struct ui_out *uiout, int colno,
805 int *width, int *alignment, const char **col_name)
807 if (!uiout->table.flag)
810 /* Column numbers are 1-based, so convert to 0-based index. */
811 int index = colno - 1;
813 if (index >= 0 && index < uiout->table.headers.size ())
815 ui_out_hdr *hdr = uiout->table.headers[index].get ();
817 gdb_assert (colno == hdr->colno);
820 *alignment = hdr->alignment;
821 *col_name = hdr->col_name.c_str ();
829 /* Initialize private members at startup. */
832 ui_out_new (const struct ui_out_impl *impl, void *data,
835 struct ui_out *uiout = new ui_out ();
836 std::unique_ptr<ui_out_level> current (new ui_out_level ());
840 uiout->flags = flags;
841 uiout->table.flag = 0;
842 uiout->table.body_flag = 0;
845 /* Create uiout->level 0, the default level. */
846 current->type = ui_out_type_tuple;
847 current->field_count = 0;
848 uiout->levels.push_back (std::move (current));
850 uiout->table.headers_iterator = uiout->table.headers.end ();