fork-child.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Fork a Unix child process, and set up to debug it, for GDB.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "inferior.h"
  17. #include "gdbcmd.h"
  18. #include "terminal.h"
  19. #include "gdbthread.h"
  20. #include "top.h"
  21. #include "gdbsupport/job-control.h"
  22. #include "gdbsupport/filestuff.h"
  23. #include "nat/fork-inferior.h"
  24. #include "gdbsupport/common-inferior.h"
  25. /* The exec-wrapper, if any, that will be used when starting the
  26. inferior. */
  27. static std::string exec_wrapper;
  28. /* See gdbsupport/common-inferior.h. */
  29. const char *
  30. get_exec_wrapper ()
  31. {
  32. return !exec_wrapper.empty () ? exec_wrapper.c_str () : nullptr;
  33. }
  34. /* See nat/fork-inferior.h. */
  35. void
  36. gdb_flush_out_err ()
  37. {
  38. gdb_flush (main_ui->m_gdb_stdout);
  39. gdb_flush (main_ui->m_gdb_stderr);
  40. }
  41. /* The ui structure that will be saved on 'prefork_hook' and
  42. restored on 'postfork_hook'. */
  43. static struct ui *saved_ui = NULL;
  44. /* See nat/fork-inferior.h. */
  45. void
  46. prefork_hook (const char *args)
  47. {
  48. gdb_assert (saved_ui == NULL);
  49. /* Retain a copy of our UI, since the child will replace this value
  50. and if we're vforked, we have to restore it. */
  51. saved_ui = current_ui;
  52. /* Tell the terminal handling subsystem what tty we plan to run on;
  53. it will just record the information for later. */
  54. new_tty_prefork (current_inferior ()->tty ());
  55. }
  56. /* See nat/fork-inferior.h. */
  57. void
  58. postfork_hook (pid_t pid)
  59. {
  60. inferior *inf = current_inferior ();
  61. inferior_appeared (inf, pid);
  62. gdb_assert (saved_ui != NULL);
  63. current_ui = saved_ui;
  64. saved_ui = NULL;
  65. new_tty_postfork ();
  66. }
  67. /* See nat/fork-inferior.h. */
  68. void
  69. postfork_child_hook ()
  70. {
  71. /* This is set to the result of setpgrp, which if vforked, will be
  72. visible to you in the parent process. It's only used by humans
  73. for debugging. */
  74. static int debug_setpgrp = 657473;
  75. /* Make sure we switch to main_ui here in order to be able to
  76. use the gdb_printf/warning/error functions. */
  77. current_ui = main_ui;
  78. /* Create a new session for the inferior process, if necessary.
  79. It will also place the inferior in a separate process group. */
  80. if (create_tty_session () <= 0)
  81. {
  82. /* No session was created, but we still want to run the inferior
  83. in a separate process group. */
  84. debug_setpgrp = gdb_setpgid ();
  85. if (debug_setpgrp == -1)
  86. perror (_("setpgrp failed in child"));
  87. }
  88. /* Ask the tty subsystem to switch to the one we specified
  89. earlier (or to share the current terminal, if none was
  90. specified). */
  91. new_tty ();
  92. }
  93. /* See inferior.h. */
  94. ptid_t
  95. gdb_startup_inferior (pid_t pid, int num_traps)
  96. {
  97. inferior *inf = current_inferior ();
  98. process_stratum_target *proc_target = inf->process_target ();
  99. ptid_t ptid = startup_inferior (proc_target, pid, num_traps, NULL, NULL);
  100. /* Mark all threads non-executing. */
  101. set_executing (proc_target, ptid, false);
  102. return ptid;
  103. }
  104. /* Implement the "unset exec-wrapper" command. */
  105. static void
  106. unset_exec_wrapper_command (const char *args, int from_tty)
  107. {
  108. exec_wrapper.clear ();
  109. }
  110. static void
  111. show_startup_with_shell (struct ui_file *file, int from_tty,
  112. struct cmd_list_element *c, const char *value)
  113. {
  114. gdb_printf (file,
  115. _("Use of shell to start subprocesses is %s.\n"),
  116. value);
  117. }
  118. void _initialize_fork_child ();
  119. void
  120. _initialize_fork_child ()
  121. {
  122. add_setshow_filename_cmd ("exec-wrapper", class_run, &exec_wrapper, _("\
  123. Set a wrapper for running programs.\n\
  124. The wrapper prepares the system and environment for the new program."),
  125. _("\
  126. Show the wrapper for running programs."), NULL,
  127. NULL, NULL,
  128. &setlist, &showlist);
  129. add_cmd ("exec-wrapper", class_run, unset_exec_wrapper_command,
  130. _("Disable use of an execution wrapper."),
  131. &unsetlist);
  132. add_setshow_boolean_cmd ("startup-with-shell", class_support,
  133. &startup_with_shell, _("\
  134. Set use of shell to start subprocesses. The default is on."), _("\
  135. Show use of shell to start subprocesses."), NULL,
  136. NULL,
  137. show_startup_with_shell,
  138. &setlist, &showlist);
  139. }