Blame SOURCES/gdb-rhbz1844458-use-fputX_unfiltered.patch

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