Subject: Re: remote kernel debugging - need some clarification
To: None <thorpej@nas.nasa.gov>
From: Matthias Pfaller <leo@dachau.marco.de>
List: tech-kern
Date: 02/05/1997 15:18:45
Jason Thorpe wrote:
> On Mon, 3 Feb 97 17:22:36 EST 
>  "Gordon W. Ross" <gwr@mc.com> wrote:
> 
>  > Yes, it would be nice to be able to use the "stock" gdb so we don't
>  > have to maintain our own remote-serial module.  Someone has already
>  > done the work to make the netbsd kernel side talk the GNU protocol.
>  > I forget who just now... maybe they'll speak up.
> 
> I think Matthias Pfaller mentioned to me he'd made it work with the
> stock GDB protocol on the pc532 port; I've CC'd him on this mail.

I have appended a few notes I had written down on the pc532 kgdb
implementation some time ago.

Matthias

Since a few weeks NetBSD/pc532 has working remote gdb support. Since one
of my goals was to do as few modifications to gdb as possible, I
converted the hp300/sun3/... kgdb_stub.c to use the standard gdb remote
target protocol. Anyone interested in this might have a look at
sys/arch/pc532/pc532/kgdb_stub.c. I tried to make this as mi as
possible. If you would like to use this on another machine, you would
have to write the functions I have done in kgdb_machdep.c and db_memrw.c
for your machine (mostly stolen from the sun3 port):

	/* Is va..(va+len) valid? */
	int kgdb_acc(vm_offset_t va, size_t len)

	/* Translate trap code to unix signal value. */
	int kgdb_signal(int type)

	/* Translate kernel regs to gdb structure. */
	void kgdb_getregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)

	/* Reverse of kgdb_getregs */
	void kgdb_setregs(db_regs_t *regs, kgdb_reg_t *gdb_regs)

	/* Read size bytes from addr to data. */
	db_read_bytes(vm_offset_t addr, size_t size, char *data)

	/* Write size bytes from data to addr (where addr might be
	 * mapped readonly). */
	db_write_bytes(addr, size, data)

The last thing that is needed to make remote gdb work is a small addition
to your db_machdep.h:

	/*
	 * This is needed for kgdb.
	 */
	typedef long kgdb_reg_t;
	#define KGDB_NUMREGS	25
	#define KGDB_BUFLEN	512

KGDB_BUFLEN should be larger then the largest message you may receive/send
to gdb. If your machine has FIXUP_PC_AFTER_BREAK defined this macro
would have to be redefined to take an argument.

The code seems to work ok on the pc532.

I had a few problems with gdb-4.16 when doing remote debugging. The
following patch fixed this (I already sent it off to gdb-bugs).

--- bfd/netbsd.h.ORIG	Sat Oct 28 01:29:17 1995
+++ bfd/netbsd.h	Sun Sep 22 12:13:07 1996
@@ -38,6 +38,26 @@
 	((exec).a_info = \
 	 ((exec).a_info & 0x03ffffff) | ((flags & 0x03f) << 26))
 
+/* Redefine N_TXTADDR to get the section vmas right for kernels
+   and .o files. N_TXTADDRSTD is stolen from ../include/aout/aout64.h.  */
+#define N_TXTADDRSTD(x) \
+    (/* The address of a QMAGIC file is always one page in, */ \
+     /* with the header in the text.  */ \
+     N_IS_QMAGIC (x) ? TARGET_PAGE_SIZE + EXEC_BYTES_SIZE : \
+     N_MAGIC(x) != ZMAGIC ? 0 :	/* object file or NMAGIC */\
+     N_SHARED_LIB(x) ? 0 :	\
+     N_HEADER_IN_TEXT(x)  ?	\
+	    TEXT_START_ADDR + EXEC_BYTES_SIZE :	/* no padding */\
+	    TEXT_START_ADDR			/* a page of padding */\
+    )
+
+#define N_TXTADDR(x) \
+	(((x).a_entry & ~(N_SEGSIZE(x) - 1)) > N_TXTADDRSTD(x) ? \
+		((x).a_entry & ~(N_SEGSIZE(x) - 1)) : \
+	((x).a_entry == 0 && N_MAGIC(x) == OMAGIC \
+	 || (x).a_entry < N_SEGSIZE(x) && N_MAGIC(x) == ZMAGIC ? \
+	 	0 : N_TXTADDRSTD(x)))
+
 #include "bfd.h"
 #include "sysdep.h"
 #include "libbfd.h"
-- 
leo@dachau.marco.de			in real life: Matthias Pfaller
marco GmbH, 85221 Dachau, Germany	tel: +49 8131 516142