xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/nat/fork-inferior.c (revision 4b004442778f1201b2161e87fd65ba87aae6601a)
1 /* Fork a Unix child process, and set up to debug it, for GDB and GDBserver.
2 
3    Copyright (C) 1990-2020 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "gdbsupport/common-defs.h"
21 #include "fork-inferior.h"
22 #include "target/waitstatus.h"
23 #include "gdbsupport/filestuff.h"
24 #include "target/target.h"
25 #include "gdbsupport/common-inferior.h"
26 #include "gdbsupport/common-gdbthread.h"
27 #include "gdbsupport/pathstuff.h"
28 #include "gdbsupport/signals-state-save-restore.h"
29 #include "gdbsupport/gdb_tilde_expand.h"
30 #include <vector>
31 
32 extern char **environ;
33 
34 /* Build the argument vector for execv(3).  */
35 
36 class execv_argv
37 {
38 public:
39   /* EXEC_FILE is the file to run.  ALLARGS is a string containing the
40      arguments to the program.  If starting with a shell, SHELL_FILE
41      is the shell to run.  Otherwise, SHELL_FILE is NULL.  */
42   execv_argv (const char *exec_file, const std::string &allargs,
43 	      const char *shell_file);
44 
45   /* Return a pointer to the built argv, in the type expected by
46      execv.  The result is (only) valid for as long as this execv_argv
47      object is live.  We return a "char **" because that's the type
48      that the execv functions expect.  Note that it is guaranteed that
49      the execv functions do not modify the argv[] array nor the
50      strings to which the array point.  */
51   char **argv ()
52   {
53     return const_cast<char **> (&m_argv[0]);
54   }
55 
56 private:
57   DISABLE_COPY_AND_ASSIGN (execv_argv);
58 
59   /* Helper methods for constructing the argument vector.  */
60 
61   /* Used when building an argv for a straight execv call, without
62      going via the shell.  */
63   void init_for_no_shell (const char *exec_file,
64 			  const std::string &allargs);
65 
66   /* Used when building an argv for execing a shell that execs the
67      child program.  */
68   void init_for_shell (const char *exec_file,
69 		       const std::string &allargs,
70 		       const char *shell_file);
71 
72   /* The argument vector built.  Holds non-owning pointers.  Elements
73      either point to the strings passed to the execv_argv ctor, or
74      inside M_STORAGE.  */
75   std::vector<const char *> m_argv;
76 
77   /* Storage.  In the no-shell case, this contains a copy of the
78      arguments passed to the ctor, split by '\0'.  In the shell case,
79      this contains the quoted shell command.  I.e., SHELL_COMMAND in
80      {"$SHELL" "-c", SHELL_COMMAND, NULL}.  */
81   std::string m_storage;
82 };
83 
84 /* Create argument vector for straight call to execvp.  Breaks up
85    ALLARGS into an argument vector suitable for passing to execvp and
86    stores it in M_ARGV.  E.g., on "run a b c d" this routine would get
87    as input the string "a b c d", and as output it would fill in
88    M_ARGV with the four arguments "a", "b", "c", "d".  Each argument
89    in M_ARGV points to a substring of a copy of ALLARGS stored in
90    M_STORAGE.  */
91 
92 void
93 execv_argv::init_for_no_shell (const char *exec_file,
94 			       const std::string &allargs)
95 {
96 
97   /* Save/work with a copy stored in our storage.  The pointers pushed
98      to M_ARGV point directly into M_STORAGE, which is modified in
99      place with the necessary NULL terminators.  This avoids N heap
100      allocations and string dups when 1 is sufficient.  */
101   std::string &args_copy = m_storage = allargs;
102 
103   m_argv.push_back (exec_file);
104 
105   for (size_t cur_pos = 0; cur_pos < args_copy.size ();)
106     {
107       /* Skip whitespace-like chars.  */
108       std::size_t pos = args_copy.find_first_not_of (" \t\n", cur_pos);
109 
110       if (pos != std::string::npos)
111 	cur_pos = pos;
112 
113       /* Find the position of the next separator.  */
114       std::size_t next_sep = args_copy.find_first_of (" \t\n", cur_pos);
115 
116       if (next_sep == std::string::npos)
117 	{
118 	  /* No separator found, which means this is the last
119 	     argument.  */
120 	  next_sep = args_copy.size ();
121 	}
122       else
123 	{
124 	  /* Replace the separator with a terminator.  */
125 	  args_copy[next_sep++] = '\0';
126 	}
127 
128       m_argv.push_back (&args_copy[cur_pos]);
129 
130       cur_pos = next_sep;
131     }
132 
133   /* NULL-terminate the vector.  */
134   m_argv.push_back (NULL);
135 }
136 
137 /* When executing a command under the given shell, return true if the
138    '!' character should be escaped when embedded in a quoted
139    command-line argument.  */
140 
141 static bool
142 escape_bang_in_quoted_argument (const char *shell_file)
143 {
144   size_t shell_file_len = strlen (shell_file);
145 
146   /* Bang should be escaped only in C Shells.  For now, simply check
147      that the shell name ends with 'csh', which covers at least csh
148      and tcsh.  This should be good enough for now.  */
149 
150   if (shell_file_len < 3)
151     return false;
152 
153   if (shell_file[shell_file_len - 3] == 'c'
154       && shell_file[shell_file_len - 2] == 's'
155       && shell_file[shell_file_len - 1] == 'h')
156     return true;
157 
158   return false;
159 }
160 
161 /* See declaration.  */
162 
163 execv_argv::execv_argv (const char *exec_file,
164 			const std::string &allargs,
165 			const char *shell_file)
166 {
167   if (shell_file == NULL)
168     init_for_no_shell (exec_file, allargs);
169   else
170     init_for_shell (exec_file, allargs, shell_file);
171 }
172 
173 /* See declaration.  */
174 
175 void
176 execv_argv::init_for_shell (const char *exec_file,
177 			    const std::string &allargs,
178 			    const char *shell_file)
179 {
180   const char *exec_wrapper = get_exec_wrapper ();
181 
182   /* We're going to call a shell.  */
183   bool escape_bang = escape_bang_in_quoted_argument (shell_file);
184 
185   /* We need to build a new shell command string, and make argv point
186      to it.  So build it in the storage.  */
187   std::string &shell_command = m_storage;
188 
189   shell_command = "exec ";
190 
191   /* Add any exec wrapper.  That may be a program name with arguments,
192      so the user must handle quoting.  */
193   if (exec_wrapper != NULL)
194     {
195       shell_command += exec_wrapper;
196       shell_command += ' ';
197     }
198 
199   /* Now add exec_file, quoting as necessary.  */
200 
201   /* Quoting in this style is said to work with all shells.  But csh
202      on IRIX 4.0.1 can't deal with it.  So we only quote it if we need
203      to.  */
204   bool need_to_quote;
205   const char *p = exec_file;
206   while (1)
207     {
208       switch (*p)
209 	{
210 	case '\'':
211 	case '!':
212 	case '"':
213 	case '(':
214 	case ')':
215 	case '$':
216 	case '&':
217 	case ';':
218 	case '<':
219 	case '>':
220 	case ' ':
221 	case '\n':
222 	case '\t':
223 	  need_to_quote = true;
224 	  goto end_scan;
225 
226 	case '\0':
227 	  need_to_quote = false;
228 	  goto end_scan;
229 
230 	default:
231 	  break;
232 	}
233       ++p;
234     }
235  end_scan:
236   if (need_to_quote)
237     {
238       shell_command += '\'';
239       for (p = exec_file; *p != '\0'; ++p)
240 	{
241 	  if (*p == '\'')
242 	    shell_command += "'\\''";
243 	  else if (*p == '!' && escape_bang)
244 	    shell_command += "\\!";
245 	  else
246 	    shell_command += *p;
247 	}
248       shell_command += '\'';
249     }
250   else
251     shell_command += exec_file;
252 
253   shell_command += ' ' + allargs;
254 
255   /* If we decided above to start up with a shell, we exec the shell.
256      "-c" says to interpret the next arg as a shell command to
257      execute, and this command is "exec <target-program> <args>".  */
258   m_argv.reserve (4);
259   m_argv.push_back (shell_file);
260   m_argv.push_back ("-c");
261   m_argv.push_back (shell_command.c_str ());
262   m_argv.push_back (NULL);
263 }
264 
265 /* See nat/fork-inferior.h.  */
266 
267 pid_t
268 fork_inferior (const char *exec_file_arg, const std::string &allargs,
269 	       char **env, void (*traceme_fun) (),
270 	       gdb::function_view<void (int)> init_trace_fun,
271 	       void (*pre_trace_fun) (),
272 	       const char *shell_file_arg,
273                void (*exec_fun)(const char *file, char * const *argv,
274                                 char * const *env))
275 {
276   pid_t pid;
277   /* Set debug_fork then attach to the child while it sleeps, to debug.  */
278   int debug_fork = 0;
279   const char *shell_file;
280   const char *exec_file;
281   char **save_our_env;
282   int i;
283   int save_errno;
284   const char *inferior_cwd;
285   std::string expanded_inferior_cwd;
286 
287   /* If no exec file handed to us, get it from the exec-file command
288      -- with a good, common error message if none is specified.  */
289   if (exec_file_arg == NULL)
290     exec_file = get_exec_file (1);
291   else
292     exec_file = exec_file_arg;
293 
294   /* 'startup_with_shell' is declared in inferior.h and bound to the
295      "set startup-with-shell" option.  If 0, we'll just do a
296      fork/exec, no shell, so don't bother figuring out what shell.  */
297   if (startup_with_shell)
298     {
299       shell_file = shell_file_arg;
300 
301       /* Figure out what shell to start up the user program under.  */
302       if (shell_file == NULL)
303 	shell_file = get_shell ();
304 
305       gdb_assert (shell_file != NULL);
306     }
307   else
308     shell_file = NULL;
309 
310   /* Build the argument vector.  */
311   execv_argv child_argv (exec_file, allargs, shell_file);
312 
313   /* Retain a copy of our environment variables, since the child will
314      replace the value of environ and if we're vforked, we have to
315      restore it.  */
316   save_our_env = environ;
317 
318   /* Perform any necessary actions regarding to TTY before the
319      fork/vfork call.  */
320   prefork_hook (allargs.c_str ());
321 
322   /* It is generally good practice to flush any possible pending stdio
323      output prior to doing a fork, to avoid the possibility of both
324      the parent and child flushing the same data after the fork.  */
325   gdb_flush_out_err ();
326 
327   /* Check if the user wants to set a different working directory for
328      the inferior.  */
329   inferior_cwd = get_inferior_cwd ();
330 
331   if (inferior_cwd != NULL)
332     {
333       /* Expand before forking because between fork and exec, the child
334 	 process may only execute async-signal-safe operations.  */
335       expanded_inferior_cwd = gdb_tilde_expand (inferior_cwd);
336       inferior_cwd = expanded_inferior_cwd.c_str ();
337     }
338 
339   /* If there's any initialization of the target layers that must
340      happen to prepare to handle the child we're about fork, do it
341      now...  */
342   if (pre_trace_fun != NULL)
343     (*pre_trace_fun) ();
344 
345   /* Create the child process.  Since the child process is going to
346      exec(3) shortly afterwards, try to reduce the overhead by
347      calling vfork(2).  However, if PRE_TRACE_FUN is non-null, it's
348      likely that this optimization won't work since there's too much
349      work to do between the vfork(2) and the exec(3).  This is known
350      to be the case on ttrace(2)-based HP-UX, where some handshaking
351      between parent and child needs to happen between fork(2) and
352      exec(2).  However, since the parent is suspended in the vforked
353      state, this doesn't work.  Also note that the vfork(2) call might
354      actually be a call to fork(2) due to the fact that autoconf will
355      ``#define vfork fork'' on certain platforms.  */
356 #if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
357   if (pre_trace_fun || debug_fork)
358     pid = fork ();
359   else
360 #endif
361     pid = vfork ();
362 
363   if (pid < 0)
364     perror_with_name (("vfork"));
365 
366   if (pid == 0)
367     {
368       /* Close all file descriptors except those that gdb inherited
369 	 (usually 0/1/2), so they don't leak to the inferior.  Note
370 	 that this closes the file descriptors of all secondary
371 	 UIs.  */
372       close_most_fds ();
373 
374       /* Change to the requested working directory if the user
375 	 requested it.  */
376       if (inferior_cwd != NULL)
377 	{
378 	  if (chdir (inferior_cwd) < 0)
379 	    trace_start_error_with_name (inferior_cwd);
380 	}
381 
382       if (debug_fork)
383 	sleep (debug_fork);
384 
385       /* Execute any necessary post-fork actions before we exec.  */
386       postfork_child_hook ();
387 
388       /* Changing the signal handlers for the inferior after
389          a vfork can also change them for the superior, so we don't mess
390          with signals here.  See comments in
391          initialize_signals for how we get the right signal handlers
392          for the inferior.  */
393 
394       /* "Trace me, Dr. Memory!"  */
395       (*traceme_fun) ();
396 
397       /* The call above set this process (the "child") as debuggable
398         by the original gdb process (the "parent").  Since processes
399         (unlike people) can have only one parent, if you are debugging
400         gdb itself (and your debugger is thus _already_ the
401         controller/parent for this child), code from here on out is
402         undebuggable.  Indeed, you probably got an error message
403         saying "not parent".  Sorry; you'll have to use print
404         statements!  */
405 
406       restore_original_signals_state ();
407 
408       /* There is no execlpe call, so we have to set the environment
409          for our child in the global variable.  If we've vforked, this
410          clobbers the parent, but environ is restored a few lines down
411          in the parent.  By the way, yes we do need to look down the
412          path to find $SHELL.  Rich Pixley says so, and I agree.  */
413       environ = env;
414 
415       char **argv = child_argv.argv ();
416 
417       if (exec_fun != NULL)
418         (*exec_fun) (argv[0], &argv[0], env);
419       else
420         execvp (argv[0], &argv[0]);
421 
422       /* If we get here, it's an error.  */
423       save_errno = errno;
424       warning ("Cannot exec %s", argv[0]);
425 
426       for (i = 1; argv[i] != NULL; i++)
427 	warning (" %s", argv[i]);
428 
429       warning ("Error: %s", safe_strerror (save_errno));
430 
431       _exit (0177);
432     }
433 
434   /* Restore our environment in case a vforked child clob'd it.  */
435   environ = save_our_env;
436 
437   postfork_hook (pid);
438 
439   /* Now that we have a child process, make it our target, and
440      initialize anything target-vector-specific that needs
441      initializing.  */
442   if (init_trace_fun)
443     init_trace_fun (pid);
444 
445   /* We are now in the child process of interest, having exec'd the
446      correct program, and are poised at the first instruction of the
447      new program.  */
448   return pid;
449 }
450 
451 /* See nat/fork-inferior.h.  */
452 
453 ptid_t
454 startup_inferior (process_stratum_target *proc_target, pid_t pid, int ntraps,
455 		  struct target_waitstatus *last_waitstatus,
456 		  ptid_t *last_ptid)
457 {
458   int pending_execs = ntraps;
459   int terminal_initted = 0;
460   ptid_t resume_ptid;
461 
462   if (startup_with_shell)
463     {
464       /* One trap extra for exec'ing the shell.  */
465       pending_execs++;
466     }
467 
468   if (target_supports_multi_process ())
469     resume_ptid = ptid_t (pid);
470   else
471     resume_ptid = minus_one_ptid;
472 
473   /* The process was started by the fork that created it, but it will
474      have stopped one instruction after execing the shell.  Here we
475      must get it up to actual execution of the real program.  */
476   if (get_exec_wrapper () != NULL)
477     pending_execs++;
478 
479   while (1)
480     {
481       enum gdb_signal resume_signal = GDB_SIGNAL_0;
482       ptid_t event_ptid;
483 
484       struct target_waitstatus ws;
485       memset (&ws, 0, sizeof (ws));
486       event_ptid = target_wait (resume_ptid, &ws, 0);
487 
488       if (last_waitstatus != NULL)
489 	*last_waitstatus = ws;
490       if (last_ptid != NULL)
491 	*last_ptid = event_ptid;
492 
493       if (ws.kind == TARGET_WAITKIND_IGNORE)
494 	/* The inferior didn't really stop, keep waiting.  */
495 	continue;
496 
497       switch (ws.kind)
498 	{
499 	  case TARGET_WAITKIND_SPURIOUS:
500 	  case TARGET_WAITKIND_LOADED:
501 	  case TARGET_WAITKIND_FORKED:
502 	  case TARGET_WAITKIND_VFORKED:
503 	  case TARGET_WAITKIND_SYSCALL_ENTRY:
504 	  case TARGET_WAITKIND_SYSCALL_RETURN:
505 	    /* Ignore gracefully during startup of the inferior.  */
506 	    break;
507 
508 	  case TARGET_WAITKIND_SIGNALLED:
509 	    target_terminal::ours ();
510 	    target_mourn_inferior (event_ptid);
511 	    error (_("During startup program terminated with signal %s, %s."),
512 		   gdb_signal_to_name (ws.value.sig),
513 		   gdb_signal_to_string (ws.value.sig));
514 	    return resume_ptid;
515 
516 	  case TARGET_WAITKIND_EXITED:
517 	    target_terminal::ours ();
518 	    target_mourn_inferior (event_ptid);
519 	    if (ws.value.integer)
520 	      error (_("During startup program exited with code %d."),
521 		     ws.value.integer);
522 	    else
523 	      error (_("During startup program exited normally."));
524 	    return resume_ptid;
525 
526 	  case TARGET_WAITKIND_EXECD:
527 	    /* Handle EXEC signals as if they were SIGTRAP signals.  */
528 	    /* Free the exec'ed pathname, but only if this isn't the
529 	       waitstatus we are returning to the caller.  */
530 	    if (pending_execs != 1)
531 	      xfree (ws.value.execd_pathname);
532 	    resume_signal = GDB_SIGNAL_TRAP;
533 	    switch_to_thread (proc_target, event_ptid);
534 	    break;
535 
536 	  case TARGET_WAITKIND_STOPPED:
537 	    resume_signal = ws.value.sig;
538 	    /* Ignore gracefully the !TRAP signals intercepted from the shell.  */
539 	    if (resume_signal == GDB_SIGNAL_TRAP)
540 	      switch_to_thread (proc_target, event_ptid);
541 	    break;
542 	}
543 
544       if (resume_signal != GDB_SIGNAL_TRAP)
545 	{
546 	  /* Let shell child handle its own signals in its own way.  */
547 	  target_continue (resume_ptid, resume_signal);
548 	}
549       else
550 	{
551 	  /* We handle SIGTRAP, however; it means child did an exec.  */
552 	  if (!terminal_initted)
553 	    {
554 	      /* Now that the child has exec'd we know it has already
555 	         set its process group.  On POSIX systems, tcsetpgrp
556 	         will fail with EPERM if we try it before the child's
557 	         setpgid.  */
558 
559 	      /* Set up the "saved terminal modes" of the inferior
560 	         based on what modes we are starting it with.  */
561 	      target_terminal::init ();
562 
563 	      /* Install inferior's terminal modes.  */
564 	      target_terminal::inferior ();
565 
566 	      terminal_initted = 1;
567 	    }
568 
569 	  if (--pending_execs == 0)
570 	    break;
571 
572 	  /* Just make it go on.  */
573 	  target_continue_no_signal (resume_ptid);
574 	}
575     }
576 
577   return resume_ptid;
578 }
579 
580 /* See nat/fork-inferior.h.  */
581 
582 void
583 trace_start_error (const char *fmt, ...)
584 {
585   va_list ap;
586 
587   va_start (ap, fmt);
588   warning ("Could not trace the inferior process.");
589   vwarning (fmt, ap);
590   va_end (ap);
591 
592   gdb_flush_out_err ();
593   _exit (0177);
594 }
595 
596 /* See nat/fork-inferior.h.  */
597 
598 void
599 trace_start_error_with_name (const char *string)
600 {
601   trace_start_error ("%s: %s", string, safe_strerror (errno));
602 }
603