Sending signal to inferior

Carlos Eduardo Rodrigues de Almeida
2005-12-30T03:37:03Z

Hi all,

I'm trying to write a front-end to GDB.
For the first experiment I wrote a small program just to control gdb..
The program is at the end of the message

I run some program and I can't stop it sending a SIGINT to GDB
if I
kill -2 GDBPID
nothing happens

the only way to stop the inferior is to ps, get its pid and
kill -2 INFERIORPID

someone had this problem in the list but I couldn't find the
solution.. someone pointed to tgdb
http://cgdb.sourceforge.net/

I noticed something:
tgdb has a tgdb_driver that is exactly what I'm trying to do but using
tgdb.. a little program that controls gdb..

Running some program with tgdb_driver I get 3 process:
tgdb_driver
gdb (created by tgdb_driver)
inferior (created by gdb)

if I send a SIGINT to gdb it stops the inferior.

with my program I get:
echo
gdb
inferior

if I send a SIGINT to gdb, it DOES not stop the inferior...

I looked at the source code and it mentions the setsid() funcition:

/* If this is not called, when user types ^c SIGINT gets sent to gdb */
setsid();

but if I comment out this line it keeps working...

I couldn't find what tgdb does to stop the inferior when I send gdb
the SIGINT.. does someone can point me a solution?

Thank you!

Eduardo

The program I wrote:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#include <termio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>

typedef struct _ARG {
int fd_read;
int fd_write;
}ARG;

char buf[2048];
int nRead;

int treatfd(void *arg);
int treatSTDIN(void *arg);

pid_t fork_status;

char cmd1[] = "file /home/eduardo/lab3/Debug/lab3\n";
char cmd2[] = "run\n";
char cmd3[] = "quit\n";
char cmd4[] = "\002";

int main(void) {

int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];

pthread_t tstdin, tstdout, tstderr;
ARG argstdin, argstdout, argstderr;

struct termio Termio;
int bwritten;

if( (pipe(stdin_pipe) != 0) || (pipe(stdout_pipe) != 0) ||
pipe(stderr_pipe) != 0) {
printf("Error creating pipes");
exit(1);
}

fork_status = fork();

if(fork_status == -1){
printf("Error forking");
exit(1);
}
/* Chield process */
else if(fork_status == 0) {
setsid();

close(0);
dup(stdin_pipe[0]);
close(stdin_pipe[0]);
close(stdin_pipe[1]);

close(1);
dup(stdout_pipe[1]);
close(stdout_pipe[0]);
close(stdout_pipe[1]);

close(2);
dup(stderr_pipe[1]);
close(stderr_pipe[0]);
close(stderr_pipe[1]);

// printf("executando gdb");
execlp("gdb", "gdb", (char *)0);

// printf("Error with execl");
exit(1);
}
/* Parent process */
else {
close(stdin_pipe[0]);
close(stdout_pipe[1]);
close(stderr_pipe[1]);

fd_set rdSet;
int max;
int selectRet;

max = (0 > stdout_pipe[0]) ? 0 : stdout_pipe[0];
max = (max > stderr_pipe[0]) ? max : stderr_pipe[0];

while(1) {
FD_ZERO(&rdSet);
FD_SET(0, &rdSet);
FD_SET(stdout_pipe[0], &rdSet);
FD_SET(stderr_pipe[0], &rdSet);

selectRet = select(max + 1, &rdSet, NULL, NULL, NULL);

if(selectRet == -1)
return;

if(FD_ISSET(0, &rdSet)) {
nRead = read(0, buf, 2048);
write(stdin_pipe[1], buf, nRead);
}

if(FD_ISSET(stdout_pipe[0], &rdSet)) {
nRead = read(stdout_pipe[0], buf, 2048);
if(nRead == 0)
return;
write(1, buf, nRead);
}

if(FD_ISSET(stderr_pipe[0], &rdSet)) {
nRead = read(stderr_pipe[0], buf, 2048);
if(nRead == 0)
return;

write(2, buf, nRead);
}
}
}
}