* Add URL for agc graph

* Add r_line_get_prompt()
  - _set_prompt() uses strdup now
  - a bit slower but more consistent
* Use r_line from r_lang
  - Added history in #!<lang> prompt
  - Fix r_lang plugin init from r2-bindings-python-threads
  - Better help message in lang ? prompt
* Fix r_cons control+arrow issue
  - resets terminal as raw
  - History data is initialized if needed
* Fix sys/swig.sh for latest OSX-ports
* Add missing get_fcn_at() method in RAnal vapi
This commit is contained in:
pancake 2011-11-01 04:37:13 +01:00
parent 5e5b3ae7b6
commit cb053b0cad
16 changed files with 209 additions and 118 deletions

View File

@ -165,12 +165,11 @@ static int cmpaddr (void *_a, void *_b) {
}
R_API int r_core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth) {
RAnalFcn *fcn, *fcni;
struct r_anal_ref_t *refi;
RListIter *iter, *iter2;
RAnalRef *ref;
ut8 *buf;
int buflen, fcnlen = 0;
RAnalFcn *fcn, *fcni;
RAnalRef *ref, *refi;
ut8 *buf;
if (depth < 0)
return R_FALSE;
@ -289,10 +288,11 @@ R_API void r_core_anal_refs(RCore *core, ut64 addr, int gv) {
// TODO: display only code or data refs?
RFlagItem *flag = r_flag_get_i (core->flags, fcnr->addr);
if (gv) r_cons_printf ("\t\"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" "
"[label=\"%s\" color=\"%s\"];\n",
"[label=\"%s\" color=\"%s\" URL=\"%s/0x%08"PFMT64x"\"];\n",
fcni->addr, fcnr->addr, flag?flag->name:"",
(fcnr->type==R_ANAL_REF_TYPE_CODE ||
fcnr->type==R_ANAL_REF_TYPE_CALL)?"green":"red");
fcnr->type==R_ANAL_REF_TYPE_CALL)?"green":"red",
flag?flag->name:"", fcnr->addr);
else r_cons_printf (" - 0x%08"PFMT64x" (%c)\n", fcnr->addr, fcnr->type);
}
}

View File

