[2/3] core-file: Never kill attached processes

Message ID 20151016204742.27288.17571.stgit@host1.jankratochvil.net
State New, archived
Headers

Commit Message

Jan Kratochvil Oct. 16, 2015, 8:47 p.m. UTC
  Hi,

even if the code of previous patch wrongly executed the core-file command it
should not kill inferiors which were attached (instead of spawned), similarly
to what quit command does.  This wrong behavior can be even reproduced with the
core-file command itself (and not to depend on special GDB commandline).

No regressions on {x86_64,x86_64-m32,i686}-fedora24pre-linux-gnu.

I wrote this fix just as such an obvious observation, I do not depend on this
part much.


Thanks,
Jan


gdb/ChangeLog
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* inferior.c (have_live_spawned_inferiors): New function.
	* inferior.h (have_live_spawned_inferiors): New declaration.
	* target.c (dispose_inferior): Check ATTACH_FLAG for target_kill.
	(target_preopen): Adjust query text for kill vs. detach according to
	have_live_spawned_inferiors.

gdb/testsuite/ChangeLog
2015-10-16  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* gdb.base/attach-kills.exp (attach for core-file, core-file): New
	tests.
---
 gdb/testsuite/gdb.base/attach-kills.exp |   14 ++++++++++++++
 1 file changed, 14 insertions(+)
  

Patch

diff --git a/gdb/inferior.c b/gdb/inferior.c
index ae8b2a1..54746a1 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -468,6 +468,24 @@  have_live_inferiors (void)
   return inf != NULL;
 }
 
+int
+have_live_spawned_inferiors (void)
+{
+  struct inferior *inf;
+
+  for (inf = inferior_list; inf; inf = inf->next)
+    if (inf->pid != 0 && !inf->attach_flag)
+      {
+	struct thread_info *tp;
+	
+	tp = any_thread_of_process (inf->pid);
+	if (tp && target_has_execution_1 (tp->ptid))
+	  break;
+      }
+
+  return inf != NULL;
+}
+
 /* Prune away any unused inferiors, and then prune away no longer used
    program spaces.  */
 
diff --git a/gdb/inferior.h b/gdb/inferior.h
index e09cb00..c105c39 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -482,6 +482,10 @@  extern int have_inferiors (void);
    (not cores, not executables, real live processes).  */
 extern int have_live_inferiors (void);
 
+/* Returns true if there are any live inferiors which have been spawned by GDB.
+   It is a subset of have_live_inferiors cases.  */
+extern int have_live_spawned_inferiors (void);
+
 /* Return a pointer to the current inferior.  It is an error to call
    this if there is no current inferior.  */
 extern struct inferior *current_inferior (void);
diff --git a/gdb/target.c b/gdb/target.c
index b8b1e9b..b80bdc7 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -2159,7 +2159,7 @@  dispose_inferior (struct inferior *inf, void *args)
       switch_to_thread (thread->ptid);
 
       /* Core inferiors actually should be detached, not killed.  */
-      if (target_has_execution)
+      if (target_has_execution && !inf->attach_flag)
 	target_kill ();
       else
 	target_detach (NULL, 0);
@@ -2180,7 +2180,9 @@  target_preopen (int from_tty)
     {
       if (!from_tty
 	  || !have_live_inferiors ()
-	  || query (_("A program is being debugged already.  Kill it? ")))
+	  || (have_live_spawned_inferiors ()
+	      ? query (_("A program is being debugged already.  Kill it? "))
+	      : query (_("A program is being debugged already.  Detach it? "))))
 	iterate_over_inferiors (dispose_inferior, NULL);
       else
 	error (_("Program not killed."));
diff --git a/gdb/testsuite/gdb.base/attach-kills.exp b/gdb/testsuite/gdb.base/attach-kills.exp
index 9a93cb7..d77524d 100644
--- a/gdb/testsuite/gdb.base/attach-kills.exp
+++ b/gdb/testsuite/gdb.base/attach-kills.exp
@@ -29,6 +29,20 @@  if { [build_executable ${testfile}.exp $testfile] == -1 } {
 set test_spawn_id [spawn_wait_for_attach $binfile]
 set testpid [spawn_id_get_pid $test_spawn_id]
 
+clean_restart $testfile
+
+set test "attach for core-file"
+gdb_test_multiple "attach $testpid" $test {
+    -re "Attaching to program: \[^\r\n\]*, process $testpid\r\n.*\r\n$gdb_prompt $" {
+        pass $test
+    }
+}
+
+gdb_test "core-file /AlSoDoEsNoTeXySt" {Program not killed\.} "core-file" \
+  {A program is being debugged already.  Detach it\? \(y or n\) } "n"
+
+gdb_exit
+
 remote_exec target "cp -pf -- $binfile $binfile-copy"
 remote_exec target "rm -f -- $binfile"