Blame SOURCES/gdb-async-stopped-on-pid-arg-1of2.patch

f9426a
http://sourceware.org/ml/gdb-patches/2014-09/msg00102.html
f9426a
Subject: Re: Regression: GDB stopped on run with attached process (PR 17347)  [Re: [pushed+7.8] Re: [PATCH] Fix "attach" command vs user input race
f9426a
f9426a
On 09/03/2014 08:58 AM, Jan Kratochvil wrote:
f9426a
f9426a
> https://sourceware.org/bugzilla/show_bug.cgi?id=17347
f9426a
f9426a
Thanks Jan.
f9426a
f9426a
Here's a fix, test included.  Comments?
f9426a
f9426a
Thanks,
f9426a
Pedro Alves
f9426a
f9426a
--------------------------
f9426a
[PATCH] gdb/17347 - Regression: GDB stopped on run with attached process
f9426a
f9426a
Doing:
f9426a
f9426a
  gdb --pid=PID -ex run
f9426a
f9426a
Results in GDB getting a SIGTTIN, and thus ending stopped.  That's
f9426a
usually indicative of a missing target_terminal_ours call.
f9426a
f9426a
E.g., from the PR:
f9426a
f9426a
 $ sleep 1h & p=$!; sleep 0.1; gdb -batch sleep $p -ex run
f9426a
 [1] 28263
f9426a
 [1]   Killed                  sleep 1h
f9426a
f9426a
 [2]+  Stopped                 gdb -batch sleep $p -ex run
f9426a
f9426a
The workaround is doing:
f9426a
f9426a
 gdb -ex "attach $PID" -ex "run"
f9426a
f9426a
instead of
f9426a
f9426a
 gdb [-p] $PID -ex "run"
f9426a
f9426a
With the former, gdb waits for the attach command to complete before
f9426a
moving on to the "run" command, because the interpreter is in sync
f9426a
mode at this point, within execute_command.  But for the latter,
f9426a
attach_command is called directly from captured_main, and thus misses
f9426a
that waiting.  IOW, "run" is running before the attach continuation
f9426a
has run, before the program stops and attach completes.  The broken
f9426a
terminal settings are just one symptom of that.  Any command that
f9426a
queries or requires input results in the same.
f9426a
f9426a
The fix is to wait in catch_command_errors (which is specific to
f9426a
main.c nowadays), just like we wait in execute_command.
f9426a
f9426a
gdb/ChangeLog:
f9426a
2014-09-03  Pedro Alves  <palves@redhat.com>
f9426a
f9426a
	PR gdb/17347
f9426a
	* main.c: Include "infrun.h".
f9426a
	(catch_command_errors, catch_command_errors_const): Wait for the
f9426a
	foreground command to complete.
f9426a
	* top.c (maybe_wait_sync_command_done): New function, factored out
f9426a
	from ...
f9426a
	(maybe_wait_sync_command_done): ... here.
f9426a
	* top.h (maybe_wait_sync_command_done): New declaration.
f9426a
f9426a
gdb/testsuite/ChangeLog:
f9426a
2014-09-03  Pedro Alves  <palves@redhat.com>
f9426a
f9426a
	PR gdb/17347
f9426a
	* gdb.base/attach.exp (spawn_test_prog): New, factored out from
f9426a
	...
f9426a
	(do_attach_tests, do_call_attach_tests, do_command_attach_tests):
f9426a
	... here.
f9426a
	(gdb_spawn_with_cmdline_opts): New procedure.
f9426a
	(test_command_line_attach_run): New procedure.
f9426a
	(top level): Call it.
f9426a
---
f9426a
 gdb/main.c                        |   9 +++
f9426a
 gdb/testsuite/gdb.base/attach.exp | 118 ++++++++++++++++++++++++++------------
f9426a
 gdb/top.c                         |  26 +++++----
f9426a
 gdb/top.h                         |   8 +++
f9426a
 4 files changed, 114 insertions(+), 47 deletions(-)
f9426a
f9426a
Index: gdb-7.8/gdb/main.c
f9426a
===================================================================
f9426a
--- gdb-7.8.orig/gdb/main.c	2014-09-07 19:12:45.066981588 +0200
f9426a
+++ gdb-7.8/gdb/main.c	2014-09-07 19:14:22.613095201 +0200
f9426a
@@ -47,6 +47,7 @@
f9426a
 #include "filenames.h"
f9426a
 #include "filestuff.h"
f9426a
 #include "event-top.h"
f9426a
+#include "infrun.h"
f9426a
 
f9426a
 /* The selected interpreter.  This will be used as a set command
f9426a
    variable, so it should always be malloc'ed - since
f9426a
@@ -350,7 +351,11 @@ catch_command_errors (catch_command_erro
f9426a
 
f9426a
   TRY_CATCH (e, mask)
f9426a
     {
f9426a
+      int was_sync = sync_execution;
f9426a
+
f9426a
       command (arg, from_tty);
f9426a
+
f9426a
+      maybe_wait_sync_command_done (was_sync);
f9426a
     }
f9426a
   return handle_command_errors (e);
f9426a
 }
f9426a
@@ -369,7 +374,11 @@ catch_command_errors_const (catch_comman
f9426a
 
f9426a
   TRY_CATCH (e, mask)
f9426a
     {
f9426a
+      int was_sync = sync_execution;
f9426a
+
f9426a
       command (arg, from_tty);
f9426a
+
f9426a
+      maybe_wait_sync_command_done (was_sync);
f9426a
     }
f9426a
   return handle_command_errors (e);
f9426a
 }
f9426a
Index: gdb-7.8/gdb/testsuite/gdb.base/attach.exp
f9426a
===================================================================
f9426a
--- gdb-7.8.orig/gdb/testsuite/gdb.base/attach.exp	2014-09-07 19:12:45.067981589 +0200
f9426a
+++ gdb-7.8/gdb/testsuite/gdb.base/attach.exp	2014-09-07 19:12:48.601985706 +0200
f9426a
@@ -58,6 +58,37 @@ if [get_compiler_info] {
f9426a
     return -1
f9426a
 }
f9426a
 
f9426a
+# Start the program running and then wait for a bit, to be sure that
f9426a
+# it can be attached to.  Return the process's PID.
f9426a
+
f9426a
+proc spawn_test_prog { executable } {
f9426a
+    set testpid [eval exec $executable &]
f9426a
+    exec sleep 2
f9426a
+    if { [istarget "*-*-cygwin*"] } {
f9426a
+	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
f9426a
+	# different due to the way fork/exec works.
f9426a
+	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
f9426a
+    }
f9426a
+
f9426a
+    return $testpid
f9426a
+}
f9426a
+
f9426a
+# Spawn GDB with CMDLINE_FLAGS appended to the GDBFLAGS global.
f9426a
+
f9426a
+proc gdb_spawn_with_cmdline_opts { cmdline_flags } {
f9426a
+    global GDBFLAGS
f9426a
+
f9426a
+    set saved_gdbflags $GDBFLAGS
f9426a
+
f9426a
+    append GDBFLAGS $cmdline_flags
f9426a
+
f9426a
+    set res [gdb_spawn]
f9426a
+
f9426a
+    set GDBFLAGS $saved_gdbflags
f9426a
+
f9426a
+    return $res
f9426a
+}
f9426a
+
f9426a
 proc do_attach_tests {} {
f9426a
     global gdb_prompt
f9426a
     global binfile
f9426a
@@ -70,13 +101,7 @@ proc do_attach_tests {} {
f9426a
     # Start the program running and then wait for a bit, to be sure
f9426a
     # that it can be attached to.
f9426a
 
f9426a
-    set testpid [eval exec $binfile &]
f9426a
-    exec sleep 2
f9426a
-    if { [istarget "*-*-cygwin*"] } {
f9426a
-	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
f9426a
-	# different due to the way fork/exec works.
f9426a
-	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
f9426a
-    }
f9426a
+    set testpid [spawn_test_prog $binfile]
f9426a
 
f9426a
     # Verify that we cannot attach to nonsense.
f9426a
 
f9426a
@@ -279,16 +304,7 @@ proc do_attach_tests {} {
f9426a
    
f9426a
     remote_exec build "kill -9 ${testpid}"
f9426a
 
f9426a
-    # Start the program running and then wait for a bit, to be sure
f9426a
-    # that it can be attached to.
f9426a
-   
f9426a
-    set testpid [eval exec $binfile &]
f9426a
-    exec sleep 2
f9426a
-    if { [istarget "*-*-cygwin*"] } {
f9426a
-	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
f9426a
-	# different due to the way fork/exec works.
f9426a
-	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
f9426a
-    }
f9426a
+    set testpid [spawn_test_prog $binfile]
f9426a
 
f9426a
     # Verify that we can attach to the process, and find its a.out
f9426a
     # when we're cd'd to some directory that doesn't contain the
f9426a
@@ -335,16 +351,7 @@ proc do_call_attach_tests {} {
f9426a
     global gdb_prompt
f9426a
     global binfile2
f9426a
     
f9426a
-    # Start the program running and then wait for a bit, to be sure
f9426a
-    # that it can be attached to.
f9426a
-   
f9426a
-    set testpid [eval exec $binfile2 &]
f9426a
-    exec sleep 2
f9426a
-    if { [istarget "*-*-cygwin*"] } {
f9426a
-	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
f9426a
-	# different due to the way fork/exec works.
f9426a
-	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
f9426a
-    }
f9426a
+    set testpid [spawn_test_prog $binfile2]
f9426a
 
f9426a
     # Attach
f9426a
    
f9426a
@@ -397,16 +404,7 @@ proc do_command_attach_tests {} {
f9426a
 	return 0
f9426a
     }
f9426a
 
f9426a
-    # Start the program running and then wait for a bit, to be sure
f9426a
-    # that it can be attached to.
f9426a
-
f9426a
-    set testpid [eval exec $binfile &]
f9426a
-    exec sleep 2
f9426a
-    if { [istarget "*-*-cygwin*"] } {
f9426a
-	# testpid is the Cygwin PID, GDB uses the Windows PID, which might be
f9426a
-	# different due to the way fork/exec works.
f9426a
-	set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ]
f9426a
-    }
f9426a
+    set testpid [spawn_test_prog $binfile]
f9426a
 
f9426a
     gdb_exit
f9426a
     if $verbose>1 then {
f9426a
@@ -429,6 +427,50 @@ proc do_command_attach_tests {} {
f9426a
     remote_exec build "kill -9 ${testpid}"
f9426a
 }
f9426a
 
f9426a
+# Test ' gdb --pid PID -ex "run" '.  GDB used to have a bug where
f9426a
+# "run" would run before the attach finished - PR17347.
f9426a
+
f9426a
+proc test_command_line_attach_run {} {
f9426a
+    global gdb_prompt
f9426a
+    global binfile
f9426a
+    global verbose
f9426a
+    global GDB
f9426a
+    global INTERNAL_GDBFLAGS
f9426a
+
f9426a
+    if ![isnative] then {
f9426a
+	unsupported "commandline attach run test"
f9426a
+	return 0
f9426a
+    }
f9426a
+
f9426a
+    with_test_prefix "cmdline attach run" {
f9426a
+	set testpid [spawn_test_prog $binfile]
f9426a
+
f9426a
+	set test "run to prompt"
f9426a
+	gdb_exit
f9426a
+	set res [gdb_spawn_with_cmdline_opts "--pid=$testpid -ex \"start\""]
f9426a
+	if { $res != 0} {
f9426a
+	    fail $test
f9426a
+	    return $res
f9426a
+	}
f9426a
+	gdb_test_multiple "" $test {
f9426a
+	    -re {Attaching to.*Start it from the beginning\? \(y or n\) } {
f9426a
+		pass $test
f9426a
+	    }
f9426a
+	}
f9426a
+
f9426a
+	send_gdb "y\n"
f9426a
+
f9426a
+	set test "run to main"
f9426a
+	gdb_test_multiple "" $test {
f9426a
+	    -re "Temporary breakpoint .* main .*$gdb_prompt $" {
f9426a
+		pass $test
f9426a
+	    }
f9426a
+	}
f9426a
+
f9426a
+	# Get rid of the process
f9426a
+	remote_exec build "kill -9 ${testpid}"
f9426a
+    }
f9426a
+}
f9426a
 
f9426a
 # Start with a fresh gdb
f9426a
 
f9426a
@@ -453,4 +495,6 @@ do_call_attach_tests
f9426a
 
f9426a
 do_command_attach_tests
f9426a
 
f9426a
+test_command_line_attach_run
f9426a
+
f9426a
 return 0
f9426a
Index: gdb-7.8/gdb/top.c
f9426a
===================================================================
f9426a
--- gdb-7.8.orig/gdb/top.c	2014-09-07 19:12:45.067981589 +0200
f9426a
+++ gdb-7.8/gdb/top.c	2014-09-07 19:12:48.601985706 +0200
f9426a
@@ -375,6 +375,21 @@ check_frame_language_change (void)
f9426a
     }
f9426a
 }
f9426a
 
f9426a
+void
f9426a
+maybe_wait_sync_command_done (int was_sync)
f9426a
+{
f9426a
+  /* If the interpreter is in sync mode (we're running a user
f9426a
+     command's list, running command hooks or similars), and we
f9426a
+     just ran a synchronous command that started the target, wait
f9426a
+     for that command to end.  */
f9426a
+  if (!interpreter_async && !was_sync && sync_execution)
f9426a
+    {
f9426a
+      while (gdb_do_one_event () >= 0)
f9426a
+	if (!sync_execution)
f9426a
+	  break;
f9426a
+    }
f9426a
+}
f9426a
+
f9426a
 /* Execute the line P as a command, in the current user context.
f9426a
    Pass FROM_TTY as second argument to the defining function.  */
