Automatic date update in version.in
[binutils-gdb.git] / gdbserver / fork-child.cc
blob7e4c69b31bd53b66b3d7a2b41b3591adc9f456e4
1 /* Fork a Unix child process, and set up to debug it, for GDBserver.
2 Copyright (C) 1989-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "gdbsupport/job-control.h"
20 #include "gdbsupport/scoped_restore.h"
21 #include "nat/fork-inferior.h"
22 #ifdef HAVE_SIGNAL_H
23 #include <signal.h>
24 #endif
26 #ifdef SIGTTOU
27 /* A file descriptor for the controlling terminal. */
28 static int terminal_fd;
30 /* TERMINAL_FD's original foreground group. */
31 static pid_t old_foreground_pgrp;
33 /* Hand back terminal ownership to the original foreground group. */
35 static void
36 restore_old_foreground_pgrp (void)
38 tcsetpgrp (terminal_fd, old_foreground_pgrp);
40 #endif
42 /* See nat/fork-inferior.h. */
44 void
45 prefork_hook (const char *args)
47 client_state &cs = get_client_state ();
48 threads_debug_printf ("args: %s", args);
50 #ifdef SIGTTOU
51 signal (SIGTTOU, SIG_DFL);
52 signal (SIGTTIN, SIG_DFL);
53 #endif
55 /* Clear this so the backend doesn't get confused, thinking
56 CONT_THREAD died, and it needs to resume all threads. */
57 cs.cont_thread = null_ptid;
60 /* See nat/fork-inferior.h. */
62 void
63 postfork_hook (pid_t pid)
67 /* See nat/fork-inferior.h. */
69 void
70 postfork_child_hook ()
72 /* This is set to the result of setpgrp, which if vforked, will be
73 visible to you in the parent process. It's only used by humans
74 for debugging. */
75 static int debug_setpgrp = 657473;
77 debug_setpgrp = gdb_setpgid ();
78 if (debug_setpgrp == -1)
79 perror (_("setpgrp failed in child"));
82 /* See nat/fork-inferior.h. */
84 void
85 gdb_flush_out_err ()
87 fflush (stdout);
88 fflush (stderr);
91 /* See server.h. */
93 void
94 post_fork_inferior (int pid, const char *program)
96 client_state &cs = get_client_state ();
97 #ifdef SIGTTOU
98 signal (SIGTTOU, SIG_IGN);
99 signal (SIGTTIN, SIG_IGN);
100 terminal_fd = fileno (stderr);
101 old_foreground_pgrp = tcgetpgrp (terminal_fd);
102 tcsetpgrp (terminal_fd, pid);
103 atexit (restore_old_foreground_pgrp);
104 #endif
106 process_info *proc = find_process_pid (pid);
108 /* If the inferior fails to start, startup_inferior mourns the
109 process (which deletes it), and then throws an error. This means
110 that on exception return, we don't need or want to clear this
111 flag back, as PROC won't exist anymore. Thus, we don't use a
112 scoped_restore. */
113 proc->starting_up = true;
115 startup_inferior (the_target, pid,
116 START_INFERIOR_TRAPS_EXPECTED,
117 &cs.last_status, &cs.last_ptid);
119 /* If we get here, the process was successfully started. */
120 proc->starting_up = false;
122 current_thread->last_resume_kind = resume_stop;
123 current_thread->last_status = cs.last_status;
124 signal_pid = pid;
125 target_post_create_inferior ();
126 fprintf (stderr, "Process %s created; pid = %d\n", program, pid);
127 fflush (stderr);