Implement PEDA-like colors for addresses in pxw and pxq

- Uses 'ai' command to get address information
- We need to define new color palete entries for them
- We now have colors for: code, data, rodata, ascii, sequence
This commit is contained in:
pancake 2014-10-24 21:31:46 +02:00
parent e807868b78
commit 25927e0b60
6 changed files with 89 additions and 12 deletions

View File

@ -89,6 +89,41 @@ R_API ut64 r_core_anal_address (RCore *core, ut64 addr) {
}
}
}
// check if it's ascii
int not_ascii = 0;
if (addr != 0) {
int i, failed_sequence, dir, on;
for (i=0; i<8; i++) {
ut8 n = (addr>> (i*8)) & 0xff;
if (n && !IS_PRINTABLE (n))
not_ascii = 1;
}
if (!not_ascii)
types |= R_ANAL_ADDR_TYPE_ASCII;
failed_sequence = 0;
dir = on = -1;
for (i=0; i<8; i++) {
ut8 n = (addr>> (i*8)) & 0xff;
if (on != -1) {
if (dir == -1)
dir = (n>on)? 1: -1;
if (n == on+dir) {
// ok
} else {
failed_sequence = 1;
break;
}
}
on = n;
}
if (!failed_sequence)
types |= R_ANAL_ADDR_TYPE_SEQUENCE;
}
return types;
}

View File

@ -1094,6 +1094,10 @@ static void cmd_address_info(RCore *core, const char *addrstr, int fmt) {
r_cons_printf ("%s\"heap\":true", COMMA);
if (type & R_ANAL_ADDR_TYPE_REG)
r_cons_printf ("%s\"reg\":true", COMMA);
if (type & R_ANAL_ADDR_TYPE_ASCII)
r_cons_printf ("%s\"ascii\":true", COMMA);
if (type & R_ANAL_ADDR_TYPE_SEQUENCE)
r_cons_printf ("%s\"sequence\":true", COMMA);
r_cons_printf ("}");
break;
default:
@ -1117,6 +1121,10 @@ static void cmd_address_info(RCore *core, const char *addrstr, int fmt) {
r_cons_printf ("heap\n");
if (type & R_ANAL_ADDR_TYPE_REG)
r_cons_printf ("reg\n");
if (type & R_ANAL_ADDR_TYPE_ASCII)
r_cons_printf ("ascii\n");
if (type & R_ANAL_ADDR_TYPE_SEQUENCE)
r_cons_printf ("sequence\n");
}
}

View File

