Extracted print_address.

This commit is contained in:
Daniel Beer 2011-03-15 16:00:34 +13:00
parent 41375ba084
commit 7e1068334f
6 changed files with 50 additions and 84 deletions

View File

@ -622,17 +622,9 @@ int cmd_break(char **arg)
if (bp->flags & DEVICE_BP_ENABLED) { if (bp->flags & DEVICE_BP_ENABLED) {
char name[128]; char name[128];
address_t offset;
printc(" %d. 0x%05x", i, bp->addr); print_address(bp->addr, name, sizeof(name));
if (!stab_nearest(bp->addr, name, printc(" %d. %s\n", i, name);
sizeof(name), &offset)) {
printc(" (%s", name);
if (offset)
printc("+0x%x", offset);
printc(")");
}
printc("\n");
} }
} }

View File

@ -27,7 +27,6 @@
static int format_addr(msp430_amode_t amode, uint16_t addr) static int format_addr(msp430_amode_t amode, uint16_t addr)
{ {
char name[64]; char name[64];
address_t offset;
int numeric = 0; int numeric = 0;
const char *prefix = ""; const char *prefix = "";
@ -51,15 +50,8 @@ static int format_addr(msp430_amode_t amode, uint16_t addr)
break; break;
} }
if ((!numeric || print_address(addr, name, sizeof(name));
(addr >= 0x200 && addr < 0xfff0)) && return printc("%s\x1b[1m%s\x1b[0m", prefix, name);
!stab_nearest(addr, name, sizeof(name), &offset) &&
!offset)
return printc("%s\x1b[1m%s\x1b[0m", prefix, name);
else if (numeric)
return printc("%s\x1b[1m0x%x\x1b[0m", prefix, addr);
else
return printc("%s\x1b[1m0x%04x\x1b[0m", prefix, addr);
} }
static int format_reg(msp430_amode_t amode, msp430_reg_t reg) static int format_reg(msp430_amode_t amode, msp430_reg_t reg)
@ -178,13 +170,12 @@ void disassemble(address_t offset, const uint8_t *data, int length)
address_t oboff; address_t oboff;
char obname[64]; char obname[64];
if (!stab_nearest(offset, obname, sizeof(obname), if (!stab_nearest(offset, obname, sizeof(obname), &oboff) &&
&oboff)) { !oboff) {
if (!oboff) printc("\x1b[m%s\x1b[0m:\n", obname);
printc("\x1b[m%s:\x1b[0m\n", obname); } else if (first_line) {
else if (first_line) print_address(offset, obname, sizeof(obname));
printc("\x1b[m%s+0x%x:\x1b[0m\n", printc("\x1b[m%s\x1b[0m:\n", obname);
obname, oboff);
} }
first_line = 0; first_line = 0;
@ -262,3 +253,21 @@ void show_regs(const address_t *regs)
printc("\n"); printc("\n");
} }
} }
int print_address(address_t addr, char *out, int max_len)
{
char name[128];
address_t offset;
if (!stab_nearest(addr, name, sizeof(name), &offset)) {
if (offset)
snprintf(out, max_len, "%s+0x%x", name, offset);
else
snprintf(out, max_len, "%s", name);
return 1;
}
snprintf(out, max_len, "0x%04x", addr);
return 0;
}

View File

@ -31,4 +31,10 @@ void hexdump(address_t addr, const uint8_t *buf, int len);
/* Colorized register dump */ /* Colorized register dump */
void show_regs(const address_t *regs); void show_regs(const address_t *regs);
/* Given an address, format it either as sym+0x0offset or just 0x0offset.
*
* Returns non-zero if the result is of the form sym+0x0offset.
*/
int print_address(address_t addr, char *buf, int max_len);
#endif #endif

View File

