summaryrefslogtreecommitdiffstats
path: root/sys/gdb
diff options
context:
space:
mode:
authormarcel <marcel@FreeBSD.org>2004-07-10 17:47:22 +0000
committermarcel <marcel@FreeBSD.org>2004-07-10 17:47:22 +0000
commit6e0dcca8a9676834e327681abedbeca81a0907f2 (patch)
tree787a3c6916bdd82a49a07a8288119d62dd89055b /sys/gdb
parent1ff719505763720315217ca8b7eb6d6f03c49222 (diff)
downloadFreeBSD-src-6e0dcca8a9676834e327681abedbeca81a0907f2.zip
FreeBSD-src-6e0dcca8a9676834e327681abedbeca81a0907f2.tar.gz
Introduce the GDB debugger backend for the new KDB framework. The
backend improves over the old GDB support in the following ways: o Unified implementation with minimal MD code. o A simple interface for devices to register themselves as debug ports, ala consoles. o Compression by using run-length encoding. o Implements GDB threading support.
Diffstat (limited to 'sys/gdb')
-rw-r--r--sys/gdb/gdb.h62
-rw-r--r--sys/gdb/gdb_int.h109
-rw-r--r--sys/gdb/gdb_main.c261
-rw-r--r--sys/gdb/gdb_packet.c289
4 files changed, 721 insertions, 0 deletions
diff --git a/sys/gdb/gdb.h b/sys/gdb/gdb.h
new file mode 100644
index 0000000..a7a5429
--- /dev/null
+++ b/sys/gdb/gdb.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2004 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _GDB_GDB_H_
+#define _GDB_GDB_H_
+
+typedef int gdb_checkc_f(void);
+typedef int gdb_getc_f(void);
+typedef void gdb_init_f(void);
+typedef int gdb_probe_f(void);
+typedef void gdb_putc_f(int);
+typedef void gdb_term_f(void);
+
+struct gdb_dbgport {
+ const char *gdb_name;
+ gdb_checkc_f *gdb_checkc;
+ gdb_getc_f *gdb_getc;
+ gdb_init_f *gdb_init;
+ gdb_probe_f *gdb_probe;
+ gdb_putc_f *gdb_putc;
+ gdb_term_f *gdb_term;
+ int gdb_active;
+};
+
+#define GDB_DBGPORT(name, probe, init, term, checkc, getc, putc) \
+ static struct gdb_dbgport name##_gdb_dbgport = { \
+ .gdb_name = #name, \
+ .gdb_checkc = checkc, \
+ .gdb_getc = getc, \
+ .gdb_init = init, \
+ .gdb_probe = probe, \
+ .gdb_putc = putc, \
+ .gdb_term = term \
+ }; \
+ DATA_SET(gdb_dbgport_set, name##_gdb_dbgport)
+
+#endif /* !_GDB_GDB_H_ */
diff --git a/sys/gdb/gdb_int.h b/sys/gdb/gdb_int.h
new file mode 100644
index 0000000..1a043bd
--- /dev/null
+++ b/sys/gdb/gdb_int.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2004 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _GDB_GDB_INT_H_
+#define _GDB_GDB_INT_H_
+
+extern struct gdb_dbgport *gdb_cur;
+
+extern char *gdb_rxp;
+extern size_t gdb_rxsz;
+extern char *gdb_txp;
+
+int gdb_rx_begin(void);
+int gdb_rx_equal(const char *);
+int gdb_rx_mem(unsigned char *, size_t);
+int gdb_rx_varhex(uintmax_t *);
+
+static __inline int
+gdb_rx_char(void)
+{
+ int c;
+
+ if (gdb_rxsz > 0) {
+ c = *gdb_rxp++;
+ gdb_rxsz--;
+ } else
+ c = -1;
+ return (c);
+}
+
+void gdb_tx_begin(char);
+int gdb_tx_end(void);
+int gdb_tx_mem(const unsigned char *, size_t);
+void gdb_tx_reg(int);
+
+static __inline void
+gdb_tx_char(char c)
+{
+ *gdb_txp++ = c;
+}
+
+static __inline int
+gdb_tx_empty(void)
+{
+ gdb_tx_begin('\0');
+ return (gdb_tx_end());
+}
+
+static __inline void
+gdb_tx_hex(uintmax_t n, int sz)
+{
+ gdb_txp += sprintf(gdb_txp, "%0*jx", sz, n);
+}
+
+static __inline int
+gdb_tx_err(int err)
+{
+ gdb_tx_begin('E');
+ gdb_tx_hex(err, 2);
+ return (gdb_tx_end());
+}
+
+static __inline int
+gdb_tx_ok(void)
+{
+ gdb_tx_begin('O');
+ gdb_tx_char('K');
+ return (gdb_tx_end());
+}
+
+static __inline void
+gdb_tx_str(const char *s)
+{
+ while (*s)
+ *gdb_txp++ = *s++;
+}
+
+static __inline void
+gdb_tx_varhex(uintmax_t n)
+{
+ gdb_txp += sprintf(gdb_txp, "%jx", n);
+}
+
+#endif /* !_GDB_GDB_INT_H_ */
diff --git a/sys/gdb/gdb_main.c b/sys/gdb/gdb_main.c
new file mode 100644
index 0000000..025c5b8
--- /dev/null
+++ b/sys/gdb/gdb_main.c
@@ -0,0 +1,261 @@
+/*
+ * Copyright (c) 2004 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kdb.h>
+#include <sys/kernel.h>
+#include <sys/pcpu.h>
+#include <sys/proc.h>
+#include <sys/reboot.h>
+
+#include <machine/gdb_machdep.h>
+#include <machine/kdb.h>
+
+#include <gdb/gdb.h>
+#include <gdb/gdb_int.h>
+
+static dbbe_init_f gdb_init;
+static dbbe_trap_f gdb_trap;
+
+KDB_BACKEND(gdb, gdb_init, NULL, gdb_trap);
+
+GDB_DBGPORT(null, NULL, NULL, NULL, NULL, NULL, NULL);
+SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport);
+
+struct gdb_dbgport *gdb_cur = NULL;
+
+static int
+gdb_init(void)
+{
+ struct gdb_dbgport *dp, **iter;
+ int cur_pri, pri;
+
+ gdb_cur = NULL;
+ cur_pri = -1;
+ SET_FOREACH(iter, gdb_dbgport_set) {
+ dp = *iter;
+ pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1;
+ dp->gdb_active = (pri >= 0) ? 0 : -1;
+ if (pri > cur_pri) {
+ cur_pri = pri;
+ gdb_cur = dp;
+ }
+ }
+ if (gdb_cur != NULL) {
+ printf("GDB: debug ports:");
+ SET_FOREACH(iter, gdb_dbgport_set) {
+ dp = *iter;
+ if (dp->gdb_active == 0)
+ printf(" %s", dp->gdb_name);
+ }
+ printf("\n");
+ } else
+ printf("GDB: no debug ports present\n");
+ if (gdb_cur != NULL) {
+ gdb_cur->gdb_init();
+ printf("GDB: current port: %s\n", gdb_cur->gdb_name);
+ }
+ if (gdb_cur != NULL)
+ cur_pri = (boothowto & RB_GDB) ? 2 : 0;
+ else
+ cur_pri = -1;
+ return (cur_pri);
+}
+
+static int
+gdb_trap(int type, int code)
+{
+ struct thread *thr_iter;
+
+ /*
+ * Send a T packet. We currently do not support watchpoints (the
+ * awatch, rwatch or watch elements).
+ */
+ gdb_tx_begin('T');
+ gdb_tx_hex(gdb_cpu_signal(type, code), 2);
+ gdb_tx_varhex(GDB_REG_PC);
+ gdb_tx_char(':');
+ gdb_tx_reg(GDB_REG_PC);
+ gdb_tx_char(';');
+ gdb_tx_str("thread:");
+ gdb_tx_varhex((long)kdb_thread->td_tid);
+ gdb_tx_char(';');
+ gdb_tx_end(); /* XXX check error condition. */
+
+ thr_iter = NULL;
+ while (gdb_rx_begin() == 0) {
+ printf("GDB: got '%s'\n", gdb_rxp);
+ switch (gdb_rx_char()) {
+ case '?': /* Last signal. */
+ gdb_tx_begin('S');
+ gdb_tx_hex(gdb_cpu_signal(type, code), 2);
+ gdb_tx_end();
+ break;
+ case 'c': { /* Continue. */
+ uintmax_t addr;
+ if (!gdb_rx_varhex(&addr))
+ gdb_cpu_setreg(GDB_REG_PC, addr);
+ kdb_cpu_clear_singlestep();
+ return (1);
+ }
+ case 'C': { /* Continue with signal. */
+ uintmax_t addr, sig;
+ if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
+ !gdb_rx_varhex(&addr))
+ gdb_cpu_setreg(GDB_REG_PC, addr);
+ kdb_cpu_clear_singlestep();
+ return (1);
+ }
+ case 'g': { /* Read registers. */
+ size_t r;
+ gdb_tx_begin(0);
+ for (r = 0; r < GDB_NREGS; r++)
+ gdb_tx_reg(r);
+ gdb_tx_end();
+ break;
+ }
+ case 'G': /* Write registers. */
+ gdb_tx_err(0);
+ break;
+ case 'H': { /* Set thread. */
+ intmax_t tid;
+ struct thread *thr;
+ gdb_rx_char();
+ gdb_rx_varhex(&tid);
+ if (tid > 0) {
+ thr = kdb_thr_lookup(tid);
+ if (thr == NULL) {
+ gdb_tx_err(ENOENT);
+ break;
+ }
+ kdb_thr_select(thr);
+ }
+ gdb_tx_ok();
+ break;
+ }
+ case 'k': /* Kill request. */
+ kdb_cpu_clear_singlestep();
+ return (1);
+ case 'm': { /* Read memory. */
+ uintmax_t addr, size;
+ if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
+ gdb_rx_varhex(&size)) {
+ gdb_tx_err(EINVAL);
+ break;
+ }
+ gdb_tx_begin(0);
+ if (gdb_tx_mem((char *)(uintptr_t)addr, size))
+ gdb_tx_end();
+ else
+ gdb_tx_err(EIO);
+ break;
+ }
+ case 'M': { /* Write memory. */
+ uintmax_t addr, size;
+ if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' ||
+ gdb_rx_varhex(&size) || gdb_rx_char() != ':') {
+ gdb_tx_err(EINVAL);
+ break;
+ }
+ if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0)
+ gdb_tx_err(EIO);
+ else
+ gdb_tx_ok();
+ break;
+ }
+ case 'P': { /* Write register. */
+ uintmax_t reg, val;
+ if (gdb_rx_varhex(&reg) || gdb_rx_char() != '=' ||
+ gdb_rx_varhex(&val)) {
+ gdb_tx_err(EINVAL);
+ break;
+ }
+ gdb_cpu_setreg(reg, val);
+ gdb_tx_ok();
+ break;
+ }
+ case 'q': /* General query. */
+ if (gdb_rx_equal("fThreadInfo")) {
+ thr_iter = kdb_thr_first();
+ gdb_tx_begin('m');
+ gdb_tx_hex((long)thr_iter->td_tid, 8);
+ gdb_tx_end();
+ } else if (gdb_rx_equal("sThreadInfo")) {
+ if (thr_iter == NULL) {
+ gdb_tx_err(ENXIO);
+ break;
+ }
+ thr_iter = kdb_thr_next(thr_iter);
+ if (thr_iter != NULL) {
+ gdb_tx_begin('m');
+ gdb_tx_hex((long)thr_iter->td_tid, 8);
+ gdb_tx_end();
+ } else {
+ gdb_tx_begin('l');
+ gdb_tx_end();
+ }
+ } else if (!gdb_cpu_query())
+ gdb_tx_empty();
+ break;
+ case 's': { /* Step. */
+ uintmax_t addr;
+ if (!gdb_rx_varhex(&addr))
+ gdb_cpu_setreg(GDB_REG_PC, addr);
+ kdb_cpu_set_singlestep();
+ return (1);
+ }
+ case 'S': { /* Step with signal. */
+ uintmax_t addr, sig;
+ if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' &&
+ !gdb_rx_varhex(&addr))
+ gdb_cpu_setreg(GDB_REG_PC, addr);
+ kdb_cpu_set_singlestep();
+ return (1);
+ }
+ case 'T': { /* Thread alive. */
+ intmax_t tid;
+ gdb_rx_varhex(&tid);
+ if (kdb_thr_lookup(tid) != NULL)
+ gdb_tx_ok();
+ else
+ gdb_tx_err(ENOENT);
+ break;
+ }
+ case -1:
+ /* Empty command. Treat as unknown command. */
+ /* FALLTHROUGH */
+ default:
+ /* Unknown command. Send empty response. */
+ gdb_tx_empty();
+ break;
+ }
+ }
+ return (0);
+}
diff --git a/sys/gdb/gdb_packet.c b/sys/gdb/gdb_packet.c
new file mode 100644
index 0000000..703d2e0
--- /dev/null
+++ b/sys/gdb/gdb_packet.c
@@ -0,0 +1,289 @@
+/*
+ * Copyright (c) 2004 Marcel Moolenaar
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/ctype.h>
+#include <sys/kdb.h>
+
+#include <machine/gdb_machdep.h>
+
+#include <gdb/gdb.h>
+#include <gdb/gdb_int.h>
+
+static char gdb_rxbuf[GDB_BUFSZ];
+char *gdb_rxp = NULL;
+size_t gdb_rxsz = 0;
+static char gdb_txbuf[GDB_BUFSZ];
+char *gdb_txp = NULL; /* Used in inline functions. */
+
+#define C2N(c) (((c) < 'A') ? (c) - '0' : \
+ 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
+#define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10)
+
+/*
+ * Functions to receive and extract from a packet.
+ */
+
+int
+gdb_rx_begin(void)
+{
+ int c, cksum;
+
+ gdb_rxp = NULL;
+ do {
+ /*
+ * Wait for the start character, ignore all others.
+ * XXX needs a timeout.
+ */
+ while ((c = gdb_cur->gdb_getc()) != '$')
+ ;
+
+ /* Read until a # or end of buffer is found. */
+ cksum = 0;
+ gdb_rxsz = 0;
+ while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
+ c = gdb_cur->gdb_getc();
+ if (c == '#')
+ break;
+ gdb_rxbuf[gdb_rxsz++] = c;
+ cksum += c;
+ }
+ gdb_rxbuf[gdb_rxsz] = 0;
+ cksum &= 0xff;
+
+ /* Bail out on a buffer overflow. */
+ if (c != '#') {
+ gdb_cur->gdb_putc('-');
+ return (ENOSPC);
+ }
+
+ c = gdb_cur->gdb_getc();
+ cksum -= (C2N(c) << 4) & 0xf0;
+ c = gdb_cur->gdb_getc();
+ cksum -= C2N(c) & 0x0f;
+ gdb_cur->gdb_putc((cksum == 0) ? '+' : '-');
+ if (cksum != 0)
+ printf("GDB: packet `%s' has invalid checksum\n",
+ gdb_rxbuf);
+ } while (cksum != 0);
+
+ gdb_rxp = gdb_rxbuf;
+ return (0);
+}
+
+int
+gdb_rx_equal(const char *str)
+{
+ int len;
+
+ len = strlen(str);
+ if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
+ return (0);
+ gdb_rxp += len;
+ gdb_rxsz -= len;
+ return (1);
+}
+
+int
+gdb_rx_mem(unsigned char *addr, size_t size)
+{
+ void *prev;
+ jmp_buf jb;
+ int ret;
+ unsigned char c;
+
+ if (size * 2 != gdb_rxsz)
+ return (-1);
+
+ prev = kdb_jmpbuf(jb);
+ ret = setjmp(jb);
+ if (ret == 0) {
+ while (size-- > 0) {
+ c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
+ c |= C2N(gdb_rxp[1]) & 0x0f;
+ *addr++ = c;
+ gdb_rxsz -= 2;
+ gdb_rxp += 2;
+ }
+ }
+ (void)kdb_jmpbuf(prev);
+ return ((ret == 0) ? 1 : 0);
+}
+
+int
+gdb_rx_varhex(uintmax_t *vp)
+{
+ uintmax_t v;
+ int c, neg;
+
+ c = gdb_rx_char();
+ neg = (c == '-') ? 1 : 0;
+ if (neg == 1)
+ c = gdb_rx_char();
+ if (!isxdigit(c)) {
+ gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
+ gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
+ return (-1);
+ }
+ v = 0;
+ do {
+ v <<= 4;
+ v += C2N(c);
+ c = gdb_rx_char();
+ } while (isxdigit(c));
+ if (c != -1) {
+ gdb_rxp--;
+ gdb_rxsz++;
+ }
+ *vp = (neg) ? -v : v;
+ return (0);
+}
+
+/*
+ * Function to build and send a package.
+ */
+
+void
+gdb_tx_begin(char tp)
+{
+
+ gdb_txp = gdb_txbuf;
+ if (tp != '\0')
+ gdb_tx_char(tp);
+}
+
+int
+gdb_tx_end(void)
+{
+ const char *p;
+ int runlen;
+ unsigned char c, cksum;
+
+ do {
+ gdb_cur->gdb_putc('$');
+
+ cksum = 0;
+ p = gdb_txbuf;
+ while (p < gdb_txp) {
+ /* Send a character and start run-length encoding. */
+ c = *p++;
+ gdb_cur->gdb_putc(c);
+ cksum += c;
+ runlen = 0;
+ /* Determine run-length and update checksum. */
+ while (p < gdb_txp && *p == c) {
+ runlen++;
+ p++;
+ }
+ /* Emit the run-length encoded string. */
+ while (runlen >= 97) {
+ gdb_cur->gdb_putc('*');
+ cksum += '*';
+ gdb_cur->gdb_putc(97+29);
+ cksum += 97+29;
+ runlen -= 97;
+ if (runlen > 0) {
+ gdb_cur->gdb_putc(c);
+ cksum += c;
+ runlen--;
+ }
+ }
+ if (runlen == 1) {
+ gdb_cur->gdb_putc(c);
+ cksum += c;
+ runlen--;
+ }
+ if (runlen == 0)
+ continue;
+ /* Don't emit '$', '#', '+' or '-'. */
+ if (runlen == 7) {
+ gdb_cur->gdb_putc(c);
+ cksum += c;
+ runlen--;
+ }
+ if (runlen == 6 || runlen == 14 || runlen == 16) {
+ gdb_cur->gdb_putc(c);
+ cksum += c;
+ runlen--;
+ }
+ gdb_cur->gdb_putc('*');
+ cksum += '*';
+ gdb_cur->gdb_putc(runlen+29);
+ cksum += runlen+29;
+ }
+
+ gdb_cur->gdb_putc('#');
+ c = cksum >> 4;
+ gdb_cur->gdb_putc(N2C(c));
+ c = cksum & 0x0f;
+ gdb_cur->gdb_putc(N2C(c));
+
+ c = gdb_cur->gdb_getc();
+ } while (c != '+');
+
+ return (0);
+}
+
+int
+gdb_tx_mem(const unsigned char *addr, size_t size)
+{
+ void *prev;
+ jmp_buf jb;
+ int ret;
+
+ prev = kdb_jmpbuf(jb);
+ ret = setjmp(jb);
+ if (ret == 0) {
+ while (size-- > 0) {
+ *gdb_txp++ = N2C(*addr >> 4);
+ *gdb_txp++ = N2C(*addr & 0x0f);
+ addr++;
+ }
+ }
+ (void)kdb_jmpbuf(prev);
+ return ((ret == 0) ? 1 : 0);
+}
+
+void
+gdb_tx_reg(int regnum)
+{
+ unsigned char *regp;
+ size_t regsz;
+
+ regp = gdb_cpu_getreg(regnum, &regsz);
+ if (regp == NULL) {
+ /* Register unavailable. */
+ while (regsz--) {
+ gdb_tx_char('x');
+ gdb_tx_char('x');
+ }
+ } else
+ gdb_tx_mem(regp, regsz);
+}
OpenPOWER on IntegriCloud