b2003ea
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
b2003ea
From: Keith Seitz <keiths@redhat.com>
b2003ea
Date: Mon, 8 Jun 2020 11:33:47 -0700
b2003ea
Subject: gdb-rhbz1844458-use-fputX_unfiltered.patch
b2003ea
b2003ea
;; Fix fput?_unfiltered functions
b2003ea
;; RH BZ 1844458 (Sergio Durigan Junior and Tom Tromey)
b2003ea
b2003ea
From 9effb44ccbf50c16da66aaab5fd535fe17e38e32 Mon Sep 17 00:00:00 2001
b2003ea
From: Sergio Durigan Junior <sergiodj@redhat.com>
b2003ea
Date: Wed, 19 Feb 2020 16:40:48 -0500
b2003ea
Subject: [PATCH] Make '{putchar,fputc}_unfiltered' use 'fputs_unfiltered'
b2003ea
b2003ea
There is currently a regression when using
b2003ea
'{putchar,fputc}_unfiltered' with 'puts_unfiltered' which was
b2003ea
introduced by one of the commits that reworked the unfiltered print
b2003ea
code.
b2003ea
b2003ea
The regression makes it impossible to use '{putchar,fputc}_unfiltered'
b2003ea
with 'puts_unfiltered', because the former writes directly to the
b2003ea
ui_file stream using 'stream->write', while the latter uses a buffered
b2003ea
mechanism (see 'wrap_buffer') and delays the printing.
b2003ea
b2003ea
If you do a quick & dirty hack on e.g. top.c:show_gdb_datadir:
b2003ea
b2003ea
  @@ -2088,6 +2088,13 @@ static void
b2003ea
   show_gdb_datadir (struct ui_file *file, int from_tty,
b2003ea
                    struct cmd_list_element *c, const char *value)
b2003ea
   {
b2003ea
  +  putchar_unfiltered ('\n');
b2003ea
  +  puts_unfiltered ("TEST");
b2003ea
  +  putchar_unfiltered ('>');
b2003ea
  +  puts_unfiltered ("PUTS");
b2003ea
  +  puts_unfiltered ("PUTS");
b2003ea
  +  putchar_unfiltered ('\n');
b2003ea
b2003ea
rebuild GDB and invoke the "show data-directory" command, you will
b2003ea
see:
b2003ea
b2003ea
  (gdb) show data-directory
b2003ea
b2003ea
  >
b2003ea
  TESTPUTSGDB's data directory is "/usr/local/share/gdb".
b2003ea
b2003ea
Note how the '>' was printed before the output, and "TEST" and "PUTS"
b2003ea
were printed together.
b2003ea
b2003ea
My first attempt to fix this was to always call 'flush_wrap_buffer' at
b2003ea
the end of 'fputs_maybe_filtered', since it seemed to me that the
b2003ea
function should always print what was requested.  But I wasn't sure
b2003ea
this was the right thing to do, so I talked to Tom on IRC and he gave
b2003ea
me another, simpler idea: make '{putchar,fputc}_unfiltered' call into
b2003ea
the already existing 'fputs_unfiltered' function.
b2003ea
b2003ea
This patch implements the idea.  I regtested it on the Buildbot, and
b2003ea
no regressions were detected.
b2003ea
b2003ea
gdb/ChangeLog:
b2003ea
2020-02-20  Sergio Durigan Junior  <sergiodj@redhat.com>
b2003ea
            Tom Tromey  <tom@tromey.com>
b2003ea
b2003ea
        * utils.c (fputs_maybe_filtered): Call 'stream->puts' instead
b2003ea
        of 'fputc_unfiltered'.
b2003ea
        (putchar_unfiltered): Call 'fputc_unfiltered'.
b2003ea
        (fputc_unfiltered): Call 'fputs_unfiltered'.
b2003ea
b2003ea
diff --git a/gdb/utils.c b/gdb/utils.c
b2003ea
--- a/gdb/utils.c
b2003ea
+++ b/gdb/utils.c
b2003ea
@@ -1783,7 +1783,12 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
b2003ea
 		     newline -- if chars_per_line is right, we
b2003ea
 		     probably just overflowed anyway; if it's wrong,
b2003ea
 		     let us keep going.  */
b2003ea
-		  fputc_unfiltered ('\n', stream);
b2003ea
+		  /* XXX: The ideal thing would be to call
b2003ea
+		     'stream->putc' here, but we can't because it
b2003ea
+		     currently calls 'fputc_unfiltered', which ends up
b2003ea
+		     calling us, which generates an infinite
b2003ea
+		     recursion.  */
b2003ea
+		  stream->puts ("\n");
b2003ea
 		}
b2003ea
 	      else
b2003ea
 		{
b2003ea
@@ -1828,7 +1833,12 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
b2003ea
 	  wrap_here ((char *) 0);	/* Spit out chars, cancel
b2003ea
 					   further wraps.  */
b2003ea
 	  lines_printed++;
b2003ea
-	  fputc_unfiltered ('\n', stream);
b2003ea
+	  /* XXX: The ideal thing would be to call
b2003ea
+	     'stream->putc' here, but we can't because it
b2003ea
+	     currently calls 'fputc_unfiltered', which ends up
b2003ea
+	     calling us, which generates an infinite
b2003ea
+	     recursion.  */
b2003ea
+	  stream->puts ("\n");
b2003ea
 	  lineptr++;
b2003ea
 	}
b2003ea
     }
b2003ea
@@ -1923,10 +1933,7 @@ fputs_highlighted (const char *str, const compiled_regex &highlight,
b2003ea
 int
b2003ea
 putchar_unfiltered (int c)
b2003ea
 {
b2003ea
-  char buf = c;
b2003ea
-
b2003ea
-  ui_file_write (gdb_stdout, &buf, 1);
b2003ea
-  return c;
b2003ea
+  return fputc_unfiltered (c, gdb_stdout);
b2003ea
 }
b2003ea
 
b2003ea
 /* Write character C to gdb_stdout using GDB's paging mechanism and return C.
b2003ea
@@ -1941,9 +1948,11 @@ putchar_filtered (int c)
b2003ea
 int
b2003ea
 fputc_unfiltered (int c, struct ui_file *stream)
b2003ea
 {
b2003ea
-  char buf = c;
b2003ea
+  char buf[2];
b2003ea
 
b2003ea
-  ui_file_write (stream, &buf, 1);
b2003ea
+  buf[0] = c;
b2003ea
+  buf[1] = 0;
b2003ea
+  fputs_unfiltered (buf, stream);
b2003ea
   return c;
b2003ea
 }
b2003ea