Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Low level interface to ptrace, for GDB when running under Unix. |
ee0613d1 | 2 | Copyright 1986, 1987, 1989, 1991, 1992 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" |
bd5635a1 RP |
26 | #include "target.h" |
27 | ||
28 | #ifdef USG | |
29 | #include <sys/types.h> | |
30 | #endif | |
31 | ||
32 | /* Some USG-esque systems (some of which are BSD-esque enough so that USG | |
33 | is not defined) want this header, and it won't do any harm. */ | |
34 | #include <fcntl.h> | |
35 | ||
36 | #include <sys/param.h> | |
bd5635a1 RP |
37 | #include <signal.h> |
38 | ||
e676a15f FF |
39 | static void |
40 | kill_command PARAMS ((char *, int)); | |
41 | ||
42 | static void | |
43 | terminal_ours_1 PARAMS ((int)); | |
2a5ec41d | 44 | |
bd5635a1 RP |
45 | /* Nonzero if we are debugging an attached outside process |
46 | rather than an inferior. */ | |
47 | ||
48 | int attach_flag; | |
49 | ||
50 | \f | |
51 | /* Record terminal status separately for debugger and inferior. */ | |
52 | ||
a88797b5 | 53 | static serial_t stdin_serial; |
bd5635a1 | 54 | |
a88797b5 FF |
55 | /* TTY state for the inferior. We save it whenever the inferior stops, and |
56 | restore it when it resumes. */ | |
57 | static serial_ttystate inferior_ttystate; | |
bd5635a1 | 58 | |
a88797b5 FF |
59 | /* Our own tty state, which we restore every time we need to deal with the |
60 | terminal. We only set it once, when GDB first starts. The settings of | |
61 | flags which readline saves and restores and unimportant. */ | |
62 | static serial_ttystate our_ttystate; | |
bd5635a1 | 63 | |
a88797b5 FF |
64 | /* fcntl flags for us and the inferior. Saved and restored just like |
65 | {our,inferior}_ttystate. */ | |
66 | static int tflags_inferior; | |
67 | static int tflags_ours; | |
bd5635a1 | 68 | |
a88797b5 FF |
69 | /* While the inferior is running, we want SIGINT and SIGQUIT to go to the |
70 | inferior only. If we have job control, that takes care of it. If not, | |
71 | we save our handlers in these two variables and set SIGINT and SIGQUIT | |
72 | to SIG_IGN. */ | |
bd5635a1 RP |
73 | static void (*sigint_ours) (); |
74 | static void (*sigquit_ours) (); | |
bd5635a1 | 75 | |
2a5ec41d JG |
76 | /* The name of the tty (from the `tty' command) that we gave to the inferior |
77 | when it was last started. */ | |
bd5635a1 | 78 | |
2a5ec41d | 79 | static char *inferior_thisrun_terminal; |
bd5635a1 | 80 | |
49781499 JK |
81 | /* Nonzero if our terminal settings are in effect. Zero if the |
82 | inferior's settings are in effect. Ignored if !gdb_has_a_terminal | |
83 | (). */ | |
2a5ec41d | 84 | |
bd5635a1 RP |
85 | static int terminal_is_ours; |
86 | ||
a88797b5 FF |
87 | enum {yes, no, have_not_checked} gdb_has_a_terminal_flag = have_not_checked; |
88 | ||
89 | /* Does GDB have a terminal (on stdin)? */ | |
90 | int | |
91 | gdb_has_a_terminal () | |
92 | { | |
93 | switch (gdb_has_a_terminal_flag) | |
94 | { | |
95 | case yes: | |
96 | return 1; | |
97 | case no: | |
98 | return 0; | |
99 | case have_not_checked: | |
100 | /* Get all the current tty settings (including whether we have a tty at | |
101 | all!). Can't do this in _initialize_inflow because SERIAL_FDOPEN | |
102 | won't work until the serial_ops_list is initialized. */ | |
103 | ||
49781499 | 104 | #ifdef F_GETFL |
a88797b5 | 105 | tflags_ours = fcntl (0, F_GETFL, 0); |
49781499 | 106 | #endif |
a88797b5 FF |
107 | |
108 | gdb_has_a_terminal_flag = no; | |
109 | stdin_serial = SERIAL_FDOPEN (0); | |
110 | if (stdin_serial != NULL) | |
111 | { | |
112 | our_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); | |
113 | if (our_ttystate != NULL) | |
114 | gdb_has_a_terminal_flag = yes; | |
115 | } | |
116 | ||
117 | return gdb_has_a_terminal_flag == yes; | |
118 | } | |
119 | } | |
120 | ||
2a5ec41d JG |
121 | /* Macro for printing errors from ioctl operations */ |
122 | ||
123 | #define OOPSY(what) \ | |
124 | if (result == -1) \ | |
125 | fprintf(stderr, "[%s failed in terminal_inferior: %s]\n", \ | |
126 | what, strerror (errno)) | |
127 | ||
a88797b5 | 128 | static void terminal_ours_1 PARAMS ((int)); |
2a5ec41d | 129 | |
bd5635a1 RP |
130 | /* Initialize the terminal settings we record for the inferior, |
131 | before we actually run the inferior. */ | |
132 | ||
133 | void | |
134 | terminal_init_inferior () | |
135 | { | |
49781499 JK |
136 | if (gdb_has_a_terminal ()) |
137 | { | |
138 | /* We could just as well copy our_ttystate (if we felt like adding | |
139 | a new function SERIAL_COPY_TTY_STATE). */ | |
140 | if (inferior_ttystate) | |
141 | free (inferior_ttystate); | |
142 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); | |
143 | SERIAL_SET_PROCESS_GROUP (stdin_serial, inferior_ttystate, inferior_pid); | |
144 | ||
145 | /* Make sure that next time we call terminal_inferior (which will be | |
146 | before the program runs, as it needs to be), we install the new | |
147 | process group. */ | |
148 | terminal_is_ours = 1; | |
149 | } | |
bd5635a1 RP |
150 | } |
151 | ||
152 | /* Put the inferior's terminal settings into effect. | |
153 | This is preparation for starting or resuming the inferior. */ | |
154 | ||
155 | void | |
156 | terminal_inferior () | |
157 | { | |
a88797b5 FF |
158 | if (gdb_has_a_terminal () && terminal_is_ours |
159 | && inferior_thisrun_terminal == 0) | |
bd5635a1 | 160 | { |
a88797b5 FF |
161 | int result; |
162 | ||
49781499 | 163 | #ifdef F_GETFL |
a88797b5 FF |
164 | /* Is there a reason this is being done twice? It happens both |
165 | places we use F_SETFL, so I'm inclined to think perhaps there | |
166 | is some reason, however perverse. Perhaps not though... */ | |
2a5ec41d JG |
167 | result = fcntl (0, F_SETFL, tflags_inferior); |
168 | result = fcntl (0, F_SETFL, tflags_inferior); | |
169 | OOPSY ("fcntl F_SETFL"); | |
49781499 | 170 | #endif |
bd5635a1 | 171 | |
a88797b5 FF |
172 | /* Because we were careful to not change in or out of raw mode in |
173 | terminal_ours, we will not change in our out of raw mode with | |
174 | this call, so we don't flush any input. */ | |
175 | result = SERIAL_SET_TTY_STATE (stdin_serial, inferior_ttystate); | |
176 | ||
177 | /* If attach_flag is set, we don't know whether we are sharing a | |
178 | terminal with the inferior or not. (attaching a process | |
179 | without a terminal is one case where we do not; attaching a | |
180 | process which we ran from the same shell as GDB via `&' is | |
181 | one case where we do, I think (but perhaps this is not | |
182 | `sharing' in the sense that we need to save and restore tty | |
183 | state)). I don't know if there is any way to tell whether we | |
184 | are sharing a terminal. So what we do is to go through all | |
185 | the saving and restoring of terminal state, but ignore errors | |
186 | (which will occur, in tcsetpgrp, if we are not sharing a | |
187 | terminal). */ | |
188 | ||
189 | if (!attach_flag) | |
190 | OOPSY ("setting tty state"); | |
191 | ||
192 | if (!job_control) | |
193 | { | |
194 | sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN); | |
195 | sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN); | |
196 | } | |
bd5635a1 RP |
197 | } |
198 | terminal_is_ours = 0; | |
199 | } | |
200 | ||
201 | /* Put some of our terminal settings into effect, | |
202 | enough to get proper results from our output, | |
203 | but do not change into or out of RAW mode | |
204 | so that no input is discarded. | |
205 | ||
206 | After doing this, either terminal_ours or terminal_inferior | |
207 | should be called to get back to a normal state of affairs. */ | |
208 | ||
209 | void | |
210 | terminal_ours_for_output () | |
211 | { | |
212 | terminal_ours_1 (1); | |
213 | } | |
214 | ||
215 | /* Put our terminal settings into effect. | |
216 | First record the inferior's terminal settings | |
217 | so they can be restored properly later. */ | |
218 | ||
219 | void | |
220 | terminal_ours () | |
221 | { | |
222 | terminal_ours_1 (0); | |
223 | } | |
224 | ||
a88797b5 FF |
225 | /* output_only is not used, and should not be used unless we introduce |
226 | separate terminal_is_ours and terminal_is_ours_for_output | |
227 | flags. */ | |
228 | ||
bd5635a1 RP |
229 | static void |
230 | terminal_ours_1 (output_only) | |
231 | int output_only; | |
232 | { | |
2a5ec41d | 233 | /* Checking inferior_thisrun_terminal is necessary so that |
bd5635a1 | 234 | if GDB is running in the background, it won't block trying |
2a5ec41d JG |
235 | to do the ioctl()'s below. Checking gdb_has_a_terminal |
236 | avoids attempting all the ioctl's when running in batch. */ | |
a88797b5 | 237 | if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0) |
bd5635a1 RP |
238 | return; |
239 | ||
240 | if (!terminal_is_ours) | |
241 | { | |
a88797b5 FF |
242 | /* Ignore this signal since it will happen when we try to set the |
243 | pgrp. */ | |
244 | void (*osigttou) (); | |
245 | int result; | |
246 | ||
bd5635a1 RP |
247 | terminal_is_ours = 1; |
248 | ||
a88797b5 FF |
249 | #ifdef SIGTTOU |
250 | if (job_control) | |
251 | osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN); | |
252 | #endif | |
bd5635a1 | 253 | |
a88797b5 FF |
254 | if (inferior_ttystate) |
255 | free (inferior_ttystate); | |
256 | inferior_ttystate = SERIAL_GET_TTY_STATE (stdin_serial); | |
bd5635a1 | 257 | |
a88797b5 FF |
258 | /* Here we used to set ICANON in our ttystate, but I believe this |
259 | was an artifact from before when we used readline. Readline sets | |
260 | the tty state when it needs to. */ | |
bd5635a1 | 261 | |
a88797b5 FF |
262 | /* Set tty state to our_ttystate. We don't change in our out of raw |
263 | mode, to avoid flushing input. We need to do the same thing | |
264 | regardless of output_only, because we don't have separate | |
265 | terminal_is_ours and terminal_is_ours_for_output flags. It's OK, | |
266 | though, since readline will deal with raw mode when/if it needs to. | |
267 | */ | |
268 | SERIAL_NOFLUSH_SET_TTY_STATE (stdin_serial, our_ttystate, | |
269 | inferior_ttystate); | |
bd5635a1 | 270 | |
a88797b5 FF |
271 | #ifdef SIGTTOU |
272 | if (job_control) | |
273 | signal (SIGTTOU, osigttou); | |
bd5635a1 | 274 | #endif |
bd5635a1 | 275 | |
a88797b5 FF |
276 | if (!job_control) |
277 | { | |
278 | signal (SIGINT, sigint_ours); | |
279 | signal (SIGQUIT, sigquit_ours); | |
280 | } | |
bd5635a1 | 281 | |
49781499 | 282 | #ifdef F_GETFL |
a88797b5 | 283 | tflags_inferior = fcntl (0, F_GETFL, 0); |
e676a15f | 284 | |
a88797b5 FF |
285 | /* Is there a reason this is being done twice? It happens both |
286 | places we use F_SETFL, so I'm inclined to think perhaps there | |
287 | is some reason, however perverse. Perhaps not though... */ | |
288 | result = fcntl (0, F_SETFL, tflags_ours); | |
289 | result = fcntl (0, F_SETFL, tflags_ours); | |
49781499 | 290 | #endif |
a88797b5 FF |
291 | |
292 | result = result; /* lint */ | |
293 | } | |
bd5635a1 RP |
294 | } |
295 | ||
296 | /* ARGSUSED */ | |
297 | void | |
298 | term_info (arg, from_tty) | |
299 | char *arg; | |
300 | int from_tty; | |
301 | { | |
302 | target_terminal_info (arg, from_tty); | |
303 | } | |
304 | ||
715d2e06 | 305 | /* ARGSUSED */ |
bd5635a1 RP |
306 | void |
307 | child_terminal_info (args, from_tty) | |
308 | char *args; | |
309 | int from_tty; | |
310 | { | |
a88797b5 FF |
311 | if (!gdb_has_a_terminal ()) |
312 | { | |
313 | printf_filtered ("This GDB does not control a terminal.\n"); | |
314 | return; | |
315 | } | |
bd5635a1 RP |
316 | |
317 | printf_filtered ("Inferior's terminal status (currently saved by GDB):\n"); | |
318 | ||
a88797b5 FF |
319 | /* First the fcntl flags. */ |
320 | { | |
321 | int flags; | |
322 | ||
323 | flags = tflags_inferior; | |
bd5635a1 | 324 | |
a88797b5 | 325 | printf_filtered ("File descriptor flags = "); |
bd5635a1 | 326 | |
a88797b5 FF |
327 | #ifndef O_ACCMODE |
328 | #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR) | |
bd5635a1 | 329 | #endif |
a88797b5 FF |
330 | /* (O_ACCMODE) parens are to avoid Ultrix header file bug */ |
331 | switch (flags & (O_ACCMODE)) | |
332 | { | |
333 | case O_RDONLY: printf_filtered ("O_RDONLY"); break; | |
334 | case O_WRONLY: printf_filtered ("O_WRONLY"); break; | |
335 | case O_RDWR: printf_filtered ("O_RDWR"); break; | |
336 | } | |
337 | flags &= ~(O_ACCMODE); | |
bd5635a1 | 338 | |
a88797b5 FF |
339 | #ifdef O_NONBLOCK |
340 | if (flags & O_NONBLOCK) | |
341 | printf_filtered (" | O_NONBLOCK"); | |
342 | flags &= ~O_NONBLOCK; | |
bd5635a1 | 343 | #endif |
a88797b5 FF |
344 | |
345 | #if defined (O_NDELAY) | |
346 | /* If O_NDELAY and O_NONBLOCK are defined to the same thing, we will | |
347 | print it as O_NONBLOCK, which is good cause that is what POSIX | |
348 | has, and the flag will already be cleared by the time we get here. */ | |
349 | if (flags & O_NDELAY) | |
350 | printf_filtered (" | O_NDELAY"); | |
351 | flags &= ~O_NDELAY; | |
bd5635a1 | 352 | #endif |
a88797b5 FF |
353 | |
354 | if (flags & O_APPEND) | |
355 | printf_filtered (" | O_APPEND"); | |
356 | flags &= ~O_APPEND; | |
357 | ||
358 | #if defined (O_BINARY) | |
359 | if (flags & O_BINARY) | |
360 | printf_filtered (" | O_BINARY"); | |
361 | flags &= ~O_BINARY; | |
e676a15f FF |
362 | #endif |
363 | ||
a88797b5 FF |
364 | if (flags) |
365 | printf_filtered (" | 0x%x", flags); | |
366 | printf_filtered ("\n"); | |
367 | } | |
368 | ||
369 | SERIAL_PRINT_TTY_STATE (stdin_serial, inferior_ttystate); | |
bd5635a1 RP |
370 | } |
371 | \f | |
715d2e06 JG |
372 | /* NEW_TTY_PREFORK is called before forking a new child process, |
373 | so we can record the state of ttys in the child to be formed. | |
374 | TTYNAME is null if we are to share the terminal with gdb; | |
375 | or points to a string containing the name of the desired tty. | |
bd5635a1 | 376 | |
715d2e06 JG |
377 | NEW_TTY is called in new child processes under Unix, which will |
378 | become debugger target processes. This actually switches to | |
379 | the terminal specified in the NEW_TTY_PREFORK call. */ | |
380 | ||
7d9884b9 | 381 | void |
715d2e06 | 382 | new_tty_prefork (ttyname) |
bd5635a1 RP |
383 | char *ttyname; |
384 | { | |
bd5635a1 RP |
385 | /* Save the name for later, for determining whether we and the child |
386 | are sharing a tty. */ | |
387 | inferior_thisrun_terminal = ttyname; | |
715d2e06 JG |
388 | } |
389 | ||
390 | void | |
391 | new_tty () | |
392 | { | |
393 | register int tty; | |
394 | ||
395 | if (inferior_thisrun_terminal == 0) | |
bd5635a1 | 396 | return; |
e676a15f | 397 | #if !defined(__GO32__) |
bd5635a1 | 398 | #ifdef TIOCNOTTY |
e676a15f FF |
399 | /* Disconnect the child process from our controlling terminal. On some |
400 | systems (SVR4 for example), this may cause a SIGTTOU, so temporarily | |
401 | ignore SIGTTOU. */ | |
bd5635a1 RP |
402 | tty = open("/dev/tty", O_RDWR); |
403 | if (tty > 0) | |
404 | { | |
49781499 JK |
405 | void (*osigttou) (); |
406 | ||
e676a15f | 407 | osigttou = (void (*)()) signal(SIGTTOU, SIG_IGN); |
bd5635a1 RP |
408 | ioctl(tty, TIOCNOTTY, 0); |
409 | close(tty); | |
a88797b5 | 410 | signal(SIGTTOU, osigttou); |
bd5635a1 RP |
411 | } |
412 | #endif | |
413 | ||
414 | /* Now open the specified new terminal. */ | |
415 | ||
7d9884b9 JG |
416 | #ifdef USE_O_NOCTTY |
417 | tty = open(inferior_thisrun_terminal, O_RDWR | O_NOCTTY); | |
418 | #else | |
715d2e06 | 419 | tty = open(inferior_thisrun_terminal, O_RDWR); |
7d9884b9 | 420 | #endif |
bd5635a1 RP |
421 | if (tty == -1) |
422 | { | |
715d2e06 | 423 | print_sys_errmsg (inferior_thisrun_terminal, errno); |
bd5635a1 RP |
424 | _exit(1); |
425 | } | |
426 | ||
427 | /* Avoid use of dup2; doesn't exist on all systems. */ | |
428 | if (tty != 0) | |
429 | { close (0); dup (tty); } | |
430 | if (tty != 1) | |
431 | { close (1); dup (tty); } | |
432 | if (tty != 2) | |
433 | { close (2); dup (tty); } | |
434 | if (tty > 2) | |
435 | close(tty); | |
a88797b5 | 436 | #endif /* !go32 */ |
bd5635a1 RP |
437 | } |
438 | \f | |
439 | /* Kill the inferior process. Make us have no inferior. */ | |
440 | ||
441 | /* ARGSUSED */ | |
442 | static void | |
443 | kill_command (arg, from_tty) | |
444 | char *arg; | |
445 | int from_tty; | |
446 | { | |
a88797b5 | 447 | /* Shouldn't this be target_has_execution? FIXME. */ |
bd5635a1 RP |
448 | if (inferior_pid == 0) |
449 | error ("The program is not being run."); | |
a88797b5 | 450 | if (!query ("Kill the program being debugged? ")) |
bd5635a1 | 451 | error ("Not confirmed."); |
ee0613d1 | 452 | target_kill (); |
777bef06 JK |
453 | |
454 | /* Killing off the inferior can leave us with a core file. If so, | |
455 | print the state we are left in. */ | |
456 | if (target_has_stack) { | |
457 | printf_filtered ("In %s,\n", current_target->to_longname); | |
458 | if (selected_frame == NULL) | |
459 | fputs_filtered ("No selected stack frame.\n", stdout); | |
460 | else | |
cadbb07a | 461 | print_stack_frame (selected_frame, selected_frame_level, 1); |
777bef06 | 462 | } |
bd5635a1 RP |
463 | } |
464 | ||
465 | /* The inferior process has died. Long live the inferior! */ | |
466 | ||
467 | void | |
468 | generic_mourn_inferior () | |
469 | { | |
470 | inferior_pid = 0; | |
471 | attach_flag = 0; | |
472 | mark_breakpoints_out (); | |
473 | registers_changed (); | |
474 | ||
475 | #ifdef CLEAR_DEFERRED_STORES | |
476 | /* Delete any pending stores to the inferior... */ | |
477 | CLEAR_DEFERRED_STORES; | |
478 | #endif | |
479 | ||
bd5635a1 | 480 | reopen_exec_file (); |
49781499 | 481 | flush_cached_frames (); |
777bef06 | 482 | if (target_has_stack) { |
bd5635a1 RP |
483 | set_current_frame ( create_new_frame (read_register (FP_REGNUM), |
484 | read_pc ())); | |
777bef06 JK |
485 | select_frame (get_current_frame (), 0); |
486 | } else { | |
bd5635a1 | 487 | set_current_frame (0); |
777bef06 JK |
488 | select_frame ((FRAME) 0, -1); |
489 | } | |
bd5635a1 RP |
490 | /* It is confusing to the user for ignore counts to stick around |
491 | from previous runs of the inferior. So clear them. */ | |
492 | breakpoint_clear_ignore_counts (); | |
493 | } | |
bd5635a1 | 494 | \f |
a88797b5 FF |
495 | /* Call set_sigint_trap when you need to pass a signal on to an attached |
496 | process when handling SIGINT */ | |
497 | ||
bd5635a1 RP |
498 | /* ARGSUSED */ |
499 | static void | |
a88797b5 FF |
500 | pass_signal (signo) |
501 | int signo; | |
bd5635a1 | 502 | { |
a88797b5 FF |
503 | kill (inferior_pid, SIGINT); |
504 | } | |
bd5635a1 | 505 | |
a88797b5 FF |
506 | static void (*osig)(); |
507 | ||
508 | void | |
509 | set_sigint_trap() | |
510 | { | |
511 | osig = (void (*) ()) signal (SIGINT, pass_signal); | |
512 | } | |
513 | ||
514 | void | |
515 | clear_sigint_trap() | |
516 | { | |
517 | signal (SIGINT, osig); | |
bd5635a1 | 518 | } |
bd5635a1 RP |
519 | \f |
520 | void | |
521 | _initialize_inflow () | |
522 | { | |
523 | add_info ("terminal", term_info, | |
524 | "Print inferior's saved terminal status."); | |
525 | ||
bd5635a1 RP |
526 | add_com ("kill", class_run, kill_command, |
527 | "Kill execution of program being debugged."); | |
528 | ||
529 | inferior_pid = 0; | |
530 | ||
bd5635a1 RP |
531 | terminal_is_ours = 1; |
532 | } |