Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use API in Visual Mode #4339

Open
wants to merge 26 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f124717
Refactor to use API in Visual Mode
HN026 Mar 6, 2024
b31cdef
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 11, 2024
218d35a
Seek to a basic Block
HN026 Mar 11, 2024
e4b73d5
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 14, 2024
8f4ca29
Implemented an API for `~...`
HN026 Mar 15, 2024
451c7b4
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 16, 2024
f180342
Changes and shifted to Visual.c
HN026 Mar 16, 2024
6bd97f4
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 17, 2024
aa4d719
Afb function
HN026 Mar 17, 2024
c060829
Refactor to use API in Visual Mode
HN026 Mar 6, 2024
8df818c
Seek to a basic Block
HN026 Mar 11, 2024
da5f4f9
Implemented an API for `~...`
HN026 Mar 15, 2024
bd6421f
Changes and shifted to Visual.c
HN026 Mar 16, 2024
42fadc8
Afb function
HN026 Mar 17, 2024
75f6fff
Merge branch 'rizinorg:dev' into dev
HN026 Mar 24, 2024
737deda
refactor
HN026 Mar 24, 2024
b809714
Merge remote-tracking branch 'origin/rz_core_seek_first_basic_block_A…
HN026 Mar 24, 2024
ce4beb8
free to RZ_FREE
HN026 Mar 24, 2024
5042768
Update visual.c
HN026 Mar 24, 2024
cedcabb
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 26, 2024
65e737e
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 26, 2024
1ae6f5d
refactor and function namechanges
HN026 Mar 26, 2024
0ba5529
NULL check
HN026 Mar 26, 2024
06bc8c9
Merge remote-tracking branch 'origin' into rz_core_seek_first_basic_b…
HN026 Mar 31, 2024
03204e8
vbt to direct API
HN026 Mar 31, 2024
a2ef3ea
JSON mode changes
HN026 Mar 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,10 @@ static ut64 __opaddr(RzAnalysisBlock *b, ut64 addr) {
return UT64_MAX;
}

