2018-01-26 16:33:18 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2018 - pancake */
|
2016-05-09 08:58:06 +00:00
|
|
|
|
2016-05-03 02:52:41 +00:00
|
|
|
#include "r_types.h"
|
|
|
|
#include "r_config.h"
|
|
|
|
#include "r_cons.h"
|
|
|
|
#include "r_core.h"
|
|
|
|
#include "r_debug.h"
|
|
|
|
#include "r_io.h"
|
2012-02-27 01:40:27 +00:00
|
|
|
|
2017-07-26 13:15:13 +00:00
|
|
|
static const char *help_msg_s[] = {
|
|
|
|
"Usage: s", "", " # Help for the seek commands. See ?$? to see all variables",
|
|
|
|
"s", "", "Print current address",
|
|
|
|
"s:", "pad", "Print current address with N padded zeros (defaults to 8)",
|
|
|
|
"s", " addr", "Seek to address",
|
|
|
|
"s-", "", "Undo seek",
|
2017-07-28 04:12:01 +00:00
|
|
|
"s-*", "", "Reset undo seek history",
|
2017-07-26 13:15:13 +00:00
|
|
|
"s-", " n", "Seek n bytes backward",
|
2017-08-01 10:18:16 +00:00
|
|
|
"s--", "[n]", "Seek blocksize bytes backward (/=n)",
|
2017-07-26 13:15:13 +00:00
|
|
|
"s+", "", "Redo seek",
|
|
|
|
"s+", " n", "Seek n bytes forward",
|
2017-08-01 10:18:16 +00:00
|
|
|
"s++", "[n]", "Seek blocksize bytes forward (/=n)",
|
2017-07-26 13:15:13 +00:00
|
|
|
"s[j*=!]", "", "List undo seek history (JSON, =list, *r2, !=names, s==)",
|
|
|
|
"s/", " DATA", "Search for next occurrence of 'DATA'",
|
|
|
|
"s/x", " 9091", "Search for next occurrence of \\x90\\x91",
|
|
|
|
"s.", "hexoff", "Seek honoring a base from core->offset",
|
|
|
|
"sa", " [[+-]a] [asz]", "Seek asz (or bsize) aligned to addr",
|
|
|
|
"sb", "", "Seek aligned to bb start",
|
|
|
|
"sC", "[?] string", "Seek to comment matching given string",
|
|
|
|
"sf", "", "Seek to next function (f->addr+f->size)",
|
|
|
|
"sf", " function", "Seek to address of specified function",
|
2017-09-04 00:59:07 +00:00
|
|
|
"sf.", "", "Seek to the beginning of current function",
|
2017-07-26 13:15:13 +00:00
|
|
|
"sg/sG", "", "Seek begin (sg) or end (sG) of section or file",
|
|
|
|
"sl", "[?] [+-]line", "Seek to line",
|
2017-11-23 13:41:11 +00:00
|
|
|
"sn/sp", " ([nkey])", "Seek to next/prev location, as specified by scr.nkey",
|
2017-07-26 13:15:13 +00:00
|
|
|
"so", " [N]", "Seek to N next opcode(s)",
|
|
|
|
"sr", " pc", "Seek to register",
|
|
|
|
"ss", "", "Seek silently (without adding an entry to the seek history)",
|
|
|
|
// "sp [page] seek page N (page = block)",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *help_msg_sC[] = {
|
|
|
|
"Usage:", "sC", "Comment grep",
|
|
|
|
"sC", "*", "List all comments",
|
|
|
|
"sC", " str", "Seek to the first comment matching 'str'",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *help_msg_sl[] = {
|
|
|
|
"Usage:", "sl+ or sl- or slc", "",
|
|
|
|
"sl", " [line]", "Seek to absolute line",
|
|
|
|
"sl", "[+-][line]", "Seek to relative line",
|
|
|
|
"slc", "", "Clear line cache",
|
|
|
|
"sll", "", "Show total number of lines",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *help_msg_ss[] = {
|
|
|
|
"Usage: ss", "", " # Seek silently (not recorded in the seek history)",
|
|
|
|
"s?", "", "Works with all s subcommands",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-07-27 12:52:17 +00:00
|
|
|
static void cmd_seek_init(RCore *core) {
|
|
|
|
DEFINE_CMD_DESCRIPTOR (core, s);
|
|
|
|
DEFINE_CMD_DESCRIPTOR (core, sC);
|
|
|
|
DEFINE_CMD_DESCRIPTOR (core, sl);
|
|
|
|
DEFINE_CMD_DESCRIPTOR (core, ss);
|
2017-07-26 13:15:13 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
static void __init_seek_line(RCore *core) {
|
2015-12-22 19:51:06 +00:00
|
|
|
ut64 from, to;
|
|
|
|
|
|
|
|
r_config_bump (core->config, "lines.to");
|
|
|
|
from = r_config_get_i (core->config, "lines.from");
|
|
|
|
to = r_config_get_i (core->config, "lines.to");
|
2016-01-02 17:18:48 +00:00
|
|
|
if (r_core_lines_initcache (core, from, to) == -1) {
|
2015-12-22 19:51:06 +00:00
|
|
|
eprintf ("ERROR: \"lines.from\" and \"lines.to\" must be set\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 11:25:08 +00:00
|
|
|
static void printPadded(RCore *core, int pad) {
|
|
|
|
if (pad < 1) {
|
|
|
|
pad = 8;
|
|
|
|
}
|
|
|
|
char *fmt = r_str_newf ("0x%%0%d" PFMT64x, pad);
|
|
|
|
char *off = r_str_newf (fmt, core->offset);
|
|
|
|
r_cons_printf ("%s\n", off);
|
|
|
|
free (off);
|
|
|
|
free (fmt);
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
static void __get_current_line(RCore *core) {
|
2015-12-22 19:51:06 +00:00
|
|
|
if (core->print->lines_cache_sz > 0) {
|
|
|
|
int curr = r_util_lines_getline (core->print->lines_cache, core->print->lines_cache_sz, core->offset);
|
|
|
|
r_cons_printf ("%d\n", curr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
static void __seek_line_absolute(RCore *core, int numline) {
|
2015-12-22 19:51:06 +00:00
|
|
|
if (numline < 1 || numline > core->print->lines_cache_sz - 1) {
|
2017-03-19 01:34:53 +00:00
|
|
|
eprintf ("ERROR: Line must be between 1 and %d\n", core->print->lines_cache_sz - 1);
|
2015-12-22 19:51:06 +00:00
|
|
|
} else {
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_seek (core, core->print->lines_cache[numline - 1], 1);
|
2015-12-22 19:51:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
static void __seek_line_relative(RCore *core, int numlines) {
|
2015-12-22 19:51:06 +00:00
|
|
|
int curr = r_util_lines_getline (core->print->lines_cache, core->print->lines_cache_sz, core->offset);
|
2017-03-19 01:34:53 +00:00
|
|
|
if (numlines > 0 && curr + numlines >= core->print->lines_cache_sz - 1) {
|
|
|
|
eprintf ("ERROR: Line must be < %d\n", core->print->lines_cache_sz - 1);
|
|
|
|
} else if (numlines < 0 && curr + numlines < 1) {
|
2015-12-22 19:51:06 +00:00
|
|
|
eprintf ("ERROR: Line must be > 1\n");
|
|
|
|
} else {
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_seek (core, core->print->lines_cache[curr + numlines - 1], 1);
|
2015-12-22 19:51:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
static void __clean_lines_cache(RCore *core) {
|
2015-12-22 19:51:06 +00:00
|
|
|
core->print->lines_cache_sz = -1;
|
|
|
|
R_FREE (core->print->lines_cache);
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
R_API int r_core_lines_currline(RCore *core) { // make priv8 again
|
2015-12-21 21:27:05 +00:00
|
|
|
int imin = 0;
|
2015-12-22 19:51:06 +00:00
|
|
|
int imax = core->print->lines_cache_sz;
|
2015-12-21 21:27:05 +00:00
|
|
|
int imid = 0;
|
|
|
|
|
|
|
|
while (imin <= imax) {
|
|
|
|
imid = imin + ((imax - imin) / 2);
|
2015-12-22 19:51:06 +00:00
|
|
|
if (core->print->lines_cache[imid] == core->offset) {
|
2015-12-21 21:27:05 +00:00
|
|
|
return imid;
|
2017-03-19 01:34:53 +00:00
|
|
|
} else if (core->print->lines_cache[imid] < core->offset) {
|
2015-12-21 21:27:05 +00:00
|
|
|
imin = imid + 1;
|
2017-03-19 01:34:53 +00:00
|
|
|
} else {
|
2015-12-21 21:27:05 +00:00
|
|
|
imax = imid - 1;
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
2015-12-21 21:27:05 +00:00
|
|
|
}
|
|
|
|
return imin;
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
R_API int r_core_lines_initcache(RCore *core, ut64 start_addr, ut64 end_addr) {
|
2015-12-22 19:51:06 +00:00
|
|
|
int i, line_count;
|
|
|
|
int bsz = core->blocksize;
|
|
|
|
char *buf;
|
|
|
|
ut64 off = start_addr;
|
2017-03-19 01:34:53 +00:00
|
|
|
ut64 baddr;
|
2015-12-22 19:51:06 +00:00
|
|
|
if (start_addr == UT64_MAX || end_addr == UT64_MAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (core->print->lines_cache);
|
|
|
|
core->print->lines_cache = R_NEWS0 (ut64, bsz);
|
|
|
|
if (!core->print->lines_cache) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:42:16 +00:00
|
|
|
baddr = r_config_get_i (core->config, "bin.baddr");
|
2016-01-03 02:10:09 +00:00
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
line_count = start_addr? 0: 1;
|
|
|
|
core->print->lines_cache[0] = start_addr? 0: baddr;
|
2015-12-22 19:51:06 +00:00
|
|
|
buf = malloc (bsz);
|
2016-11-20 18:20:14 +00:00
|
|
|
if (!buf) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
r_cons_break_push (NULL, NULL);
|
2015-12-22 19:51:06 +00:00
|
|
|
while (off < end_addr) {
|
2016-11-20 18:20:14 +00:00
|
|
|
if (r_cons_is_breaked ()) {
|
2015-12-22 19:51:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
r_io_read_at (core->io, off, (ut8 *) buf, bsz);
|
2016-11-20 18:20:14 +00:00
|
|
|
for (i = 0; i < bsz; i++) {
|
2015-12-22 19:51:06 +00:00
|
|
|
if (buf[i] == '\n') {
|
2017-03-19 01:34:53 +00:00
|
|
|
core->print->lines_cache[line_count] = start_addr? off + i + 1: off + i + 1 + baddr;
|
2015-12-22 19:51:06 +00:00
|
|
|
line_count++;
|
|
|
|
if (line_count % bsz == 0) {
|
|
|
|
ut64 *tmp = realloc (core->print->lines_cache,
|
2017-03-27 11:12:59 +00:00
|
|
|
(line_count + bsz) * sizeof (ut64));
|
2015-12-22 19:51:06 +00:00
|
|
|
if (tmp) {
|
|
|
|
core->print->lines_cache = tmp;
|
|
|
|
} else {
|
|
|
|
R_FREE (core->print->lines_cache);
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
off += bsz;
|
|
|
|
}
|
|
|
|
free (buf);
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2015-12-22 19:51:06 +00:00
|
|
|
return line_count;
|
|
|
|
beach:
|
|
|
|
free (buf);
|
2016-11-20 18:20:14 +00:00
|
|
|
r_cons_break_pop ();
|
2015-12-22 19:51:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
static void seek_to_register(RCore *core, const char *input, bool is_silent) {
|
|
|
|
ut64 off;
|
|
|
|
if (core->io->debug) {
|
|
|
|
off = r_debug_reg_get (core->dbg, input);
|
|
|
|
if (!is_silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
|
|
|
r_core_seek (core, off, 1);
|
|
|
|
} else {
|
|
|
|
RReg *orig = core->dbg->reg;
|
|
|
|
core->dbg->reg = core->anal->reg;
|
|
|
|
off = r_debug_reg_get (core->dbg, input);
|
|
|
|
core->dbg->reg = orig;
|
|
|
|
if (!is_silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
|
|
|
r_core_seek (core, off, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 09:41:47 +00:00
|
|
|
static int cmd_seek_opcode_backward(RCore *core, int numinstr) {
|
|
|
|
int i, val = 0;
|
2018-06-03 16:53:58 +00:00
|
|
|
// N previous instructions
|
|
|
|
ut64 addr = core->offset;
|
|
|
|
int ret = 0;
|
|
|
|
if (r_core_prevop_addr (core, core->offset, numinstr, &addr)) {
|
|
|
|
ret = core->offset - addr;
|
|
|
|
} else {
|
|
|
|
#if 0
|
|
|
|
// core_asm_bwdis_len is buggy as hell we should kill it. seems like prevop_addr
|
|
|
|
// works as expected, because is the one used from visual
|
|
|
|
ret = r_core_asm_bwdis_len (core, &instr_len, &addr, numinstr);
|
|
|
|
#endif
|
2018-08-10 09:41:47 +00:00
|
|
|
addr = core->offset;
|
|
|
|
const int mininstrsize = r_anal_archinfo (core->anal, R_ANAL_ARCHINFO_MIN_OP_SIZE);
|
|
|
|
for (i = 0; i < numinstr; i++) {
|
2018-12-12 21:03:32 +00:00
|
|
|
ut64 prev_addr = r_core_prevop_addr_force (core, addr, 1);
|
2018-08-10 09:41:47 +00:00
|
|
|
if (prev_addr == UT64_MAX) {
|
|
|
|
prev_addr = addr - mininstrsize;
|
|
|
|
}
|
|
|
|
if (prev_addr == UT64_MAX || prev_addr >= core->offset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
RAsmOp op = {0};
|
2018-06-03 16:53:58 +00:00
|
|
|
r_core_seek (core, prev_addr, 1);
|
2018-08-10 09:41:47 +00:00
|
|
|
r_asm_disassemble (core->assembler, &op, core->block, 32);
|
|
|
|
if (op.size < mininstrsize) {
|
|
|
|
op.size = mininstrsize;
|
|
|
|
}
|
2018-06-03 16:53:58 +00:00
|
|
|
val += op.size;
|
2018-08-10 09:41:47 +00:00
|
|
|
addr = prev_addr;
|
2018-06-03 16:53:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
r_core_seek (core, addr, true);
|
|
|
|
val += ret;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_seek_opcode_forward (RCore *core, int n) {
|
|
|
|
// N forward instructions
|
|
|
|
int i, ret, val = 0;
|
|
|
|
for (val = i = 0; i < n; i++) {
|
|
|
|
RAnalOp op;
|
2018-08-09 14:32:11 +00:00
|
|
|
ret = r_anal_op (core->anal, &op, core->offset, core->block,
|
|
|
|
core->blocksize, R_ANAL_OP_MASK_BASIC);
|
2018-06-03 16:53:58 +00:00
|
|
|
if (ret < 1) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
r_core_seek_delta (core, ret);
|
|
|
|
r_anal_op_fini (&op);
|
|
|
|
val += ret;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_seek_opcode(RCore *core, const char *input) {
|
|
|
|
if (input[0] == '?') {
|
|
|
|
eprintf ("Usage: so [-][n]\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp (input, "-")) {
|
|
|
|
input = "-1";
|
|
|
|
}
|
|
|
|
int n = r_num_math (core->num, input);
|
|
|
|
if (n == 0) {
|
|
|
|
n = 1;
|
|
|
|
}
|
|
|
|
int val = (n < 0)
|
2018-08-10 09:41:47 +00:00
|
|
|
? cmd_seek_opcode_backward (core, -n)
|
2018-06-03 16:53:58 +00:00
|
|
|
: cmd_seek_opcode_forward (core, n);
|
|
|
|
core->num->value = val;
|
|
|
|
}
|
|
|
|
|
2012-02-27 01:40:27 +00:00
|
|
|
static int cmd_seek(void *data, const char *input) {
|
2017-03-19 01:34:53 +00:00
|
|
|
RCore *core = (RCore *) data;
|
2012-02-27 01:40:27 +00:00
|
|
|
char *cmd, *p;
|
2017-12-22 11:36:02 +00:00
|
|
|
ut64 off = core->offset;
|
2012-02-27 01:40:27 +00:00
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
if (!*input) {
|
|
|
|
r_cons_printf ("0x%"PFMT64x "\n", core->offset);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
char *ptr;
|
|
|
|
if ((ptr = strstr (input, "+.")) != NULL) {
|
|
|
|
char *dup = strdup (input);
|
|
|
|
dup[ptr - input] = '\x00';
|
|
|
|
off = r_num_math (core->num, dup + 1);
|
|
|
|
core->offset = off;
|
|
|
|
free (dup);
|
|
|
|
}
|
|
|
|
const char *inputnum = strchr (input, ' ');
|
|
|
|
{
|
|
|
|
const char *u_num = inputnum? inputnum + 1: input + 1;
|
|
|
|
off = r_num_math (core->num, u_num);
|
|
|
|
if (*u_num == '-') {
|
2017-12-18 23:17:43 +00:00
|
|
|
off = -(st64)off;
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
2018-02-09 22:36:52 +00:00
|
|
|
#if 1
|
|
|
|
// int sign = 1;
|
2017-03-19 01:34:53 +00:00
|
|
|
if (input[0] == ' ') {
|
|
|
|
switch (input[1]) {
|
2017-04-10 11:17:38 +00:00
|
|
|
case '-':
|
2018-02-09 22:36:52 +00:00
|
|
|
// sign = -1;
|
2017-04-10 11:17:38 +00:00
|
|
|
/* pass thru */
|
|
|
|
case '+':
|
|
|
|
input++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 17:45:54 +00:00
|
|
|
#endif
|
2017-04-10 11:17:38 +00:00
|
|
|
bool silent = false;
|
|
|
|
if (*input == 's') {
|
|
|
|
silent = true;
|
|
|
|
input++;
|
|
|
|
if (*input == '?') {
|
2017-07-26 13:15:13 +00:00
|
|
|
r_core_cmd_help (core, help_msg_ss);
|
2017-04-10 11:17:38 +00:00
|
|
|
return 0;
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
2012-02-27 01:40:27 +00:00
|
|
|
|
2017-03-19 01:34:53 +00:00
|
|
|
switch (*input) {
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'r': // "sr"
|
2017-04-10 14:39:10 +00:00
|
|
|
if (input[1] && input[2]) {
|
|
|
|
seek_to_register (core, input + 2, silent);
|
|
|
|
} else {
|
|
|
|
eprintf ("|Usage| 'sr PC' seek to program counter register\n");
|
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'C': // "sC"
|
|
|
|
if (input[1] == '*') { // "sC*"
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_cmd0 (core, "C*~^\"CC");
|
|
|
|
} else if (input[1] == ' ') {
|
|
|
|
typedef struct {
|
2014-03-12 01:44:49 +00:00
|
|
|
ut64 addr;
|
2017-03-19 01:34:53 +00:00
|
|
|
char *str;
|
|
|
|
} MetaCallback;
|
|
|
|
int count = 0;
|
|
|
|
MetaCallback cb = {
|
|
|
|
0, NULL
|
|
|
|
};
|
|
|
|
ut64 addr;
|
|
|
|
char key[128];
|
|
|
|
const char *val, *comma;
|
|
|
|
char *list = sdb_get (core->anal->sdb_meta, "meta.C", 0);
|
|
|
|
char *str, *next, *cur = list;
|
|
|
|
if (list) {
|
|
|
|
for (;;) {
|
|
|
|
cur = sdb_anext (cur, &next);
|
|
|
|
addr = sdb_atoi (cur);
|
|
|
|
snprintf (key, sizeof (key) - 1, "meta.C.0x%"PFMT64x, addr);
|
|
|
|
val = sdb_const_get (core->anal->sdb_meta, key, 0);
|
|
|
|
if (val) {
|
|
|
|
comma = strchr (val, ',');
|
|
|
|
if (comma) {
|
|
|
|
str = (char *) sdb_decode (comma + 1, 0);
|
|
|
|
if (strstr (str, input + 2)) {
|
|
|
|
r_cons_printf ("0x%08"PFMT64x " %s\n", addr, str);
|
|
|
|
count++;
|
|
|
|
cb.addr = addr;
|
|
|
|
free (cb.str);
|
|
|
|
cb.str = str;
|
|
|
|
} else {
|
|
|
|
free (str);
|
2014-04-02 16:07:47 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("sdb_const_get key not found '%s'\n", key);
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
if (!next) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur = next;
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2016-12-21 08:02:55 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 0:
|
|
|
|
eprintf ("No matching comments\n");
|
2012-06-10 21:58:34 +00:00
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
case 1:
|
|
|
|
off = cb.addr;
|
2017-04-10 11:17:38 +00:00
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_seek (core, off, 1);
|
|
|
|
r_core_block_read (core);
|
2015-03-03 21:34:44 +00:00
|
|
|
break;
|
2012-12-30 00:05:12 +00:00
|
|
|
default:
|
2017-03-19 01:34:53 +00:00
|
|
|
eprintf ("Too many results\n");
|
2012-06-10 21:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
free (cb.str);
|
|
|
|
} else {
|
2017-07-26 13:15:13 +00:00
|
|
|
r_core_cmd_help (core, help_msg_sC);
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case ' ': // "s "
|
2018-05-06 15:25:06 +00:00
|
|
|
{
|
|
|
|
ut64 addr = r_num_math (core->num, input + 1);
|
2018-08-21 03:42:24 +00:00
|
|
|
if (core->num->nc.errors) {
|
|
|
|
if (r_cons_singleton ()->is_interactive) {
|
|
|
|
eprintf ("Cannot seek to unknown address '%s'\n", core->num->nc.calc_buf);
|
|
|
|
}
|
2018-05-06 15:25:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-10 11:17:38 +00:00
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
2018-05-06 15:25:06 +00:00
|
|
|
r_core_seek (core, addr, 1);
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_block_read (core);
|
2018-05-06 15:25:06 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '/': // "s/"
|
2017-03-19 01:34:53 +00:00
|
|
|
{
|
|
|
|
const char *pfx = r_config_get (core->config, "search.prefix");
|
2017-12-24 17:10:03 +00:00
|
|
|
const ut64 saved_from = r_config_get_i (core->config, "search.from");
|
|
|
|
const ut64 saved_maxhits = r_config_get_i (core->config, "search.maxhits");
|
2017-03-19 01:34:53 +00:00
|
|
|
// kwidx cfg var is ignored
|
|
|
|
int kwidx = core->search->n_kws; // (int)r_config_get_i (core->config, "search.kwidx")-1;
|
|
|
|
if (kwidx < 0) {
|
|
|
|
kwidx = 0;
|
|
|
|
}
|
|
|
|
switch (input[1]) {
|
|
|
|
case ' ':
|
|
|
|
case 'v':
|
|
|
|
case 'V':
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
case 'z':
|
|
|
|
case 'm':
|
|
|
|
case 'c':
|
|
|
|
case 'A':
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'i':
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
|
|
|
case '/':
|
|
|
|
case 'x':
|
|
|
|
r_config_set_i (core->config, "search.from", core->offset + 1);
|
2017-09-18 00:04:01 +00:00
|
|
|
r_config_set_i (core->config, "search.maxhits", 1);
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_cmdf (core, "s+1; %s; s-1; s %s%d_0; f-%s%d_0",
|
|
|
|
input, pfx, kwidx, pfx, kwidx, pfx, kwidx);
|
2017-09-18 00:04:01 +00:00
|
|
|
r_config_set_i (core->config, "search.from", saved_from);
|
|
|
|
r_config_set_i (core->config, "search.maxhits", saved_maxhits);
|
2012-02-27 01:40:27 +00:00
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
case '?':
|
|
|
|
eprintf ("Usage: s/.. arg.\n");
|
|
|
|
r_cons_printf ("/?\n");
|
2012-02-27 01:40:27 +00:00
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
default:
|
|
|
|
eprintf ("unknown search method\n");
|
2012-02-27 01:40:27 +00:00
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-05-25 14:46:44 +00:00
|
|
|
case '.': // "s." "s.."
|
2017-03-19 01:34:53 +00:00
|
|
|
for (input++; *input == '.'; input++) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
r_core_seek_base (core, input);
|
2018-04-02 20:41:12 +00:00
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
2017-03-19 01:34:53 +00:00
|
|
|
break;
|
2017-07-27 00:26:54 +00:00
|
|
|
case 'j': // "sj"
|
2017-06-19 06:35:25 +00:00
|
|
|
{
|
|
|
|
RList /*<ut64 *>*/ *addrs = r_list_newf (free);
|
|
|
|
RList /*<char *>*/ *names = r_list_newf (free);
|
|
|
|
RList *list = r_io_sundo_list (core->io, '!');
|
|
|
|
ut64 lsz = 0;
|
|
|
|
ut64 i;
|
|
|
|
RListIter *iter;
|
|
|
|
RIOUndos *undo;
|
|
|
|
if (list) {
|
|
|
|
r_list_foreach (list, iter, undo) {
|
|
|
|
char *name = NULL;
|
|
|
|
|
|
|
|
RFlagItem *f = r_flag_get_at (core->flags, undo->off, true);
|
|
|
|
if (f) {
|
|
|
|
if (f->offset != undo->off) {
|
2017-07-27 16:38:28 +00:00
|
|
|
name = r_str_newf ("%s+%d", f->name,
|
2017-06-19 06:35:25 +00:00
|
|
|
(int)(undo->off- f->offset));
|
|
|
|
} else {
|
|
|
|
name = strdup (f->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!name) {
|
|
|
|
name = strdup ("");
|
|
|
|
}
|
|
|
|
ut64 *val = malloc (sizeof (ut64));
|
|
|
|
if (!val) {
|
2017-06-21 05:00:44 +00:00
|
|
|
free (name);
|
2017-06-19 06:35:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*val = undo->off;
|
|
|
|
r_list_append (addrs, val);
|
|
|
|
r_list_append (names, strdup (name));
|
|
|
|
lsz++;
|
|
|
|
free (name);
|
|
|
|
}
|
|
|
|
r_list_free (list);
|
|
|
|
}
|
|
|
|
r_cons_printf ("[");
|
|
|
|
for (i = 0; i < lsz; ++i) {
|
|
|
|
ut64 *addr = r_list_get_n (addrs, i);
|
|
|
|
const char *name = r_list_get_n (names, i);
|
|
|
|
// XXX(should the "name" field be optional? That might make
|
|
|
|
// a bit more sense.
|
|
|
|
r_cons_printf ("{\"offset\":%"PFMT64d",\"symbol\":\"%s\"}", *addr, name);
|
|
|
|
if (i != lsz - 1) {
|
|
|
|
r_cons_printf (",");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_cons_printf ("]\n");
|
|
|
|
r_list_free (addrs);
|
|
|
|
r_list_free (names);
|
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '*': // "s*"
|
|
|
|
case '=': // "s="
|
|
|
|
case '!': // "s!"
|
2017-03-27 11:12:59 +00:00
|
|
|
{
|
2017-06-28 12:13:12 +00:00
|
|
|
char mode = input[0];
|
|
|
|
if (input[1] == '=') {
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
RList *list = r_io_sundo_list (core->io, mode);
|
2017-03-27 11:42:52 +00:00
|
|
|
if (list) {
|
2017-06-28 12:13:12 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RIOUndos *undo;
|
2017-03-27 11:42:52 +00:00
|
|
|
r_list_foreach (list, iter, undo) {
|
|
|
|
char *name = NULL;
|
2017-03-27 11:12:59 +00:00
|
|
|
|
2017-03-27 11:42:52 +00:00
|
|
|
RFlagItem *f = r_flag_get_at (core->flags, undo->off, true);
|
|
|
|
if (f) {
|
|
|
|
if (f->offset != undo->off) {
|
|
|
|
name = r_str_newf ("%s + %d\n", f->name,
|
2017-06-28 12:13:12 +00:00
|
|
|
(int)(undo->off - f->offset));
|
2017-03-27 11:42:52 +00:00
|
|
|
} else {
|
|
|
|
name = strdup (f->name);
|
|
|
|
}
|
2017-03-27 11:12:59 +00:00
|
|
|
}
|
2017-06-28 12:13:12 +00:00
|
|
|
if (mode) {
|
|
|
|
r_cons_printf ("0x%"PFMT64x" %s\n", undo->off, name? name: "");
|
|
|
|
} else {
|
|
|
|
if (!name) {
|
|
|
|
name = r_str_newf ("0x%"PFMT64x, undo->off);
|
|
|
|
}
|
|
|
|
r_cons_printf ("%s%s", name, iter->n? " > ":"");
|
2017-03-27 11:42:52 +00:00
|
|
|
}
|
|
|
|
free (name);
|
2017-03-27 11:12:59 +00:00
|
|
|
}
|
2017-03-27 11:42:52 +00:00
|
|
|
r_list_free (list);
|
2017-06-28 12:13:12 +00:00
|
|
|
if (!mode) {
|
|
|
|
r_cons_newline ();
|
|
|
|
}
|
2017-03-27 11:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '+': // "s+"
|
2017-03-19 01:34:53 +00:00
|
|
|
if (input[1] != '\0') {
|
2017-08-01 10:18:16 +00:00
|
|
|
int delta = off;
|
|
|
|
if (input[1] == '+') {
|
|
|
|
delta = core->blocksize;
|
|
|
|
int mult = r_num_math (core->num, input + 2);
|
|
|
|
if (mult > 0) {
|
|
|
|
delta /= mult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// int delta = (input[1] == '+')? core->blocksize: off;
|
2017-04-10 11:17:38 +00:00
|
|
|
if (!silent) {
|
2017-05-19 22:00:24 +00:00
|
|
|
r_io_sundo_push (core->io, core->offset,
|
|
|
|
r_print_get_cursor (core->print));
|
2017-04-10 11:17:38 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_seek_delta (core, delta);
|
2017-05-19 22:00:24 +00:00
|
|
|
r_core_block_read (core);
|
2017-03-19 01:34:53 +00:00
|
|
|
} else {
|
|
|
|
RIOUndos *undo = r_io_sundo_redo (core->io);
|
|
|
|
if (undo) {
|
|
|
|
r_core_seek (core, undo->off, 0);
|
2017-05-19 22:00:24 +00:00
|
|
|
r_core_block_read (core);
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-05-19 22:00:24 +00:00
|
|
|
case '-': // "s-"
|
2017-08-01 10:18:16 +00:00
|
|
|
switch (input[1]) {
|
2017-09-04 00:59:07 +00:00
|
|
|
case '*': // "s-*"
|
2017-08-01 10:18:16 +00:00
|
|
|
r_io_sundo_reset (core->io);
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 0: // "s-"
|
2017-08-01 10:18:16 +00:00
|
|
|
{
|
|
|
|
RIOUndos *undo = r_io_sundo (core->io, core->offset);
|
|
|
|
if (undo) {
|
|
|
|
r_core_seek (core, undo->off, 0);
|
|
|
|
r_core_block_read (core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '-': // "s--"
|
2017-08-01 10:18:16 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
int delta = -off;
|
|
|
|
if (input[1] == '-') {
|
|
|
|
delta = -core->blocksize;
|
|
|
|
int mult = r_num_math (core->num, input + 2);
|
|
|
|
if (mult > 0) {
|
|
|
|
delta /= mult;
|
|
|
|
}
|
|
|
|
}
|
2017-07-28 04:12:01 +00:00
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset,
|
2017-08-01 10:18:16 +00:00
|
|
|
r_print_get_cursor (core->print));
|
2017-07-28 04:12:01 +00:00
|
|
|
}
|
|
|
|
r_core_seek_delta (core, delta);
|
|
|
|
r_core_block_read (core);
|
2017-04-10 11:17:38 +00:00
|
|
|
}
|
2017-08-01 10:18:16 +00:00
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'n': // "sn"
|
2017-11-23 13:41:11 +00:00
|
|
|
{
|
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
|
|
|
const char *nkey = (input[1] == ' ')
|
|
|
|
? input + 2
|
|
|
|
: r_config_get (core->config, "scr.nkey");
|
|
|
|
r_core_seek_next (core, nkey);
|
2017-04-10 11:17:38 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'p': // "sp"
|
2017-11-23 13:41:11 +00:00
|
|
|
{
|
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
|
|
|
const char *nkey = (input[1] == ' ')
|
|
|
|
? input + 2
|
|
|
|
: r_config_get (core->config, "scr.nkey");
|
|
|
|
r_core_seek_previous (core, nkey);
|
2017-04-10 11:17:38 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'a': // "sa"
|
2017-03-19 01:34:53 +00:00
|
|
|
off = core->blocksize;
|
|
|
|
if (input[1] && input[2]) {
|
|
|
|
cmd = strdup (input);
|
|
|
|
p = strchr (cmd + 2, ' ');
|
|
|
|
if (p) {
|
|
|
|
off = r_num_math (core->num, p + 1);;
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
cmd[0] = 's';
|
|
|
|
// perform real seek if provided
|
|
|
|
r_cmd_call (core->rcmd, cmd);
|
|
|
|
free (cmd);
|
|
|
|
}
|
2017-04-10 11:17:38 +00:00
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_seek_align (core, off, 0);
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'b': // "sb"
|
2017-03-19 01:34:53 +00:00
|
|
|
if (off == 0) {
|
|
|
|
off = core->offset;
|
|
|
|
}
|
2017-04-10 11:17:38 +00:00
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_anal_bb_seek (core, off);
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'f': { // "sf"
|
|
|
|
RAnalFunction *fcn;
|
|
|
|
switch (input[1]) {
|
|
|
|
case '\0': // "sf"
|
|
|
|
fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
|
|
|
if (fcn) {
|
|
|
|
r_core_seek (core, fcn->addr + r_anal_fcn_size (fcn), 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ': // "sf "
|
|
|
|
fcn = r_anal_fcn_find_name (core->anal, input + 2);
|
|
|
|
if (fcn) {
|
|
|
|
r_core_seek (core, fcn->addr, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '.': // "sf."
|
|
|
|
fcn = r_anal_get_fcn_in (core->anal, core->offset, 0);
|
2014-05-18 12:17:43 +00:00
|
|
|
if (fcn) {
|
2017-03-19 01:34:53 +00:00
|
|
|
r_core_seek (core, fcn->addr, 1);
|
2013-10-19 22:25:37 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
case 'o': // "so"
|
2018-06-03 16:53:58 +00:00
|
|
|
cmd_seek_opcode (core, input + 1);
|
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
case 'g': // "sg"
|
|
|
|
{
|
2019-01-13 02:07:51 +00:00
|
|
|
RIOMap *map = r_io_map_get (core->io, core->offset);
|
|
|
|
if (map) {
|
|
|
|
r_core_seek (core, map->itv.addr, 1);
|
2017-03-19 01:34:53 +00:00
|
|
|
} else {
|
|
|
|
r_core_seek (core, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'G': // "sG"
|
|
|
|
{
|
|
|
|
if (!core->file) {
|
|
|
|
break;
|
|
|
|
}
|
2019-01-13 02:07:51 +00:00
|
|
|
RIOMap *map = r_io_map_get (core->io, core->offset);
|
2017-03-19 01:34:53 +00:00
|
|
|
// XXX: this +2 is a hack. must fix gap between sections
|
2019-01-13 02:07:51 +00:00
|
|
|
if (map) {
|
|
|
|
r_core_seek (core, map->itv.addr + map->itv.size + 2, 1);
|
2017-03-19 01:34:53 +00:00
|
|
|
} else {
|
2017-08-22 07:42:16 +00:00
|
|
|
r_core_seek (core, r_io_fd_size (core->io, core->file->fd), 1);
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l': // "sl"
|
|
|
|
{
|
|
|
|
int sl_arg = r_num_math (core->num, input + 1);
|
|
|
|
switch (input[1]) {
|
2017-09-04 00:59:07 +00:00
|
|
|
case '\0': // "sl"
|
2017-03-19 01:34:53 +00:00
|
|
|
if (!core->print->lines_cache) {
|
|
|
|
__init_seek_line (core);
|
2013-11-25 01:03:54 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
__get_current_line (core);
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case ' ': // "sl "
|
2017-03-19 01:34:53 +00:00
|
|
|
if (!core->print->lines_cache) {
|
|
|
|
__init_seek_line (core);
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
__seek_line_absolute (core, sl_arg);
|
2012-02-27 01:40:27 +00:00
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '+': // "sl+"
|
|
|
|
case '-': // "sl-"
|
2017-03-19 01:34:53 +00:00
|
|
|
if (!core->print->lines_cache) {
|
|
|
|
__init_seek_line (core);
|
2013-12-10 00:58:06 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
__seek_line_relative (core, sl_arg);
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'c': // "slc"
|
2017-03-19 01:34:53 +00:00
|
|
|
__clean_lines_cache (core);
|
2013-12-10 00:58:06 +00:00
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case 'l': // "sll"
|
2017-03-19 01:34:53 +00:00
|
|
|
if (!core->print->lines_cache) {
|
|
|
|
__init_seek_line (core);
|
2013-12-10 00:58:06 +00:00
|
|
|
}
|
2017-03-19 01:34:53 +00:00
|
|
|
eprintf ("%d lines\n", core->print->lines_cache_sz - 1);
|
2013-12-10 00:58:06 +00:00
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '?': // "sl?"
|
2017-07-26 13:15:13 +00:00
|
|
|
r_core_cmd_help (core, help_msg_sl);
|
2017-03-19 01:34:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case ':': // "s:"
|
2017-03-19 01:34:53 +00:00
|
|
|
printPadded (core, atoi (input + 1));
|
|
|
|
break;
|
2017-09-04 00:59:07 +00:00
|
|
|
case '?': // "s?"
|
2017-07-26 13:15:13 +00:00
|
|
|
r_core_cmd_help (core, help_msg_s);
|
2017-03-19 01:34:53 +00:00
|
|
|
break;
|
2017-12-22 17:58:35 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
ut64 n = r_num_math (core->num, input);
|
|
|
|
if (n) {
|
|
|
|
if (!silent) {
|
|
|
|
r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print));
|
|
|
|
}
|
|
|
|
r_core_seek (core, n, 1);
|
|
|
|
r_core_block_read (core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-03-19 01:34:53 +00:00
|
|
|
}
|
2012-02-27 01:40:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|