inflow.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2. Copyright (C) 1986-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 "defs.h"
  15. #include "frame.h"
  16. #include "inferior.h"
  17. #include "command.h"
  18. #include "serial.h"
  19. #include "terminal.h"
  20. #include "target.h"
  21. #include "gdbthread.h"
  22. #include "observable.h"
  23. #include <signal.h>
  24. #include <fcntl.h>
  25. #include "gdbsupport/gdb_select.h"
  26. #include "gdbcmd.h"
  27. #ifdef HAVE_TERMIOS_H
  28. #include <termios.h>
  29. #endif
  30. #include "gdbsupport/job-control.h"
  31. #include "gdbsupport/scoped_ignore_sigttou.h"
  32. #ifdef HAVE_SYS_IOCTL_H
  33. #include <sys/ioctl.h>
  34. #endif
  35. #ifndef O_NOCTTY
  36. #define O_NOCTTY 0
  37. #endif
  38. static void pass_signal (int);
  39. static void child_terminal_ours_1 (target_terminal_state);
  40. /* Record terminal status separately for debugger and inferior. */
  41. static struct serial *stdin_serial;
  42. /* Terminal related info we need to keep track of. Each inferior
  43. holds an instance of this structure --- we save it whenever the
  44. corresponding inferior stops, and restore it to the terminal when
  45. the inferior is resumed in the foreground. */
  46. struct terminal_info
  47. {
  48. terminal_info () = default;
  49. ~terminal_info ();
  50. terminal_info &operator= (const terminal_info &) = default;
  51. /* The name of the tty (from the `tty' command) that we gave to the
  52. inferior when it was started. */
  53. std::string run_terminal;
  54. /* TTY state. We save it whenever the inferior stops, and restore
  55. it when it resumes in the foreground. */
  56. serial_ttystate ttystate {};
  57. #ifdef HAVE_TERMIOS_H
  58. /* The terminal's foreground process group. Saved whenever the
  59. inferior stops. This is the pgrp displayed by "info terminal".
  60. Note that this may be not the inferior's actual process group,
  61. since each inferior that we spawn has its own process group, and
  62. only one can be in the foreground at a time. When the inferior
  63. resumes, if we can determine the inferior's actual pgrp, then we
  64. make that the foreground pgrp instead of what was saved here.
  65. While it's a bit arbitrary which inferior's pgrp ends up in the
  66. foreground when we resume several inferiors, this at least makes
  67. 'resume inf1+inf2' + 'stop all' + 'resume inf2' end up with
  68. inf2's pgrp in the foreground instead of inf1's (which would be
  69. problematic since it would be left stopped: Ctrl-C wouldn't work,
  70. for example). */
  71. pid_t process_group = 0;
  72. #endif
  73. /* fcntl flags. Saved and restored just like ttystate. */
  74. int tflags = 0;
  75. };
  76. /* Our own tty state, which we restore every time we need to deal with
  77. the terminal. This is set once, when GDB first starts, and then
  78. whenever we enter/leave TUI mode (gdb_save_tty_state). The
  79. settings of flags which readline saves and restores are
  80. unimportant. */
  81. static struct terminal_info our_terminal_info;
  82. /* Snapshot of the initial tty state taken during initialization of
  83. GDB, before readline/ncurses have had a chance to change it. This
  84. is used as the initial tty state given to each new spawned
  85. inferior. Unlike our_terminal_info, this is only ever set
  86. once. */
  87. static serial_ttystate initial_gdb_ttystate;
  88. static struct terminal_info *get_inflow_inferior_data (struct inferior *);
  89. /* While the inferior is running, we want SIGINT and SIGQUIT to go to the
  90. inferior only. If we have job control, that takes care of it. If not,
  91. we save our handlers in these two variables and set SIGINT and SIGQUIT
  92. to SIG_IGN. */
  93. static sighandler_t sigint_ours;
  94. #ifdef SIGQUIT
  95. static sighandler_t sigquit_ours;
  96. #endif
  97. /* The name of the tty (from the `tty' command) that we're giving to
  98. the inferior when starting it up. This is only (and should only
  99. be) used as a transient global by new_tty_prefork,
  100. create_tty_session, new_tty and new_tty_postfork, all called from
  101. fork_inferior, while forking a new child. */
  102. static std::string inferior_thisrun_terminal;
  103. /* Track who owns GDB's terminal (is it GDB or some inferior?). While
  104. target_terminal::is_ours() etc. tracks the core's intention and is
  105. independent of the target backend, this tracks the actual state of
  106. GDB's own tty. So for example,
  107. (target_terminal::is_inferior () && gdb_tty_state == terminal_is_ours)
  108. is true when the (native) inferior is not sharing a terminal with
  109. GDB (e.g., because we attached to an inferior that is running on a
  110. different terminal). */
  111. static target_terminal_state gdb_tty_state = target_terminal_state::is_ours;
  112. /* See terminal.h. */
  113. void
  114. set_initial_gdb_ttystate (void)
  115. {
  116. /* Note we can't do any of this in _initialize_inflow because at
  117. that point stdin_serial has not been created yet. */
  118. initial_gdb_ttystate = serial_get_tty_state (stdin_serial);
  119. if (initial_gdb_ttystate != NULL)
  120. {
  121. our_terminal_info.ttystate
  122. = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
  123. #ifdef F_GETFL
  124. our_terminal_info.tflags = fcntl (0, F_GETFL, 0);
  125. #endif
  126. #ifdef HAVE_TERMIOS_H
  127. our_terminal_info.process_group = tcgetpgrp (0);
  128. #endif
  129. }
  130. }
  131. /* Does GDB have a terminal (on stdin)? */
  132. static int
  133. gdb_has_a_terminal (void)
  134. {
  135. return initial_gdb_ttystate != NULL;
  136. }
  137. /* Macro for printing errors from ioctl operations */
  138. #define OOPSY(what) \
  139. if (result == -1) \
  140. gdb_printf(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
  141. what, safe_strerror (errno))
  142. /* Initialize the terminal settings we record for the inferior,
  143. before we actually run the inferior. */
  144. void
  145. child_terminal_init (struct target_ops *self)
  146. {
  147. if (!gdb_has_a_terminal ())
  148. return;
  149. inferior *inf = current_inferior ();
  150. terminal_info *tinfo = get_inflow_inferior_data (inf);
  151. #ifdef HAVE_TERMIOS_H
  152. /* A child we spawn should be a process group leader (PGID==PID) at
  153. this point, though that may not be true if we're attaching to an
  154. existing process. */
  155. tinfo->process_group = inf->pid;
  156. #endif
  157. xfree (tinfo->ttystate);
  158. tinfo->ttystate = serial_copy_tty_state (stdin_serial, initial_gdb_ttystate);
  159. }
  160. /* Save the terminal settings again. This is necessary for the TUI
  161. when it switches to TUI or non-TUI mode; curses changes the terminal
  162. and gdb must be able to restore it correctly. */
  163. void
  164. gdb_save_tty_state (void)
  165. {
  166. if (gdb_has_a_terminal ())
  167. {
  168. xfree (our_terminal_info.ttystate);
  169. our_terminal_info.ttystate = serial_get_tty_state (stdin_serial);
  170. }
  171. }
  172. /* Try to determine whether TTY is GDB's input terminal. Returns
  173. TRIBOOL_UNKNOWN if we can't tell. */
  174. static tribool
  175. is_gdb_terminal (const char *tty)
  176. {
  177. struct stat gdb_tty;
  178. struct stat other_tty;
  179. int res;
  180. res = stat (tty, &other_tty);
  181. if (res == -1)
  182. return TRIBOOL_UNKNOWN;
  183. res = fstat (STDIN_FILENO, &gdb_tty);
  184. if (res == -1)
  185. return TRIBOOL_UNKNOWN;
  186. return ((gdb_tty.st_dev == other_tty.st_dev
  187. && gdb_tty.st_ino == other_tty.st_ino)
  188. ? TRIBOOL_TRUE
  189. : TRIBOOL_FALSE);
  190. }
  191. /* Helper for sharing_input_terminal. Try to determine whether
  192. inferior INF is using the same TTY for input as GDB is. Returns
  193. TRIBOOL_UNKNOWN if we can't tell. */
  194. static tribool
  195. sharing_input_terminal_1 (inferior *inf)
  196. {
  197. /* Using host-dependent code here is fine, because the
  198. child_terminal_foo functions are meant to be used by child/native
  199. targets. */
  200. #if defined (__linux__) || defined (__sun__)
  201. char buf[100];
  202. xsnprintf (buf, sizeof (buf), "/proc/%d/fd/0", inf->pid);
  203. return is_gdb_terminal (buf);
  204. #else
  205. return TRIBOOL_UNKNOWN;
  206. #endif
  207. }
  208. /* Return true if the inferior is using the same TTY for input as GDB
  209. is. If this is true, then we save/restore terminal flags/state.
  210. This is necessary because if inf->attach_flag is set, we don't
  211. offhand know whether we are sharing a terminal with the inferior or
  212. not. Attaching a process without a terminal is one case where we
  213. do not; attaching a process which we ran from the same shell as GDB
  214. via `&' is one case where we do.
  215. If we can't determine, we assume the TTY is being shared. This
  216. works OK if you're only debugging one inferior. However, if you're
  217. debugging more than one inferior, and e.g., one is spawned by GDB
  218. with "run" (sharing terminal with GDB), and another is attached to
  219. (and running on a different terminal, as is most common), then it
  220. matters, because we can only restore the terminal settings of one
  221. of the inferiors, and in that scenario, we want to restore the
  222. settings of the "run"'ed inferior.
  223. Note, this is not the same as determining whether GDB and the
  224. inferior are in the same session / connected to the same
  225. controlling tty. An inferior (fork child) may call setsid,
  226. disconnecting itself from the ctty, while still leaving
  227. stdin/stdout/stderr associated with the original terminal. If
  228. we're debugging that process, we should also save/restore terminal
  229. settings. */
  230. static bool
  231. sharing_input_terminal (inferior *inf)
  232. {
  233. terminal_info *tinfo = get_inflow_inferior_data (inf);
  234. tribool res = sharing_input_terminal_1 (inf);
  235. if (res == TRIBOOL_UNKNOWN)
  236. {
  237. /* As fallback, if we can't determine by stat'ing the inferior's
  238. tty directly (because it's not supported on this host) and
  239. the child was spawned, check whether run_terminal is our tty.
  240. This isn't ideal, since this is checking the child's
  241. controlling terminal, not the input terminal (which may have
  242. been redirected), but is still better than nothing. A false
  243. positive ("set inferior-tty" points to our terminal, but I/O
  244. was redirected) is much more likely than a false negative
  245. ("set inferior-tty" points to some other terminal, and then
  246. output was redirected to our terminal), and with a false
  247. positive we just end up trying to save/restore terminal
  248. settings when we didn't need to or we actually can't. */
  249. if (!tinfo->run_terminal.empty ())
  250. res = is_gdb_terminal (tinfo->run_terminal.c_str ());
  251. /* If we still can't determine, assume yes. */
  252. if (res == TRIBOOL_UNKNOWN)
  253. return true;
  254. }
  255. return res == TRIBOOL_TRUE;
  256. }
  257. /* Put the inferior's terminal settings into effect. This is
  258. preparation for starting or resuming the inferior. */
  259. void
  260. child_terminal_inferior (struct target_ops *self)
  261. {
  262. /* If we resume more than one inferior in the foreground on GDB's
  263. terminal, then the first inferior's terminal settings "win".
  264. Note that every child process is put in its own process group, so
  265. the first process that ends up resumed ends up determining which
  266. process group the kernel forwards Ctrl-C/Ctrl-Z (SIGINT/SIGTTOU)
  267. to. */
  268. if (gdb_tty_state == target_terminal_state::is_inferior)
  269. return;
  270. inferior *inf = current_inferior ();
  271. terminal_info *tinfo = get_inflow_inferior_data (inf);
  272. if (gdb_has_a_terminal ()
  273. && tinfo->ttystate != NULL
  274. && sharing_input_terminal (inf))
  275. {
  276. int result;
  277. /* Ignore SIGTTOU since it will happen when we try to set the
  278. terminal's state (if gdb_tty_state is currently
  279. ours_for_output). */
  280. scoped_ignore_sigttou ignore_sigttou;
  281. #ifdef F_GETFL
  282. result = fcntl (0, F_SETFL, tinfo->tflags);
  283. OOPSY ("fcntl F_SETFL");
  284. #endif
  285. result = serial_set_tty_state (stdin_serial, tinfo->ttystate);
  286. OOPSY ("setting tty state");
  287. if (!job_control)
  288. {
  289. sigint_ours = signal (SIGINT, SIG_IGN);
  290. #ifdef SIGQUIT
  291. sigquit_ours = signal (SIGQUIT, SIG_IGN);
  292. #endif
  293. }
  294. if (job_control)
  295. {
  296. #ifdef HAVE_TERMIOS_H
  297. /* If we can't tell the inferior's actual process group,
  298. then restore whatever was the foreground pgrp the last
  299. time the inferior was running. See also comments
  300. describing terminal_state::process_group. */
  301. #ifdef HAVE_GETPGID
  302. result = tcsetpgrp (0, getpgid (inf->pid));
  303. #else
  304. result = tcsetpgrp (0, tinfo->process_group);
  305. #endif
  306. if (result == -1)
  307. {
  308. #if 0
  309. /* This fails if either GDB has no controlling terminal,
  310. e.g., running under 'setsid(1)', or if the inferior
  311. is not attached to GDB's controlling terminal. E.g.,
  312. if it called setsid to create a new session or used
  313. the TIOCNOTTY ioctl, or simply if we've attached to a
  314. process running on another terminal and we couldn't
  315. tell whether it was sharing GDB's terminal (and so
  316. assumed yes). */
  317. gdb_printf
  318. (gdb_stderr,
  319. "[tcsetpgrp failed in child_terminal_inferior: %s]\n",
  320. safe_strerror (errno));
  321. #endif
  322. }
  323. #endif
  324. }
  325. gdb_tty_state = target_terminal_state::is_inferior;
  326. }
  327. }
  328. /* Put some of our terminal settings into effect,
  329. enough to get proper results from our output,
  330. but do not change into or out of RAW mode
  331. so that no input is discarded.
  332. After doing this, either terminal_ours or terminal_inferior
  333. should be called to get back to a normal state of affairs.
  334. N.B. The implementation is (currently) no different than
  335. child_terminal_ours. See child_terminal_ours_1. */
  336. void
  337. child_terminal_ours_for_output (struct target_ops *self)
  338. {
  339. child_terminal_ours_1 (target_terminal_state::is_ours_for_output);
  340. }
  341. /* Put our terminal settings into effect.
  342. First record the inferior's terminal settings
  343. so they can be restored properly later.
  344. N.B. Targets that want to use this with async support must build that
  345. support on top of this (e.g., the caller still needs to add stdin to the
  346. event loop). E.g., see linux_nat_terminal_ours. */
  347. void
  348. child_terminal_ours (struct target_ops *self)
  349. {
  350. child_terminal_ours_1 (target_terminal_state::is_ours);
  351. }
  352. /* Save the current terminal settings in the inferior's terminal_info
  353. cache. */
  354. void
  355. child_terminal_save_inferior (struct target_ops *self)
  356. {
  357. /* Avoid attempting all the ioctl's when running in batch. */
  358. if (!gdb_has_a_terminal ())
  359. return;
  360. inferior *inf = current_inferior ();
  361. terminal_info *tinfo = get_inflow_inferior_data (inf);
  362. /* No need to save/restore if the inferior is not sharing GDB's
  363. tty. */
  364. if (!sharing_input_terminal (inf))
  365. return;
  366. xfree (tinfo->ttystate);
  367. tinfo->ttystate = serial_get_tty_state (stdin_serial);
  368. #ifdef HAVE_TERMIOS_H
  369. tinfo->process_group = tcgetpgrp (0);
  370. #endif
  371. #ifdef F_GETFL
  372. tinfo->tflags = fcntl (0, F_GETFL, 0);
  373. #endif
  374. }
  375. /* Switch terminal state to DESIRED_STATE, either is_ours, or
  376. is_ours_for_output. */
  377. static void
  378. child_terminal_ours_1 (target_terminal_state desired_state)
  379. {
  380. gdb_assert (desired_state != target_terminal_state::is_inferior);
  381. /* Avoid attempting all the ioctl's when running in batch. */
  382. if (!gdb_has_a_terminal ())
  383. return;
  384. if (gdb_tty_state != desired_state)
  385. {
  386. int result ATTRIBUTE_UNUSED;
  387. /* Ignore SIGTTOU since it will happen when we try to set the
  388. terminal's pgrp. */
  389. scoped_ignore_sigttou ignore_sigttou;
  390. /* Set tty state to our_ttystate. */
  391. serial_set_tty_state (stdin_serial, our_terminal_info.ttystate);
  392. /* If we only want output, then leave the inferior's pgrp in the
  393. foreground, so that Ctrl-C/Ctrl-Z reach the inferior
  394. directly. */
  395. if (job_control && desired_state == target_terminal_state::is_ours)
  396. {
  397. #ifdef HAVE_TERMIOS_H
  398. result = tcsetpgrp (0, our_terminal_info.process_group);
  399. #if 0
  400. /* This fails on Ultrix with EINVAL if you run the testsuite
  401. in the background with nohup, and then log out. GDB never
  402. used to check for an error here, so perhaps there are other
  403. such situations as well. */
  404. if (result == -1)
  405. gdb_printf (gdb_stderr,
  406. "[tcsetpgrp failed in child_terminal_ours: %s]\n",
  407. safe_strerror (errno));
  408. #endif
  409. #endif /* termios */
  410. }
  411. if (!job_control && desired_state == target_terminal_state::is_ours)
  412. {
  413. signal (SIGINT, sigint_ours);
  414. #ifdef SIGQUIT
  415. signal (SIGQUIT, sigquit_ours);
  416. #endif
  417. }
  418. #ifdef F_GETFL
  419. result = fcntl (0, F_SETFL, our_terminal_info.tflags);
  420. #endif
  421. gdb_tty_state = desired_state;
  422. }
  423. }
  424. /* Interrupt the inferior. Implementation of target_interrupt for
  425. child/native targets. */
  426. void
  427. child_interrupt (struct target_ops *self)
  428. {
  429. /* Interrupt the first inferior that has a resumed thread. */
  430. thread_info *resumed = NULL;
  431. for (thread_info *thr : all_non_exited_threads ())
  432. {
  433. if (thr->executing ())
  434. {
  435. resumed = thr;
  436. break;
  437. }
  438. if (thr->has_pending_waitstatus ())
  439. resumed = thr;
  440. }
  441. if (resumed != NULL)
  442. {
  443. /* Note that unlike pressing Ctrl-C on the controlling terminal,
  444. here we only interrupt one process, not the whole process
  445. group. This is because interrupting a process group (with
  446. either Ctrl-C or with kill(3) with negative PID) sends a
  447. SIGINT to each process in the process group, and we may not
  448. be debugging all processes in the process group. */
  449. #ifndef _WIN32
  450. kill (resumed->inf->pid, SIGINT);
  451. #endif
  452. }
  453. }
  454. /* Pass a Ctrl-C to the inferior as-if a Ctrl-C was pressed while the
  455. inferior was in the foreground. Implementation of
  456. target_pass_ctrlc for child/native targets. */
  457. void
  458. child_pass_ctrlc (struct target_ops *self)
  459. {
  460. gdb_assert (!target_terminal::is_ours ());
  461. #ifdef HAVE_TERMIOS_H
  462. if (job_control)
  463. {
  464. pid_t term_pgrp = tcgetpgrp (0);
  465. /* If there's any inferior sharing our terminal, pass the SIGINT
  466. to the terminal's foreground process group. This acts just
  467. like the user typed a ^C on the terminal while the inferior
  468. was in the foreground. Note that using a negative process
  469. number in kill() is a System V-ism. The proper BSD interface
  470. is killpg(). However, all modern BSDs support the System V
  471. interface too. */
  472. if (term_pgrp != -1 && term_pgrp != our_terminal_info.process_group)
  473. {
  474. kill (-term_pgrp, SIGINT);
  475. return;
  476. }
  477. }
  478. #endif
  479. /* Otherwise, pass the Ctrl-C to the first inferior that was resumed
  480. in the foreground. */
  481. for (inferior *inf : all_inferiors ())
  482. {
  483. if (inf->terminal_state != target_terminal_state::is_ours)
  484. {
  485. gdb_assert (inf->pid != 0);
  486. #ifndef _WIN32
  487. kill (inf->pid, SIGINT);
  488. #endif
  489. return;
  490. }
  491. }
  492. /* If no inferior was resumed in the foreground, then how did the
  493. !is_ours assert above pass? */
  494. gdb_assert_not_reached ("no inferior resumed in the fg found");
  495. }
  496. /* Per-inferior data key. */
  497. static const struct inferior_key<terminal_info> inflow_inferior_data;
  498. terminal_info::~terminal_info ()
  499. {
  500. xfree (ttystate);
  501. }
  502. /* Get the current svr4 data. If none is found yet, add it now. This
  503. function always returns a valid object. */
  504. static struct terminal_info *
  505. get_inflow_inferior_data (struct inferior *inf)
  506. {
  507. struct terminal_info *info;
  508. info = inflow_inferior_data.get (inf);
  509. if (info == NULL)
  510. info = inflow_inferior_data.emplace (inf);
  511. return info;
  512. }
  513. /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member
  514. of the inferior structure. This field is private to inflow.c, and
  515. its type is opaque to the rest of GDB. PID is the target pid of
  516. the inferior that is about to be removed from the inferior
  517. list. */
  518. static void
  519. inflow_inferior_exit (struct inferior *inf)
  520. {
  521. inf->terminal_state = target_terminal_state::is_ours;
  522. inflow_inferior_data.clear (inf);
  523. }
  524. void
  525. copy_terminal_info (struct inferior *to, struct inferior *from)
  526. {
  527. struct terminal_info *tinfo_to, *tinfo_from;
  528. tinfo_to = get_inflow_inferior_data (to);
  529. tinfo_from = get_inflow_inferior_data (from);
  530. xfree (tinfo_to->ttystate);
  531. *tinfo_to = *tinfo_from;
  532. if (tinfo_from->ttystate)
  533. tinfo_to->ttystate
  534. = serial_copy_tty_state (stdin_serial, tinfo_from->ttystate);
  535. to->terminal_state = from->terminal_state;
  536. }
  537. /* See terminal.h. */
  538. void
  539. swap_terminal_info (inferior *a, inferior *b)
  540. {
  541. terminal_info *info_a = inflow_inferior_data.get (a);
  542. terminal_info *info_b = inflow_inferior_data.get (b);
  543. inflow_inferior_data.set (a, info_b);
  544. inflow_inferior_data.set (b, info_a);
  545. std::swap (a->terminal_state, b->terminal_state);
  546. }
  547. static void
  548. info_terminal_command (const char *arg, int from_tty)
  549. {
  550. target_terminal::info (arg, from_tty);
  551. }
  552. void
  553. child_terminal_info (struct target_ops *self, const char *args, int from_tty)
  554. {
  555. struct inferior *inf;
  556. struct terminal_info *tinfo;
  557. if (!gdb_has_a_terminal ())
  558. {
  559. gdb_printf (_("This GDB does not control a terminal.\n"));
  560. return;
  561. }
  562. if (inferior_ptid == null_ptid)
  563. return;
  564. inf = current_inferior ();
  565. tinfo = get_inflow_inferior_data (inf);
  566. gdb_printf (_("Inferior's terminal status "
  567. "(currently saved by GDB):\n"));
  568. /* First the fcntl flags. */
  569. {
  570. int flags;
  571. flags = tinfo->tflags;
  572. gdb_printf ("File descriptor flags = ");
  573. #ifndef O_ACCMODE
  574. #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
  575. #endif
  576. /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
  577. switch (flags & (O_ACCMODE))
  578. {
  579. case O_RDONLY:
  580. gdb_printf ("O_RDONLY");
  581. break;
  582. case O_WRONLY:
  583. gdb_printf ("O_WRONLY");
  584. break;
  585. case O_RDWR:
  586. gdb_printf ("O_RDWR");
  587. break;
  588. }
  589. flags &= ~(O_ACCMODE);
  590. #ifdef O_NONBLOCK
  591. if (flags & O_NONBLOCK)
  592. gdb_printf (" | O_NONBLOCK");
  593. flags &= ~O_NONBLOCK;
  594. #endif
  595. #if defined (O_NDELAY)
  596. /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will
  597. print it as O_NONBLOCK, which is good cause that is what POSIX
  598. has, and the flag will already be cleared by the time we get here. */
  599. if (flags & O_NDELAY)
  600. gdb_printf (" | O_NDELAY");
  601. flags &= ~O_NDELAY;
  602. #endif
  603. if (flags & O_APPEND)
  604. gdb_printf (" | O_APPEND");
  605. flags &= ~O_APPEND;
  606. #if defined (O_BINARY)
  607. if (flags & O_BINARY)
  608. gdb_printf (" | O_BINARY");
  609. flags &= ~O_BINARY;
  610. #endif
  611. if (flags)
  612. gdb_printf (" | 0x%x", flags);
  613. gdb_printf ("\n");
  614. }
  615. #ifdef HAVE_TERMIOS_H
  616. gdb_printf ("Process group = %d\n", (int) tinfo->process_group);
  617. #endif
  618. serial_print_tty_state (stdin_serial, tinfo->ttystate, gdb_stdout);
  619. }
  620. /* NEW_TTY_PREFORK is called before forking a new child process,
  621. so we can record the state of ttys in the child to be formed.
  622. TTYNAME is empty if we are to share the terminal with gdb;
  623. otherwise it contains the name of the desired tty.
  624. NEW_TTY is called in new child processes under Unix, which will
  625. become debugger target processes. This actually switches to
  626. the terminal specified in the NEW_TTY_PREFORK call. */
  627. void
  628. new_tty_prefork (std::string ttyname)
  629. {
  630. /* Save the name for later, for determining whether we and the child
  631. are sharing a tty. */
  632. inferior_thisrun_terminal = std::move (ttyname);
  633. }
  634. #if !defined(__GO32__) && !defined(_WIN32)
  635. /* If RESULT, assumed to be the return value from a system call, is
  636. negative, print the error message indicated by errno and exit.
  637. MSG should identify the operation that failed. */
  638. static void
  639. check_syscall (const char *msg, int result)
  640. {
  641. if (result < 0)
  642. {
  643. print_sys_errmsg (msg, errno);
  644. _exit (1);
  645. }
  646. }
  647. #endif
  648. void
  649. new_tty (void)
  650. {
  651. if (inferior_thisrun_terminal.empty ())
  652. return;
  653. #if !defined(__GO32__) && !defined(_WIN32)
  654. int tty;
  655. #ifdef TIOCNOTTY
  656. /* Disconnect the child process from our controlling terminal. On some
  657. systems (SVR4 for example), this may cause a SIGTTOU, so temporarily
  658. ignore SIGTTOU. */
  659. tty = open ("/dev/tty", O_RDWR);
  660. if (tty >= 0)
  661. {
  662. scoped_ignore_sigttou ignore_sigttou;
  663. ioctl (tty, TIOCNOTTY, 0);
  664. close (tty);
  665. }
  666. #endif
  667. /* Now open the specified new terminal. */
  668. tty = open (inferior_thisrun_terminal.c_str (), O_RDWR | O_NOCTTY);
  669. check_syscall (inferior_thisrun_terminal.c_str (), tty);
  670. /* Avoid use of dup2; doesn't exist on all systems. */
  671. if (tty != 0)
  672. {
  673. close (0);
  674. check_syscall ("dup'ing tty into fd 0", dup (tty));
  675. }
  676. if (tty != 1)
  677. {
  678. close (1);
  679. check_syscall ("dup'ing tty into fd 1", dup (tty));
  680. }
  681. if (tty != 2)
  682. {
  683. close (2);
  684. check_syscall ("dup'ing tty into fd 2", dup (tty));
  685. }
  686. #ifdef TIOCSCTTY
  687. /* Make tty our new controlling terminal. */
  688. if (ioctl (tty, TIOCSCTTY, 0) == -1)
  689. /* Mention GDB in warning because it will appear in the inferior's
  690. terminal instead of GDB's. */
  691. warning (_("GDB: Failed to set controlling terminal: %s"),
  692. safe_strerror (errno));
  693. #endif
  694. if (tty > 2)
  695. close (tty);
  696. #endif /* !go32 && !win32 */
  697. }
  698. /* NEW_TTY_POSTFORK is called after forking a new child process, and
  699. adding it to the inferior table, to store the TTYNAME being used by
  700. the child, or empty if it sharing the terminal with gdb. */
  701. void
  702. new_tty_postfork (void)
  703. {
  704. /* Save the name for later, for determining whether we and the child
  705. are sharing a tty. */
  706. struct inferior *inf = current_inferior ();
  707. struct terminal_info *tinfo = get_inflow_inferior_data (inf);
  708. tinfo->run_terminal = std::move (inferior_thisrun_terminal);
  709. inferior_thisrun_terminal.clear ();
  710. }
  711. /* Call set_sigint_trap when you need to pass a signal on to an attached
  712. process when handling SIGINT. */
  713. static void
  714. pass_signal (int signo)
  715. {
  716. #ifndef _WIN32
  717. kill (inferior_ptid.pid (), SIGINT);
  718. #endif
  719. }
  720. static sighandler_t osig;
  721. static int osig_set;
  722. void
  723. set_sigint_trap (void)
  724. {
  725. struct inferior *inf = current_inferior ();
  726. struct terminal_info *tinfo = get_inflow_inferior_data (inf);
  727. if (inf->attach_flag || !tinfo->run_terminal.empty ())
  728. {
  729. osig = signal (SIGINT, pass_signal);
  730. osig_set = 1;
  731. }
  732. else
  733. osig_set = 0;
  734. }
  735. void
  736. clear_sigint_trap (void)
  737. {
  738. if (osig_set)
  739. {
  740. signal (SIGINT, osig);
  741. osig_set = 0;
  742. }
  743. }
  744. /* Create a new session if the inferior will run in a different tty.
  745. A session is UNIX's way of grouping processes that share a controlling
  746. terminal, so a new one is needed if the inferior terminal will be
  747. different from GDB's.
  748. Returns the session id of the new session, 0 if no session was created
  749. or -1 if an error occurred. */
  750. pid_t
  751. create_tty_session (void)
  752. {
  753. #ifdef HAVE_SETSID
  754. pid_t ret;
  755. if (!job_control || inferior_thisrun_terminal.empty ())
  756. return 0;
  757. ret = setsid ();
  758. if (ret == -1)
  759. warning (_("Failed to create new terminal session: setsid: %s"),
  760. safe_strerror (errno));
  761. return ret;
  762. #else
  763. return 0;
  764. #endif /* HAVE_SETSID */
  765. }
  766. /* Get all the current tty settings (including whether we have a
  767. tty at all!). We can't do this in _initialize_inflow because
  768. serial_fdopen() won't work until the serial_ops_list is
  769. initialized, but we don't want to do it lazily either, so
  770. that we can guarantee stdin_serial is opened if there is
  771. a terminal. */
  772. void
  773. initialize_stdin_serial (void)
  774. {
  775. stdin_serial = serial_fdopen (0);
  776. }
  777. void _initialize_inflow ();
  778. void
  779. _initialize_inflow ()
  780. {
  781. add_info ("terminal", info_terminal_command,
  782. _("Print inferior's saved terminal status."));
  783. /* OK, figure out whether we have job control. */
  784. have_job_control ();
  785. gdb::observers::inferior_exit.attach (inflow_inferior_exit, "inflow");
  786. }