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. */
129 /* States (steps) of a table generation. */
131 enum ui_out_table_state
133 /* We are generating the table headers. */
136 /* We are generating the table body. */
140 /* Tables are special. Maintain a separate structure that tracks
141 their state. At present an output can only contain a single table
142 but that restriction might eventually be lifted. */
146 /* If on, a table is being generated. */
149 ui_out_table_state state;
151 /* The level at which each entry of the table is to be found. A row
152 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
153 above that of the table. */
156 /* Number of table columns (as specified in the table_begin call). */
159 /* String identifying the table (as specified in the table_begin
163 /* Pointers to the column headers. */
164 std::vector<std::unique_ptr<ui_out_hdr>> headers;
166 /* Iterator over the headers vector, used when printing successive fields. */
167 std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator headers_iterator;
172 /* The ui_out structure */
177 /* Specific implementation of ui-out. */
178 const struct ui_out_impl *impl;
181 /* Vector to store and track the ui-out levels. */
182 std::vector<std::unique_ptr<ui_out_level>> levels;
186 return this->levels.size ();
189 /* A table, if any. At present only a single table is supported. */
190 struct ui_out_table table;
193 /* The current (inner most) level. */
194 static ui_out_level *
195 current_level (struct ui_out *uiout)
197 return uiout->levels.back ().get ();
200 /* Create a new level, of TYPE. */
202 push_level (struct ui_out *uiout,
203 enum ui_out_type type)
205 std::unique_ptr<ui_out_level> level (new ui_out_level (type));
207 uiout->levels.push_back (std::move (level));
210 /* Discard the current level. TYPE is the type of the level being
213 pop_level (struct ui_out *uiout,
214 enum ui_out_type type)
216 /* We had better not underflow the buffer. */
217 gdb_assert (uiout->level () > 0);
218 gdb_assert (current_level (uiout)->type () == type);
220 uiout->levels.pop_back ();
223 /* These are the interfaces to implementation functions. */
225 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
226 int nr_rows, const char *tblid);
227 static void uo_table_body (struct ui_out *uiout);
228 static void uo_table_end (struct ui_out *uiout);
229 static void uo_table_header (struct ui_out *uiout, int width,
231 const std::string &col_name,
232 const std::string &col_hdr);
233 static void uo_begin (struct ui_out *uiout,
234 enum ui_out_type type,
236 static void uo_end (struct ui_out *uiout,
237 enum ui_out_type type);
238 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
239 enum ui_align align, const char *fldname, int value);
240 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
241 enum ui_align align, const char *fldname);
242 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
243 enum ui_align align, const char *fldname,
244 const char *format, va_list args)
245 ATTRIBUTE_PRINTF (6, 0);
246 static void uo_spaces (struct ui_out *uiout, int numspaces);
247 static void uo_text (struct ui_out *uiout, const char *string);
248 static void uo_message (struct ui_out *uiout,
249 const char *format, va_list args)
250 ATTRIBUTE_PRINTF (2, 0);
251 static void uo_wrap_hint (struct ui_out *uiout, const char *identstring);
252 static void uo_flush (struct ui_out *uiout);
253 static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
255 /* Prototypes for local functions */
257 static void append_header_to_list (struct ui_out *uiout, int width,
258 enum ui_align alignment,
259 const std::string &col_name,
260 const std::string &col_hdr);
261 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
262 enum ui_align *alignment, const char **col_hdr);
263 static void clear_header_list (struct ui_out *uiout);
264 static void clear_table (struct ui_out *uiout);
265 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
266 enum ui_align *align);
268 /* exported functions (ui_out API) */
270 /* Mark beginning of a table. */
273 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
274 int nr_rows, const std::string &tblid)
276 if (uiout->table.flag)
277 internal_error (__FILE__, __LINE__,
278 _("tables cannot be nested; table_begin found before \
279 previous table_end."));
281 uiout->table.flag = 1;
282 uiout->table.state = TABLE_STATE_HEADERS;
283 uiout->table.entry_level = uiout->level () + 1;
284 uiout->table.columns = nbrofcols;
285 uiout->table.id = tblid;
287 clear_header_list (uiout);
289 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id.c_str ());
293 ui_out_table_body (struct ui_out *uiout)
295 if (!uiout->table.flag)
296 internal_error (__FILE__, __LINE__,
297 _("table_body outside a table is not valid; it must be \
298 after a table_begin and before a table_end."));
300 if (uiout->table.state == TABLE_STATE_BODY)
301 internal_error (__FILE__, __LINE__,
302 _("extra table_body call not allowed; there must be \
303 only one table_body after a table_begin and before a table_end."));
305 if (uiout->table.headers.size () != uiout->table.columns)
306 internal_error (__FILE__, __LINE__,
307 _("number of headers differ from number of table \
310 uiout->table.state = TABLE_STATE_BODY;
312 uo_table_body (uiout);
316 ui_out_table_end (struct ui_out *uiout)
318 if (!uiout->table.flag)
319 internal_error (__FILE__, __LINE__,
320 _("misplaced table_end or missing table_begin."));
322 uiout->table.entry_level = 0;
323 uiout->table.state = TABLE_STATE_HEADERS;
324 uiout->table.flag = 0;
326 uo_table_end (uiout);
331 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
332 const std::string &col_name, const std::string &col_hdr)
334 if (!uiout->table.flag || uiout->table.state != TABLE_STATE_HEADERS)
335 internal_error (__FILE__, __LINE__,
336 _("table header must be specified after table_begin \
337 and before table_body."));
339 append_header_to_list (uiout, width, alignment, col_name, col_hdr);
341 uo_table_header (uiout, width, alignment, col_name, col_hdr);
345 do_cleanup_table_end (void *data)
347 struct ui_out *ui_out = (struct ui_out *) data;
349 ui_out_table_end (ui_out);
353 make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
354 int nr_rows, const char *tblid)
356 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
357 return make_cleanup (do_cleanup_table_end, ui_out);
361 ui_out_begin (struct ui_out *uiout,
362 enum ui_out_type type,
365 if (uiout->table.flag && uiout->table.state != TABLE_STATE_BODY)
366 internal_error (__FILE__, __LINE__,
367 _("table header or table_body expected; lists must be \
368 specified after table_body."));
370 /* Be careful to verify the ``field'' before the new tuple/list is
371 pushed onto the stack. That way the containing list/table/row is
372 verified and not the newly created tuple/list. This verification
373 is needed (at least) for the case where a table row entry
374 contains either a tuple/list. For that case bookkeeping such as
375 updating the column count or advancing to the next heading still
376 needs to be performed. */
382 verify_field (uiout, &fldno, &width, &align);
385 push_level (uiout, type);
387 /* If the push puts us at the same level as a table row entry, we've
388 got a new table row. Put the header pointer back to the start. */
389 if (uiout->table.state == TABLE_STATE_BODY
390 && uiout->table.entry_level == uiout->level ())
391 uiout->table.headers_iterator = uiout->table.headers.begin ();
393 uo_begin (uiout, type, id);
397 ui_out_end (struct ui_out *uiout,
398 enum ui_out_type type)
400 pop_level (uiout, type);
402 uo_end (uiout, type);
405 struct ui_out_end_cleanup_data
407 struct ui_out *uiout;
408 enum ui_out_type type;
412 do_cleanup_end (void *data)
414 struct ui_out_end_cleanup_data *end_cleanup_data
415 = (struct ui_out_end_cleanup_data *) data;
417 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
418 xfree (end_cleanup_data);
421 static struct cleanup *
422 make_cleanup_ui_out_end (struct ui_out *uiout,
423 enum ui_out_type type)
425 struct ui_out_end_cleanup_data *end_cleanup_data;
427 end_cleanup_data = XNEW (struct ui_out_end_cleanup_data);
428 end_cleanup_data->uiout = uiout;
429 end_cleanup_data->type = type;
430 return make_cleanup (do_cleanup_end, end_cleanup_data);
434 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
437 ui_out_begin (uiout, ui_out_type_tuple, id);
438 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
442 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
445 ui_out_begin (uiout, ui_out_type_list, id);
446 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
450 ui_out_field_int (struct ui_out *uiout,
458 verify_field (uiout, &fldno, &width, &align);
460 uo_field_int (uiout, fldno, width, align, fldname, value);
464 ui_out_field_fmt_int (struct ui_out *uiout,
466 enum ui_align input_align,
474 verify_field (uiout, &fldno, &width, &align);
476 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
479 /* Documented in ui-out.h. */
482 ui_out_field_core_addr (struct ui_out *uiout,
484 struct gdbarch *gdbarch,
487 ui_out_field_string (uiout, fldname,
488 print_core_address (gdbarch, address));
492 ui_out_field_stream (struct ui_out *uiout,
494 struct ui_file *stream)
496 std::string buffer = ui_file_as_string (stream);
498 if (!buffer.empty ())
499 ui_out_field_string (uiout, fldname, buffer.c_str ());
501 ui_out_field_skip (uiout, fldname);
502 ui_file_rewind (stream);
505 /* Used to omit a field. */
508 ui_out_field_skip (struct ui_out *uiout,
515 verify_field (uiout, &fldno, &width, &align);
517 uo_field_skip (uiout, fldno, width, align, fldname);
521 ui_out_field_string (struct ui_out *uiout,
529 verify_field (uiout, &fldno, &width, &align);
531 uo_field_string (uiout, fldno, width, align, fldname, string);
536 ui_out_field_fmt (struct ui_out *uiout,
538 const char *format, ...)
545 /* Will not align, but has to call anyway. */
546 verify_field (uiout, &fldno, &width, &align);
548 va_start (args, format);
550 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
556 ui_out_spaces (struct ui_out *uiout, int numspaces)
558 uo_spaces (uiout, numspaces);
562 ui_out_text (struct ui_out *uiout,
565 uo_text (uiout, string);
569 ui_out_message (struct ui_out *uiout, const char *format, ...)
573 va_start (args, format);
574 uo_message (uiout, format, args);
579 ui_out_wrap_hint (struct ui_out *uiout, const char *identstring)
581 uo_wrap_hint (uiout, identstring);
585 ui_out_flush (struct ui_out *uiout)
591 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
593 return uo_redirect (uiout, outstream);
596 /* Test the flags against the mask given. */
598 ui_out_test_flags (struct ui_out *uiout, int mask)
600 return (uiout->flags & mask);
604 ui_out_is_mi_like_p (struct ui_out *uiout)
606 return uiout->impl->is_mi_like_p;
609 /* Interface to the implementation functions. */
612 uo_table_begin (struct ui_out *uiout, int nbrofcols,
616 if (!uiout->impl->table_begin)
618 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
622 uo_table_body (struct ui_out *uiout)
624 if (!uiout->impl->table_body)
626 uiout->impl->table_body (uiout);
630 uo_table_end (struct ui_out *uiout)
632 if (!uiout->impl->table_end)
634 uiout->impl->table_end (uiout);
638 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
639 const std::string &col_name, const std::string &col_hdr)
641 if (!uiout->impl->table_header)
643 uiout->impl->table_header (uiout, width, align, col_name, col_hdr);
646 /* Clear the table associated with UIOUT. */
649 clear_table (struct ui_out *uiout)
651 uiout->table.id.clear ();
652 clear_header_list (uiout);
656 uo_begin (struct ui_out *uiout,
657 enum ui_out_type type,
660 if (uiout->impl->begin == NULL)
662 uiout->impl->begin (uiout, type, id);
666 uo_end (struct ui_out *uiout,
667 enum ui_out_type type)
669 if (uiout->impl->end == NULL)
671 uiout->impl->end (uiout, type);
675 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
679 if (!uiout->impl->field_int)
681 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
685 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
688 if (!uiout->impl->field_skip)
690 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
694 uo_field_string (struct ui_out *uiout, int fldno, int width,
699 if (!uiout->impl->field_string)
701 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
705 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
710 if (!uiout->impl->field_fmt)
712 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
716 uo_spaces (struct ui_out *uiout, int numspaces)
718 if (!uiout->impl->spaces)
720 uiout->impl->spaces (uiout, numspaces);
724 uo_text (struct ui_out *uiout,
727 if (!uiout->impl->text)
729 uiout->impl->text (uiout, string);
733 uo_message (struct ui_out *uiout,
737 if (!uiout->impl->message)
739 uiout->impl->message (uiout, format, args);
743 uo_wrap_hint (struct ui_out *uiout, const char *identstring)
745 if (!uiout->impl->wrap_hint)
747 uiout->impl->wrap_hint (uiout, identstring);
751 uo_flush (struct ui_out *uiout)
753 if (!uiout->impl->flush)
755 uiout->impl->flush (uiout);
759 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
761 if (!uiout->impl->redirect)
763 return uiout->impl->redirect (uiout, outstream);
766 /* local functions */
768 /* List of column headers manipulation routines. */
771 clear_header_list (struct ui_out *uiout)
773 uiout->table.headers.clear ();
774 uiout->table.headers_iterator = uiout->table.headers.end ();
778 append_header_to_list (struct ui_out *uiout,
780 enum ui_align alignment,
781 const std::string &col_name,
782 const std::string &col_hdr)
784 std::unique_ptr<ui_out_hdr> temphdr(
785 new ui_out_hdr (uiout->table.headers.size () + 1, width,
786 alignment, col_name, col_hdr));
788 uiout->table.headers.push_back (std::move (temphdr));
791 /* Extract the format information for the NEXT header and advance
792 the header pointer. Return 0 if there was no next header. */
795 get_next_header (struct ui_out *uiout,
798 enum ui_align *alignment,
799 const char **col_hdr)
801 /* There may be no headers at all or we may have used all columns. */
802 if (uiout->table.headers_iterator == uiout->table.headers.end ())
805 ui_out_hdr *hdr = uiout->table.headers_iterator->get ();
807 *colno = hdr->number ();
808 *width = hdr->min_width ();
809 *alignment = hdr->alignment ();
810 *col_hdr = hdr->header ().c_str ();
812 /* Advance the header pointer to the next entry. */
813 uiout->table.headers_iterator++;
819 /* Verify that the field/tuple/list is correctly positioned. Return
820 the field number and corresponding alignment (if
821 available/applicable). */
824 verify_field (struct ui_out *uiout, int *fldno, int *width,
825 enum ui_align *align)
827 ui_out_level *current = current_level (uiout);
830 if (uiout->table.flag && uiout->table.state != TABLE_STATE_BODY)
832 internal_error (__FILE__, __LINE__,
833 _("table_body missing; table fields must be \
834 specified after table_body and inside a list."));
837 current->inc_field_count ();
839 if (uiout->table.state == TABLE_STATE_BODY
840 && uiout->table.entry_level == uiout->level ()
841 && get_next_header (uiout, fldno, width, align, &text))
843 if (*fldno != current->field_count ())
844 internal_error (__FILE__, __LINE__,
845 _("ui-out internal error in handling headers."));
851 *fldno = current->field_count ();
856 /* Access to ui-out members data. */
859 ui_out_data (struct ui_out *uiout)
864 /* Access table field parameters. */
866 ui_out_query_field (struct ui_out *uiout, int colno,
867 int *width, int *alignment, const char **col_name)
869 if (!uiout->table.flag)
872 /* Column numbers are 1-based, so convert to 0-based index. */
873 int index = colno - 1;
875 if (index >= 0 && index < uiout->table.headers.size ())
877 ui_out_hdr *hdr = uiout->table.headers[index].get ();
879 gdb_assert (colno == hdr->number ());
881 *width = hdr->min_width ();
882 *alignment = hdr->alignment ();
883 *col_name = hdr->name ().c_str ();
891 /* Initialize private members at startup. */
894 ui_out_new (const struct ui_out_impl *impl, void *data,
897 struct ui_out *uiout = new ui_out ();
901 uiout->flags = flags;
902 uiout->table.flag = 0;
903 uiout->table.state = TABLE_STATE_HEADERS;
905 /* Create the ui-out level #1, the default level. */
906 push_level (uiout, ui_out_type_tuple);
908 uiout->table.headers_iterator = uiout->table.headers.end ();