94 const std::string &allargs)
101 std::string &args_copy =
m_storage = allargs;
103 m_argv.push_back (exec_file);
105 for (
size_t cur_pos = 0; cur_pos < args_copy.size ();)
108 std::size_t pos = args_copy.find_first_not_of (
" \t\n", cur_pos);
110 if (pos != std::string::npos)
114 std::size_t next_sep = args_copy.find_first_of (
" \t\n", cur_pos);
116 if (next_sep == std::string::npos)
120 next_sep = args_copy.size ();
125 args_copy[next_sep++] =
'\0';
128 m_argv.push_back (&args_copy[cur_pos]);
177 const std::string &allargs,
178 const char *shell_file)
180 const char *exec_wrapper = get_exec_wrapper ();
189 shell_command =
"exec ";
193 if (exec_wrapper != NULL)
195 shell_command += exec_wrapper;
196 shell_command +=
' ';
205 const char *p = exec_file;
223 need_to_quote =
true;
227 need_to_quote =
false;
238 shell_command +=
'\'';
239 for (p = exec_file; *p !=
'\0'; ++p)
242 shell_command +=
"'\\''";
243 else if (*p ==
'!' && escape_bang)
244 shell_command +=
"\\!";
248 shell_command +=
'\'';
251 shell_command += exec_file;
253 shell_command +=
' ' + allargs;
259 m_argv.push_back (shell_file);
261 m_argv.push_back (shell_command.c_str ());
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,
279 const char *shell_file;
280 const char *exec_file;
287 if (exec_file_arg == NULL)
288 exec_file = get_exec_file (1);
290 exec_file = exec_file_arg;
295 if (startup_with_shell)
297 shell_file = shell_file_arg;
300 if (shell_file == NULL)
301 shell_file = get_shell ();
303 gdb_assert (shell_file != NULL);
309 execv_argv child_argv (exec_file, allargs, shell_file);
327 std::string inferior_cwd = get_inferior_cwd ();
329 if (!inferior_cwd.empty ())
333 inferior_cwd = gdb_tilde_expand (inferior_cwd.c_str ());
339 if (pre_trace_fun != NULL)
353#if !(defined(__UCLIBC__) && defined(HAS_NOMMU))
354 if (pre_trace_fun || debug_fork)
361 perror_with_name ((
"vfork"));
373 if (!inferior_cwd.empty ())
375 if (chdir (inferior_cwd.c_str ()) < 0)
403 restore_original_signals_state ();
412 char **argv = child_argv.
argv ();
414 if (exec_fun != NULL)
415 (*exec_fun) (argv[0], &argv[0], env);
417 execvp (argv[0], &argv[0]);
421 warning (
"Cannot exec %s", argv[0]);
423 for (i = 1; argv[i] != NULL; i++)
424 warning (
" %s", argv[i]);
426 warning (
"Error: %s", safe_strerror (save_errno));
440 init_trace_fun (pid);
452 struct target_waitstatus *last_waitstatus,
455 int pending_execs = ntraps;
456 int terminal_initted = 0;
459 if (startup_with_shell)
465 if (target_supports_multi_process ())
466 resume_ptid = ptid_t (pid);
468 resume_ptid = minus_one_ptid;
473 if (get_exec_wrapper () != NULL)
478 enum gdb_signal resume_signal = GDB_SIGNAL_0;
481 struct target_waitstatus ws;
482 event_ptid = target_wait (resume_ptid, &ws, 0);
484 if (last_waitstatus != NULL)
485 *last_waitstatus = ws;
486 if (last_ptid != NULL)
487 *last_ptid = event_ptid;
489 if (ws.kind () == TARGET_WAITKIND_IGNORE)
495 case TARGET_WAITKIND_SPURIOUS:
496 case TARGET_WAITKIND_LOADED:
497 case TARGET_WAITKIND_FORKED:
498 case TARGET_WAITKIND_VFORKED:
499 case TARGET_WAITKIND_SYSCALL_ENTRY:
500 case TARGET_WAITKIND_SYSCALL_RETURN:
502 switch_to_thread (proc_target, event_ptid);
505 case TARGET_WAITKIND_SIGNALLED:
506 target_terminal::ours ();
507 target_mourn_inferior (event_ptid);
508 error (_(
"During startup program terminated with signal %s, %s."),
509 gdb_signal_to_name (ws.sig ()),
510 gdb_signal_to_string (ws.sig ()));
513 case TARGET_WAITKIND_EXITED:
514 target_terminal::ours ();
515 target_mourn_inferior (event_ptid);
516 if (ws.exit_status ())
517 error (_(
"During startup program exited with code %d."),
520 error (_(
"During startup program exited normally."));
523 case TARGET_WAITKIND_EXECD:
525 resume_signal = GDB_SIGNAL_TRAP;
526 switch_to_thread (proc_target, event_ptid);
529 case TARGET_WAITKIND_STOPPED:
530 resume_signal = ws.sig ();
531 switch_to_thread (proc_target, event_ptid);
535 if (resume_signal != GDB_SIGNAL_TRAP)
538 target_continue (resume_ptid, resume_signal);
543 if (!terminal_initted)
552 target_terminal::init ();
555 target_terminal::inferior ();
557 terminal_initted = 1;
560 if (--pending_execs == 0)
564 target_continue_no_signal (resume_ptid);
ptid_t startup_inferior(process_stratum_target *proc_target, pid_t pid, int ntraps, struct target_waitstatus *last_waitstatus, ptid_t *last_ptid)
pid_t fork_inferior(const char *exec_file_arg, const std::string &allargs, char **env, void(*traceme_fun)(), gdb::function_view< void(int)> init_trace_fun, void(*pre_trace_fun)(), const char *shell_file_arg, void(*exec_fun)(const char *file, char *const *argv, char *const *env))