garg@alpha.ces.cwru.edu (07/27/88)
Hi There :
I am developing a menu driven X window interface for gdb.
I have finished most of it. I have a separate window for debugee process's
I/O. I opened up a pseudo terminal and duped the slave side to the
child process's stdin, stdout and stderr. After writing to the slave side,
I try to read from the parent side (master), My program gets stuck. I will
highly appreciate if some kind soul can help me out.
The related code goes as follows:
get_pty ()
{
int devindex, letter = 0;
while (letter < 3)
{
ttydev [8] = ptydev [8] = "pqr" [letter++];
devindex = 0;
while (devindex < 16)
{
ttydev [9] = ptydev [9] = "0123456789abcdef" [devindex++];
if ((master = open (ptydev, O_RDWR )) < 0)
continue;
if ((slave = open (ttydev, O_RDWR)) < 0)
{
close(master);
continue;
}
close(slave);
return;
}
}
fprintf (stderr, "Not enough available pty's\n");
exit (1);
}
/*
I call this function in the function create_inferior() before
the execle system call in gdb's source.
*/
/* SETS SLAVE AS THE CHILD'S CONTROL TERMINAL AND I/O CHANNEL */
set_slave_as_inferior_terminal()
{
int cnttty; /* control terminal fd */
struct sgttyb oldsg;
struct tchars oldtc;
struct ltchars oldltc;
int lined;
unsigned lmode;
/* Remove control terminal */
cnttty = open("/dev/tty", O_RDWR);
/* Save old terminal characteristics */
ioctl(cnttty, TIOCGETP, &oldsg);
ioctl(cnttty, TIOCGETC, &oldtc);
ioctl(cnttty, TIOCGLTC, &oldltc);
ioctl(cnttty, TIOCGETD, &lined);
ioctl(cnttty, TIOCLGET, (char *)&lmode);
/* Now remove */
ioctl(cnttty, TIOCNOTTY, 0);
close(cnttty);
if((cnttty = open(ttydev, O_RDWR)) < 0)
{
printf(stderr, "error opening childs control terminal\n");
exit(1);
}
/* chown(ttydev, getuid(), getgid());
chmod(ttydev, 0622);
oldsg.sg_flags & = ~(ALLDELAY | XTABS | CBREAK | RAW);
oldsg.sg_flags | = ECHO | CRMOD;
*/
/* Set as old terminal characteristics */
if((ioctl(cnttty, TIOCSETP, &oldsg)) == -1)
{
printf(stderr, "error setting childs control terminal\n");
exit(1);
}
if((ioctl(cnttty, TIOCSETC, &oldtc)) == -1)
{
printf(stderr, "error setting childs control terminal\n");
exit(1);
}
if((ioctl(cnttty, TIOCSLTC, &oldltc)) == -1)
{
printf(stderr, "error setting childs control terminal\n");
exit(1);
}
if((ioctl(cnttty, TIOCSETD, &lined)) == -1)
{
printf(stderr, "error setting childs control terminal\n");
exit(1);
}
if((ioctl(cnttty, TIOCLSET, (char *)&lmode)) == -1)
{
printf(stderr, "error setting childs control terminal\n");
exit(1);
}
/* set standard files to be the same as the control terminal */
dup2(cnttty, 0);
dup2(cnttty, 1);
dup2(cnttty, 2);
/* close the control terminal not needed any more */
close(cnttty);
}
/* From gdb's command_loop() function I enter this function and after that
I will remain in this function only.
This works fine if my debugee process does not write anything and gets
stuck if the debugee process writes something on the slave side. */
/* This is the main command loop */
void
xgdb_dispatch (fp)
FILE *fp;
{
int xmask = 1 << ConnectionNumber(XtDisplay(toplevel));
int rfds = 0;
int nfds;
int pend, i;
char buffer[64];
int inferior_out_mask;
get_pty();
inferior_out_mask = 1 << master;
while(1)
{
pend = XPending(XtDisplay(toplevel));
if (!pend)
{
rfds = xmask | inferior_out_mask ;
nfds = select (32, &rfds, 0, 0, (struct timeval *) 0);
}
if (pend || rfds & xmask)
{
XNextEvent(XtDisplay(toplevel), &ev);
XtDispatchEvent(&ev);
}
while(rfds & inferior_out_mask) /* !! IT SEEMS STUCK HERE */
{
if((i = read(master, buffer, 64)) > 0)
printf("%s", buffer);
fflush(stdout);
rfds = rfds & (~inferior_out_mask);
}
}
}
/* Any help will be highly appreciated. */
usenet: {decvax,cbosgd,sun}!mandrill!garg Dev Datt Garg
csnet: garg@mandrill.CWRU.edu
arpa: garg@mandrill.CWRU.edushikascs@clutx.clarkson.edu (Glo) (03/28/91)
Hi, There is a problem with the pseudoterminals not closing properly.. Is there any way to change the state of the terminal from W (waiting for another process to a normal state? This is the results from pstat -t.. 1 Console # RAW CAN OUT MODE ADDR DEL COL STATE PGRP DISC 0 0 0 0 54050cd8 0 0 0 O C 4893 ntty 64 As lines # RAW CAN OUT MODE ADDR DEL COL STATE PGRP DISC 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 40000cd2 0 0 0 0 3 0 0 0 18 0 0 0 W 0 4 0 0 0 18 0 0 0 W 0 5 0 0 0 18 0 0 0 W 0 6 0 0 0 18 0 0 0 W 0 7 0 0 0 544500f0 0 0 0 O C 25782 8 0 0 0 544500f0 0 0 0 O C 27484 9 0 0 0 0 0 0 0 0 10 0 0 0 18 0 0 10 W 0 11 0 0 0 18 0 0 0 W 0 12 0 0 0 18 0 0 0 W 0 13 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 16 72 0 338 40000cd2 0 1 61 O CBA S 0 ntty 17 0 0 0 18 0 0 0 W 0 18 0 0 0 18 0 0 0 W 0 19 0 0 0 544500d8 0 0 17 O C 26423 ntty 20 0 0 0 18 0 0 0 W 0 21 0 0 0 18 0 0 0 W 0 22 0 0 0 18 0 0 0 W 0 23 0 0 0 18 0 0 0 W 0 24 0 0 0 18 0 0 0 W 0 25 0 0 0 0 0 0 0 0 26 0 0 0 18 0 0 0 W 0 27 0 0 0 0 0 0 0 0 28 0 0 0 544500d8 0 0 10 O C 24512 ntty 29 0 0 0 0 0 0 0 0 30 0 0 0 544500f0 0 0 0 O C 28328 31 0 0 0 0 0 0 0 0 32 0 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 34 0 0 0 0 0 0 0 0 35 0 0 0 0 0 0 0 0 36 0 0 0 0 0 0 0 0 37 0 0 0 0 0 0 0 0 38 0 0 0 0 0 0 0 0 39 0 0 0 0 0 0 0 0 40 0 0 0 0 0 0 0 0 41 0 0 0 0 0 0 0 0 42 0 0 0 0 0 0 0 0 43 0 0 0 0 0 0 0 0 44 0 0 0 0 0 0 0 0 45 0 0 0 0 0 0 0 0 46 0 0 0 0 0 0 0 0 47 0 0 0 0 0 0 0 0 48 0 0 0 0 0 0 0 0 49 0 0 0 0 0 0 0 0 50 0 0 0 0 0 0 0 0 51 0 0 0 0 0 0 0 0 52 0 0 0 0 0 0 0 0 53 0 0 0 0 0 0 0 0 54 0 0 0 0 0 0 0 0 55 0 0 0 0 0 0 0 0 56 0 0 0 0 0 0 0 0 57 0 0 0 0 0 0 0 0 58 0 0 0 0 0 0 0 0 59 0 0 0 0 0 0 0 0 60 0 0 0 0 0 0 0 0 61 0 0 0 0 0 0 0 0 62 0 0 0 0 0 0 0 0 63 0 0 0 0 0 0 0 0 64 pty lines # RAW CAN OUT MODE ADDR DEL COL STATE PGRP DISC 0 0 0 0 544500d8 0 0 0 W 0 1 0 0 0 544500d8 0 0 0 W 0 2 0 0 0 544500d8 0 0 0 W 0 3 0 0 0 544500d8 0 0 0 W 0 4 0 0 0 540500d8 0 0 0 O 14620 ntty 5 0 0 0 544500d8 0 0 6 W 0 6 0 0 0 544500d8 0 0 0 W 0 7 0 0 0 544500d8 0 0 10 W 0 8 0 0 0 544500d8 0 0 0 O 961 ntty 9 0 0 0 544500d8 0 0 0 W 0 10 0 0 0 544500d8 0 0 0 W 0 11 0 0 0 540500c2 0 0 14 O C 5907 ntty 12 0 0 0 cd8 0 0 0 O C 6208 ntty 13 0 0 0 544500d8 0 0 0 W 0 14 0 0 0 544500d8 0 0 0 O C 8250 ntty 15 0 0 0 540500d8 0 0 11 O C 18539 ntty 16 0 0 0 140500d8 0 0 6 0 17 0 0 0 544500d8 0 0 0 O C 29312 ntty 18 0 0 0 544500d8 0 0 0 O 10951 ntty 19 0 0 0 544500d8 0 0 0 W 0 20 0 0 0 544500d8 0 0 0 O C 6121 ntty 21 0 0 0 544500d2 0 0 74 O C 6093 ntty 22 0 0 0 54450cd8 0 0 0 W 0 23 0 0 0 540500c2 0 0 20 O C 6145 ntty 24 0 0 0 544500d8 0 0 0 W 0 25 0 0 0 540500d8 0 0 2 O C 28697 ntty 26 0 0 0 544500d8 0 0 0 O C 6211 ntty 27 0 0 0 544500d8 0 0 5 W 0 28 0 0 0 544500d8 0 0 6 W 0 29 0 0 0 54450cd8 0 0 0 W 0 30 0 0 0 544500d8 0 0 11 W 0 31 0 0 0 544500d8 0 0 0 W 0 32 0 0 0 544500d8 0 0 6 W 0 33 0 0 0 540500d8 0 0 0 O C 6212 ntty 34 0 0 0 544500d8 0 0 0 W 0 35 0 0 0 544500d8 0 0 10 O C 4896 ntty 36 0 0 0 544500d8 0 0 0 W 0 37 0 0 0 544500d8 0 0 0 W 0 38 0 0 0 544500d8 0 0 0 0 39 0 0 0 544500d8 0 0 0 W 0 40 0 0 0 544500d8 0 0 0 0 41 0 0 0 544500d8 0 0 0 0 42 0 0 0 544500d8 0 0 2 O C 4432 ntty 43 0 0 0 544500d2 0 0 11 0 44 0 0 0 544500d8 0 0 0 O 15154 ntty 45 0 0 0 544500d8 0 0 0 W 0 46 0 0 0 544500d8 0 0 0 0 47 0 0 0 544500d8 0 0 0 W 0 48 0 0 0 54450cd8 0 0 0 0 49 0 0 0 544500d8 0 0 0 0 50 0 0 0 544500d8 0 0 0 O 20127 ntty 51 0 0 0 544500d2 0 0 98 O C 5976 ntty 52 0 0 0 540500d8 0 0 6 W 0 53 0 0 0 544500d8 0 0 0 0 54 0 0 0 544500d8 0 0 0 0 55 0 0 0 544500d8 0 0 10 O C 24209 ntty 56 0 0 0 544500d8 0 0 0 O 1706 ntty 57 0 0 0 540500d8 0 0 0 O 21915 ntty 58 0 0 0 540500d8 0 0 6 W 0 59 0 0 0 544500d8 0 0 0 O 18771 ntty 60 0 0 0 54450cd8 0 0 6 0 61 0 0 0 544500d8 0 0 0 0 62 0 0 0 544500d8 0 0 4 0 63 0 0 0 54050cd8 0 0 0 0 Thanks in advance, -Chris Addr: shikascs@clutx.clarkson.edu