* Refactoring of the r_print module

- State-aware now
  - Sync examples and r_core
This commit is contained in:
pancake 2009-03-11 11:42:11 +00:00
parent 50fbf06b64
commit dab6dacf50
9 changed files with 216 additions and 117 deletions

View File

@ -318,19 +318,19 @@ static int cmd_print(void *data, const char *input)
}
break;
case 's':
r_print_string(core->seek, core->block, len, 1, 78, 1);
r_print_string(&core->print, core->seek, core->block, len); //, 78, 1);
break;
case 'c':
r_print_code(core->seek, core->block, len, 1, 78, 1);
r_print_code(&core->print, core->seek, core->block, len); //, 78, 1);
break;
case 'r':
r_print_raw(core->block, len);
r_print_raw(&core->print, core->block, len);
break;
case 'x':
r_print_hexdump(core->seek, core->block, len, 1, 78, !(input[1]=='-'));
r_print_hexdump(&core->print, core->seek, core->block, len, 1); //, 78, !(input[1]=='-'));
break;
case '8':
r_print_bytes(core->block, len, "%02x");
r_print_bytes(&core->print, core->block, len, "%02x");
break;
default:
fprintf(stderr, "Usage: p[8] [len]\n"

View File

@ -125,6 +125,7 @@ int r_core_init(struct r_core_t *core)
core->yank_off = 0LL;
core->num.callback = &num_callback;
core->num.userptr = core;
r_print_init(&core->print);
r_lang_init(&core->lang);
r_lang_set_user_ptr(&core->lang, core);
r_anal_init(&core->anal);

View File

@ -46,13 +46,13 @@ int r_core_visual_cmd(struct r_core_t *core, int ch)
curset ^= 1;
if (curset) flags|=R_PRINT_FLAGS_CURSOR; // XXX dupped flag imho
else flags &= !(flags&R_PRINT_FLAGS_CURSOR);
r_print_set_flags(flags);
r_print_set_flags(&core->print, flags);
break;
case 'C':
color ^= 1;
if (color) flags|=R_PRINT_FLAGS_COLOR;
else flags &= !(flags&R_PRINT_FLAGS_COLOR);
r_print_set_flags(flags);
r_print_set_flags(&core->print, flags);
break;
/* select */
case 'H':
@ -187,7 +187,7 @@ int r_core_visual(struct r_core_t *core, const char *input)
if (cmdprompt && cmdprompt[0])
r_core_cmd(core, cmdprompt, 0);
r_cons_clear00();
r_print_set_cursor(curset, ocursor, cursor);
r_print_set_cursor(&core->print, curset, ocursor, cursor);
r_core_visual_prompt(core);
r_core_cmd(core, printfmt[printidx%NPF], 0);
r_cons_flush();

View File

@ -53,6 +53,7 @@ struct r_core_t {
struct r_anal_t anal;
struct r_asm_t assembler;
struct r_parse_t parser;
struct r_print_t print;
struct r_bin_t bin;
struct r_meta_t meta;
struct r_lang_t lang;

View File

@ -5,16 +5,33 @@
#define R_PRINT_FLAGS_COLOR 0x00000001
#define R_PRINT_FLAGS_ADDRMOD 0x00000002
#define R_PRINT_FLAGS_CURSOR 0x00000003
#define R_PRINT_FLAGS_CURSOR 0x00000004
#define R_PRINT_FLAGS_HEADER 0x00000008
void r_print_set_flags(int flags);
void r_print_addr(u64 addr);
void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int header);
void r_print_bytes(const u8* buf, int len, const char *fmt);
void r_print_raw(const u8* buf, int len);
void r_print_cursor(int cur, int set);
void r_print_set_cursor(int curset, int ocursor, int cursor);
void r_print_code(u64 addr, u8 *buf, int len, int step, int columns, int header);
void r_print_string(u64 addr, u8 *buf, int len, int step, int columns, int header);
struct r_print_t {
void *user;
int (*read_at)(u64 addr, u8 *buf, int len, void *user);
/* TODO: add printf callback */
int width;
int cur_enabled;
int cur;
int ocur;
int flags;
int addrmod;
};
struct r_print_t *r_print_new();
struct r_print_t *r_print_free(struct r_print_t *p);
void r_print_set_flags(struct r_print_t *p, int _flags);
void r_print_set_width(struct r_print_t *p, int width);
void r_print_addr(struct r_print_t *p, u64 addr);
void r_print_hexdump(struct r_print_t *p, u64 addr, u8 *buf, int len, int step);
void r_print_bytes(struct r_print_t *p, const u8* buf, int len, const char *fmt);
void r_print_raw(struct r_print_t *p, const u8* buf, int len);
void r_print_cursor(struct r_print_t *p, int cur, int set);
void r_print_set_cursor(struct r_print_t *p, int curset, int ocursor, int cursor);
void r_print_code(struct r_print_t *p, u64 addr, u8 *buf, int len);
void r_print_string(struct r_print_t *p, u64 addr, u8 *buf, int len);
void r_print_format(struct r_print_t *p, u64 seek, const u8* buf, int len, const char *fmt);
#endif

View File

@ -1,19 +1,46 @@
/* radare - LGPL - Copyright 2007-2009 pancake<nopcode.org> */
#include "r_cons.h"
#include "r_util.h"
#include "r_print.h"
void r_print_format(const u8* buf, int len, const char *fmt)
static void print_mem_help()
{
printf(
"Usage: pm [times][format] [arg0 arg1]\n"
"Example: pm 10xiz pointer length string\n"
"Example: pm {array_size}b @ array_base\n"
" e - temporally swap endian\n"
//" D - double (8 bytes)\n"
" f - float value\n"
" b - one byte \n"
" B - show 10 first bytes of buffer\n"
" d - %%d integer value (4 bytes)\n"
" w - word (16 bit hexa)\n"
" q - quadword (8 bytes)\n"
" p - pointer reference\n"
" x - 0x%%08x hexadecimal value\n"
" X - 0x%%08x hexadecimal value and flag (fd @ addr)\n"
" z - \\0 terminated string\n"
" Z - \\0 terminated wide string\n"
" s - pointer to string\n"
" t - unix timestamp string\n"
" * - next char is pointer\n"
" . - skip 1 byte\n");
}
void r_print_format(struct r_print_t *p, u64 seek, const u8* buf, int len, const char *fmt)
{
/* TODO: needs refactoring */
#if 0
unsigned char buffer[256];
int endian = 0;
int i,j,idx;
int times, otimes;
char tmp, last;
char *args, *bracket;
int nargs;
const char *arg = fmt;
u64 addr = 0;
i = j = 0;
while(*arg && *arg==' ') arg = arg +1;
@ -25,12 +52,12 @@ void r_print_format(const u8* buf, int len, const char *fmt)
if (bracket) {
char *end = strchr(arg,'}');
if (end == NULL) {
eprintf("No end bracket. Try pm {ecx}b @ esi\n");
fprintf(stderr, "No end bracket. Try pm {ecx}b @ esi\n");
return;
}
*end='\0';
times = get_math(bracket+1);
arg = end+1;
times = r_num_math(NULL, bracket+1);
arg = end + 1;
}
if (arg[0]=='\0') {
@ -41,20 +68,19 @@ void r_print_format(const u8* buf, int len, const char *fmt)
args = strchr(arg, ' ');
if (args) {
args = strdup(args+1);
nargs = set0word(args);
nargs = r_str_word_set0(args);
if (nargs == 0)
efree((void **)&args);
free((void **)&args);
}
/* go format */
i = 0;
if (times==0) otimes=times=1;
for(;times;times--) {// repeat N times
for(;times;times--) { // repeat N times
const char * orig = arg;
if (otimes>1)
cons_printf("0x%08llx [%d] {\n", config.seek+i, otimes-times);
config.interrupted = 0;
for(idx=0;!config.interrupted && idx<len;idx++, arg=arg+1) {
r_cons_printf("0x%08llx [%d] {\n", seek+i, otimes-times);
for(idx=0;idx<len;idx++, arg=arg+1) {
addr = 0LL;
if (endian)
addr = (*(buf+i))<<24 | (*(buf+i+1))<<16 | *(buf+i+2)<<8 | *(buf+i+3);
@ -91,7 +117,7 @@ void r_print_format(const u8* buf, int len, const char *fmt)
continue;
}
if (idx<nargs)
cons_printf("%10s : ", get0word(args, idx));
r_cons_printf("%10s : ", r_str_word_get0(args, idx));
/* cmt chars */
switch(tmp) {
#if 0
@ -99,6 +125,7 @@ void r_print_format(const u8* buf, int len, const char *fmt)
j ^= 1;
continue;
#endif
#if 0
case 't':
/* unix timestamp */
D cons_printf("0x%08x = ", config.seek+i);
@ -113,101 +140,111 @@ void r_print_format(const u8* buf, int len, const char *fmt)
radare_seek(old, SEEK_SET);
}
break;
#endif
case 'e': {
double doub;
memcpy(&doub, buf+i, sizeof(double));
D cons_printf("%e = ", doub);
cons_printf("(double)");
r_cons_printf("%e = ", doub);
r_cons_printf("(double)");
i+=8;
}
break;
case 'q':
D cons_printf("0x%08x = ", config.seek+i);
cons_printf("(qword)");
r_cons_printf("0x%08x = ", seek+i);
r_cons_printf("(qword)");
i+=8;
break;
case 'b':
D cons_printf("0x%08x = ", config.seek+i);
cons_printf("%d ; 0x%02x ; '%c' ",
buf[i], buf[i], is_printable(buf[i])?buf[i]:0);
r_cons_printf("0x%08x = ", seek+i);
r_cons_printf("%d ; 0x%02x ; '%c' ",
buf[i], buf[i], IS_PRINTABLE(buf[i])?buf[i]:0);
i++;
break;
case 'B':
memset(buffer, '\0', 255);
radare_read_at((u64)addr, buffer, 248);
D cons_printf("0x%08x = ", config.seek+i);
for(j=0;j<10;j++) cons_printf("%02x ", buf[j]);
cons_strcat(" ... (");
if (p->read_at)
p->read_at((u64)addr, buffer, 248, p->user);
else {
printf("(cannot read memory)\n");
break;
}
r_cons_printf("0x%08x = ", seek+i);
for(j=0;j<10;j++) r_cons_printf("%02x ", buf[j]);
r_cons_strcat(" ... (");
for(j=0;j<10;j++)
if (is_printable(buf[j]))
cons_printf("%c", buf[j]);
cons_strcat(")");
if (IS_PRINTABLE(buf[j]))
r_cons_printf("%c", buf[j]);
r_cons_strcat(")");
i+=4;
break;
case 'd':
D cons_printf("0x%08x = ", config.seek+i);
cons_printf("%d", addr);
r_cons_printf("0x%08x = ", seek+i);
r_cons_printf("%d", addr);
i+=4;
break;
case 'x':
D cons_printf("0x%08x = ", config.seek+i);
cons_printf("0x%08x ", addr);
r_cons_printf("0x%08x = ", seek+i);
r_cons_printf("0x%08x ", addr);
i+=4;
break;
case 'X': {
u32 addr32 = (u32)addr;
char buf[128];
D cons_printf("0x%08x = ", config.seek+i);
cons_printf("0x%08llx ", addr32);
if (string_flag_offset(buf, (u64)addr32, -1))
cons_printf("; %s", buf);
//char buf[128];
r_cons_printf("0x%08x = ", seek+i);
r_cons_printf("0x%08llx ", addr32);
//if (string_flag_offset(buf, (u64)addr32, -1))
// r_cons_printf("; %s", buf);
i+=4;
} break;
case 'w':
case '1': // word (16 bits)
D cons_printf("0x%08x = ", config.seek+i);
r_cons_printf("0x%08x = ", seek+i);
if (endian)
addr = (*(buf+i))<<8 | (*(buf+i+1));
else addr = (*(buf+i+1))<<8 | (*(buf+i));
cons_printf("0x%04x ", addr);
r_cons_printf("0x%04x ", addr);
break;
case 'z': // zero terminated string
D cons_printf("0x%08x = ", config.seek+i);
r_cons_printf("0x%08x = ", seek+i);
for(;buf[i]&&i<len;i++) {
if (is_printable(buf[i]))
cons_printf("%c", buf[i]);
else cons_strcat(".");
if (IS_PRINTABLE(buf[i]))
r_cons_printf("%c", buf[i]);
else r_cons_strcat(".");
}
break;
case 'Z': // zero terminated wide string
D cons_printf("0x%08x = ", config.seek+i);
r_cons_printf("0x%08x = ", seek+i);
for(;buf[i]&&i<len;i+=2) {
if (is_printable(buf[i]))
cons_printf("%c", buf[i]);
else cons_strcat(".");
if (IS_PRINTABLE(buf[i]))
r_cons_printf("%c", buf[i]);
else r_cons_strcat(".");
}
cons_strcat(" ");
r_cons_strcat(" ");
break;
case 's':
D cons_printf("0x%08x = ", config.seek+i);
r_cons_printf("0x%08x = ", seek+i);
memset(buffer, '\0', 255);
radare_read_at((u64)addr, buffer, 248);
D cons_printf("0x%08x -> 0x%08x ", config.seek+i, addr);
cons_printf("%s ", buffer);
if (p->read_at)
p->read_at((u64)addr, buffer, 248, p->user);
else {
printf("(cannot read memory)\n");
break;
}
r_cons_printf("0x%08x -> 0x%08x ", seek+i, addr);
r_cons_printf("%s ", buffer);
i+=4;
break;
default:
/* ignore unknown chars */
continue;
}
D cons_newline();
r_cons_newline();
last = tmp;
}
if (otimes>1)
cons_printf("}\n");
r_cons_printf("}\n");
arg = orig;
idx=0;
idx = 0;
}
efree((void *)&args);
#endif
// free((void *)&args);
}

View File

@ -4,52 +4,78 @@
#include "r_print.h"
#include "r_util.h"
static int flags =
R_PRINT_FLAGS_COLOR |
R_PRINT_FLAGS_ADDRMOD;
void r_print_set_flags(int _flags)
int r_print_init(struct r_print_t *p)
{
flags = _flags;
if (p == NULL)
return R_FALSE;
/* read callback */
p->user = NULL;
p->read_at = NULL;
/* setup prefs */
p->width = 78;
p->cur_enabled = R_FALSE;
p->cur = p->ocur = -1;
p->addrmod = 4;
p->flags = \
R_PRINT_FLAGS_COLOR |
R_PRINT_FLAGS_HEADER |
R_PRINT_FLAGS_ADDRMOD;
return R_TRUE;
}
struct r_print_t *r_print_new()
{
struct r_print_t *p = MALLOC_STRUCT(struct r_print_t);
r_print_init(p);
return p;
}
void r_print_set_flags(struct r_print_t *p, int _flags)
{
p->flags = _flags;
}
struct r_print_t *r_print_free(struct r_print_t *p)
{
free(p);
return NULL;
}
// XXX this is not thread safe...store into r_print_t ?
static int r_print_cur_enabled = 0;
static int r_print_cur = -1;
static int r_print_ocur = -1;
void r_print_set_cursor(int enable, int ocursor, int cursor)
void r_print_set_cursor(struct r_print_t *p, int enable, int ocursor, int cursor)
{
r_print_cur_enabled = enable;
p->cur_enabled = enable;
//if (ocursor<0) ocursor=0;
r_print_ocur = ocursor;
p->ocur = ocursor;
if (cursor<0) cursor=0;
r_print_cur = cursor;
p->cur = cursor;
}
void r_print_cursor(int cur, int set)
void r_print_cursor(struct r_print_t *p, int cur, int set)
{
if (!r_print_cur_enabled)
if (!p->cur_enabled)
return;
if (r_print_ocur == -1) {
if (cur==r_print_cur)
r_cons_invert(set, flags&R_PRINT_FLAGS_COLOR);
if (p->ocur == -1) {
if (cur==p->cur)
r_cons_invert(set, p->flags&R_PRINT_FLAGS_COLOR);
} else {
int from = r_print_ocur;
int to = r_print_cur;
int from = p->ocur;
int to = p->cur;
r_num_minmax_swap_i(&from, &to);
if (cur>=from&&cur<=to)
r_cons_invert(set, flags&R_PRINT_FLAGS_COLOR);
r_cons_invert(set, p->flags&R_PRINT_FLAGS_COLOR);
}
}
void r_print_addr(u64 addr)
void r_print_addr(struct r_print_t *p, u64 addr)
{
//config_get_i("cfg.addrmod");
int mod = flags & R_PRINT_FLAGS_ADDRMOD;
int mod = p->flags & R_PRINT_FLAGS_ADDRMOD;
char ch = (0==(addr%(mod?mod:1)))?',':' ';
if (flags & R_PRINT_FLAGS_COLOR) {
if (p->flags & R_PRINT_FLAGS_COLOR) {
r_cons_printf("%s0x%08llx"C_RESET"%c ",
r_cons_palette[PAL_ADDRESS], addr, ch);
} else r_cons_printf("0x%08llx%c ", addr, ch);
@ -57,50 +83,57 @@ void r_print_addr(u64 addr)
void r_print_byte(int idx, u8 ch)
{
// if (flags & R_PRINT_FLAGS_CURSOR && idx == r_print_cur)
r_cons_printf("%c", ch);
// if (flags & R_PRINT_FLAGS_CURSOR && idx == p->cur)
r_cons_printf("%c", ch);
// else r_cons_printf("%c", ch);
}
void r_print_code(u64 addr, u8 *buf, int len, int step, int columns, int header)
void r_print_code(struct r_print_t *p, u64 addr, u8 *buf, int len)
{
int i, w = 0;
r_cons_printf("#define _BUFFER_SIZE %d\n", len);
r_cons_printf("unsigned char buffer[%d] = {", len);
for(i=0;i<len;i++) {
if (!(w%columns))
if (!(w%p->width))
r_cons_printf("\n ");
r_print_cursor(i, 1);
r_print_cursor(p, i, 1);
r_cons_printf("0x%02x, ", buf[i]);
r_print_cursor(i, 0);
r_print_cursor(p, i, 0);
w+=6;
}
r_cons_printf("};\n");
}
void r_print_string(u64 addr, u8 *buf, int len, int step, int columns, int header)
void r_print_string(struct r_print_t *p, u64 addr, u8 *buf, int len)
{
int i;
for(i=0;i<len;i++) {
r_print_cursor(i, 1);
r_print_cursor(p, i, 1);
if (IS_PRINTABLE(buf[i]))
r_cons_printf("%c", buf[i]);
else r_cons_printf("\\x%02x", buf[i]);
r_print_cursor(i, 0);
r_print_cursor(p, i, 0);
}
r_cons_newline();
}
static const char hex[16] = "0123456789ABCDEF";
void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int header)
// XXX: step is borken
void r_print_hexdump(struct r_print_t *p, u64 addr, u8 *buf, int len, int step)
{
int i,j,k,inc;
inc = 2+(int)((columns-14)/4);
if (p == NULL) {
// TODO: use defaults r_print_t (static one)
fprintf(stderr, "TODO: r_print_hexdump does not supports NULL as arg0\n");
return;
}
inc = 2 + (int)((p->width-14)/4);
if (inc%2) inc++;
inc = 16;
if (header) {
if (p->flags & R_PRINT_FLAGS_HEADER) {
// only for color..too many options .. brbr
r_cons_printf(r_cons_palette[PAL_HEADER]);
r_cons_strcat(" offset ");
@ -115,7 +148,7 @@ void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int head
}
for(i=0; i<len; i+=inc) {
r_print_addr(addr+(i*step));
r_print_addr(p, addr+(i*step));
for(j=i;j<i+inc;j++) {
if (j>=len) {
@ -123,9 +156,9 @@ void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int head
if (j%2) r_cons_printf(" ");
continue;
}
r_print_cursor(j, 1);
r_print_cursor(p, j, 1);
r_cons_printf("%02x", (u8)buf[j]);
r_print_cursor(j, 0);
r_print_cursor(p, j, 0);
//print_color_byte_i(j, "%02x", (unsigned char)buf[j]);
if (j%2) r_cons_strcat(" ");
}
@ -134,11 +167,11 @@ void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int head
if (j >= len)
r_cons_strcat(" ");
else {
r_print_cursor(j, 1);
r_print_cursor(p, j, 1);
r_cons_printf("%c",
(IS_PRINTABLE(buf[j]))?
buf[j] : '.');
r_print_cursor(j, 0);
r_print_cursor(p, j, 0);
}
}
r_cons_newline();
@ -146,7 +179,7 @@ void r_print_hexdump(u64 addr, u8 *buf, int len, int step, int columns, int head
}
}
void r_print_bytes(const u8* buf, int len, const char *fmt)
void r_print_bytes(struct r_print_t *p, const u8* buf, int len, const char *fmt)
{
int i;
for(i=0;i<len;i++)
@ -154,7 +187,7 @@ void r_print_bytes(const u8* buf, int len, const char *fmt)
r_cons_newline();
}
void r_print_raw(const u8* buf, int len)
void r_print_raw(struct r_print_t *p, const u8* buf, int len)
{
r_cons_memcat((char *)buf, len);
}

View File

@ -1,5 +1,13 @@
all: hex fmt
BIN=hex
OBJ=hex.o
BINDEPS=r_cons r_util r_line r_print
ftm: fmt.o
${CC} -I../../include fmt.o ../*.o -o fmt
myclean:
rm -f fmt fmt.o
include ../../rules.mk

View File

@ -3,10 +3,12 @@
int main()
{
struct r_print_t *p;
u8 *buf = (u8*)main;
r_cons_init();
r_print_hexdump((u64)(u32)(main), buf, 128, 1, 78, 1);
p = r_print_new();
r_print_hexdump(p, (u64)(u32)(main), buf, 128, 1);
r_cons_flush();
return 0;