]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/fork-child.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / fork-child.c
1 /* Fork a Unix child process, and set up to debug it, for GDB.
2
3 Copyright (C) 1990-2020 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Support.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "gdbcmd.h"
25 #include "terminal.h"
26 #include "gdbthread.h"
27 #include "top.h"
28 #include "gdbsupport/job-control.h"
29 #include "gdbsupport/filestuff.h"
30 #include "nat/fork-inferior.h"
31 #include "gdbsupport/common-inferior.h"
32
33 /* The exec-wrapper, if any, that will be used when starting the
34 inferior. */
35
36 static char *exec_wrapper = NULL;
37
38 /* See gdbsupport/common-inferior.h. */
39
40 const char *
41 get_exec_wrapper ()
42 {
43 return exec_wrapper;
44 }
45
46 /* See nat/fork-inferior.h. */
47
48 void
49 gdb_flush_out_err ()
50 {
51 gdb_flush (main_ui->m_gdb_stdout);
52 gdb_flush (main_ui->m_gdb_stderr);
53 }
54
55 /* The ui structure that will be saved on 'prefork_hook' and
56 restored on 'postfork_hook'. */
57 static struct ui *saved_ui = NULL;
58
59 /* See nat/fork-inferior.h. */
60
61 void
62 prefork_hook (const char *args)
63 {
64 const char *inferior_io_terminal = get_inferior_io_terminal ();
65
66 gdb_assert (saved_ui == NULL);
67 /* Retain a copy of our UI, since the child will replace this value
68 and if we're vforked, we have to restore it. */
69 saved_ui = current_ui;
70
71 /* Tell the terminal handling subsystem what tty we plan to run on;
72 it will just record the information for later. */
73 new_tty_prefork (inferior_io_terminal);
74 }
75
76 /* See nat/fork-inferior.h. */
77
78 void
79 postfork_hook (pid_t pid)
80 {
81 inferior *inf = current_inferior ();
82
83 inferior_appeared (inf, pid);
84
85 /* Needed for wait_for_inferior stuff. */
86 inferior_ptid = ptid_t (pid);
87
88 gdb_assert (saved_ui != NULL);
89 current_ui = saved_ui;
90 saved_ui = NULL;
91
92 new_tty_postfork ();
93 }
94
95 /* See nat/fork-inferior.h. */
96
97 void
98 postfork_child_hook ()
99 {
100 /* This is set to the result of setpgrp, which if vforked, will be
101 visible to you in the parent process. It's only used by humans
102 for debugging. */
103 static int debug_setpgrp = 657473;
104
105 /* Make sure we switch to main_ui here in order to be able to
106 use the fprintf_unfiltered/warning/error functions. */
107 current_ui = main_ui;
108
109 /* Create a new session for the inferior process, if necessary.
110 It will also place the inferior in a separate process group. */
111 if (create_tty_session () <= 0)
112 {
113 /* No session was created, but we still want to run the inferior
114 in a separate process group. */
115 debug_setpgrp = gdb_setpgid ();
116 if (debug_setpgrp == -1)
117 perror (_("setpgrp failed in child"));
118 }
119
120 /* Ask the tty subsystem to switch to the one we specified
121 earlier (or to share the current terminal, if none was
122 specified). */
123 new_tty ();
124 }
125
126 /* See inferior.h. */
127
128 ptid_t
129 gdb_startup_inferior (pid_t pid, int num_traps)
130 {
131 inferior *inf = current_inferior ();
132 process_stratum_target *proc_target = inf->process_target ();
133
134 ptid_t ptid = startup_inferior (proc_target, pid, num_traps, NULL, NULL);
135
136 /* Mark all threads non-executing. */
137 set_executing (proc_target, ptid, false);
138
139 return ptid;
140 }
141
142 /* Implement the "unset exec-wrapper" command. */
143
144 static void
145 unset_exec_wrapper_command (const char *args, int from_tty)
146 {
147 xfree (exec_wrapper);
148 exec_wrapper = NULL;
149 }
150
151 static void
152 show_startup_with_shell (struct ui_file *file, int from_tty,
153 struct cmd_list_element *c, const char *value)
154 {
155 fprintf_filtered (file,
156 _("Use of shell to start subprocesses is %s.\n"),
157 value);
158 }
159
160 void _initialize_fork_child ();
161 void
162 _initialize_fork_child ()
163 {
164 add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
165 Set a wrapper for running programs.\n\
166 The wrapper prepares the system and environment for the new program."),
167 _("\
168 Show the wrapper for running programs."), NULL,
169 NULL, NULL,
170 &setlist, &showlist);
171
172 add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
173 _("Disable use of an execution wrapper."),
174 &unsetlist);
175
176 add_setshow_boolean_cmd ("startup-with-shell", class_support,
177 &startup_with_shell, _("\
178 Set use of shell to start subprocesses. The default is on."), _("\
179 Show use of shell to start subprocesses."), NULL,
180 NULL,
181 show_startup_with_shell,
182 &setlist, &showlist);
183 }