static void bb_info_print(RzCore *core, RzAnalysisFunction *fcn, RzAnalysisBlock *bb,
static char *bb_info_print(RzCore *core, RzAnalysisFunction *fcn, RzAnalysisBlock *bb,
HN026 marked this conversation as resolved.
Show resolved Hide resolved
ut64 addr, RzOutputMode mode, PJ *pj, RzTable *t) {
RzDebugTracepoint *tp = NULL;
RzStrBuf *buf = rz_strbuf_new("");
HN026 marked this conversation as resolved.
Show resolved Hide resolved
int outputs = (bb->jump != UT64_MAX) + (bb->fail != UT64_MAX);
int inputs = 0;

Expand All @@ -386,26 +387,26 @@ static void bb_info_print(RzCore *core, RzAnalysisFunction *fcn, RzAnalysisBlock
switch (mode) {
case RZ_OUTPUT_MODE_STANDARD:
tp = rz_debug_trace_get(core->dbg, bb->addr);
rz_cons_printf("0x%08" PFMT64x " 0x%08" PFMT64x " %02X:%04X %" PFMT64d,
rz_strbuf_appendf(buf, "0x%08" PFMT64x " 0x%08" PFMT64x " %02X:%04X %" PFMT64d,
bb->addr, bb->addr + bb->size,
tp ? tp->times : 0, tp ? tp->count : 0,
bb->size);
if (bb->jump != UT64_MAX) {
rz_cons_printf(" j 0x%08" PFMT64x, bb->jump);
rz_strbuf_appendf(buf, " j 0x%08" PFMT64x, bb->jump);
}
if (bb->fail != UT64_MAX) {
rz_cons_printf(" f 0x%08" PFMT64x, bb->fail);
rz_strbuf_appendf(buf, " f 0x%08" PFMT64x, bb->fail);
}
if (bb->switch_op) {
RzAnalysisCaseOp *cop;
RzListIter *iter;
RzList *unique_cases = rz_list_uniq(bb->switch_op->cases, casecmp, NULL);
rz_list_foreach (unique_cases, iter, cop) {
rz_cons_printf(" s 0x%08" PFMT64x, cop->addr);
rz_strbuf_appendf(buf, " s 0x%08" PFMT64x, cop->addr);
}
rz_list_free(unique_cases);
}
rz_cons_newline();
rz_strbuf_append(buf, "\n");
break;
case RZ_OUTPUT_MODE_JSON: {
pj_o(pj);
Expand Down Expand Up @@ -446,33 +447,39 @@ static void bb_info_print(RzCore *core, RzAnalysisFunction *fcn, RzAnalysisBlock
pj_ki(pj, "ninstr", bb->ninstr);
pj_kb(pj, "traced", bb->traced);
pj_end(pj);
break;
char *json_str = strdup(pj_string(pj));
pj_free(pj);
return json_str;
}
case RZ_OUTPUT_MODE_TABLE:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It broke tables output:

[XX] db/cmd/tables tables sum
RZ_NOPLUGINS=1 /Users/runner/bin/rizin -escr.utf8=0 -escr.color=0 -escr.interactive=0 -eflirt.sigdb.load.system=false -eflirt.sigdb.load.home=false -N -Qc 's main
af
afbt
afbt:fancy
afbt:size/sort/inc:simple
' bins/elf/hello_world
-- stdout
--- expected
+++ actual
@@ -1,17 +1,29 @@
-addr  size jump  fail  
------------------------
-0x7aa 92   0x844 0x806
-0x806 62   0x844 -1
-0x844 3    -1    -1
-.------------------------------.
-| addr  | size | jump  | fail  |
-)------------------------------(
-| 0x7aa | 92   | 0x844 | 0x806 |
-| 0x806 | 62   | 0x844 | -1    |
-| 0x844 | 3    | -1    | -1    |
-`------------------------------'
-addr  size jump  fail  
------------------------
-0x844 3    -1    -1
-0x806 62   0x844 -1
-0x7aa 92   0x844 0x806
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+
+.---------------------------.
+| addr | size | jump | fail |
+)---------------------------(
+`---------------------------'
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+addr size jump fail 
+--------------------
+
+addr size jump fail 
+--------------------

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I will take a look on weekend and fix it, I had tried to fix it multiple times but it didn't help. On Weekend, I will try again.

rz_table_add_rowf(t, "xdxx", bb->addr, bb->size, bb->jump, bb->fail);
char *table_str = rz_table_tostring(t);
rz_strbuf_append(buf, table_str);
RZ_FREE(table_str);
break;
case RZ_OUTPUT_MODE_RIZIN:
rz_cons_printf("f bb.%05" PFMT64x " @ 0x%08" PFMT64x "\n", bb->addr & 0xFFFFF, bb->addr);
rz_strbuf_appendf(buf, "f bb.%05" PFMT64x " @ 0x%08" PFMT64x "\n", bb->addr & 0xFFFFF, bb->addr);
break;
case RZ_OUTPUT_MODE_QUIET:
rz_cons_printf("0x%08" PFMT64x "\n", bb->addr);
rz_strbuf_appendf(buf, "0x%08" PFMT64x "\n", bb->addr);
break;
case RZ_OUTPUT_MODE_LONG: {
if (bb->jump != UT64_MAX) {
rz_cons_printf("jump: 0x%08" PFMT64x "\n", bb->jump);
rz_strbuf_appendf(buf, "jump: 0x%08" PFMT64x "\n", bb->jump);
}
if (bb->fail != UT64_MAX) {
rz_cons_printf("fail: 0x%08" PFMT64x "\n", bb->fail);
rz_strbuf_appendf(buf, "fail: 0x%08" PFMT64x "\n", bb->fail);
}
rz_cons_printf("opaddr: 0x%08" PFMT64x "\n", opaddr);
rz_cons_printf("addr: 0x%08" PFMT64x "\nsize: %" PFMT64d "\ninputs: %d\noutputs: %d\nninstr: %d\ntraced: %s\n",
rz_strbuf_appendf(buf, "opaddr: 0x%08" PFMT64x "\n", opaddr);
rz_strbuf_appendf(buf, "addr: 0x%08" PFMT64x "\nsize: %" PFMT64d "\ninputs: %d\noutputs: %d\nninstr: %d\ntraced: %s\n",
bb->addr, bb->size, inputs, outputs, bb->ninstr, rz_str_bool(bb->traced));
break;
}
default:
rz_warn_if_reached();
break;
}

return rz_strbuf_drain(buf);
}

static int bb_cmp(const void *a, const void *b, void *user) {
Expand All @@ -481,30 +488,39 @@ static int bb_cmp(const void *a, const void *b, void *user) {
return ba->addr - bb->addr;
}

RZ_IPI void rz_core_analysis_bbs_info_print(RzCore *core, RzAnalysisFunction *fcn, RzCmdStateOutput *state) {
rz_return_if_fail(core && fcn && state);
RZ_IPI char *rz_core_analysis_bbs_info_print(RzCore *core, RzAnalysisFunction *fcn, RzCmdStateOutput *state) {
HN026 marked this conversation as resolved.
Show resolved Hide resolved
rz_return_val_if_fail(core && fcn && state, NULL);
void **iter;
RzAnalysisBlock *bb;
RzStrBuf *buf = rz_strbuf_new("");
rz_cmd_state_output_array_start(state);
rz_cmd_state_output_set_columnsf(state, "xdxx", "addr", "size", "jump", "fail");
if (state->mode == RZ_OUTPUT_MODE_RIZIN) {
rz_cons_printf("fs blocks\n");
rz_strbuf_append(buf, "fs blocks\n");
}

rz_pvector_sort(fcn->bbs, bb_cmp, NULL);
rz_pvector_foreach (fcn->bbs, iter) {
bb = (RzAnalysisBlock *)*iter;
bb_info_print(core, fcn, bb, bb->addr, state->mode, state->d.pj, state->d.t);
char *bb_info = bb_info_print(core, fcn, bb, bb->addr, state->mode, state->d.pj, state->d.t);
rz_strbuf_append(buf, bb_info);
RZ_FREE(bb_info);
}

rz_cmd_state_output_array_end(state);
char *result = rz_strbuf_drain(buf);
return result;
}

RZ_IPI void rz_core_analysis_bb_info_print(RzCore *core, RzAnalysisBlock *bb, ut64 addr, RzCmdStateOutput *state) {
rz_return_if_fail(core && bb && state);
rz_cmd_state_output_set_columnsf(state, "xdxx", "addr", "size", "jump", "fail");
RzAnalysisFunction *fcn = rz_list_first(bb->fcns);
bb_info_print(core, fcn, bb, addr, state->mode, state->d.pj, state->d.t);
char *bb_info = bb_info_print(core, fcn, bb, addr, state->mode, state->d.pj, state->d.t);
if (bb_info) {
rz_cons_printf("%s", bb_info);
RZ_FREE(bb_info);
}
}

/*this only autoname those function that start with fcn.* or sym.func.* */
Expand Down
28 changes: 28 additions & 0 deletions librz/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2446,3 +2446,31 @@ RZ_API RzCmdStatus rz_core_core_plugins_print(RzCore *core, RzCmdStateOutput *st
rz_cmd_state_output_array_end(state);
return RZ_CMD_STATUS_OK;
}

/**
* \brief Filters the given string based on the provided filter.
*
* \param str RZ_NONNULL The string to be filtered.
* \param filter The filter string to be used for filtering the str.
* \return RZ_OWN char* The filtered string. The caller is responsible for freeing this string.
*/
RZ_API RZ_OWN char *rz_core_filter_string_output(RZ_NONNULL const char *str, const char *filter) {
HN026 marked this conversation as resolved.
Show resolved Hide resolved
rz_return_val_if_fail(str, NULL);
char *filtered_str = NULL;
char *str_copy = rz_str_dup(str);
RzList *lines = rz_str_split_list(str_copy, "\n", 0);
RzListIter *iter;
char *line;
rz_list_foreach (lines, iter, line) {
if (strstr(line, filter)) {
if (filtered_str) {
filtered_str = rz_str_append(filtered_str, "\n");
} else {
filtered_str = rz_str_dup("");
}
filtered_str = rz_str_append(filtered_str, line);
}
}
RZ_FREE(str_copy);
return filtered_str;
}
2 changes: 1 addition & 1 deletion librz/core/core_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RZ_IPI bool rz_core_analysis_function_set_signature(RzCore *core, RzAnalysisFunc
RZ_IPI void rz_core_analysis_function_signature_editor(RzCore *core, ut64 addr);
RZ_IPI void rz_core_analysis_bbs_asciiart(RzCore *core, RzAnalysisFunction *fcn);
RZ_IPI void rz_core_analysis_fcn_returns(RzCore *core, RzAnalysisFunction *fcn);
RZ_IPI void rz_core_analysis_bbs_info_print(RzCore *core, RzAnalysisFunction *fcn, RzCmdStateOutput *state);
RZ_IPI char *rz_core_analysis_bbs_info_print(RzCore *core, RzAnalysisFunction *fcn, RzCmdStateOutput *state);
RZ_IPI void rz_core_analysis_bb_info_print(RzCore *core, RzAnalysisBlock *bb, ut64 addr, RzCmdStateOutput *state);
RZ_IPI void rz_core_analysis_function_until(RzCore *core, ut64 addr_end);
RZ_IPI void rz_core_analysis_value_pointers(RzCore *core, RzOutputMode mode);
Expand Down
47 changes: 46 additions & 1 deletion librz/core/tui/visual.c
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,51 @@ static bool insert_mode_enabled(RzCore *core) {
return true;
}

static char *get_afb_output(RZ_NONNULL RzCore *core, RZ_NONNULL RzAnalysisFunction *fcn) {
HN026 marked this conversation as resolved.
Show resolved Hide resolved
rz_return_val_if_fail(core && fcn, NULL);
RzCmdStateOutput state;
state.mode = RZ_OUTPUT_MODE_STANDARD;
char *fcn_info = rz_core_analysis_bbs_info_print(core, fcn, &state);
return fcn_info;
}

/**
* \brief Seeks to any basic block of the current function.
*
* \param core The RzCore instance.
*/
static void rz_view_and_seek_to_bb(RZ_NONNULL RzCore *core) {
HN026 marked this conversation as resolved.
Show resolved Hide resolved
rz_return_if_fail(core);
RzAnalysisFunction *fcn = rz_analysis_get_fcn_in(core->analysis, core->offset, 0);
if (!fcn) {
return;
}
char *afb_output = get_afb_output(core, fcn);
HN026 marked this conversation as resolved.
Show resolved Hide resolved
char *output = rz_core_filter_string_output(afb_output, "");
RZ_FREE(afb_output);
if (!output) {
return;
}
rz_cons_println(output);
rz_cons_flush();
char *input = rz_cons_input("Seek to address: ");
if (RZ_STR_ISEMPTY(input)) {
return;
}
ut64 addr = strtoull(input, NULL, 16);
RZ_FREE(input);
RZ_FREE(output);
RzAnalysisBlock *bb;
void **iter;
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
if (bb->addr <= addr && addr < bb->addr + bb->size) {
rz_core_seek(core, addr, true);
break;
}
}
}

RZ_IPI void rz_core_visual_browse(RzCore *core, const char *input) {
const char *browsemsg =
"Browse stuff:\n"
Expand Down Expand Up @@ -1987,7 +2032,7 @@ RZ_IPI void rz_core_visual_browse(RzCore *core, const char *input) {
rz_debug_switch_to_first_thread(core->dbg);
break;
case 'b':
rz_core_cmd0(core, "s $(afb~...)");
rz_view_and_seek_to_bb(core);
break;
case 'i':
// XXX ii shows index first and iiq shows no offset :(
Expand Down
2 changes: 2 additions & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ RZ_API bool rz_core_plugin_add(RzCore *core, RZ_NONNULL RzCorePlugin *plugin);
RZ_API bool rz_core_plugin_del(RzCore *core, RZ_NONNULL RzCorePlugin *plugin);
RZ_API bool rz_core_plugin_fini(RzCore *core);

RZ_API RZ_OWN char *rz_core_filter_string_output(RZ_NONNULL const char *str, const char *filter);

// #define rz_core_ncast(x) (RzCore*)(size_t)(x)
RZ_API RZ_OWN RzList /*<char *>*/ *rz_core_theme_list(RZ_NONNULL RzCore *core);
RZ_API char *rz_core_theme_get(RzCore *core);
Expand Down
Loading