Skip to content

Commit

Permalink
* Fix LIBR_PLUGINS in env.sh
Browse files Browse the repository at this point in the history
* Initial implementation of the visual mode in core
* Added pc and ps print formats (c code and string)
* Added '/' command in core (search hexa and str)
* Add '-d' to radare2 test program
* New r_file_path to resolve file path thru $PATH
* 'make install' works everywhere
  • Loading branch information
radare committed Feb 9, 2009
1 parent 389cce0 commit 9bd9cbe
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 27 deletions.
2 changes: 1 addition & 1 deletion env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

new_env='
LIBR_PLUGINS=$PATH:$PWD/prefix/lib/radare2
LIBR_PLUGINS=$PWD/prefix/lib/radare2
PATH=$PATH:$PWD/prefix/bin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/prefix/lib
'
Expand Down
4 changes: 2 additions & 2 deletions libr/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ install:
@for a in `find */t -perm /u+x -type f | grep 2`; \
do echo " $$a"; cp $$a ${PREFIX}/bin ; done
# plugins
@mkdir -p ${PREFIX}/plugins
@mkdir -p ${PREFIX}/lib/radare2
@for a in `find */p -perm /u+x -type f`; \
do echo " $$a"; cp $$a ${PREFIX}/plugins ; done
do echo " $$a"; cp $$a ${PREFIX}/lib/radare2 ; done
# test programs
@mkdir -p ${PREFIX}/bin-test
@for a in `find */t -perm /u+x -type f | grep -v 2`; \
Expand Down
13 changes: 8 additions & 5 deletions libr/cons/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ const char *r_cons_color_names[CONS_COLORS_SIZE+1] = {
NULL
};

void r_cons_invert(int set)
void r_cons_invert(int set, int color)
{
if (set)
r_cons_strcat("\x1b[7m");
else
r_cons_strcat("\x1b[0m");
if (color) {
if (set) r_cons_strcat("\x1b[7m");
else r_cons_strcat("\x1b[0m");
} else {
if (set) r_cons_strcat("[");
else r_cons_strcat("]");
}
}

const char *r_cons_colors[CONS_COLORS_SIZE+1] = {
Expand Down
5 changes: 2 additions & 3 deletions libr/cons/cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ void r_cons_gotoxy(int x, int y)

void r_cons_clear00()
{
r_cons_lines = 0;
r_cons_clear();
r_cons_gotoxy(0,0);
r_cons_gotoxy(0, 0);
}

void r_cons_clear()
Expand Down Expand Up @@ -137,14 +136,14 @@ void r_cons_reset()
if (r_cons_buffer)
r_cons_buffer[0] = '\0';
r_cons_buffer_len = 0;
r_cons_lines = 0;
}

const char *r_cons_get_buffer()
{
return r_cons_buffer;
}


void r_cons_grep(const char *str)
{
char *ptr, *ptr2, *ptr3;
Expand Down
2 changes: 2 additions & 0 deletions libr/cons/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ int r_cons_readchar()
return -1;
SetConsoleMode(h, mode);
#else
r_cons_set_raw(1);
if (read(0, buf, 1)==-1)
return -1;
r_cons_set_raw(0);
#endif
return buf[0];
}
85 changes: 78 additions & 7 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ static int cmd_seek(void *data, const char *input)
r_core_seek(core, off);
break;
case '+':
r_core_seek(core, core->seek + off);
if (input[1]=='+') r_core_seek(core, core->seek + core->blocksize);
else r_core_seek(core, core->seek + off);
break;
case '-':
r_core_seek(core, core->seek - off);
if (input[1]=='-') r_core_seek(core, core->seek - core->blocksize);
else r_core_seek(core, core->seek - off);
break;
case '?':
fprintf(stderr,
"Usage: s[+-] [addr]\n"
" s 0x320 ; seek to this address\n"
" s++ ; seek blocksize bytes forward\n"
" s-- ; seek blocksize bytes backward\n"
" s+ 512 ; seek 512 bytes forward\n"
" s- 512 ; seek 512 bytes backward\n");
break;
Expand Down Expand Up @@ -238,10 +242,17 @@ static int cmd_print(void *data, const char *input)
for(idx=ret=0; idx < len; idx+=ret) {
r_asm_set_pc(&a, a.pc + ret);
ret = r_asm_disasm(&a, buf+idx, len-idx);
r_cons_printf("0x%08llx %14s %s\n", core->seek+idx, a.buf_hex, a.buf_asm);
r_cons_printf("0x%08llx %14s %s\n",
core->seek+idx, a.buf_hex, a.buf_asm);
}
}
break;
case 's':
r_print_string(core->seek, core->block, len, 1, 78, 1);
break;
case 'c':
r_print_code(core->seek, core->block, len, 1, 78, 1);
break;
case 'r':
r_print_raw(core->block, len);
break;
Expand All @@ -252,9 +263,11 @@ static int cmd_print(void *data, const char *input)
r_print_bytes(core->block, len, "%02x");
break;
default:
fprintf(stderr, "Usage: p[8] [len]"
fprintf(stderr, "Usage: p[8] [len]\n"
" p8 [len] 8bit hexpair list of bytes\n"
" px [len] hexdump of N bytes\n"
" pc [len] output C format\n"
" ps [len] print string\n"
" pd [len] disassemble N bytes\n"
" pr [len] print N raw bytes\n");
break;
Expand Down Expand Up @@ -482,10 +495,68 @@ static int cmd_write(void *data, const char *input)
return 0;
}

