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 /* A header of a ui_out_table. */
38 explicit ui_out_hdr (int number, int min_width, ui_align alignment,
39 const std::string &name, const std::string &header)
41 m_min_width (min_width),
42 m_alignment (alignment),
53 int min_width () const
58 ui_align alignment () const
63 const std::string &header () const
68 const std::string &name () const
75 /* The number of the table column this header represents, 1-based. */
78 /* Minimal column width in characters. May or may not be applicable,
79 depending on the actual implementation of ui_out. */
82 /* Alignment of the content in the column. May or may not be applicable,
83 depending on the actual implementation of ui_out. */
86 /* Internal column name, used to internally refer to the column. */
89 /* Printed header text of the column. */
93 /* A level of nesting (either a list or a tuple) in a ui_out output. */
99 explicit ui_out_level (ui_out_type type)
105 ui_out_type type () const
110 int field_count () const
112 return m_field_count;
115 void inc_field_count ()
122 /* The type of this level. */
125 /* Count each field; the first element is for non-list fields. */
130 /* Tables are special. Maintain a separate structure that tracks
131 their state. At present an output can only contain a single table
132 but that restriction might eventually be lifted. */
136 /* If on, a table is being generated. */
139 /* If on, the body of a table is being generated. If off, the table
140 header is being generated. */
143 /* The level at which each entry of the table is to be found. A row
144 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
145 above that of the table. */
148 /* Number of table columns (as specified in the table_begin call). */
151 /* String identifying the table (as specified in the table_begin
155 /* Pointers to the column headers. */
156 std::vector<std::unique_ptr<ui_out_hdr>> headers;
158 /* Iterator over the headers vector, used when printing successive fields. */
159 std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator headers_iterator;
164 /* The ui_out structure */
169 /* Specific implementation of ui-out. */
170 const struct ui_out_impl *impl;
173 /* Vector to store and track the ui-out levels. */
174 std::vector<std::unique_ptr<ui_out_level>> levels;
178 return this->levels.size ();
181 /* A table, if any. At present only a single table is supported. */
182 struct ui_out_table table;
185 /* The current (inner most) level. */
186 static ui_out_level *
187 current_level (struct ui_out *uiout)
189 return uiout->levels.back ().get ();
192 /* Create a new level, of TYPE. */
194 push_level (struct ui_out *uiout,
195 enum ui_out_type type)
197 std::unique_ptr<ui_out_level> level (new ui_out_level (type));
199 uiout->levels.push_back (std::move (level));
202 /* Discard the current level. TYPE is the type of the level being
205 pop_level (struct ui_out *uiout,
206 enum ui_out_type type)
208 /* We had better not underflow the buffer. */
209 gdb_assert (uiout->level () > 0);
210 gdb_assert (current_level (uiout)->type () == type);
212 uiout->levels.pop_back ();
215 /* These are the interfaces to implementation functions. */
217 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
218 int nr_rows, const char *tblid);
219 static void uo_table_body (struct ui_out *uiout);
220 static void uo_table_end (struct ui_out *uiout);
221 static void uo_table_header (struct ui_out *uiout, int width,
223 const std::string &col_name,
224 const std::string &col_hdr);
225 static void uo_begin (struct ui_out *uiout,
226 enum ui_out_type type,
228 static void uo_end (struct ui_out *uiout,
229 enum ui_out_type type);
230 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
231 enum ui_align align, const char *fldname, int value);
232 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
233 enum ui_align align, const char *fldname);
234 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
235 enum ui_align align, const char *fldname,
236 const char *format, va_list args)
237 ATTRIBUTE_PRINTF (6, 0);
238 static void uo_spaces (struct ui_out *uiout, int numspaces);
239 static void uo_text (struct ui_out *uiout, const char *string);
240 static void uo_message (struct ui_out *uiout,
241 const char *format, va_list args)
242 ATTRIBUTE_PRINTF (2, 0);
243 static void uo_wrap_hint (struct ui_out *uiout, const char *identstring);
244 static void uo_flush (struct ui_out *uiout);
245 static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
247 /* Prototypes for local functions */
249 static void append_header_to_list (struct ui_out *uiout, int width,
250 enum ui_align alignment,
251 const std::string &col_name,
252 const std::string &col_hdr);
253 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
254 enum ui_align *alignment, const char **col_hdr);
255 static void clear_header_list (struct ui_out *uiout);
256 static void clear_table (struct ui_out *uiout);
257 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
258 enum ui_align *align);
260 /* exported functions (ui_out API) */
262 /* Mark beginning of a table. */
265 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
266 int nr_rows, const std::string &tblid)
268 if (uiout->table.flag)
269 internal_error (__FILE__, __LINE__,
270 _("tables cannot be nested; table_begin found before \
271 previous table_end."));
273 uiout->table.flag = 1;
274 uiout->table.body_flag = 0;
275 uiout->table.entry_level = uiout->level () + 1;
276 uiout->table.columns = nbrofcols;
277 uiout->table.id = tblid;
279 clear_header_list (uiout);
281 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id.c_str ());
285 ui_out_table_body (struct ui_out *uiout)
287 if (!uiout->table.flag)
288 internal_error (__FILE__, __LINE__,
289 _("table_body outside a table is not valid; it must be \
290 after a table_begin and before a table_end."));
291 if (uiout->table.body_flag)
292 internal_error (__FILE__, __LINE__,
293 _("extra table_body call not allowed; there must be \
294 only one table_body after a table_begin and before a table_end."));
295 if (uiout->table.headers.size () != uiout->table.columns)
296 internal_error (__FILE__, __LINE__,
297 _("number of headers differ from number of table \
300 uiout->table.body_flag = 1;
302 uo_table_body (uiout);
306 ui_out_table_end (struct ui_out *uiout)
308 if (!uiout->table.flag)
309 internal_error (__FILE__, __LINE__,
310 _("misplaced table_end or missing table_begin."));
312 uiout->table.entry_level = 0;
313 uiout->table.body_flag = 0;
314 uiout->table.flag = 0;
316 uo_table_end (uiout);
321 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
322 const std::string &col_name, const std::string &col_hdr)
324 if (!uiout->table.flag || uiout->table.body_flag)
325 internal_error (__FILE__, __LINE__,
326 _("table header must be specified after table_begin \
327 and before table_body."));
329 append_header_to_list (uiout, width, alignment, col_name, col_hdr);
331 uo_table_header (uiout, width, alignment, col_name, col_hdr);
335 do_cleanup_table_end (void *data)
337 struct ui_out *ui_out = (struct ui_out *) data;
339 ui_out_table_end (ui_out);
343 make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
344 int nr_rows, const char *tblid)
346 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
347 return make_cleanup (do_cleanup_table_end, ui_out);
351 ui_out_begin (struct ui_out *uiout,
352 enum ui_out_type type,
355 if (uiout->table.flag && !uiout->table.body_flag)
356 internal_error (__FILE__, __LINE__,
357 _("table header or table_body expected; lists must be \
358 specified after table_body."));
360 /* Be careful to verify the ``field'' before the new tuple/list is
361 pushed onto the stack. That way the containing list/table/row is
362 verified and not the newly created tuple/list. This verification
363 is needed (at least) for the case where a table row entry
364 contains either a tuple/list. For that case bookkeeping such as
365 updating the column count or advancing to the next heading still
366 needs to be performed. */
372 verify_field (uiout, &fldno, &width, &align);
375 push_level (uiout, type);
377 /* If the push puts us at the same level as a table row entry, we've
378 got a new table row. Put the header pointer back to the start. */
379 if (uiout->table.body_flag
380 && uiout->table.entry_level == uiout->level ())
381 uiout->table.headers_iterator = uiout->table.headers.begin ();
383 uo_begin (uiout, type, id);
387 ui_out_end (struct ui_out *uiout,
388 enum ui_out_type type)
390 pop_level (uiout, type);
392 uo_end (uiout, type);
395 struct ui_out_end_cleanup_data
397 struct ui_out *uiout;
398 enum ui_out_type type;
402 do_cleanup_end (void *data)
404 struct ui_out_end_cleanup_data *end_cleanup_data
405 = (struct ui_out_end_cleanup_data *) data;
407 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
408 xfree (end_cleanup_data);
411 static struct cleanup *
412 make_cleanup_ui_out_end (struct ui_out *uiout,
413 enum ui_out_type type)
415 struct ui_out_end_cleanup_data *end_cleanup_data;
417 end_cleanup_data = XNEW (struct ui_out_end_cleanup_data);
418 end_cleanup_data->uiout = uiout;
419 end_cleanup_data->type = type;
420 return make_cleanup (do_cleanup_end, end_cleanup_data);
424 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
427 ui_out_begin (uiout, ui_out_type_tuple, id);
428 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
432 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
435 ui_out_begin (uiout, ui_out_type_list, id);
436 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
440 ui_out_field_int (struct ui_out *uiout,
448 verify_field (uiout, &fldno, &width, &align);
450 uo_field_int (uiout, fldno, width, align, fldname, value);
454 ui_out_field_fmt_int (struct ui_out *uiout,
456 enum ui_align input_align,
464 verify_field (uiout, &fldno, &width, &align);
466 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
469 /* Documented in ui-out.h. */
472 ui_out_field_core_addr (struct ui_out *uiout,
474 struct gdbarch *gdbarch,
477 ui_out_field_string (uiout, fldname,
478 print_core_address (gdbarch, address));
482 ui_out_field_stream (struct ui_out *uiout,
484 struct ui_file *stream)
486 std::string buffer = ui_file_as_string (stream);
488 if (!buffer.empty ())
489 ui_out_field_string (uiout, fldname, buffer.c_str ());
491 ui_out_field_skip (uiout, fldname);
492 ui_file_rewind (stream);
495 /* Used to omit a field. */
498 ui_out_field_skip (struct ui_out *uiout,
505 verify_field (uiout, &fldno, &width, &align);
507 uo_field_skip (uiout, fldno, width, align, fldname);
511 ui_out_field_string (struct ui_out *uiout,
519 verify_field (uiout, &fldno, &width, &align);
521 uo_field_string (uiout, fldno, width, align, fldname, string);
526 ui_out_field_fmt (struct ui_out *uiout,
528 const char *format, ...)
535 /* Will not align, but has to call anyway. */
536 verify_field (uiout, &fldno, &width, &align);
538 va_start (args, format);
540 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
546 ui_out_spaces (struct ui_out *uiout, int numspaces)
548 uo_spaces (uiout, numspaces);
552 ui_out_text (struct ui_out *uiout,
555 uo_text (uiout, string);
559 ui_out_message (struct ui_out *uiout, const char *format, ...)
563 va_start (args, format);
564 uo_message (uiout, format, args);
569 ui_out_wrap_hint (struct ui_out *uiout, const char *identstring)
571 uo_wrap_hint (uiout, identstring);
575 ui_out_flush (struct ui_out *uiout)
581 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
583 return uo_redirect (uiout, outstream);
586 /* Test the flags against the mask given. */
588 ui_out_test_flags (struct ui_out *uiout, int mask)
590 return (uiout->flags & mask);
594 ui_out_is_mi_like_p (struct ui_out *uiout)
596 return uiout->impl->is_mi_like_p;
599 /* Interface to the implementation functions. */
602 uo_table_begin (struct ui_out *uiout, int nbrofcols,
606 if (!uiout->impl->table_begin)
608 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
612 uo_table_body (struct ui_out *uiout)
614 if (!uiout->impl->table_body)
616 uiout->impl->table_body (uiout);
620 uo_table_end (struct ui_out *uiout)
622 if (!uiout->impl->table_end)
624 uiout->impl->table_end (uiout);
628 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
629 const std::string &col_name, const std::string &col_hdr)
631 if (!uiout->impl->table_header)
633 uiout->impl->table_header (uiout, width, align, col_name, col_hdr);
636 /* Clear the table associated with UIOUT. */
639 clear_table (struct ui_out *uiout)
641 uiout->table.id.clear ();
642 clear_header_list (uiout);
646 uo_begin (struct ui_out *uiout,
647 enum ui_out_type type,
650 if (uiout->impl->begin == NULL)
652 uiout->impl->begin (uiout, type, id);
656 uo_end (struct ui_out *uiout,
657 enum ui_out_type type)
659 if (uiout->impl->end == NULL)
661 uiout->impl->end (uiout, type);
665 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
669 if (!uiout->impl->field_int)
671 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
675 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
678 if (!uiout->impl->field_skip)
680 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
684 uo_field_string (struct ui_out *uiout, int fldno, int width,
689 if (!uiout->impl->field_string)
691 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
695 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
700 if (!uiout->impl->field_fmt)
702 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
706 uo_spaces (struct ui_out *uiout, int numspaces)
708 if (!uiout->impl->spaces)
710 uiout->impl->spaces (uiout, numspaces);
714 uo_text (struct ui_out *uiout,
717 if (!uiout->impl->text)
719 uiout->impl->text (uiout, string);
723 uo_message (struct ui_out *uiout,
727 if (!uiout->impl->message)
729 uiout->impl->message (uiout, format, args);
733 uo_wrap_hint (struct ui_out *uiout, const char *identstring)
735 if (!uiout->impl->wrap_hint)
737 uiout->impl->wrap_hint (uiout, identstring);
741 uo_flush (struct ui_out *uiout)
743 if (!uiout->impl->flush)
745 uiout->impl->flush (uiout);
749 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
751 if (!uiout->impl->redirect)
753 return uiout->impl->redirect (uiout, outstream);
756 /* local functions */
758 /* List of column headers manipulation routines. */
761 clear_header_list (struct ui_out *uiout)
763 uiout->table.headers.clear ();
764 uiout->table.headers_iterator = uiout->table.headers.end ();
768 append_header_to_list (struct ui_out *uiout,
770 enum ui_align alignment,
771 const std::string &col_name,
772 const std::string &col_hdr)
774 std::unique_ptr<ui_out_hdr> temphdr(
775 new ui_out_hdr (uiout->table.headers.size () + 1, width,
776 alignment, col_name, col_hdr));
778 uiout->table.headers.push_back (std::move (temphdr));
781 /* Extract the format information for the NEXT header and advance
782 the header pointer. Return 0 if there was no next header. */
785 get_next_header (struct ui_out *uiout,
788 enum ui_align *alignment,
789 const char **col_hdr)
791 /* There may be no headers at all or we may have used all columns. */
792 if (uiout->table.headers_iterator == uiout->table.headers.end ())
795 ui_out_hdr *hdr = uiout->table.headers_iterator->get ();
797 *colno = hdr->number ();
798 *width = hdr->min_width ();
799 *alignment = hdr->alignment ();
800 *col_hdr = hdr->header ().c_str ();
802 /* Advance the header pointer to the next entry. */
803 uiout->table.headers_iterator++;
809 /* Verify that the field/tuple/list is correctly positioned. Return
810 the field number and corresponding alignment (if
811 available/applicable). */
814 verify_field (struct ui_out *uiout, int *fldno, int *width,
815 enum ui_align *align)
817 ui_out_level *current = current_level (uiout);
820 if (uiout->table.flag)
822 if (!uiout->table.body_flag)
823 internal_error (__FILE__, __LINE__,
824 _("table_body missing; table fields must be \
825 specified after table_body and inside a list."));
828 current->inc_field_count ();
830 if (uiout->table.body_flag
831 && uiout->table.entry_level == uiout->level ()
832 && get_next_header (uiout, fldno, width, align, &text))
834 if (*fldno != current->field_count ())
835 internal_error (__FILE__, __LINE__,
836 _("ui-out internal error in handling headers."));
842 *fldno = current->field_count ();
847 /* Access to ui-out members data. */
850 ui_out_data (struct ui_out *uiout)
855 /* Access table field parameters. */
857 ui_out_query_field (struct ui_out *uiout, int colno,
858 int *width, int *alignment, const char **col_name)
860 if (!uiout->table.flag)
863 /* Column numbers are 1-based, so convert to 0-based index. */
864 int index = colno - 1;
866 if (index >= 0 && index < uiout->table.headers.size ())
868 ui_out_hdr *hdr = uiout->table.headers[index].get ();
870 gdb_assert (colno == hdr->number ());
872 *width = hdr->min_width ();
873 *alignment = hdr->alignment ();
874 *col_name = hdr->name ().c_str ();
882 /* Initialize private members at startup. */
885 ui_out_new (const struct ui_out_impl *impl, void *data,
888 struct ui_out *uiout = new ui_out ();
892 uiout->flags = flags;
893 uiout->table.flag = 0;
894 uiout->table.body_flag = 0;
896 /* Create the ui-out level #1, the default level. */
897 push_level (uiout, ui_out_type_tuple);
899 uiout->table.headers_iterator = uiout->table.headers.end ();