int resume_parent = -1;
/* This exec or exit marks the end of the shared memory region
- between the parent and the child. If the user wanted to
- detach from the parent, now is the time. */
+ between the parent and the child. Break the bonds. */
+ inferior *vfork_parent = inf->vfork_parent;
+ inf->vfork_parent->vfork_child = NULL;
+ inf->vfork_parent = NULL;
- if (inf->vfork_parent->pending_detach)
+ /* If the user wanted to detach from the parent, now is the
+ time. */
+ if (vfork_parent->pending_detach)
{
struct thread_info *tp;
struct program_space *pspace;
/* follow-fork child, detach-on-fork on. */
- inf->vfork_parent->pending_detach = 0;
+ vfork_parent->pending_detach = 0;
gdb::optional<scoped_restore_exited_inferior>
maybe_restore_inferior;
maybe_restore_thread.emplace ();
/* We're letting loose of the parent. */
- tp = any_live_thread_of_inferior (inf->vfork_parent);
+ tp = any_live_thread_of_inferior (vfork_parent);
switch_to_thread (tp);
/* We're about to detach from the parent, which implicitly
if (print_inferior_events)
{
std::string pidstr
- = target_pid_to_str (ptid_t (inf->vfork_parent->pid));
+ = target_pid_to_str (ptid_t (vfork_parent->pid));
target_terminal::ours_for_output ();
}
}
- target_detach (inf->vfork_parent, 0);
+ target_detach (vfork_parent, 0);
/* Put it back. */
inf->pspace = pspace;
inf->removable = 1;
set_current_program_space (inf->pspace);
- resume_parent = inf->vfork_parent->pid;
-
- /* Break the bonds. */
- inf->vfork_parent->vfork_child = NULL;
+ resume_parent = vfork_parent->pid;
}
else
{
set_current_program_space (pspace);
inf->removable = 1;
inf->symfile_flags = SYMFILE_NO_READ;
- clone_program_space (pspace, inf->vfork_parent->pspace);
+ clone_program_space (pspace, vfork_parent->pspace);
inf->pspace = pspace;
inf->aspace = pspace->aspace;
- resume_parent = inf->vfork_parent->pid;
- /* Break the bonds. */
- inf->vfork_parent->vfork_child = NULL;
+ resume_parent = vfork_parent->pid;
}
- inf->vfork_parent = NULL;
-
gdb_assert (current_program_space == inf->pspace);
if (non_stop && resume_parent != -1)
+2019-04-18 Tom de Vries <tdevries@suse.de>
+ Pedro Alves <palves@redhat.com>
+
+ PR gdb/24454
+ * gdb.threads/vfork-follow-child-exec.c: New file.
+ * gdb.threads/vfork-follow-child-exec.exp: New file.
+ * gdb.threads/vfork-follow-child-exit.c: New file.
+ * gdb.threads/vfork-follow-child-exit.exp: New file.
+
2019-04-15 Leszek Swirski <leszeks@google.com>
* gdb.arch/amd64-eval.cc: New file.
--- /dev/null
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2019 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <string.h>
+#include <stdlib.h>
+
+static char *program_name;
+
+static void *
+f (void *arg)
+{
+ int res = vfork ();
+
+ if (res == -1)
+ {
+ perror ("vfork");
+ return NULL;
+ }
+ else if (res == 0)
+ {
+ /* Child. */
+ execl (program_name, program_name, "1", NULL);
+ perror ("exec");
+ abort ();
+ }
+ else
+ {
+ /* Parent. */
+ return NULL;
+ }
+}
+
+int
+main (int argc, char **argv)
+{
+ pthread_t tid;
+
+ if (argc > 1)
+ {
+ /* Getting here via execl. */
+ return 0;
+ }
+
+ program_name = argv[0];
+
+ pthread_create (&tid, NULL, f, NULL);
+ pthread_join (tid, NULL);
+ return 0;
+}
--- /dev/null
+# Copyright (C) 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+# Test following a vfork child that execs, when the vfork parent is a
+# threaded program, and it's a non-main thread that vforks.
+
+standard_testfile
+
+if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}]} {
+ return -1
+}
+
+# DETACH indicates whether "set detach-on-fork" is enabled. It is
+# either "on" or "off".
+
+proc test_vfork {detach} {
+ global binfile
+
+ clean_restart $binfile
+
+ if ![runto_main] then {
+ fail "can't run to main"
+ return 0
+ }
+
+ delete_breakpoints
+
+ gdb_test_no_output "set follow-fork-mode child"
+ gdb_test_no_output "set detach-on-fork $detach"
+
+ if {$detach == "off"} {
+ gdb_test "continue" \
+ [multi_line \
+ "Attaching after .* vfork to child .*" \
+ ".*New inferior 2 .*" \
+ ".* is executing new program: .*" \
+ ".*Inferior 2 .* exited normally.*"]
+ } else {
+ gdb_test "continue" \
+ [multi_line \
+ "Attaching after .* vfork to child .*" \
+ ".*New inferior 2 .*" \
+ ".*Detaching vfork parent process .* after child exec.*" \
+ ".*Inferior 1 .* detached.*" \
+ ".*is executing new program: .*" \
+ ".*Inferior 2 .*exited normally.*"]
+ }
+}
+
+foreach_with_prefix detach-on-fork {"off" "on"} {
+ test_vfork ${detach-on-fork}
+}
--- /dev/null
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2019 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+
+static void *
+f (void *arg)
+{
+ int res = vfork ();
+
+ if (res == -1)
+ {
+ perror ("vfork");
+ return NULL;
+ }
+ else if (res == 0)
+ {
+ /* Child. */
+ _exit (0);
+ }
+ else
+ {
+ /* Parent. */
+ return NULL;
+ }
+}
+
+int
+main (void)
+{
+ pthread_t tid;
+
+ pthread_create (&tid, NULL, f, NULL);
+ pthread_join (tid, NULL);
+ return 0;
+}
--- /dev/null
+# Copyright (C) 2019 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+# Test following a vfork child that exits, when the vfork parent is a
+# threaded program, and it's a non-main thread that vforks.
+
+standard_testfile
+
+if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}]} {
+ return -1
+}
+
+# DETACH indicates whether "set detach-on-fork" is enabled. It is
+# either "on" or "off".
+
+proc test_vfork {detach} {
+ global binfile
+
+ clean_restart $binfile
+
+ if ![runto_main] then {
+ fail "can't run to main"
+ return 0
+ }
+
+ gdb_test_no_output "set follow-fork-mode child"
+ gdb_test_no_output "set detach-on-fork $detach"
+
+ if {$detach == "off"} {
+ gdb_test "continue" \
+ [multi_line \
+ "Attaching after .* vfork to child .*" \
+ ".*New inferior 2 .*" \
+ ".*Inferior 2 .*exited normally.*"]
+ } else {
+ gdb_test "continue" \
+ [multi_line \
+ "Attaching after .* vfork to child .*" \
+ ".*New inferior 2 .*" \
+ ".*Detaching vfork parent process .* after child exit.*" \
+ ".*Inferior 1 .* detached.*" \
+ ".*Inferior 2 .*exited normally.*"]
+ }
+}
+
+foreach_with_prefix detach-on-fork {"off" "on"} {
+ test_vfork ${detach-on-fork}
+}