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 9 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
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;
}
53 changes: 52 additions & 1 deletion librz/core/tui/visual.c
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,57 @@ static bool insert_mode_enabled(RzCore *core) {
return true;
}

static char *rz_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);
RzAnalysisBlock *bb;
void **it;
RzStrBuf *buf = rz_strbuf_new("");
rz_pvector_foreach (fcn->bbs, it) {
bb = *it;
rz_strbuf_appendf(buf, "0x%08" PFMT64x " 0x%08" PFMT64x " 00:0000 %" PFMT64u " j 0x%08" PFMT64x " f 0x%08" PFMT64x "\n",
bb->addr, bb->addr + bb->size, bb->size, bb->jump, bb->fail);
}
char *result = rz_strbuf_drain(buf);
return result;
}

/**
* \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 = rz_get_afb_output(core, fcn);
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 +2038,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