fork-child.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Fork a Unix child process, and set up to debug it, for GDBserver.
  2. Copyright (C) 1989-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "server.h"
  15. #include "gdbsupport/job-control.h"
  16. #include "nat/fork-inferior.h"
  17. #ifdef HAVE_SIGNAL_H
  18. #include <signal.h>
  19. #endif
  20. #ifdef SIGTTOU
  21. /* A file descriptor for the controlling terminal. */
  22. static int terminal_fd;
  23. /* TERMINAL_FD's original foreground group. */
  24. static pid_t old_foreground_pgrp;
  25. /* Hand back terminal ownership to the original foreground group. */
  26. static void
  27. restore_old_foreground_pgrp (void)
  28. {
  29. tcsetpgrp (terminal_fd, old_foreground_pgrp);
  30. }
  31. #endif
  32. /* See nat/fork-inferior.h. */
  33. void
  34. prefork_hook (const char *args)
  35. {
  36. client_state &cs = get_client_state ();
  37. threads_debug_printf ("args: %s", args);
  38. #ifdef SIGTTOU
  39. signal (SIGTTOU, SIG_DFL);
  40. signal (SIGTTIN, SIG_DFL);
  41. #endif
  42. /* Clear this so the backend doesn't get confused, thinking
  43. CONT_THREAD died, and it needs to resume all threads. */
  44. cs.cont_thread = null_ptid;
  45. }
  46. /* See nat/fork-inferior.h. */
  47. void
  48. postfork_hook (pid_t pid)
  49. {
  50. }
  51. /* See nat/fork-inferior.h. */
  52. void
  53. postfork_child_hook ()
  54. {
  55. /* This is set to the result of setpgrp, which if vforked, will be
  56. visible to you in the parent process. It's only used by humans
  57. for debugging. */
  58. static int debug_setpgrp = 657473;
  59. debug_setpgrp = gdb_setpgid ();
  60. if (debug_setpgrp == -1)
  61. perror (_("setpgrp failed in child"));
  62. }
  63. /* See nat/fork-inferior.h. */
  64. void
  65. gdb_flush_out_err ()
  66. {
  67. fflush (stdout);
  68. fflush (stderr);
  69. }
  70. /* See server.h. */
  71. void
  72. post_fork_inferior (int pid, const char *program)
  73. {
  74. client_state &cs = get_client_state ();
  75. #ifdef SIGTTOU
  76. signal (SIGTTOU, SIG_IGN);
  77. signal (SIGTTIN, SIG_IGN);
  78. terminal_fd = fileno (stderr);
  79. old_foreground_pgrp = tcgetpgrp (terminal_fd);
  80. tcsetpgrp (terminal_fd, pid);
  81. atexit (restore_old_foreground_pgrp);
  82. #endif
  83. startup_inferior (the_target, pid,
  84. START_INFERIOR_TRAPS_EXPECTED,
  85. &cs.last_status, &cs.last_ptid);
  86. current_thread->last_resume_kind = resume_stop;
  87. current_thread->last_status = cs.last_status;
  88. signal_pid = pid;
  89. target_post_create_inferior ();
  90. fprintf (stderr, "Process %s created; pid = %d\n", program, pid);
  91. fflush (stderr);
  92. }