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

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