Skip to content

Commit

Permalink
Use API instead of the direct use of pdb command (#3855)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu committed Sep 14, 2023
1 parent 7a50bd7 commit a66b6c7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
24 changes: 23 additions & 1 deletion librz/core/cgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,29 @@ RZ_API RZ_OWN RzGraph /*<RzGraphNodeInfo *>*/ *rz_core_graph_callgraph(RZ_NONNUL
typedef char *(*GraphBodyFn)(RzCore *core, ut64 addr, RzAnalysisBlock *bb);

static inline char *block_disasm(RzCore *core, ut64 addr, RzAnalysisBlock *bb) {
return rz_core_cmd_strf(core, "pdb @ 0x%" PFMT64x, addr);
RzAnalysisBlock *b = rz_analysis_find_most_relevant_block_in(core->analysis, addr);
if (!b) {
RZ_LOG_ERROR("Cannot find function at 0x%08" PFMT64x "\n", addr);
return NULL;
}
ut8 *block = malloc(b->size + 1);
if (!block) {
RZ_LOG_ERROR("Cannot allocate buffer\n");
return NULL;
}
rz_cons_push();
rz_io_read_at(core->io, b->addr, block, b->size);
RzCoreDisasmOptions disasm_options = {
.cbytes = 2,
};
rz_core_print_disasm(core, b->addr, block, b->size, 9999, NULL, &disasm_options);
rz_cons_filter();
const char *retstr = rz_str_get(rz_cons_get_buffer());
char *opcodes = strdup(retstr);
rz_cons_pop();
rz_cons_echo(NULL);
free(block);
return opcodes;
}

static inline RzGraphNode *graph_add_cached(RzCore *core, HtUP *cache, RzAnalysisBlock *bb, ut64 offset, RzGraph /*<RzGraphNodeInfo *>*/ *graph, GraphBodyFn body_fn) {
Expand Down
26 changes: 25 additions & 1 deletion librz/main/rz-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ static void print_color_node(RzCore *core, RzAnalysisBlock *bbi) {
static char *basic_block_opcodes(RzCore *core, RzAnalysisBlock *bbi) {
char *opcodes = NULL;
RzConfigHold *hc = NULL;
ut8 *block = NULL;

if (!(hc = rz_config_hold_new(core->config))) {
return NULL;
Expand All @@ -1481,8 +1482,31 @@ static char *basic_block_opcodes(RzCore *core, RzAnalysisBlock *bbi) {
rz_config_set_i(core->config, "asm.comments", 0);
rz_config_set_i(core->config, "scr.color", COLOR_MODE_DISABLED);

opcodes = rz_core_cmd_strf(core, "pdb @ 0x%08" PFMT64x, bbi->addr);
rz_cons_push();
RzAnalysisBlock *b = rz_analysis_find_most_relevant_block_in(core->analysis, bbi->addr);
if (!b) {
RZ_LOG_ERROR("Cannot find function at 0x%08" PFMT64x "\n", bbi->addr);
goto exit;
}

block = malloc(b->size + 1);
if (!block) {
RZ_LOG_ERROR("Cannot allocate buffer\n");
goto exit;
}

rz_io_read_at(core->io, b->addr, block, b->size);
RzCoreDisasmOptions disasm_options = {
.cbytes = 2,
};
rz_core_print_disasm(core, b->addr, block, b->size, 9999, NULL, &disasm_options);
rz_cons_filter();
const char *retstr = rz_str_get(rz_cons_get_buffer());
opcodes = strdup(retstr);
exit:
rz_cons_pop();
rz_cons_echo(NULL);
free(block);
rz_config_hold_restore(hc);
rz_config_hold_free(hc);
return opcodes;
Expand Down

0 comments on commit a66b6c7

Please sign in to comment.