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 */
33 /* A header of a ui_out_table. */
39 explicit ui_out_hdr (int number
, int min_width
, ui_align alignment
,
40 const std::string
&name
, const std::string
&header
)
42 m_min_width (min_width
),
43 m_alignment (alignment
),
54 int min_width () const
59 ui_align
alignment () const
64 const std::string
&header () const
69 const std::string
&name () const
76 /* The number of the table column this header represents, 1-based. */
79 /* Minimal column width in characters. May or may not be applicable,
80 depending on the actual implementation of ui_out. */
83 /* Alignment of the content in the column. May or may not be applicable,
84 depending on the actual implementation of ui_out. */
87 /* Internal column name, used to internally refer to the column. */
90 /* Printed header text of the column. */
94 /* A level of nesting (either a list or a tuple) in a ui_out output. */
100 explicit ui_out_level (ui_out_type type
)
106 ui_out_type
type () const
111 int field_count () const
113 return m_field_count
;
116 void inc_field_count ()
123 /* The type of this level. */
126 /* 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. */
138 /* States (steps) of a table generation. */
142 /* We are generating the table headers. */
145 /* We are generating the table body. */
149 explicit ui_out_table (int entry_level
, int nr_cols
, const std::string
&id
)
150 : m_state (state::HEADERS
),
151 m_entry_level (entry_level
),
157 /* Start building the body of the table. */
161 /* Add a new header to the table. */
163 void append_header (int width
, ui_align alignment
,
164 const std::string
&col_name
, const std::string
&col_hdr
);
168 /* Extract the format information for the next header and advance
169 the header iterator. Return false if there was no next header. */
171 bool get_next_header (int *colno
, int *width
, ui_align
*alignment
,
172 const char **col_hdr
);
174 bool query_field (int colno
, int *width
, int *alignment
,
175 const char **col_name
) const;
177 state
current_state () const;
179 int entry_level () const;
185 /* The level at which each entry of the table is to be found. A row
186 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
187 above that of the table. */
190 /* Number of table columns (as specified in the table_begin call). */
193 /* String identifying the table (as specified in the table_begin
197 /* Pointers to the column headers. */
198 std::vector
<std::unique_ptr
<ui_out_hdr
>> m_headers
;
200 /* Iterator over the headers vector, used when printing successive fields. */
201 std::vector
<std::unique_ptr
<ui_out_hdr
>>::const_iterator m_headers_iterator
;
206 void ui_out_table::start_body ()
208 if (m_state
!= state::HEADERS
)
209 internal_error (__FILE__
, __LINE__
,
210 _("extra table_body call not allowed; there must be only "
211 "one table_body after a table_begin and before a "
214 /* Check if the number of defined headers matches the number of expected
216 if (m_headers
.size () != m_nr_cols
)
217 internal_error (__FILE__
, __LINE__
,
218 _("number of headers differ from number of table "
221 m_state
= state::BODY
;
222 m_headers_iterator
= m_headers
.begin ();
227 void ui_out_table::append_header (int width
, ui_align alignment
,
228 const std::string
&col_name
,
229 const std::string
&col_hdr
)
231 if (m_state
!= state::HEADERS
)
232 internal_error (__FILE__
, __LINE__
,
233 _("table header must be specified after table_begin and "
234 "before table_body."));
236 std::unique_ptr
<ui_out_hdr
> header (new ui_out_hdr (m_headers
.size () + 1,
240 m_headers
.push_back (std::move (header
));
245 void ui_out_table::start_row ()
247 m_headers_iterator
= m_headers
.begin ();
252 bool ui_out_table::get_next_header (int *colno
, int *width
, ui_align
*alignment
,
253 const char **col_hdr
)
255 /* There may be no headers at all or we may have used all columns. */
256 if (m_headers_iterator
== m_headers
.end ())
259 ui_out_hdr
*hdr
= m_headers_iterator
->get ();
261 *colno
= hdr
->number ();
262 *width
= hdr
->min_width ();
263 *alignment
= hdr
->alignment ();
264 *col_hdr
= hdr
->header ().c_str ();
266 /* Advance the header pointer to the next entry. */
267 m_headers_iterator
++;
274 bool ui_out_table::query_field (int colno
, int *width
, int *alignment
,
275 const char **col_name
) const
277 /* Column numbers are 1-based, so convert to 0-based index. */
278 int index
= colno
- 1;
280 if (index
>= 0 && index
< m_headers
.size ())
282 ui_out_hdr
*hdr
= m_headers
[index
].get ();
284 gdb_assert (colno
== hdr
->number ());
286 *width
= hdr
->min_width ();
287 *alignment
= hdr
->alignment ();
288 *col_name
= hdr
->name ().c_str ();
298 ui_out_table::state
ui_out_table::current_state () const
305 int ui_out_table::entry_level () const
307 return m_entry_level
;
311 ui_out::level () const
313 return m_levels
.size ();
316 /* The current (inner most) level. */
319 ui_out::current_level () const
321 return m_levels
.back ().get ();
324 /* Create a new level, of TYPE. */
326 ui_out::push_level (ui_out_type type
)
328 std::unique_ptr
<ui_out_level
> level (new ui_out_level (type
));
330 m_levels
.push_back (std::move (level
));
333 /* Discard the current level. TYPE is the type of the level being
336 ui_out::pop_level (ui_out_type type
)
338 /* We had better not underflow the buffer. */
339 gdb_assert (m_levels
.size () > 0);
340 gdb_assert (current_level ()->type () == type
);
342 m_levels
.pop_back ();
345 /* Mark beginning of a table. */
348 ui_out::table_begin (int nr_cols
, int nr_rows
, const std::string
&tblid
)
350 if (m_table_up
!= nullptr)
351 internal_error (__FILE__
, __LINE__
,
352 _("tables cannot be nested; table_begin found before \
353 previous table_end."));
355 m_table_up
.reset (new ui_out_table (level () + 1, nr_cols
, tblid
));
357 do_table_begin (nr_cols
, nr_rows
, tblid
.c_str ());
361 ui_out::table_header (int width
, ui_align alignment
,
362 const std::string
&col_name
, const std::string
&col_hdr
)
364 if (m_table_up
== nullptr)
365 internal_error (__FILE__
, __LINE__
,
366 _("table_header outside a table is not valid; it must be \
367 after a table_begin and before a table_body."));
369 m_table_up
->append_header (width
, alignment
, col_name
, col_hdr
);
371 do_table_header (width
, alignment
, col_name
, col_hdr
);
375 ui_out::table_body ()
377 if (m_table_up
== nullptr)
378 internal_error (__FILE__
, __LINE__
,
379 _("table_body outside a table is not valid; it must be "
380 "after a table_begin and before a table_end."));
382 m_table_up
->start_body ();
390 if (m_table_up
== nullptr)
391 internal_error (__FILE__
, __LINE__
,
392 _("misplaced table_end or missing table_begin."));
396 m_table_up
= nullptr;
400 do_cleanup_table_end (void *data
)
402 ui_out
*uiout
= (ui_out
*) data
;
408 make_cleanup_ui_out_table_begin_end (ui_out
*uiout
, int nr_cols
, int nr_rows
,
411 uiout
->table_begin (nr_cols
, nr_rows
, tblid
);
412 return make_cleanup (do_cleanup_table_end
, uiout
);
416 ui_out::begin (ui_out_type type
, const char *id
)
418 /* Be careful to verify the ``field'' before the new tuple/list is
419 pushed onto the stack. That way the containing list/table/row is
420 verified and not the newly created tuple/list. This verification
421 is needed (at least) for the case where a table row entry
422 contains either a tuple/list. For that case bookkeeping such as
423 updating the column count or advancing to the next heading still
424 needs to be performed. */
430 verify_field (&fldno
, &width
, &align
);
435 /* If the push puts us at the same level as a table row entry, we've
436 got a new table row. Put the header pointer back to the start. */
437 if (m_table_up
!= nullptr
438 && m_table_up
->current_state () == ui_out_table::state::BODY
439 && m_table_up
->entry_level () == level ())
440 m_table_up
->start_row ();
446 ui_out::end (ui_out_type type
)
453 struct ui_out_end_cleanup_data
455 struct ui_out
*uiout
;
456 enum ui_out_type type
;
460 do_cleanup_end (void *data
)
462 struct ui_out_end_cleanup_data
*end_cleanup_data
463 = (struct ui_out_end_cleanup_data
*) data
;
465 end_cleanup_data
->uiout
->end (end_cleanup_data
->type
);
466 xfree (end_cleanup_data
);
469 static struct cleanup
*
470 make_cleanup_ui_out_end (struct ui_out
*uiout
,
471 enum ui_out_type type
)
473 struct ui_out_end_cleanup_data
*end_cleanup_data
;
475 end_cleanup_data
= XNEW (struct ui_out_end_cleanup_data
);
476 end_cleanup_data
->uiout
= uiout
;
477 end_cleanup_data
->type
= type
;
478 return make_cleanup (do_cleanup_end
, end_cleanup_data
);
482 make_cleanup_ui_out_tuple_begin_end (struct ui_out
*uiout
,
485 uiout
->begin (ui_out_type_tuple
, id
);
486 return make_cleanup_ui_out_end (uiout
, ui_out_type_tuple
);
490 make_cleanup_ui_out_list_begin_end (struct ui_out
*uiout
,
493 uiout
->begin (ui_out_type_list
, id
);
494 return make_cleanup_ui_out_end (uiout
, ui_out_type_list
);
498 ui_out::field_int (const char *fldname
, int value
)
504 verify_field (&fldno
, &width
, &align
);
506 do_field_int (fldno
, width
, align
, fldname
, value
);
510 ui_out::field_fmt_int (int input_width
, ui_align input_align
,
511 const char *fldname
, int value
)
517 verify_field (&fldno
, &width
, &align
);
519 do_field_int (fldno
, input_width
, input_align
, fldname
, value
);
522 /* Documented in ui-out.h. */
525 ui_out::field_core_addr (const char *fldname
, struct gdbarch
*gdbarch
,
528 field_string (fldname
, print_core_address (gdbarch
, address
));
532 ui_out::field_stream (const char *fldname
, ui_file
*stream
)
534 std::string buffer
= ui_file_as_string (stream
);
536 if (!buffer
.empty ())
537 field_string (fldname
, buffer
.c_str ());
539 field_skip (fldname
);
540 ui_file_rewind (stream
);
543 /* Used to omit a field. */
546 ui_out::field_skip (const char *fldname
)
552 verify_field (&fldno
, &width
, &align
);
554 do_field_skip (fldno
, width
, align
, fldname
);
558 ui_out::field_string (const char *fldname
, const char *string
)
564 verify_field (&fldno
, &width
, &align
);
566 do_field_string (fldno
, width
, align
, fldname
, string
);
571 ui_out::field_fmt (const char *fldname
, const char *format
, ...)
578 /* Will not align, but has to call anyway. */
579 verify_field (&fldno
, &width
, &align
);
581 va_start (args
, format
);
583 do_field_fmt (fldno
, width
, align
, fldname
, format
, args
);
589 ui_out::spaces (int numspaces
)
591 do_spaces (numspaces
);
595 ui_out::text (const char *string
)
601 ui_out::message (const char *format
, ...)
605 va_start (args
, format
);
606 do_message (format
, args
);
611 ui_out::wrap_hint (const char *identstring
)
613 do_wrap_hint (identstring
);
623 ui_out::redirect (ui_file
*outstream
)
625 do_redirect (outstream
);
628 /* Test the flags against the mask given. */
630 ui_out::test_flags (ui_out_flags mask
)
632 return m_flags
& mask
;
636 ui_out::is_mi_like_p ()
638 return do_is_mi_like_p ();
641 /* Verify that the field/tuple/list is correctly positioned. Return
642 the field number and corresponding alignment (if
643 available/applicable). */
646 ui_out::verify_field (int *fldno
, int *width
, ui_align
*align
)
648 ui_out_level
*current
= current_level ();
651 if (m_table_up
!= nullptr
652 && m_table_up
->current_state () != ui_out_table::state::BODY
)
654 internal_error (__FILE__
, __LINE__
,
655 _("table_body missing; table fields must be \
656 specified after table_body and inside a list."));
659 current
->inc_field_count ();
661 if (m_table_up
!= nullptr
662 && m_table_up
->current_state () == ui_out_table::state::BODY
663 && m_table_up
->entry_level () == level ()
664 && m_table_up
->get_next_header (fldno
, width
, align
, &text
))
666 if (*fldno
!= current
->field_count ())
667 internal_error (__FILE__
, __LINE__
,
668 _("ui-out internal error in handling headers."));
674 *fldno
= current
->field_count ();
678 /* Access table field parameters. */
681 ui_out::query_table_field (int colno
, int *width
, int *alignment
,
682 const char **col_name
)
684 if (m_table_up
== nullptr)
687 return m_table_up
->query_field (colno
, width
, alignment
, col_name
);
690 /* The constructor. */
692 ui_out::ui_out (ui_out_flags flags
)
695 /* Create the ui-out level #1, the default level. */
696 push_level (ui_out_type_tuple
);