PERFORCE change 51151 for review

Marcel Moolenaar marcel at FreeBSD.org
Thu Apr 15 23:42:00 PDT 2004


http://perforce.freebsd.org/chv.cgi?CH=51151

Change 51151 by marcel at marcel_nfs on 2004/04/15 23:41:38

	Call openpty(3) and add a target remote command to the
	command file. The kgdb shell now also gets the remote
	protocol packets.
	While here, move more code into main. I don't know how
	best to structure the code yet, but I do know I don't
	like where it was heading...
	
	Time to open a core file with kgdb, pass the kernel to
	gdb and reply to packets...

Affected files ...

.. //depot/projects/gdb/usr.bin/kgdb/Makefile#3 edit
.. //depot/projects/gdb/usr.bin/kgdb/main.c#4 edit

Differences ...

==== //depot/projects/gdb/usr.bin/kgdb/Makefile#3 (text+ko) ====

@@ -2,5 +2,10 @@
 
 PROG=	kgdb
 SRCS=	main.c
+
+DPADD=	${LIBUTIL}
+LDADD=	-lutil
+
 WARNS?=	4
+
 .include <bsd.prog.mk>

==== //depot/projects/gdb/usr.bin/kgdb/main.c#4 (text+ko) ====

@@ -28,6 +28,7 @@
 __FBSDID("$FreeBSD$");
 
 #include <sys/types.h>
+#include <sys/ioctl.h>
 #include <sys/resource.h>
 #include <sys/select.h>
 #include <sys/time.h>
@@ -37,14 +38,22 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <unistd.h>
 
+#include <libutil.h>
+
 int gdb_in, gdb_out;
 pid_t gdb_pid;
 
 char *cmdfile_name;
 int cmdfile_fd;
 
+char *pty_dev;
+int pty_master, pty_slave;
+
+static const char cmd_set_prompt[] = "set prompt (kgdb) \n";
+
 static void
 atexit_unlink_cmdfile(void)
 {
@@ -82,7 +91,7 @@
 	if (gdb_pid == 0) {
 		if (asprintf(&command, "--command=%s", cmdfile_name) < 0)
 			err(1, "asprintf(3)");
-		/* Dup stderr last so that err(3) work as long as possible. */
+		/* Dup stderr last so that err(3) works as long as possible. */
 		if (dup2(in, 0) == -1 || dup2(out, 1) == -1 ||
 		    dup2(out, 2) == -1)
 			err(1, "dup2(2)");
@@ -95,12 +104,16 @@
 
 	close(in);
 	close(out);
+	close(pty_slave);
 }
 
-static void
-mkcmdfile(void)
+int
+main(int argc __unused, char *argv[] __unused)
 {
-	static const char set_prompt[] = "set prompt (kgdb) ";
+	char buf[256];
+	fd_set rfds, wfds, xfds;
+	ssize_t sz;
+	int status;
 
 	cmdfile_name = strdup("/tmp/kgdb.XXXXXXXX");
 	if (cmdfile_name == NULL)
@@ -109,19 +122,18 @@
 	if (cmdfile_fd == -1)
 		err(1, "mkstemp(3)");
 	atexit(atexit_unlink_cmdfile);
-	if (write(cmdfile_fd, set_prompt, sizeof(set_prompt) - 1) < 0)
+
+	if (write(cmdfile_fd, cmd_set_prompt, sizeof(cmd_set_prompt) - 1) < 0)
 		err(1, "write(2)");
-}
+
+	if (openpty(&pty_master, &pty_slave, buf, NULL, NULL) == -1)
+		err(1, "openpty(3)");
+	pty_dev = strdup(buf);
 
-int
-main(int argc __unused, char *argv[] __unused)
-{
-	char buf[128];
-	fd_set rfds, wfds, xfds;
-	ssize_t sz;
-	int status;
+	sz = snprintf(buf, sizeof(buf), "target remote %s\n", pty_dev);
+	if (write(cmdfile_fd, buf, sz) < 0)
+		err(1, "write(2)");
 
-	mkcmdfile();
 	launch_gdb();
 
 	while (1) {
@@ -130,14 +142,17 @@
 		FD_ZERO(&xfds);
 		FD_SET(0, &rfds);
 		FD_SET(gdb_out, &rfds);
+		FD_SET(pty_master, &rfds);
 		FD_SET(gdb_in, &xfds);
 		FD_SET(gdb_out, &xfds);
+		FD_SET(pty_master, &xfds);
 		if (select(gdb_out + 1, &rfds, &wfds, &xfds, NULL) == -1) {
 			if (errno != EINTR)
 				err(1, "select(2)");
 			continue;
 		}
-		if (FD_ISSET(gdb_in, &xfds) || FD_ISSET(gdb_out, &xfds))
+		if (FD_ISSET(gdb_in, &xfds) || FD_ISSET(gdb_out, &xfds) ||
+		    FD_ISSET(pty_master, &xfds))
 			break;
 		if (FD_ISSET(0, &rfds)) {
 			sz = read(0, buf, sizeof(buf));
@@ -149,10 +164,19 @@
 			if (sz > 0)
 				sz = write(1, buf, sz);
 		}
+		if (FD_ISSET(pty_master, &rfds)) {
+			sz = read(pty_master, buf, sizeof(buf));
+			if (sz > 0) {
+				buf[sz] = 0;
+				printf("``%s''\n", buf);
+				write(pty_master, "+", 1);
+			}
+		}
 	}
 
 	close(gdb_in);
 	close(gdb_out);
+	close(pty_master);
 
 	wait4(gdb_pid, &status, 0, NULL);
 	return (WEXITSTATUS(status));


More information about the p4-projects mailing list