Sort includes for files gdb/[a-f]*.[chyl].
[deliverable/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-2019 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
24 /* Local non-gdb includes. */
25 #include "common/common-inferior.h"
26 #include "common/filestuff.h"
27 #include "common/job-control.h"
28 #include "gdbcmd.h"
29 #include "gdbthread.h"
30 #include "inferior.h"
31 #include "nat/fork-inferior.h"
32 #include "terminal.h"
33 #include "top.h"
34
35 /* The exec-wrapper, if any, that will be used when starting the
36 inferior. */
37
38 static char *exec_wrapper = NULL;
39
40 /* See common/common-inferior.h. */
41
42 const char *
43 get_exec_wrapper ()
44 {
45 return exec_wrapper;
46 }
47
48 /* See nat/fork-inferior.h. */
49
50 void
51 gdb_flush_out_err ()
52 {
53 gdb_flush (main_ui->m_gdb_stdout);
54 gdb_flush (main_ui->m_gdb_stderr);
55 }
56
57 /* The ui structure that will be saved on 'prefork_hook' and
58 restored on 'postfork_hook'. */
59 static struct ui *saved_ui = NULL;
60
61 /* See nat/fork-inferior.h. */
62
63 void
64 prefork_hook (const char *args)
65 {
66 const char *inferior_io_terminal = get_inferior_io_terminal ();
67
68 gdb_assert (saved_ui == NULL);
69 /* Retain a copy of our UI, since the child will replace this value
70 and if we're vforked, we have to restore it. */
71 saved_ui = current_ui;
72
73 /* Tell the terminal handling subsystem what tty we plan to run on;
74 it will just record the information for later. */
75 new_tty_prefork (inferior_io_terminal);
76 }
77
78 /* See nat/fork-inferior.h. */
79
80 void
81 postfork_hook (pid_t pid)
82 {
83 inferior *inf = current_inferior ();
84
85 inferior_appeared (inf, pid);
86
87 /* Needed for wait_for_inferior stuff. */
88 inferior_ptid = ptid_t (pid);
89
90 gdb_assert (saved_ui != NULL);
91 current_ui = saved_ui;
92 saved_ui = NULL;
93
94 new_tty_postfork ();
95 }
96
97 /* See nat/fork-inferior.h. */
98
99 void
100 postfork_child_hook ()
101 {
102 /* This is set to the result of setpgrp, which if vforked, will be
103 visible to you in the parent process. It's only used by humans
104 for debugging. */
105 static int debug_setpgrp = 657473;
106
107 /* Make sure we switch to main_ui here in order to be able to
108 use the fprintf_unfiltered/warning/error functions. */
109 current_ui = main_ui;
110
111 /* Create a new session for the inferior process, if necessary.
112 It will also place the inferior in a separate process group. */
113 if (create_tty_session () <= 0)
114 {
115 /* No session was created, but we still want to run the inferior
116 in a separate process group. */
117 debug_setpgrp = gdb_setpgid ();
118 if (debug_setpgrp == -1)
119 perror (_("setpgrp failed in child"));
120 }
121
122 /* Ask the tty subsystem to switch to the one we specified
123 earlier (or to share the current terminal, if none was
124 specified). */
125 new_tty ();
126 }
127
128 /* See inferior.h. */
129
130 ptid_t
131 gdb_startup_inferior (pid_t pid, int num_traps)
132 {
133 ptid_t ptid = startup_inferior (pid, num_traps, NULL, NULL);
134
135 /* Mark all threads non-executing. */
136 set_executing (ptid, 0);
137
138 return ptid;
139 }
140
141 /* Implement the "unset exec-wrapper" command. */
142
143 static void
144 unset_exec_wrapper_command (const char *args, int from_tty)
145 {
146 xfree (exec_wrapper);
147 exec_wrapper = NULL;
148 }
149
150 static void
151 show_startup_with_shell (struct ui_file *file, int from_tty,
152 struct cmd_list_element *c, const char *value)
153 {
154 fprintf_filtered (file,
155 _("Use of shell to start subprocesses is %s.\n"),
156 value);
157 }
158
159 void
160 _initialize_fork_child (void)
161 {
162 add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
163 Set a wrapper for running programs.\n\
164 The wrapper prepares the system and environment for the new program."),
165 _("\
166 Show the wrapper for running programs."), NULL,
167 NULL, NULL,
168 &setlist, &showlist);
169
170 add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
171 _("Disable use of an execution wrapper."),
172 &unsetlist);
173
174 add_setshow_boolean_cmd ("startup-with-shell", class_support,
175 &startup_with_shell, _("\
176 Set use of shell to start subprocesses. The default is on."), _("\
177 Show use of shell to start subprocesses."), NULL,
178 NULL,
179 show_startup_with_shell,
180 &setlist, &showlist);
181 }
This page took 0.051935 seconds and 5 git commands to generate.