@ -559,6 +559,22 @@ static char *getbitfield(void *_core, const char *name, ut64 val) {
return ret;
}
R_API const char *__colorfor(RCore *core, ut64 addr) {
ut64 type = r_core_anal_address (core, addr);
// same colors as PEDA
if (type & R_ANAL_ADDR_TYPE_EXEC)
return Color_RED;
if (type & R_ANAL_ADDR_TYPE_WRITE)
return Color_BLUE;
if (type & R_ANAL_ADDR_TYPE_READ)
return Color_GREEN;
if (type & R_ANAL_ADDR_TYPE_SEQUENCE)
return Color_MAGENTA;
if (type & R_ANAL_ADDR_TYPE_ASCII)
return Color_YELLOW;
return NULL;
}
R_API int r_core_init(RCore *core) {
static int singleton = R_TRUE;
core->cmd_depth = R_CORE_CMD_DEPTH+1;
@ -575,6 +591,7 @@ R_API int r_core_init(RCore *core) {
core->print->printf = (void *)r_cons_printf;
core->print->write = (void *)r_cons_memcat;
core->print->disasm = __disasm;
core->print->colorfor = __colorfor;
core->rtr_n = 0;
core->blocksize_max = R_CORE_BLOCKSIZE_MAX;
core->watchers = r_list_new ();

View File

@ -70,16 +70,18 @@ enum {
};
// used from core/anal.c
#define R_ANAL_ADDR_TYPE_EXEC 1
#define R_ANAL_ADDR_TYPE_READ 2
#define R_ANAL_ADDR_TYPE_WRITE 4
#define R_ANAL_ADDR_TYPE_FLAG 8
#define R_ANAL_ADDR_TYPE_FUNC 16
#define R_ANAL_ADDR_TYPE_HEAP 32
#define R_ANAL_ADDR_TYPE_STACK 64
#define R_ANAL_ADDR_TYPE_REG 128
#define R_ANAL_ADDR_TYPE_PROGRAM 256
#define R_ANAL_ADDR_TYPE_LIBRARY 512
#define R_ANAL_ADDR_TYPE_EXEC 1
#define R_ANAL_ADDR_TYPE_READ 1<<1
#define R_ANAL_ADDR_TYPE_WRITE 1<<2
#define R_ANAL_ADDR_TYPE_FLAG 1<<3
#define R_ANAL_ADDR_TYPE_FUNC 1<<4
#define R_ANAL_ADDR_TYPE_HEAP 1<<5
#define R_ANAL_ADDR_TYPE_STACK 1<<6
#define R_ANAL_ADDR_TYPE_REG 1<<7
#define R_ANAL_ADDR_TYPE_PROGRAM 1<<8
#define R_ANAL_ADDR_TYPE_LIBRARY 1<<9
#define R_ANAL_ADDR_TYPE_ASCII 1<<10
#define R_ANAL_ADDR_TYPE_SEQUENCE 1<<11
/* type = (R_ANAL_VAR_TYPE_BYTE & R_ANAL_VAR_TYPE_SIZE_MASK) |
* ( RANAL_VAR_TYPE_SIGNED & RANAL_VAR_TYPE_SIGN_MASK) |

View File

@ -20,6 +20,7 @@ extern "C" {
typedef int (*RPrintZoomCallback)(void *user, int mode, ut64 addr, ut8 *bufz, ut64 size);
typedef const char *(*RPrintNameCallback)(void *user, ut64 addr);
typedef const char *(*RPrintColorFor)(void *user, ut64 addr);
typedef struct r_print_zoom_t {
ut8 *buf;
@ -56,6 +57,7 @@ typedef struct r_print_t {
int pairs;
RPrintZoom *zoom;
RPrintNameCallback offname;
RPrintColorFor colorfor;
RStrHT *formats;
RCons *cons;
} RPrint;

View File

@ -428,6 +428,7 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
const char *fmt = "%02x";
const char *pre = "";
int last_sparse = 0;
const char *a, *b;
if (p) {
pairs = p->pairs;
@ -521,7 +522,14 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
ut32 n = 0;
r_mem_copyendian ((ut8*)&n, buf+j, sizeof (n), !p->big_endian);
r_print_cursor (p, j, 1);
printfmt ("0x%08x ", n);
// stub for colors
if (p && p->colorfor) {
a = p->colorfor (p->user, n);
if (a && *a) { b = Color_RESET; } else { a = b = ""; }
} else { a = b = ""; }
printfmt ("%s0x%08x%s ", a, n, b);
r_print_cursor (p, j, 0);
j += 3;
} else
@ -531,7 +539,12 @@ R_API void r_print_hexdump(RPrint *p, ut64 addr, const ut8 *buf, int len, int ba
* a multiple of 4 for base == 64. */
r_mem_copyendian ((ut8*)&x, buf+j, sizeof (x), !p->big_endian);
r_print_cursor (p, j, 1);
printfmt ("0x%016"PFMT64x" ", x);
// stub for colors
if (p && p->colorfor) {
a = p->colorfor (p->user, x);
if (a && *a) { b = Color_RESET; } else { a = b = ""; }
} else { a = b = ""; }
printfmt ("%s0x%016"PFMT64x"%s ", a, x, b);
r_print_cursor (p, j, 0);
j += 7;
} else {