#include "defs.h"
#include "frame.h"
#include "inferior.h"
#include "command.h"
#include "serial.h"
#include "terminal.h"
#include "target.h"
#include "gdbthread.h"
#include "gdb_string.h"
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_TERMIOS
#define PROCESS_GROUP_TYPE pid_t
#endif
#ifdef HAVE_TERMIO
#define PROCESS_GROUP_TYPE int
#endif
#ifdef HAVE_SGTTY
#ifdef SHORT_PGRP
#define PROCESS_GROUP_TYPE short
#else
#define PROCESS_GROUP_TYPE int
#endif
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
static void handle_sigio (int);
#endif
extern void _initialize_inflow (void);
static void pass_signal (int);
static void kill_command (char *, int);
static void terminal_ours_1 (int);
static struct serial *stdin_serial;
static serial_ttystate inferior_ttystate;
static serial_ttystate our_ttystate;
static int tflags_inferior;
static int tflags_ours;
#ifdef PROCESS_GROUP_TYPE
PROCESS_GROUP_TYPE our_process_group;
PROCESS_GROUP_TYPE inferior_process_group;
#endif
static void (*sigint_ours) ();
static void (*sigquit_ours) ();
static char *inferior_thisrun_terminal;
int terminal_is_ours;
enum
{
yes, no, have_not_checked
}
gdb_has_a_terminal_flag = have_not_checked;
int
gdb_has_a_terminal (void)
{
switch (gdb_has_a_terminal_flag)
{
case yes:
return 1;
case no:
return 0;
case have_not_checked:
#ifdef F_GETFL
tflags_ours = fcntl (0, F_GETFL, 0);
#endif
gdb_has_a_terminal_flag = no;
stdin_serial = serial_fdopen (0);
if (stdin_serial != NULL)
{
our_ttystate = serial_get_tty_state (stdin_serial);
if (our_ttystate != NULL)
{
gdb_has_a_terminal_flag = yes;
#ifdef HAVE_TERMIOS
our_process_group = tcgetpgrp (0);
#endif
#ifdef HAVE_TERMIO
our_process_group = getpgrp ();
#endif
#ifdef HAVE_SGTTY
ioctl (0, TIOCGPGRP, &our_process_group);
#endif
}
}
return gdb_has_a_terminal_flag == yes;
default:
return 0;
}
}
#define OOPSY(what) \
if (result == -1) \
fprintf_unfiltered(gdb_stderr, "[%s failed in terminal_inferior: %s]\n", \
what, safe_strerror (errno))
static void terminal_ours_1 (int);
void
terminal_init_inferior_with_pgrp (int pgrp)
{
if (gdb_has_a_terminal ())
{
if (inferior_ttystate)
xfree (inferior_ttystate);
inferior_ttystate = serial_get_tty_state (stdin_serial);
#ifdef PROCESS_GROUP_TYPE
inferior_process_group = pgrp;
#endif
terminal_is_ours = 1;
}
}
void
terminal_init_inferior (void)
{
#ifdef PROCESS_GROUP_TYPE
terminal_init_inferior_with_pgrp (PIDGET (inferior_ptid));
#endif
}
void
terminal_inferior (void)
{
if (gdb_has_a_terminal () && terminal_is_ours
&& inferior_thisrun_terminal == 0)
{
int result;
#ifdef F_GETFL
result = fcntl (0, F_SETFL, tflags_inferior);
result = fcntl (0, F_SETFL, tflags_inferior);
OOPSY ("fcntl F_SETFL");
#endif
result = serial_set_tty_state (stdin_serial, inferior_ttystate);
OOPSY ("setting tty state");
if (!job_control)
{
sigint_ours = (void (*)()) signal (SIGINT, SIG_IGN);
#ifdef SIGQUIT
sigquit_ours = (void (*)()) signal (SIGQUIT, SIG_IGN);
#endif
}
if (job_control)
{
#ifdef HAVE_TERMIOS
result = tcsetpgrp (0, inferior_process_group);
if (!attach_flag)
OOPSY ("tcsetpgrp");
#endif
#ifdef HAVE_SGTTY
result = ioctl (0, TIOCSPGRP, &inferior_process_group);
if (!attach_flag)
OOPSY ("TIOCSPGRP");
#endif
}
}
terminal_is_ours = 0;
}
void
terminal_ours_for_output (void)
{
terminal_ours_1 (1);
}
void
terminal_ours (void)
{
terminal_ours_1 (0);
}
static void
terminal_ours_1 (int output_only)
{
if (inferior_thisrun_terminal != 0 || gdb_has_a_terminal () == 0)
return;
if (!terminal_is_ours)
{
#ifdef SIGTTOU
void (*osigttou) () = NULL;
#endif
int result;
terminal_is_ours = 1;
#ifdef SIGTTOU
if (job_control)
osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
#endif
if (inferior_ttystate)
xfree (inferior_ttystate);
inferior_ttystate = serial_get_tty_state (stdin_serial);
if (!attach_flag)
{
#ifdef HAVE_TERMIOS
inferior_process_group = tcgetpgrp (0);
#endif
#ifdef HAVE_TERMIO
inferior_process_group = getpgrp ();
#endif
#ifdef HAVE_SGTTY
ioctl (0, TIOCGPGRP, &inferior_process_group);
#endif
}
serial_noflush_set_tty_state (stdin_serial, our_ttystate,
inferior_ttystate);
if (job_control)
{
#ifdef HAVE_TERMIOS
result = tcsetpgrp (0, our_process_group);
#if 0
if (result == -1)
fprintf_unfiltered (gdb_stderr, "[tcsetpgrp failed in terminal_ours: %s]\n",
safe_strerror (errno));
#endif
#endif
#ifdef HAVE_SGTTY
result = ioctl (0, TIOCSPGRP, &our_process_group);
#endif
}
#ifdef SIGTTOU
if (job_control)
signal (SIGTTOU, osigttou);
#endif
if (!job_control)
{
signal (SIGINT, sigint_ours);
#ifdef SIGQUIT
signal (SIGQUIT, sigquit_ours);
#endif
}
#ifdef F_GETFL
tflags_inferior = fcntl (0, F_GETFL, 0);
result = fcntl (0, F_SETFL, tflags_ours);
result = fcntl (0, F_SETFL, tflags_ours);
#endif
result = result;
}
}
void
term_info (char *arg, int from_tty)
{
target_terminal_info (arg, from_tty);
}
static void
print_terminal_flags (int flags)
{
#ifndef O_ACCMODE
#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
#endif
switch (flags & (O_ACCMODE))
{
case O_RDONLY:
printf_filtered ("O_RDONLY");
break;
case O_WRONLY:
printf_filtered ("O_WRONLY");
break;
case O_RDWR:
printf_filtered ("O_RDWR");
break;
}
flags &= ~(O_ACCMODE);
#ifdef O_NONBLOCK
if (flags & O_NONBLOCK)
printf_filtered (" | O_NONBLOCK");
flags &= ~O_NONBLOCK;
#endif
#if defined (O_NDELAY)
if (flags & O_NDELAY)
printf_filtered (" | O_NDELAY");
flags &= ~O_NDELAY;
#endif
if (flags & O_APPEND)
printf_filtered (" | O_APPEND");
flags &= ~O_APPEND;
#if defined (O_BINARY)
if (flags & O_BINARY)
printf_filtered (" | O_BINARY");
flags &= ~O_BINARY;
#endif
if (flags)
printf_filtered (" | 0x%x", flags);
printf_filtered ("\n");
}
void
child_terminal_info (char *args, int from_tty)
{
if (!gdb_has_a_terminal ())
{
printf_filtered ("This GDB does not control a terminal.\n");
return;
}
printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
printf_filtered ("File descriptor flags = ");
print_terminal_flags (tflags_inferior);
#ifdef PROCESS_GROUP_TYPE
printf_filtered ("Process group = %d\n", inferior_process_group);
#endif
printf_filtered ("tty state:\n");
serial_print_tty_state (stdin_serial, inferior_ttystate, gdb_stdout);
printf_filtered ("GDB's terminal status (currently in use):\n");
#ifdef PROCESS_GROUP_TYPE
printf_filtered ("Process group = %d\n", our_process_group);
#endif
printf_filtered ("tty state:\n");
serial_print_tty_state (stdin_serial, our_ttystate, gdb_stdout);
}
void
new_tty_prefork (char *ttyname)
{
inferior_thisrun_terminal = ttyname;
}
void
new_tty (void)
{
register int tty;
if (inferior_thisrun_terminal == 0)
return;
#if !defined(__GO32__) && !defined(_WIN32)
#ifdef TIOCNOTTY
tty = open ("/dev/tty", O_RDWR);
if (tty > 0)
{
void (*osigttou) ();
osigttou = (void (*)()) signal (SIGTTOU, SIG_IGN);
ioctl (tty, TIOCNOTTY, 0);
close (tty);
signal (SIGTTOU, osigttou);
}
#endif
#ifdef USE_O_NOCTTY
tty = open (inferior_thisrun_terminal, O_RDWR | O_NOCTTY);
#else
tty = open (inferior_thisrun_terminal, O_RDWR);
#endif
if (tty == -1)
{
print_sys_errmsg (inferior_thisrun_terminal, errno);
_exit (1);
}
if (tty != 0)
{
close (0);
dup (tty);
}
if (tty != 1)
{
close (1);
dup (tty);
}
if (tty != 2)
{
close (2);
dup (tty);
}
if (tty > 2)
close (tty);
#endif
}
static void
kill_command (char *arg, int from_tty)
{
if (ptid_equal (inferior_ptid, null_ptid))
error ("The program is not being run.");
if (!query ("Kill the program being debugged? "))
error ("Not confirmed.");
target_kill ();
init_thread_list ();
if (target_has_stack)
{
printf_filtered ("In %s,\n", target_longname);
if (selected_frame == NULL)
fputs_filtered ("No selected stack frame.\n", gdb_stdout);
else
print_stack_frame (selected_frame, selected_frame_level, 1);
}
if (state_change_hook)
{
state_change_hook (STATE_INFERIOR_LOADED);
}
}
static void
pass_signal (int signo)
{
#ifndef _WIN32
kill (PIDGET (inferior_ptid), SIGINT);
#endif
}
static void (*osig) ();
void
set_sigint_trap (void)
{
if (attach_flag || inferior_thisrun_terminal)
{
osig = (void (*)()) signal (SIGINT, pass_signal);
}
}
void
clear_sigint_trap (void)
{
if (attach_flag || inferior_thisrun_terminal)
{
signal (SIGINT, osig);
}
}
#if defined (SIGIO) && defined (FASYNC) && defined (FD_SET) && defined (F_SETOWN)
static void (*old_sigio) ();
static void
handle_sigio (int signo)
{
int numfds;
fd_set readfds;
signal (SIGIO, handle_sigio);
FD_ZERO (&readfds);
FD_SET (target_activity_fd, &readfds);
numfds = select (target_activity_fd + 1, &readfds, NULL, NULL, NULL);
if (numfds >= 0 && FD_ISSET (target_activity_fd, &readfds))
{
#ifndef _WIN32
if ((*target_activity_function) ())
kill (PIDGET (inferior_ptid), SIGINT);
#endif
}
}
static int old_fcntl_flags;
void
set_sigio_trap (void)
{
if (target_activity_function)
{
old_sigio = (void (*)()) signal (SIGIO, handle_sigio);
fcntl (target_activity_fd, F_SETOWN, getpid ());
old_fcntl_flags = fcntl (target_activity_fd, F_GETFL, 0);
fcntl (target_activity_fd, F_SETFL, old_fcntl_flags | FASYNC);
}
}
void
clear_sigio_trap (void)
{
if (target_activity_function)
{
signal (SIGIO, old_sigio);
fcntl (target_activity_fd, F_SETFL, old_fcntl_flags);
}
}
#else
void
set_sigio_trap (void)
{
if (target_activity_function)
internal_error (__FILE__, __LINE__, "failed internal consistency check");
}
void
clear_sigio_trap (void)
{
if (target_activity_function)
internal_error (__FILE__, __LINE__, "failed internal consistency check");
}
#endif
int
gdb_setpgid (void)
{
int retval = 0;
if (job_control)
{
#if defined (HAVE_TERMIOS) || defined (TIOCGPGRP)
#ifdef HAVE_SETPGID
retval = setpgid (getpid (), getpid ());
#else
#ifdef HAVE_SETPGRP
#ifdef SETPGRP_VOID
retval = setpgrp ();
#else
retval = setpgrp (getpid (), getpid ());
#endif
#endif
#endif
#endif
}
return retval;
}
void
_initialize_inflow (void)
{
add_info ("terminal", term_info,
"Print inferior's saved terminal status.");
add_com ("kill", class_run, kill_command,
"Kill execution of program being debugged.");
inferior_ptid = null_ptid;
terminal_is_ours = 1;
#if defined (HAVE_TERMIOS)
#ifdef _POSIX_JOB_CONTROL
job_control = 1;
#else
#ifdef _SC_JOB_CONTROL
job_control = sysconf (_SC_JOB_CONTROL);
#else
job_control = 0;
#endif
#endif
#endif
#ifdef HAVE_SGTTY
#ifdef TIOCGPGRP
job_control = 1;
#else
job_control = 0;
#endif
#endif
}