a64c79481cad9447880d68cf4043474df75b8193
[deliverable/binutils-gdb.git] / gdb / ui-out.c
1 /* Output generating routines for GDB.
2
3 Copyright (C) 1999-2019 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
23 #include "defs.h"
24 #include "expression.h" /* For language.h */
25 #include "language.h"
26 #include "ui-out.h"
27 #include "gdbsupport/format.h"
28 #include "cli/cli-style.h"
29
30 #include <vector>
31 #include <memory>
32 #include <string>
33
34 namespace {
35
36 /* A header of a ui_out_table. */
37
38 class ui_out_hdr
39 {
40 public:
41
42 explicit ui_out_hdr (int number, int min_width, ui_align alignment,
43 const std::string &name, const std::string &header)
44 : m_number (number),
45 m_min_width (min_width),
46 m_alignment (alignment),
47 m_name (name),
48 m_header (header)
49 {
50 }
51
52 int number () const
53 {
54 return m_number;
55 }
56
57 int min_width () const
58 {
59 return m_min_width;
60 }
61
62 ui_align alignment () const
63 {
64 return m_alignment;
65 }
66
67 const std::string &header () const
68 {
69 return m_header;
70 }
71
72 const std::string &name () const
73 {
74 return m_name;
75 }
76
77 private:
78
79 /* The number of the table column this header represents, 1-based. */
80 int m_number;
81
82 /* Minimal column width in characters. May or may not be applicable,
83 depending on the actual implementation of ui_out. */
84 int m_min_width;
85
86 /* Alignment of the content in the column. May or may not be applicable,
87 depending on the actual implementation of ui_out. */
88 ui_align m_alignment;
89
90 /* Internal column name, used to internally refer to the column. */
91 std::string m_name;
92
93 /* Printed header text of the column. */
94 std::string m_header;
95 };
96
97 } // namespace
98
99 /* A level of nesting (either a list or a tuple) in a ui_out output. */
100
101 class ui_out_level
102 {
103 public:
104
105 explicit ui_out_level (ui_out_type type)
106 : m_type (type),
107 m_field_count (0)
108 {
109 }
110
111 ui_out_type type () const
112 {
113 return m_type;
114 }
115
116 int field_count () const
117 {
118 return m_field_count;
119 }
120
121 void inc_field_count ()
122 {
123 m_field_count++;
124 }
125
126 private:
127
128 /* The type of this level. */
129 ui_out_type m_type;
130
131 /* Count each field; the first element is for non-list fields. */
132 int m_field_count;
133 };
134
135 /* Tables are special. Maintain a separate structure that tracks
136 their state. At present an output can only contain a single table
137 but that restriction might eventually be lifted. */
138
139 class ui_out_table
140 {
141 public:
142
143 /* States (steps) of a table generation. */
144
145 enum class state
146 {
147 /* We are generating the table headers. */
148 HEADERS,
149
150 /* We are generating the table body. */
151 BODY,
152 };
153
154 explicit ui_out_table (int entry_level, int nr_cols, const std::string &id)
155 : m_state (state::HEADERS),
156 m_entry_level (entry_level),
157 m_nr_cols (nr_cols),
158 m_id (id)
159 {
160 }
161
162 /* Start building the body of the table. */
163
164 void start_body ();
165
166 /* Add a new header to the table. */
167
168 void append_header (int width, ui_align alignment,
169 const std::string &col_name, const std::string &col_hdr);
170
171 void start_row ();
172
173 /* Extract the format information for the next header and advance
174 the header iterator. Return false if there was no next header. */
175
176 bool get_next_header (int *colno, int *width, ui_align *alignment,
177 const char **col_hdr);
178
179 bool query_field (int colno, int *width, int *alignment,
180 const char **col_name) const;
181
182 state current_state () const;
183
184 int entry_level () const;
185
186 private:
187
188 state m_state;
189
190 /* The level at which each entry of the table is to be found. A row
191 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
192 above that of the table. */
193 int m_entry_level;
194
195 /* Number of table columns (as specified in the table_begin call). */
196 int m_nr_cols;
197
198 /* String identifying the table (as specified in the table_begin
199 call). */
200 std::string m_id;
201
202 /* Pointers to the column headers. */
203 std::vector<std::unique_ptr<ui_out_hdr>> m_headers;
204
205 /* Iterator over the headers vector, used when printing successive fields. */
206 std::vector<std::unique_ptr<ui_out_hdr>>::const_iterator m_headers_iterator;
207 };
208
209 /* See ui-out.h. */
210
211 void ui_out_table::start_body ()
212 {
213 if (m_state != state::HEADERS)
214 internal_error (__FILE__, __LINE__,
215 _("extra table_body call not allowed; there must be only "
216 "one table_body after a table_begin and before a "
217 "table_end."));
218
219 /* Check if the number of defined headers matches the number of expected
220 columns. */
221 if (m_headers.size () != m_nr_cols)
222 internal_error (__FILE__, __LINE__,
223 _("number of headers differ from number of table "
224 "columns."));
225
226 m_state = state::BODY;
227 m_headers_iterator = m_headers.begin ();
228 }
229
230 /* See ui-out.h. */
231
232 void ui_out_table::append_header (int width, ui_align alignment,
233 const std::string &col_name,
234 const std::string &col_hdr)
235 {
236 if (m_state != state::HEADERS)
237 internal_error (__FILE__, __LINE__,
238 _("table header must be specified after table_begin and "
239 "before table_body."));
240
241 std::unique_ptr<ui_out_hdr> header (new ui_out_hdr (m_headers.size () + 1,
242 width, alignment,
243 col_name, col_hdr));
244
245 m_headers.push_back (std::move (header));
246 }
247
248 /* See ui-out.h. */
249
250 void ui_out_table::start_row ()
251 {
252 m_headers_iterator = m_headers.begin ();
253 }
254
255 /* See ui-out.h. */
256
257 bool ui_out_table::get_next_header (int *colno, int *width, ui_align *alignment,
258 const char **col_hdr)
259 {
260 /* There may be no headers at all or we may have used all columns. */
261 if (m_headers_iterator == m_headers.end ())
262 return false;
263
264 ui_out_hdr *hdr = m_headers_iterator->get ();
265
266 *colno = hdr->number ();
267 *width = hdr->min_width ();
268 *alignment = hdr->alignment ();
269 *col_hdr = hdr->header ().c_str ();
270
271 /* Advance the header pointer to the next entry. */
272 m_headers_iterator++;
273
274 return true;
275 }
276
277 /* See ui-out.h. */
278
279 bool ui_out_table::query_field (int colno, int *width, int *alignment,
280 const char **col_name) const
281 {
282 /* Column numbers are 1-based, so convert to 0-based index. */
283 int index = colno - 1;
284
285 if (index >= 0 && index < m_headers.size ())
286 {
287 ui_out_hdr *hdr = m_headers[index].get ();
288
289 gdb_assert (colno == hdr->number ());
290
291 *width = hdr->min_width ();
292 *alignment = hdr->alignment ();
293 *col_name = hdr->name ().c_str ();
294
295 return true;
296 }
297 else
298 return false;
299 }
300
301 /* See ui-out.h. */
302
303 ui_out_table::state ui_out_table::current_state () const
304 {
305 return m_state;
306 }
307
308 /* See ui-out.h. */
309
310 int ui_out_table::entry_level () const
311 {
312 return m_entry_level;
313 }
314
315 int
316 ui_out::level () const
317 {
318 return m_levels.size ();
319 }
320
321 /* The current (inner most) level. */
322
323 ui_out_level *
324 ui_out::current_level () const
325 {
326 return m_levels.back ().get ();
327 }
328
329 /* Create a new level, of TYPE. */
330 void
331 ui_out::push_level (ui_out_type type)
332 {
333 std::unique_ptr<ui_out_level> level (new ui_out_level (type));
334
335 m_levels.push_back (std::move (level));
336 }
337
338 /* Discard the current level. TYPE is the type of the level being
339 discarded. */
340 void
341 ui_out::pop_level (ui_out_type type)
342 {
343 /* We had better not underflow the buffer. */
344 gdb_assert (m_levels.size () > 0);
345 gdb_assert (current_level ()->type () == type);
346
347 m_levels.pop_back ();
348 }
349
350 /* Mark beginning of a table. */
351
352 void
353 ui_out::table_begin (int nr_cols, int nr_rows, const std::string &tblid)
354 {
355 if (m_table_up != nullptr)
356 internal_error (__FILE__, __LINE__,
357 _("tables cannot be nested; table_begin found before \
358 previous table_end."));
359
360 m_table_up.reset (new ui_out_table (level () + 1, nr_cols, tblid));
361
362 do_table_begin (nr_cols, nr_rows, tblid.c_str ());
363 }
364
365 void
366 ui_out::table_header (int width, ui_align alignment,
367 const std::string &col_name, const std::string &col_hdr)
368 {
369 if (m_table_up == nullptr)
370 internal_error (__FILE__, __LINE__,
371 _("table_header outside a table is not valid; it must be \
372 after a table_begin and before a table_body."));
373
374 m_table_up->append_header (width, alignment, col_name, col_hdr);
375
376 do_table_header (width, alignment, col_name, col_hdr);
377 }
378
379 void
380 ui_out::table_body ()
381 {
382 if (m_table_up == nullptr)
383 internal_error (__FILE__, __LINE__,
384 _("table_body outside a table is not valid; it must be "
385 "after a table_begin and before a table_end."));
386
387 m_table_up->start_body ();
388
389 do_table_body ();
390 }
391
392 void
393 ui_out::table_end ()
394 {
395 if (m_table_up == nullptr)
396 internal_error (__FILE__, __LINE__,
397 _("misplaced table_end or missing table_begin."));
398
399 do_table_end ();
400
401 m_table_up = nullptr;
402 }
403
404 void
405 ui_out::begin (ui_out_type type, const char *id)
406 {
407 /* Be careful to verify the ``field'' before the new tuple/list is
408 pushed onto the stack. That way the containing list/table/row is
409 verified and not the newly created tuple/list. This verification
410 is needed (at least) for the case where a table row entry
411 contains either a tuple/list. For that case bookkeeping such as
412 updating the column count or advancing to the next heading still
413 needs to be performed. */
414 {
415 int fldno;
416 int width;
417 ui_align align;
418
419 verify_field (&fldno, &width, &align);
420 }
421
422 push_level (type);
423
424 /* If the push puts us at the same level as a table row entry, we've
425 got a new table row. Put the header pointer back to the start. */
426 if (m_table_up != nullptr
427 && m_table_up->current_state () == ui_out_table::state::BODY
428 && m_table_up->entry_level () == level ())
429 m_table_up->start_row ();
430
431 do_begin (type, id);
432 }
433
434 void
435 ui_out::end (ui_out_type type)
436 {
437 pop_level (type);
438
439 do_end (type);
440 }
441
442 void
443 ui_out::field_signed (const char *fldname, LONGEST value)
444 {
445 int fldno;
446 int width;
447 ui_align align;
448
449 verify_field (&fldno, &width, &align);
450
451 do_field_signed (fldno, width, align, fldname, value);
452 }
453
454 void
455 ui_out::field_fmt_signed (int input_width, ui_align input_align,
456 const char *fldname, LONGEST value)
457 {
458 int fldno;
459 int width;
460 ui_align align;
461
462 verify_field (&fldno, &width, &align);
463
464 do_field_signed (fldno, input_width, input_align, fldname, value);
465 }
466
467 /* See ui-out.h. */
468
469 void
470 ui_out::field_unsigned (const char *fldname, ULONGEST value)
471 {
472 int fldno;
473 int width;
474 ui_align align;
475
476 verify_field (&fldno, &width, &align);
477
478 do_field_unsigned (fldno, width, align, fldname, value);
479 }
480
481 /* Documented in ui-out.h. */
482
483 void
484 ui_out::field_core_addr (const char *fldname, struct gdbarch *gdbarch,
485 CORE_ADDR address)
486 {
487 field_string (fldname, print_core_address (gdbarch, address),
488 address_style.style ());
489 }
490
491 void
492 ui_out::field_stream (const char *fldname, string_file &stream,
493 const ui_file_style &style)
494 {
495 if (!stream.empty ())
496 field_string (fldname, stream.c_str (), style);
497 else
498 field_skip (fldname);
499 stream.clear ();
500 }
501
502 /* Used to omit a field. */
503
504 void
505 ui_out::field_skip (const char *fldname)
506 {
507 int fldno;
508 int width;
509 ui_align align;
510
511 verify_field (&fldno, &width, &align);
512
513 do_field_skip (fldno, width, align, fldname);
514 }
515
516 void
517 ui_out::field_string (const char *fldname, const char *string,
518 const ui_file_style &style)
519 {
520 int fldno;
521 int width;
522 ui_align align;
523
524 verify_field (&fldno, &width, &align);
525
526 do_field_string (fldno, width, align, fldname, string, style);
527 }
528
529 void
530 ui_out::field_string (const char *fldname, const std::string &string)
531 {
532 field_string (fldname, string.c_str ());
533 }
534
535 /* VARARGS */
536 void
537 ui_out::field_fmt (const char *fldname, const char *format, ...)
538 {
539 va_list args;
540 int fldno;
541 int width;
542 ui_align align;
543
544 verify_field (&fldno, &width, &align);
545
546 va_start (args, format);
547
548 do_field_fmt (fldno, width, align, fldname, ui_file_style (), format, args);
549
550 va_end (args);
551 }
552
553 void
554 ui_out::field_fmt (const char *fldname, const ui_file_style &style,
555 const char *format, ...)
556 {
557 va_list args;
558 int fldno;
559 int width;
560 ui_align align;
561
562 verify_field (&fldno, &width, &align);
563
564 va_start (args, format);
565
566 do_field_fmt (fldno, width, align, fldname, style, format, args);
567
568 va_end (args);
569 }
570
571 void
572 ui_out::spaces (int numspaces)
573 {
574 do_spaces (numspaces);
575 }
576
577 void
578 ui_out::text (const char *string)
579 {
580 do_text (string);
581 }
582
583 void
584 ui_out::call_do_message (const ui_file_style &style, const char *format,
585 ...)
586 {
587 va_list args;
588
589 va_start (args, format);
590 do_message (style, format, args);
591 va_end (args);
592 }
593
594 void
595 ui_out::vmessage (const ui_file_style &in_style, const char *format,
596 va_list args)
597 {
598 format_pieces fpieces (&format, true);
599
600 ui_file_style style = in_style;
601
602 for (auto &&piece : fpieces)
603 {
604 const char *current_substring = piece.string;
605
606 gdb_assert (piece.n_int_args >= 0 && piece.n_int_args <= 2);
607 int intvals[2] = { 0, 0 };
608 for (int i = 0; i < piece.n_int_args; ++i)
609 intvals[i] = va_arg (args, int);
610
611 /* The only ones we support for now. */
612 gdb_assert (piece.n_int_args == 0
613 || piece.argclass == string_arg
614 || piece.argclass == int_arg
615 || piece.argclass == long_arg);
616
617 switch (piece.argclass)
618 {
619 case string_arg:
620 {
621 const char *str = va_arg (args, const char *);
622 switch (piece.n_int_args)
623 {
624 case 0:
625 call_do_message (style, current_substring, str);
626 break;
627 case 1:
628 call_do_message (style, current_substring, intvals[0], str);
629 break;
630 case 2:
631 call_do_message (style, current_substring,
632 intvals[0], intvals[1], str);
633 break;
634 }
635 }
636 break;
637 case wide_string_arg:
638 gdb_assert_not_reached (_("wide_string_arg not supported in vmessage"));
639 break;
640 case wide_char_arg:
641 gdb_assert_not_reached (_("wide_char_arg not supported in vmessage"));
642 break;
643 case long_long_arg:
644 call_do_message (style, current_substring, va_arg (args, long long));
645 break;
646 case int_arg:
647 {
648 int val = va_arg (args, int);
649 switch (piece.n_int_args)
650 {
651 case 0:
652 call_do_message (style, current_substring, val);
653 break;
654 case 1:
655 call_do_message (style, current_substring, intvals[0], val);
656 break;
657 case 2:
658 call_do_message (style, current_substring,
659 intvals[0], intvals[1], val);
660 break;
661 }
662 }
663 break;
664 case long_arg:
665 {
666 long val = va_arg (args, long);
667 switch (piece.n_int_args)
668 {
669 case 0:
670 call_do_message (style, current_substring, val);
671 break;
672 case 1:
673 call_do_message (style, current_substring, intvals[0], val);
674 break;
675 case 2:
676 call_do_message (style, current_substring,
677 intvals[0], intvals[1], val);
678 break;
679 }
680 }
681 break;
682 case double_arg:
683 call_do_message (style, current_substring, va_arg (args, double));
684 break;
685 case long_double_arg:
686 gdb_assert_not_reached (_("long_double_arg not supported in vmessage"));
687 break;
688 case dec32float_arg:
689 gdb_assert_not_reached (_("dec32float_arg not supported in vmessage"));
690 break;
691 case dec64float_arg:
692 gdb_assert_not_reached (_("dec64float_arg not supported in vmessage"));
693 break;
694 case dec128float_arg:
695 gdb_assert_not_reached (_("dec128float_arg not supported in vmessage"));
696 break;
697 case ptr_arg:
698 switch (current_substring[2])
699 {
700 case 'F':
701 {
702 gdb_assert (!test_flags (disallow_ui_out_field));
703 base_field_s *bf = va_arg (args, base_field_s *);
704 switch (bf->kind)
705 {
706 case field_kind::SIGNED:
707 {
708 auto *f = (signed_field_s *) bf;
709 field_signed (f->name, f->val);
710 }
711 break;
712 case field_kind::STRING:
713 {
714 auto *f = (string_field_s *) bf;
715 field_string (f->name, f->str);
716 }
717 break;
718 }
719 }
720 break;
721 case 's':
722 {
723 styled_string_s *ss = va_arg (args, styled_string_s *);
724 call_do_message (ss->style, "%s", ss->str);
725 }
726 break;
727 case '[':
728 style = *va_arg (args, const ui_file_style *);
729 break;
730 case ']':
731 {
732 void *arg = va_arg (args, void *);
733 gdb_assert (arg == nullptr);
734
735 style = {};
736 }
737 break;
738 default:
739 call_do_message (style, current_substring, va_arg (args, void *));
740 break;
741 }
742 break;
743 case literal_piece:
744 /* Print a portion of the format string that has no
745 directives. Note that this will not include any ordinary
746 %-specs, but it might include "%%". That is why we use
747 call_do_message here. Also, we pass a dummy argument
748 because some platforms have modified GCC to include
749 -Wformat-security by default, which will warn here if
750 there is no argument. */
751 call_do_message (style, current_substring, 0);
752 break;
753 default:
754 internal_error (__FILE__, __LINE__,
755 _("failed internal consistency check"));
756 }
757 }
758 }
759
760 void
761 ui_out::message (const char *format, ...)
762 {
763 va_list args;
764 va_start (args, format);
765
766 vmessage (ui_file_style (), format, args);
767
768 va_end (args);
769 }
770
771 void
772 ui_out::wrap_hint (const char *identstring)
773 {
774 do_wrap_hint (identstring);
775 }
776
777 void
778 ui_out::flush ()
779 {
780 do_flush ();
781 }
782
783 void
784 ui_out::redirect (ui_file *outstream)
785 {
786 do_redirect (outstream);
787 }
788
789 /* Test the flags against the mask given. */
790 ui_out_flags
791 ui_out::test_flags (ui_out_flags mask)
792 {
793 return m_flags & mask;
794 }
795
796 bool
797 ui_out::is_mi_like_p () const
798 {
799 return do_is_mi_like_p ();
800 }
801
802 /* Verify that the field/tuple/list is correctly positioned. Return
803 the field number and corresponding alignment (if
804 available/applicable). */
805
806 void
807 ui_out::verify_field (int *fldno, int *width, ui_align *align)
808 {
809 ui_out_level *current = current_level ();
810 const char *text;
811
812 if (m_table_up != nullptr
813 && m_table_up->current_state () != ui_out_table::state::BODY)
814 {
815 internal_error (__FILE__, __LINE__,
816 _("table_body missing; table fields must be \
817 specified after table_body and inside a list."));
818 }
819
820 current->inc_field_count ();
821
822 if (m_table_up != nullptr
823 && m_table_up->current_state () == ui_out_table::state::BODY
824 && m_table_up->entry_level () == level ()
825 && m_table_up->get_next_header (fldno, width, align, &text))
826 {
827 if (*fldno != current->field_count ())
828 internal_error (__FILE__, __LINE__,
829 _("ui-out internal error in handling headers."));
830 }
831 else
832 {
833 *width = 0;
834 *align = ui_noalign;
835 *fldno = current->field_count ();
836 }
837 }
838
839 /* Access table field parameters. */
840
841 bool
842 ui_out::query_table_field (int colno, int *width, int *alignment,
843 const char **col_name)
844 {
845 if (m_table_up == nullptr)
846 return false;
847
848 return m_table_up->query_field (colno, width, alignment, col_name);
849 }
850
851 /* The constructor. */
852
853 ui_out::ui_out (ui_out_flags flags)
854 : m_flags (flags)
855 {
856 /* Create the ui-out level #1, the default level. */
857 push_level (ui_out_type_tuple);
858 }
859
860 ui_out::~ui_out ()
861 {
862 }
This page took 0.047882 seconds and 4 git commands to generate.