Commands now support 20-bit memory range.

This commit is contained in:
Daniel Beer 2010-08-05 16:38:05 +12:00
parent 9ecf177655
commit b9356d2cad
5 changed files with 57 additions and 59 deletions

View File

@ -231,7 +231,7 @@ static void display_option(const struct cproc_option *o)
break; break;
case CPROC_OPTION_NUMERIC: case CPROC_OPTION_NUMERIC:
printf("0x%x (%d)", o->data.numeric, printf("0x%x (%u)", o->data.numeric,
o->data.numeric); o->data.numeric);
break; break;

View File

@ -214,7 +214,7 @@ void cproc_disassemble(cproc_t cp,
count = length; count = length;
len += snprintf(buf + len, sizeof(buf) - len, len += snprintf(buf + len, sizeof(buf) - len,
" \x1b[36m%04x\x1b[0m:", offset); " \x1b[36m%05x\x1b[0m:", offset);
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
len += snprintf(buf + len, sizeof(buf) - len, len += snprintf(buf + len, sizeof(buf) - len,
@ -250,7 +250,7 @@ void cproc_hexdump(cproc_t cp, uint16_t addr, const uint8_t *data, int data_len)
/* Address label */ /* Address label */
len += snprintf(buf + len, sizeof(buf) - len, len += snprintf(buf + len, sizeof(buf) - len,
" \x1b[36m%04x:\x1b[0m", offset + addr); " \x1b[36m%05x:\x1b[0m", offset + addr);
/* Hex portion */ /* Hex portion */
for (i = 0; i < 16 && offset + i < data_len; i++) for (i = 0; i < 16 && offset + i < data_len; i++)
@ -295,7 +295,7 @@ void cproc_regs(cproc_t cp, const address_t *regs)
int k = j * 4 + i; int k = j * 4 + i;
len += snprintf(buf + len, sizeof(buf) - len, len += snprintf(buf + len, sizeof(buf) - len,
"(\x1b[1m%3s:\x1b[0m %04x) ", "(\x1b[1m%3s:\x1b[0m %05x) ",
dis_reg_name(k), regs[k]); dis_reg_name(k), regs[k]);
} }

View File

@ -81,11 +81,6 @@ static int cmd_md(cproc_t cp, char **arg)
length = 0x10000 - offset; length = 0x10000 - offset;
} }
if (offset < 0 || length <= 0 || (offset + length) > 0x10000) {
fprintf(stderr, "md: memory out of range\n");
return -1;
}
while (length) { while (length) {
uint8_t buf[128]; uint8_t buf[128];
int blen = length > sizeof(buf) ? sizeof(buf) : length; int blen = length > sizeof(buf) ? sizeof(buf) : length;
@ -133,11 +128,6 @@ static int cmd_mw(cproc_t cp, char **arg)
if (!length) if (!length)
return 0; return 0;
if (offset < 0 || (offset + length) > 0x10000) {
fprintf(stderr, "md: memory out of range\n");
return -1;
}
if (dev->writemem(dev, offset, buf, length) < 0) if (dev->writemem(dev, offset, buf, length) < 0)
return -1; return -1;
@ -295,12 +285,6 @@ static int cmd_dis(cproc_t cp, char **arg)
length = 0x10000 - offset; length = 0x10000 - offset;
} }
if (offset < 0 || length <= 0 || length > sizeof(buf) ||
(offset + length) > 0x10000) {
fprintf(stderr, "dis: memory out of range\n");
return -1;
}
if (dev->readmem(dev, offset, buf, length) < 0) if (dev->readmem(dev, offset, buf, length) < 0)
return -1; return -1;
@ -310,9 +294,11 @@ static int cmd_dis(cproc_t cp, char **arg)
struct hexout_data { struct hexout_data {
FILE *file; FILE *file;
uint16_t addr; address_t addr;
uint8_t buf[16]; uint8_t buf[16];
int len; int len;
uint16_t segoff;
}; };
static int hexout_start(struct hexout_data *hexout, const char *filename) static int hexout_start(struct hexout_data *hexout, const char *filename)
@ -325,35 +311,32 @@ static int hexout_start(struct hexout_data *hexout, const char *filename)
hexout->addr = 0; hexout->addr = 0;
hexout->len = 0; hexout->len = 0;
hexout->segoff = 0;
return 0; return 0;
} }
static int hexout_flush(struct hexout_data *hexout) static int hexout_write(FILE *out, int len, uint16_t addr, int type,
const uint8_t *payload)
{ {
int i; int i;
int cksum = 0; int cksum = 0;
if (!hexout->len) if (fprintf(out, ":%02X%04X00", len, addr) < 0)
return 0;
if (fprintf(hexout->file, ":%02X%04X00",
hexout->len, hexout->addr) < 0)
goto fail; goto fail;
cksum += hexout->len; cksum += len;
cksum += hexout->addr & 0xff; cksum += addr & 0xff;
cksum += hexout->addr >> 8; cksum += addr >> 8;
for (i = 0; i < hexout->len; i++) { for (i = 0; i < len; i++) {
if (fprintf(hexout->file, "%02X", hexout->buf[i]) < 0) if (fprintf(out, "%02X", payload[i]) < 0)
goto fail; goto fail;
cksum += hexout->buf[i]; cksum += payload[i];
} }
if (fprintf(hexout->file, "%02X\n", ~(cksum - 1) & 0xff) < 0) if (fprintf(out, "%02X\n", ~(cksum - 1) & 0xff) < 0)
goto fail; goto fail;
hexout->len = 0;
return 0; return 0;
fail: fail:
@ -361,6 +344,29 @@ fail:
return -1; return -1;
} }
static int hexout_flush(struct hexout_data *hexout)
{
address_t addr_low = hexout->addr & 0xffff;
address_t segoff = hexout->addr >> 16;
if (!hexout->len)
return 0;
if (segoff != hexout->segoff) {
uint8_t offset_data[] = {segoff >> 8, segoff & 0xff};
if (hexout_write(hexout->file, 2, 0, 4, offset_data) < 0)
return -1;
hexout->segoff = segoff;
}
if (hexout_write(hexout->file, hexout->len, addr_low,
0, hexout->buf) < 0)
return -1;
hexout->len = 0;
return 0;
}
static int hexout_feed(struct hexout_data *hexout, static int hexout_feed(struct hexout_data *hexout,
uint16_t addr, const uint8_t *buf, int len) uint16_t addr, const uint8_t *buf, int len)
{ {
@ -658,7 +664,7 @@ static int cmd_break(cproc_t cp, char **arg)
char name[128]; char name[128];
address_t offset; address_t offset;
printf(" %d. 0x%04x", i, bp->addr); printf(" %d. 0x%05x", i, bp->addr);
if (!stab_nearest(stab, bp->addr, name, if (!stab_nearest(stab, bp->addr, name,
sizeof(name), &offset)) { sizeof(name), &offset)) {
printf(" (%s", name); printf(" (%s", name);

View File

@ -327,19 +327,12 @@ static int isearch_match(const struct msp430_instruction *insn,
return 1; return 1;
} }
static int do_isearch(cproc_t cp, static int do_isearch(cproc_t cp, address_t addr, address_t len,
int addr, int len, const struct isearch_query *q) const struct isearch_query *q)
{ {
uint8_t *mbuf; uint8_t *mbuf;
device_t dev = cproc_device(cp); device_t dev = cproc_device(cp);
int i; address_t i;
if (len <= 0 || len > 0x10000 ||
addr <= 0 || addr >= 0x10000 ||
addr + len > 0x10000) {
fprintf(stderr, "isearch: invalid memory range\n");
return -1;
}
mbuf = malloc(len); mbuf = malloc(len);
if (!mbuf) { if (!mbuf) {
@ -354,6 +347,8 @@ static int do_isearch(cproc_t cp,
return -1; return -1;
} }
addr &= ~1;
len &= ~1;
for (i = 0; i < len; i += 2) { for (i = 0; i < len; i += 2) {
struct msp430_instruction insn; struct msp430_instruction insn;
int count = dis_decode(mbuf + i, addr + i, len - i, &insn); int count = dis_decode(mbuf + i, addr + i, len - i, &insn);
@ -440,8 +435,8 @@ static int cmd_isearch(cproc_t cp, char **arg)
struct cg_edge { struct cg_edge {
int is_tail_call; int is_tail_call;
uint16_t src; address_t src;
uint16_t dst; address_t dst;
}; };
static int cmp_branch_by_dst(const void *a, const void *b) static int cmp_branch_by_dst(const void *a, const void *b)
@ -491,7 +486,7 @@ static int cmp_branch_by_src(const void *a, const void *b)
} }
struct cg_node { struct cg_node {
uint16_t offset; address_t offset;
}; };
static int cmp_node(const void *a, const void *b) static int cmp_node(const void *a, const void *b)
@ -558,7 +553,7 @@ static int find_possible_edges(int offset, int len, uint8_t *memory,
static int add_nodes_from_edges(struct call_graph *graph) static int add_nodes_from_edges(struct call_graph *graph)
{ {
int i; int i;
uint16_t last_addr = 0; address_t last_addr = 0;
int have_last_addr = 0; int have_last_addr = 0;
qsort(graph->edge_from.ptr, graph->edge_from.size, qsort(graph->edge_from.ptr, graph->edge_from.size,
@ -675,11 +670,14 @@ static int build_inverse(struct call_graph *graph)
return 0; return 0;
} }
static int add_irq_edges(int offset, int len, uint8_t *memory, static int add_irq_edges(address_t offset, address_t len, uint8_t *memory,
struct call_graph *graph) struct call_graph *graph)
{ {
int i; int i;
if (offset > 0x10000 || offset + len <= 0xffe0)
return 0;
if (offset < 0xffe0) { if (offset < 0xffe0) {
len -= (0xffe0 - offset); len -= (0xffe0 - offset);
memory += (0xffe0 - offset); memory += (0xffe0 - offset);
@ -732,7 +730,7 @@ static int add_symbol_nodes(void *user_data, const char *name,
return 0; return 0;
} }
static int cgraph_init(int offset, int len, uint8_t *memory, static int cgraph_init(address_t offset, address_t len, uint8_t *memory,
struct call_graph *graph, stab_t stab) struct call_graph *graph, stab_t stab)
{ {
vector_init(&graph->edge_to, sizeof(struct cg_edge)); vector_init(&graph->edge_to, sizeof(struct cg_edge));
@ -925,12 +923,6 @@ static int cmd_cgraph(cproc_t cp, char **arg)
return -1; return -1;
} }
if (offset < 0 || offset >= 0x10000 ||
len <= 0 || (offset + len) > 0x10000) {
fprintf(stderr, "cgraph: invalid range\n");
return -1;
}
/* Grab the memory to be analysed */ /* Grab the memory to be analysed */
memory = malloc(len); memory = malloc(len);
if (!memory) { if (!memory) {

2
sym.c
View File

@ -42,7 +42,7 @@ static int cmd_eval(cproc_t cp, char **arg)
return -1; return -1;
} }
printf("0x%04x", addr); printf("0x%05x", addr);
if (!stab_nearest(stab, addr, name, sizeof(name), &offset)) { if (!stab_nearest(stab, addr, name, sizeof(name), &offset)) {
printf(" = %s", name); printf(" = %s", name);
if (offset) if (offset)