1 /* Output generating routines for GDB.
2 
3    Copyright (C) 1999-2024 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 "expression.h"
24 #include "language.h"
25 #include "ui-out.h"
26 #include "gdbsupport/format.h"
27 #include "cli/cli-style.h"
28 #include "diagnostics.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 
ui_out_hdr(int number,int min_width,ui_align alignment,const std::string & name,const std::string & header)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 
number()52   int number () const
53   {
54     return m_number;
55   }
56 
min_width()57   int min_width () const
58   {
59     return m_min_width;
60   }
61 
alignment()62   ui_align alignment () const
63   {
64     return m_alignment;
65   }
66 
header()67   const std::string &header () const
68   {
69     return m_header;
70   }
71 
name()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 
ui_out_level(ui_out_type type)105   explicit ui_out_level (ui_out_type type)
106   : m_type (type),
107     m_field_count (0)
108   {
109   }
110 
type()111   ui_out_type type () const
112   {
113     return m_type;
114   }
115 
field_count()116   int field_count () const
117   {
118     return m_field_count;
119   }
120 
inc_field_count()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 
ui_out_table(int entry_level,int nr_cols,const std::string & id)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 
start_body()211 void ui_out_table::start_body ()
212 {
213   if (m_state != state::HEADERS)
214     internal_error (_("extra table_body call not allowed; there must be only "
215                           "one table_body after a table_begin and before a "
216                           "table_end."));
217 
218   /* Check if the number of defined headers matches the number of expected
219      columns.  */
220   if (m_headers.size () != m_nr_cols)
221     internal_error (_("number of headers differ from number of table "
222                           "columns."));
223 
224   m_state = state::BODY;
225   m_headers_iterator = m_headers.begin ();
226 }
227 
228 /* See ui-out.h.  */
229 
append_header(int width,ui_align alignment,const std::string & col_name,const std::string & col_hdr)230 void ui_out_table::append_header (int width, ui_align alignment,
231                                           const std::string &col_name,
232                                           const std::string &col_hdr)
233 {
234   if (m_state != state::HEADERS)
235     internal_error (_("table header must be specified after table_begin and "
236                           "before table_body."));
237 
238   auto header = std::make_unique<ui_out_hdr> (m_headers.size () + 1,
239                                                         width, alignment,
240                                                         col_name, col_hdr);
241 
242   m_headers.push_back (std::move (header));
243 }
244 
245 /* See ui-out.h.  */
246 
start_row()247 void ui_out_table::start_row ()
248 {
249   m_headers_iterator = m_headers.begin ();
250 }
251 
252 /* See ui-out.h.  */
253 
get_next_header(int * colno,int * width,ui_align * alignment,const char ** col_hdr)254 bool ui_out_table::get_next_header (int *colno, int *width, ui_align *alignment,
255                                             const char **col_hdr)
256 {
257   /* There may be no headers at all or we may have used all columns.  */
258   if (m_headers_iterator == m_headers.end ())
259     return false;
260 
261   ui_out_hdr *hdr = m_headers_iterator->get ();
262 
263   *colno = hdr->number ();
264   *width = hdr->min_width ();
265   *alignment = hdr->alignment ();
266   *col_hdr = hdr->header ().c_str ();
267 
268   /* Advance the header pointer to the next entry.  */
269   m_headers_iterator++;
270 
271   return true;
272 }
273 
274 /* See ui-out.h.  */
275 
query_field(int colno,int * width,int * alignment,const char ** col_name)276 bool ui_out_table::query_field (int colno, int *width, int *alignment,
277                                         const char **col_name) const
278 {
279   /* Column numbers are 1-based, so convert to 0-based index.  */
280   int index = colno - 1;
281 
282   if (index >= 0 && index < m_headers.size ())
283     {
284       ui_out_hdr *hdr = m_headers[index].get ();
285 
286       gdb_assert (colno == hdr->number ());
287 
288       *width = hdr->min_width ();
289       *alignment = hdr->alignment ();
290       *col_name = hdr->name ().c_str ();
291 
292       return true;
293     }
294   else
295     return false;
296 }
297 
298 /* See ui-out.h.  */
299 
current_state()300 ui_out_table::state ui_out_table::current_state () const
301 {
302   return m_state;
303 }
304 
305 /* See ui-out.h.  */
306 
entry_level()307 int ui_out_table::entry_level () const
308 {
309   return m_entry_level;
310 }
311 
312 int
level()313 ui_out::level () const
314 {
315   return m_levels.size ();
316 }
317 
318 /* The current (inner most) level.  */
319 
320 ui_out_level *
current_level()321 ui_out::current_level () const
322 {
323   return m_levels.back ().get ();
324 }
325 
326 /* Create a new level, of TYPE.  */
327 void
push_level(ui_out_type type)328 ui_out::push_level (ui_out_type type)
329 {
330   auto level = std::make_unique<ui_out_level> (type);
331 
332   m_levels.push_back (std::move (level));
333 }
334 
335 /* Discard the current level.  TYPE is the type of the level being
336    discarded.  */
337 void
pop_level(ui_out_type type)338 ui_out::pop_level (ui_out_type type)
339 {
340   /* We had better not underflow the buffer.  */
341   gdb_assert (m_levels.size () > 0);
342   gdb_assert (current_level ()->type () == type);
343 
344   m_levels.pop_back ();
345 }
346 
347 /* Mark beginning of a table.  */
348 
349 void
table_begin(int nr_cols,int nr_rows,const std::string & tblid)350 ui_out::table_begin (int nr_cols, int nr_rows, const std::string &tblid)
351 {
352   if (m_table_up != nullptr)
353     internal_error (_("tables cannot be nested; table_begin found before \
354 previous table_end."));
355 
356   m_table_up.reset (new ui_out_table (level () + 1, nr_cols, tblid));
357 
358   do_table_begin (nr_cols, nr_rows, tblid.c_str ());
359 }
360 
361 void
table_header(int width,ui_align alignment,const std::string & col_name,const std::string & col_hdr)362 ui_out::table_header (int width, ui_align alignment,
363                           const std::string &col_name, const std::string &col_hdr)
364 {
365   if (m_table_up == nullptr)
366     internal_error (_("table_header outside a table is not valid; it must be \
367 after a table_begin and before a table_body."));
368 
369   m_table_up->append_header (width, alignment, col_name, col_hdr);
370 
371   do_table_header (width, alignment, col_name, col_hdr);
372 }
373 
374 void
table_body()375 ui_out::table_body ()
376 {
377   if (m_table_up == nullptr)
378     internal_error (_("table_body outside a table is not valid; it must be "
379                           "after a table_begin and before a table_end."));
380 
381   m_table_up->start_body ();
382 
383   do_table_body ();
384 }
385 
386 void
table_end()387 ui_out::table_end ()
388 {
389   if (m_table_up == nullptr)
390     internal_error (_("misplaced table_end or missing table_begin."));
391 
392   do_table_end ();
393 
394   m_table_up = nullptr;
395 }
396 
397 void
begin(ui_out_type type,const char * id)398 ui_out::begin (ui_out_type type, const char *id)
399 {
400   /* Be careful to verify the ``field'' before the new tuple/list is
401      pushed onto the stack.  That way the containing list/table/row is
402      verified and not the newly created tuple/list.  This verification
403      is needed (at least) for the case where a table row entry
404      contains either a tuple/list.  For that case bookkeeping such as
405      updating the column count or advancing to the next heading still
406      needs to be performed.  */
407   {
408     int fldno;
409     int width;
410     ui_align align;
411 
412     verify_field (&fldno, &width, &align);
413   }
414 
415   push_level (type);
416 
417   /* If the push puts us at the same level as a table row entry, we've
418      got a new table row.  Put the header pointer back to the start.  */
419   if (m_table_up != nullptr
420       && m_table_up->current_state () == ui_out_table::state::BODY
421       && m_table_up->entry_level () == level ())
422     m_table_up->start_row ();
423 
424   do_begin (type, id);
425 }
426 
427 void
end(ui_out_type type)428 ui_out::end (ui_out_type type)
429 {
430   pop_level (type);
431 
432   do_end (type);
433 }
434 
435 void
field_signed(const char * fldname,LONGEST value)436 ui_out::field_signed (const char *fldname, LONGEST value)
437 {
438   int fldno;
439   int width;
440   ui_align align;
441 
442   verify_field (&fldno, &width, &align);
443 
444   do_field_signed (fldno, width, align, fldname, value);
445 }
446 
447 void
field_fmt_signed(int input_width,ui_align input_align,const char * fldname,LONGEST value)448 ui_out::field_fmt_signed (int input_width, ui_align input_align,
449                                 const char *fldname, LONGEST value)
450 {
451   int fldno;
452   int width;
453   ui_align align;
454 
455   verify_field (&fldno, &width, &align);
456 
457   do_field_signed (fldno, input_width, input_align, fldname, value);
458 }
459 
460 /* See ui-out.h.  */
461 
462 void
field_unsigned(const char * fldname,ULONGEST value)463 ui_out::field_unsigned (const char *fldname, ULONGEST value)
464 {
465   int fldno;
466   int width;
467   ui_align align;
468 
469   verify_field (&fldno, &width, &align);
470 
471   do_field_unsigned (fldno, width, align, fldname, value);
472 }
473 
474 /* Documented in ui-out.h.  */
475 
476 void
field_core_addr(const char * fldname,struct gdbarch * gdbarch,CORE_ADDR address)477 ui_out::field_core_addr (const char *fldname, struct gdbarch *gdbarch,
478                                CORE_ADDR address)
479 {
480   field_string (fldname, print_core_address (gdbarch, address),
481                     address_style.style ());
482 }
483 
484 void
field_stream(const char * fldname,string_file & stream,const ui_file_style & style)485 ui_out::field_stream (const char *fldname, string_file &stream,
486                           const ui_file_style &style)
487 {
488   if (!stream.empty ())
489     field_string (fldname, stream.c_str (), style);
490   else
491     field_skip (fldname);
492   stream.clear ();
493 }
494 
495 /* Used to omit a field.  */
496 
497 void
field_skip(const char * fldname)498 ui_out::field_skip (const char *fldname)
499 {
500   int fldno;
501   int width;
502   ui_align align;
503 
504   verify_field (&fldno, &width, &align);
505 
506   do_field_skip (fldno, width, align, fldname);
507 }
508 
509 void
field_string(const char * fldname,const char * string,const ui_file_style & style)510 ui_out::field_string (const char *fldname, const char *string,
511                           const ui_file_style &style)
512 {
513   int fldno;
514   int width;
515   ui_align align;
516 
517   verify_field (&fldno, &width, &align);
518 
519   do_field_string (fldno, width, align, fldname, string, style);
520 }
521 
522 /* VARARGS */
523 void
field_fmt(const char * fldname,const char * format,...)524 ui_out::field_fmt (const char *fldname, const char *format, ...)
525 {
526   va_list args;
527   int fldno;
528   int width;
529   ui_align align;
530 
531   verify_field (&fldno, &width, &align);
532 
533   va_start (args, format);
534 
535   do_field_fmt (fldno, width, align, fldname, ui_file_style (), format, args);
536 
537   va_end (args);
538 }
539 
540 void
field_fmt(const char * fldname,const ui_file_style & style,const char * format,...)541 ui_out::field_fmt (const char *fldname, const ui_file_style &style,
542                        const char *format, ...)
543 {
544   va_list args;
545   int fldno;
546   int width;
547   ui_align align;
548 
549   verify_field (&fldno, &width, &align);
550 
551   va_start (args, format);
552 
553   do_field_fmt (fldno, width, align, fldname, style, format, args);
554 
555   va_end (args);
556 }
557 
558 void
call_do_message(const ui_file_style & style,const char * format,...)559 ui_out::call_do_message (const ui_file_style &style, const char *format,
560                                ...)
561 {
562   va_list args;
563 
564   va_start (args, format);
565 
566   /* Since call_do_message is only used as a helper of vmessage, silence the
567      warning here once instead of at all call sites in vmessage, if we were
568      to put a "format" attribute on call_do_message.  */
569   DIAGNOSTIC_PUSH
570   DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
571   do_message (style, format, args);
572   DIAGNOSTIC_POP
573 
574   va_end (args);
575 }
576 
577 void
vmessage(const ui_file_style & in_style,const char * format,va_list args)578 ui_out::vmessage (const ui_file_style &in_style, const char *format,
579                       va_list args)
580 {
581   format_pieces fpieces (&format, true);
582 
583   ui_file_style style = in_style;
584 
585   for (auto &&piece : fpieces)
586     {
587       const char *current_substring = piece.string;
588 
589       gdb_assert (piece.n_int_args >= 0 && piece.n_int_args <= 2);
590       int intvals[2] = { 0, 0 };
591       for (int i = 0; i < piece.n_int_args; ++i)
592           intvals[i] = va_arg (args, int);
593 
594       /* The only ones we support for now.  */
595       gdb_assert (piece.n_int_args == 0
596                       || piece.argclass == string_arg
597                       || piece.argclass == int_arg
598                       || piece.argclass == long_arg);
599 
600       switch (piece.argclass)
601           {
602           case string_arg:
603             {
604               const char *str = va_arg (args, const char *);
605               switch (piece.n_int_args)
606                 {
607                 case 0:
608                     call_do_message (style, current_substring, str);
609                     break;
610                 case 1:
611                     call_do_message (style, current_substring, intvals[0], str);
612                     break;
613                 case 2:
614                     call_do_message (style, current_substring,
615                                          intvals[0], intvals[1], str);
616                     break;
617                 }
618             }
619             break;
620           case wide_string_arg:
621             gdb_assert_not_reached ("wide_string_arg not supported in vmessage");
622             break;
623           case wide_char_arg:
624             gdb_assert_not_reached ("wide_char_arg not supported in vmessage");
625             break;
626           case long_long_arg:
627             call_do_message (style, current_substring, va_arg (args, long long));
628             break;
629           case int_arg:
630             {
631               int val = va_arg (args, int);
632               switch (piece.n_int_args)
633                 {
634                 case 0:
635                     call_do_message (style, current_substring, val);
636                     break;
637                 case 1:
638                     call_do_message (style, current_substring, intvals[0], val);
639                     break;
640                 case 2:
641                     call_do_message (style, current_substring,
642                                          intvals[0], intvals[1], val);
643                     break;
644                 }
645             }
646             break;
647           case long_arg:
648             {
649               long val = va_arg (args, long);
650               switch (piece.n_int_args)
651                 {
652                 case 0:
653                     call_do_message (style, current_substring, val);
654                     break;
655                 case 1:
656                     call_do_message (style, current_substring, intvals[0], val);
657                     break;
658                 case 2:
659                     call_do_message (style, current_substring,
660                                          intvals[0], intvals[1], val);
661                     break;
662                 }
663             }
664             break;
665           case size_t_arg:
666             {
667               size_t val = va_arg (args, size_t);
668               switch (piece.n_int_args)
669                 {
670                 case 0:
671                     call_do_message (style, current_substring, val);
672                     break;
673                 case 1:
674                     call_do_message (style, current_substring, intvals[0], val);
675                     break;
676                 case 2:
677                     call_do_message (style, current_substring,
678                                          intvals[0], intvals[1], val);
679                     break;
680                 }
681             }
682             break;
683           case double_arg:
684             call_do_message (style, current_substring, va_arg (args, double));
685             break;
686           case long_double_arg:
687             gdb_assert_not_reached ("long_double_arg not supported in vmessage");
688             break;
689           case dec32float_arg:
690             gdb_assert_not_reached ("dec32float_arg not supported in vmessage");
691             break;
692           case dec64float_arg:
693             gdb_assert_not_reached ("dec64float_arg not supported in vmessage");
694             break;
695           case dec128float_arg:
696             gdb_assert_not_reached ("dec128float_arg not supported in vmessage");
697             break;
698           case ptr_arg:
699             switch (current_substring[2])
700               {
701               case 'F':
702                 {
703                     gdb_assert (!test_flags (disallow_ui_out_field));
704                     base_field_s *bf = va_arg (args, base_field_s *);
705                     switch (bf->kind)
706                       {
707                       case field_kind::FIELD_SIGNED:
708                         {
709                           auto *f = (signed_field_s *) bf;
710                           field_signed (f->name, f->val);
711                         }
712                         break;
713                       case field_kind::FIELD_STRING:
714                         {
715                           auto *f = (string_field_s *) bf;
716                           field_string (f->name, f->str);
717                         }
718                         break;
719                       }
720                 }
721                 break;
722               case 's':
723                 {
724                     styled_string_s *ss = va_arg (args, styled_string_s *);
725                     call_do_message (ss->style, "%s", ss->str);
726                 }
727                 break;
728               case '[':
729                 style = *va_arg (args, const ui_file_style *);
730                 break;
731               case ']':
732                 {
733                     void *arg = va_arg (args, void *);
734                     gdb_assert (arg == nullptr);
735 
736                     style = {};
737                 }
738                 break;
739               default:
740                 call_do_message (style, current_substring, va_arg (args, void *));
741                 break;
742               }
743             break;
744           case literal_piece:
745             /* Print a portion of the format string that has no
746                directives.  Note that this will not include any ordinary
747                %-specs, but it might include "%%".  That is why we use
748                call_do_message here.  Also, we pass a dummy argument
749                because some platforms have modified GCC to include
750                -Wformat-security by default, which will warn here if
751                there is no argument.  */
752             call_do_message (style, current_substring, 0);
753             break;
754           default:
755             internal_error (_("failed internal consistency check"));
756           }
757     }
758 }
759 
760 void
message(const char * format,...)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 /* Verify that the field/tuple/list is correctly positioned.  Return
772    the field number and corresponding alignment (if
773    available/applicable).  */
774 
775 void
verify_field(int * fldno,int * width,ui_align * align)776 ui_out::verify_field (int *fldno, int *width, ui_align *align)
777 {
778   ui_out_level *current = current_level ();
779   const char *text;
780 
781   if (m_table_up != nullptr
782       && m_table_up->current_state () != ui_out_table::state::BODY)
783     {
784       internal_error (_("table_body missing; table fields must be \
785 specified after table_body and inside a list."));
786     }
787 
788   current->inc_field_count ();
789 
790   if (m_table_up != nullptr
791       && m_table_up->current_state () == ui_out_table::state::BODY
792       && m_table_up->entry_level () == level ()
793       && m_table_up->get_next_header (fldno, width, align, &text))
794     {
795       if (*fldno != current->field_count ())
796           internal_error (_("ui-out internal error in handling headers."));
797     }
798   else
799     {
800       *width = 0;
801       *align = ui_noalign;
802       *fldno = current->field_count ();
803     }
804 }
805 
806 /* Access table field parameters.  */
807 
808 bool
query_table_field(int colno,int * width,int * alignment,const char ** col_name)809 ui_out::query_table_field (int colno, int *width, int *alignment,
810                                  const char **col_name)
811 {
812   if (m_table_up == nullptr)
813     return false;
814 
815   return m_table_up->query_field (colno, width, alignment, col_name);
816 }
817 
818 /* The constructor.  */
819 
ui_out(ui_out_flags flags)820 ui_out::ui_out (ui_out_flags flags)
821 : m_flags (flags)
822 {
823   /* Create the ui-out level #1, the default level.  */
824   push_level (ui_out_type_tuple);
825 }
826 
~ui_out()827 ui_out::~ui_out ()
828 {
829 }
830 
831 /* See ui-out.h.  */
832 
833 void
flush()834 buffer_group::output_unit::flush () const
835 {
836   if (!m_msg.empty ())
837     m_stream->puts (m_msg.c_str ());
838 
839   if (m_wrap_hint >= 0)
840     m_stream->wrap_here (m_wrap_hint);
841 
842   if (m_flush)
843     m_stream->flush ();
844 }
845 
846 /* See ui-out.h.  */
847 
848 void
write(const char * buf,long length_buf,ui_file * stream)849 buffer_group::write (const char *buf, long length_buf, ui_file *stream)
850 {
851   /* Record each line separately.  */
852   for (size_t prev = 0, cur = 0; cur < length_buf; ++cur)
853     if (buf[cur] == '\n' || cur == length_buf - 1)
854       {
855           std::string msg (buf + prev, cur - prev + 1);
856 
857           if (m_buffered_output.size () > 0
858               && m_buffered_output.back ().m_wrap_hint == -1
859               && m_buffered_output.back ().m_stream == stream
860               && m_buffered_output.back ().m_msg.size () > 0
861               && m_buffered_output.back ().m_msg.back () != '\n')
862             m_buffered_output.back ().m_msg.append (msg);
863           else
864             m_buffered_output.emplace_back (msg).m_stream = stream;
865           prev = cur + 1;
866       }
867 }
868 
869 /* See ui-out.h.  */
870 
871 void
wrap_here(int indent,ui_file * stream)872 buffer_group::wrap_here (int indent, ui_file *stream)
873 {
874   m_buffered_output.emplace_back ("", indent).m_stream = stream;
875 }
876 
877 /* See ui-out.h.  */
878 
879 void
flush_here(ui_file * stream)880 buffer_group::flush_here (ui_file *stream)
881 {
882   m_buffered_output.emplace_back ("", -1, true).m_stream = stream;
883 }
884 
885 /* See ui-out.h.  */
886 
887 ui_file *
get_unbuffered(ui_file * stream)888 get_unbuffered (ui_file *stream)
889 {
890   buffering_file *buf = dynamic_cast<buffering_file *> (stream);
891 
892   if (buf == nullptr)
893     return stream;
894 
895   return get_unbuffered (buf->stream ());
896 }
897 
buffered_streams(buffer_group * group,ui_out * uiout)898 buffered_streams::buffered_streams (buffer_group *group, ui_out *uiout)
899   : m_buffered_stdout (group, gdb_stdout),
900     m_buffered_stderr (group, gdb_stderr),
901     m_buffered_stdlog (group, gdb_stdlog),
902     m_buffered_stdtarg (group, gdb_stdtarg),
903     m_uiout (uiout)
904 {
905   gdb_stdout = &m_buffered_stdout;
906   gdb_stderr = &m_buffered_stderr;
907   gdb_stdlog = &m_buffered_stdlog;
908   gdb_stdtarg = &m_buffered_stdtarg;
909 
910   ui_file *stream = current_uiout->current_stream ();
911   if (stream != nullptr)
912     {
913       m_buffered_current_uiout.emplace (group, stream);
914       current_uiout->redirect (&(*m_buffered_current_uiout));
915     }
916 
917   stream = m_uiout->current_stream ();
918   if (stream != nullptr && current_uiout != m_uiout)
919     {
920       m_buffered_uiout.emplace (group, stream);
921       m_uiout->redirect (&(*m_buffered_uiout));
922     }
923 
924   m_buffers_in_place = true;
925 }
926 
927 /* See ui-out.h.  */
928 
929 void
remove_buffers()930 buffered_streams::remove_buffers ()
931 {
932   if (!m_buffers_in_place)
933     return;
934 
935   m_buffers_in_place = false;
936 
937   gdb_stdout = m_buffered_stdout.stream ();
938   gdb_stderr = m_buffered_stderr.stream ();
939   gdb_stdlog = m_buffered_stdlog.stream ();
940   gdb_stdtarg = m_buffered_stdtarg.stream ();
941 
942   if (m_buffered_current_uiout.has_value ())
943     current_uiout->redirect (nullptr);
944 
945   if (m_buffered_uiout.has_value ())
946     m_uiout->redirect (nullptr);
947 }
948 
buffer_group(ui_out * uiout)949 buffer_group::buffer_group (ui_out *uiout)
950   : m_buffered_streams (new buffered_streams (this, uiout))
951 { /* Nothing.  */ }
952 
953 /* See ui-out.h.  */
954 
955 void
flush()956 buffer_group::flush () const
957 {
958   m_buffered_streams->remove_buffers ();
959 
960   for (const output_unit &ou : m_buffered_output)
961     ou.flush ();
962 }
963