2015-01-05 04:10:55 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2015 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-04-15 18:24:19 +00:00
|
|
|
#include <r_core.h>
|
2010-08-22 19:42:08 +00:00
|
|
|
#include <r_socket.h>
|
2009-04-11 21:22:20 +00:00
|
|
|
#include "../config.h"
|
2011-04-01 16:33:26 +00:00
|
|
|
#if __UNIX__
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2014-04-05 12:16:08 +00:00
|
|
|
#define DB core->sdb
|
|
|
|
|
2013-06-14 00:51:33 +00:00
|
|
|
R_LIB_VERSION(r_core);
|
|
|
|
|
2015-10-08 18:00:31 +00:00
|
|
|
static ut64 letter_divs[R_CORE_ASMQJMPS_LEN_LETTERS - 1] = {
|
|
|
|
R_CORE_ASMQJMPS_LETTERS * R_CORE_ASMQJMPS_LETTERS * R_CORE_ASMQJMPS_LETTERS * R_CORE_ASMQJMPS_LETTERS,
|
|
|
|
R_CORE_ASMQJMPS_LETTERS * R_CORE_ASMQJMPS_LETTERS * R_CORE_ASMQJMPS_LETTERS,
|
|
|
|
R_CORE_ASMQJMPS_LETTERS * R_CORE_ASMQJMPS_LETTERS,
|
|
|
|
R_CORE_ASMQJMPS_LETTERS
|
|
|
|
};
|
|
|
|
|
2015-10-18 13:49:46 +00:00
|
|
|
#define TMP_ARGV_SZ 512
|
|
|
|
static const char *tmp_argv[TMP_ARGV_SZ];
|
|
|
|
static bool tmp_argv_heap = false;
|
|
|
|
|
|
|
|
static void r_core_free_autocomplete(RCore *core) {
|
|
|
|
int i;
|
2015-12-27 01:33:53 +00:00
|
|
|
RLine *line;
|
|
|
|
if (!core || !core->cons || !core->cons->line)
|
|
|
|
return;
|
|
|
|
line = core->cons->line;
|
2015-10-18 13:49:46 +00:00
|
|
|
if (tmp_argv_heap) {
|
|
|
|
int argc = line->completion.argc;
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
free ((char*)tmp_argv[i]);
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
}
|
|
|
|
tmp_argv_heap = false;
|
|
|
|
}
|
|
|
|
line->completion.argc = 0;
|
|
|
|
line->completion.argv = tmp_argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-16 01:52:26 +00:00
|
|
|
static int on_fcn_new(void *_anal, void* _user, RAnalFunction *fcn) {
|
|
|
|
RCore *core = (RCore*)_user;
|
|
|
|
const char *cmd = r_config_get (core->config, "cmd.fcn.new");
|
|
|
|
if (cmd && *cmd) {
|
|
|
|
ut64 oaddr = core->offset;
|
|
|
|
ut64 addr = fcn->addr;
|
|
|
|
r_core_seek (core, addr, 1);
|
|
|
|
r_core_cmd0 (core, cmd);
|
|
|
|
r_core_seek (core, oaddr, 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int on_fcn_delete (void *_anal, void* _user, RAnalFunction *fcn) {
|
|
|
|
RCore *core = (RCore*)_user;
|
|
|
|
const char *cmd = r_config_get (core->config, "cmd.fcn.delete");
|
|
|
|
if (cmd && *cmd) {
|
|
|
|
ut64 oaddr = core->offset;
|
|
|
|
ut64 addr = fcn->addr;
|
|
|
|
r_core_seek (core, addr, 1);
|
|
|
|
r_core_cmd0 (core, cmd);
|
|
|
|
r_core_seek (core, oaddr, 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int on_fcn_rename(void *_anal, void* _user, RAnalFunction *fcn, const char *oname) {
|
|
|
|
RCore *core = (RCore*)_user;
|
|
|
|
const char *cmd = r_config_get (core->config, "cmd.fcn.rename");
|
|
|
|
if (cmd && *cmd) {
|
|
|
|
// XXX: wat do with old name here?
|
|
|
|
ut64 oaddr = core->offset;
|
|
|
|
ut64 addr = fcn->addr;
|
|
|
|
r_core_seek (core, addr, 1);
|
|
|
|
r_core_cmd0 (core, cmd);
|
|
|
|
r_core_seek (core, oaddr, 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-30 21:58:51 +00:00
|
|
|
static void r_core_debug_breakpoint_hit(RCore *core, RBreakpointItem *bpi) {
|
|
|
|
const char *cmdbp;
|
|
|
|
int oecho = core->cons->echo; // should be configurable by user?
|
|
|
|
core->cons->echo = 1; // should be configurable by user?
|
|
|
|
cmdbp = r_config_get (core->config, "cmd.bp");
|
|
|
|
if (cmdbp && *cmdbp)
|
|
|
|
r_core_cmd0 (core, cmdbp);
|
|
|
|
r_core_cmd0 (core, bpi->data);
|
|
|
|
core->cons->echo = oecho;
|
|
|
|
}
|
|
|
|
|
2015-10-08 18:00:31 +00:00
|
|
|
/* returns the address of a jmp/call given a shortcut by the user or UT64_MAX
|
|
|
|
* if there's no valid shortcut. When is_asmqjmps_letter is true, the string
|
|
|
|
* should be of the form XYZWu, where XYZW are uppercase letters and u is a
|
|
|
|
* lowercase one. If is_asmqjmps_letter is false, the string should be a number
|
|
|
|
* between 1 and 9 included. */
|
|
|
|
R_API ut64 r_core_get_asmqjmps(RCore *core, const char *str) {
|
|
|
|
if (!core->asmqjmps) return UT64_MAX;
|
|
|
|
|
|
|
|
if (core->is_asmqjmps_letter) {
|
|
|
|
int i, pos = 0;
|
|
|
|
int len = strlen (str);
|
|
|
|
|
|
|
|
for (i = 0; i < len - 1; ++i) {
|
2015-10-14 02:36:58 +00:00
|
|
|
if (!isupper ((ut8)str[i])) return UT64_MAX;
|
2015-10-08 18:00:31 +00:00
|
|
|
pos *= R_CORE_ASMQJMPS_LETTERS;
|
|
|
|
pos += str[i] - 'A' + 1;
|
|
|
|
}
|
2015-10-14 02:36:58 +00:00
|
|
|
if (!islower ((ut8)str[i])) return UT64_MAX;
|
2015-10-08 18:00:31 +00:00
|
|
|
pos *= R_CORE_ASMQJMPS_LETTERS;
|
|
|
|
pos += str[i] - 'a';
|
2015-10-28 20:07:11 +00:00
|
|
|
if (pos < core->asmqjmps_count) return core->asmqjmps[pos + 1];
|
2015-10-08 18:00:31 +00:00
|
|
|
} else if (str[0] > '0' && str[1] <= '9') {
|
2015-10-29 07:41:24 +00:00
|
|
|
int pos = str[0] - '0';
|
|
|
|
if (pos <= core->asmqjmps_count) return core->asmqjmps[pos];
|
2015-10-08 18:00:31 +00:00
|
|
|
}
|
|
|
|
return UT64_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns in str a string that represents the shortcut to access the asmqjmp
|
|
|
|
* at position pos. When is_asmqjmps_letter is true, pos is converted into a
|
|
|
|
* multiletter shortcut of the form XYWZu and returned (see r_core_get_asmqjmps
|
|
|
|
* for more info). Otherwise, the shortcut is the string representation of pos. */
|
|
|
|
R_API void r_core_set_asmqjmps(RCore *core, char *str, size_t len, int pos) {
|
|
|
|
if (core->is_asmqjmps_letter) {
|
|
|
|
int i, j = 0;
|
|
|
|
|
|
|
|
pos -= 1;
|
|
|
|
for (i = 0; i < R_CORE_ASMQJMPS_LEN_LETTERS - 1; ++i) {
|
|
|
|
ut64 div = pos / letter_divs[i];
|
|
|
|
pos %= letter_divs[i];
|
|
|
|
if (div != 0 && j < len) {
|
|
|
|
str[j++] = 'A' + div - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j < len) {
|
|
|
|
ut64 div = pos % R_CORE_ASMQJMPS_LETTERS;
|
|
|
|
str[j++] = 'a' + div;
|
|
|
|
}
|
|
|
|
str[j] = '\0';
|
|
|
|
} else {
|
|
|
|
snprintf (str, len, "%d", pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 21:58:51 +00:00
|
|
|
R_API int r_core_bind(RCore *core, RCoreBind *bnd) {
|
|
|
|
bnd->core = core;
|
|
|
|
bnd->bphit = (RCoreDebugBpHit)r_core_debug_breakpoint_hit;
|
|
|
|
bnd->cmd = (RCoreCmd)r_core_cmd0;
|
|
|
|
bnd->cmdstr = (RCoreCmdStr)r_core_cmd_str;
|
|
|
|
bnd->puts = (RCorePuts)r_cons_strcat;
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2014-10-30 21:58:51 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 00:45:12 +00:00
|
|
|
R_API RCore *r_core_ncast(ut64 p) {
|
|
|
|
return (RCore*)(size_t)p;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RCore *r_core_cast(void *p) {
|
|
|
|
return (RCore*)p;
|
|
|
|
}
|
|
|
|
|
2011-08-27 18:25:37 +00:00
|
|
|
static int core_cmd_callback (void *user, const char *cmd) {
|
2011-08-04 19:28:07 +00:00
|
|
|
RCore *core = (RCore *)user;
|
|
|
|
return r_core_cmd0 (core, cmd);
|
|
|
|
}
|
|
|
|
|
2015-06-08 23:45:39 +00:00
|
|
|
static char *core_cmdstr_callback (void *user, const char *cmd) {
|
|
|
|
RCore *core = (RCore *)user;
|
|
|
|
return r_core_cmd_str (core, cmd);
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:28:42 +00:00
|
|
|
static ut64 getref (RCore *core, int n, char t, int type) {
|
2014-09-26 13:40:17 +00:00
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
2013-04-18 22:02:23 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RAnalRef *r;
|
|
|
|
RList *list;
|
|
|
|
int i=0;
|
|
|
|
if (!fcn) return UT64_MAX;
|
2014-09-22 22:40:35 +00:00
|
|
|
#if FCN_OLD
|
2013-04-18 22:02:23 +00:00
|
|
|
list = (t=='r')? fcn->refs: fcn->xrefs;
|
|
|
|
r_list_foreach (list, iter, r) {
|
|
|
|
if (r->type == type) {
|
|
|
|
if (i == n)
|
|
|
|
return r->addr;
|
|
|
|
i++;
|
2012-10-22 00:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 22:40:35 +00:00
|
|
|
#else
|
|
|
|
#warning implement getref() using sdb
|
|
|
|
#endif
|
2012-10-22 00:28:42 +00:00
|
|
|
return UT64_MAX;
|
|
|
|
}
|
2012-12-10 00:52:11 +00:00
|
|
|
|
2010-05-19 22:59:42 +00:00
|
|
|
static ut64 num_callback(RNum *userptr, const char *str, int *ok) {
|
|
|
|
RCore *core = (RCore *)userptr; // XXX ?
|
2013-02-11 09:51:45 +00:00
|
|
|
RAnalFunction *fcn;
|
2015-03-11 10:46:42 +00:00
|
|
|
char *ptr, *bptr, *out;
|
2010-04-14 20:28:45 +00:00
|
|
|
RFlagItem *flag;
|
2013-04-18 22:02:23 +00:00
|
|
|
RIOSection *s;
|
2011-02-24 15:50:29 +00:00
|
|
|
RAnalOp op;
|
2011-02-23 01:10:28 +00:00
|
|
|
ut64 ret = 0;
|
2013-04-18 22:02:23 +00:00
|
|
|
|
2015-09-14 10:35:38 +00:00
|
|
|
if (ok) *ok = false;
|
2015-01-30 23:50:05 +00:00
|
|
|
switch (*str) {
|
|
|
|
case '[':
|
|
|
|
{
|
2015-07-13 09:21:21 +00:00
|
|
|
ut64 n = 0LL;
|
2015-10-21 22:48:14 +00:00
|
|
|
int refsz = core->assembler->bits / 8;
|
2014-09-12 11:28:31 +00:00
|
|
|
const char *p = NULL;
|
|
|
|
if (strlen (str)>5)
|
|
|
|
p = strchr (str+5, ':');
|
2013-06-07 08:26:37 +00:00
|
|
|
// TODO: honor LE
|
2012-06-27 23:27:40 +00:00
|
|
|
if (p) {
|
|
|
|
refsz = atoi (str+1);
|
|
|
|
str = p;
|
|
|
|
}
|
2012-10-22 00:28:42 +00:00
|
|
|
// push state
|
|
|
|
{
|
2015-07-08 12:55:52 +00:00
|
|
|
if (str[0] && str[1]) {
|
|
|
|
const char *q;
|
2015-07-05 17:48:41 +00:00
|
|
|
char *o = strdup (str+1);
|
2015-07-08 12:55:52 +00:00
|
|
|
if (o) {
|
|
|
|
q = r_num_calc_index (core->num, NULL);
|
2015-07-08 16:33:29 +00:00
|
|
|
if (q) {
|
|
|
|
if (r_str_replace_char (o, ']', 0)>0) {
|
|
|
|
n = r_num_math (core->num, o);
|
|
|
|
r_num_calc_index (core->num, q);
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 12:55:52 +00:00
|
|
|
free (o);
|
|
|
|
}
|
2015-07-05 17:48:41 +00:00
|
|
|
}
|
2012-12-10 00:52:11 +00:00
|
|
|
}
|
|
|
|
// pop state
|
2014-09-22 11:54:08 +00:00
|
|
|
if (ok) *ok = 1;
|
2015-10-21 22:48:14 +00:00
|
|
|
ut64 num = 0;
|
2012-06-27 23:27:40 +00:00
|
|
|
switch (refsz) {
|
2014-11-13 17:25:31 +00:00
|
|
|
case 8:
|
|
|
|
case 4:
|
|
|
|
case 2:
|
|
|
|
case 1:
|
|
|
|
(void)r_io_read_at (core->io, n, (ut8*)&num, refsz);
|
|
|
|
r_mem_copyendian ((ut8*)&num, (ut8*)&num, refsz, !core->assembler->big_endian);
|
|
|
|
return num;
|
2012-06-27 23:27:40 +00:00
|
|
|
default:
|
2013-04-16 17:48:59 +00:00
|
|
|
eprintf ("Invalid reference size: %d (%s)\n", refsz, str);
|
2014-11-13 17:25:31 +00:00
|
|
|
return 0LL;
|
2012-06-27 23:27:40 +00:00
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '$':
|
2013-04-18 22:02:23 +00:00
|
|
|
if (ok) *ok = 1;
|
2012-12-14 09:51:25 +00:00
|
|
|
// TODO: group analop-dependant vars after a char, so i can filter
|
|
|
|
r_anal_op (core->anal, &op, core->offset,
|
|
|
|
core->block, core->blocksize);
|
2010-02-02 10:09:52 +00:00
|
|
|
switch (str[1]) {
|
2012-12-14 09:51:25 +00:00
|
|
|
case '.': // can use pc, sp, a0, a1, ...
|
|
|
|
return r_debug_reg_get (core->dbg, str+2);
|
2015-01-11 18:16:46 +00:00
|
|
|
case 'k':
|
2015-03-11 10:46:42 +00:00
|
|
|
if (str[2]!='{') {
|
2015-01-11 18:16:46 +00:00
|
|
|
eprintf ("Expected '{' after 'k'.\n");
|
2015-03-11 10:46:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
bptr = strdup (str+3);
|
|
|
|
ptr = strchr (bptr, '}');
|
|
|
|
if (ptr == NULL) {
|
|
|
|
// invalid json
|
|
|
|
free (bptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
ret = 0LL;
|
|
|
|
out = sdb_querys (core->sdb, NULL, 0, bptr);
|
|
|
|
if (out && *out) {
|
|
|
|
if (strstr (out, "$k{")) {
|
|
|
|
eprintf ("Recursivity is not permitted here\n");
|
|
|
|
} else {
|
|
|
|
ret = r_num_math (core->num, out);
|
|
|
|
}
|
2015-01-11 18:16:46 +00:00
|
|
|
}
|
2015-03-11 10:46:42 +00:00
|
|
|
free (bptr);
|
|
|
|
free (out);
|
|
|
|
return ret;
|
2015-01-11 18:16:46 +00:00
|
|
|
break;
|
2009-04-15 18:24:19 +00:00
|
|
|
case '{':
|
2013-04-18 22:02:23 +00:00
|
|
|
bptr = strdup (str+2);
|
|
|
|
ptr = strchr (bptr, '}');
|
|
|
|
if (ptr != NULL) {
|
|
|
|
ut64 ret;
|
|
|
|
ptr[0] = '\0';
|
|
|
|
ret = r_config_get_i (core->config, bptr);
|
|
|
|
free (bptr);
|
|
|
|
return ret;
|
2009-04-15 18:24:19 +00:00
|
|
|
}
|
2014-11-01 04:18:37 +00:00
|
|
|
free (bptr);
|
2013-04-18 22:02:23 +00:00
|
|
|
break;
|
2013-07-04 12:08:58 +00:00
|
|
|
case 'c': return r_cons_get_size (NULL);
|
|
|
|
case 'r': { int rows; r_cons_get_size (&rows); return rows; }
|
2014-08-24 12:35:15 +00:00
|
|
|
case 'e': return r_anal_op_is_eob (&op);
|
2011-02-24 15:50:29 +00:00
|
|
|
case 'j': return op.jump;
|
2014-10-17 22:25:55 +00:00
|
|
|
case 'p': return r_sys_getpid ();
|
2015-01-11 18:16:46 +00:00
|
|
|
case 'P': return (core->dbg->pid>0)? core->dbg->pid: 0;
|
2011-02-24 15:50:29 +00:00
|
|
|
case 'f': return op.fail;
|
2013-07-04 12:08:58 +00:00
|
|
|
case 'm': return op.ptr; // memref
|
2014-05-21 12:35:22 +00:00
|
|
|
case 'v': return op.val; // immediate value
|
2013-12-06 04:04:17 +00:00
|
|
|
case 'l': return op.size;
|
2009-02-05 21:08:46 +00:00
|
|
|
case 'b': return core->blocksize;
|
2015-04-01 12:35:43 +00:00
|
|
|
case 's':
|
|
|
|
if (core->file) {
|
|
|
|
return r_io_desc_size (core->io, core->file->desc);
|
|
|
|
}
|
|
|
|
return 0LL;
|
2013-04-18 22:02:23 +00:00
|
|
|
case 'w': return r_config_get_i (core->config, "asm.bits") / 8;
|
|
|
|
case 'S':
|
2014-08-21 22:05:45 +00:00
|
|
|
s = r_io_section_vget (core->io, core->offset);
|
|
|
|
return s? (str[2]=='S'? s->size: s->vaddr): 3;
|
2010-05-19 22:59:42 +00:00
|
|
|
case '?': return core->num->value;
|
2011-02-23 01:10:28 +00:00
|
|
|
case '$': return core->offset;
|
2015-09-09 20:01:29 +00:00
|
|
|
case 'o': return r_io_section_vaddr_to_maddr_try (core->io,
|
2014-08-04 18:35:35 +00:00
|
|
|
core->offset);
|
2013-04-18 22:02:23 +00:00
|
|
|
case 'C': return getref (core, atoi (str+2), 'r',
|
|
|
|
R_ANAL_REF_TYPE_CALL);
|
|
|
|
case 'J': return getref (core, atoi (str+2), 'r',
|
|
|
|
R_ANAL_REF_TYPE_CODE);
|
|
|
|
case 'D': return getref (core, atoi (str+2), 'r',
|
|
|
|
R_ANAL_REF_TYPE_DATA);
|
|
|
|
case 'X': return getref (core, atoi (str+2), 'x',
|
|
|
|
R_ANAL_REF_TYPE_CALL);
|
2015-10-30 16:16:11 +00:00
|
|
|
case 'B':
|
|
|
|
fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
|
|
|
return fcn? fcn->addr: 0;
|
2012-10-22 00:28:42 +00:00
|
|
|
case 'I':
|
2014-09-26 13:40:17 +00:00
|
|
|
fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
2013-04-18 22:02:23 +00:00
|
|
|
return fcn? fcn->ninstr: 0;
|
2012-10-22 00:28:42 +00:00
|
|
|
case 'F':
|
2014-09-26 13:40:17 +00:00
|
|
|
fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
2013-04-18 22:02:23 +00:00
|
|
|
return fcn? fcn->size: 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
break;
|
2015-01-31 01:11:32 +00:00
|
|
|
default:
|
|
|
|
if (*str>'A') {
|
2015-03-16 01:52:26 +00:00
|
|
|
// NOTE: functions override flags
|
|
|
|
RAnalFunction *fcn = r_anal_fcn_find_name (core->anal, str);
|
|
|
|
if (fcn) {
|
2015-09-14 10:35:38 +00:00
|
|
|
if (ok) *ok = true;
|
2015-03-16 01:52:26 +00:00
|
|
|
return fcn->addr;
|
|
|
|
}
|
2014-09-23 07:35:37 +00:00
|
|
|
#if 0
|
2015-01-31 01:11:32 +00:00
|
|
|
ut64 addr = r_anal_fcn_label_get (core->anal, core->offset, str);
|
|
|
|
if (addr != 0) {
|
|
|
|
ret = addr;
|
|
|
|
} else {
|
|
|
|
...
|
|
|
|
}
|
2014-09-23 07:35:37 +00:00
|
|
|
#endif
|
2015-01-31 01:11:32 +00:00
|
|
|
if ((flag = r_flag_get (core->flags, str))) {
|
|
|
|
ret = flag->offset;
|
2015-09-14 10:35:38 +00:00
|
|
|
if (ok) *ok = true;
|
2015-01-31 01:11:32 +00:00
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-02-23 01:10:28 +00:00
|
|
|
}
|
2014-09-23 07:35:37 +00:00
|
|
|
|
2010-06-16 19:44:19 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API RCore *r_core_new() {
|
2014-09-22 11:45:36 +00:00
|
|
|
RCore *c = R_NEW0 (RCore);
|
2010-01-30 13:02:53 +00:00
|
|
|
r_core_init (c);
|
2009-02-05 21:08:46 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-04-07 11:28:22 +00:00
|
|
|
/*-----------------------------------*/
|
2011-02-05 01:21:40 +00:00
|
|
|
#define CMDS (sizeof (radare_argv)/sizeof(const char*))
|
|
|
|
static const char *radare_argv[] = {
|
2015-07-22 22:45:06 +00:00
|
|
|
"?", "?v", "whereis", "which", "ls", "rm", "mkdir", "pwd", "cat", "less",
|
2013-12-04 00:14:22 +00:00
|
|
|
"dH", "ds", "dso", "dsl", "dc", "dd", "dm", "db ", "db-",
|
2013-03-06 00:09:43 +00:00
|
|
|
"dp", "dr", "dcu", "dmd", "dmp", "dml",
|
2013-07-04 12:08:58 +00:00
|
|
|
"ec","ecs",
|
2015-10-19 01:18:19 +00:00
|
|
|
"S", "S.", "S*", "S-", "S=", "Sa", "Sa-", "Sd", "Sl", "SSj", "Sr",
|
2011-02-05 01:21:40 +00:00
|
|
|
"s", "s+", "s++", "s-", "s--", "s*", "sa", "sb", "sr",
|
2014-05-03 11:49:19 +00:00
|
|
|
"!", "!!",
|
|
|
|
"#sha1", "#crc32", "#pcprint", "#sha256", "#sha512", "#md4", "#md5",
|
2011-07-11 19:10:14 +00:00
|
|
|
"#!python", "#!perl", "#!vala",
|
2011-02-05 01:21:40 +00:00
|
|
|
"V",
|
2015-08-11 06:45:12 +00:00
|
|
|
"aa", "ab", "af", "ar", "ag", "at", "a?", "ax", "ad",
|
2015-11-27 00:22:50 +00:00
|
|
|
"af", "afa", "afan", "afc", "afi", "afb", "afbb", "afn", "afr", "afs", "af*", "afv", "afvn",
|
2011-08-27 23:49:29 +00:00
|
|
|
"aga", "agc", "agd", "agl", "agfl",
|
2013-12-14 02:10:04 +00:00
|
|
|
"e", "e-", "e*", "e!", "e?", "env ",
|
2011-02-05 01:21:40 +00:00
|
|
|
"i", "ii", "iI", "is", "iS", "iz",
|
2015-11-17 01:04:38 +00:00
|
|
|
"q", "q!",
|
2011-11-12 00:52:31 +00:00
|
|
|
"f", "fl", "fr", "f-", "f*", "fs", "fS", "fr", "fo", "f?",
|
2011-02-05 01:21:40 +00:00
|
|
|
"m", "m*", "ml", "m-", "my", "mg", "md", "mp", "m?",
|
2014-12-19 01:08:58 +00:00
|
|
|
"o", "o+", "oc", "on", "op", "o-", "x", "wf", "wF", "wt", "wp",
|
2015-08-05 00:39:16 +00:00
|
|
|
"t", "to ", "t-", "tf", "td", "td-", "tb", "te", "tl", "tk", "ts",
|
2013-04-11 23:15:00 +00:00
|
|
|
"(", "(*", "(-", "()", ".", ".!", ".(", "./",
|
2011-02-05 11:51:37 +00:00
|
|
|
"r", "r+", "r-",
|
2011-02-05 01:21:40 +00:00
|
|
|
"b", "bf", "b?",
|
2014-10-18 04:30:51 +00:00
|
|
|
"/", "//", "/a", "/c", "/m", "/x", "/v", "/v2", "/v4", "/v8", "/r"
|
2011-02-05 01:21:40 +00:00
|
|
|
"y", "yy", "y?",
|
2013-04-30 23:01:43 +00:00
|
|
|
"wx", "ww", "w?",
|
2013-01-23 13:50:50 +00:00
|
|
|
"p6d", "p6e", "p8", "pb", "pc",
|
2015-10-18 13:49:46 +00:00
|
|
|
"pd", "pda", "pdb", "pdc", "pdj", "pdr", "pdf", "pdi", "pdl", "pds", "pdt",
|
2015-10-22 15:19:15 +00:00
|
|
|
"pD", "px", "pX", "po", "pf", "pf.", "pf*", "pf*.", "pfd", "pfd.", "pv", "p=", "p-",
|
2015-11-26 19:59:04 +00:00
|
|
|
"pfj", "pfj.", "pfv", "pfv.",
|
2013-01-23 13:50:50 +00:00
|
|
|
"pm", "pr", "pt", "ptd", "ptn", "pt?", "ps", "pz", "pu", "pU", "p?",
|
2015-11-13 00:41:09 +00:00
|
|
|
"#!pipe", "z", "zf", "zF", "zFd", "zh", "zn", "zn-",
|
2011-02-05 01:21:40 +00:00
|
|
|
NULL
|
2009-04-07 11:28:22 +00:00
|
|
|
};
|
|
|
|
|
2015-10-26 01:42:08 +00:00
|
|
|
static int getsdelta(const char *data) {
|
|
|
|
int i;
|
|
|
|
for (i=1; data[i]; i++) {
|
|
|
|
if (data[i] == ' ')
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-25 17:47:47 +00:00
|
|
|
static int autocomplete(RLine *line) {
|
2015-01-05 04:10:55 +00:00
|
|
|
int pfree = 0;
|
2010-06-25 17:47:47 +00:00
|
|
|
RCore *core = line->user;
|
2010-12-23 23:51:01 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RFlagItem *flag;
|
2010-06-25 17:47:47 +00:00
|
|
|
if (core) {
|
2015-10-18 13:49:46 +00:00
|
|
|
r_core_free_autocomplete (core);
|
2011-02-09 23:21:05 +00:00
|
|
|
char *ptr = strchr (line->buffer.data, '@');
|
|
|
|
if (ptr && line->buffer.data+line->buffer.index >= ptr) {
|
2011-02-09 23:55:30 +00:00
|
|
|
int sdelta, n, i = 0;
|
|
|
|
ptr = (char *)r_str_chop_ro (ptr+1);
|
2011-02-09 23:21:05 +00:00
|
|
|
n = strlen (ptr);//(line->buffer.data+sdelta);
|
2011-02-09 23:55:30 +00:00
|
|
|
sdelta = (int)(size_t)(ptr - line->buffer.data);
|
2011-02-09 23:21:05 +00:00
|
|
|
r_list_foreach (core->flags->flags, iter, flag) {
|
2015-10-18 13:49:46 +00:00
|
|
|
if (!strncmp (flag->name, line->buffer.data+sdelta, n)) {
|
2011-02-09 23:21:05 +00:00
|
|
|
tmp_argv[i++] = flag->name;
|
2015-10-18 13:49:46 +00:00
|
|
|
if (i == TMP_ARGV_SZ-1)
|
2011-02-09 23:21:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if (!strncmp (line->buffer.data, "#!pipe ", 7)) {
|
2015-10-26 01:42:08 +00:00
|
|
|
int j = 0;
|
|
|
|
if (strchr (line->buffer.data + 7, ' ')) {
|
|
|
|
goto openfile;
|
|
|
|
}
|
|
|
|
tmp_argv_heap = false;
|
|
|
|
#define ADDARG(x) if (!strncmp (line->buffer.data+7, x, strlen (line->buffer.data+7))) { tmp_argv[j++] = x; }
|
|
|
|
ADDARG("node");
|
|
|
|
ADDARG("vala");
|
|
|
|
ADDARG("ruby");
|
|
|
|
ADDARG("newlisp");
|
|
|
|
ADDARG("perl");
|
|
|
|
ADDARG("python");
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if ((!strncmp (line->buffer.data, "pf.", 3))
|
2015-10-22 15:19:15 +00:00
|
|
|
|| (!strncmp (line->buffer.data, "pf*.", 4))
|
2015-11-26 19:59:04 +00:00
|
|
|
|| (!strncmp (line->buffer.data, "pfd.", 4))
|
|
|
|
|| (!strncmp (line->buffer.data, "pfv.", 4))
|
|
|
|
|| (!strncmp (line->buffer.data, "pfj.", 4))) {
|
2015-10-22 15:19:15 +00:00
|
|
|
char pfx[2];
|
|
|
|
int chr = (line->buffer.data[2]=='.')? 3: 4;
|
2015-10-26 01:42:08 +00:00
|
|
|
if (chr == 4) {
|
2015-10-22 15:19:15 +00:00
|
|
|
pfx[0] = line->buffer.data[2];
|
|
|
|
pfx[1] = 0;
|
|
|
|
} else {
|
|
|
|
*pfx = 0;
|
|
|
|
}
|
2015-10-18 13:49:46 +00:00
|
|
|
RStrHT *sht = core->print->formats;
|
|
|
|
int *i, j = 0;
|
|
|
|
r_list_foreach (sht->ls, iter, i) {
|
|
|
|
int idx = ((int)(size_t)i)-1;
|
|
|
|
const char *key = r_strpool_get (sht->sp, idx);
|
2015-10-22 15:19:15 +00:00
|
|
|
int len = strlen (line->buffer.data + chr);
|
|
|
|
if (!len || !strncmp (line->buffer.data + chr, key, len)) {
|
|
|
|
tmp_argv[j++] = r_str_newf ("pf%s.%s", pfx, key);
|
2015-10-18 13:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 19:59:04 +00:00
|
|
|
if (j > 0) tmp_argv_heap = true;
|
2015-10-18 13:49:46 +00:00
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-27 00:22:50 +00:00
|
|
|
} else if ((!strncmp (line->buffer.data, "afvn ", 5))) {
|
|
|
|
RAnalFunction *fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
|
|
|
RList *vars = r_anal_var_list (core->anal, fcn, R_ANAL_VAR_KIND_VAR);
|
|
|
|
const char *f_ptr, *l_ptr;
|
|
|
|
RAnalVar *var;
|
|
|
|
int j = 0, len = strlen (line->buffer.data);
|
|
|
|
|
|
|
|
f_ptr = r_sub_str_lchr (line->buffer.data, 0, line->buffer.index, ' ');
|
|
|
|
f_ptr = f_ptr != NULL ? f_ptr + 1 : line->buffer.data;
|
|
|
|
l_ptr = r_sub_str_rchr (line->buffer.data, line->buffer.index, len, ' ');
|
|
|
|
if (l_ptr == NULL) {
|
|
|
|
l_ptr = line->buffer.data + strlen (line->buffer.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
r_list_foreach (vars, iter, var) {
|
|
|
|
if (!strncmp (f_ptr, var->name, l_ptr - f_ptr)) {
|
|
|
|
tmp_argv[j++] = strdup(var->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if ((!strncmp (line->buffer.data, "te ", 3))) {
|
2015-10-27 11:41:09 +00:00
|
|
|
int i = 0;
|
|
|
|
SdbList *l = sdb_foreach_list (core->anal->sdb_types);
|
|
|
|
SdbListIter *iter;
|
|
|
|
SdbKv *kv;
|
|
|
|
int chr = 3;
|
|
|
|
ls_foreach (l, iter, kv) {
|
|
|
|
int len = strlen (line->buffer.data + chr);
|
|
|
|
if (!len || !strncmp (line->buffer.data + chr, kv->key, len)) {
|
|
|
|
if (!strncmp (kv->value, "0x", 2)) {
|
|
|
|
tmp_argv[i++] = strdup (kv->key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 19:59:04 +00:00
|
|
|
if (i > 0) tmp_argv_heap = true;
|
2015-10-27 11:41:09 +00:00
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
ls_free (l);
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if ((!strncmp (line->buffer.data, "o ", 2)) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
!strncmp (line->buffer.data, "o+ ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "oc ", 3) ||
|
2015-10-30 00:45:49 +00:00
|
|
|
!strncmp (line->buffer.data, "r2 ", 3) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
!strncmp (line->buffer.data, "cd ", 3) ||
|
2015-11-13 00:41:09 +00:00
|
|
|
!strncmp (line->buffer.data, "zF ", 3) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
!strncmp (line->buffer.data, "on ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "op ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, ". ", 2) ||
|
|
|
|
!strncmp (line->buffer.data, "wf ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "rm ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "ls ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "ls -l ", 5) ||
|
|
|
|
!strncmp (line->buffer.data, "wF ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "cat ", 4) ||
|
|
|
|
!strncmp (line->buffer.data, "less ", 5) ||
|
|
|
|
!strncmp (line->buffer.data, "wt ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "wp ", 3) ||
|
2015-10-19 01:18:19 +00:00
|
|
|
!strncmp (line->buffer.data, "Sd ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "Sl ", 3) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
!strncmp (line->buffer.data, "to ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "pm ", 3) ||
|
|
|
|
!strncmp (line->buffer.data, "dml ", 4) ||
|
|
|
|
!strncmp (line->buffer.data, "/m ", 3)) {
|
2011-02-13 00:37:02 +00:00
|
|
|
// XXX: SO MANY FUCKING MEMORY LEAKS
|
|
|
|
char *str, *p, *path;
|
2015-01-05 04:10:55 +00:00
|
|
|
int n = 0, i = 0, isroot = 0, iscwd = 0;
|
2013-03-06 00:09:43 +00:00
|
|
|
RList *list;
|
2015-10-26 01:42:08 +00:00
|
|
|
int sdelta;
|
|
|
|
openfile:
|
|
|
|
if (!strncmp (line->buffer.data, "#!pipe ", 7)) {
|
|
|
|
sdelta = getsdelta (line->buffer.data + 7) + 7;
|
|
|
|
} else {
|
|
|
|
sdelta = getsdelta (line->buffer.data);
|
|
|
|
}
|
2015-11-26 19:59:04 +00:00
|
|
|
path = sdelta > 0 ? strdup (line->buffer.data + sdelta):
|
2013-03-06 00:09:43 +00:00
|
|
|
r_sys_getdir ();
|
2013-06-09 01:25:32 +00:00
|
|
|
p = (char *)r_str_lchr (path, '/');
|
2011-02-13 00:37:02 +00:00
|
|
|
if (p) {
|
2015-10-27 11:41:09 +00:00
|
|
|
if (p == path) { // ^/
|
2015-01-05 04:10:55 +00:00
|
|
|
isroot = 1;
|
|
|
|
*p = 0;
|
|
|
|
p++;
|
2015-10-26 01:42:08 +00:00
|
|
|
} else if (p==path + 1) { // ^./
|
2015-01-05 04:10:55 +00:00
|
|
|
*p = 0;
|
2015-10-26 01:42:08 +00:00
|
|
|
iscwd = 1;
|
2015-01-05 04:10:55 +00:00
|
|
|
p++;
|
|
|
|
} else { // *
|
|
|
|
*p = 0;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-26 01:42:08 +00:00
|
|
|
iscwd = 1;
|
2015-01-05 04:10:55 +00:00
|
|
|
pfree = 1;
|
|
|
|
p = strdup (path);
|
2011-09-11 01:59:24 +00:00
|
|
|
free (path);
|
2015-10-26 01:42:08 +00:00
|
|
|
path = strdup (".");
|
2015-01-05 04:10:55 +00:00
|
|
|
}
|
|
|
|
if (pfree) {
|
|
|
|
if (p) {
|
|
|
|
if (*p) {
|
|
|
|
n = strlen (p);
|
|
|
|
} else {
|
|
|
|
free (p);
|
|
|
|
p = strdup ("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (p) { if (*p) n = strlen (p); else p = ""; }
|
|
|
|
}
|
|
|
|
if (iscwd) {
|
2015-10-26 01:42:08 +00:00
|
|
|
list = r_sys_dir ("./");
|
2015-01-05 04:10:55 +00:00
|
|
|
} else if (isroot) {
|
2015-10-26 01:42:08 +00:00
|
|
|
const char *lastslash = r_str_lchr (path, '/');
|
|
|
|
if (lastslash && lastslash[1]) {
|
|
|
|
list = r_sys_dir (path);
|
|
|
|
} else {
|
|
|
|
list = r_sys_dir ("/");
|
|
|
|
}
|
2015-01-05 04:10:55 +00:00
|
|
|
} else {
|
|
|
|
if (*path=='~') { // if implicit home
|
2015-10-26 01:42:08 +00:00
|
|
|
char *lala = r_str_home (path + 1);
|
2015-01-05 04:10:55 +00:00
|
|
|
free (path);
|
|
|
|
path = lala;
|
|
|
|
} else if (*path!='.' && *path!='/') { // ifnot@home
|
2015-10-26 01:42:08 +00:00
|
|
|
char *o = malloc (strlen (path) + 4);
|
2015-01-05 04:10:55 +00:00
|
|
|
memcpy (o, "./", 2);
|
|
|
|
p = o+2;
|
|
|
|
n = strlen (path);
|
2015-10-26 01:42:08 +00:00
|
|
|
memcpy (o + 2, path, strlen (path) + 1);
|
2015-01-05 04:10:55 +00:00
|
|
|
free (path);
|
|
|
|
path = o;
|
|
|
|
}
|
|
|
|
list = p? r_sys_dir (path): NULL;
|
2011-09-11 01:59:24 +00:00
|
|
|
}
|
2015-10-26 01:42:08 +00:00
|
|
|
i = 0;
|
2011-02-13 00:37:02 +00:00
|
|
|
if (list) {
|
2015-10-26 01:42:08 +00:00
|
|
|
// bool isroot = !strcmp (path, "/");
|
2011-02-13 00:37:02 +00:00
|
|
|
r_list_foreach (list, iter, str) {
|
2015-01-05 04:10:55 +00:00
|
|
|
if (*str == '.') // also list hidden files
|
2011-02-13 00:37:02 +00:00
|
|
|
continue;
|
2014-11-25 23:02:13 +00:00
|
|
|
if (!p || !*p || !strncmp (str, p, n)) {
|
2015-10-26 01:42:08 +00:00
|
|
|
tmp_argv[i++] = r_str_newf ("%s/%s", path, str);
|
|
|
|
if (i == TMP_ARGV_SZ) {
|
2014-01-27 00:12:16 +00:00
|
|
|
i--;
|
2011-02-13 00:37:02 +00:00
|
|
|
break;
|
2014-01-27 00:12:16 +00:00
|
|
|
}
|
2011-02-13 00:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-03 11:49:19 +00:00
|
|
|
r_list_purge (list);
|
|
|
|
free (list);
|
2015-11-26 19:59:04 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("\nInvalid directory (%s)\n", path);
|
|
|
|
}
|
2011-02-13 00:37:02 +00:00
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2014-05-05 12:38:47 +00:00
|
|
|
free (path);
|
2015-01-05 04:10:55 +00:00
|
|
|
if (pfree)
|
|
|
|
free (p);
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if ((!strncmp (line->buffer.data, ".(", 2)) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
(!strncmp (line->buffer.data, "(-", 2))) {
|
2014-01-27 00:12:16 +00:00
|
|
|
const char *str = line->buffer.data;
|
2013-04-11 23:15:00 +00:00
|
|
|
RCmdMacroItem *item;
|
|
|
|
char buf[1024];
|
|
|
|
int n, i = 0;
|
|
|
|
|
|
|
|
n = line->buffer.length-2;
|
2014-01-27 00:12:16 +00:00
|
|
|
if (str && !strchr (str+2, ' ')) {
|
|
|
|
str += 2;
|
2013-04-11 23:15:00 +00:00
|
|
|
r_list_foreach (core->rcmd->macro.macros, iter, item) {
|
|
|
|
char *p = item->name;
|
2015-10-18 13:49:46 +00:00
|
|
|
if (!str || !*str || !strncmp (str, p, n)) {
|
2013-04-11 23:15:00 +00:00
|
|
|
snprintf (buf, sizeof (buf), "%c%c%s)",
|
|
|
|
line->buffer.data[0],
|
|
|
|
line->buffer.data[1],
|
|
|
|
p);
|
|
|
|
eprintf ("------ %p\n", tmp_argv[i]);
|
2013-04-18 01:58:44 +00:00
|
|
|
if (r_is_heap ((void*)tmp_argv[i]))
|
2013-04-11 23:15:00 +00:00
|
|
|
free ((char *)tmp_argv[i]);
|
|
|
|
tmp_argv[i] = strdup (buf); // LEAKS
|
|
|
|
i++;
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 20:48:13 +00:00
|
|
|
tmp_argv[(i-1>0)?i-1:0] = NULL;
|
2013-04-11 23:15:00 +00:00
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if (!strncmp (line->buffer.data, "fs ", 3)) {
|
2015-10-19 00:51:30 +00:00
|
|
|
const char *msg = line->buffer.data + 3;
|
|
|
|
RFlag *flag = core->flags;
|
|
|
|
int j, i = 0;
|
2015-10-19 21:31:36 +00:00
|
|
|
for (j=0; j<R_FLAG_SPACES_MAX-1; j++) {
|
2015-10-19 00:51:30 +00:00
|
|
|
if (flag->spaces[j] && flag->spaces[j][0]) {
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
if (!strncmp (msg, flag->spaces[j], strlen (msg))) {
|
|
|
|
tmp_argv[i++] = flag->spaces[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-20 20:29:16 +00:00
|
|
|
if (flag->spaces[j] && !strncmp (msg, flag->spaces[j],
|
|
|
|
strlen (msg))) {
|
2015-10-19 00:51:30 +00:00
|
|
|
tmp_argv[i++] = "*";
|
|
|
|
}
|
|
|
|
tmp_argv[i] = NULL;
|
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if ((!strncmp (line->buffer.data, "s ", 2)) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
(!strncmp (line->buffer.data, "ad ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "bf ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "ag ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "afi ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "afb ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "afc ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "axt ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "axf ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "aga ", 5)) ||
|
|
|
|
(!strncmp (line->buffer.data, "agc ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "agl ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "agd ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "agfl ", 5)) ||
|
|
|
|
(!strncmp (line->buffer.data, "b ", 2)) ||
|
|
|
|
(!strncmp (line->buffer.data, "dcu ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "/v ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "db ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "db- ", 4)) ||
|
|
|
|
(!strncmp (line->buffer.data, "f ", 2)) ||
|
2015-11-16 23:49:59 +00:00
|
|
|
(!strncmp (line->buffer.data, "f- ", 3)) ||
|
2015-08-11 06:45:12 +00:00
|
|
|
(!strncmp (line->buffer.data, "fr ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "tf ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "/a ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "?v ", 3)) ||
|
|
|
|
(!strncmp (line->buffer.data, "? ", 2))) {
|
2010-06-25 17:47:47 +00:00
|
|
|
int n, i = 0;
|
2011-06-08 23:20:02 +00:00
|
|
|
int sdelta = (line->buffer.data[1]==' ')?2:
|
|
|
|
(line->buffer.data[2]==' ')?3:4;
|
2010-07-12 12:46:43 +00:00
|
|
|
n = strlen (line->buffer.data+sdelta);
|
2010-12-23 23:51:01 +00:00
|
|
|
r_list_foreach (core->flags->flags, iter, flag) {
|
2015-10-18 13:49:46 +00:00
|
|
|
if (!strncmp (flag->name, line->buffer.data+sdelta, n)) {
|
2010-06-25 17:47:47 +00:00
|
|
|
tmp_argv[i++] = flag->name;
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 12:22:18 +00:00
|
|
|
tmp_argv[i>255?255:i] = NULL;
|
2010-06-25 17:47:47 +00:00
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if (!strncmp (line->buffer.data, "-", 1)) {
|
2012-10-30 01:49:05 +00:00
|
|
|
int count;
|
|
|
|
char **keys = r_cmd_alias_keys(core->rcmd, &count);
|
|
|
|
char *data = line->buffer.data;
|
|
|
|
if (keys) {
|
|
|
|
int i, j;
|
|
|
|
for (i=j=0; i<count; i++) {
|
2014-09-24 20:45:38 +00:00
|
|
|
if (!strncmp (keys[i], data, line->buffer.index)) {
|
2012-10-30 01:49:05 +00:00
|
|
|
tmp_argv[j++] = keys[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
|
|
|
} else {
|
|
|
|
line->completion.argc = 0;
|
|
|
|
line->completion.argv = NULL;
|
|
|
|
}
|
2015-11-26 19:59:04 +00:00
|
|
|
} else if ( (!strncmp (line->buffer.data, "e ", 2))
|
2015-11-27 00:22:50 +00:00
|
|
|
|| (!strncmp (line->buffer.data, "e? ", 3))
|
|
|
|
|| (!strncmp (line->buffer.data, "e! ", 3))) {
|
|
|
|
const char p = line->buffer.data[1];
|
|
|
|
int m = (p == '?' || p == '!') ? 3 : 2;
|
2012-12-30 00:05:12 +00:00
|
|
|
int i = 0, n = strlen (line->buffer.data+m);
|
2011-03-17 18:05:39 +00:00
|
|
|
RConfigNode *bt;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (core->config->nodes, iter, bt) {
|
2014-11-06 11:03:06 +00:00
|
|
|
if (!strncmp (bt->name, line->buffer.data+m, n)) {
|
2010-06-25 17:47:47 +00:00
|
|
|
tmp_argv[i++] = bt->name;
|
|
|
|
if (i==TMP_ARGV_SZ)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-02-03 14:53:21 +00:00
|
|
|
tmp_argv[R_MIN(i, TMP_ARGV_SZ - 1)] = NULL;
|
2010-06-25 17:47:47 +00:00
|
|
|
line->completion.argc = i;
|
|
|
|
line->completion.argv = tmp_argv;
|
2010-06-28 23:18:30 +00:00
|
|
|
} else {
|
2011-02-09 23:21:05 +00:00
|
|
|
int i, j;
|
2015-11-26 19:59:04 +00:00
|
|
|
for (i = j = 0; i < CMDS && radare_argv[i]; i++)
|
2014-09-24 20:45:38 +00:00
|
|
|
if (!strncmp (radare_argv[i], line->buffer.data,
|
2014-04-21 11:50:38 +00:00
|
|
|
line->buffer.index))
|
2010-06-28 23:18:30 +00:00
|
|
|
tmp_argv[j++] = radare_argv[i];
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2010-06-25 17:47:47 +00:00
|
|
|
}
|
2010-06-28 23:18:30 +00:00
|
|
|
} else {
|
2015-10-18 13:49:46 +00:00
|
|
|
int i, j;
|
2014-11-16 09:24:04 +00:00
|
|
|
for (i=j=0; i<CMDS && radare_argv[i]; i++)
|
2015-10-18 13:49:46 +00:00
|
|
|
if (!strncmp (radare_argv[i], line->buffer.data,
|
2014-04-21 11:50:38 +00:00
|
|
|
line->buffer.index))
|
2010-06-28 23:18:30 +00:00
|
|
|
tmp_argv[j++] = radare_argv[i];
|
|
|
|
tmp_argv[j] = NULL;
|
|
|
|
line->completion.argc = j;
|
|
|
|
line->completion.argv = tmp_argv;
|
2010-06-25 17:47:47 +00:00
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2010-06-25 17:47:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 02:08:08 +00:00
|
|
|
R_API int r_core_fgets(char *buf, int len) {
|
2015-01-30 23:50:05 +00:00
|
|
|
const char *ptr;
|
2014-05-03 11:49:19 +00:00
|
|
|
RLine *rli = r_line_singleton ();
|
2015-01-31 01:11:32 +00:00
|
|
|
buf[0] = '\0';
|
2010-02-18 15:36:55 +00:00
|
|
|
rli->completion.argc = CMDS;
|
|
|
|
rli->completion.argv = radare_argv;
|
2010-06-25 17:47:47 +00:00
|
|
|
rli->completion.run = autocomplete;
|
2015-01-30 23:50:05 +00:00
|
|
|
ptr = r_line_readline ();
|
2009-04-07 11:28:22 +00:00
|
|
|
if (ptr == NULL)
|
2014-03-03 01:01:40 +00:00
|
|
|
return -1;
|
2010-01-09 01:05:04 +00:00
|
|
|
strncpy (buf, ptr, len);
|
2015-01-31 23:10:37 +00:00
|
|
|
buf[len-1] = 0;
|
2010-01-09 01:05:04 +00:00
|
|
|
return strlen (buf)+1;
|
2009-04-07 11:28:22 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------*/
|
|
|
|
|
2009-09-10 20:51:34 +00:00
|
|
|
#if 0
|
2009-07-08 11:49:55 +00:00
|
|
|
static int __dbg_read(void *user, int pid, ut64 addr, ut8 *buf, int len)
|
2009-04-12 23:10:22 +00:00
|
|
|
{
|
2010-03-19 03:02:23 +00:00
|
|
|
RCore *core = (RCore *)user;
|
2009-04-12 23:10:22 +00:00
|
|
|
// TODO: pid not used
|
|
|
|
return r_core_read_at(core, addr, buf, len);
|
|
|
|
}
|
|
|
|
|
2011-05-20 17:49:50 +00:00
|
|
|
static int __dbg_write(void *user, int pid, ut64 addr, const ut8 *buf, int len) {
|
2010-03-19 03:02:23 +00:00
|
|
|
RCore *core = (RCore *)user;
|
2009-04-12 23:10:22 +00:00
|
|
|
// TODO: pid not used
|
|
|
|
return r_core_write_at(core, addr, buf, len);
|
|
|
|
}
|
2009-09-10 20:51:34 +00:00
|
|
|
#endif
|
2009-04-12 23:10:22 +00:00
|
|
|
|
2011-05-20 17:49:50 +00:00
|
|
|
static const char *r_core_print_offname(void *p, ut64 addr) {
|
|
|
|
RCore *c = (RCore*)p;
|
|
|
|
RFlagItem *item = r_flag_get_i (c->flags, addr);
|
|
|
|
if (item) return item->name;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-02-25 21:52:57 +00:00
|
|
|
static int __disasm(void *_core, ut64 addr) {
|
|
|
|
RCore *core = _core;
|
|
|
|
ut8 buf[32], *oblock;
|
2013-02-25 08:36:07 +00:00
|
|
|
int len;
|
2013-02-25 21:52:57 +00:00
|
|
|
oblock = core->block;
|
2013-02-25 08:36:07 +00:00
|
|
|
r_io_read_at (core->io, addr, (ut8*)buf, sizeof (buf));
|
|
|
|
len = r_core_print_disasm_instructions (core, sizeof (buf), 1);
|
|
|
|
core->block = oblock;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-03-05 02:52:42 +00:00
|
|
|
static void update_sdb(RCore *core) {
|
2015-03-11 10:46:42 +00:00
|
|
|
Sdb *d;
|
2014-08-24 23:10:10 +00:00
|
|
|
RBinObject *o;
|
2014-08-29 14:26:43 +00:00
|
|
|
if (!core)
|
|
|
|
return;
|
2014-08-24 23:10:10 +00:00
|
|
|
//SDB// anal/
|
2014-08-29 14:26:43 +00:00
|
|
|
if (core->anal && core->anal->sdb)
|
|
|
|
sdb_ns_set (DB, "anal", core->anal->sdb);
|
2014-08-24 23:10:10 +00:00
|
|
|
//SDB// bin/
|
2014-08-29 14:26:43 +00:00
|
|
|
if (core->bin && core->bin->sdb)
|
|
|
|
sdb_ns_set (DB, "bin", core->bin->sdb);
|
2014-08-24 23:10:10 +00:00
|
|
|
//SDB// bin/info
|
|
|
|
o = r_bin_get_object (core->bin);
|
2014-05-14 03:48:03 +00:00
|
|
|
if (o) {
|
2014-06-05 22:06:30 +00:00
|
|
|
sdb_ns_set (sdb_ns (DB, "bin", 1), "info", o->kv);
|
2014-05-14 03:48:03 +00:00
|
|
|
}
|
2014-03-12 01:54:21 +00:00
|
|
|
//sdb_ns_set (core->sdb, "flags", core->flags->sdb);
|
|
|
|
//sdb_ns_set (core->sdb, "bin", core->bin->sdb);
|
2014-08-24 23:10:10 +00:00
|
|
|
//SDB// syscall/
|
2014-05-11 19:57:35 +00:00
|
|
|
if (core->assembler && core->assembler->syscall && core->assembler->syscall->db) {
|
2014-05-07 23:32:06 +00:00
|
|
|
core->assembler->syscall->db->refs++;
|
2014-04-09 02:53:34 +00:00
|
|
|
sdb_ns_set (DB, "syscall", core->assembler->syscall->db);
|
2014-05-07 23:32:06 +00:00
|
|
|
}
|
2015-03-11 10:46:42 +00:00
|
|
|
d = sdb_ns (DB, "debug", 1);
|
|
|
|
core->dbg->sgnls->refs++;
|
|
|
|
sdb_ns_set (d, "signals", core->dbg->sgnls);
|
2014-03-05 02:52:42 +00:00
|
|
|
}
|
|
|
|
|
2014-09-20 23:39:18 +00:00
|
|
|
// dupped in cmd_type.c
|
|
|
|
static char *getenumname(void *_core, const char *name, ut64 val) {
|
|
|
|
const char *isenum;
|
|
|
|
RCore *core = (RCore*)_core;
|
2015-03-11 10:46:42 +00:00
|
|
|
|
2014-09-20 23:39:18 +00:00
|
|
|
isenum = sdb_const_get (core->anal->sdb_types, name, 0);
|
|
|
|
if (isenum && !strcmp (isenum, "enum")) {
|
|
|
|
const char *q = sdb_fmt (0, "%s.0x%x", name, val);
|
|
|
|
return sdb_get (core->anal->sdb_types, q, 0);
|
|
|
|
} else {
|
|
|
|
eprintf ("This is not an enum\n");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-22 16:08:29 +00:00
|
|
|
// TODO: dupped in cmd_type.c
|
2014-09-20 23:39:18 +00:00
|
|
|
static char *getbitfield(void *_core, const char *name, ut64 val) {
|
2015-03-11 10:46:42 +00:00
|
|
|
const char *isenum, *q, *res;
|
|
|
|
RCore *core = (RCore*)_core;
|
2014-09-22 16:08:29 +00:00
|
|
|
char *ret = NULL;
|
2014-09-20 23:39:18 +00:00
|
|
|
int i;
|
2015-03-11 10:46:42 +00:00
|
|
|
|
2014-09-20 23:39:18 +00:00
|
|
|
isenum = sdb_const_get (core->anal->sdb_types, name, 0);
|
|
|
|
if (isenum && !strcmp (isenum, "enum")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
int isFirst = true;
|
2014-09-22 16:40:38 +00:00
|
|
|
ret = r_str_concatf (ret, "0x%08"PFMT64x" : ", val);
|
2015-03-11 10:46:42 +00:00
|
|
|
for (i=0; i < 32; i++) {
|
|
|
|
if (!(val & (1<<i)))
|
|
|
|
continue;
|
|
|
|
q = sdb_fmt (0, "%s.0x%x", name, (1<<i));
|
|
|
|
res = sdb_const_get (core->anal->sdb_types, q, 0);
|
2015-03-29 20:03:26 +00:00
|
|
|
if (isFirst) {
|
2015-09-14 10:35:38 +00:00
|
|
|
isFirst = false;
|
2015-03-29 20:03:26 +00:00
|
|
|
} else {
|
2015-03-11 10:46:42 +00:00
|
|
|
ret = r_str_concat (ret, " | ");
|
|
|
|
}
|
|
|
|
if (res) {
|
|
|
|
ret = r_str_concat (ret, res);
|
|
|
|
} else {
|
|
|
|
ret = r_str_concatf (ret, "0x%x", (1<<i));
|
2014-09-20 23:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("This is not an enum\n");
|
|
|
|
}
|
2014-09-22 16:08:29 +00:00
|
|
|
return ret;
|
2014-09-20 23:39:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 12:43:17 +00:00
|
|
|
#define MINLEN 1
|
|
|
|
static int is_string (const ut8 *buf, int size, int *len) {
|
|
|
|
int i;
|
|
|
|
if (size < 1) return 0;
|
|
|
|
if (size > 3 && buf[0] && !buf[1] && buf[2] && !buf[3]) {
|
|
|
|
*len = 1; // XXX: TODO: Measure wide string length
|
|
|
|
return 2; // is wide
|
|
|
|
}
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (!buf[i] && i > MINLEN) {
|
|
|
|
*len = i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (buf[i] == 10|| buf[i] == 13|| buf[i] == 9) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (buf[i] < 32 || buf[i] > 127) {
|
|
|
|
// not ascii text
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!IS_PRINTABLE (buf[i])) {
|
|
|
|
*len = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*len = i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-04 20:58:08 +00:00
|
|
|
R_API char *r_core_anal_hasrefs(RCore *core, ut64 value) {
|
|
|
|
RStrBuf *s = r_strbuf_new (NULL);
|
2014-10-27 16:23:13 +00:00
|
|
|
ut64 type;
|
2015-09-28 00:28:20 +00:00
|
|
|
RIOSection *sect;
|
|
|
|
char *mapname;
|
2014-10-27 16:23:13 +00:00
|
|
|
RAnalFunction *fcn;
|
|
|
|
RFlagItem *fi;
|
|
|
|
fi = r_flag_get_i (core->flags, value);
|
|
|
|
type = r_core_anal_address (core, value);
|
|
|
|
fcn = r_anal_get_fcn_in (core->anal, value, 0);
|
2015-09-28 01:06:18 +00:00
|
|
|
if (value && value != UT64_MAX) {
|
|
|
|
RDebugMap *map = r_debug_map_get (core->dbg, value);
|
2015-10-05 12:43:17 +00:00
|
|
|
if (map && map->name && map->name[0]) {
|
2015-09-28 00:28:20 +00:00
|
|
|
mapname = strdup (map->name);
|
2015-10-05 12:43:17 +00:00
|
|
|
} else {
|
|
|
|
mapname = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mapname = NULL;
|
2014-10-27 16:23:13 +00:00
|
|
|
}
|
2015-10-05 12:43:17 +00:00
|
|
|
sect = value? r_io_section_vget (core->io, value): NULL;
|
|
|
|
if (fi) r_strbuf_appendf (s, " %s", fi->name);
|
|
|
|
if (fcn) r_strbuf_appendf (s, " %s", fcn->name);
|
2014-10-27 16:23:13 +00:00
|
|
|
if (type) {
|
|
|
|
const char *c = r_core_anal_optype_colorfor (core, value);
|
2015-10-05 12:43:17 +00:00
|
|
|
const char *cend = (c && *c) ? Color_RESET: "";
|
2014-10-27 16:23:13 +00:00
|
|
|
if (!c) c = "";
|
|
|
|
if (type & R_ANAL_ADDR_TYPE_HEAP) {
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sheap%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
} else if (type & R_ANAL_ADDR_TYPE_STACK) {
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sstack%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
}
|
|
|
|
if (type & R_ANAL_ADDR_TYPE_PROGRAM)
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sprogram%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_LIBRARY)
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %slibrary%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_ASCII)
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sascii%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_SEQUENCE)
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %ssequence%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_READ)
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sR%s", c, cend);
|
2014-10-27 16:23:13 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_WRITE)
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sW%s", c, cend);
|
2014-10-28 10:35:54 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_EXEC) {
|
2015-09-28 00:28:20 +00:00
|
|
|
RAsmOp op;
|
|
|
|
ut8 buf[32];
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " %sX%s", c, cend);
|
2015-09-28 00:28:20 +00:00
|
|
|
/* instruction disassembly */
|
|
|
|
r_io_read_at (core->io, value, buf, sizeof (buf));
|
|
|
|
r_asm_set_pc (core->assembler, value);
|
|
|
|
r_asm_disassemble (core->assembler, &op, buf, sizeof (buf));
|
|
|
|
r_strbuf_appendf (s, " '%s'", op.buf_asm);
|
2014-10-28 16:05:30 +00:00
|
|
|
/* get library name */
|
2015-09-28 00:28:20 +00:00
|
|
|
{ // NOTE: dup for mapname?
|
2014-10-28 16:05:30 +00:00
|
|
|
RDebugMap *map;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (core->dbg->maps, iter, map) {
|
2015-10-05 12:43:17 +00:00
|
|
|
if ((value >= map->addr) &&
|
|
|
|
(value<map->addr_end)) {
|
2014-10-28 16:05:30 +00:00
|
|
|
const char *lastslash = r_str_lchr (map->name, '/');
|
2015-06-04 20:58:08 +00:00
|
|
|
r_strbuf_appendf (s, " '%s'", lastslash?
|
2014-10-28 16:05:30 +00:00
|
|
|
lastslash+1:map->name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 01:36:03 +00:00
|
|
|
} else if (type & R_ANAL_ADDR_TYPE_READ) {
|
|
|
|
ut8 buf[32];
|
|
|
|
ut32 *n32 = (ut32 *)buf;
|
|
|
|
ut64 *n64 = (ut64*)buf;
|
|
|
|
ut64 n = (core->assembler->bits == 64)? *n64: *n32;
|
|
|
|
r_io_read_at (core->io, value, buf, sizeof (buf));
|
|
|
|
r_strbuf_appendf (s, " [0]=0x%"PFMT64x, n);
|
2014-10-28 10:35:54 +00:00
|
|
|
}
|
2014-10-27 16:23:13 +00:00
|
|
|
}
|
2015-09-28 00:28:20 +00:00
|
|
|
if (sect && sect->name[0]) {
|
|
|
|
r_strbuf_appendf (s," (%s)", sect->name);
|
|
|
|
}
|
|
|
|
if (mapname) {
|
|
|
|
r_strbuf_appendf (s, " (%s)", mapname);
|
|
|
|
free (mapname);
|
|
|
|
}
|
2015-10-05 12:43:17 +00:00
|
|
|
{
|
|
|
|
ut8 buf[128], widebuf[256];
|
|
|
|
const char *c = core->cons->pal.ai_ascii;
|
|
|
|
const char *cend = Color_RESET;
|
|
|
|
int len, r;
|
|
|
|
r = r_io_read_at (core->io, value, buf, sizeof(buf));
|
|
|
|
if (r) {
|
|
|
|
switch (is_string (buf, sizeof(buf), &len)) {
|
|
|
|
case 1:
|
|
|
|
r_strbuf_appendf (s, " (%s%s%s)", c, buf, cend);
|
|
|
|
break;
|
|
|
|
case 2:
|
2015-10-09 11:26:25 +00:00
|
|
|
r = r_utf8_encode_str ((const RRune *)buf, widebuf,
|
2015-10-05 12:43:17 +00:00
|
|
|
sizeof(widebuf) - 1);
|
|
|
|
if (r == -1) {
|
|
|
|
eprintf ("Something was wrong %s-%d\n",
|
|
|
|
__FILE__, __LINE__);
|
2015-10-07 11:08:23 +00:00
|
|
|
} else {
|
|
|
|
r_strbuf_appendf (s, " (%s%s%s)", c, widebuf, cend);
|
2015-10-05 12:43:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-06-04 20:58:08 +00:00
|
|
|
{
|
|
|
|
char *rs = strdup (r_strbuf_get (s));
|
|
|
|
r_strbuf_free (s);
|
|
|
|
return rs;
|
|
|
|
}
|
2014-10-27 16:23:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-26 18:10:07 +00:00
|
|
|
R_API const char *r_core_anal_optype_colorfor(RCore *core, ut64 addr) {
|
2014-10-24 22:54:22 +00:00
|
|
|
ut64 type;
|
|
|
|
if (!(core->print->flags & R_PRINT_FLAGS_COLOR))
|
|
|
|
return NULL;
|
|
|
|
type = r_core_anal_address (core, addr);
|
2014-10-24 19:31:46 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_EXEC)
|
2014-10-27 10:55:52 +00:00
|
|
|
return core->cons->pal.ai_exec; //Color_RED;
|
2014-10-24 19:31:46 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_WRITE)
|
2014-10-27 10:55:52 +00:00
|
|
|
return core->cons->pal.ai_write; //Color_BLUE;
|
2014-10-24 19:31:46 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_READ)
|
2014-10-27 10:55:52 +00:00
|
|
|
return core->cons->pal.ai_read; //Color_GREEN;
|
2014-10-24 19:31:46 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_SEQUENCE)
|
2014-10-27 10:55:52 +00:00
|
|
|
return core->cons->pal.ai_seq; //Color_MAGENTA;
|
2014-10-24 19:31:46 +00:00
|
|
|
if (type & R_ANAL_ADDR_TYPE_ASCII)
|
2014-10-27 10:55:52 +00:00
|
|
|
return core->cons->pal.ai_ascii; //Color_YELLOW;
|
2014-10-24 19:31:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-09-28 10:03:19 +00:00
|
|
|
static void r_core_setenv (RCore *core) {
|
|
|
|
char *e = r_sys_getenv ("PATH");
|
|
|
|
char *h = r_str_home (".config/radare2/bin");
|
|
|
|
char *n = r_str_newf ("%s:%s", h, e);
|
|
|
|
r_sys_setenv ("PATH", n);
|
|
|
|
free (n);
|
|
|
|
free (h);
|
|
|
|
free (e);
|
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API int r_core_init(RCore *core) {
|
2015-09-28 10:03:19 +00:00
|
|
|
r_core_setenv(core);
|
2014-01-09 00:09:40 +00:00
|
|
|
core->cmd_depth = R_CORE_CMD_DEPTH+1;
|
2014-03-07 00:26:11 +00:00
|
|
|
core->sdb = sdb_new (NULL, "r2kv.sdb", 0); // XXX: path must be in home?
|
2015-09-23 11:24:10 +00:00
|
|
|
core->lastsearch = NULL;
|
2015-09-14 10:35:38 +00:00
|
|
|
core->incomment = false;
|
2014-09-22 11:45:36 +00:00
|
|
|
core->screen_bounds = 0LL;
|
2013-03-26 23:17:32 +00:00
|
|
|
core->config = NULL;
|
2015-09-14 10:35:38 +00:00
|
|
|
core->http_up = false;
|
2014-09-20 23:39:18 +00:00
|
|
|
core->print = r_print_new ();
|
2013-01-22 17:08:33 +00:00
|
|
|
core->print->user = core;
|
2014-09-20 23:39:18 +00:00
|
|
|
core->print->get_enumname = getenumname;
|
|
|
|
core->print->get_bitfield = getbitfield;
|
2013-01-22 17:08:33 +00:00
|
|
|
core->print->offname = r_core_print_offname;
|
2015-08-08 18:15:13 +00:00
|
|
|
core->print->cb_printf = (void *)r_cons_printf;
|
2013-01-22 17:08:33 +00:00
|
|
|
core->print->write = (void *)r_cons_memcat;
|
2013-02-25 08:36:07 +00:00
|
|
|
core->print->disasm = __disasm;
|
2014-10-26 18:10:07 +00:00
|
|
|
core->print->colorfor = (RPrintColorFor)r_core_anal_optype_colorfor;
|
2014-10-27 16:23:13 +00:00
|
|
|
core->print->hasrefs = (RPrintColorFor)r_core_anal_hasrefs;
|
2010-08-24 09:58:09 +00:00
|
|
|
core->rtr_n = 0;
|
2012-04-24 06:46:36 +00:00
|
|
|
core->blocksize_max = R_CORE_BLOCKSIZE_MAX;
|
2014-11-01 03:46:33 +00:00
|
|
|
core->tasks = r_list_new ();
|
2012-12-07 14:44:36 +00:00
|
|
|
core->watchers = r_list_new ();
|
2012-12-10 00:52:11 +00:00
|
|
|
core->watchers->free = (RListFree)r_core_cmpwatch_free;
|
2013-11-09 02:25:43 +00:00
|
|
|
core->scriptstack = r_list_new ();
|
|
|
|
core->scriptstack->free = (RListFree)free;
|
2012-12-23 12:52:57 +00:00
|
|
|
core->log = r_core_log_new ();
|
2015-09-14 10:35:38 +00:00
|
|
|
core->vmode = false;
|
2012-11-30 00:06:30 +00:00
|
|
|
core->section = NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
core->oobi = NULL;
|
|
|
|
core->oobi_len = 0;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->printidx = 0;
|
2011-08-27 18:25:37 +00:00
|
|
|
core->lastcmd = NULL;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->cmdqueue = NULL;
|
2015-09-14 10:35:38 +00:00
|
|
|
core->cmdrepeat = true;
|
2014-03-20 16:59:08 +00:00
|
|
|
core->yank_buf = r_buf_new();
|
2010-05-19 22:59:42 +00:00
|
|
|
core->num = r_num_new (&num_callback, core);
|
|
|
|
//core->num->callback = &num_callback;
|
|
|
|
//core->num->userptr = core;
|
2011-02-15 15:19:18 +00:00
|
|
|
core->curasmstep = 0;
|
2011-10-04 08:10:00 +00:00
|
|
|
core->egg = r_egg_new ();
|
|
|
|
r_egg_setup (core->egg, R_SYS_ARCH, R_SYS_BITS, 0, R_SYS_OS);
|
2009-04-01 00:28:13 +00:00
|
|
|
|
|
|
|
/* initialize libraries */
|
2015-01-21 17:06:06 +00:00
|
|
|
core->cons = r_cons_new ();
|
2015-01-21 17:55:46 +00:00
|
|
|
if (core->cons->refcnt == 1) {
|
2014-05-19 09:37:32 +00:00
|
|
|
core->cons = r_cons_singleton ();
|
|
|
|
if (core->cons->line) {
|
|
|
|
core->cons->line->user = core;
|
|
|
|
core->cons->line->editor_cb = \
|
|
|
|
(RLineEditorCb)&r_core_editor;
|
|
|
|
}
|
2013-09-16 02:08:08 +00:00
|
|
|
#if __EMSCRIPTEN__
|
|
|
|
core->cons->user_fgets = NULL;
|
|
|
|
#else
|
|
|
|
core->cons->user_fgets = (void *)r_core_fgets;
|
|
|
|
#endif
|
2010-06-25 17:47:47 +00:00
|
|
|
//r_line_singleton()->user = (void *)core;
|
2013-06-27 17:15:59 +00:00
|
|
|
r_line_hist_load (R2_HOMEDIR"/history");
|
2010-05-28 00:44:51 +00:00
|
|
|
}
|
2013-05-23 00:26:48 +00:00
|
|
|
core->print->cons = core->cons;
|
2011-06-26 21:49:11 +00:00
|
|
|
core->cons->num = core->num;
|
2011-06-04 01:14:04 +00:00
|
|
|
core->blocksize = R_CORE_BLOCKSIZE;
|
2015-07-05 11:38:59 +00:00
|
|
|
core->block = (ut8*)malloc (R_CORE_BLOCKSIZE+1);
|
2011-06-04 01:14:04 +00:00
|
|
|
if (core->block == NULL) {
|
|
|
|
eprintf ("Cannot allocate %d bytes\n", R_CORE_BLOCKSIZE);
|
|
|
|
/* XXX memory leak */
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2011-06-04 01:14:04 +00:00
|
|
|
}
|
2010-05-19 22:59:42 +00:00
|
|
|
core->lang = r_lang_new ();
|
2015-03-21 02:06:31 +00:00
|
|
|
core->lang->cmd_str = (char *(*)(void *, const char *))r_core_cmd_str;
|
2014-12-19 12:01:44 +00:00
|
|
|
core->cons->editor = (RConsEditorCallback)r_core_editor;
|
2014-12-19 02:17:28 +00:00
|
|
|
core->cons->user = (void*)core;
|
2015-08-08 18:15:13 +00:00
|
|
|
core->lang->cb_printf = r_cons_printf;
|
2010-05-19 22:59:42 +00:00
|
|
|
r_lang_define (core->lang, "RCore", "core", core);
|
|
|
|
r_lang_set_user_ptr (core->lang, core);
|
|
|
|
core->assembler = r_asm_new ();
|
2012-11-16 00:34:26 +00:00
|
|
|
core->assembler->num = core->num;
|
2010-05-19 22:59:42 +00:00
|
|
|
r_asm_set_user_ptr (core->assembler, core);
|
2013-04-21 23:09:27 +00:00
|
|
|
core->anal = r_anal_new ();
|
2015-08-08 18:15:13 +00:00
|
|
|
core->anal->meta_spaces.cb_printf = r_cons_printf;
|
2015-03-16 01:52:26 +00:00
|
|
|
core->anal->cb.on_fcn_new = on_fcn_new;
|
|
|
|
core->anal->cb.on_fcn_delete = on_fcn_delete;
|
|
|
|
core->anal->cb.on_fcn_rename = on_fcn_rename;
|
2014-06-14 00:27:43 +00:00
|
|
|
core->assembler->syscall = \
|
|
|
|
core->anal->syscall; // BIND syscall anal/asm
|
2013-04-21 23:09:27 +00:00
|
|
|
r_anal_set_user_ptr (core->anal, core);
|
2015-08-08 18:15:13 +00:00
|
|
|
core->anal->cb_printf = (void *) r_cons_printf;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->parser = r_parse_new ();
|
2013-05-21 23:49:18 +00:00
|
|
|
core->parser->anal = core->anal;
|
2014-12-14 01:10:55 +00:00
|
|
|
core->parser->varlist = r_anal_var_list;
|
2010-05-19 22:59:42 +00:00
|
|
|
r_parse_set_user_ptr (core->parser, core);
|
|
|
|
core->bin = r_bin_new ();
|
2015-08-08 18:15:13 +00:00
|
|
|
core->bin->cb_printf = (PrintfCallback) r_cons_printf;
|
2010-05-19 22:59:42 +00:00
|
|
|
r_bin_set_user_ptr (core->bin, core);
|
|
|
|
core->io = r_io_new ();
|
2014-05-28 17:32:29 +00:00
|
|
|
core->io->ff = 1;
|
2011-08-04 19:28:07 +00:00
|
|
|
core->io->user = (void *)core;
|
2015-06-08 23:45:39 +00:00
|
|
|
core->io->cb_core_cmd = core_cmd_callback;
|
|
|
|
core->io->cb_core_cmdstr = core_cmdstr_callback;
|
2010-05-19 22:59:42 +00:00
|
|
|
core->sign = r_sign_new ();
|
|
|
|
core->search = r_search_new (R_SEARCH_KEYWORD);
|
|
|
|
r_io_undo_enable (core->io, 1, 0); // TODO: configurable via eval
|
2011-01-14 00:02:20 +00:00
|
|
|
core->fs = r_fs_new ();
|
2014-09-26 17:25:02 +00:00
|
|
|
core->flags = r_flag_new ();
|
2015-07-09 21:56:55 +00:00
|
|
|
core->graph = r_agraph_new (r_cons_canvas_new (1, 1));
|
2015-10-08 18:00:31 +00:00
|
|
|
core->asmqjmps_size = R_CORE_ASMQJMPS_NUM;
|
2015-11-03 19:50:42 +00:00
|
|
|
if (sizeof(ut64) * core->asmqjmps_size < core->asmqjmps_size) {
|
|
|
|
core->asmqjmps_size = 0;
|
|
|
|
core->asmqjmps = NULL;
|
|
|
|
} else {
|
|
|
|
core->asmqjmps = R_NEWS (ut64, core->asmqjmps_size);
|
|
|
|
}
|
2014-09-26 17:25:02 +00:00
|
|
|
|
2011-06-26 18:29:24 +00:00
|
|
|
r_bin_bind (core->bin, &(core->assembler->binb));
|
2014-05-07 19:04:04 +00:00
|
|
|
r_bin_bind (core->bin, &(core->anal->binb));
|
|
|
|
r_bin_bind (core->bin, &(core->anal->binb));
|
|
|
|
|
2011-03-02 10:45:20 +00:00
|
|
|
r_io_bind (core->io, &(core->search->iob));
|
2011-02-16 23:58:54 +00:00
|
|
|
r_io_bind (core->io, &(core->print->iob));
|
2011-02-02 23:20:39 +00:00
|
|
|
r_io_bind (core->io, &(core->anal->iob));
|
2011-01-14 00:02:20 +00:00
|
|
|
r_io_bind (core->io, &(core->fs->iob));
|
2014-04-29 16:10:35 +00:00
|
|
|
r_io_bind (core->io, &(core->bin->iob));
|
2014-09-26 12:24:33 +00:00
|
|
|
r_flag_bind (core->flags, &(core->anal->flb));
|
2012-02-16 01:40:50 +00:00
|
|
|
|
2009-02-05 21:08:46 +00:00
|
|
|
core->file = NULL;
|
2011-02-07 08:46:01 +00:00
|
|
|
core->files = r_list_new ();
|
2011-02-09 23:55:30 +00:00
|
|
|
core->files->free = (RListFree)r_core_file_free;
|
2010-01-12 01:12:18 +00:00
|
|
|
core->offset = 0LL;
|
2010-01-30 13:02:53 +00:00
|
|
|
r_core_cmd_init (core);
|
2015-09-14 10:35:38 +00:00
|
|
|
core->dbg = r_debug_new (true);
|
2014-10-30 21:58:51 +00:00
|
|
|
r_core_bind (core, &core->dbg->corebind);
|
2015-08-08 18:15:13 +00:00
|
|
|
core->dbg->cb_printf = (PrintfCallback)r_cons_printf;
|
2010-06-04 21:47:35 +00:00
|
|
|
core->dbg->anal = core->anal; // XXX: dupped instance.. can cause lost pointerz
|
2011-05-05 22:59:10 +00:00
|
|
|
//r_debug_use (core->dbg, "native");
|
2014-06-23 17:49:01 +00:00
|
|
|
// XXX pushing unititialized regstate results in trashed reg values
|
|
|
|
// r_reg_arena_push (core->dbg->reg); // create a 2 level register state stack
|
2010-06-16 19:44:19 +00:00
|
|
|
// core->dbg->anal->reg = core->anal->reg; // XXX: dupped instance.. can cause lost pointerz
|
2015-08-08 18:15:13 +00:00
|
|
|
core->sign->cb_printf = r_cons_printf;
|
|
|
|
core->io->cb_printf = r_cons_printf;
|
|
|
|
core->dbg->cb_printf = r_cons_printf;
|
|
|
|
core->dbg->bp->cb_printf = r_cons_printf;
|
2010-05-19 22:59:42 +00:00
|
|
|
r_debug_io_bind (core->dbg, core->io);
|
2013-03-26 23:17:32 +00:00
|
|
|
|
2010-01-30 13:02:53 +00:00
|
|
|
r_core_config_init (core);
|
2009-04-01 00:28:13 +00:00
|
|
|
|
2014-04-03 10:42:53 +00:00
|
|
|
r_core_loadlibs_init (core);
|
|
|
|
//r_core_loadlibs (core);
|
2010-01-26 00:28:33 +00:00
|
|
|
|
|
|
|
// TODO: get arch from r_bin or from native arch
|
2010-05-19 22:59:42 +00:00
|
|
|
r_asm_use (core->assembler, R_SYS_ARCH);
|
|
|
|
r_anal_use (core->anal, R_SYS_ARCH);
|
2010-03-04 00:46:25 +00:00
|
|
|
if (R_SYS_BITS & R_SYS_BITS_64)
|
2010-05-19 22:59:42 +00:00
|
|
|
r_config_set_i (core->config, "asm.bits", 64);
|
2010-03-04 00:46:25 +00:00
|
|
|
else
|
|
|
|
if (R_SYS_BITS & R_SYS_BITS_32)
|
2010-05-19 22:59:42 +00:00
|
|
|
r_config_set_i (core->config, "asm.bits", 32);
|
|
|
|
r_config_set (core->config, "asm.arch", R_SYS_ARCH);
|
2015-06-22 10:23:38 +00:00
|
|
|
r_bp_use (core->dbg->bp, R_SYS_ARCH, core->anal->bits);
|
2014-03-05 02:52:42 +00:00
|
|
|
update_sdb (core);
|
2009-02-05 21:08:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-13 12:14:23 +00:00
|
|
|
R_API RCore *r_core_fini(RCore *c) {
|
2011-12-16 15:33:06 +00:00
|
|
|
if (!c) return NULL;
|
2010-01-26 00:28:33 +00:00
|
|
|
/* TODO: it leaks as shit */
|
2014-08-29 14:26:43 +00:00
|
|
|
//update_sdb (c);
|
2015-02-10 01:33:22 +00:00
|
|
|
// avoid double free
|
2015-10-18 13:49:46 +00:00
|
|
|
r_core_free_autocomplete(c);
|
2015-09-23 11:04:15 +00:00
|
|
|
R_FREE (c->lastsearch);
|
2015-02-10 01:33:22 +00:00
|
|
|
c->cons->pager = NULL;
|
2014-11-02 01:01:09 +00:00
|
|
|
r_core_task_join (c, NULL);
|
2014-08-19 23:21:31 +00:00
|
|
|
free (c->cmdqueue);
|
|
|
|
free (c->lastcmd);
|
2012-01-31 01:45:17 +00:00
|
|
|
r_io_free (c->io);
|
2014-05-06 02:00:26 +00:00
|
|
|
r_num_free (c->num);
|
2014-03-07 00:26:11 +00:00
|
|
|
// TODO: sync or not? sdb_sync (c->sdb);
|
|
|
|
// TODO: sync all dbs?
|
2014-01-18 15:26:09 +00:00
|
|
|
//r_core_file_free (c->file);
|
|
|
|
//c->file = NULL;
|
2014-01-20 02:29:50 +00:00
|
|
|
r_list_free (c->files);
|
2013-11-09 02:25:43 +00:00
|
|
|
r_list_free (c->watchers);
|
|
|
|
r_list_free (c->scriptstack);
|
2014-08-29 14:26:43 +00:00
|
|
|
c->rcmd = r_cmd_free (c->rcmd);
|
|
|
|
c->anal = r_anal_free (c->anal);
|
|
|
|
c->assembler = r_asm_free (c->assembler);
|
|
|
|
c->print = r_print_free (c->print);
|
|
|
|
c->bin = r_bin_free (c->bin); // XXX segfaults rabin2 -c
|
|
|
|
c->lang = r_lang_free (c->lang); // XXX segfaults
|
|
|
|
c->dbg = r_debug_free (c->dbg);
|
2012-01-31 01:45:17 +00:00
|
|
|
r_config_free (c->config);
|
2013-12-09 04:23:06 +00:00
|
|
|
/* after r_config_free, the value of I.teefile is trashed */
|
|
|
|
/* rconfig doesnt knows how to deinitialize vars, so we
|
|
|
|
should probably need to add a r_config_free_payload callback */
|
2014-08-19 23:21:31 +00:00
|
|
|
r_cons_free ();
|
2013-12-09 04:23:06 +00:00
|
|
|
r_cons_singleton()->teefile = NULL; // HACK
|
2012-01-31 01:45:17 +00:00
|
|
|
r_search_free (c->search);
|
|
|
|
r_sign_free (c->sign);
|
2012-06-14 15:41:07 +00:00
|
|
|
r_flag_free (c->flags);
|
2012-01-31 01:45:17 +00:00
|
|
|
r_fs_free (c->fs);
|
2011-10-04 08:10:00 +00:00
|
|
|
r_egg_free (c->egg);
|
2012-06-14 15:41:07 +00:00
|
|
|
r_lib_free (c->lib);
|
2014-03-20 16:59:08 +00:00
|
|
|
r_buf_free (c->yank_buf);
|
2015-07-09 21:56:55 +00:00
|
|
|
r_agraph_free (c->graph);
|
2015-10-08 18:00:31 +00:00
|
|
|
R_FREE (c->asmqjmps);
|
2014-08-24 23:10:10 +00:00
|
|
|
sdb_free (c->sdb);
|
2012-08-13 12:14:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RCore *r_core_free(RCore *c) {
|
2015-10-18 13:49:46 +00:00
|
|
|
if (c) {
|
|
|
|
r_core_fini (c);
|
|
|
|
free (c);
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-04 00:34:54 +00:00
|
|
|
|
2011-08-27 02:32:27 +00:00
|
|
|
R_API void r_core_prompt_loop(RCore *r) {
|
|
|
|
int ret;
|
2014-09-26 13:40:17 +00:00
|
|
|
do {
|
2015-09-14 10:35:38 +00:00
|
|
|
if (r_core_prompt (r, false)<1)
|
2011-08-27 02:32:27 +00:00
|
|
|
break;
|
|
|
|
// if (lock) r_th_lock_enter (lock);
|
|
|
|
if ((ret = r_core_prompt_exec (r))==-1)
|
|
|
|
eprintf ("Invalid command\n");
|
|
|
|
/* if (lock) r_th_lock_leave (lock);
|
|
|
|
if (rabin_th && !r_th_wait_async (rabin_th)) {
|
|
|
|
eprintf ("rabin thread end \n");
|
|
|
|
r_th_free (rabin_th);
|
|
|
|
r_th_lock_free (lock);
|
|
|
|
lock = NULL;
|
|
|
|
rabin_th = NULL;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} while (ret != R_CORE_CMD_EXIT);
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2015-08-11 09:49:56 +00:00
|
|
|
static int prompt_flag (RCore *r, char *s, size_t maxlen) {
|
|
|
|
const char DOTS[] = "...";
|
2015-08-11 17:42:12 +00:00
|
|
|
const RFlagItem *f = r_flag_get_at (r->flags, r->offset);
|
2015-09-14 10:35:38 +00:00
|
|
|
if (!f) return false;
|
2015-08-11 09:49:56 +00:00
|
|
|
|
2015-08-11 17:42:12 +00:00
|
|
|
if (f->offset < r->offset) {
|
|
|
|
snprintf (s, maxlen, "%s + %" PFMT64u, f->name,
|
|
|
|
r->offset - f->offset);
|
2015-08-11 09:49:56 +00:00
|
|
|
} else {
|
2015-08-11 17:42:12 +00:00
|
|
|
snprintf (s, maxlen, "%s", f->name);
|
2015-08-11 09:49:56 +00:00
|
|
|
}
|
|
|
|
if (strlen (s) > maxlen - sizeof (DOTS)) {
|
|
|
|
s[maxlen - sizeof (DOTS) - 1] = '\0';
|
|
|
|
strcat (s, DOTS);
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2015-08-11 09:49:56 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 17:42:12 +00:00
|
|
|
static void prompt_sec(RCore *r, char *s, size_t maxlen) {
|
|
|
|
const RIOSection *sec = r_io_section_vget (r->io, r->offset);
|
|
|
|
if (!sec) return;
|
|
|
|
|
|
|
|
snprintf (s, maxlen, "%s:", sec->name);
|
|
|
|
}
|
|
|
|
|
2015-09-02 23:51:11 +00:00
|
|
|
static void chop_prompt (const char *filename, char *tmp, size_t max_tmp_size) {
|
2015-08-11 21:11:12 +00:00
|
|
|
size_t tmp_len, file_len;
|
|
|
|
unsigned int OTHRSCH = 3;
|
|
|
|
const char DOTS[] = "...";
|
|
|
|
int w, p_len;
|
|
|
|
|
|
|
|
w = r_cons_get_size (NULL);
|
|
|
|
file_len = strlen (filename);
|
|
|
|
tmp_len = strlen (tmp);
|
|
|
|
p_len = R_MAX (0, w - 6);
|
|
|
|
if (file_len + tmp_len + OTHRSCH >= p_len) {
|
2015-09-02 23:51:11 +00:00
|
|
|
size_t dots_size = sizeof (DOTS);
|
|
|
|
size_t chop_point = (size_t)(p_len - OTHRSCH - file_len - dots_size - 1);
|
|
|
|
if (chop_point < (max_tmp_size - dots_size - 1)) {
|
|
|
|
tmp[chop_point] = '\0';
|
|
|
|
strncat (tmp, DOTS, dots_size);
|
|
|
|
}
|
2015-08-11 21:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_prompt (RCore *r) {
|
2015-09-02 23:51:11 +00:00
|
|
|
size_t max_tmp_size = 128;
|
|
|
|
char tmp[max_tmp_size];
|
2015-08-11 21:11:12 +00:00
|
|
|
char *prompt = NULL;
|
2015-03-18 22:59:05 +00:00
|
|
|
char *filename = strdup ("");
|
2010-05-19 22:59:42 +00:00
|
|
|
const char *cmdprompt = r_config_get (r->config, "cmd.prompt");
|
2015-08-11 09:49:56 +00:00
|
|
|
const char *BEGIN = "";
|
|
|
|
const char *END = "";
|
2015-07-09 21:44:45 +00:00
|
|
|
const char *remote = "";
|
2013-05-22 02:22:49 +00:00
|
|
|
|
2013-02-11 09:51:45 +00:00
|
|
|
// hacky fix fo rio
|
|
|
|
r_core_block_read (r, 0);
|
2011-07-05 23:29:18 +00:00
|
|
|
if (cmdprompt && *cmdprompt)
|
2012-02-01 10:49:46 +00:00
|
|
|
r_core_cmd (r, cmdprompt, 0);
|
2009-02-18 00:43:57 +00:00
|
|
|
|
2015-03-18 22:59:05 +00:00
|
|
|
if (r_config_get_i (r->config, "scr.promptfile")) {
|
2015-03-21 00:27:54 +00:00
|
|
|
free (filename);
|
2015-03-18 22:59:05 +00:00
|
|
|
filename = r_str_newf ("\"%s\"",
|
|
|
|
r_file_basename (r->io->desc->name));
|
|
|
|
}
|
2015-07-09 21:44:45 +00:00
|
|
|
if (r->cmdremote) {
|
2015-07-09 21:47:48 +00:00
|
|
|
char *s = r_core_cmd_str (r, "s");
|
|
|
|
r->offset = r_num_math (NULL, s);
|
|
|
|
free (s);
|
2015-07-09 21:44:45 +00:00
|
|
|
remote = "=!";
|
|
|
|
}
|
2015-08-11 09:49:56 +00:00
|
|
|
#if __UNIX__
|
|
|
|
if (r_config_get_i (r->config, "scr.color")) {
|
|
|
|
BEGIN = r->cons->pal.prompt;
|
|
|
|
END = r->cons->pal.reset;
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-14 02:25:32 +00:00
|
|
|
// TODO: also in visual prompt and disasm/hexdump ?
|
2012-11-16 00:34:26 +00:00
|
|
|
if (r_config_get_i (r->config, "asm.segoff")) {
|
2012-11-14 02:25:32 +00:00
|
|
|
ut32 a, b;
|
2015-08-11 21:11:12 +00:00
|
|
|
|
2015-08-11 09:49:56 +00:00
|
|
|
a = ((r->offset >> 16) << 12);
|
2012-11-14 02:25:32 +00:00
|
|
|
b = (r->offset & 0xffff);
|
2015-09-02 23:51:11 +00:00
|
|
|
snprintf (tmp, max_tmp_size, "%04x:%04x", a, b);
|
2012-11-14 02:25:32 +00:00
|
|
|
} else {
|
2015-08-11 21:11:12 +00:00
|
|
|
char p[64], sec[32];
|
2015-09-14 10:35:38 +00:00
|
|
|
int promptset = false;
|
2015-08-11 09:49:56 +00:00
|
|
|
|
2015-08-11 17:42:12 +00:00
|
|
|
sec[0] = '\0';
|
2015-08-11 09:49:56 +00:00
|
|
|
if (r_config_get_i (r->config, "scr.promptflag")) {
|
2015-08-11 21:11:12 +00:00
|
|
|
promptset = prompt_flag (r, p, sizeof (p));
|
2015-08-11 09:49:56 +00:00
|
|
|
}
|
2015-08-11 17:42:12 +00:00
|
|
|
if (r_config_get_i (r->config, "scr.promptsect")) {
|
|
|
|
prompt_sec (r, sec, sizeof (sec));
|
|
|
|
}
|
2015-08-11 09:49:56 +00:00
|
|
|
|
|
|
|
if (!promptset) {
|
2015-08-11 21:11:12 +00:00
|
|
|
snprintf (p, sizeof (p), "0x%08" PFMT64x, r->offset);
|
2015-08-11 09:49:56 +00:00
|
|
|
}
|
2015-08-11 21:11:12 +00:00
|
|
|
snprintf (tmp, sizeof (tmp), "%s%s", sec, p);
|
2012-11-14 02:25:32 +00:00
|
|
|
}
|
2015-08-11 21:11:12 +00:00
|
|
|
|
2015-09-02 23:51:11 +00:00
|
|
|
chop_prompt (filename, tmp, max_tmp_size);
|
2015-08-11 21:11:12 +00:00
|
|
|
prompt = r_str_newf ("%s%s[%s%s]>%s ", filename, BEGIN, remote,
|
|
|
|
tmp, END);
|
|
|
|
r_line_set_prompt (prompt ? prompt : "");
|
|
|
|
|
|
|
|
R_FREE (filename);
|
|
|
|
R_FREE (prompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_prompt(RCore *r, int sync) {
|
|
|
|
int ret, rnv;
|
|
|
|
char line[4096];
|
|
|
|
|
|
|
|
rnv = r->num->value;
|
|
|
|
set_prompt (r);
|
|
|
|
|
2010-01-09 01:05:04 +00:00
|
|
|
ret = r_cons_fgets (line, sizeof (line), 0, NULL);
|
2014-03-03 01:01:40 +00:00
|
|
|
if (ret == -2) return R_CORE_CMD_EXIT; // ^D
|
2015-09-14 10:35:38 +00:00
|
|
|
if (ret == -1) return false; // FD READ ERROR
|
2013-08-25 21:10:15 +00:00
|
|
|
r->num->value = rnv;
|
2014-03-03 01:01:40 +00:00
|
|
|
if (sync) {
|
|
|
|
return r_core_prompt_exec (r);
|
|
|
|
}
|
2012-02-03 00:13:09 +00:00
|
|
|
free (r->cmdqueue);
|
|
|
|
r->cmdqueue = strdup (line);
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2010-06-23 15:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_prompt_exec(RCore *r) {
|
2015-09-14 10:35:38 +00:00
|
|
|
int ret = r_core_cmd (r, r->cmdqueue, true);
|
2010-01-09 01:05:04 +00:00
|
|
|
r_cons_flush ();
|
2015-10-24 21:25:56 +00:00
|
|
|
if (r->cons && r->cons->line && r->cons->line->zerosep)
|
2014-08-16 23:41:53 +00:00
|
|
|
r_cons_zero ();
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-02 13:41:17 +00:00
|
|
|
R_API int r_core_block_size(RCore *core, int bsize) {
|
2012-09-20 21:32:20 +00:00
|
|
|
ut8 *bump;
|
2015-09-14 10:35:38 +00:00
|
|
|
int ret = false;
|
|
|
|
if (bsize<0) return false;
|
2011-01-23 16:48:31 +00:00
|
|
|
if (bsize == core->blocksize)
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2015-06-25 21:41:14 +00:00
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
// TODO : restrict to filesize?
|
|
|
|
if (bsize > 1024*32) {
|
|
|
|
eprintf ("Sandbox mode restricts blocksize bigger than 32k\n");
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2015-06-25 21:41:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bsize > core->blocksize_max) {
|
2013-10-19 21:10:08 +00:00
|
|
|
eprintf ("Block size %d is too big\n", bsize);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2013-10-19 21:10:08 +00:00
|
|
|
}
|
|
|
|
if (bsize<1) {
|
2012-02-12 22:45:04 +00:00
|
|
|
bsize = 1;
|
2013-10-19 21:10:08 +00:00
|
|
|
} else if (core->blocksize_max && bsize>core->blocksize_max) {
|
2013-11-06 01:52:34 +00:00
|
|
|
eprintf ("bsize is bigger than `bm`. dimmed to 0x%x > 0x%x\n",
|
2012-08-31 09:45:06 +00:00
|
|
|
bsize, core->blocksize_max);
|
2012-04-21 12:28:53 +00:00
|
|
|
bsize = core->blocksize_max;
|
2015-08-29 16:08:38 +00:00
|
|
|
}
|
2012-09-20 01:38:48 +00:00
|
|
|
bump = realloc (core->block, bsize+1);
|
|
|
|
if (bump == NULL) {
|
2010-04-08 16:01:45 +00:00
|
|
|
eprintf ("Oops. cannot allocate that much (%u)\n", bsize);
|
2015-09-14 10:35:38 +00:00
|
|
|
ret = false;
|
2013-02-11 10:28:27 +00:00
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
ret = true;
|
2013-02-11 10:28:27 +00:00
|
|
|
core->block = bump;
|
|
|
|
core->blocksize = bsize;
|
|
|
|
memset (core->block, 0xff, core->blocksize);
|
|
|
|
r_core_block_read (core, 0);
|
2012-09-20 01:38:48 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-19 03:02:23 +00:00
|
|
|
R_API int r_core_seek_align(RCore *core, ut64 align, int times) {
|
2014-01-16 01:46:00 +00:00
|
|
|
int diff, inc = (times>=0)?1:-1;
|
2010-01-12 01:12:18 +00:00
|
|
|
ut64 seek = core->offset;
|
2014-01-16 01:46:00 +00:00
|
|
|
|
|
|
|
if (!align)
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2014-01-16 01:46:00 +00:00
|
|
|
diff = core->offset%align;
|
2010-01-09 01:05:04 +00:00
|
|
|
if (times == 0)
|
|
|
|
diff = -diff;
|
2009-03-12 22:19:58 +00:00
|
|
|
else if (diff) {
|
|
|
|
if (inc>0) diff += align-diff;
|
|
|
|
else diff = -diff;
|
|
|
|
if (times) times -= inc;
|
2009-03-12 12:30:32 +00:00
|
|
|
}
|
2010-01-09 01:05:04 +00:00
|
|
|
while ((times*inc)>0) {
|
2009-03-12 12:30:32 +00:00
|
|
|
times -= inc;
|
|
|
|
diff += align*inc;
|
|
|
|
}
|
2010-02-02 10:09:52 +00:00
|
|
|
if (diff<0 && -diff>seek)
|
|
|
|
seek = diff = 0;
|
2010-01-09 01:05:04 +00:00
|
|
|
return r_core_seek (core, seek+diff, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2009-02-18 00:43:57 +00:00
|
|
|
|
2010-05-20 07:29:39 +00:00
|
|
|
R_API char *r_core_op_str(RCore *core, ut64 addr) {
|
2011-02-24 15:50:29 +00:00
|
|
|
RAsmOp op;
|
2010-06-04 21:47:35 +00:00
|
|
|
ut8 buf[64];
|
|
|
|
int ret;
|
2010-05-20 07:29:39 +00:00
|
|
|
r_asm_set_pc (core->assembler, addr);
|
|
|
|
r_core_read_at (core, addr, buf, sizeof (buf));
|
2011-02-24 15:50:29 +00:00
|
|
|
ret = r_asm_disassemble (core->assembler, &op, buf, sizeof (buf));
|
|
|
|
return (ret>0)?strdup (op.buf_asm): NULL;
|
2010-05-20 07:29:39 +00:00
|
|
|
}
|
|
|
|
|
2010-05-20 23:46:26 +00:00
|
|
|
R_API RAnalOp *r_core_op_anal(RCore *core, ut64 addr) {
|
2010-05-20 07:29:39 +00:00
|
|
|
ut8 buf[64];
|
2011-02-24 15:50:29 +00:00
|
|
|
RAnalOp *op = R_NEW (RAnalOp);
|
2010-05-20 07:29:39 +00:00
|
|
|
r_core_read_at (core, addr, buf, sizeof (buf));
|
2011-02-24 15:50:29 +00:00
|
|
|
r_anal_op (core->anal, op, addr, buf, sizeof (buf));
|
|
|
|
return op;
|
2010-05-20 07:29:39 +00:00
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
|
2012-12-30 22:40:17 +00:00
|
|
|
static void rap_break (void *u) {
|
|
|
|
RIORap *rior = (RIORap*) u;
|
|
|
|
if (u) {
|
|
|
|
r_socket_free (rior->fd);
|
|
|
|
rior->fd = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-07 08:26:37 +00:00
|
|
|
// TODO: PLEASE move into core/io/rap? */
|
2013-02-08 10:42:46 +00:00
|
|
|
// TODO: use static buffer instead of mallocs all the time. it's network!
|
2011-02-04 10:30:08 +00:00
|
|
|
R_API int r_core_serve(RCore *core, RIODesc *file) {
|
2012-02-12 22:45:04 +00:00
|
|
|
ut8 cmd, flg, *ptr = NULL, buf[1024];
|
2011-03-21 00:47:17 +00:00
|
|
|
RSocket *c, *fd;
|
2013-02-11 09:51:45 +00:00
|
|
|
int i, pipefd;
|
2011-04-17 18:58:32 +00:00
|
|
|
RIORap *rior;
|
2013-02-11 09:51:45 +00:00
|
|
|
ut64 x;
|
2013-06-07 08:26:37 +00:00
|
|
|
int LE = 1; // 1 if host is little LE
|
2010-08-22 19:42:08 +00:00
|
|
|
|
2011-04-17 18:58:32 +00:00
|
|
|
rior = (RIORap *)file->data;
|
|
|
|
if (rior == NULL|| rior->fd == NULL) {
|
2010-08-22 19:42:08 +00:00
|
|
|
eprintf ("rap: cannot listen.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-17 18:58:32 +00:00
|
|
|
fd = rior->fd;
|
2010-08-22 19:42:08 +00:00
|
|
|
|
2011-08-04 19:28:07 +00:00
|
|
|
eprintf ("RAP Server started (rap.loop=%s)\n",
|
|
|
|
r_config_get (core->config, "rap.loop"));
|
2010-08-22 19:42:08 +00:00
|
|
|
#if __UNIX__
|
|
|
|
// XXX: ugly workaround
|
2012-12-30 22:40:17 +00:00
|
|
|
//signal (SIGINT, exit);
|
|
|
|
//signal (SIGPIPE, SIG_DFL);
|
2010-08-22 19:42:08 +00:00
|
|
|
#endif
|
2010-08-23 04:39:23 +00:00
|
|
|
reaccept:
|
2011-02-04 23:20:28 +00:00
|
|
|
core->io->plugin = NULL;
|
2012-12-30 22:40:17 +00:00
|
|
|
r_cons_break (rap_break, rior);
|
|
|
|
while (!core->cons->breaked) {
|
|
|
|
c = r_socket_accept (fd);
|
|
|
|
if (!c) break;
|
|
|
|
if (core->cons->breaked)
|
|
|
|
return -1;
|
2011-03-21 00:47:17 +00:00
|
|
|
if (c == NULL) {
|
2010-08-22 19:42:08 +00:00
|
|
|
eprintf ("rap: cannot accept\n");
|
2015-06-28 12:04:41 +00:00
|
|
|
/*r_socket_close (c);*/
|
|
|
|
r_socket_free (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("rap: client connected\n");
|
2012-12-30 22:40:17 +00:00
|
|
|
for (;!core->cons->breaked;) {
|
2011-08-04 19:28:07 +00:00
|
|
|
if (!r_socket_read (c, &cmd, 1)) {
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("rap: connection closed\n");
|
2011-02-04 23:20:28 +00:00
|
|
|
if (r_config_get_i (core->config, "rap.loop")) {
|
|
|
|
eprintf ("rap: waiting for new connection\n");
|
2015-06-28 12:04:41 +00:00
|
|
|
/*r_socket_close (c);*/
|
|
|
|
r_socket_free (c);
|
2010-08-23 04:39:23 +00:00
|
|
|
goto reaccept;
|
2011-02-04 23:20:28 +00:00
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
|
|
|
|
switch ((ut8)cmd) {
|
|
|
|
case RMT_OPEN:
|
|
|
|
r_socket_read_block (c, &flg, 1); // flags
|
|
|
|
eprintf ("open (%d): ", cmd);
|
|
|
|
r_socket_read_block (c, &cmd, 1); // len
|
2010-08-23 04:39:23 +00:00
|
|
|
pipefd = -1;
|
2011-02-12 00:52:41 +00:00
|
|
|
ptr = malloc (cmd);
|
2014-05-03 11:49:19 +00:00
|
|
|
//XXX cmd is ut8..so <256 if (cmd<RMT_MAX)
|
2011-02-12 00:52:41 +00:00
|
|
|
if (ptr == NULL) {
|
|
|
|
eprintf ("Cannot malloc in rmt-open len = %d\n", cmd);
|
|
|
|
} else {
|
|
|
|
RCoreFile *file;
|
2014-08-25 01:58:22 +00:00
|
|
|
ut64 baddr = r_config_get_i (core->config, "bin.laddr");
|
2011-02-12 00:52:41 +00:00
|
|
|
r_socket_read_block (c, ptr, cmd); //filename
|
|
|
|
ptr[cmd] = 0;
|
|
|
|
file = r_core_file_open (core, (const char *)ptr, R_IO_READ, 0); // XXX: write mode?
|
|
|
|
if (file) {
|
2013-11-06 00:32:24 +00:00
|
|
|
r_core_bin_load (core, NULL, baddr);
|
2014-05-28 02:34:12 +00:00
|
|
|
file->map = r_io_map_add (core->io, file->desc->fd,
|
2015-04-01 12:35:43 +00:00
|
|
|
R_IO_READ, 0, 0, r_io_desc_size (core->io, file->desc));
|
|
|
|
if (core->file && core->file->desc) {
|
|
|
|
pipefd = core->file->desc->fd;
|
|
|
|
} else {
|
|
|
|
pipefd = -1;
|
|
|
|
}
|
2012-02-14 23:17:31 +00:00
|
|
|
eprintf ("(flags: %d) len: %d filename: '%s'\n",
|
2010-08-23 04:39:23 +00:00
|
|
|
flg, cmd, ptr); //config.file);
|
2011-02-12 00:52:41 +00:00
|
|
|
} else {
|
|
|
|
pipefd = -1;
|
|
|
|
eprintf ("Cannot open file (%s)\n", ptr);
|
2015-06-28 12:04:41 +00:00
|
|
|
r_socket_close (c);
|
2011-04-19 12:43:48 +00:00
|
|
|
return -1; //XXX: Close conection and goto accept
|
2010-08-23 04:39:23 +00:00
|
|
|
}
|
2011-02-12 00:52:41 +00:00
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
buf[0] = RMT_OPEN | RMT_REPLY;
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian (buf+1, (ut8 *)&pipefd, 4, !LE);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, buf, 5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
|
2012-12-30 22:40:17 +00:00
|
|
|
#if 0
|
2011-08-04 19:28:07 +00:00
|
|
|
/* Write meta info */
|
|
|
|
RMetaItem *d;
|
|
|
|
r_list_foreach (core->anal->meta->data, iter, d) {
|
|
|
|
if (d->type == R_META_TYPE_COMMENT)
|
|
|
|
snprintf ((char *)buf, sizeof (buf), "%s %s @ 0x%08"PFMT64x,
|
|
|
|
r_meta_type_to_string (d->type), d->str, d->from);
|
|
|
|
else
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"%s %d %s @ 0x%08"PFMT64x,
|
|
|
|
r_meta_type_to_string (d->type),
|
|
|
|
(int)(d->to-d->from), d->str, d->from);
|
|
|
|
i = strlen ((char *)buf);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
}
|
2012-12-30 22:40:17 +00:00
|
|
|
#endif
|
2011-08-04 19:28:07 +00:00
|
|
|
#if 0
|
|
|
|
RIOSection *s;
|
|
|
|
r_list_foreach_prev (core->io->sections, iter, s) {
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"S 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" %s %d",
|
|
|
|
s->offset, s->vaddr, s->size, s->vsize, s->name, s->rwx);
|
|
|
|
i = strlen ((char *)buf);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-30 22:40:17 +00:00
|
|
|
#if 0
|
2011-08-04 19:28:07 +00:00
|
|
|
int fs = -1;
|
|
|
|
RFlagItem *flag;
|
|
|
|
r_list_foreach_prev (core->flags->flags, iter, flag) {
|
|
|
|
if (fs == -1 || flag->space != fs) {
|
|
|
|
fs = flag->space;
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"fs %s", r_flag_space_get_i (core->flags, fs));
|
|
|
|
i = strlen ((char *)buf);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
|
|
|
}
|
|
|
|
snprintf ((char *)buf, sizeof (buf),
|
|
|
|
"f %s %"PFMT64d" 0x%08"PFMT64x,
|
|
|
|
flag->name, flag->size, flag->offset);
|
|
|
|
i = strlen ((char *)buf);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-08-04 19:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf ((char *)buf, sizeof (buf), "s 0x%"PFMT64x, core->offset);
|
|
|
|
i = strlen ((char *)buf);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&j, (ut8 *)&i, 4, !LE);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_write (c, (ut8 *)&j, 4);
|
2011-08-04 19:28:07 +00:00
|
|
|
r_socket_write (c, buf, i);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
r_socket_write (c, (ut8 *)&i, 4);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2012-12-30 22:40:17 +00:00
|
|
|
#endif
|
2010-08-23 04:39:23 +00:00
|
|
|
free (ptr);
|
2013-02-25 10:09:24 +00:00
|
|
|
ptr = NULL;
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
case RMT_READ:
|
|
|
|
r_socket_read_block (c, (ut8*)&buf, 4);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8*)&i, buf, 4, !LE);
|
2010-08-23 04:39:23 +00:00
|
|
|
ptr = (ut8 *)malloc (i+core->blocksize+5);
|
2010-08-22 19:42:08 +00:00
|
|
|
if (ptr==NULL) {
|
|
|
|
eprintf ("Cannot read %d bytes\n", i);
|
2014-05-06 20:45:32 +00:00
|
|
|
r_socket_close (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
// TODO: reply error here
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
r_core_block_read (core, 0);
|
|
|
|
ptr[0] = RMT_READ|RMT_REPLY;
|
2010-08-23 04:39:23 +00:00
|
|
|
if (i>RMT_MAX)
|
|
|
|
i = RMT_MAX;
|
2011-08-04 19:28:07 +00:00
|
|
|
if (i>core->blocksize)
|
2010-08-22 19:42:08 +00:00
|
|
|
r_core_block_size (core, i);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian (ptr+1, (ut8 *)&i, 4, !LE);
|
2010-08-22 19:42:08 +00:00
|
|
|
memcpy (ptr+5, core->block, i); //core->blocksize);
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_write (c, ptr, i+5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2014-08-01 13:13:31 +00:00
|
|
|
free(ptr);
|
|
|
|
ptr = NULL;
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RMT_CMD:
|
|
|
|
{
|
|
|
|
char bufr[8], *bufw = NULL;
|
|
|
|
char *cmd = NULL, *cmd_output = NULL;
|
2014-03-01 23:31:35 +00:00
|
|
|
ut32 cmd_len = 0;
|
|
|
|
int i;
|
2010-08-22 19:42:08 +00:00
|
|
|
|
|
|
|
/* read */
|
|
|
|
r_socket_read_block (c, (ut8*)&bufr, 4);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8*)&i, (ut8 *)bufr, 4, !LE);
|
2010-11-08 18:30:25 +00:00
|
|
|
if (i>0 && i<RMT_MAX) {
|
2014-04-19 22:51:42 +00:00
|
|
|
if ((cmd=malloc (i+1))) {
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_read_block (c, (ut8*)cmd, i);
|
|
|
|
cmd[i] = '\0';
|
|
|
|
eprintf ("len: %d cmd: '%s'\n",
|
|
|
|
i, cmd); fflush(stdout);
|
|
|
|
cmd_output = r_core_cmd_str (core, cmd);
|
2011-12-05 23:27:57 +00:00
|
|
|
free (cmd);
|
2010-08-23 04:39:23 +00:00
|
|
|
} else eprintf ("rap: cannot malloc\n");
|
|
|
|
} else eprintf ("rap: invalid length '%d'\n", i);
|
|
|
|
/* write */
|
2014-03-01 23:31:35 +00:00
|
|
|
if (cmd_output) {
|
|
|
|
cmd_len = strlen (cmd_output) + 1;
|
|
|
|
} else {
|
|
|
|
cmd_output = strdup ("");
|
2014-04-19 22:51:42 +00:00
|
|
|
cmd_len = 0;
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
2011-12-05 23:27:57 +00:00
|
|
|
bufw = malloc (cmd_len + 5);
|
2010-08-23 04:39:23 +00:00
|
|
|
bufw[0] = RMT_CMD | RMT_REPLY;
|
2014-03-01 23:31:35 +00:00
|
|
|
r_mem_copyendian ((ut8*)bufw+1,
|
|
|
|
(ut8 *)&cmd_len, 4, !LE);
|
2011-12-05 23:27:57 +00:00
|
|
|
memcpy (bufw+5, cmd_output, cmd_len);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, bufw, cmd_len+5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2011-12-05 23:27:57 +00:00
|
|
|
free (bufw);
|
|
|
|
free (cmd_output);
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RMT_WRITE:
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_read (c, buf, 5);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian((ut8 *)&x, buf+1, 4, LE);
|
2010-08-22 19:42:08 +00:00
|
|
|
ptr = malloc (x);
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_read (c, ptr, x);
|
2010-08-22 19:42:08 +00:00
|
|
|
r_core_write_at (core, core->offset, ptr, x);
|
|
|
|
free (ptr);
|
2013-02-25 10:09:24 +00:00
|
|
|
ptr = NULL;
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
case RMT_SEEK:
|
|
|
|
r_socket_read_block (c, buf, 9);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian((ut8 *)&x, buf+1, 8, !LE);
|
2010-08-23 04:39:23 +00:00
|
|
|
if (buf[0]!=2) {
|
|
|
|
r_core_seek (core, x, buf[0]);
|
|
|
|
x = core->offset;
|
2015-04-07 12:47:20 +00:00
|
|
|
} else {
|
|
|
|
if (core->file) {
|
|
|
|
x = r_io_desc_size (core->io, core->file->desc);
|
|
|
|
} else {
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-08-22 19:42:08 +00:00
|
|
|
buf[0] = RMT_SEEK | RMT_REPLY;
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian (buf+1, (ut8*)&x, 8, !LE);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, buf, 9);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
case RMT_CLOSE:
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("CLOSE\n");
|
2010-08-22 19:42:08 +00:00
|
|
|
// XXX : proper shutdown
|
|
|
|
r_socket_read_block (c, buf, 4);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8*)&i, buf, 4, LE);
|
2010-08-22 19:42:08 +00:00
|
|
|
{
|
2011-03-21 00:47:17 +00:00
|
|
|
//FIXME: Use r_socket_close
|
|
|
|
int ret = close (i);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian (buf+1, (ut8*)&ret, 4, !LE);
|
2010-08-22 19:42:08 +00:00
|
|
|
buf[0] = RMT_CLOSE | RMT_REPLY;
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, buf, 5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RMT_SYSTEM:
|
|
|
|
// read
|
|
|
|
r_socket_read_block (c, buf, 4);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8*)&i, buf, 4, !LE);
|
2010-08-23 04:39:23 +00:00
|
|
|
if (i>0&&i<RMT_MAX) {
|
2014-01-27 00:12:16 +00:00
|
|
|
ptr = (ut8 *) malloc (i+7);
|
2015-01-25 13:35:30 +00:00
|
|
|
if (!ptr) {
|
|
|
|
r_socket_close (c);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2015-01-25 13:35:30 +00:00
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
ptr[5]='!';
|
|
|
|
r_socket_read_block (c, ptr+6, i);
|
|
|
|
ptr[6+i]='\0';
|
|
|
|
//env_update();
|
|
|
|
//pipe_stdout_to_tmp_file((char*)&buf, (char*)ptr+5);
|
2011-12-05 23:27:57 +00:00
|
|
|
strcpy ((char*)buf, "/tmp/.out");
|
2013-11-29 16:27:46 +00:00
|
|
|
pipefd = r_cons_pipe_open ((const char *)buf, 1, 0);
|
2011-02-04 10:30:08 +00:00
|
|
|
//eprintf("SYSTEM(%s)\n", ptr+6);
|
2015-06-20 22:50:53 +00:00
|
|
|
r_sandbox_system ((const char*)ptr+6, 1);
|
2010-08-23 04:39:23 +00:00
|
|
|
r_cons_pipe_close (pipefd);
|
|
|
|
{
|
2012-10-19 22:31:18 +00:00
|
|
|
FILE *fd = r_sandbox_fopen((char*)buf, "r");
|
2014-01-27 00:12:16 +00:00
|
|
|
i = 0;
|
|
|
|
if (fd) {
|
2011-12-05 23:27:57 +00:00
|
|
|
fseek (fd, 0, SEEK_END);
|
|
|
|
i = ftell (fd);
|
|
|
|
fseek (fd, 0, SEEK_SET);
|
|
|
|
free (ptr);
|
2014-01-27 14:24:44 +00:00
|
|
|
ptr = NULL; // potential use after free if i == 0
|
2014-01-27 00:12:16 +00:00
|
|
|
if (i>0) {
|
2014-05-22 09:10:36 +00:00
|
|
|
int r;
|
2014-04-19 22:54:53 +00:00
|
|
|
ptr = (ut8 *) malloc (i+6);
|
2014-05-26 22:06:29 +00:00
|
|
|
if (!ptr) {
|
|
|
|
fclose (fd);
|
2015-04-18 23:38:41 +00:00
|
|
|
r_socket_close (c);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2014-05-26 22:06:29 +00:00
|
|
|
}
|
2014-05-22 09:10:36 +00:00
|
|
|
r = fread (ptr+5, i, 1, fd);
|
|
|
|
ptr[5+r]='\0';
|
2014-01-27 00:12:16 +00:00
|
|
|
}
|
2011-12-05 23:27:57 +00:00
|
|
|
fclose (fd);
|
2014-01-27 00:12:16 +00:00
|
|
|
} else {
|
|
|
|
eprintf ("Cannot open tmpfile\n");
|
|
|
|
i = -1;
|
2010-08-23 04:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
char *out = r_file_slurp ((char*)buf, &i);
|
2011-12-05 23:27:57 +00:00
|
|
|
free (ptr);
|
2011-02-04 10:30:08 +00:00
|
|
|
//eprintf("PIPE(%s)\n", out);
|
2011-12-05 23:27:57 +00:00
|
|
|
ptr = (ut8 *) malloc (i+5);
|
2010-08-23 04:39:23 +00:00
|
|
|
if (ptr) {
|
|
|
|
memcpy (ptr+5, out, i);
|
|
|
|
}
|
2014-01-16 23:14:59 +00:00
|
|
|
free (out);
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
2010-08-23 04:39:23 +00:00
|
|
|
//unlink((char*)buf);
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-05 23:27:57 +00:00
|
|
|
if (!ptr) ptr = (ut8 *) malloc (5); // malloc for 5 byets? c'mon!
|
2015-09-14 10:35:38 +00:00
|
|
|
if (!ptr) return false;
|
2014-05-03 11:49:19 +00:00
|
|
|
|
2010-08-22 19:42:08 +00:00
|
|
|
// send
|
|
|
|
ptr[0] = (RMT_SYSTEM | RMT_REPLY);
|
2013-06-07 08:26:37 +00:00
|
|
|
r_mem_copyendian ((ut8*)ptr+1, (ut8*)&i, 4, !LE);
|
2011-12-05 23:27:57 +00:00
|
|
|
if (i<0) i = 0;
|
2010-08-23 04:39:23 +00:00
|
|
|
r_socket_write (c, ptr, i+5);
|
2011-08-16 00:55:46 +00:00
|
|
|
r_socket_flush (c);
|
2010-08-22 19:42:08 +00:00
|
|
|
eprintf ("REPLY SENT (%d) (%s)\n", i, ptr+5);
|
|
|
|
free (ptr);
|
2012-05-31 00:41:45 +00:00
|
|
|
ptr = NULL;
|
2010-08-22 19:42:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf ("unknown command 0x%02x\n", cmd);
|
|
|
|
r_socket_close (c);
|
2014-01-16 23:14:59 +00:00
|
|
|
free (ptr);
|
|
|
|
ptr = NULL;
|
2010-08-22 19:42:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2012-12-30 22:40:17 +00:00
|
|
|
r_cons_break_end ();
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("client: disconnected\n");
|
2010-08-22 19:42:08 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-15 10:06:10 +00:00
|
|
|
|
|
|
|
R_API int r_core_search_cb(RCore *core, ut64 from, ut64 to, RCoreSearchCallback cb) {
|
|
|
|
int ret, len = core->blocksize;
|
|
|
|
ut8 *buf;
|
|
|
|
if ((buf = malloc (len)) == NULL)
|
|
|
|
eprintf ("Cannot allocate blocksize\n");
|
|
|
|
else while (from<to) {
|
|
|
|
ut64 delta = to-from;
|
|
|
|
if (delta<len)
|
|
|
|
len = (int)delta;
|
|
|
|
if (!r_io_read_at (core->io, from, buf, len)) {
|
|
|
|
eprintf ("Cannot read at 0x%"PFMT64x"\n", from);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (ret=0; ret<len;) {
|
|
|
|
int done = cb (core, from, buf+ret, len-ret);
|
2012-09-26 08:01:43 +00:00
|
|
|
if (done<1) { /* interrupted */
|
|
|
|
free (buf);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2012-09-26 08:01:43 +00:00
|
|
|
}
|
2010-11-15 10:06:10 +00:00
|
|
|
ret += done;
|
|
|
|
}
|
|
|
|
from += len;
|
|
|
|
}
|
2012-09-26 08:01:43 +00:00
|
|
|
free (buf);
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2010-11-15 10:06:10 +00:00
|
|
|
}
|
2011-05-21 12:27:46 +00:00
|
|
|
|
2014-12-19 02:17:28 +00:00
|
|
|
R_API char *r_core_editor (const RCore *core, const char *file, const char *str) {
|
2015-03-31 00:26:36 +00:00
|
|
|
const char *editor = r_config_get (core->config, "cfg.editor");
|
2013-09-03 22:35:48 +00:00
|
|
|
char *name, *ret = NULL;
|
|
|
|
int len, fd;
|
2015-03-31 00:26:36 +00:00
|
|
|
|
|
|
|
if (!editor || !*editor) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-19 02:17:28 +00:00
|
|
|
if (file) {
|
|
|
|
name = strdup (file);
|
2014-12-19 12:01:44 +00:00
|
|
|
fd = r_sandbox_open (file, O_RDWR, 0644);
|
2014-12-19 02:17:28 +00:00
|
|
|
} else {
|
2014-12-19 16:49:42 +00:00
|
|
|
name = NULL;
|
2014-12-19 02:17:28 +00:00
|
|
|
fd = r_file_mkstemp ("r2ed", &name);
|
|
|
|
}
|
2014-12-19 16:49:42 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
free (name);
|
2011-05-21 12:27:46 +00:00
|
|
|
return NULL;
|
2014-12-19 16:49:42 +00:00
|
|
|
}
|
2011-05-21 12:27:46 +00:00
|
|
|
if (str) write (fd, str, strlen (str));
|
|
|
|
close (fd);
|
2013-09-03 22:35:48 +00:00
|
|
|
|
2015-03-31 00:26:36 +00:00
|
|
|
if (name && (!editor || !*editor || !strcmp (editor, "-"))) {
|
2014-12-19 02:17:28 +00:00
|
|
|
r_cons_editor (name, NULL);
|
2015-03-31 00:26:36 +00:00
|
|
|
} else {
|
|
|
|
if (editor && name)
|
|
|
|
r_sys_cmdf ("%s '%s'", editor, name);
|
|
|
|
}
|
|
|
|
ret = name? r_file_slurp (name, &len): 0;
|
2014-03-02 01:08:16 +00:00
|
|
|
if (ret) {
|
2014-08-25 20:56:48 +00:00
|
|
|
if (len && ret[len - 1] == '\n')
|
2014-03-02 01:08:16 +00:00
|
|
|
ret[len-1] = 0; // chop
|
2014-12-19 02:17:28 +00:00
|
|
|
if (!file) {
|
|
|
|
r_file_rm (name);
|
|
|
|
}
|
2014-03-02 01:08:16 +00:00
|
|
|
}
|
2011-05-21 12:27:46 +00:00
|
|
|
free (name);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-06-12 22:51:16 +00:00
|
|
|
|
|
|
|
/* weak getters */
|
|
|
|
R_API RCons *r_core_get_cons (RCore *core) { return core->cons; }
|
|
|
|
R_API RConfig *r_core_get_config (RCore *core) { return core->config; }
|
2012-09-18 01:39:32 +00:00
|
|
|
R_API RBin *r_core_get_bin (RCore *core) { return core->bin; }
|
2013-08-29 02:44:18 +00:00
|
|
|
|
2013-09-15 00:24:23 +00:00
|
|
|
R_API RBuffer *r_core_syscallf (RCore *core, const char *name, const char *fmt, ...) {
|
|
|
|
char str[1024];
|
|
|
|
RBuffer *buf;
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
vsnprintf (str, sizeof (str), fmt, ap);
|
|
|
|
buf = r_core_syscall (core, name, str);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:44:18 +00:00
|
|
|
R_API RBuffer *r_core_syscall (RCore *core, const char *name, const char *args) {
|
|
|
|
int i, num;
|
2013-09-18 00:11:23 +00:00
|
|
|
RBuffer *b = NULL;
|
2013-09-04 00:01:42 +00:00
|
|
|
char code[1024];
|
2013-08-29 02:44:18 +00:00
|
|
|
|
|
|
|
num = r_syscall_get_num (core->anal->syscall, name);
|
2015-08-26 01:07:20 +00:00
|
|
|
if (!num) {
|
|
|
|
num = atoi (name);
|
|
|
|
}
|
2013-08-29 02:44:18 +00:00
|
|
|
snprintf (code, sizeof (code),
|
2015-08-26 01:07:20 +00:00
|
|
|
"sc@syscall(%d);\n"
|
|
|
|
"main@global(0) { sc(%s);\n"
|
2013-08-29 17:46:48 +00:00
|
|
|
":int3\n" /// XXX USE trap
|
|
|
|
"}\n", num, args);
|
2013-08-29 02:44:18 +00:00
|
|
|
r_egg_reset (core->egg);
|
|
|
|
// TODO: setup arch/bits/os?
|
|
|
|
r_egg_load (core->egg, code, 0);
|
2015-08-26 01:07:20 +00:00
|
|
|
|
2013-08-29 02:44:18 +00:00
|
|
|
if (!r_egg_compile (core->egg))
|
2013-12-17 01:10:13 +00:00
|
|
|
eprintf ("Cannot compile.\n");
|
|
|
|
if (!r_egg_assemble (core->egg))
|
2013-08-29 02:44:18 +00:00
|
|
|
eprintf ("r_egg_assemble: invalid assembly\n");
|
|
|
|
if ((b = r_egg_get_bin (core->egg))) {
|
|
|
|
if (b->length>0) {
|
|
|
|
for (i=0; i<b->length; i++)
|
|
|
|
r_cons_printf ("%02x", b->buf[i]);
|
|
|
|
r_cons_printf ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|