[2/3] core-file: Never kill attached processes
Commit Message
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(+)
@@ -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. */
@@ -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);
@@ -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."));
@@ -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"