Fix "start" for D, Rust, etc
[binutils-gdb.git] / gdb / fork-child.c
blobd210427f4b01c3068a2b98cc6f9b44692f769b22
1 /* Fork a Unix child process, and set up to debug it, for GDB.
3 Copyright (C) 1990-2023 Free Software Foundation, Inc.
5 Contributed by Cygnus Support.
7 This file is part of GDB.
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.
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.
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/>. */
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"
33 /* The exec-wrapper, if any, that will be used when starting the
34 inferior. */
36 static std::string exec_wrapper;
38 /* See gdbsupport/common-inferior.h. */
40 const char *
41 get_exec_wrapper ()
43 return !exec_wrapper.empty () ? exec_wrapper.c_str () : nullptr;
46 /* See nat/fork-inferior.h. */
48 void
49 gdb_flush_out_err ()
51 gdb_flush (main_ui->m_gdb_stdout);
52 gdb_flush (main_ui->m_gdb_stderr);
55 /* The ui structure that will be saved on 'prefork_hook' and
56 restored on 'postfork_hook'. */
57 static struct ui *saved_ui = NULL;
59 /* See nat/fork-inferior.h. */
61 void
62 prefork_hook (const char *args)
64 gdb_assert (saved_ui == NULL);
65 /* Retain a copy of our UI, since the child will replace this value
66 and if we're vforked, we have to restore it. */
67 saved_ui = current_ui;
69 /* Tell the terminal handling subsystem what tty we plan to run on;
70 it will just record the information for later. */
71 new_tty_prefork (current_inferior ()->tty ());
74 /* See nat/fork-inferior.h. */
76 void
77 postfork_hook (pid_t pid)
79 inferior *inf = current_inferior ();
81 inferior_appeared (inf, pid);
83 gdb_assert (saved_ui != NULL);
84 current_ui = saved_ui;
85 saved_ui = NULL;
87 new_tty_postfork ();
90 /* See nat/fork-inferior.h. */
92 void
93 postfork_child_hook ()
95 /* This is set to the result of setpgrp, which if vforked, will be
96 visible to you in the parent process. It's only used by humans
97 for debugging. */
98 static int debug_setpgrp = 657473;
100 /* Make sure we switch to main_ui here in order to be able to
101 use the gdb_printf/warning/error functions. */
102 current_ui = main_ui;
104 /* Create a new session for the inferior process, if necessary.
105 It will also place the inferior in a separate process group. */
106 if (create_tty_session () <= 0)
108 /* No session was created, but we still want to run the inferior
109 in a separate process group. */
110 debug_setpgrp = gdb_setpgid ();
111 if (debug_setpgrp == -1)
112 perror (_("setpgrp failed in child"));
115 /* Ask the tty subsystem to switch to the one we specified
116 earlier (or to share the current terminal, if none was
117 specified). */
118 new_tty ();
121 /* See inferior.h. */
123 ptid_t
124 gdb_startup_inferior (pid_t pid, int num_traps)
126 inferior *inf = current_inferior ();
127 process_stratum_target *proc_target = inf->process_target ();
129 scoped_restore save_starting_up
130 = make_scoped_restore (&inf->starting_up, true);
132 ptid_t ptid = startup_inferior (proc_target, pid, num_traps, NULL, NULL);
134 /* Mark all threads non-executing. */
135 set_executing (proc_target, ptid, false);
137 return ptid;
140 /* Implement the "unset exec-wrapper" command. */
142 static void
143 unset_exec_wrapper_command (const char *args, int from_tty)
145 exec_wrapper.clear ();
148 static void
149 show_startup_with_shell (struct ui_file *file, int from_tty,
150 struct cmd_list_element *c, const char *value)
152 gdb_printf (file,
153 _("Use of shell to start subprocesses is %s.\n"),
154 value);
157 void _initialize_fork_child ();
158 void
159 _initialize_fork_child ()
161 add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
162 Set a wrapper for running programs.\n\
163 The wrapper prepares the system and environment for the new program."),
164 _("\
165 Show the wrapper for running programs."), NULL,
166 NULL, NULL,
167 &setlist, &showlist);
169 add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
170 _("Disable use of an execution wrapper."),
171 &unsetlist);
173 add_setshow_boolean_cmd ("startup-with-shell", class_support,
174 &startup_with_shell, _("\
175 Set use of shell to start subprocesses. The default is on."), _("\
176 Show use of shell to start subprocesses."), NULL,
177 NULL,
178 show_startup_with_shell,
179 &setlist, &showlist);