2010-03-22 06:12:51 +00:00
|
|
|
/* MSPDebug - debugging tool for MSP430 MCUs
|
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
2010-05-13 00:57:21 +00:00
|
|
|
#include <stdint.h>
|
2011-07-26 23:43:22 +00:00
|
|
|
|
|
|
|
#include "sockets.h"
|
2010-03-22 06:12:51 +00:00
|
|
|
#include "device.h"
|
2010-03-31 06:33:28 +00:00
|
|
|
#include "util.h"
|
2010-08-13 03:14:15 +00:00
|
|
|
#include "opdb.h"
|
2010-03-22 06:12:51 +00:00
|
|
|
#include "gdb.h"
|
2010-08-16 23:07:03 +00:00
|
|
|
#include "output.h"
|
2010-08-16 23:47:47 +00:00
|
|
|
#include "reader.h"
|
2010-10-11 22:26:36 +00:00
|
|
|
#include "expr.h"
|
2011-06-06 01:50:50 +00:00
|
|
|
#include "gdb_proto.h"
|
2012-10-08 22:57:14 +00:00
|
|
|
#include "ctrlc.h"
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-03-22 06:12:51 +00:00
|
|
|
/************************************************************************
|
|
|
|
* GDB server
|
|
|
|
*/
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int read_registers(struct gdb_data *data)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-03-23 00:38:14 +00:00
|
|
|
int i;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Reading registers\n");
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_getregs(regs) < 0)
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_packet_start(data);
|
2010-03-23 00:38:14 +00:00
|
|
|
for (i = 0; i < DEVICE_NUM_REGS; i++)
|
2010-12-02 07:09:44 +00:00
|
|
|
gdb_printf(data, "%02x%02x",
|
|
|
|
regs[i] & 0xff,
|
|
|
|
(regs[i] >> 8) & 0xff);
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_packet_end(data);
|
|
|
|
return gdb_flush_ack(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-08-16 23:47:47 +00:00
|
|
|
struct monitor_buf {
|
2011-06-06 01:50:50 +00:00
|
|
|
char buf[GDB_MAX_XFER];
|
2010-08-16 23:47:47 +00:00
|
|
|
int len;
|
|
|
|
int trunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void monitor_capture(void *user_data, const char *text)
|
|
|
|
{
|
|
|
|
struct monitor_buf *mb = (struct monitor_buf *)user_data;
|
|
|
|
int len = strlen(text);
|
|
|
|
|
|
|
|
if (mb->trunc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mb->len + len + 64 > sizeof(mb->buf)) {
|
|
|
|
text = "...<truncated>";
|
|
|
|
len = strlen(text);
|
|
|
|
mb->trunc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(mb->buf + mb->len, text, len);
|
|
|
|
mb->len += len;
|
|
|
|
mb->buf[mb->len++] = '\n';
|
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int monitor_command(struct gdb_data *data, char *buf)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
|
|
|
char cmd[128];
|
|
|
|
int len = 0;
|
2010-08-16 23:47:47 +00:00
|
|
|
int i;
|
|
|
|
struct monitor_buf mbuf;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
while (len + 1 < sizeof(cmd) && *buf && buf[1]) {
|
2010-05-12 05:28:59 +00:00
|
|
|
if (len + 1 >= sizeof(cmd))
|
|
|
|
break;
|
|
|
|
|
2010-03-22 06:12:51 +00:00
|
|
|
cmd[len++] = (hexval(buf[0]) << 4) | hexval(buf[1]);
|
|
|
|
buf += 2;
|
|
|
|
}
|
2010-05-12 05:28:59 +00:00
|
|
|
cmd[len] = 0;
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Monitor command received: %s\n", cmd);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-08-16 23:47:47 +00:00
|
|
|
mbuf.len = 0;
|
|
|
|
mbuf.trunc = 0;
|
|
|
|
capture_start(monitor_capture, &mbuf);
|
|
|
|
process_command(cmd);
|
|
|
|
capture_end();
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-08-16 23:47:47 +00:00
|
|
|
if (!mbuf.len)
|
|
|
|
return gdb_send(data, "OK");
|
|
|
|
|
|
|
|
gdb_packet_start(data);
|
|
|
|
for (i = 0; i < mbuf.len; i++)
|
|
|
|
gdb_printf(data, "%02x", mbuf.buf[i]);
|
|
|
|
gdb_packet_end(data);
|
|
|
|
|
|
|
|
return gdb_flush_ack(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int write_registers(struct gdb_data *data, char *buf)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-03-22 06:12:51 +00:00
|
|
|
int i;
|
|
|
|
|
2010-03-23 00:38:14 +00:00
|
|
|
if (strlen(buf) < DEVICE_NUM_REGS * 4)
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Writing registers\n");
|
2010-03-22 06:12:51 +00:00
|
|
|
for (i = 0; i < DEVICE_NUM_REGS; i++) {
|
|
|
|
regs[i] = (hexval(buf[2]) << 12) |
|
|
|
|
(hexval(buf[3]) << 8) |
|
|
|
|
(hexval(buf[0]) << 4) |
|
|
|
|
hexval(buf[1]);
|
|
|
|
buf += 4;
|
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_setregs(regs) < 0)
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "OK");
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int read_memory(struct gdb_data *data, char *text)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
|
|
|
char *length_text = strchr(text, ',');
|
2010-08-05 04:44:41 +00:00
|
|
|
address_t length, addr;
|
2011-06-06 01:50:50 +00:00
|
|
|
uint8_t buf[GDB_MAX_XFER];
|
2010-03-23 00:38:14 +00:00
|
|
|
int i;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
if (!length_text) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: malformed memory read request\n");
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*(length_text++) = 0;
|
|
|
|
|
|
|
|
length = strtoul(length_text, NULL, 16);
|
|
|
|
addr = strtoul(text, NULL, 16);
|
|
|
|
|
|
|
|
if (length > sizeof(buf))
|
|
|
|
length = sizeof(buf);
|
|
|
|
|
2011-03-11 01:59:02 +00:00
|
|
|
printc("Reading %4d bytes from 0x%04x\n", length, addr);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(addr, buf, length) < 0)
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_packet_start(data);
|
2010-03-23 00:38:14 +00:00
|
|
|
for (i = 0; i < length; i++)
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_printf(data, "%02x", buf[i]);
|
|
|
|
gdb_packet_end(data);
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_flush_ack(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int write_memory(struct gdb_data *data, char *text)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
|
|
|
char *data_text = strchr(text, ':');
|
|
|
|
char *length_text = strchr(text, ',');
|
2010-08-05 04:44:41 +00:00
|
|
|
address_t length, addr;
|
2011-06-06 01:50:50 +00:00
|
|
|
uint8_t buf[GDB_MAX_XFER];
|
2010-03-22 06:12:51 +00:00
|
|
|
int buflen = 0;
|
|
|
|
|
|
|
|
if (!(data_text && length_text)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: malformed memory write request\n");
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*(data_text++) = 0;
|
|
|
|
*(length_text++) = 0;
|
|
|
|
|
|
|
|
length = strtoul(length_text, NULL, 16);
|
|
|
|
addr = strtoul(text, NULL, 16);
|
|
|
|
|
|
|
|
while (buflen < sizeof(buf) && *data_text && data_text[1]) {
|
|
|
|
buf[buflen++] = (hexval(data_text[0]) << 4) |
|
|
|
|
hexval(data_text[1]);
|
|
|
|
data_text += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buflen != length) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: length mismatch\n");
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 01:59:02 +00:00
|
|
|
printc("Writing %4d bytes to 0x%04x\n", length, addr);
|
2010-09-12 22:43:21 +00:00
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_writemem(addr, buf, buflen) < 0)
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "OK");
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
static int run_set_pc(char *buf)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-03-23 00:38:14 +00:00
|
|
|
if (!*buf)
|
|
|
|
return 0;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_getregs(regs) < 0)
|
2010-03-23 00:38:14 +00:00
|
|
|
return -1;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-03-23 00:38:14 +00:00
|
|
|
regs[0] = strtoul(buf, NULL, 16);
|
2011-03-15 00:12:48 +00:00
|
|
|
return device_setregs(regs);
|
2010-03-23 00:38:14 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int run_final_status(struct gdb_data *data)
|
2010-03-23 00:38:14 +00:00
|
|
|
{
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-03-23 00:38:14 +00:00
|
|
|
int i;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_getregs(regs) < 0)
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_packet_start(data);
|
2010-07-27 04:54:08 +00:00
|
|
|
gdb_printf(data, "T05");
|
2010-08-05 04:44:41 +00:00
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
address_t value = regs[i];
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* NOTE: this only gives GDB the lower 16 bits of each
|
|
|
|
* register. It complains if we give the full data.
|
|
|
|
*/
|
|
|
|
gdb_printf(data, "%02x:", i);
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
gdb_printf(data, "%02x", value & 0xff);
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
gdb_printf(data, ";");
|
|
|
|
}
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_packet_end(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_flush_ack(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int single_step(struct gdb_data *data, char *buf)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Single stepping\n");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
if (run_set_pc(buf) < 0 ||
|
2011-03-15 00:12:48 +00:00
|
|
|
device_ctl(DEVICE_CTL_STEP) < 0)
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return run_final_status(data);
|
2010-03-23 00:38:14 +00:00
|
|
|
}
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static int run(struct gdb_data *data, char *buf)
|
2010-03-23 00:38:14 +00:00
|
|
|
{
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Running\n");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
if (run_set_pc(buf) < 0 ||
|
2011-03-15 00:12:48 +00:00
|
|
|
device_ctl(DEVICE_CTL_RUN) < 0)
|
2010-05-12 05:43:30 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2011-03-15 00:12:48 +00:00
|
|
|
device_status_t status = device_poll();
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-05-12 05:43:30 +00:00
|
|
|
if (status == DEVICE_STATUS_ERROR)
|
|
|
|
return gdb_send(data, "E00");
|
2010-03-23 00:38:14 +00:00
|
|
|
|
2010-03-23 01:37:53 +00:00
|
|
|
if (status == DEVICE_STATUS_HALTED) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Target halted\n");
|
2010-03-22 06:12:51 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-03-23 01:37:53 +00:00
|
|
|
if (status == DEVICE_STATUS_INTR)
|
|
|
|
goto out;
|
|
|
|
|
2011-06-06 03:47:52 +00:00
|
|
|
while (gdb_peek(data, 0)) {
|
2010-04-30 04:01:03 +00:00
|
|
|
int c = gdb_getc(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
if (c < 0)
|
2010-03-23 00:38:14 +00:00
|
|
|
return -1;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
if (c == 3) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Interrupted by gdb\n");
|
2010-03-22 06:12:51 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_ctl(DEVICE_CTL_HALT) < 0)
|
2010-05-12 05:43:30 +00:00
|
|
|
return gdb_send(data, "E00");
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return run_final_status(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-05-12 06:17:12 +00:00
|
|
|
static int set_breakpoint(struct gdb_data *data, int enable, char *buf)
|
|
|
|
{
|
|
|
|
char *parts[2];
|
2010-08-05 04:44:41 +00:00
|
|
|
address_t addr;
|
2012-04-25 01:31:18 +00:00
|
|
|
device_bptype_t type;
|
2010-05-12 06:17:12 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Break up the arguments */
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
parts[i] = strsep(&buf, ",");
|
|
|
|
|
|
|
|
/* Make sure there's a type argument */
|
|
|
|
if (!parts[0]) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: breakpoint requested with no type\n");
|
2010-05-12 06:17:12 +00:00
|
|
|
return gdb_send(data, "E00");
|
|
|
|
}
|
|
|
|
|
2012-04-25 01:31:18 +00:00
|
|
|
switch (atoi(parts[0])) {
|
2012-04-29 20:52:12 +00:00
|
|
|
case 0:
|
2012-04-25 01:31:18 +00:00
|
|
|
case 1:
|
|
|
|
type = DEVICE_BPTYPE_BREAK;
|
|
|
|
break;
|
|
|
|
|
2012-04-25 02:10:26 +00:00
|
|
|
case 2:
|
|
|
|
type = DEVICE_BPTYPE_WRITE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
type = DEVICE_BPTYPE_READ;
|
|
|
|
break;
|
|
|
|
|
2012-04-25 01:31:18 +00:00
|
|
|
case 4:
|
|
|
|
type = DEVICE_BPTYPE_WATCH;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: unsupported breakpoint type: %s\n",
|
2010-05-12 06:17:12 +00:00
|
|
|
parts[0]);
|
|
|
|
return gdb_send(data, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There needs to be an address specified */
|
|
|
|
if (!parts[1]) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: breakpoint address missing\n");
|
2010-05-12 06:17:12 +00:00
|
|
|
return gdb_send(data, "E00");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the breakpoint address */
|
|
|
|
addr = strtoul(parts[1], NULL, 16);
|
|
|
|
|
2010-07-02 02:22:52 +00:00
|
|
|
if (enable) {
|
2012-04-25 01:31:18 +00:00
|
|
|
if (device_setbrk(device_default, -1, 1, addr, type) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: can't add breakpoint at "
|
2010-07-02 02:22:52 +00:00
|
|
|
"0x%04x\n", addr);
|
|
|
|
return gdb_send(data, "E00");
|
|
|
|
}
|
2010-05-12 06:17:12 +00:00
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Breakpoint set at 0x%04x\n", addr);
|
2010-07-02 02:22:52 +00:00
|
|
|
} else {
|
2012-04-25 01:31:18 +00:00
|
|
|
device_setbrk(device_default, -1, 0, addr, type);
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Breakpoint cleared at 0x%04x\n", addr);
|
2010-07-02 02:22:52 +00:00
|
|
|
}
|
2010-05-12 06:17:12 +00:00
|
|
|
|
|
|
|
return gdb_send(data, "OK");
|
|
|
|
}
|
|
|
|
|
2011-06-06 03:53:06 +00:00
|
|
|
static int restart_program(struct gdb_data *data)
|
|
|
|
{
|
|
|
|
if (device_ctl(DEVICE_CTL_RESET) < 0)
|
|
|
|
return gdb_send(data, "E00");
|
|
|
|
|
|
|
|
return gdb_send(data, "OK");
|
|
|
|
}
|
|
|
|
|
2011-11-27 21:29:27 +00:00
|
|
|
static int gdb_send_empty_threadlist(struct gdb_data *data)
|
|
|
|
{
|
|
|
|
return gdb_send(data, "<?xml version=\"1.0\"?><threads></threads>");
|
|
|
|
}
|
|
|
|
|
2010-08-15 23:13:39 +00:00
|
|
|
static int gdb_send_supported(struct gdb_data *data)
|
|
|
|
{
|
|
|
|
gdb_packet_start(data);
|
2011-06-06 01:50:50 +00:00
|
|
|
gdb_printf(data, "PacketSize=%x", GDB_MAX_XFER * 2);
|
2010-08-15 23:13:39 +00:00
|
|
|
gdb_packet_end(data);
|
|
|
|
return gdb_flush_ack(data);
|
|
|
|
}
|
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
static int process_gdb_command(struct gdb_data *data, char *buf)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
2011-03-20 22:21:40 +00:00
|
|
|
#ifdef DEBUG_GDB
|
|
|
|
printc("process_gdb_command: %s\n", buf);
|
|
|
|
#endif
|
2010-03-22 06:12:51 +00:00
|
|
|
switch (buf[0]) {
|
|
|
|
case '?': /* Return target halt reason */
|
2010-07-27 04:54:08 +00:00
|
|
|
return run_final_status(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-05-12 06:17:12 +00:00
|
|
|
case 'z':
|
|
|
|
case 'Z':
|
|
|
|
return set_breakpoint(data, buf[0] == 'Z', buf + 1);
|
|
|
|
|
2011-06-06 03:53:06 +00:00
|
|
|
case 'r': /* Restart */
|
|
|
|
case 'R':
|
|
|
|
return restart_program(data);
|
|
|
|
|
2010-03-22 06:12:51 +00:00
|
|
|
case 'g': /* Read registers */
|
2010-04-30 04:01:03 +00:00
|
|
|
return read_registers(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
case 'G': /* Write registers */
|
2010-04-30 04:01:03 +00:00
|
|
|
return write_registers(data, buf + 1);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
case 'q': /* Query */
|
|
|
|
if (!strncmp(buf, "qRcmd,", 6))
|
2010-04-30 04:01:03 +00:00
|
|
|
return monitor_command(data, buf + 6);
|
2010-08-15 23:13:39 +00:00
|
|
|
if (!strncmp(buf, "qSupported", 10))
|
|
|
|
return gdb_send_supported(data);
|
2011-11-27 21:29:27 +00:00
|
|
|
if (!strncmp(buf, "qfThreadInfo", 12))
|
|
|
|
return gdb_send_empty_threadlist(data);
|
2010-03-22 06:12:51 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm': /* Read memory */
|
2010-04-30 04:01:03 +00:00
|
|
|
return read_memory(data, buf + 1);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
case 'M': /* Write memory */
|
2010-04-30 04:01:03 +00:00
|
|
|
return write_memory(data, buf + 1);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
case 'c': /* Continue */
|
2010-04-30 04:01:03 +00:00
|
|
|
return run(data, buf + 1);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
case 's': /* Single step */
|
2010-04-30 04:01:03 +00:00
|
|
|
return single_step(data, buf + 1);
|
2011-03-20 22:21:40 +00:00
|
|
|
case 'k': /* kill */
|
|
|
|
return -1;
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 22:21:40 +00:00
|
|
|
#ifdef DEBUG_GDB
|
|
|
|
printc("process_gdb_command: unknown command %s\n", buf);
|
|
|
|
#endif
|
|
|
|
|
2010-03-22 06:12:51 +00:00
|
|
|
/* For unknown/unsupported packets, return an empty reply */
|
2010-04-30 04:01:03 +00:00
|
|
|
return gdb_send(data, "");
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
static void gdb_reader_loop(struct gdb_data *data)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
2012-10-08 22:57:14 +00:00
|
|
|
while (!ctrlc_check()) {
|
2011-06-06 01:50:50 +00:00
|
|
|
char buf[GDB_BUF_SIZE];
|
2010-03-22 06:12:51 +00:00
|
|
|
int len = 0;
|
|
|
|
|
2011-06-06 01:50:50 +00:00
|
|
|
len = gdb_read_packet(data, buf);
|
|
|
|
if (len < 0)
|
2010-03-22 06:12:51 +00:00
|
|
|
return;
|
2011-11-14 18:38:19 +00:00
|
|
|
if (len && process_gdb_command(data, buf) < 0)
|
2010-03-22 06:12:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:25:28 +00:00
|
|
|
static int gdb_server(int port)
|
2010-03-22 06:12:51 +00:00
|
|
|
{
|
|
|
|
int sock;
|
|
|
|
int client;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t len;
|
2010-03-23 00:42:44 +00:00
|
|
|
int arg;
|
2010-04-30 04:01:03 +00:00
|
|
|
struct gdb_data data;
|
2010-07-02 02:22:52 +00:00
|
|
|
int i;
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2011-07-26 16:38:54 +00:00
|
|
|
if (SOCKET_ISERR(sock)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("gdb: can't create socket");
|
2010-03-22 06:12:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-03-23 00:42:44 +00:00
|
|
|
arg = 1;
|
2011-07-26 13:21:44 +00:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(void *)&arg, sizeof(arg)) < 0)
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("gdb: warning: can't reuse socket address");
|
2010-03-23 00:42:44 +00:00
|
|
|
|
2010-03-22 06:12:51 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: can't bind to port %d: %s\n",
|
2011-07-26 13:51:06 +00:00
|
|
|
port, last_error());
|
2011-07-26 23:43:22 +00:00
|
|
|
closesocket(sock);
|
2010-03-22 06:12:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(sock, 1) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("gdb: can't listen on socket");
|
2011-07-26 23:43:22 +00:00
|
|
|
closesocket(sock);
|
2010-03-22 06:12:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Bound to port %d. Now waiting for connection...\n", port);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
|
|
|
len = sizeof(addr);
|
2011-07-26 16:38:54 +00:00
|
|
|
client = sockets_accept(sock, (struct sockaddr *)&addr, &len);
|
|
|
|
if (SOCKET_ISERR(client)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("gdb: failed to accept connection");
|
2011-07-26 23:43:22 +00:00
|
|
|
closesocket(sock);
|
2010-03-22 06:12:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-26 23:43:22 +00:00
|
|
|
closesocket(sock);
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Client connected from %s:%d\n",
|
2010-03-22 06:12:51 +00:00
|
|
|
inet_ntoa(addr.sin_addr), htons(addr.sin_port));
|
|
|
|
|
2011-06-06 01:50:50 +00:00
|
|
|
gdb_init(&data, client);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-05-12 06:17:12 +00:00
|
|
|
/* Put the hardware breakpoint setting into a known state. */
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Clearing all breakpoints...\n");
|
2010-08-13 00:25:28 +00:00
|
|
|
for (i = 0; i < device_default->max_breakpoints; i++)
|
2012-04-25 00:43:14 +00:00
|
|
|
device_setbrk(device_default, i, 0, 0, 0);
|
2010-05-12 06:17:12 +00:00
|
|
|
|
2011-03-20 22:21:40 +00:00
|
|
|
#ifdef DEBUG_GDB
|
|
|
|
printc("starting GDB reader loop...\n");
|
|
|
|
#endif
|
2010-04-30 04:01:03 +00:00
|
|
|
gdb_reader_loop(&data);
|
2011-03-20 22:21:40 +00:00
|
|
|
#ifdef DEBUG_GDB
|
|
|
|
printc("... reader loop returned\n");
|
|
|
|
#endif
|
2011-07-26 23:43:22 +00:00
|
|
|
closesocket(client);
|
2010-03-22 06:12:51 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
return data.error ? -1 : 0;
|
2010-03-22 06:12:51 +00:00
|
|
|
}
|
2010-03-31 06:33:28 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_gdb(char **arg)
|
2010-03-31 06:33:28 +00:00
|
|
|
{
|
|
|
|
char *port_text = get_arg(arg);
|
2010-10-11 22:26:36 +00:00
|
|
|
address_t port = 2000;
|
2010-03-31 06:33:28 +00:00
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (port_text && expr_eval(port_text, &port) < 0) {
|
2010-10-11 22:26:36 +00:00
|
|
|
printc_err("gdb: can't parse port: %s\n", port_text);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-31 06:33:28 +00:00
|
|
|
|
|
|
|
if (port <= 0 || port > 65535) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("gdb: invalid port: %d\n", port);
|
2010-03-31 06:33:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-06-15 02:51:12 +00:00
|
|
|
do {
|
2010-08-13 00:25:28 +00:00
|
|
|
if (gdb_server(port) < 0)
|
2010-06-15 02:51:12 +00:00
|
|
|
return -1;
|
2010-08-13 03:14:15 +00:00
|
|
|
} while (opdb_get_boolean("gdb_loop"));
|
2010-06-15 02:51:12 +00:00
|
|
|
|
|
|
|
return 0;
|
2010-03-31 06:33:28 +00:00
|
|
|
}
|