Skip to content

Commit

Permalink
Use internal API instead of command (#4053)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu authored Jan 3, 2024
1 parent 63d3369 commit 8fba56b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
7 changes: 5 additions & 2 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <rz_util.h>
#include <rz_type.h>
#include <rz_types.h>
#include <limits.h>

#include "../core_private.h"
#include "rz_util/rz_strbuf.h"
Expand Down Expand Up @@ -4447,8 +4446,12 @@ RZ_IPI RzCmdStatus rz_cmd_disassemble_recursively_no_function_handler(RzCore *co
}

RZ_IPI RzCmdStatus rz_cmd_disassemble_summarize_n_bytes_handler(RzCore *core, int argc, const char **argv) {
ut64 n_bytes = argc > 1 ? rz_num_math(core->num, argv[1]) : 0;
if (argc <= 1) {
RZ_LOG_ERROR("Invalid argument.");
return RZ_CMD_STATUS_ERROR;
}

ut64 n_bytes = rz_num_math(core->num, argv[1]);
// small patch to reuse rz_core_print_disasm_strings which
// needs to be rewritten entirely
char *string = rz_core_print_disasm_strings(core, argc > 1 ? RZ_CORE_DISASM_STRINGS_MODE_BYTES : RZ_CORE_DISASM_STRINGS_MODE_INST, n_bytes, NULL);
Expand Down
41 changes: 33 additions & 8 deletions librz/core/cprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,22 +540,44 @@ static void core_handle_call(RzCore *core, char *line, char **str) {
}

/**
* \brief Get the console output of disassembling \p byte_len bytes at \p addr
* \brief Get the console output of disassembling \p byte_len bytes
* or \p inst_len opcodes at \p addr. Restricted by \p byte_len
* and \p inst_len at the same time. Set one of them to zero to
* ignore its restriction.
*/
static char *cons_dis_n_bytes(RzCore *core, ut64 addr, ut32 byte_len) {
static char *cons_disassembly(RzCore *core, ut64 addr, ut32 byte_len, ut32 inst_len) {
rz_return_val_if_fail(core && (byte_len || inst_len), NULL);

// cbytes in disasm_options decides whether byte_len constrains inst_len
bool cbytes = true;

if (byte_len == 0) {
cbytes = false;
byte_len = inst_len;
}

if (inst_len == 0) {
inst_len = byte_len;
}

ut8 *block = malloc(byte_len + 1);
if (!block) {
RZ_LOG_ERROR("Cannot allocate buffer\n");
return NULL;
}

rz_io_read_at(core->io, addr, block, byte_len);
if (rz_io_nread_at(core->io, addr, block, byte_len) == -1) {
RZ_LOG_ERROR("Fail to read from 0x%" PFMT64x ".", addr);
free(block);
return NULL;
}

RzCoreDisasmOptions disasm_options = {
.cbytes = true,
.cbytes = cbytes,
};

rz_cons_push();
rz_core_print_disasm(core, addr, block, byte_len, 9999, NULL, &disasm_options);
rz_core_print_disasm(core, addr, block, byte_len, inst_len, NULL, &disasm_options);
rz_cons_filter();
const char *cons_str = rz_str_get(rz_cons_get_buffer());
char *ret = strdup(cons_str);
Expand Down Expand Up @@ -616,7 +638,7 @@ RZ_API RZ_OWN char *rz_core_print_disasm_strings(RZ_NONNULL RzCore *core, RzCore
case RZ_CORE_DISASM_STRINGS_MODE_BLOCK: {
RzAnalysisBlock *bb = rz_analysis_find_most_relevant_block_in(core->analysis, core->offset);
if (bb) {
dump_string = cons_dis_n_bytes(core, bb->addr, bb->size);
dump_string = cons_disassembly(core, bb->addr, bb->size, 0);
if (!dump_string) {
goto restore_conf;
}
Expand All @@ -637,12 +659,15 @@ RZ_API RZ_OWN char *rz_core_print_disasm_strings(RZ_NONNULL RzCore *core, RzCore
break;
}
case RZ_CORE_DISASM_STRINGS_MODE_INST: {
dump_string = rz_core_cmd_strf(core, "pd %d", core->blocksize);
dump_string = cons_disassembly(core, core->offset, 0, core->blocksize);
if (!dump_string) {
goto restore_conf;
}
break;
}
case RZ_CORE_DISASM_STRINGS_MODE_BYTES:
default: {
dump_string = cons_dis_n_bytes(core, core->offset, n_bytes);
dump_string = cons_disassembly(core, core->offset, n_bytes, 0);
if (!dump_string) {
goto restore_conf;
}
Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ typedef struct rz_core_asm_hit {
*/
typedef struct rz_core_disasm_options {
int invbreak;
int cbytes;
int cbytes; ///< set false to ignore the constraint of \p len and print \p nlines instructions in rz_core_print_disasm
RzAnalysisFunction *function; ///< Disassemble a function
RzPVector /*<RzAnalysisDisasmText *>*/ *vec; ///< Not print, but append as RzPVector<RzAnalysisDisasmText>
} RzCoreDisasmOptions;
Expand Down
7 changes: 0 additions & 7 deletions test/db/cmd/cmd_0
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,6 @@ EXPECT=<<EOF
EOF
RUN

NAME=pds 0
FILE=bins/elf/analysis/hello-arm32
CMDS=pds 0
EXPECT=<<EOF
EOF
RUN

NAME=pdt 0
FILE=bins/elf/analysis/hello-arm32
CMDS=pdt 0
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_pd
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ s sym.func.100004401
af
afn newname
s entry0
pds
pds 0x420
EOF
EXPECT=<<EOF
0x100001085 call newname
Expand Down

0 comments on commit 8fba56b

Please sign in to comment.