@ -785,7 +785,6 @@ static void cgraph_summary(struct call_graph *graph)
int from_count = 0; int from_count = 0;
int to_count = 0; int to_count = 0;
char name[64]; char name[64];
address_t o;
while (j < graph->edge_from.size && while (j < graph->edge_from.size &&
CG_EDGE_FROM(graph, j)->src < n->offset) CG_EDGE_FROM(graph, j)->src < n->offset)
@ -807,11 +806,7 @@ static void cgraph_summary(struct call_graph *graph)
k++; k++;
} }
if (stab_nearest(n->offset, print_address(n->offset, name, sizeof(name));
name, sizeof(name), &o) ||
o)
name[0] = 0;
printc("0x%04x [%3d ==> %3d] %s\n", printc("0x%04x [%3d ==> %3d] %s\n",
n->offset, to_count, from_count, name); n->offset, to_count, from_count, name);
} }
@ -823,7 +818,6 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
int j = 0; int j = 0;
int k = 0; int k = 0;
char name[64]; char name[64];
address_t offset;
struct cg_node *n; struct cg_node *n;
while (i + 1 < graph->node_list.size && while (i + 1 < graph->node_list.size &&
@ -845,12 +839,8 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
CG_EDGE_TO(graph, k)->dst < n->offset) CG_EDGE_TO(graph, k)->dst < n->offset)
k++; k++;
if (stab_nearest(n->offset, name, sizeof(name), &offset)) print_address(n->offset, name, sizeof(name));
printc("0x%04x:\n", n->offset); printc("0x%04x %s:\n", n->offset, name);
else if (offset)
printc("0x%04x %s+0x%x:\n", n->offset, name, offset);
else
printc("0x%04x %s:\n", n->offset, name);
if (j < graph->edge_from.size && if (j < graph->edge_from.size &&
CG_EDGE_FROM(graph, j)->src == n->offset) { CG_EDGE_FROM(graph, j)->src == n->offset) {
@ -861,11 +851,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
if (e->src != n->offset) if (e->src != n->offset)
break; break;
if (stab_nearest(e->dst, print_address(e->dst, name, sizeof(name));
name, sizeof(name),
&offset) ||
offset)
snprintf(name, sizeof(name), "0x%04x", e->dst);
printc(" %s%s\n", printc(" %s%s\n",
e->is_tail_call ? "*" : "", name); e->is_tail_call ? "*" : "", name);
@ -883,11 +869,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
if (e->dst != n->offset) if (e->dst != n->offset)
break; break;
if (stab_nearest(e->src, print_address(e->src, name, sizeof(name));
name, sizeof(name),
&offset) ||
offset)
snprintf(name, sizeof(name), "0x%04x", e->src);
printc(" %s%s\n", printc(" %s%s\n",
e->is_tail_call ? "*" : "", name); e->is_tail_call ? "*" : "", name);

View File

@ -23,6 +23,7 @@
#include "simio_tracer.h" #include "simio_tracer.h"
#include "expr.h" #include "expr.h"
#include "output.h" #include "output.h"
#include "output_util.h"
#include "dis.h" #include "dis.h"
#define DEFAULT_HISTORY 16 #define DEFAULT_HISTORY 16
@ -65,47 +66,28 @@ struct tracer {
int verbose; int verbose;
}; };
static void print_address(address_t addr)
{
char name[64];
address_t offset;
printc("0x%04x", addr);
if (!stab_nearest(addr, name, sizeof(name), &offset)) {
printc(" (%s", name);
if (offset)
printc("+0x%x", offset);
printc(")");
}
}
static void event_print(const struct event *e) static void event_print(const struct event *e)
{ {
char name[128];
print_address(e->addr, name, sizeof(name));
printc(" %10lld: ", e->when); printc(" %10lld: ", e->when);
switch (e->what) { switch (e->what) {
case EVENT_WRITE_16: case EVENT_WRITE_16:
printc("write.w "); printc("write.w => %s 0x%04x\n", name, e->data);
print_address(e->addr);
printc(" => 0x%04x\n", e->data);
break; break;
case EVENT_READ_16: case EVENT_READ_16:
printc("read.w "); printc("read.w => %s 0x%04x\n", name);
print_address(e->addr);
printc("\n");
break; break;
case EVENT_WRITE_8: case EVENT_WRITE_8:
printc("write.b "); printc("write.b => %s 0x%02x\n", name, e->data);
print_address(e->addr);
printc(" => 0x%02x\n", e->data);
break; break;
case EVENT_READ_8: case EVENT_READ_8:
printc("read.b "); printc("read.b => %s\n", name);
print_address(e->addr);
printc("\n");
break; break;
case EVENT_IRQ_HANDLE: case EVENT_IRQ_HANDLE:

11
sym.c
View File

@ -28,6 +28,7 @@
#include "binfile.h" #include "binfile.h"
#include "util.h" #include "util.h"
#include "output.h" #include "output.h"
#include "output_util.h"
#include "vector.h" #include "vector.h"
#include "sym.h" #include "sym.h"
#include "reader.h" #include "reader.h"
@ -35,7 +36,6 @@
int cmd_eval(char **arg) int cmd_eval(char **arg)
{ {
address_t addr; address_t addr;
address_t offset;
char name[64]; char name[64];
if (expr_eval(*arg, &addr) < 0) { if (expr_eval(*arg, &addr) < 0) {
@ -43,13 +43,8 @@ int cmd_eval(char **arg)
return -1; return -1;
} }
printc("0x%05x", addr); print_address(addr, name, sizeof(name));
if (!stab_nearest(addr, name, sizeof(name), &offset)) { printc("0x%05x = %s\n", addr, name);
printc(" = %s", name);
if (offset)
printc("+0x%x", offset);
}
printc("\n");
return 0; return 0;
} }