Skip to content

Commit

Permalink
Fix old math commands (#3828)
Browse files Browse the repository at this point in the history
* core/cmd: Adjust math commands

* shell: make `?x` commands a parsing failure

We have moved the `?x` commands to `%x`, thus now no command should
start with `?`. This patch makes the parser fail to parse `?x`
strings.

* core/tui: use APIs in panels

* There's no `%q` anymore, use `%=`
  • Loading branch information
ret2libc authored and XVilka committed Sep 14, 2023
1 parent 383a646 commit 546e31b
Show file tree
Hide file tree
Showing 41 changed files with 418 additions and 401 deletions.
4 changes: 2 additions & 2 deletions doc/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ conditional breakpoints are implemented in the following way:
1. ignore breakpoint at address `0x4000ce` for five times:

f times=5
(dec_times,f times=`?vi times-1`,?= times)
(dec_times,f times=`%vi times-1`,?= times)
db @ 0x4000ce
dbC .(dec_times) @ 0x4000ce
dc

2. execute until rax==0x31c0 at address `0x4000ce`

e cmd.hitinfo=0
(break_rax,f reg_rax=`dr rax`,f test=`?vi reg_rax-0x31c0`,?= test)
(break_rax,f reg_rax=`dr rax`,f test=`%vi reg_rax-0x31c0`,?= test)
db @ 0x4000ce
dbC .(break_rax) @ 0x4000ce
dc
Expand Down
46 changes: 23 additions & 23 deletions doc/hud
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
add comment;?i Comment;CC `ys`
list comments;CC;?ik
change block size;?i block size;b `ys`
add comment;%i Comment;CC `ys`
list comments;CC;%ik
change block size;%i block size;b `ys`
analyze all;aa
analyze function;af
analyze preludes;ap
continue process execution;dc;?i
continue process execution;dc;%i
disable colors;e scr.color=0
enable colors (ANSI 16);e scr.color=1
enable colors (256);e scr.color=2
enable colors (16M);e scr.color=3
randomize colors;ecr
hide bytes in disassembly;e asm.bytes=false
show bytes in disassembly;e asm.bytes=true
list imports ;ii;?ik
list sections;iS;?ik
list symbols;is;?ik
list processes;dp*;?ik
run command;?i cmd;`ys`;?ik
list imports ;ii;%ik
list sections;iS;%ik
list symbols;is;%ik
list processes;dp*;%ik
run command;%i cmd;`ys`;%ik
seek to entrypoint;s entry0
seek to main;s main
set breakpoint;db;?i new breakpoint address;db `ys`
remove breakpoint ?i delete breakpoint at given address;db-`ys`
show backtrace;dbt;?ik
set breakpoint;db;%i new breakpoint address;db `ys`
remove breakpoint %i delete breakpoint at given address;db-`ys`
show backtrace;dbt;%ik
show bytes in disassembly;e asm.bytes=1
show debugger register values;dr;?ik
show process memory maps;dm;?ik
step instruction;sr PC;pd 1;ds;?ik
step over call;sr PC;pd 1;dso;?ik
write string;?i string;w `ys`
write hexadecimal data in current offset;?i hexpairs;wx `ys`
xor current block with hexpair key;?i hexpair key;wox `ys`
show debugger register values;dr;%ik
show process memory maps;dm;%ik
step instruction;sr PC;pd 1;ds;%ik
step over call;sr PC;pd 1;dso;%ik
write string;%i string;w `ys`
write hexadecimal data in current offset;%i hexpairs;wx `ys`
xor current block with hexpair key;%i hexpair key;wox `ys`
edit eval configuration;Veq
calculator;?i enter math expression;? `ys`;?ik
calculator;%i enter math expression;? `ys`;%ik
disassemble 1024 bytes and pipe to less(1);b 1024;pd|less -R
rename function;?i New name;afn `ys`
set flag;?i Flag name;f `ys`
unset flag;?i Flag name to delete;f-`ys`
rename function;%i New name;afn `ys`
set flag;%i Flag name;f `ys`
unset flag;%i Flag name to delete;f-`ys`
2 changes: 1 addition & 1 deletion librz/analysis/p/analysis_mips_gnu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ static int mips_op(RzAnalysis *analysis, RzAnalysisOp *op, ut64 addr, const ut8
// TODO: currently there is no way for the macro to get access to this register
op->src[0] = rz_analysis_value_new();
op->src[0]->reg = rz_reg_get(analysis->reg, mips_reg_decode(rs), RZ_REG_TYPE_GPR);
op->val = imm; // Beware: this one is signed... use `?vi $v`
op->val = imm; // Beware: this one is signed... use `%vi $v`
if (rs == 0) {
insn.id = MIPS_INS_LI;
snprintf((char *)insn.i_reg.imm, REG_BUF_MAX, "0x%" PFMT32x, imm);
Expand Down
4 changes: 2 additions & 2 deletions librz/bin/format/xnu/scripts/machtraps.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def walk_back_until(addr, pattern, min_addr):


def carve_trap_num(addr, flag):
saved_seek = r.cmd("?v $$")
saved_seek = r.cmd("%v $$")
r.cmd("e io.cache=true")
r.cmd("e emu.write=true")
r.cmd("aei")
r.cmd("aeim")
min_addr = int(r.cmd("?v " + flag), 0)
min_addr = int(r.cmd("%v " + flag), 0)
emu_start = walk_back_until(addr - 4, r"^b|^ret|^invalid", min_addr)
r.cmd("s " + str(emu_start))
obj = r.cmd("aefa 0x%08x~[0]:0" % addr)
Expand Down
2 changes: 1 addition & 1 deletion librz/core/cautocmpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void guess_data_free(struct guess_data_t *g) {
/**
* Modify the input in \p buf to add additional characters, trying to detect
* what token could go in the current position of the buffer. For example, to
* detect that a CMD_ID is expected at * `?e $(<TAB>`, you could try inserting
* detect that a CMD_ID is expected at `echo $(<TAB>`, you could try inserting
* a letter and see what would be the new syntax tree.
*/
static struct guess_data_t *guess_next_autocmplt_token(RzCore *core, RzLineBuffer *buf, const char *fake_text, size_t offset) {
Expand Down
1 change: 0 additions & 1 deletion librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ RZ_API void rz_core_bin_export_info(RzCore *core, int mode) {
char *k = sdbkv_key(kv);
char *v = sdbkv_value(kv);
char *dup = strdup(k);
// printf ("?e (%s) (%s)\n", k, v);
if ((flagname = strstr(dup, ".offset"))) {
*flagname = 0;
flagname = dup;
Expand Down
2 changes: 1 addition & 1 deletion librz/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -3248,7 +3248,7 @@ RZ_API int rz_core_config_init(RzCore *core) {
SETPREF("cfg.user", rz_sys_whoami(buf), "Set current username/pid");
SETCB("cfg.fortunes", "true", &cb_cfg_fortunes, "If enabled show tips at start");
SETCB("cfg.fortunes.file", "tips", &cb_cfg_fortunes_file, "Type of fortunes to show (tips, fun)");
SETBPREF("cfg.fortunes.clippy", "false", "Use ?E instead of ?e");
SETBPREF("cfg.fortunes.clippy", "false", "Use 'clippy' instead of 'echo'");
SETPREF("cfg.prefixdump", "dump", "Filename prefix for automated dumps");
SETBPREF("cfg.wseek", "false", "Seek after write");
SETICB("cfg.seek.histsize", 63, NULL, "Maximum size of the seek history");
Expand Down
4 changes: 2 additions & 2 deletions librz/core/cfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,9 @@ static bool setbpint(RzCore *r, const char *mode, const char *sym) {
if (bp) {
bp->internal = true;
#if __linux__
bp->data = rz_str_newf("?e %s: %s", mode, sym);
bp->data = rz_str_newf("echo %s: %s", mode, sym);
#else
bp->data = rz_str_newf("?e %s: %s;ps@rdi", mode, sym);
bp->data = rz_str_newf("echo %s: %s;ps@rdi", mode, sym);
#endif
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions librz/core/cmd/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1860,7 +1860,7 @@ static int rz_core_cmd_subst_i(RzCore *core, char *cmd, char *colon, bool *tmpse
if (ptr[1] == '!') {
str = rz_core_cmd_str_pipe(core, ptr + 1);
} else {
// Color disabled when doing backticks ?e `pi 1`
// Color disabled when doing backticks echo `pi 1`
int ocolor = rz_config_get_i(core->config, "scr.color");
rz_config_set_i(core->config, "scr.color", 0);
core->cmd_in_backticks = true;
Expand Down Expand Up @@ -3950,11 +3950,13 @@ DEFINE_HANDLE_TS_FCN_AND_SYMBOL(help_stmt) {
if (node_str_len >= 2 && !strcmp(node_string + node_str_len - 2, "?*")) {
node_string[node_str_len - 2] = 0;
const char *argv[2] = { NULL, node_string };
return rz_cmd_help_search_handler(state->core, 2, argv, RZ_OUTPUT_MODE_STANDARD);
int argc = node_str_len > 2 ? 2 : 1;
return rz_cmd_help_search_handler(state->core, argc, argv, RZ_OUTPUT_MODE_STANDARD);
} else if (node_str_len >= 3 && !strcmp(node_string + node_str_len - 3, "?*j")) {
node_string[node_str_len - 3] = 0;
const char *argv[2] = { NULL, node_string };
return rz_cmd_help_search_handler(state->core, 2, argv, RZ_OUTPUT_MODE_JSON);
int argc = node_str_len > 2 ? 2 : 1;
return rz_cmd_help_search_handler(state->core, argc, argv, RZ_OUTPUT_MODE_JSON);
}

TSNode command = ts_node_child_by_field_name(node, "command", strlen("command"));
Expand Down
80 changes: 34 additions & 46 deletions librz/core/cmd/cmd_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct rz_core_var core_vars[] = {
{ "$b", "block size" },
{ "$c", "get terminal width in character columns" },
{ "$Cn", "get nth call of function" },
{ "$D", "current debug map base address ?v $D @ rsp" },
{ "$D", "current debug map base address %v $D @ rsp" },
{ "$DB", "same as dbg.baddr, progam base address" },
{ "$DD", "current debug map size" },
{ "$Dn", "get nth data reference in function" },
Expand Down Expand Up @@ -258,33 +258,23 @@ RZ_IPI RzCmdStatus rz_print_binary_handler(RzCore *core, int argc, const char **
}

RZ_IPI RzCmdStatus rz_base64_encode_handler(RzCore *core, int argc, const char **argv) {
char *buf = NULL;
for (int i = 1; i < argc; i++) {
const int buflen = (strlen(argv[i]) * 4) + 1;
buf = (char *)realloc((void *)buf, buflen * sizeof(char));
if (!buf) {
RZ_LOG_ERROR("core: Out of memory!");
return RZ_CMD_STATUS_ERROR;
}
rz_base64_encode(buf, (const ut8 *)argv[i], strlen(argv[i]));
rz_cons_println((const char *)buf);
char *buf = rz_base64_encode_dyn((ut8 *)argv[1], strlen(argv[1]));
if (!buf) {
RZ_LOG_ERROR("core: Out of memory!");
return RZ_CMD_STATUS_ERROR;
}
rz_cons_println(buf);
free(buf);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_base64_decode_handler(RzCore *core, int argc, const char **argv) {
ut8 *buf = NULL;
for (int i = 1; i < argc; i++) {
const int buflen = (strlen(argv[i]) * 4) + 1;
buf = (ut8 *)realloc((void *)buf, buflen * sizeof(ut8));
if (!buf) {
RZ_LOG_ERROR("core: Out of memory!");
return RZ_CMD_STATUS_ERROR;
}
rz_base64_decode(buf, argv[i], -1);
rz_cons_println((const char *)buf);
ut8 *buf = rz_base64_decode_dyn(argv[1], -1);
if (!buf) {
RZ_LOG_ERROR("core: Out of memory!");
return RZ_CMD_STATUS_ERROR;
}
rz_cons_println((char *)buf);
free(buf);
return RZ_CMD_STATUS_OK;
}
Expand All @@ -298,7 +288,7 @@ RZ_IPI RzCmdStatus rz_check_between_handler(RzCore *core, int argc, const char *
}

RZ_IPI RzCmdStatus rz_print_boundaries_prot_handler(RzCore *core, int argc, const char **argv) {
const char *mode = rz_str_trim_head_ro(argv[0]);
const char *mode = rz_str_trim_head_ro(argv[1]);
RzList *list = rz_core_get_boundaries_prot(core, -1, mode, "search");
if (!list) {
RZ_LOG_ERROR("core: Failed to get boundaries protection values in RzList");
Expand All @@ -314,10 +304,8 @@ RZ_IPI RzCmdStatus rz_print_boundaries_prot_handler(RzCore *core, int argc, cons
}

RZ_IPI RzCmdStatus rz_print_djb2_hash_handler(RzCore *core, int argc, const char **argv) {
for (int i = 1; i < argc; i++) {
ut32 hash = (ut32)rz_str_djb2_hash(argv[i]);
rz_cons_printf("0x%08x\n", hash);
}
ut32 hash = (ut32)rz_str_djb2_hash(argv[1]);
rz_cons_printf("0x%08x\n", hash);
return RZ_CMD_STATUS_OK;
}

Expand Down Expand Up @@ -345,10 +333,6 @@ RZ_IPI RzCmdStatus rz_num_to_units_handler(RzCore *core, int argc, const char **
}

RZ_IPI RzCmdStatus rz_set_last_eval_expr_handler(RzCore *core, int argc, const char **argv) {
if (core->num->dbz) {
RZ_LOG_ERROR("core: RzNum ERROR: Division by Zero\n");
return RZ_CMD_STATUS_ERROR;
}
rz_num_math(core->num, argv[1]);
return RZ_CMD_STATUS_OK;
}
Expand All @@ -369,7 +353,7 @@ RZ_IPI RzCmdStatus rz_show_value_hex_handler(RzCore *core, int argc, const char
RZ_LOG_ERROR("core: RzNum ERROR: Division by Zero\n");
return RZ_CMD_STATUS_ERROR;
}
rz_cons_printf("0x%08" PFMT64x "\n", n); // differs from ?v here 0x%08
rz_cons_printf("0x%08" PFMT64x "\n", n); // differs from %v here 0x%08
return RZ_CMD_STATUS_OK;
}

Expand Down Expand Up @@ -421,11 +405,6 @@ RZ_IPI RzCmdStatus rz_show_value_int_handler(RzCore *core, int argc, const char
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_set_core_num_value_handler(RzCore *core, int argc, const char **argv) {
rz_num_math(core->num, argv[1]);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_compare_and_set_core_num_value_handler(RzCore *core, int argc, const char **argv) {
core->num->value = strcmp(argv[1], argv[2]);
return RZ_CMD_STATUS_OK;
Expand Down Expand Up @@ -596,7 +575,7 @@ RZ_IPI RzCmdStatus rz_yank_hud_file_handler(RzCore *core, int argc, const char *
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_input_numerical_expr_handler(RzCore *core, int argc, const char **argv) {
static RzCmdStatus prompt_handler(RzCore *core, int argc, const char **argv, bool echo) {
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("core: Not running in interactive mode\n");
return RZ_CMD_STATUS_WRONG_ARGS;
Expand All @@ -611,27 +590,36 @@ RZ_IPI RzCmdStatus rz_input_numerical_expr_handler(RzCore *core, int argc, const
rz_core_yank_set_str(core, RZ_CORE_FOREIGN_ADDR, foo);
core->num->value = rz_num_math(core->num, foo);
rz_cons_set_raw(0);
if (echo) {
rz_cons_printf("%s\n", foo);
}
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_input_yesno_no_handler(RzCore *core, int argc, const char **argv) {
RZ_IPI RzCmdStatus rz_input_prompt_handler(RzCore *core, int argc, const char **argv) {
return prompt_handler(core, argc, argv, false);
}

RZ_IPI RzCmdStatus rz_input_prompt_echo_handler(RzCore *core, int argc, const char **argv) {
return prompt_handler(core, argc, argv, true);
}

static RzCmdStatus yesno_handler(RzCore *core, int argc, const char **argv, const char *yn) {
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("core: Not running in interactive mode\n");
return RZ_CMD_STATUS_WRONG_ARGS;
}
core->num->value = rz_cons_yesno(0, "%s? (y/N)", argv[1]);
core->num->value = rz_cons_yesno(0, "%s? (%s) ", argv[1], yn);
rz_cons_set_raw(0);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_input_yesno_no_handler(RzCore *core, int argc, const char **argv) {
return yesno_handler(core, argc, argv, "y/N");
}

RZ_IPI RzCmdStatus rz_input_yesno_yes_handler(RzCore *core, int argc, const char **argv) {
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("core: Not running in interactive mode\n");
return RZ_CMD_STATUS_WRONG_ARGS;
}
core->num->value = rz_cons_yesno(0, "%s? (Y/n)", argv[1]);
rz_cons_set_raw(0);
return RZ_CMD_STATUS_OK;
return yesno_handler(core, argc, argv, "Y/n");
}

RZ_IPI RzCmdStatus rz_input_any_key_handler(RzCore *core, int argc, const char **argv) {
Expand Down
Loading

0 comments on commit 546e31b

Please sign in to comment.