1 /* Fork a Unix child process, and set up to debug it, for GDBserver.
2    Copyright (C) 1989-2020 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
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.
10 
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.
15 
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/>.  */
18 
19 #include "server.h"
20 #include "gdbsupport/job-control.h"
21 #include "nat/fork-inferior.h"
22 #ifdef HAVE_SIGNAL_H
23 #include <signal.h>
24 #endif
25 
26 #ifdef SIGTTOU
27 /* A file descriptor for the controlling terminal.  */
28 static int terminal_fd;
29 
30 /* TERMINAL_FD's original foreground group.  */
31 static pid_t old_foreground_pgrp;
32 
33 /* Hand back terminal ownership to the original foreground group.  */
34 
35 static void
restore_old_foreground_pgrp(void)36 restore_old_foreground_pgrp (void)
37 {
38   tcsetpgrp (terminal_fd, old_foreground_pgrp);
39 }
40 #endif
41 
42 /* See nat/fork-inferior.h.  */
43 
44 void
prefork_hook(const char * args)45 prefork_hook (const char *args)
46 {
47   client_state &cs = get_client_state ();
48   if (debug_threads)
49     {
50       debug_printf ("args: %s\n", args);
51       debug_flush ();
52     }
53 
54 #ifdef SIGTTOU
55   signal (SIGTTOU, SIG_DFL);
56   signal (SIGTTIN, SIG_DFL);
57 #endif
58 
59   /* Clear this so the backend doesn't get confused, thinking
60      CONT_THREAD died, and it needs to resume all threads.  */
61   cs.cont_thread = null_ptid;
62 }
63 
64 /* See nat/fork-inferior.h.  */
65 
66 void
postfork_hook(pid_t pid)67 postfork_hook (pid_t pid)
68 {
69 }
70 
71 /* See nat/fork-inferior.h.  */
72 
73 void
postfork_child_hook()74 postfork_child_hook ()
75 {
76   /* This is set to the result of setpgrp, which if vforked, will be
77      visible to you in the parent process.  It's only used by humans
78      for debugging.  */
79   static int debug_setpgrp = 657473;
80 
81   debug_setpgrp = gdb_setpgid ();
82   if (debug_setpgrp == -1)
83     perror (_("setpgrp failed in child"));
84 }
85 
86 /* See nat/fork-inferior.h.  */
87 
88 void
gdb_flush_out_err()89 gdb_flush_out_err ()
90 {
91   fflush (stdout);
92   fflush (stderr);
93 }
94 
95 /* See server.h.  */
96 
97 void
post_fork_inferior(int pid,const char * program)98 post_fork_inferior (int pid, const char *program)
99 {
100   client_state &cs = get_client_state ();
101 #ifdef SIGTTOU
102   signal (SIGTTOU, SIG_IGN);
103   signal (SIGTTIN, SIG_IGN);
104   terminal_fd = fileno (stderr);
105   old_foreground_pgrp = tcgetpgrp (terminal_fd);
106   tcsetpgrp (terminal_fd, pid);
107   atexit (restore_old_foreground_pgrp);
108 #endif
109 
110   startup_inferior (the_target, pid,
111 		    START_INFERIOR_TRAPS_EXPECTED,
112 		    &cs.last_status, &cs.last_ptid);
113   current_thread->last_resume_kind = resume_stop;
114   current_thread->last_status = cs.last_status;
115   signal_pid = pid;
116   target_post_create_inferior ();
117   fprintf (stderr, "Process %s created; pid = %d\n", program, pid);
118   fflush (stderr);
119 }
120