Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 18 Apr 2004 23:18:46 -0700 (PDT)
From:      Marcel Moolenaar <marcel@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 51331 for review
Message-ID:  <200404190618.i3J6Ik0G007048@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=51331

Change 51331 by marcel@marcel_nfs on 2004/04/18 23:17:49

	Bring in the packet code from the kernel. Duplicate for now.
	Still needed:
	o  kvm_openfiles() -- which implies argument parsing
	o  gdb_packet() -- to handle remote protocol packets.

Affected files ...

.. //depot/projects/gdb/usr.bin/kgdb/Makefile#4 edit
.. //depot/projects/gdb/usr.bin/kgdb/kgdb.h#1 add
.. //depot/projects/gdb/usr.bin/kgdb/main.c#5 edit
.. //depot/projects/gdb/usr.bin/kgdb/md_i386.c#1 add
.. //depot/projects/gdb/usr.bin/kgdb/packet.c#1 add

Differences ...

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

@@ -1,10 +1,10 @@
 # $FreeBSD$
 
 PROG=	kgdb
-SRCS=	main.c
+SRCS=	main.c md_${MACHINE_ARCH}.c packet.c
 
-DPADD=	${LIBUTIL}
-LDADD=	-lutil
+DPADD=	${LIBKVM} ${LIBUTIL}
+LDADD=	-lkvm -lutil
 
 WARNS?=	4
 

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

@@ -35,6 +35,9 @@
 #include <sys/wait.h>
 #include <errno.h>
 #include <err.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <kvm.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -43,15 +46,20 @@
 
 #include <libutil.h>
 
-int gdb_in, gdb_out;
+#include "kgdb.h"
+
+int gdb_stdin;
+int gdb_stdout;
+int gdb_rp_fd;
+char *gdb_rp_dev;
 pid_t gdb_pid;
 
+kvm_t *kvm;
+struct kthr *curkthr;
+
 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
@@ -73,38 +81,21 @@
 	*wr = f[1];
 }
 
-static void
-launch_gdb(void)
+int
+gdb_packet_recv(char *p, size_t sz)
 {
-	char *command;
-	int in, out;
-	int f, fmax;
+	return (read(gdb_rp_fd, p, sz));
+}
 
-	close(cmdfile_fd);
+int
+gdb_packet_send(const char *p, size_t sz)
+{
+	return (write(gdb_rp_fd, p, sz));
+}
 
-	mkpipe(&in, &gdb_in);
-	mkpipe(&gdb_out, &out);
-
-	gdb_pid = fork();
-	if (gdb_pid == -1)
-		err(1, "fork(2)");
-	if (gdb_pid == 0) {
-		if (asprintf(&command, "--command=%s", cmdfile_name) < 0)
-			err(1, "asprintf(3)");
-		/* 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)");
-		fmax = getdtablesize();
-		for (f = 3; f < fmax; f++)
-			close(f);
-		execlp("gdb", "gdb", command, NULL);
-		_exit(1);
-	}
-
-	close(in);
-	close(out);
-	close(pty_slave);
+void gdb_packet(void)
+{
+	printf("`%s'\n", gdb_rxp);
 }
 
 int
@@ -113,7 +104,8 @@
 	char buf[256];
 	fd_set rfds, wfds, xfds;
 	ssize_t sz;
-	int status;
+	int fdin, fdout;
+	int f, nfds, status;
 
 	cmdfile_name = strdup("/tmp/kgdb.XXXXXXXX");
 	if (cmdfile_name == NULL)
@@ -126,57 +118,79 @@
 	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)
+	if (openpty(&gdb_rp_fd, &f, buf, NULL, NULL) == -1)
 		err(1, "openpty(3)");
-	pty_dev = strdup(buf);
+	close(f);
+	gdb_rp_dev = strdup(buf);
 
-	sz = snprintf(buf, sizeof(buf), "target remote %s\n", pty_dev);
+	sz = snprintf(buf, sizeof(buf), "target remote %s\n", gdb_rp_dev);
 	if (write(cmdfile_fd, buf, sz) < 0)
 		err(1, "write(2)");
 
-	launch_gdb();
+	close(cmdfile_fd);
+
+	mkpipe(&fdin, &gdb_stdin);
+	mkpipe(&gdb_stdout, &fdout);
+
+	gdb_pid = fork();
+	if (gdb_pid == -1)
+		err(1, "fork(2)");
+	if (gdb_pid == 0) {
+		snprintf(buf, sizeof(buf), "--command=%s", cmdfile_name);
+		/* Dup stderr last so that err(3) works as long as possible. */
+		if (dup2(fdin, 0) == -1 || dup2(fdout, 1) == -1 ||
+		    dup2(fdout, 2) == -1)
+			err(1, "dup2(2)");
+		nfds = getdtablesize();
+		for (f = 3; f < nfds; f++)
+			close(f);
+		execlp("gdb", "gdb", buf, NULL);
+		_exit(1);
+	}
+
+	close(fdin);
+	close(fdout);
+
+	nfds = (gdb_stdin > gdb_stdout) ? gdb_stdin : gdb_stdout;
+	nfds = (gdb_rp_fd > nfds) ? gdb_rp_fd : nfds;
+	nfds++;
 
 	while (1) {
 		FD_ZERO(&rfds);
 		FD_ZERO(&wfds);
 		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) {
+		FD_SET(gdb_stdout, &rfds);
+		FD_SET(gdb_rp_fd, &rfds);
+		FD_SET(gdb_stdin, &xfds);
+		FD_SET(gdb_stdout, &xfds);
+		if (select(nfds, &rfds, &wfds, &xfds, NULL) == -1) {
 			if (errno != EINTR)
 				err(1, "select(2)");
 			continue;
 		}
-		if (FD_ISSET(gdb_in, &xfds) || FD_ISSET(gdb_out, &xfds) ||
-		    FD_ISSET(pty_master, &xfds))
+		if (FD_ISSET(gdb_stdin, &xfds) || FD_ISSET(gdb_stdout, &xfds))
 			break;
 		if (FD_ISSET(0, &rfds)) {
 			sz = read(0, buf, sizeof(buf));
 			if (sz > 0)
-				sz = write(gdb_in, buf, sz);
+				sz = write(gdb_stdin, buf, sz);
 		}
-		if (FD_ISSET(gdb_out, &rfds)) {
-			sz = read(gdb_out, buf, sizeof(buf));
+		if (FD_ISSET(gdb_stdout, &rfds)) {
+			sz = read(gdb_stdout, buf, sizeof(buf));
 			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);
-			}
+		if (FD_ISSET(gdb_rp_fd, &rfds)) {
+			sz = read(gdb_rp_fd, buf, sizeof(buf));
+			if (sz > 0)
+				gdb_packet_data(buf, sz);
 		}
 	}
 
-	close(gdb_in);
-	close(gdb_out);
-	close(pty_master);
+	close(gdb_rp_fd);
+	close(gdb_stdin);
+	close(gdb_stdout);
 
 	wait4(gdb_pid, &status, 0, NULL);
 	return (WEXITSTATUS(status));



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200404190618.i3J6Ik0G007048>