static int __cb_hit(struct r_search_kw_t *kw, void *user, u64 addr)
{
r_cons_printf("f hit0_%d @ 0x%08llx\n", kw->count, addr);
return R_TRUE;
}

static int cmd_search(void *data, const char *input)
{
//struct r_core_t *core = (struct r_core_t *)data;
/* TODO */
struct r_core_t *core = (struct r_core_t *)data;
u64 at;
u32 n32;
int ret, dosearch = 0;
u8 *buf;
switch (input[0]) {
case '/':
r_search_initialize(core->search);
dosearch = 1;
break;
case 'v':
r_search_free(core->search);
core->search = r_search_new(R_SEARCH_KEYWORD);
n32 = r_num_math(&core->num, input+1);
r_search_kw_add_bin(core->search, &n32, 4, "",0);
r_search_initialize(core->search);
dosearch = 1;
break;
case ' ': /* search string */
r_search_free(core->search);
core->search = r_search_new(R_SEARCH_KEYWORD);
r_search_kw_add(core->search, input+1, "");
r_search_initialize(core->search);
dosearch = 1;
break;
case 'x': /* search hex */
r_search_free(core->search);
core->search = r_search_new(R_SEARCH_KEYWORD);
r_search_kw_add_hex(core->search, input+2, "");
r_search_initialize(core->search);
dosearch = 1;
break;
default:
r_cons_printf("Usage: /[x/] [arg]\n"
" / foo ; search for string 'foo'\n"
" /x ff0033 ; search for hex string\n"
" // ; repeat last search\n");
break;
}
if (dosearch) {
/* set callback */
/* TODO: handle last block of data */
/* TODO: handle ^C */
/* TODO: launch search in background support */
buf = (u8 *)malloc(core->blocksize);
r_search_set_callback(core->search, &__cb_hit, &core);
for(at = core->seek; at < core->file->size; at += core->blocksize) {
r_io_lseek(&core->io, core->file->fd, at, R_IO_SEEK_SET);
ret = r_io_read(&core->io, core->file->fd, buf, core->blocksize);
if (ret != core->blocksize)
break;
r_search_update(core->search, &at, buf, ret);
}
}
return R_TRUE;
}

Expand Down Expand Up @@ -567,7 +638,7 @@ static int cmd_hash(void *data, const char *input)
static int cmd_visual(void *data, const char *input)
{
struct r_core_t *core = (struct r_core_t *)data;
r_core_visual(core);
r_core_visual(core, input);
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions libr/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int r_core_init(struct r_core_t *core)
core->num.userptr = core;
r_lang_init(&core->lang);
r_cons_init();
core->search = r_search_new(R_SEARCH_KEYWORD);
r_io_init(&core->io);
r_macro_init(&core->macro);
core->macro.num = &core->num;
Expand Down
1 change: 1 addition & 0 deletions libr/core/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct r_core_file_t *r_core_file_open(struct r_core_t *r, const char *file, int
fh->filename = p+3;
fh->rwx = mode;
r->file = fh;
fh->size = r_io_size(&r->io, fd);
r_core_block_read(r, 0);
list_add(&(fh->list), &r->files);
return fh;
Expand Down
2 changes: 1 addition & 1 deletion libr/core/t/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
OBJ=radare2.o
BIN=radare2
BINDEPS=r_core r_cons r_macro r_util r_flags r_lib r_io r_hash r_cmd r_config r_asm r_lang r_debug r_print r_line r_bin
BINDEPS=r_core r_cons r_macro r_util r_flags r_lib r_io r_hash r_cmd r_config r_asm r_lang r_debug r_print r_line r_bin r_search

#LIBS=-lr_core -L..
### shared
Expand Down
36 changes: 34 additions & 2 deletions libr/core/t/radare2.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ struct r_core_t r;

int main_help(int line)
{
printf("Usage: radare2 [-wn] [-e k=v] [file]\n");
printf("Usage: radare2 [-dwn] [-e k=v] [file] [...]\n");
if (!line) printf(
" -d use 'file' as a program to debug\n"
" -w open file in write mode\n"
" -n do not run ~/.radarerc\n"
" -e k=v evaluate config var\n");
Expand All @@ -22,16 +23,20 @@ int main(int argc, char **argv)
struct r_core_file_t *fh;
int c, perms = R_IO_READ;
int run_rc = 1;
int debug = 0;

if (argc<2)
return main_help(1);

r_core_init(&r);

while((c = getopt(argc, argv, "when"))!=-1) {
while((c = getopt(argc, argv, "whend"))!=-1) {
switch(c) {
case 'h':
return main_help(0);
case 'd':
debug = 1;
break;
case 'e':
r_config_eval(&r.config, optarg);
break;
Expand All @@ -46,6 +51,33 @@ int main(int argc, char **argv)
}
}

if (debug) {
char file[1024];

strcpy(file, "dbg://");
if (optind<argc) {
char *ptr = r_file_path(argv[optind]);
if (ptr) {
strcat(file, ptr);
free(ptr);
optind++;
}
}
while (optind<argc) {
strcat(file, argv[optind]);
strcat(file, " ");
optind++;
if (optind!=argc)
strcat(file, " ");
}
fh = r_core_file_open(&r, file, perms);
if (fh == NULL) {
fprintf(stderr,
"Cannot open file '%s'\n", file);
return 1;
}

} else
while (optind<argc) {
const char *file = argv[optind];
fh = r_core_file_open(&r, argv[optind++], perms);
Expand Down
Loading

0 comments on commit 9bd9cbe

Please sign in to comment.