1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2017 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 */
35 /* A header of a ui_out_table. */
41 explicit ui_out_hdr (int number, int min_width, ui_align alignment,
42 const std::string &name, const std::string &header)
44 m_min_width (min_width),
45 m_alignment (alignment),
56 int min_width () const
61 ui_align alignment () const
66 const std::string &header () const
71 const std::string &name () const
78 /* The number of the table column this header represents, 1-based. */
81 /* Minimal column width in characters. May or may not be applicable,
82 depending on the actual implementation of ui_out. */
85 /* Alignment of the content in the column. May or may not be applicable,
86 depending on the actual implementation of ui_out. */
89 /* Internal column name, used to internally refer to the column. */
92 /* Printed header text of the column. */
98 /* A level of nesting (either a list or a tuple) in a ui_out output. */
104 explicit ui_out_level (ui_out_type type)
110 ui_out_type type () const
115 int field_count () const
117 return m_field_count;
120 void inc_field_count ()
127 /* The type of this level. */
130 /* Count each field; the first element is for non-list fields. */
134 /* Tables are special. Maintain a separate structure that tracks
135 their state. At present an output can only contain a single table
136 but that restriction might eventually be lifted. */
142 /* States (steps) of a table generation. */
146 /* We are generating the table headers. */
149 /* We are generating the table body. */
153 explicit ui_out_table (int entry_level, int nr_cols, const std::string &id)
154 : m_state (state::HEADERS),
155 m_entry_level (entry_level),
161 /* Start building the body of the table. */
165 /* Add a new header to the table. */
167 void append_header (int width, ui_align alignment,
168 const std::string &col_name, const std::string &col_hdr);
172 /* Extract the format information for the next header and advance
173 the header iterator. Return false if there was no next header. */
175 bool get_next_header (int *colno, int *width, ui_align *alignment,
176 const char **col_hdr);
178 bool query_field (int colno, int *width, int *alignment,
179 const char **col_name) const;
181 state current_state () const;
183 int entry_level () const;
189 /* The level at which each entry of the table is to be found. A row
190 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
191 above that of the table. */
194 /* Number of table columns (as specified in the table_begin call). */
197 /* String identifying the table (as specified in the table_begin
201 /* Pointers to the column headers. */
202 std::vector<std::unique_ptr<ui_out_hdr>> m_headers;
204 /* Iterator over the headers vector, used when printing successive fields. */
205 std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator m_headers_iterator;
210 void ui_out_table::start_body ()
212 if (m_state != state::HEADERS)
213 internal_error (__FILE__, __LINE__,
214 _("extra table_body call not allowed; there must be only "
215 "one table_body after a table_begin and before a "
218 /* Check if the number of defined headers matches the number of expected
220 if (m_headers.size () != m_nr_cols)
221 internal_error (__FILE__, __LINE__,
222 _("number of headers differ from number of table "
225 m_state = state::BODY;
226 m_headers_iterator = m_headers.begin ();
231 void ui_out_table::append_header (int width, ui_align alignment,
232 const std::string &col_name,
233 const std::string &col_hdr)
235 if (m_state != state::HEADERS)
236 internal_error (__FILE__, __LINE__,
237 _("table header must be specified after table_begin and "
238 "before table_body."));
240 std::unique_ptr<ui_out_hdr> header (new ui_out_hdr (m_headers.size () + 1,
244 m_headers.push_back (std::move (header));
249 void ui_out_table::start_row ()
251 m_headers_iterator = m_headers.begin ();
256 bool ui_out_table::get_next_header (int *colno, int *width, ui_align *alignment,
257 const char **col_hdr)
259 /* There may be no headers at all or we may have used all columns. */
260 if (m_headers_iterator == m_headers.end ())
263 ui_out_hdr *hdr = m_headers_iterator->get ();
265 *colno = hdr->number ();
266 *width = hdr->min_width ();
267 *alignment = hdr->alignment ();
268 *col_hdr = hdr->header ().c_str ();
270 /* Advance the header pointer to the next entry. */
271 m_headers_iterator++;
278 bool ui_out_table::query_field (int colno, int *width, int *alignment,
279 const char **col_name) const
281 /* Column numbers are 1-based, so convert to 0-based index. */
282 int index = colno - 1;
284 if (index >= 0 && index < m_headers.size ())
286 ui_out_hdr *hdr = m_headers[index].get ();
288 gdb_assert (colno == hdr->number ());
290 *width = hdr->min_width ();
291 *alignment = hdr->alignment ();
292 *col_name = hdr->name ().c_str ();
302 ui_out_table::state ui_out_table::current_state () const
309 int ui_out_table::entry_level () const
311 return m_entry_level;
315 ui_out::level () const
317 return m_levels.size ();
320 /* The current (inner most) level. */
323 ui_out::current_level () const
325 return m_levels.back ().get ();
328 /* Create a new level, of TYPE. */
330 ui_out::push_level (ui_out_type type)
332 std::unique_ptr<ui_out_level> level (new ui_out_level (type));
334 m_levels.push_back (std::move (level));
337 /* Discard the current level. TYPE is the type of the level being
340 ui_out::pop_level (ui_out_type type)
342 /* We had better not underflow the buffer. */
343 gdb_assert (m_levels.size () > 0);
344 gdb_assert (current_level ()->type () == type);
346 m_levels.pop_back ();
349 /* Mark beginning of a table. */
352 ui_out::table_begin (int nr_cols, int nr_rows, const std::string &tblid)
354 if (m_table_up != nullptr)
355 internal_error (__FILE__, __LINE__,
356 _("tables cannot be nested; table_begin found before \
357 previous table_end."));
359 m_table_up.reset (new ui_out_table (level () + 1, nr_cols, tblid));
361 do_table_begin (nr_cols, nr_rows, tblid.c_str ());
365 ui_out::table_header (int width, ui_align alignment,
366 const std::string &col_name, const std::string &col_hdr)
368 if (m_table_up == nullptr)
369 internal_error (__FILE__, __LINE__,
370 _("table_header outside a table is not valid; it must be \
371 after a table_begin and before a table_body."));
373 m_table_up->append_header (width, alignment, col_name, col_hdr);
375 do_table_header (width, alignment, col_name, col_hdr);
379 ui_out::table_body ()
381 if (m_table_up == nullptr)
382 internal_error (__FILE__, __LINE__,
383 _("table_body outside a table is not valid; it must be "
384 "after a table_begin and before a table_end."));
386 m_table_up->start_body ();
394 if (m_table_up == nullptr)
395 internal_error (__FILE__, __LINE__,
396 _("misplaced table_end or missing table_begin."));
400 m_table_up = nullptr;
404 do_cleanup_table_end (void *data)
406 ui_out *uiout = (ui_out *) data;
412 make_cleanup_ui_out_table_begin_end (ui_out *uiout, int nr_cols, int nr_rows,
415 uiout->table_begin (nr_cols, nr_rows, tblid);
416 return make_cleanup (do_cleanup_table_end, uiout);
420 ui_out::begin (ui_out_type type, const char *id)
422 /* Be careful to verify the ``field'' before the new tuple/list is
423 pushed onto the stack. That way the containing list/table/row is
424 verified and not the newly created tuple/list. This verification
425 is needed (at least) for the case where a table row entry
426 contains either a tuple/list. For that case bookkeeping such as
427 updating the column count or advancing to the next heading still
428 needs to be performed. */
434 verify_field (&fldno, &width, &align);
439 /* If the push puts us at the same level as a table row entry, we've
440 got a new table row. Put the header pointer back to the start. */
441 if (m_table_up != nullptr
442 && m_table_up->current_state () == ui_out_table::state::BODY
443 && m_table_up->entry_level () == level ())
444 m_table_up->start_row ();
450 ui_out::end (ui_out_type type)
457 struct ui_out_end_cleanup_data
459 struct ui_out *uiout;
460 enum ui_out_type type;
464 do_cleanup_end (void *data)
466 struct ui_out_end_cleanup_data *end_cleanup_data
467 = (struct ui_out_end_cleanup_data *) data;
469 end_cleanup_data->uiout->end (end_cleanup_data->type);
470 xfree (end_cleanup_data);
473 static struct cleanup *
474 make_cleanup_ui_out_end (struct ui_out *uiout,
475 enum ui_out_type type)
477 struct ui_out_end_cleanup_data *end_cleanup_data;
479 end_cleanup_data = XNEW (struct ui_out_end_cleanup_data);
480 end_cleanup_data->uiout = uiout;
481 end_cleanup_data->type = type;
482 return make_cleanup (do_cleanup_end, end_cleanup_data);
486 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
489 uiout->begin (ui_out_type_tuple, id);
490 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
494 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
497 uiout->begin (ui_out_type_list, id);
498 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
502 ui_out::field_int (const char *fldname, int value)
508 verify_field (&fldno, &width, &align);
510 do_field_int (fldno, width, align, fldname, value);
514 ui_out::field_fmt_int (int input_width, ui_align input_align,
515 const char *fldname, int value)
521 verify_field (&fldno, &width, &align);
523 do_field_int (fldno, input_width, input_align, fldname, value);
526 /* Documented in ui-out.h. */
529 ui_out::field_core_addr (const char *fldname, struct gdbarch *gdbarch,
532 field_string (fldname, print_core_address (gdbarch, address));
536 ui_out::field_stream (const char *fldname, string_file &stream)
538 if (!stream.empty ())
539 field_string (fldname, stream.c_str ());
541 field_skip (fldname);
545 /* Used to omit a field. */
548 ui_out::field_skip (const char *fldname)
554 verify_field (&fldno, &width, &align);
556 do_field_skip (fldno, width, align, fldname);
560 ui_out::field_string (const char *fldname, const char *string)
566 verify_field (&fldno, &width, &align);
568 do_field_string (fldno, width, align, fldname, string);
573 ui_out::field_fmt (const char *fldname, const char *format, ...)
580 /* Will not align, but has to call anyway. */
581 verify_field (&fldno, &width, &align);
583 va_start (args, format);
585 do_field_fmt (fldno, width, align, fldname, format, args);
591 ui_out::spaces (int numspaces)
593 do_spaces (numspaces);
597 ui_out::text (const char *string)
603 ui_out::message (const char *format, ...)
607 va_start (args, format);
608 do_message (format, args);
613 ui_out::wrap_hint (const char *identstring)
615 do_wrap_hint (identstring);
625 ui_out::redirect (ui_file *outstream)
627 do_redirect (outstream);
630 /* Test the flags against the mask given. */
632 ui_out::test_flags (ui_out_flags mask)
634 return m_flags & mask;
638 ui_out::is_mi_like_p ()
640 return do_is_mi_like_p ();
643 /* Verify that the field/tuple/list is correctly positioned. Return
644 the field number and corresponding alignment (if
645 available/applicable). */
648 ui_out::verify_field (int *fldno, int *width, ui_align *align)
650 ui_out_level *current = current_level ();
653 if (m_table_up != nullptr
654 && m_table_up->current_state () != ui_out_table::state::BODY)
656 internal_error (__FILE__, __LINE__,
657 _("table_body missing; table fields must be \
658 specified after table_body and inside a list."));
661 current->inc_field_count ();
663 if (m_table_up != nullptr
664 && m_table_up->current_state () == ui_out_table::state::BODY
665 && m_table_up->entry_level () == level ()
666 && m_table_up->get_next_header (fldno, width, align, &text))
668 if (*fldno != current->field_count ())
669 internal_error (__FILE__, __LINE__,
670 _("ui-out internal error in handling headers."));
676 *fldno = current->field_count ();
680 /* Access table field parameters. */
683 ui_out::query_table_field (int colno, int *width, int *alignment,
684 const char **col_name)
686 if (m_table_up == nullptr)
689 return m_table_up->query_field (colno, width, alignment, col_name);
692 /* The constructor. */
694 ui_out::ui_out (ui_out_flags flags)
697 /* Create the ui-out level #1, the default level. */
698 push_level (ui_out_type_tuple);