Skip to content

Commit

Permalink
Remove the use of rz_core_cmd_strf() in cprint.c (#3869)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu authored Sep 18, 2023
1 parent 2477d90 commit a21f4bc
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions librz/core/cprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,32 @@ 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
*/
static char *cons_dis_n_bytes(RzCore *core, ut64 addr, ut32 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);
RzCoreDisasmOptions disasm_options = {
.cbytes = true,
};

rz_cons_push();
rz_core_print_disasm(core, addr, block, byte_len, 9999, NULL, &disasm_options);
rz_cons_filter();
const char *cons_str = rz_str_get(rz_cons_get_buffer());
char *ret = strdup(cons_str);
rz_cons_pop();
rz_cons_echo(NULL);
free(block);
return ret;
}

/**
* \brief Get string in disassembly line for \p mode
* \param mode RzCorePrintDisasmStringsMode RZ_CORE_DISASM_STRINGS_MODE_{BYTES,INST,BLOCK,FUNCTION}
Expand Down Expand Up @@ -590,7 +616,10 @@ 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 = rz_core_cmd_strf(core, "pD %" PFMT64u " @ 0x%08" PFMT64x, bb->size, bb->addr);
dump_string = cons_dis_n_bytes(core, bb->addr, bb->size);
if (!dump_string) {
goto restore_conf;
}
} else {
RZ_LOG_ERROR("cannot find block %" PFMT64x ".\n", core->offset);
goto restore_conf;
Expand All @@ -612,10 +641,14 @@ RZ_API RZ_OWN char *rz_core_print_disasm_strings(RZ_NONNULL RzCore *core, RzCore
break;
}
case RZ_CORE_DISASM_STRINGS_MODE_BYTES:
default:
dump_string = rz_core_cmd_strf(core, "pD %" PFMT64d, n_bytes);
default: {
dump_string = cons_dis_n_bytes(core, core->offset, n_bytes);
if (!dump_string) {
goto restore_conf;
}
break;
}
}
rz_config_hold_restore(hc);

lines = rz_str_split_duplist(dump_string, "\n", true);
Expand Down

0 comments on commit a21f4bc

Please sign in to comment.