Create default device to avoid carrying around device_t.

This commit is contained in:
Daniel Beer 2010-08-13 12:25:28 +12:00
parent 4cc81497af
commit 0120b146ae
8 changed files with 73 additions and 108 deletions

12
cproc.c
View File

@ -41,8 +41,6 @@ struct cproc {
int modify_flags;
int in_reader_loop;
device_t device;
};
static struct cproc_option *find_option(cproc_t cp, const char *name)
@ -322,7 +320,7 @@ static const struct cproc_option built_in_options[] = {
}
};
cproc_t cproc_new(device_t dev)
cproc_t cproc_new(void)
{
cproc_t cp = malloc(sizeof(*cp));
@ -331,8 +329,6 @@ cproc_t cproc_new(device_t dev)
memset(cp, 0, sizeof(*cp));
cp->device = dev;
vector_init(&cp->command_list, sizeof(struct cproc_command));
vector_init(&cp->option_list, sizeof(struct cproc_option));
@ -351,17 +347,11 @@ cproc_t cproc_new(device_t dev)
void cproc_destroy(cproc_t cp)
{
cp->device->destroy(cp->device);
vector_destroy(&cp->command_list);
vector_destroy(&cp->option_list);
free(cp);
}
device_t cproc_device(cproc_t cp)
{
return cp->device;
}
int cproc_register_commands(cproc_t cp, const struct cproc_command *cmd,
int count)
{

View File

@ -19,8 +19,6 @@
#ifndef CPROC_H_
#define CPROC_H_
#include "device.h"
/* Command processor.
*
* This contains a list of all defined commands and options, plus modification
@ -89,12 +87,9 @@ struct cproc_option {
* has been given. When you destroy a command processor, the device is
* also destroyed.
*/
cproc_t cproc_new(device_t dev);
cproc_t cproc_new(void);
void cproc_destroy(cproc_t cp);
/* Fetch the command processor's device and symbol table */
device_t cproc_device(cproc_t cp);
/* Register commands and options with the command processor. These functions
* return 0 on success or -1 if an error occurs (failure to allocate memory).
*/

101
devcmd.c
View File

@ -33,19 +33,18 @@
static int cmd_regs(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
address_t regs[DEVICE_NUM_REGS];
uint8_t code[16];
int len = sizeof(code);
if (dev->getregs(dev, regs) < 0)
if (device_default->getregs(device_default, regs) < 0)
return -1;
cproc_regs(cp, regs);
/* Try to disassemble the instruction at PC */
if (len > 0x10000 - regs[0])
len = 0x10000 - regs[0];
if (dev->readmem(dev, regs[0], code, len) < 0)
if (device_default->readmem(device_default, regs[0], code, len) < 0)
return 0;
cproc_disassemble(cp, regs[0], (uint8_t *)code, len);
@ -54,7 +53,6 @@ static int cmd_regs(cproc_t cp, char **arg)
static int cmd_md(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *off_text = get_arg(arg);
char *len_text = get_arg(arg);
address_t offset = 0;
@ -84,7 +82,8 @@ static int cmd_md(cproc_t cp, char **arg)
uint8_t buf[128];
int blen = length > sizeof(buf) ? sizeof(buf) : length;
if (dev->readmem(dev, offset, buf, blen) < 0)
if (device_default->readmem(device_default,
offset, buf, blen) < 0)
return -1;
cproc_hexdump(cp, offset, buf, blen);
@ -97,7 +96,6 @@ static int cmd_md(cproc_t cp, char **arg)
static int cmd_mw(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *off_text = get_arg(arg);
char *byte_text;
address_t offset = 0;
@ -126,7 +124,7 @@ static int cmd_mw(cproc_t cp, char **arg)
if (!length)
return 0;
if (dev->writemem(dev, offset, buf, length) < 0)
if (device_default->writemem(device_default, offset, buf, length) < 0)
return -1;
return 0;
@ -134,25 +132,20 @@ static int cmd_mw(cproc_t cp, char **arg)
static int cmd_reset(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
return dev->ctl(dev, DEVICE_CTL_RESET);
return device_default->ctl(device_default, DEVICE_CTL_RESET);
}
static int cmd_erase(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
if (dev->ctl(dev, DEVICE_CTL_HALT) < 0)
if (device_default->ctl(device_default, DEVICE_CTL_HALT) < 0)
return -1;
printf("Erasing...\n");
return dev->ctl(dev, DEVICE_CTL_ERASE);
return device_default->ctl(device_default, DEVICE_CTL_ERASE);
}
static int cmd_step(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *count_text = get_arg(arg);
int count = 1;
@ -160,7 +153,7 @@ static int cmd_step(cproc_t cp, char **arg)
count = atoi(count_text);
while (count > 0) {
if (dev->ctl(dev, DEVICE_CTL_STEP) < 0)
if (device_default->ctl(device_default, DEVICE_CTL_STEP) < 0)
return -1;
count--;
}
@ -170,31 +163,31 @@ static int cmd_step(cproc_t cp, char **arg)
static int cmd_run(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
device_status_t status;
address_t regs[DEVICE_NUM_REGS];
if (dev->getregs(dev, regs) < 0) {
if (device_default->getregs(device_default, regs) < 0) {
fprintf(stderr, "warning: device: can't fetch registers\n");
} else {
int i;
for (i = 0; i < dev->max_breakpoints; i++) {
struct device_breakpoint *bp = &dev->breakpoints[i];
for (i = 0; i < device_default->max_breakpoints; i++) {
struct device_breakpoint *bp =
&device_default->breakpoints[i];
if ((bp->flags & DEVICE_BP_ENABLED) &&
bp->addr == regs[0])
break;
}
if (i < dev->max_breakpoints) {
if (i < device_default->max_breakpoints) {
printf("Stepping over breakpoint #%d at 0x%04x\n",
i, regs[0]);
dev->ctl(dev, DEVICE_CTL_STEP);
device_default->ctl(device_default, DEVICE_CTL_STEP);
}
}
if (dev->ctl(dev, DEVICE_CTL_RUN) < 0) {
if (device_default->ctl(device_default, DEVICE_CTL_RUN) < 0) {
fprintf(stderr, "run: failed to start CPU\n");
return -1;
}
@ -202,7 +195,7 @@ static int cmd_run(cproc_t cp, char **arg)
printf("Running. Press Ctrl+C to interrupt...\n");
do {
status = dev->poll(dev);
status = device_default->poll(device_default);
} while (status == DEVICE_STATUS_RUNNING);
if (status == DEVICE_STATUS_INTR)
@ -211,7 +204,7 @@ static int cmd_run(cproc_t cp, char **arg)
if (status == DEVICE_STATUS_ERROR)
return -1;
if (dev->ctl(dev, DEVICE_CTL_HALT) < 0)
if (device_default->ctl(device_default, DEVICE_CTL_HALT) < 0)
return -1;
return cmd_regs(cp, NULL);
@ -219,7 +212,6 @@ static int cmd_run(cproc_t cp, char **arg)
static int cmd_set(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *reg_text = get_arg(arg);
char *val_text = get_arg(arg);
int reg;
@ -242,10 +234,10 @@ static int cmd_set(cproc_t cp, char **arg)
return -1;
}
if (dev->getregs(dev, regs) < 0)
if (device_default->getregs(device_default, regs) < 0)
return -1;
regs[reg] = value;
if (dev->setregs(dev, regs) < 0)
if (device_default->setregs(device_default, regs) < 0)
return -1;
cproc_regs(cp, regs);
@ -254,7 +246,6 @@ static int cmd_set(cproc_t cp, char **arg)
static int cmd_dis(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *off_text = get_arg(arg);
char *len_text = get_arg(arg);
address_t offset = 0;
@ -287,7 +278,8 @@ static int cmd_dis(cproc_t cp, char **arg)
return -1;
}
if (dev->readmem(dev, offset, buf, length) < 0) {
if (device_default->readmem(device_default,
offset, buf, length) < 0) {
free(buf);
return -1;
}
@ -403,7 +395,6 @@ static int hexout_feed(struct hexout_data *hexout,
static int cmd_hexout(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *off_text = get_arg(arg);
char *len_text = get_arg(arg);
char *filename = *arg;
@ -431,7 +422,8 @@ static int cmd_hexout(cproc_t cp, char **arg)
count = sizeof(buf);
printf("Reading %d bytes from 0x%04x...\n", count, off);
if (dev->readmem(dev, off, buf, count) < 0) {
if (device_default->readmem(device_default,
off, buf, count) < 0) {
perror("hexout: can't read memory");
goto fail;
}
@ -459,21 +451,12 @@ fail:
}
struct prog_data {
device_t dev;
uint8_t buf[128];
address_t addr;
int len;
int have_erased;
};
static void prog_init(struct prog_data *prog, device_t dev)
{
prog->dev = dev;
prog->len = 0;
prog->have_erased = 0;
}
static int prog_flush(struct prog_data *prog)
{
while (prog->len) {
@ -485,14 +468,15 @@ static int prog_flush(struct prog_data *prog)
if (!prog->have_erased) {
printf("Erasing...\n");
if (prog->dev->ctl(prog->dev, DEVICE_CTL_ERASE) < 0)
if (device_default->ctl(device_default,
DEVICE_CTL_ERASE) < 0)
return -1;
prog->have_erased = 1;
}
printf("Writing %3d bytes to %04x...\n", wlen, prog->addr);
if (prog->dev->writemem(prog->dev, prog->addr,
prog->buf, wlen) < 0)
if (device_default->writemem(device_default, prog->addr,
prog->buf, wlen) < 0)
return -1;
memmove(prog->buf, prog->buf + wlen, prog->len - wlen);
@ -541,7 +525,6 @@ static int prog_feed(void *user_data,
static int cmd_prog(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
FILE *in;
struct prog_data prog;
@ -554,12 +537,12 @@ static int cmd_prog(cproc_t cp, char **arg)
return -1;
}
if (dev->ctl(dev, DEVICE_CTL_HALT) < 0) {
if (device_default->ctl(device_default, DEVICE_CTL_HALT) < 0) {
fclose(in);
return -1;
}
prog_init(&prog, dev);
memset(&prog, 0, sizeof(prog));
if (binfile_extract(in, prog_feed, &prog) < 0) {
fclose(in);
@ -576,7 +559,7 @@ static int cmd_prog(cproc_t cp, char **arg)
if (prog_flush(&prog) < 0)
return -1;
if (dev->ctl(dev, DEVICE_CTL_RESET) < 0) {
if (device_default->ctl(device_default, DEVICE_CTL_RESET) < 0) {
fprintf(stderr, "prog: failed to reset after programming\n");
return -1;
}
@ -587,7 +570,6 @@ static int cmd_prog(cproc_t cp, char **arg)
static int cmd_setbreak(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *addr_text = get_arg(arg);
char *index_text = get_arg(arg);
int index = -1;
@ -606,14 +588,14 @@ static int cmd_setbreak(cproc_t cp, char **arg)
if (index_text) {
index = atoi(index_text);
if (index < 0 || index >= dev->max_breakpoints) {
if (index < 0 || index >= device_default->max_breakpoints) {
fprintf(stderr, "setbreak: invalid breakpoint "
"slot: %d\n", index);
return -1;
}
}
index = device_setbrk(dev, index, 1, addr);
index = device_setbrk(device_default, index, 1, addr);
if (index < 0) {
fprintf(stderr, "setbreak: all breakpoint slots are "
"occupied\n");
@ -626,27 +608,26 @@ static int cmd_setbreak(cproc_t cp, char **arg)
static int cmd_delbreak(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *index_text = get_arg(arg);
int ret = 0;
if (index_text) {
int index = atoi(index_text);
if (index < 0 || index >= dev->max_breakpoints) {
if (index < 0 || index >= device_default->max_breakpoints) {
fprintf(stderr, "delbreak: invalid breakpoint "
"slot: %d\n", index);
return -1;
}
printf("Clearing breakpoint %d\n", index);
device_setbrk(dev, index, 0, 0);
device_setbrk(device_default, index, 0, 0);
} else {
int i;
printf("Clearing all breakpoints...\n");
for (i = 0; i < dev->max_breakpoints; i++)
device_setbrk(dev, i, 0, 0);
for (i = 0; i < device_default->max_breakpoints; i++)
device_setbrk(device_default, i, 0, 0);
}
return ret;
@ -654,12 +635,12 @@ static int cmd_delbreak(cproc_t cp, char **arg)
static int cmd_break(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
int i;
printf("%d breakpoints available:\n", dev->max_breakpoints);
for (i = 0; i < dev->max_breakpoints; i++) {
const struct device_breakpoint *bp = &dev->breakpoints[i];
printf("%d breakpoints available:\n", device_default->max_breakpoints);
for (i = 0; i < device_default->max_breakpoints; i++) {
const struct device_breakpoint *bp =
&device_default->breakpoints[i];
if (bp->flags & DEVICE_BP_ENABLED) {
char name[128];

View File

@ -18,6 +18,8 @@
#include "device.h"
device_t device_default;
static int addbrk(device_t dev, uint16_t addr)
{
int i;

View File

@ -25,6 +25,8 @@
struct device;
typedef struct device *device_t;
extern device_t device_default;
typedef enum {
DEVICE_CTL_RESET,
DEVICE_CTL_RUN,

41
gdb.c
View File

@ -47,8 +47,6 @@ struct gdb_data {
char outbuf[MAX_MEM_XFER * 2 + 64];
int outlen;
device_t device;
};
static void gdb_printf(struct gdb_data *data, const char *fmt, ...)
@ -217,7 +215,7 @@ static int read_registers(struct gdb_data *data)
int i;
printf("Reading registers\n");
if (data->device->getregs(data->device, regs) < 0)
if (device_default->getregs(device_default, regs) < 0)
return gdb_send(data, "E00");
gdb_packet_start(data);
@ -245,11 +243,11 @@ static int monitor_command(struct gdb_data *data, char *buf)
if (!strcasecmp(cmd, "reset")) {
printf("Resetting device\n");
if (data->device->ctl(data->device, DEVICE_CTL_RESET) < 0)
if (device_default->ctl(device_default, DEVICE_CTL_RESET) < 0)
return gdb_send_hex(data, "Reset failed\n");
} else if (!strcasecmp(cmd, "erase")) {
printf("Erasing device\n");
if (data->device->ctl(data->device, DEVICE_CTL_ERASE) < 0)
if (device_default->ctl(device_default, DEVICE_CTL_ERASE) < 0)
return gdb_send_hex(data, "Erase failed\n");
}
@ -273,7 +271,7 @@ static int write_registers(struct gdb_data *data, char *buf)
buf += 4;
}
if (data->device->setregs(data->device, regs) < 0)
if (device_default->setregs(device_default, regs) < 0)
return gdb_send(data, "E00");
return gdb_send(data, "OK");
@ -301,7 +299,7 @@ static int read_memory(struct gdb_data *data, char *text)
printf("Reading %d bytes from 0x%04x\n", length, addr);
if (data->device->readmem(data->device, addr, buf, length) < 0)
if (device_default->readmem(device_default, addr, buf, length) < 0)
return gdb_send(data, "E00");
gdb_packet_start(data);
@ -344,7 +342,7 @@ static int write_memory(struct gdb_data *data, char *text)
printf("Writing %d bytes to 0x%04x\n", buflen, addr);
if (data->device->writemem(data->device, addr, buf, buflen) < 0)
if (device_default->writemem(device_default, addr, buf, buflen) < 0)
return gdb_send(data, "E00");
return gdb_send(data, "OK");
@ -357,11 +355,11 @@ static int run_set_pc(struct gdb_data *data, char *buf)
if (!*buf)
return 0;
if (data->device->getregs(data->device, regs) < 0)
if (device_default->getregs(device_default, regs) < 0)
return -1;
regs[0] = strtoul(buf, NULL, 16);
return data->device->setregs(data->device, regs);
return device_default->setregs(device_default, regs);
}
static int run_final_status(struct gdb_data *data)
@ -369,7 +367,7 @@ static int run_final_status(struct gdb_data *data)
address_t regs[DEVICE_NUM_REGS];
int i;
if (data->device->getregs(data->device, regs) < 0)
if (device_default->getregs(device_default, regs) < 0)
return gdb_send(data, "E00");
gdb_packet_start(data);
@ -398,7 +396,7 @@ static int single_step(struct gdb_data *data, char *buf)
printf("Single stepping\n");
if (run_set_pc(data, buf) < 0 ||
data->device->ctl(data->device, DEVICE_CTL_STEP) < 0)
device_default->ctl(device_default, DEVICE_CTL_STEP) < 0)
gdb_send(data, "E00");
return run_final_status(data);
@ -409,11 +407,11 @@ static int run(struct gdb_data *data, char *buf)
printf("Running\n");
if (run_set_pc(data, buf) < 0 ||
data->device->ctl(data->device, DEVICE_CTL_RUN) < 0)
device_default->ctl(device_default, DEVICE_CTL_RUN) < 0)
return gdb_send(data, "E00");
for (;;) {
device_status_t status = data->device->poll(data->device);
device_status_t status = device_default->poll(device_default);
if (status == DEVICE_STATUS_ERROR)
return gdb_send(data, "E00");
@ -440,7 +438,7 @@ static int run(struct gdb_data *data, char *buf)
}
out:
if (data->device->ctl(data->device, DEVICE_CTL_HALT) < 0)
if (device_default->ctl(device_default, DEVICE_CTL_HALT) < 0)
return gdb_send(data, "E00");
return run_final_status(data);
@ -481,7 +479,7 @@ static int set_breakpoint(struct gdb_data *data, int enable, char *buf)
addr = strtoul(parts[1], NULL, 16);
if (enable) {
if (device_setbrk(data->device, -1, 1, addr) < 0) {
if (device_setbrk(device_default, -1, 1, addr) < 0) {
fprintf(stderr, "gdb: can't add breakpoint at "
"0x%04x\n", addr);
return gdb_send(data, "E00");
@ -489,7 +487,7 @@ static int set_breakpoint(struct gdb_data *data, int enable, char *buf)
printf("Breakpoint set at 0x%04x\n", addr);
} else {
device_setbrk(data->device, -1, 0, addr);
device_setbrk(device_default, -1, 0, addr);
printf("Breakpoint cleared at 0x%04x\n", addr);
}
@ -597,7 +595,7 @@ static void gdb_reader_loop(struct gdb_data *data)
}
}
static int gdb_server(device_t device, int port)
static int gdb_server(int port)
{
int sock;
int client;
@ -652,12 +650,11 @@ static int gdb_server(device_t device, int port)
data.head = 0;
data.tail = 0;
data.outlen = 0;
data.device = device;
/* Put the hardware breakpoint setting into a known state. */
printf("Clearing all breakpoints...\n");
for (i = 0; i < device->max_breakpoints; i++)
device_setbrk(device, i, 0, 0);
for (i = 0; i < device_default->max_breakpoints; i++)
device_setbrk(device_default, i, 0, 0);
gdb_reader_loop(&data);
@ -681,7 +678,7 @@ static int cmd_gdb(cproc_t cp, char **arg)
}
do {
if (gdb_server(cproc_device(cp), port) < 0)
if (gdb_server(port) < 0)
return -1;
} while (want_loop);

10
main.c
View File

@ -407,7 +407,6 @@ static int parse_cmdline_args(int argc, char **argv,
cproc_t setup_cproc(struct cmdline_args *args)
{
int i;
device_t msp430_dev;
cproc_t cp;
i = 0;
@ -424,15 +423,15 @@ cproc_t setup_cproc(struct cmdline_args *args)
if (!stab_default)
return NULL;
msp430_dev = driver_table[i].func(args);
if (!msp430_dev) {
device_default = driver_table[i].func(args);
if (!device_default) {
stab_destroy(stab_default);
return NULL;
}
cp = cproc_new(msp430_dev);
cp = cproc_new();
if (!cp) {
msp430_dev->destroy(msp430_dev);
device_default->destroy(device_default);
stab_destroy(stab_default);
return NULL;
}
@ -488,6 +487,7 @@ int main(int argc, char **argv)
cproc_destroy(cp);
stab_destroy(stab_default);
device_default->destroy(device_default);
return ret;
}

View File

@ -344,7 +344,6 @@ static int do_isearch(cproc_t cp, address_t addr, address_t len,
const struct isearch_query *q)
{
uint8_t *mbuf;
device_t dev = cproc_device(cp);
address_t i;
mbuf = malloc(len);
@ -354,7 +353,7 @@ static int do_isearch(cproc_t cp, address_t addr, address_t len,
return -1;
}
if (dev->readmem(dev, addr, mbuf, len) < 0) {
if (device_default->readmem(device_default, addr, mbuf, len) < 0) {
fprintf(stderr, "isearch: couldn't read device memory\n");
free(mbuf);
return -1;
@ -903,7 +902,6 @@ static void cgraph_func_info(struct call_graph *graph, cproc_t cp,
static int cmd_cgraph(cproc_t cp, char **arg)
{
device_t dev = cproc_device(cp);
char *offset_text, *len_text, *addr_text;;
address_t offset, len, addr;
uint8_t *memory;
@ -945,7 +943,7 @@ static int cmd_cgraph(cproc_t cp, char **arg)
return -1;
}
if (dev->readmem(dev, offset, memory, len) < 0) {
if (device_default->readmem(device_default, offset, memory, len) < 0) {
fprintf(stderr, "cgraph: couldn't fetch memory\n");
free(memory);
return -1;