@ -4390,8 +4390,8 @@ static int r_core_cmd_subst(RCore *core, char *cmd) {
ptr[0] = '\0';
if (ptr[1]=='<') {
/* this is a bit mess */
const char *oprompt = r_line_singleton ()->prompt;
oprompt = ">";
//const char *oprompt = strdup (r_line_singleton ()->prompt);
//oprompt = ">";
for (str=ptr+2; str[0]==' '; str++);
eprintf ("==> Reading from stdin until '%s'\n", str);
free (core->oobi);
@ -4413,7 +4413,7 @@ static int r_core_cmd_subst(RCore *core, char *cmd) {
break;
strcat ((char *)core->oobi, buf);
}
r_line_singleton ()->prompt = oprompt;
//r_line_set_prompt (oprompt);
} else {
for (str=ptr+1; *str== ' ';str++);
eprintf ("SLURPING FILE '%s'\n", str);

View File

@ -438,7 +438,7 @@ R_API int r_core_prompt(RCore *r, int sync) {
Color_YELLOW"[0x%08"PFMT64x"]> "Color_RESET, r->offset);
#endif
else sprintf (prompt, "[0x%08"PFMT64x"]> ", r->offset);
r_line_singleton()->prompt = prompt;
r_line_set_prompt (prompt);
ret = r_cons_fgets (line, sizeof (line), 0, NULL);
if (ret == -2) return R_CORE_CMD_EXIT;
if (ret == -1) return R_FALSE;

View File

@ -37,7 +37,7 @@ typedef struct r_line_t {
RLineBuffer buffer;
int echo;
int has_echo;
const char *prompt;
char *prompt;
char *clipboard;
int disable;
void *user;
@ -49,6 +49,7 @@ typedef struct r_line_t {
R_API RLine *r_line_new ();
R_API RLine *r_line_singleton ();
R_API void r_line_free ();
R_API char *r_line_get_prompt ();
R_API void r_line_set_prompt (const char *prompt);
R_API int r_line_hist_load(const char *file);

View File

@ -164,26 +164,9 @@ err_fork:
}
#else
#if 0
static int __waitpid(int pid) {
int st = 0;
if (waitpid (pid, &st, 0) == -1)
return R_FALSE;
if (WIFEXITED (st)) {
//if ((WEXITSTATUS(wait_val)) != 0) {
perror ("==> Process has exited\n");
//debug_exit();
return -1;
}
return R_TRUE;
}
#endif
static int fork_and_ptraceme(const char *cmd) {
char **argv;
int ret, status, pid = -1;
pid = vfork ();
int ret, status, pid = vfork ();
switch (pid) {
case -1:
perror ("fork_and_ptraceme");
@ -236,7 +219,7 @@ static int __plugin_open(struct r_io_t *io, const char *file) {
}
static RIODesc *__open(RIO *io, const char *file, int rw, int mode) {
char uri[1024];
char uri[128];
if (__plugin_open (io, file)) {
int pid = atoi (file+6);
if (pid == 0) {

View File

@ -1,5 +1,5 @@
NAME=r_lang
OBJ=lang.o
DEPS=r_util r_lib
DEPS=r_util r_lib r_line
include ../rules.mk

View File

@ -82,13 +82,13 @@ R_API int r_lang_setup(RLang *lang) {
R_API void r_lang_plugin_free (RLang *lang, RLangPlugin *p) {
if (p && p->fini)
p->fini (lang->user);
p->fini (lang);
}
R_API int r_lang_add(RLang *lang, RLangPlugin *foo) {
if (foo && (!r_lang_get (lang, foo->name))) {
if (foo->init)
foo->init (lang->user);
foo->init (lang);
r_list_append (lang->langs, foo);
}
return R_TRUE;
@ -108,9 +108,8 @@ R_API RLangPlugin *r_lang_get (RLang *lang, const char *name) {
RListIter *iter;
RLangPlugin *h;
r_list_foreach (lang->langs, iter, h) {
if (!strcmp (h->name, name)) {
if (!strcmp (h->name, name))
return h;
}
}
return NULL;
}
@ -163,26 +162,56 @@ R_API int r_lang_prompt(RLang *lang) {
return R_FALSE;
if (lang->cur->prompt)
if (lang->cur->prompt(lang) == R_TRUE)
if (lang->cur->prompt (lang) == R_TRUE)
return R_TRUE;
/* init line */
RLine *line = r_line_singleton ();
RLineHistory hist = line->history;
RLineHistory histnull = {0};
RLineCompletion oc = line->completion;
RLineCompletion ocnull = {0};
char *prompt = strdup (line->prompt);
line->completion = ocnull;
line->history = histnull;
/* foo */
for (;;) {
snprintf (buf, sizeof (buf)-1, "%s> ", lang->cur->name);
r_line_set_prompt (buf);
#if 0
printf ("%s> ", lang->cur->name);
fflush (stdout);
fgets (buf, sizeof (buf)-1, stdin);
if (feof (stdin)) break;
buf[strlen (buf)-1]='\0';
if (*buf) buf[strlen (buf)-1]='\0';
#endif
char *p = r_line_readline ();
if (!p) break;
r_line_hist_add (p);
strcpy (buf, p);
if (!strcmp (buf, "q"))
return R_TRUE;
if (!strcmp (buf, "?")) {
if (lang->cur) {
printf ("Help for %s scripting prompt:\n", lang->cur->name);
if (lang->cur->help)
printf ("%s", *lang->cur->help);
} else printf ("no selected r_lang plugin\n");
printf (" ? - show this help message\n"
RLangDef *def;
RListIter *iter;
eprintf(" ? - show this help message\n"
" q - quit\n");
if (lang->cur) {
eprintf ("%s example:\n", lang->cur->name);
if (lang->cur->help)
eprintf ("%s", *lang->cur->help);
} else eprintf ("no selected r_lang plugin\n");
if (!r_list_empty (lang->defs))
eprintf ("variables:\n");
r_list_foreach (lang->defs, iter, def) {
eprintf (" %s %s\n", def->type, def->name);
}
} else r_lang_run (lang, buf, strlen (buf));
}
// XXX: leaking history
r_line_set_prompt (prompt);
line->completion = oc;
line->history = hist;
clearerr (stdin);
printf ("\n");
return R_TRUE;

View File

@ -33,45 +33,62 @@ static void perl_radare_cmd(pTHX_ CV* cv) {
str = (char *)(size_t)items; /* dummy unreachable code */
}
static void xs_init(pTHX)
{
newXS("r", perl_radare_cmd, __FILE__);
static void xs_init(pTHX) {
newXS ("r", perl_radare_cmd, __FILE__);
}
static int init(struct r_lang_t *lang)
{
static int init(struct r_lang_t *lang) {
char *perl_embed[] = { "", "-e", "0" };
core = lang->user;
my_perl = perl_alloc();
my_perl = perl_alloc ();
if (my_perl == NULL) {
printf("Cannot init perl module\n");
return R_FALSE;
}
perl_construct(my_perl);
perl_parse(my_perl, xs_init, 3, perl_embed, (char **)NULL);
perl_construct (my_perl);
perl_parse (my_perl, xs_init, 3, perl_embed, (char **)NULL);
return R_TRUE;
}
static int fini(void *user)
{
perl_destruct(my_perl);
perl_free(my_perl);
static int fini(void *user) {
perl_destruct (my_perl);
perl_free (my_perl);
my_perl = NULL;
return R_TRUE;
}
static int run(void *user, const char *code, int len)
{
static int run(void *user, const char *code, int len) {
/* TODO: catcth errors */
eval_pv(code, TRUE);
eval_pv (code, TRUE);
return R_TRUE;
}
static int setargv(void *user, int argc, char **argv)
{
perl_parse(my_perl, xs_init, argc, argv, (char **)NULL);
static int setargv(void *user, int argc, char **argv) {
perl_parse (my_perl, xs_init, argc, argv, (char **)NULL);
return R_TRUE;
}
static int setup(RLang *lang) {
RListIter *iter;
RLangDef *def;
char cmd[128];
// Segfault if already initialized ?
//PyRun_SimpleString ("require r2/r_core.pl");
#warning TODO: implement setup in lang/perl
core = lang->user;
r_list_foreach (lang->defs, iter, def) {
if (!def->type || !def->name)
continue;
if (!strcmp (def->type, "int"))
snprintf (cmd, sizeof (cmd), "%s=%d", def->name, (int)(size_t)def->value);
else if (!strcmp (def->type, "string"))
snprintf (cmd, sizeof (cmd), "%s=\"%s\"", def->name, (char *)def->value);
else snprintf (cmd, sizeof (cmd), "%s=%s.cast(%p)",
def->name, def->type, def->value);
// PyRun_SimpleString (cmd);
}
return R_TRUE;
}
@ -83,6 +100,7 @@ static struct r_lang_plugin_t r_lang_plugin_perl = {
.name = "perl",
.desc = "Perl language extension",
.init = &init,
.setup = &setup,
.fini = (void *)&fini,
.help = (void *)&help,
.prompt = NULL,

View File

@ -22,22 +22,22 @@ static int run(RLang *lang, const char *code, int len) {
}
static int slurp_python(const char *file) {
FILE *fd = fopen(file, "r");
FILE *fd = fopen (file, "r");
if (fd == NULL)
return R_FALSE;
PyRun_SimpleFile(fd, file);
fclose(fd);
PyRun_SimpleFile (fd, file);
fclose (fd);
return R_TRUE;
}
static int run_file(struct r_lang_t *lang, const char *file) {
return slurp_python(file);
return slurp_python (file);
}
/* init */
typedef struct {
PyObject_HEAD
PyObject *first; /* first name */
PyObject *first; /* first name */
PyObject *last; /* last name */
int number;
} Radare;
@ -46,48 +46,39 @@ typedef struct {
static char *py_nullstr = "";
static void Radare_dealloc(Radare* self) {
Py_XDECREF(self->first);
Py_XDECREF(self->last);
Py_XDECREF (self->first);
Py_XDECREF (self->last);
//self->ob_type->tp_free((PyObject*)self);
}
static PyObject * Radare_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
Radare *self;
self = (Radare *)type->tp_alloc(type, 0);
Radare *self = (Radare *)type->tp_alloc (type, 0);
if (self != NULL) {
self->first = PyString_FromString("");
self->first = PyString_FromString ("");
if (self->first == NULL) {
Py_DECREF(self);
Py_DECREF (self);
return NULL;
}
self->last = PyString_FromString("");
self->last = PyString_FromString ("");
if (self->last == NULL) {
Py_DECREF(self);
Py_DECREF (self);
return NULL;
}
self->number = 0;
}
return (PyObject *)self;
}
static PyObject * Radare_cmd(Radare* self, PyObject *args) {
static PyObject *Radare_cmd(Radare* self, PyObject *args) {
PyObject *result;
char *str, *cmd = NULL;
if (!PyArg_ParseTuple(args, "s", &cmd))
if (!PyArg_ParseTuple (args, "s", &cmd))
return NULL;
str = r_core_cmd_str (core, cmd);
if (str == NULL)
str = py_nullstr;
result = PyString_FromString(str);
return result;
return PyString_FromString (str? str: py_nullstr);
}
static int Radare_init(Radare *self, PyObject *args, PyObject *kwds) {
@ -95,22 +86,22 @@ static int Radare_init(Radare *self, PyObject *args, PyObject *kwds) {
static char *kwlist[] = {"first", "last", "number", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OOi",
if (!PyArg_ParseTupleAndKeywords (args, kwds, "|OOi",
kwlist, &first, &last, &self->number))
return -1;
if (first) {
tmp = self->first;
Py_INCREF(first);
Py_INCREF (first);
self->first = first;
Py_XDECREF(tmp);
Py_XDECREF (tmp);
}
if (last) {
tmp = self->last;
Py_INCREF(last);
Py_INCREF (last);
self->last = last;
Py_XDECREF(tmp);
Py_XDECREF (tmp);
}
return 0;
@ -134,10 +125,10 @@ static PyMethodDef Radare_methods[] = {
};
static PyTypeObject RadareType = {
PyObject_HEAD_INIT(NULL)
PyObject_HEAD_INIT (NULL)
0, /*ob_size*/
"radare.RadareInternal", /*tp_name*/
sizeof(Radare), /*tp_basicsize*/
sizeof (Radare), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Radare_dealloc,/*tp_dealloc*/
0, /*tp_print*/
@ -179,8 +170,7 @@ static void init_radare_module(void) {
PyObject* m;
if (PyType_Ready (&RadareType) < 0)
return;
m = Py_InitModule3 ("r", Radare_methods, //module_methods,
"Example module that creates an extension type.");
m = Py_InitModule3 ("r", Radare_methods, "radare python extension");
}
#else
@ -223,7 +213,9 @@ static int setup(RLang *lang) {
RListIter *iter;
RLangDef *def;
char cmd[128];
// Segfault if already initialized ?
PyRun_SimpleString ("from r2.r_core import RCore");
core = lang->user;
r_list_foreach (lang->defs, iter, def) {
if (!def->type || !def->name)
continue;
@ -240,6 +232,10 @@ static int setup(RLang *lang) {
static int init(RLang *lang) {
core = lang->user;
// DO NOT INITIALIZE MODULE IF ALREADY INITIALIZED
if (Py_IsInitialized ()) {
return 0;
}
Py_Initialize ();
init_radare_module ();
return R_TRUE;
@ -250,9 +246,8 @@ static int fini(void *user) {
}
static const char *help =
"Python plugin usage:\n"
//" r = new RadareInternal()\n"
" bytes = r.cmd(\"p8 10\");\n";
" print r.cmd(\"p8 10\");\n";
struct r_lang_plugin_t r_lang_plugin_python = {
.name = "python",

View File

@ -16,15 +16,21 @@
static char *r_line_nullstr = "";
/* initialize history stuff */
R_API int r_line_dietline_init() {
static int inithist() {
ZERO_FILL (&I.history);
ZERO_FILL (&I.completion);
I.history.data = (char **)malloc ((I.history.size+1024)*sizeof(char *));
if (I.history.data==NULL)
return R_FALSE;
I.history.size = R_LINE_HISTSIZE;
memset (I.history.data, 0, I.history.size*sizeof(char *));
return R_TRUE;
}
/* initialize history stuff */
R_API int r_line_dietline_init() {
ZERO_FILL (&I.completion);
if (!inithist ())
return R_FALSE;
I.echo = R_TRUE;
return R_TRUE;
}
@ -51,9 +57,11 @@ static int r_line_readchar() {
return 0; // read no char
if (ret == 0) // EOF
return -1;
//eprintf ("(((%x)))\n", *buf);
// TODO: add support for other invalid chars
if (*buf==0xc2 || *buf==0xc3) {
read (0, buf+1, 1);
//eprintf ("(((%x)))\n", buf[1]);
*buf = '\0';
}
} while (*buf == '\0');
@ -62,6 +70,8 @@ static int r_line_readchar() {
}
R_API int r_line_hist_add(const char *line) {
if (!I.history.data)
inithist ();
if (I.history.top>=I.history.size)
I.history.top = I.history.index = 0; // workaround
if (line && *line) { // && I.history.index < I.history.size) {
@ -73,6 +83,8 @@ R_API int r_line_hist_add(const char *line) {
}
static int r_line_hist_up() {
if (!I.history.data)
inithist ();
if (I.history.index>0) {
strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE-1);
I.buffer.index = I.buffer.length = strlen (I.buffer.data);
@ -83,6 +95,8 @@ static int r_line_hist_up() {
static int r_line_hist_down() {
I.buffer.index = 0;
if (!I.history.data)
inithist ();
if (I.history.index<I.history.size) {
if (I.history.data[I.history.index] == NULL) {
I.buffer.data[0]='\0';
@ -98,6 +112,8 @@ static int r_line_hist_down() {
R_API int r_line_hist_list() {
int i = 0;
if (!I.history.data)
inithist ();
if (I.history.data != NULL)
for (i=0; i<I.history.size && I.history.data[i]; i++)
printf ("%.3d %s\n", i, I.history.data[i]);
@ -144,7 +160,7 @@ R_API int r_line_hist_save(const char *file) {
char *path = r_str_home (file);
if (path != NULL) {
fd = fopen (path, "w");
if (fd != NULL) {
if (fd != NULL && I.history.data) {
for (i=0; i<I.history.index; i++) {
fputs (I.history.data[i], fd);
fputs ("\n", fd);
@ -392,7 +408,7 @@ R_API char *r_line_readline() {
if (buf[1] == -1)
return NULL;
if (buf[0]==0x5b) {
switch(buf[1]) {
switch (buf[1]) {
case 0x33: // supr
if (I.buffer.index<I.buffer.length)
memmove (I.buffer.data+I.buffer.index,
@ -414,15 +430,51 @@ R_API char *r_line_readline() {
gcomp_idx--;
} else r_line_hist_down ();
break;
case 0x43:
case 0x43: // end
I.buffer.index = I.buffer.index<I.buffer.length?
I.buffer.index+1: I.buffer.length;
break;
case 0x44:
I.buffer.index = I.buffer.index?I.buffer.index-1:0;
case 0x44: // begin
I.buffer.index = I.buffer.index? I.buffer.index-1: 0;
break;
case 0x31:
case 0x31: // control + arrow
r_cons_readchar ();
r_cons_readchar ();
ch = r_cons_readchar ();
switch (ch) {
case 0x41:
//first
I.buffer.index = 0;
break;
case 0x44:
// previous word
for (i=I.buffer.index; i>0; i--) {
if (I.buffer.data[i] == ' ') {
I.buffer.index = i-1;
break;
}
}
if (I.buffer.data[i] != ' ')
I.buffer.index = 0;
break;
case 0x42:
//end
I.buffer.index = I.buffer.length;
break;
case 0x43:
// next word
for (i=I.buffer.index; i<I.buffer.length; i++) {
if (I.buffer.data[i] == ' ') {
I.buffer.index = i+1;
break;
}
}
if (I.buffer.data[i] != ' ')
I.buffer.index = I.buffer.length;
break;
}
r_cons_set_raw (1);
break;
case 0x48: // Start
I.buffer.index = 0;
break;
@ -511,7 +563,7 @@ R_API char *r_line_readline() {
} else {
printf ("\r%s%s", I.prompt, I.buffer.data);
printf ("\r%s", I.prompt);
for (i=0;i<I.buffer.index;i++)
for (i=0; i<I.buffer.index; i++)
printf ("%c", I.buffer.data[i]);
}
fflush (stdout);

View File

@ -29,7 +29,13 @@ R_API void r_line_free () {
// handle const or dynamic prompts?
R_API void r_line_set_prompt (const char *prompt) {
I.prompt = prompt;
free (I.prompt);
I.prompt = strdup (prompt);
}
// handle const or dynamic prompts?
R_API char *r_line_get_prompt () {
return strdup (I.prompt);
}
#include "dietline.c"

View File

@ -1,5 +1,6 @@
#!/usr/bin/python
import os,sys
p='.'.join('/'.join(os.__file__.split('/')[:-1]).split('.')[:-1])
try: print([x for x in sys.path if x.find(p)!=-1 and x[-9:]=="-packages"][0])
except: print([x for x in sys.path if x[-9:]=="-packages"][0])
from distutils.sysconfig import get_python_lib;print(get_python_lib())
#import os,sys
#p='.'.join('/'.join(os.__file__.split('/')[:-1]).split('.')[:-1])
#try: print([x for x in sys.path if x.find(p)!=-1 and x[-9:]=="-packages"][0])
#except: print([x for x in sys.path if x[-9:]=="-packages"][0])

View File

@ -1,6 +1,6 @@
from r_asm import *
def disasm(a, arch, op):
def ass(a, arch, op):
print "---------------------------->8- - - - - -"
print "OPCODE: %s"%op
a.use (arch)
@ -12,5 +12,5 @@ def disasm(a, arch, op):
print "HEX: %s"%code.buf_hex
a = RAsm()
disasm (a, 'x86.olly', 'mov eax, 33')
disasm (a, 'java', 'bipush 33')
ass (a, 'x86.olly', 'mov eax, 33')
ass (a, 'java', 'bipush 33')

View File

@ -1,8 +1,9 @@
import sys
from r2.r_core import *
from r2.r_cons import *
core = RCore()
core.file_open("/bin/ls", False, 0)
core.cmd0("pd 8");
RCons.flush()
#
core.cons.flush()

View File

@ -27,10 +27,13 @@ namespace Radare {
public bool set_bits (int bits);
public bool set_big_endian (bool big);
//public bool set_pc (uint64 addr);
public RList<RAnal.Fcn> get_fcns();
public void diff_setup(bool doops, double thbb, double thfcn);
public void diff_setup_i(bool doops, int thbb, int thfcn);
public RList<RAnal.Fcn> get_fcns();
public Fcn get_fcn_at (uint64 addr);
public void trace_bb (uint64 addr);
[Compact]
[CCode (cname="RAnalValue")]
public class Value {

View File

@ -5,6 +5,8 @@ cd `dirname $PWD/$0`
if [ -x /opt/local/bin/port ]; then
sudo port install i386-mingw32-gcc
sudo port install swig
sudo port install swig-python
elif [ -x /usr/bin/pacman ]; then
sudo pacman -S swig
elif [ -x /usr/bin/apt-get ]; then