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