f9426a
 
f9426a
@@ -461,16 +476,7 @@ execute_command (char *p, int from_tty)
f9426a
       else
f9426a
 	cmd_func (c, arg, from_tty);
f9426a
 
f9426a
-      /* If the interpreter is in sync mode (we're running a user
f9426a
-	 command's list, running command hooks or similars), and we
f9426a
-	 just ran a synchronous command that started the target, wait
f9426a
-	 for that command to end.  */
f9426a
-      if (!interpreter_async && !was_sync && sync_execution)
f9426a
-	{
f9426a
-	  while (gdb_do_one_event () >= 0)
f9426a
-	    if (!sync_execution)
f9426a
-	      break;
f9426a
-	}
f9426a
+      maybe_wait_sync_command_done (was_sync);
f9426a
 
f9426a
       /* If this command has been post-hooked, run the hook last.  */
f9426a
       execute_cmd_post_hook (c);
f9426a
Index: gdb-7.8/gdb/top.h
f9426a
===================================================================
f9426a
--- gdb-7.8.orig/gdb/top.h	2014-09-07 19:12:45.068981590 +0200
f9426a
+++ gdb-7.8/gdb/top.h	2014-09-07 19:12:48.601985706 +0200
f9426a
@@ -42,6 +42,14 @@ extern void quit_command (char *, int);
f9426a
 extern void quit_cover (void);
f9426a
 extern void execute_command (char *, int);
f9426a
 
f9426a
+/* If the interpreter is in sync mode (we're running a user command's
f9426a
+   list, running command hooks or similars), and we just ran a
f9426a
+   synchronous command that started the target, wait for that command
f9426a
+   to end.  WAS_SYNC indicates whether sync_execution was set before
f9426a
+   the command was run.  */
f9426a
+
f9426a
+extern void maybe_wait_sync_command_done (int was_sync);
f9426a
+
f9426a
 extern void check_frame_language_change (void);
f9426a
 
f9426a
 /* Prepare for execution of a command.