#include #include #include #include #include #include void get_output (int fd); int main (int argc, const char **argv) { int master; pid_t pid; if ((pid = forkpty (&master, NULL, NULL, NULL)) < 0) { perror ("forkpty"); exit (1); } /* child */ if (pid == 0) { const char *av[100]; // putenv ("HOME=/tmp"); int i = 0; #ifdef STRACE_GDB av[i++] = "strace"; av[i++] = "-o"; av[i++] = "/tmp/strace.out"; #ifdef __CYGWIN__ av[i++] = "--mask=all+paranoid"; #endif #endif av[i++] = argv[1] ?: "gdb"; fprintf (stderr, "*** using %s\n", av[0]); av[i++] = "-i=mi"; av[i] = NULL; execvp (av[0], (char * const *) av); /* shouldn't get here */ exit (1); } /* parent */ const char *input[20]; int i = 0; input[i++] = "1-inferior-tty-set /dev/pty3\n"; input[i++] = "2-gdb-set height 0\n"; input[i++] = "3-gdb-set non-stop 1\n"; input[i++] = "4-file-list-exec-source-files\n"; input[i++] = "5-file-list-exec-source-file\n"; input[i++] = "6-gdb-show prompt\n"; input[i++] = "7-stack-info-frame\n"; input[i++] = "8-thread-info\n"; input[i++] = "9-break-list\n"; input[i++] = "q\n"; input[i] = NULL; for (int i = 0; input[i]; ++i) { write (master, input[i], strlen (input[i])); // sleep (1); } get_output (master); wait (NULL); } void get_output (int fd) { char buf[4096]; while (1) { int nread = read (fd, buf, sizeof (buf)); if (nread > 0) write (STDOUT_FILENO, buf, nread); else { printf ("No more output. nread %d\n", nread); break; } } }