From 7e1068334f34d5fb30b6fb95e4b36fd85bbbd25c Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Tue, 15 Mar 2011 16:00:34 +1300 Subject: [PATCH] Extracted print_address. --- devcmd.c | 12 ++---------- output_util.c | 43 ++++++++++++++++++++++++++----------------- output_util.h | 6 ++++++ rtools.c | 28 +++++----------------------- simio_tracer.c | 34 ++++++++-------------------------- sym.c | 11 +++-------- 6 files changed, 50 insertions(+), 84 deletions(-) diff --git a/devcmd.c b/devcmd.c index a4d8e10..65e18d7 100644 --- a/devcmd.c +++ b/devcmd.c @@ -622,17 +622,9 @@ int cmd_break(char **arg) if (bp->flags & DEVICE_BP_ENABLED) { char name[128]; - address_t offset; - printc(" %d. 0x%05x", i, bp->addr); - if (!stab_nearest(bp->addr, name, - sizeof(name), &offset)) { - printc(" (%s", name); - if (offset) - printc("+0x%x", offset); - printc(")"); - } - printc("\n"); + print_address(bp->addr, name, sizeof(name)); + printc(" %d. %s\n", i, name); } } diff --git a/output_util.c b/output_util.c index 89feeda..e042546 100644 --- a/output_util.c +++ b/output_util.c @@ -27,7 +27,6 @@ static int format_addr(msp430_amode_t amode, uint16_t addr) { char name[64]; - address_t offset; int numeric = 0; const char *prefix = ""; @@ -51,15 +50,8 @@ static int format_addr(msp430_amode_t amode, uint16_t addr) break; } - if ((!numeric || - (addr >= 0x200 && addr < 0xfff0)) && - !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); + print_address(addr, name, sizeof(name)); + return printc("%s\x1b[1m%s\x1b[0m", prefix, name); } 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; char obname[64]; - if (!stab_nearest(offset, obname, sizeof(obname), - &oboff)) { - if (!oboff) - printc("\x1b[m%s:\x1b[0m\n", obname); - else if (first_line) - printc("\x1b[m%s+0x%x:\x1b[0m\n", - obname, oboff); + if (!stab_nearest(offset, obname, sizeof(obname), &oboff) && + !oboff) { + printc("\x1b[m%s\x1b[0m:\n", obname); + } else if (first_line) { + print_address(offset, obname, sizeof(obname)); + printc("\x1b[m%s\x1b[0m:\n", obname); } first_line = 0; @@ -262,3 +253,21 @@ void show_regs(const address_t *regs) 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; +} diff --git a/output_util.h b/output_util.h index fa4c952..05ea8b9 100644 --- a/output_util.h +++ b/output_util.h @@ -31,4 +31,10 @@ void hexdump(address_t addr, const uint8_t *buf, int len); /* Colorized register dump */ 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 diff --git a/rtools.c b/rtools.c index 6db185e..991041a 100644 --- a/rtools.c +++ b/rtools.c @@ -785,7 +785,6 @@ static void cgraph_summary(struct call_graph *graph) int from_count = 0; int to_count = 0; char name[64]; - address_t o; while (j < graph->edge_from.size && CG_EDGE_FROM(graph, j)->src < n->offset) @@ -807,11 +806,7 @@ static void cgraph_summary(struct call_graph *graph) k++; } - if (stab_nearest(n->offset, - name, sizeof(name), &o) || - o) - name[0] = 0; - + print_address(n->offset, name, sizeof(name)); printc("0x%04x [%3d ==> %3d] %s\n", 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 k = 0; char name[64]; - address_t offset; struct cg_node *n; 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) k++; - if (stab_nearest(n->offset, name, sizeof(name), &offset)) - printc("0x%04x:\n", n->offset); - else if (offset) - printc("0x%04x %s+0x%x:\n", n->offset, name, offset); - else - printc("0x%04x %s:\n", n->offset, name); + print_address(n->offset, name, sizeof(name)); + printc("0x%04x %s:\n", n->offset, name); if (j < graph->edge_from.size && 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) break; - if (stab_nearest(e->dst, - name, sizeof(name), - &offset) || - offset) - snprintf(name, sizeof(name), "0x%04x", e->dst); + print_address(e->dst, name, sizeof(name)); printc(" %s%s\n", 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) break; - if (stab_nearest(e->src, - name, sizeof(name), - &offset) || - offset) - snprintf(name, sizeof(name), "0x%04x", e->src); + print_address(e->src, name, sizeof(name)); printc(" %s%s\n", e->is_tail_call ? "*" : "", name); diff --git a/simio_tracer.c b/simio_tracer.c index fc65aa4..e39c8a7 100644 --- a/simio_tracer.c +++ b/simio_tracer.c @@ -23,6 +23,7 @@ #include "simio_tracer.h" #include "expr.h" #include "output.h" +#include "output_util.h" #include "dis.h" #define DEFAULT_HISTORY 16 @@ -65,47 +66,28 @@ struct tracer { 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) { + char name[128]; + + print_address(e->addr, name, sizeof(name)); printc(" %10lld: ", e->when); switch (e->what) { case EVENT_WRITE_16: - printc("write.w "); - print_address(e->addr); - printc(" => 0x%04x\n", e->data); + printc("write.w => %s 0x%04x\n", name, e->data); break; case EVENT_READ_16: - printc("read.w "); - print_address(e->addr); - printc("\n"); + printc("read.w => %s 0x%04x\n", name); break; case EVENT_WRITE_8: - printc("write.b "); - print_address(e->addr); - printc(" => 0x%02x\n", e->data); + printc("write.b => %s 0x%02x\n", name, e->data); break; case EVENT_READ_8: - printc("read.b "); - print_address(e->addr); - printc("\n"); + printc("read.b => %s\n", name); break; case EVENT_IRQ_HANDLE: diff --git a/sym.c b/sym.c index fdfc5c4..23b1471 100644 --- a/sym.c +++ b/sym.c @@ -28,6 +28,7 @@ #include "binfile.h" #include "util.h" #include "output.h" +#include "output_util.h" #include "vector.h" #include "sym.h" #include "reader.h" @@ -35,7 +36,6 @@ int cmd_eval(char **arg) { address_t addr; - address_t offset; char name[64]; if (expr_eval(*arg, &addr) < 0) { @@ -43,13 +43,8 @@ int cmd_eval(char **arg) return -1; } - printc("0x%05x", addr); - if (!stab_nearest(addr, name, sizeof(name), &offset)) { - printc(" = %s", name); - if (offset) - printc("+0x%x", offset); - } - printc("\n"); + print_address(addr, name, sizeof(name)); + printc("0x%05x = %s\n", addr, name); return 0; }