GDB (xrefs)
/tmp/gdb-8.1/gdb/fork-child.c
Go to the documentation of this file.
1 /* Fork a Unix child process, and set up to debug it, for GDB.
2 
3  Copyright (C) 1990-2018 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 "job-control.h"
29 #include "filestuff.h"
30 #include "nat/fork-inferior.h"
31 #include "common/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 common/common-inferior.h. */
39 
40 const char *
42 {
43  return exec_wrapper;
44 }
45 
46 /* See nat/fork-inferior.h. */
47 
48 void
50 {
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. */
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
80 {
81  struct inferior *inf;
82 
83  if (!have_inferiors ())
85 
86  inf = current_inferior ();
87 
89 
90  /* Needed for wait_for_inferior stuff. */
92 
93  gdb_assert (saved_ui != NULL);
95  saved_ui = NULL;
96 
98 }
99 
100 /* See nat/fork-inferior.h. */
101 
102 void
104 {
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. */
113 
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)
117  {
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"));
123  }
124 
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 }
130 
131 /* See inferior.h. */
132 
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);
137 
138  /* Mark all threads non-executing. */
139  set_executing (ptid, 0);
140 
141  return ptid;
142 }
143 
144 /* Implement the "unset exec-wrapper" command. */
145 
146 static void
147 unset_exec_wrapper_command (const char *args, int from_tty)
148 {
150  exec_wrapper = NULL;
151 }
152 
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 
162 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);
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,
183  &setlist, &showlist);
184 }
void add_setshow_filename_cmd(const char *name, enum command_class theclass, char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:591
ptid_t startup_inferior(pid_t pid, int ntraps, struct target_waitstatus *last_waitstatus, ptid_t *last_ptid)
int gdb_setpgid()
Definition: job-control.c:41
struct ui * current_ui
Definition: event-top.c:453
int have_inferiors(void)
Definition: inferior.c:408
static char * exec_wrapper
Definition: fork-child.c:36
void xfree(void *)
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1824
void new_tty_prefork(const char *ttyname)
Definition: inflow.c:587
void new_tty_postfork(void)
Definition: inflow.c:671
void gdb_flush_out_err()
Definition: fork-child.c:49
void new_tty(void)
Definition: inflow.c:610
void prefork_hook(const char *args)
Definition: fork-child.c:62
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
#define _(String)
Definition: gdb_locale.h:35
static void show_startup_with_shell(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition: fork-child.c:154
struct cmd_list_element * setlist
Definition: cli-cmds.c:111
const char * get_exec_wrapper()
Definition: fork-child.c:41
static void unset_exec_wrapper_command(const char *args, int from_tty)
Definition: fork-child.c:147
const char * get_inferior_io_terminal(void)
Definition: infcmd.c:131
void fprintf_filtered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2008
Definition: ptid.h:35
struct cmd_list_element * showlist
Definition: cli-cmds.c:119
ptid_t pid_to_ptid(int pid)
Definition: ptid.c:39
struct cmd_list_element * unsetlist
Definition: cli-cmds.c:115
struct ui * main_ui
Definition: event-top.c:452
Definition: gnu-nat.c:174
Definition: top.h:56
ptid_t gdb_startup_inferior(pid_t pid, int num_traps)
Definition: fork-child.c:134
struct ui_file * m_gdb_stdout
Definition: top.h:139
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: value.c:169
static struct ui * saved_ui
Definition: fork-child.c:57
void postfork_hook(pid_t pid)
Definition: fork-child.c:79
ptid_t inferior_ptid
Definition: infcmd.c:94
void _initialize_fork_child(void)
Definition: fork-child.c:163
void add_setshow_boolean_cmd(const char *name, enum command_class theclass, int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:569
struct inferior * current_inferior(void)
Definition: inferior.c:58
int startup_with_shell
Definition: infcmd.c:111
char * args
Definition: inferior.h:352
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:93
pid_t create_tty_session(void)
Definition: inflow.c:736
void set_executing(ptid_t ptid, int executing)
Definition: thread.c:991
void inferior_appeared(struct inferior *inf, int pid)
Definition: inferior.c:268
void postfork_child_hook()
Definition: fork-child.c:103
void init_thread_list(void)
Definition: thread.c:221
struct ui_file * m_gdb_stderr
Definition: top.h:143