Blame SOURCES/gdb-rhbz2022177-dprintf-2.patch

79b363
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
79b363
From: Kevin Buettner <kevinb@redhat.com>
79b363
Date: Wed, 10 Nov 2021 18:55:43 -0700
79b363
Subject: gdb-rhbz2022177-dprintf-2.patch
79b363
79b363
;; Backport test case for dprintf bug (RH BZ 2022177).
79b363
79b363
Test case for Bug 28308
79b363
79b363
The purpose of this test is described in the comments in
79b363
dprintf-execution-x-script.exp.
79b363
79b363
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28308
79b363
79b363
The name of this new test was based on that of an existing test,
79b363
bp-cmds-execution-x-script.exp.  I started off by copying that test,
79b363
adding to it, and then rewriting almost all of it.  It's different
79b363
enough that I decided that listing the copyright year as 2021
79b363
was sufficient.
79b363
79b363
diff --git a/gdb/testsuite/gdb.base/dprintf-execution-x-script.c b/gdb/testsuite/gdb.base/dprintf-execution-x-script.c
79b363
new file mode 100644
79b363
--- /dev/null
79b363
+++ b/gdb/testsuite/gdb.base/dprintf-execution-x-script.c
79b363
@@ -0,0 +1,53 @@
79b363
+/* This testcase is part of GDB, the GNU debugger.
79b363
+
79b363
+   Copyright 2021 Free Software Foundation, Inc.
79b363
+
79b363
+   This program is free software; you can redistribute it and/or modify
79b363
+   it under the terms of the GNU General Public License as published by
79b363
+   the Free Software Foundation; either version 3 of the License, or
79b363
+   (at your option) any later version.
79b363
+
79b363
+   This program is distributed in the hope that it will be useful,
79b363
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
79b363
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
79b363
+   GNU General Public License for more details.
79b363
+
79b363
+   You should have received a copy of the GNU General Public License
79b363
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
79b363
+
79b363
+#include <stdlib.h>
79b363
+#include <stdio.h>
79b363
+
79b363
+volatile int vi = 0;
79b363
+
79b363
+void
79b363
+inc_vi ()
79b363
+{
79b363
+  vi++;
79b363
+}
79b363
+
79b363
+void
79b363
+print_vi ()
79b363
+{
79b363
+  printf ("vi=%d\n", vi);
79b363
+}
79b363
+
79b363
+void
79b363
+increment ()
79b363
+{
79b363
+  inc_vi ();
79b363
+}
79b363
+
79b363
+int
79b363
+main (int argc, char **argv)
79b363
+{
79b363
+  print_vi ();
79b363
+  increment ();
79b363
+  print_vi ();
79b363
+  increment ();
79b363
+  print_vi ();
79b363
+  increment ();
79b363
+  print_vi ();
79b363
+
79b363
+  exit (0);
79b363
+}
79b363
diff --git a/gdb/testsuite/gdb.base/dprintf-execution-x-script.exp b/gdb/testsuite/gdb.base/dprintf-execution-x-script.exp
79b363
new file mode 100644
79b363
--- /dev/null
79b363
+++ b/gdb/testsuite/gdb.base/dprintf-execution-x-script.exp
79b363
@@ -0,0 +1,85 @@
79b363
+# Copyright 2021 Free Software Foundation, Inc.
79b363
+
79b363
+# This program is free software; you can redistribute it and/or modify
79b363
+# it under the terms of the GNU General Public License as published by
79b363
+# the Free Software Foundation; either version 3 of the License, or
79b363
+# (at your option) any later version.
79b363
+#
79b363
+# This program is distributed in the hope that it will be useful,
79b363
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
79b363
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
79b363
+# GNU General Public License for more details.
79b363
+#
79b363
+# You should have received a copy of the GNU General Public License
79b363
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
79b363
+
79b363
+# Test that commands in a GDB script file run via GDB's -x flag work
79b363
+# as expected.  Specifically, the script creates a dprintf breakpoint
79b363
+# as well as a normal breakpoint that has "continue" in its command
79b363
+# list, and then does "run".  Correct output from GDB is checked as
79b363
+# part of this test.
79b363
+
79b363
+# Bail out if the target can't use the 'run' command.
79b363
+if ![target_can_use_run_cmd] {
79b363
+    return 0
79b363
+}
79b363
+
79b363
+standard_testfile
79b363
+
79b363
+if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
79b363
+    return -1
79b363
+}
79b363
+
79b363
+# This is the name of the GDB script to load.
79b363
+set x_file ${srcdir}/${subdir}/$testfile.gdb
79b363
+
79b363
+# Create context in which the global, GDBFLAGS, will be restored at
79b363
+# the end of the block.  All commands run within the block are
79b363
+# actually run in the outer context.  (This is why 'res' is available
79b363
+# outside of the save_vars block.)
79b363
+save_vars { GDBFLAGS } {
79b363
+    # Set flags with which to start GDB.
79b363
+    append GDBFLAGS " -ex \"set height unlimited\""
79b363
+    append GDBFLAGS " -x \"$x_file\""
79b363
+    append GDBFLAGS " --args \"$binfile\""
79b363
+
79b363
+    # Start GDB with above flags.
79b363
+    set res [gdb_spawn]
79b363
+}
79b363
+
79b363
+set test "load and run script with -x"
79b363
+if { $res != 0} {
79b363
+    fail $test
79b363
+    return -1
79b363
+}
79b363
+
79b363
+# The script loaded via -x contains a run command; while running, GDB
79b363
+# is expected to print three messages from dprintf breakpoints along
79b363
+# with three interspersed messages from an ordinary breakpoint (which
79b363
+# was set up with a continue command).  Set up pattern D to match
79b363
+# output from hitting the dprintf breakpoint and B for the ordinary
79b363
+# breakpoint.  Then set PAT to contain the entire pattern of expected
79b363
+# output from the interspersed dprintf and ordinary breakpoints along
79b363
+# with some (additional) expected output from the dprintf breakpoints,
79b363
+# i.e. 0, 1, and 2.
79b363
+set d "dprintf in increment.., vi="
79b363
+set b "Breakpoint ., inc_vi"
79b363
+set pat "${d}0.*?$b.*?${d}1.*?$b.*?${d}2.*?$b.*?"
79b363
+
79b363
+proc do_test {cmd test} {
79b363
+    gdb_test $cmd "$::pat$::inferior_exited_re normally.*" $test
79b363
+}
79b363
+
79b363
+# Check output from running script with -x
79b363
+do_test "" $test
79b363
+
79b363
+# Restart GDB and 'source' the script; this will (still) run the program
79b363
+# due to the 'run' command in the script.
79b363
+clean_restart $binfile
79b363
+do_test "source $x_file" "load and run script using source command"
79b363
+
79b363
+# This should leave us at the gdb prompt; Run program again using
79b363
+# already established breakpoints, i.e. those loaded from the
79b363
+# script.  Prior to fixing PR 28308, this was the only test that
79b363
+# would pass.
79b363
+do_test "run" "run again"
79b363
diff --git a/gdb/testsuite/gdb.base/dprintf-execution-x-script.gdb b/gdb/testsuite/gdb.base/dprintf-execution-x-script.gdb
79b363
new file mode 100644
79b363
--- /dev/null
79b363
+++ b/gdb/testsuite/gdb.base/dprintf-execution-x-script.gdb
79b363
@@ -0,0 +1,21 @@
79b363
+# Copyright 2021 Free Software Foundation, Inc.
79b363
+
79b363
+# This program is free software; you can redistribute it and/or modify
79b363
+# it under the terms of the GNU General Public License as published by
79b363
+# the Free Software Foundation; either version 3 of the License, or
79b363
+# (at your option) any later version.
79b363
+#
79b363
+# This program is distributed in the hope that it will be useful,
79b363
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
79b363
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
79b363
+# GNU General Public License for more details.
79b363
+#
79b363
+# You should have received a copy of the GNU General Public License
79b363
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
79b363
+
79b363
+dprintf increment, "dprintf in increment(), vi=%d\n", vi
79b363
+break inc_vi
79b363
+commands
79b363
+  continue
79b363
+end
79b363
+run