Introduce gdb-specific %p format suffixes
[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, format, args);
549
550 va_end (args);
551 }
552
553 void
554 ui_out::spaces (int numspaces)
555 {
556 do_spaces (numspaces);
557 }
558
559 void
560 ui_out::text (const char *string)
561 {
562 do_text (string);
563 }
564
565 void
566 ui_out::call_do_message (const ui_file_style &style, const char *format,
567 ...)
568 {
569 va_list args;
570
571 va_start (args, format);
572 do_message (style, format, args);
573 va_end (args);
574 }
575
576 void
577 ui_out::vmessage (const ui_file_style &in_style, const char *format,
578 va_list args)
579 {
580 format_pieces fpieces (&format, true);
581
582 ui_file_style style = in_style;
583
584 for (auto &&piece : fpieces)
585 {
586 const char *current_substring = piece.string;
587
588 gdb_assert (piece.n_int_args >= 0 && piece.n_int_args <= 2);
589 int intvals[2] = { 0, 0 };
590 for (int i = 0; i < piece.n_int_args; ++i)
591 intvals[i] = va_arg (args, int);
592
593 /* The only ones we support for now. */
594 gdb_assert (piece.n_int_args == 0
595 || piece.argclass == string_arg
596 || piece.argclass == int_arg
597 || piece.argclass == long_arg);
598
599 switch (piece.argclass)
600 {
601 case string_arg:
602 {
603 const char *str = va_arg (args, const char *);
604 switch (piece.n_int_args)
605 {
606 case 0:
607 call_do_message (style, current_substring, str);
608 break;
609 case 1:
610 call_do_message (style, current_substring, intvals[0], str);
611 break;
612 case 2:
613 call_do_message (style, current_substring,
614 intvals[0], intvals[1], str);
615 break;
616 }
617 }
618 break;
619 case wide_string_arg:
620 gdb_assert_not_reached (_("wide_string_arg not supported in vmessage"));
621 break;
622 case wide_char_arg:
623 gdb_assert_not_reached (_("wide_char_arg not supported in vmessage"));
624 break;
625 case long_long_arg:
626 call_do_message (style, current_substring, va_arg (args, long long));
627 break;
628 case int_arg:
629 {
630 int val = va_arg (args, int);
631 switch (piece.n_int_args)
632 {
633 case 0:
634 call_do_message (style, current_substring, val);
635 break;
636 case 1:
637 call_do_message (style, current_substring, intvals[0], val);
638 break;
639 case 2:
640 call_do_message (style, current_substring,
641 intvals[0], intvals[1], val);
642 break;
643 }
644 }
645 break;
646 case long_arg:
647 {
648 long val = va_arg (args, long);
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 double_arg:
665 call_do_message (style, current_substring, va_arg (args, double));
666 break;
667 case long_double_arg:
668 gdb_assert_not_reached (_("long_double_arg not supported in vmessage"));
669 break;
670 case dec32float_arg:
671 gdb_assert_not_reached (_("dec32float_arg not supported in vmessage"));
672 break;
673 case dec64float_arg:
674 gdb_assert_not_reached (_("dec64float_arg not supported in vmessage"));
675 break;
676 case dec128float_arg:
677 gdb_assert_not_reached (_("dec128float_arg not supported in vmessage"));
678 break;
679 case ptr_arg:
680 switch (current_substring[2])
681 {
682 case 'F':
683 {
684 gdb_assert (!test_flags (disallow_ui_out_field));
685 base_field_s *bf = va_arg (args, base_field_s *);
686 switch (bf->kind)
687 {
688 case field_kind::SIGNED:
689 {
690 auto *f = (signed_field_s *) bf;
691 field_signed (f->name, f->val);
692 }
693 break;
694 case field_kind::STRING:
695 {
696 auto *f = (string_field_s *) bf;
697 field_string (f->name, f->str);
698 }
699 break;
700 }
701 }
702 break;
703 case 's':
704 {
705 styled_string_s *ss = va_arg (args, styled_string_s *);
706 call_do_message (ss->style, "%s", ss->str);
707 }
708 break;
709 case '[':
710 style = *va_arg (args, const ui_file_style *);
711 break;
712 case ']':
713 {
714 void *arg = va_arg (args, void *);
715 gdb_assert (arg == nullptr);
716
717 style = {};
718 }
719 break;
720 default:
721 call_do_message (style, current_substring, va_arg (args, void *));
722 break;
723 }
724 break;
725 case literal_piece:
726 /* Print a portion of the format string that has no
727 directives. Note that this will not include any ordinary
728 %-specs, but it might include "%%". That is why we use
729 call_do_message here. Also, we pass a dummy argument
730 because some platforms have modified GCC to include
731 -Wformat-security by default, which will warn here if
732 there is no argument. */
733 call_do_message (style, current_substring, 0);
734 break;
735 default:
736 internal_error (__FILE__, __LINE__,
737 _("failed internal consistency check"));
738 }
739 }
740 }
741
742 void
743 ui_out::message (const char *format, ...)
744 {
745 va_list args;
746 va_start (args, format);
747
748 vmessage (ui_file_style (), format, args);
749
750 va_end (args);
751 }
752
753 void
754 ui_out::wrap_hint (const char *identstring)
755 {
756 do_wrap_hint (identstring);
757 }
758
759 void
760 ui_out::flush ()
761 {
762 do_flush ();
763 }
764
765 void
766 ui_out::redirect (ui_file *outstream)
767 {
768 do_redirect (outstream);
769 }
770
771 /* Test the flags against the mask given. */
772 ui_out_flags
773 ui_out::test_flags (ui_out_flags mask)
774 {
775 return m_flags & mask;
776 }
777
778 bool
779 ui_out::is_mi_like_p () const
780 {
781 return do_is_mi_like_p ();
782 }
783
784 /* Verify that the field/tuple/list is correctly positioned. Return
785 the field number and corresponding alignment (if
786 available/applicable). */
787
788 void
789 ui_out::verify_field (int *fldno, int *width, ui_align *align)
790 {
791 ui_out_level *current = current_level ();
792 const char *text;
793
794 if (m_table_up != nullptr
795 && m_table_up->current_state () != ui_out_table::state::BODY)
796 {
797 internal_error (__FILE__, __LINE__,
798 _("table_body missing; table fields must be \
799 specified after table_body and inside a list."));
800 }
801
802 current->inc_field_count ();
803
804 if (m_table_up != nullptr
805 && m_table_up->current_state () == ui_out_table::state::BODY
806 && m_table_up->entry_level () == level ()
807 && m_table_up->get_next_header (fldno, width, align, &text))
808 {
809 if (*fldno != current->field_count ())
810 internal_error (__FILE__, __LINE__,
811 _("ui-out internal error in handling headers."));
812 }
813 else
814 {
815 *width = 0;
816 *align = ui_noalign;
817 *fldno = current->field_count ();
818 }
819 }
820
821 /* Access table field parameters. */
822
823 bool
824 ui_out::query_table_field (int colno, int *width, int *alignment,
825 const char **col_name)
826 {
827 if (m_table_up == nullptr)
828 return false;
829
830 return m_table_up->query_field (colno, width, alignment, col_name);
831 }
832
833 /* The constructor. */
834
835 ui_out::ui_out (ui_out_flags flags)
836 : m_flags (flags)
837 {
838 /* Create the ui-out level #1, the default level. */
839 push_level (ui_out_type_tuple);
840 }
841
842 ui_out::~ui_out ()
843 {
844 }
This page took 0.090254 seconds and 4 git commands to generate.