]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/fork-child.c
Fix regression on aarch64-linux gdbserver
[thirdparty/binutils-gdb.git] / gdb / fork-child.c
CommitLineData
c906108c 1/* Fork a Unix child process, and set up to debug it, for GDB.
74a4fe32 2
1d506c26 3 Copyright (C) 1990-2024 Free Software Foundation, Inc.
74a4fe32 4
c906108c
SS
5 Contributed by Cygnus Support.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
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.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 21
d55e5aa6 22#include "inferior.h"
5b9707eb 23#include "cli/cli-cmds.h"
d55e5aa6 24#include "terminal.h"
4de283e4 25#include "gdbthread.h"
13d03262 26#include "ui.h"
268a13a5
TT
27#include "gdbsupport/job-control.h"
28#include "gdbsupport/filestuff.h"
4de283e4 29#include "nat/fork-inferior.h"
268a13a5 30#include "gdbsupport/common-inferior.h"
6037b830 31
2090129c
SDJ
32/* The exec-wrapper, if any, that will be used when starting the
33 inferior. */
808480f6 34
e0700ba4 35static std::string exec_wrapper;
808480f6 36
268a13a5 37/* See gdbsupport/common-inferior.h. */
808480f6 38
2090129c
SDJ
39const char *
40get_exec_wrapper ()
808480f6 41{
e0700ba4 42 return !exec_wrapper.empty () ? exec_wrapper.c_str () : nullptr;
808480f6
PA
43}
44
2090129c 45/* See nat/fork-inferior.h. */
808480f6
PA
46
47void
2090129c 48gdb_flush_out_err ()
808480f6 49{
2090129c
SDJ
50 gdb_flush (main_ui->m_gdb_stdout);
51 gdb_flush (main_ui->m_gdb_stderr);
6037b830 52}
c906108c 53
2090129c
SDJ
54/* The ui structure that will be saved on 'prefork_hook' and
55 restored on 'postfork_hook'. */
56static struct ui *saved_ui = NULL;
0db8980c 57
2090129c 58/* See nat/fork-inferior.h. */
0db8980c
SDJ
59
60void
2090129c 61prefork_hook (const char *args)
0db8980c 62{
2090129c
SDJ
63 gdb_assert (saved_ui == NULL);
64 /* Retain a copy of our UI, since the child will replace this value
65 and if we're vforked, we have to restore it. */
66 saved_ui = current_ui;
49940788 67
c906108c
SS
68 /* Tell the terminal handling subsystem what tty we plan to run on;
69 it will just record the information for later. */
05779d57 70 new_tty_prefork (current_inferior ()->tty ());
2090129c 71}
c906108c 72
2090129c 73/* See nat/fork-inferior.h. */
c906108c 74
2090129c
SDJ
75void
76postfork_hook (pid_t pid)
77{
08036331 78 inferior *inf = current_inferior ();
6c95b8df
PA
79
80 inferior_appeared (inf, pid);
7f9f62ba 81
2090129c
SDJ
82 gdb_assert (saved_ui != NULL);
83 current_ui = saved_ui;
84 saved_ui = NULL;
191c4426 85
2090129c 86 new_tty_postfork ();
c906108c
SS
87}
88
2090129c 89/* See nat/fork-inferior.h. */
c906108c
SS
90
91void
2090129c 92postfork_child_hook ()
c906108c 93{
2090129c
SDJ
94 /* This is set to the result of setpgrp, which if vforked, will be
95 visible to you in the parent process. It's only used by humans
96 for debugging. */
97 static int debug_setpgrp = 657473;
98
99 /* Make sure we switch to main_ui here in order to be able to
6cb06a8c 100 use the gdb_printf/warning/error functions. */
2090129c 101 current_ui = main_ui;
d90e17a7 102
2090129c
SDJ
103 /* Create a new session for the inferior process, if necessary.
104 It will also place the inferior in a separate process group. */
105 if (create_tty_session () <= 0)
98882a26 106 {
2090129c
SDJ
107 /* No session was created, but we still want to run the inferior
108 in a separate process group. */
109 debug_setpgrp = gdb_setpgid ();
110 if (debug_setpgrp == -1)
111 perror (_("setpgrp failed in child"));
98882a26
PA
112 }
113
2090129c
SDJ
114 /* Ask the tty subsystem to switch to the one we specified
115 earlier (or to share the current terminal, if none was
116 specified). */
117 new_tty ();
118}
c906108c 119
2090129c 120/* See inferior.h. */
ccd213ac 121
2090129c
SDJ
122ptid_t
123gdb_startup_inferior (pid_t pid, int num_traps)
124{
5b6d1e4f
PA
125 inferior *inf = current_inferior ();
126 process_stratum_target *proc_target = inf->process_target ();
127
a9deee17
PA
128 scoped_restore save_starting_up
129 = make_scoped_restore (&inf->starting_up, true);
130
5b6d1e4f 131 ptid_t ptid = startup_inferior (proc_target, pid, num_traps, NULL, NULL);
2e979b94
PA
132
133 /* Mark all threads non-executing. */
719546c4 134 set_executing (proc_target, ptid, false);
2090129c
SDJ
135
136 return ptid;
c906108c 137}
ccd213ac
DJ
138
139/* Implement the "unset exec-wrapper" command. */
140
141static void
e0b2930c 142unset_exec_wrapper_command (const char *args, int from_tty)
ccd213ac 143{
e0700ba4 144 exec_wrapper.clear ();
ccd213ac
DJ
145}
146
98882a26
PA
147static void
148show_startup_with_shell (struct ui_file *file, int from_tty,
149 struct cmd_list_element *c, const char *value)
150{
6cb06a8c
TT
151 gdb_printf (file,
152 _("Use of shell to start subprocesses is %s.\n"),
153 value);
98882a26
PA
154}
155
6c265988 156void _initialize_fork_child ();
ccd213ac 157void
6c265988 158_initialize_fork_child ()
ccd213ac
DJ
159{
160 add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
161Set a wrapper for running programs.\n\
162The wrapper prepares the system and environment for the new program."),
163 _("\
164Show the wrapper for running programs."), NULL,
165 NULL, NULL,
166 &setlist, &showlist);
167
168 add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
dda83cd7
SM
169 _("Disable use of an execution wrapper."),
170 &unsetlist);
98882a26
PA
171
172 add_setshow_boolean_cmd ("startup-with-shell", class_support,
173 &startup_with_shell, _("\
174Set use of shell to start subprocesses. The default is on."), _("\
175Show use of shell to start subprocesses."), NULL,
176 NULL,
177 show_startup_with_shell,
178 &setlist, &showlist);
ccd213ac 179}