GDBserver
fork-child.c
Go to the documentation of this file.
1 /* Fork a Unix child process, and set up to debug it, for GDBserver.
2  Copyright (C) 1989-2018 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 "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
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
45 prefork_hook (const char *args)
46 {
47  if (debug_threads)
48  {
49  debug_printf ("args: %s\n", args);
50  debug_flush ();
51  }
52 
53 #ifdef SIGTTOU
54  signal (SIGTTOU, SIG_DFL);
55  signal (SIGTTIN, SIG_DFL);
56 #endif
57 
58  /* Clear this so the backend doesn't get confused, thinking
59  CONT_THREAD died, and it needs to resume all threads. */
61 }
62 
63 /* See nat/fork-inferior.h. */
64 
65 void
66 postfork_hook (pid_t pid)
67 {
68 }
69 
70 /* See nat/fork-inferior.h. */
71 
72 void
74 {
75  /* This is set to the result of setpgrp, which if vforked, will be
76  visible to you in the parent process. It's only used by humans
77  for debugging. */
78  static int debug_setpgrp = 657473;
79 
80  debug_setpgrp = gdb_setpgid ();
81  if (debug_setpgrp == -1)
82  perror (_("setpgrp failed in child"));
83 }
84 
85 /* See nat/fork-inferior.h. */
86 
87 void
89 {
90  fflush (stdout);
91  fflush (stderr);
92 }
93 
94 /* See server.h. */
95 
96 void
97 post_fork_inferior (int pid, const char *program)
98 {
99 #ifdef SIGTTOU
100  signal (SIGTTOU, SIG_IGN);
101  signal (SIGTTIN, SIG_IGN);
102  terminal_fd = fileno (stderr);
103  old_foreground_pgrp = tcgetpgrp (terminal_fd);
104  tcsetpgrp (terminal_fd, pid);
105  atexit (restore_old_foreground_pgrp);
106 #endif
107 
110  current_thread->last_resume_kind = resume_stop;
112  signal_pid = pid;
114  fprintf (stderr, "Process %s created; pid = %d\n", program, pid);
115  fflush (stderr);
116 }
int debug_threads
Definition: debug.c:24
ptid_t startup_inferior(pid_t pid, int ntraps, struct target_waitstatus *last_waitstatus, ptid_t *last_ptid)
struct thread_info * current_thread
Definition: inferiors.c:28
int gdb_setpgid()
Definition: job-control.c:41
void debug_flush(void)
Definition: debug.c:66
void gdb_flush_out_err()
Definition: fork-child.c:88
unsigned long signal_pid
Definition: server.c:124
void prefork_hook(const char *args)
Definition: fork-child.c:45
#define _(String)
Definition: gdb_locale.h:35
void perror(const char *)
Definition: wincecompat.c:24
#define target_post_create_inferior()
Definition: target.h:492
enum resume_kind last_resume_kind
Definition: gdbthread.h:39
ptid_t cont_thread
Definition: server.c:75
void post_fork_inferior(int pid, const char *program)
Definition: fork-child.c:97
struct target_waitstatus last_status
Definition: server.c:135
ptid_t null_ptid
Definition: ptid.c:25
void postfork_hook(pid_t pid)
Definition: fork-child.c:66
struct target_waitstatus last_status
Definition: gdbthread.h:42
#define START_INFERIOR_TRAPS_EXPECTED
Definition: fork-inferior.h:29
void debug_printf(const char *fmt,...)
Definition: common-debug.c:30
ptid_t last_ptid
Definition: server.c:136
void postfork_child_hook()
Definition: fork-child.c:73