diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index afbee0625a4..937ae202af1 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -7,7 +7,6 @@ #include #include #include -#include #include "../core_private.h" #include "rz_util/rz_strbuf.h" @@ -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); diff --git a/librz/core/cprint.c b/librz/core/cprint.c index eebceeb5db7..7193e079f7e 100644 --- a/librz/core/cprint.c +++ b/librz/core/cprint.c @@ -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); @@ -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; } @@ -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; } diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index f40332cb6e8..166950e579f 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -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 /**/ *vec; ///< Not print, but append as RzPVector } RzCoreDisasmOptions; diff --git a/test/db/cmd/cmd_0 b/test/db/cmd/cmd_0 index 9fd4a1bb4ae..ba5f104c78c 100644 --- a/test/db/cmd/cmd_0 +++ b/test/db/cmd/cmd_0 @@ -250,13 +250,6 @@ EXPECT=<