]>
Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
1a494973 | 2 | Copyright 1986, 1987, 1989, 1991, 1992, 1995 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
715d2e06 | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
715d2e06 JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
715d2e06 | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
715d2e06 | 17 | along with this program; if not, write to the Free Software |
647e52ea | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 | 19 | |
bd5635a1 | 20 | #include "defs.h" |
bd5635a1 RP |
21 | #include "frame.h" |
22 | #include "inferior.h" | |
23 | #include "command.h" | |
24 | #include "signals.h" | |
a88797b5 | 25 | #include "serial.h" |
a14a8fad | 26 | #include "terminal.h" |
bd5635a1 | 27 | #include "target.h" |
647e52ea | 28 | #include "gdbthread.h" |
bd5635a1 | 29 | |
2b576293 | 30 | #include "gdb_string.h" |
a14a8fad JK |
31 | #include <signal.h> |
32 | #include <fcntl.h> | |
647e52ea | 33 | #ifdef HAVE_UNISTD_H |
a14a8fad JK |
34 | #include <unistd.h> |
35 | #endif | |
bd5635a1 | 36 | |
a14a8fad JK |
37 | #ifdef HAVE_TERMIOS |
38 | #define PROCESS_GROUP_TYPE pid_t | |
39 | #endif | |
40 | ||
41 | #ifdef HAVE_SGTTY | |
42 | #ifdef SHORT_PGRP | |
43 | /* This is only used for the ultra. Does it have pid_t? */ | |
44 | #define PROCESS_GROUP_TYPE short | |
45 | #else | |
46 | #define PROCESS_GROUP_TYPE int | |
47 | #endif | |
48 | #endif /* sgtty */ | |
bd5635a1 | 49 | |
e676a15f FF |
50 | static void |
51 | kill_command PARAMS ((char *, int)); | |
52 | ||
53 | static void | |
54 | terminal_ours_1 PARAMS ((int)); | |
bd5635a1 RP |
55 | \f |
56 | /* Record terminal status separately for debugger and inferior. */ | |
57 | ||
a88797b5 | 58 | static serial_t stdin_serial; |
bd5635a1 | 59 | |
a88797b5 FF |
60 | /* TTY state for the inferior. We save it whenever the inferior stops, and |
61 | restore it when it resumes. */ | |
62 | static serial_ttystate inferior_ttystate; | |
bd5635a1 | 63 | |
a88797b5 FF |
64 | /* Our own tty state, which we restore every time we need to deal with the |
65 | terminal. We only set it once, when GDB first starts. The settings of | |
66 | flags which readline saves and restores and unimportant. */ | |
67 | static serial_ttystate our_ttystate; | |
bd5635a1 | 68 | |
a88797b5 FF |
69 | /* fcntl flags for us and the inferior. Saved and restored just like |
70 | {our,inferior}_ttystate. */ | |
71 | static int tflags_inferior; | |
72 | static int tflags_ours; | |
bd5635a1 | 73 | |
a14a8fad JK |
74 | #ifdef PROCESS_GROUP_TYPE |
75 | /* Process group for us and the inferior. Saved and restored just like | |
76 | {our,inferior}_ttystate. */ | |
77 | PROCESS_GROUP_TYPE our_process_group; | |
78 | PROCESS_GROUP_TYPE inferior_process_group; | |
79 | #endif | |
80 | ||
a88797b5 FF |
81 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the |
82 | inferior only. If we have job control, that takes care of it. If not, | |
83 | we save our handlers in these two variables and set SIGINT and SIGQUIT | |
84 | to SIG_IGN. */ | |
bd5635a1 RP |
85 | static void (*sigint_ours) (); |
86 | static void (*sigquit_ours) (); | |
bd5635a1 | 87 | |
2a5ec41d JG |
88 | /* The name of the tty (from the `tty' command) that we gave to the inferior |
89 | when it was last started. */ | |
bd5635a1 | 90 | |
2a5ec41d | 91 | static char *inferior_thisrun_terminal; |
bd5635a1 | 92 | |
49781499 JK |
93 | /* Nonzero if our terminal settings are in effect. Zero if the |
94 | inferior's settings are in effect. Ignored if !gdb_has_a_terminal | |
95 | (). */ | |
2a5ec41d | 96 | |
bd5635a1 RP |
97 | static int terminal_is_ours; |
98 | ||
a88797b5 FF |
99 | enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked; |
100 | ||
101 | /* Does GDB have a terminal (on stdin)? */ | |
102 | int | |
103 | gdb_has_a_terminal () | |
104 | { | |
105 | switch (gdb_has_a_terminal_flag) | |
106 | { | |
107 | case yes: | |
108 | return 1; | |
109 | case no: | |
110 | return 0; | |
111 | case have_not_checked: | |
112 | /* Get all the current tty settings (including whether we have a tty at | |
113 | all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN | |
114 | won't work until the serial_ops_list is initialized. */ | |
115 | ||
49781499 | 116 | #ifdef F_GETFL |
a88797b5 | 117 | tflags_ours = fcntl (0, F_GETFL, 0); |
49781499 | 118 | #endif |
a88797b5 FF |
119 | |
120 | gdb_has_a_terminal_flag = no; | |
121 | stdin_serial = SERIAL_FDOPEN (0); | |
122 | if (stdin_serial != NULL) | |
123 | { | |
124 | our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); | |
a14a8fad | 125 | |
a88797b5 | 126 | if (our_ttystate != NULL) |
a14a8fad JK |
127 | { |
128 | gdb_has_a_terminal_flag = yes; | |
129 | #ifdef HAVE_TERMIOS | |
130 | our_process_group = tcgetpgrp (0); | |
131 | #endif | |
132 | #ifdef HAVE_SGTTY | |
d76eb5f4 | 133 | ioctl (0, TIOCGPGRP, &our_process_group); |
a14a8fad JK |
134 | #endif |
135 | } | |
a88797b5 FF |
136 | } |
137 | ||
138 | return gdb_has_a_terminal_flag == yes; | |
199b2450 TL |
139 | default: |
140 | /* "Can't happen". */ | |
141 | return 0; | |
a88797b5 FF |
142 | } |
143 | } | |
144 | ||
2a5ec41d JG |
145 | /* Macro for printing errors from ioctl operations */ |
146 | ||
147 | #define OOPSY(what) \ | |
148 | if (result == -1) \ | |
199b2450 | 149 | fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \ |
2a5ec41d JG |
150 | what, strerror (errno)) |
151 | ||
a88797b5 | 152 | static void terminal_ours_1 PARAMS ((int)); |
2a5ec41d | 153 | |
bd5635a1 RP |
154 | /* Initialize the terminal settings we record for the inferior, |
155 | before we actually run the inferior. */ | |
156 | ||
157 | void | |
647e52ea SG |
158 | terminal_init_inferior_with_pgrp (pgrp) |
159 | int pgrp; | |
bd5635a1 | 160 | { |
49781499 JK |
161 | if (gdb_has_a_terminal ()) |
162 | { | |
163 | /* We could just as well copy our_ttystate (if we felt like adding | |
164 | a new function SERIAL_COPY_TTY_STATE). */ | |
165 | if (inferior_ttystate) | |
166 | free (inferior_ttystate); | |
167 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); | |
647e52ea | 168 | |
a14a8fad | 169 | #ifdef PROCESS_GROUP_TYPE |
647e52ea | 170 | inferior_process_group = pgrp; |
a14a8fad | 171 | #endif |
49781499 JK |
172 | |
173 | /* Make sure that next time we call terminal_inferior (which will be | |
174 | before the program runs, as it needs to be), we install the new | |
175 | process group. */ | |
176 | terminal_is_ours = 1; | |
177 | } | |
bd5635a1 RP |
178 | } |
179 | ||
647e52ea SG |
180 | void |
181 | terminal_init_inferior () | |
182 | { | |
183 | #ifdef PROCESS_GROUP_TYPE | |
184 | #ifdef PIDGET | |
185 | /* This is for Lynx, and should be cleaned up by having Lynx be a separate | |
186 | debugging target with a version of target_terminal_init_inferior which | |
187 | passes in the process group to a generic routine which does all the work | |
188 | (and the non-threaded child_terminal_init_inferior can just pass in | |
189 | inferior_pid to the same routine). */ | |
190 | terminal_init_inferior_with_pgrp (PIDGET (inferior_pid)); | |
191 | #else | |
192 | /* By default, we assume INFERIOR_PID is also the child's process group. */ | |
193 | terminal_init_inferior_with_pgrp (inferior_pid); | |
194 | #endif | |
195 | #endif /* PROCESS_GROUP_TYPE */ | |
196 | } | |
197 | ||
bd5635a1 RP |
198 | /* Put the inferior's terminal settings into effect. |
199 | This is preparation for starting or resuming the inferior. */ | |
200 | ||
201 | void | |
202 | terminal_inferior () | |
203 | { | |
a88797b5 FF |
204 | if (gdb_has_a_terminal () && terminal_is_ours |
205 | && inferior_thisrun_terminal == 0) | |
bd5635a1 | 206 | { |
a88797b5 FF |
207 | int result; |
208 | ||
49781499 | 209 | #ifdef F_GETFL |
a88797b5 FF |
210 | /* Is there a reason this is being done twice? It happens both |
211 | places we use F_SETFL, so I'm inclined to think perhaps there | |
212 | is some reason, however perverse. Perhaps not though... */ | |
2a5ec41d JG |
213 | result = fcntl (0, F_SETFL, tflags_inferior); |
214 | result = fcntl (0, F_SETFL, tflags_inferior); | |
215 | OOPSY ("fcntl F_SETFL"); | |
49781499 | 216 | #endif |
bd5635a1 | 217 | |
a88797b5 FF |
218 | /* Because we were careful to not change in or out of raw mode in |
219 | terminal_ours, we will not change in our out of raw mode with | |
220 | this call, so we don't flush any input. */ | |
221 | result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate); | |
a14a8fad JK |
222 | OOPSY ("setting tty state"); |
223 | ||
224 | if (!job_control) | |
225 | { | |
226 | sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN); | |
227 | sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN); | |
228 | } | |
a88797b5 FF |
229 | |
230 | /* If attach_flag is set, we don't know whether we are sharing a | |
231 | terminal with the inferior or not. (attaching a process | |
232 | without a terminal is one case where we do not; attaching a | |
233 | process which we ran from the same shell as GDB via `&' is | |
234 | one case where we do, I think (but perhaps this is not | |
235 | `sharing' in the sense that we need to save and restore tty | |
236 | state)). I don't know if there is any way to tell whether we | |
237 | are sharing a terminal. So what we do is to go through all | |
a14a8fad JK |
238 | the saving and restoring of the tty state, but ignore errors |
239 | setting the process group, which will happen if we are not | |
240 | sharing a terminal). */ | |
a88797b5 | 241 | |
a14a8fad | 242 | if (job_control) |
a88797b5 | 243 | { |
a14a8fad JK |
244 | #ifdef HAVE_TERMIOS |
245 | result = tcsetpgrp (0, inferior_process_group); | |
246 | if (!attach_flag) | |
247 | OOPSY ("tcsetpgrp"); | |
248 | #endif | |
249 | ||
250 | #ifdef HAVE_SGTTY | |
179cd923 | 251 | result = ioctl (0, TIOCSPGRP, &inferior_process_group); |
a14a8fad JK |
252 | if (!attach_flag) |
253 | OOPSY ("TIOCSPGRP"); | |
254 | #endif | |
a88797b5 | 255 | } |
a14a8fad | 256 | |
bd5635a1 RP |
257 | } |
258 | terminal_is_ours = 0; | |
259 | } | |
260 | ||
261 | /* Put some of our terminal settings into effect, | |
262 | enough to get proper results from our output, | |
263 | but do not change into or out of RAW mode | |
264 | so that no input is discarded. | |
265 | ||
266 | After doing this, either terminal_ours or terminal_inferior | |
267 | should be called to get back to a normal state of affairs. */ | |
268 | ||
269 | void | |
270 | terminal_ours_for_output () | |
271 | { | |
272 | terminal_ours_1 (1); | |
273 | } | |
274 | ||
275 | /* Put our terminal settings into effect. | |
276 | First record the inferior's terminal settings | |
277 | so they can be restored properly later. */ | |
278 | ||
279 | void | |
280 | terminal_ours () | |
281 | { | |
282 | terminal_ours_1 (0); | |
283 | } | |
284 | ||
a88797b5 FF |
285 | /* output_only is not used, and should not be used unless we introduce |
286 | separate terminal_is_ours and terminal_is_ours_for_output | |
287 | flags. */ | |
288 | ||
bd5635a1 RP |
289 | static void |
290 | terminal_ours_1 (output_only) | |
291 | int output_only; | |
292 | { | |
2a5ec41d | 293 | /* Checking inferior_thisrun_terminal is necessary so that |
bd5635a1 | 294 | if GDB is running in the background, it won't block trying |
2a5ec41d JG |
295 | to do the ioctl()'s below. Checking gdb_has_a_terminal |
296 | avoids attempting all the ioctl's when running in batch. */ | |
a88797b5 | 297 | if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) |
bd5635a1 RP |
298 | return; |
299 | ||
300 | if (!terminal_is_ours) | |
301 | { | |
a88797b5 FF |
302 | /* Ignore this signal since it will happen when we try to set the |
303 | pgrp. */ | |
304 | void (*osigttou) (); | |
305 | int result; | |
306 | ||
bd5635a1 RP |
307 | terminal_is_ours = 1; |
308 | ||
a88797b5 FF |
309 | #ifdef SIGTTOU |
310 | if (job_control) | |
311 | osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN); | |
312 | #endif | |
bd5635a1 | 313 | |
a88797b5 FF |
314 | if (inferior_ttystate) |
315 | free (inferior_ttystate); | |
316 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); | |
a14a8fad JK |
317 | #ifdef HAVE_TERMIOS |
318 | inferior_process_group = tcgetpgrp (0); | |
319 | #endif | |
320 | #ifdef HAVE_SGTTY | |
d76eb5f4 | 321 | ioctl (0, TIOCGPGRP, &inferior_process_group); |
a14a8fad | 322 | #endif |
bd5635a1 | 323 | |
a88797b5 FF |
324 | /* Here we used to set ICANON in our ttystate, but I believe this |
325 | was an artifact from before when we used readline. Readline sets | |
91ecc8ef SS |
326 | the tty state when it needs to. |
327 | FIXME-maybe: However, query() expects non-raw mode and doesn't | |
328 | use readline. Maybe query should use readline (on the other hand, | |
329 | this only matters for HAVE_SGTTY, not termio or termios, I think). */ | |
bd5635a1 | 330 | |
a88797b5 FF |
331 | /* Set tty state to our_ttystate. We don't change in our out of raw |
332 | mode, to avoid flushing input. We need to do the same thing | |
333 | regardless of output_only, because we don't have separate | |
334 | terminal_is_ours and terminal_is_ours_for_output flags. It's OK, | |
335 | though, since readline will deal with raw mode when/if it needs to. | |
336 | */ | |
91ecc8ef | 337 | |
a88797b5 FF |
338 | SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate, |
339 | inferior_ttystate); | |
bd5635a1 | 340 | |
a14a8fad JK |
341 | if (job_control) |
342 | { | |
343 | #ifdef HAVE_TERMIOS | |
344 | result = tcsetpgrp (0, our_process_group); | |
76e473bb JK |
345 | #if 0 |
346 | /* This fails on Ultrix with EINVAL if you run the testsuite | |
347 | in the background with nohup, and then log out. GDB never | |
348 | used to check for an error here, so perhaps there are other | |
349 | such situations as well. */ | |
d76eb5f4 | 350 | if (result == -1) |
199b2450 | 351 | fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n", |
d76eb5f4 | 352 | strerror (errno)); |
a14a8fad | 353 | #endif |
76e473bb | 354 | #endif /* termios */ |
a14a8fad JK |
355 | |
356 | #ifdef HAVE_SGTTY | |
179cd923 | 357 | result = ioctl (0, TIOCSPGRP, &our_process_group); |
a14a8fad JK |
358 | #endif |
359 | } | |
360 | ||
a88797b5 FF |
361 | #ifdef SIGTTOU |
362 | if (job_control) | |
363 | signal (SIGTTOU, osigttou); | |
bd5635a1 | 364 | #endif |
bd5635a1 | 365 | |
a88797b5 FF |
366 | if (!job_control) |
367 | { | |
368 | signal (SIGINT, sigint_ours); | |
369 | signal (SIGQUIT, sigquit_ours); | |
370 | } | |
bd5635a1 | 371 | |
49781499 | 372 | #ifdef F_GETFL |
a88797b5 | 373 | tflags_inferior = fcntl (0, F_GETFL, 0); |
e676a15f | 374 | |
a88797b5 FF |
375 | /* Is there a reason this is being done twice? It happens both |
376 | places we use F_SETFL, so I'm inclined to think perhaps there | |
377 | is some reason, however perverse. Perhaps not though... */ | |
378 | result = fcntl (0, F_SETFL, tflags_ours); | |
379 | result = fcntl (0, F_SETFL, tflags_ours); | |
49781499 | 380 | #endif |
a88797b5 FF |
381 | |
382 | result = result; /* lint */ | |
383 | } | |
bd5635a1 RP |
384 | } |
385 | ||
386 | /* ARGSUSED */ | |
387 | void | |
388 | term_info (arg, from_tty) | |
389 | char *arg; | |
390 | int from_tty; | |
391 | { | |
392 | target_terminal_info (arg, from_tty); | |
393 | } | |
394 | ||
715d2e06 | 395 | /* ARGSUSED */ |
bd5635a1 RP |
396 | void |
397 | child_terminal_info (args, from_tty) | |
398 | char *args; | |
399 | int from_tty; | |
400 | { | |
a88797b5 FF |
401 | if (!gdb_has_a_terminal ()) |
402 | { | |
403 | printf_filtered ("This GDB does not control a terminal.\n"); | |
404 | return; | |
405 | } | |
bd5635a1 RP |
406 | |
407 | printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); | |
408 | ||
a88797b5 FF |
409 | /* First the fcntl flags. */ |
410 | { | |
411 | int flags; | |
412 | ||
413 | flags = tflags_inferior; | |
bd5635a1 | 414 | |
a88797b5 | 415 | printf_filtered ("File descriptor flags = "); |
bd5635a1 | 416 | |
a88797b5 FF |
417 | #ifndef O_ACCMODE |
418 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) | |
bd5635a1 | 419 | #endif |
a88797b5 FF |
420 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ |
421 | switch (flags & (O_ACCMODE)) | |
422 | { | |
423 | case O_RDONLY: printf_filtered ("O_RDONLY"); break; | |
424 | case O_WRONLY: printf_filtered ("O_WRONLY"); break; | |
425 | case O_RDWR: printf_filtered ("O_RDWR"); break; | |
426 | } | |
427 | flags &= ~(O_ACCMODE); | |
bd5635a1 | 428 | |
a88797b5 FF |
429 | #ifdef O_NONBLOCK |
430 | if (flags & O_NONBLOCK) | |
431 | printf_filtered (" | O_NONBLOCK"); | |
432 | flags &= ~O_NONBLOCK; | |
bd5635a1 | 433 | #endif |
a88797b5 FF |
434 | |
435 | #if defined (O_NDELAY) | |
436 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will | |
437 | print it as O_NONBLOCK, which is good cause that is what POSIX | |
438 | has, and the flag will already be cleared by the time we get here. */ | |
439 | if (flags & O_NDELAY) | |
440 | printf_filtered (" | O_NDELAY"); | |
441 | flags &= ~O_NDELAY; | |
bd5635a1 | 442 | #endif |
a88797b5 FF |
443 | |
444 | if (flags & O_APPEND) | |
445 | printf_filtered (" | O_APPEND"); | |
446 | flags &= ~O_APPEND; | |
447 | ||
448 | #if defined (O_BINARY) | |
449 | if (flags & O_BINARY) | |
450 | printf_filtered (" | O_BINARY"); | |
451 | flags &= ~O_BINARY; | |
e676a15f FF |
452 | #endif |
453 | ||
a88797b5 FF |
454 | if (flags) |
455 | printf_filtered (" | 0x%x", flags); | |
456 | printf_filtered ("\n"); | |
457 | } | |
458 | ||
a14a8fad JK |
459 | #ifdef PROCESS_GROUP_TYPE |
460 | printf_filtered ("Process group = %d\n", inferior_process_group); | |
461 | #endif | |
462 | ||
a88797b5 | 463 | SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate); |
bd5635a1 RP |
464 | } |
465 | \f | |
715d2e06 JG |
466 | /* NEW_TTY_PREFORK is called before forking a new child process, |
467 | so we can record the state of ttys in the child to be formed. | |
468 | TTYNAME is null if we are to share the terminal with gdb; | |
469 | or points to a string containing the name of the desired tty. | |
bd5635a1 | 470 | |
715d2e06 JG |
471 | NEW_TTY is called in new child processes under Unix, which will |
472 | become debugger target processes. This actually switches to | |
473 | the terminal specified in the NEW_TTY_PREFORK call. */ | |
474 | ||
7d9884b9 | 475 | void |
715d2e06 | 476 | new_tty_prefork (ttyname) |
bd5635a1 RP |
477 | char *ttyname; |
478 | { | |
bd5635a1 RP |
479 | /* Save the name for later, for determining whether we and the child |
480 | are sharing a tty. */ | |
481 | inferior_thisrun_terminal = ttyname; | |
715d2e06 JG |
482 | } |
483 | ||
484 | void | |
485 | new_tty () | |
486 | { | |
487 | register int tty; | |
488 | ||
489 | if (inferior_thisrun_terminal == 0) | |
bd5635a1 | 490 | return; |
647e52ea | 491 | #if !defined(__GO32__) && !defined(__WIN32__) |
bd5635a1 | 492 | #ifdef TIOCNOTTY |
e676a15f FF |
493 | /* Disconnect the child process from our controlling terminal. On some |
494 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily | |
495 | ignore SIGTTOU. */ | |
bd5635a1 RP |
496 | tty = open("/dev/tty", O_RDWR); |
497 | if (tty > 0) | |
498 | { | |
49781499 JK |
499 | void (*osigttou) (); |
500 | ||
e676a15f | 501 | osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN); |
bd5635a1 RP |
502 | ioctl(tty, TIOCNOTTY, 0); |
503 | close(tty); | |
a88797b5 | 504 | signal(SIGTTOU, osigttou); |
bd5635a1 RP |
505 | } |
506 | #endif | |
507 | ||
508 | /* Now open the specified new terminal. */ | |
509 | ||
7d9884b9 JG |
510 | #ifdef USE_O_NOCTTY |
511 | tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY); | |
512 | #else | |
715d2e06 | 513 | tty = open(inferior_thisrun_terminal, O_RDWR); |
7d9884b9 | 514 | #endif |
bd5635a1 RP |
515 | if (tty == -1) |
516 | { | |
715d2e06 | 517 | print_sys_errmsg (inferior_thisrun_terminal, errno); |
bd5635a1 RP |
518 | _exit(1); |
519 | } | |
520 | ||
521 | /* Avoid use of dup2; doesn't exist on all systems. */ | |
522 | if (tty != 0) | |
523 | { close (0); dup (tty); } | |
524 | if (tty != 1) | |
525 | { close (1); dup (tty); } | |
526 | if (tty != 2) | |
527 | { close (2); dup (tty); } | |
528 | if (tty > 2) | |
529 | close(tty); | |
1a494973 | 530 | #endif /* !go32 && !win32*/ |
bd5635a1 RP |
531 | } |
532 | \f | |
533 | /* Kill the inferior process. Make us have no inferior. */ | |
534 | ||
535 | /* ARGSUSED */ | |
536 | static void | |
537 | kill_command (arg, from_tty) | |
538 | char *arg; | |
539 | int from_tty; | |
540 | { | |
1a494973 C |
541 | /* FIXME: This should not really be inferior_pid (or target_has_execution). |
542 | It should be a distinct flag that indicates that a target is active, cuz | |
543 | some targets don't have processes! */ | |
544 | ||
545 | if (inferior_pid == 0) | |
bd5635a1 | 546 | error ("The program is not being run."); |
a88797b5 | 547 | if (!query ("Kill the program being debugged? ")) |
bd5635a1 | 548 | error ("Not confirmed."); |
ee0613d1 | 549 | target_kill (); |
777bef06 | 550 | |
199b2450 TL |
551 | init_thread_list(); /* Destroy thread info */ |
552 | ||
777bef06 JK |
553 | /* Killing off the inferior can leave us with a core file. If so, |
554 | print the state we are left in. */ | |
555 | if (target_has_stack) { | |
cad1498f | 556 | printf_filtered ("In %s,\n", target_longname); |
777bef06 | 557 | if (selected_frame == NULL) |
199b2450 | 558 | fputs_filtered ("No selected stack frame.\n", gdb_stdout); |
777bef06 | 559 | else |
cadbb07a | 560 | print_stack_frame (selected_frame, selected_frame_level, 1); |
777bef06 | 561 | } |
bd5635a1 | 562 | } |
bd5635a1 | 563 | \f |
a88797b5 FF |
564 | /* Call set_sigint_trap when you need to pass a signal on to an attached |
565 | process when handling SIGINT */ | |
566 | ||
bd5635a1 RP |
567 | /* ARGSUSED */ |
568 | static void | |
a88797b5 FF |
569 | pass_signal (signo) |
570 | int signo; | |
bd5635a1 | 571 | { |
a88797b5 FF |
572 | kill (inferior_pid, SIGINT); |
573 | } | |
bd5635a1 | 574 | |
a88797b5 FF |
575 | static void (*osig)(); |
576 | ||
577 | void | |
578 | set_sigint_trap() | |
579 | { | |
1a494973 C |
580 | if (attach_flag || inferior_thisrun_terminal) |
581 | { | |
582 | osig = (void (*) ()) signal (SIGINT, pass_signal); | |
583 | } | |
a88797b5 FF |
584 | } |
585 | ||
586 | void | |
587 | clear_sigint_trap() | |
588 | { | |
1a494973 C |
589 | if (attach_flag || inferior_thisrun_terminal) |
590 | { | |
591 | signal (SIGINT, osig); | |
592 | } | |
bd5635a1 | 593 | } |
bd5635a1 | 594 | \f |
0c18c737 | 595 | #if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN) |
cad1498f SG |
596 | static void (*old_sigio) (); |
597 | ||
598 | static void | |
599 | handle_sigio (signo) | |
600 | int signo; | |
601 | { | |
602 | int numfds; | |
603 | fd_set readfds; | |
604 | ||
605 | signal (SIGIO, handle_sigio); | |
606 | ||
607 | FD_ZERO (&readfds); | |
608 | FD_SET (target_activity_fd, &readfds); | |
609 | numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL); | |
610 | if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds)) | |
611 | { | |
612 | if ((*target_activity_function) ()) | |
613 | kill (inferior_pid, SIGINT); | |
614 | } | |
615 | } | |
616 | ||
617 | static int old_fcntl_flags; | |
618 | ||
619 | void | |
620 | set_sigio_trap () | |
621 | { | |
622 | if (target_activity_function) | |
623 | { | |
624 | old_sigio = (void (*) ()) signal (SIGIO, handle_sigio); | |
625 | fcntl (target_activity_fd, F_SETOWN, getpid()); | |
626 | old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0); | |
627 | fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC); | |
628 | } | |
629 | } | |
630 | ||
631 | void | |
632 | clear_sigio_trap () | |
633 | { | |
634 | if (target_activity_function) | |
635 | { | |
636 | signal (SIGIO, old_sigio); | |
637 | fcntl (target_activity_fd, F_SETFL, old_fcntl_flags); | |
638 | } | |
639 | } | |
640 | #else /* No SIGIO. */ | |
641 | void | |
642 | set_sigio_trap () | |
643 | { | |
644 | if (target_activity_function) | |
645 | abort (); | |
646 | } | |
647 | ||
648 | void | |
649 | clear_sigio_trap () | |
650 | { | |
651 | if (target_activity_function) | |
652 | abort (); | |
653 | } | |
654 | #endif /* No SIGIO. */ | |
655 | \f | |
a14a8fad | 656 | |
a14a8fad JK |
657 | /* This is here because this is where we figure out whether we (probably) |
658 | have job control. Just using job_control only does part of it because | |
659 | setpgid or setpgrp might not exist on a system without job control. | |
660 | It might be considered misplaced (on the other hand, process groups and | |
661 | job control are closely related to ttys). | |
662 | ||
663 | For a more clean implementation, in libiberty, put a setpgid which merely | |
664 | calls setpgrp and a setpgrp which does nothing (any system with job control | |
665 | will have one or the other). */ | |
666 | int | |
667 | gdb_setpgid () | |
668 | { | |
669 | int retval = 0; | |
91ecc8ef | 670 | |
a14a8fad JK |
671 | if (job_control) |
672 | { | |
647e52ea | 673 | #if defined (NEED_POSIX_SETPGID) || (defined (HAVE_TERMIOS) && defined (HAVE_SETPGID)) |
a14a8fad JK |
674 | /* setpgid (0, 0) is supposed to work and mean the same thing as |
675 | this, but on Ultrix 4.2A it fails with EPERM (and | |
676 | setpgid (getpid (), getpid ()) succeeds). */ | |
677 | retval = setpgid (getpid (), getpid ()); | |
678 | #else | |
679 | #if defined (TIOCGPGRP) | |
680 | #if defined(USG) && !defined(SETPGRP_ARGS) | |
681 | retval = setpgrp (); | |
682 | #else | |
683 | retval = setpgrp (getpid (), getpid ()); | |
684 | #endif /* USG */ | |
685 | #endif /* TIOCGPGRP. */ | |
686 | #endif /* NEED_POSIX_SETPGID */ | |
687 | } | |
688 | return retval; | |
689 | } | |
690 | ||
bd5635a1 RP |
691 | void |
692 | _initialize_inflow () | |
693 | { | |
694 | add_info ("terminal", term_info, | |
695 | "Print inferior's saved terminal status."); | |
696 | ||
bd5635a1 RP |
697 | add_com ("kill", class_run, kill_command, |
698 | "Kill execution of program being debugged."); | |
699 | ||
700 | inferior_pid = 0; | |
701 | ||
bd5635a1 | 702 | terminal_is_ours = 1; |
a14a8fad JK |
703 | |
704 | /* OK, figure out whether we have job control. If neither termios nor | |
705 | sgtty (i.e. termio or go32), leave job_control 0. */ | |
706 | ||
707 | #if defined (HAVE_TERMIOS) | |
708 | /* Do all systems with termios have the POSIX way of identifying job | |
709 | control? I hope so. */ | |
710 | #ifdef _POSIX_JOB_CONTROL | |
711 | job_control = 1; | |
712 | #else | |
647e52ea | 713 | #ifdef _SC_JOB_CONTROL |
a14a8fad | 714 | job_control = sysconf (_SC_JOB_CONTROL); |
647e52ea SG |
715 | #else |
716 | job_control = 0; /* have to assume the worst */ | |
717 | #endif /* _SC_JOB_CONTROL */ | |
718 | #endif /* _POSIX_JOB_CONTROL */ | |
719 | #endif /* HAVE_TERMIOS */ | |
a14a8fad JK |
720 | |
721 | #ifdef HAVE_SGTTY | |
722 | #ifdef TIOCGPGRP | |
723 | job_control = 1; | |
724 | #else | |
725 | job_control = 0; | |
726 | #endif /* TIOCGPGRP */ | |
727 | #endif /* sgtty */ | |
bd5635a1 | 728 | } |