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