From 7dbe8d37866292271e390bd59dbc2a822e3b0189 Mon Sep 17 00:00:00 2001 From: Rot127 <45763064+Rot127@users.noreply.github.com> Date: Thu, 28 Nov 2024 17:18:51 +0000 Subject: [PATCH] Port `ik` and `ik*` commands to RzShell. (#4750) It removed the undocumented `ik.` and `ikv` commands, because they were an alias for `ik`. --- librz/core/cmd/cmd_info.c | 61 ++++++++++-------------------- librz/core/cmd_descs/cmd_descs.c | 41 ++++++++++++++++---- librz/core/cmd_descs/cmd_descs.h | 4 +- librz/core/cmd_descs/cmd_info.yaml | 31 +++++++++++---- test/db/cmd/cmd_i | 42 ++++++++++---------- 5 files changed, 101 insertions(+), 78 deletions(-) diff --git a/librz/core/cmd/cmd_info.c b/librz/core/cmd/cmd_info.c index 5b520c408c1..6ce43167b9d 100644 --- a/librz/core/cmd/cmd_info.c +++ b/librz/core/cmd/cmd_info.c @@ -135,49 +135,26 @@ static bool print_source_info(RzCore *core, PrintSourceInfoType type, RzCmdState return true; } -RZ_IPI int rz_cmd_info_kuery(void *data, const char *input) { - RzCore *core = (RzCore *)data; - RzBinObject *o = rz_bin_cur_object(core->bin); - Sdb *db = o ? o->kv : NULL; - switch (input[0]) { - case 'v': - if (db) { - char *o = sdb_querys(db, NULL, 0, input + 2); - if (o && *o) { - rz_cons_print(o); - } - free(o); - } - break; - case '*': - rz_core_bin_export_info(core, RZ_MODE_RIZINCMD); - break; - case '.': - case ' ': - if (db) { - char *o = sdb_querys(db, NULL, 0, input + 1); - if (o && *o) { - rz_cons_print(o); - } - free(o); - } - break; - case '\0': - if (db) { - char *o = sdb_querys(db, NULL, 0, "*"); - if (o && *o) { - rz_cons_print(o); - } - free(o); - } - break; - case '?': - default: - RZ_LOG_ERROR("core: Usage: ik [sdb-query]\n"); - RZ_LOG_ERROR("core: Usage: ik* # load all header information\n"); - return 1; +RZ_IPI RzCmdStatus rz_cmd_info_query_handler(RzCore *core, int argc, const char **argv) { + RzBinObject *obj = rz_bin_cur_object(core->bin); + if (!obj || !obj->kv) { + RZ_LOG_ERROR("No object file loaded to query.\n"); + return RZ_CMD_STATUS_ERROR; + } + char *query_result = sdb_querys(obj->kv, NULL, 0, argc > 1 ? argv[1] : "*"); + rz_cons_print(query_result); + free(query_result); + return RZ_CMD_STATUS_OK; +} + +RZ_IPI RzCmdStatus rz_cmd_info_show_header_info_handler(RzCore *core, int argc, const char **argv) { + RzBinObject *obj = rz_bin_cur_object(core->bin); + if (!obj || !obj->kv) { + RZ_LOG_ERROR("No object file loaded to query.\n"); + return RZ_CMD_STATUS_ERROR; } - return 0; + rz_core_bin_export_info(core, RZ_MODE_RIZINCMD); + return RZ_CMD_STATUS_OK; } #define GET_CHECK_CUR_BINFILE(core) \ diff --git a/librz/core/cmd_descs/cmd_descs.c b/librz/core/cmd_descs/cmd_descs.c index 5ee50914cf6..731a7affc8a 100644 --- a/librz/core/cmd_descs/cmd_descs.c +++ b/librz/core/cmd_descs/cmd_descs.c @@ -62,6 +62,7 @@ static const RzCmdDescDetail cmd_debug_inject_syscall_details[2]; static const RzCmdDescDetail eval_getset_details[2]; static const RzCmdDescDetail egg_config_details[2]; static const RzCmdDescDetail history_list_or_exec_details[2]; +static const RzCmdDescDetail cmd_info_query_details[2]; static const RzCmdDescDetail query_sdb_get_set_details[2]; static const RzCmdDescDetail cmd_print_byte_array_details[3]; static const RzCmdDescDetail pf_details[3]; @@ -532,7 +533,7 @@ static const RzCmdDescArg cmd_info_pdb_load_args[2]; static const RzCmdDescArg cmd_info_pdb_show_args[2]; static const RzCmdDescArg cmd_pdb_extract_args[3]; static const RzCmdDescArg cmd_info_demangle_args[3]; -static const RzCmdDescArg cmd_info_kuery_args[2]; +static const RzCmdDescArg cmd_info_query_args[2]; static const RzCmdDescArg cmd_info_plugins_args[2]; static const RzCmdDescArg cmd_info_resources_args[2]; static const RzCmdDescArg cmd_info_sections_args[2]; @@ -11681,9 +11682,22 @@ static const RzCmdDescHelp cmd_info_binary_help = { .args = cmd_info_binary_args, }; -static const RzCmdDescArg cmd_info_kuery_args[] = { +static const RzCmdDescHelp ik_help = { + .summary = "Query key-value database from RzBinObject", +}; +static const RzCmdDescDetailEntry cmd_info_query_Examples_detail_entries[] = { + { .text = "ik", .arg_str = NULL, .comment = "Show all key value pairs in the root namespace (same as 'ik *')." }, + { .text = "ik", .arg_str = " **", .comment = "Show all namespaces under root" }, + { .text = "ik", .arg_str = " versioninfo/versym/*", .comment = "Show all key value pairs in the 'versioninfo/versym/' namespace." }, + { 0 }, +}; +static const RzCmdDescDetail cmd_info_query_details[] = { + { .name = "Examples", .entries = cmd_info_query_Examples_detail_entries }, + { 0 }, +}; +static const RzCmdDescArg cmd_info_query_args[] = { { - .name = "query", + .name = "sdb-query", .type = RZ_CMD_ARG_TYPE_STRING, .flags = RZ_CMD_ARG_FLAG_LAST, .optional = true, @@ -11691,9 +11705,18 @@ static const RzCmdDescArg cmd_info_kuery_args[] = { }, { 0 }, }; -static const RzCmdDescHelp cmd_info_kuery_help = { - .summary = "Key-value database from RzBinObject", - .args = cmd_info_kuery_args, +static const RzCmdDescHelp cmd_info_query_help = { + .summary = "Query key-value database from RzBinObject", + .details = cmd_info_query_details, + .args = cmd_info_query_args, +}; + +static const RzCmdDescArg cmd_info_show_header_info_args[] = { + { 0 }, +}; +static const RzCmdDescHelp cmd_info_show_header_info_help = { + .summary = "Show all binary header information.", + .args = cmd_info_show_header_info_args, }; static const RzCmdDescArg cmd_info_libs_args[] = { @@ -21871,8 +21894,10 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) { rz_warn_if_fail(cmd_info_binary_cd); rz_cmd_desc_set_default_mode(cmd_info_binary_cd, RZ_OUTPUT_MODE_TABLE); - RzCmdDesc *cmd_info_kuery_cd = rz_cmd_desc_oldinput_new(core->rcmd, i_cd, "ik", rz_cmd_info_kuery, &cmd_info_kuery_help); - rz_warn_if_fail(cmd_info_kuery_cd); + RzCmdDesc *ik_cd = rz_cmd_desc_group_new(core->rcmd, i_cd, "ik", rz_cmd_info_query_handler, &cmd_info_query_help, &ik_help); + rz_warn_if_fail(ik_cd); + RzCmdDesc *cmd_info_show_header_info_cd = rz_cmd_desc_argv_new(core->rcmd, ik_cd, "ik*", rz_cmd_info_show_header_info_handler, &cmd_info_show_header_info_help); + rz_warn_if_fail(cmd_info_show_header_info_cd); RzCmdDesc *cmd_info_libs_cd = rz_cmd_desc_argv_state_new(core->rcmd, i_cd, "il", RZ_OUTPUT_MODE_TABLE | RZ_OUTPUT_MODE_JSON | RZ_OUTPUT_MODE_QUIET, rz_cmd_info_libs_handler, &cmd_info_libs_help); rz_warn_if_fail(cmd_info_libs_cd); diff --git a/librz/core/cmd_descs/cmd_descs.h b/librz/core/cmd_descs/cmd_descs.h index 1c01565171b..347493ef9bb 100644 --- a/librz/core/cmd_descs/cmd_descs.h +++ b/librz/core/cmd_descs/cmd_descs.h @@ -1569,7 +1569,9 @@ RZ_IPI RzCmdStatus rz_cmd_info_imports_handler(RzCore *core, int argc, const cha // "iI" RZ_IPI RzCmdStatus rz_cmd_info_binary_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state); // "ik" -RZ_IPI int rz_cmd_info_kuery(void *data, const char *input); +RZ_IPI RzCmdStatus rz_cmd_info_query_handler(RzCore *core, int argc, const char **argv); +// "ik*" +RZ_IPI RzCmdStatus rz_cmd_info_show_header_info_handler(RzCore *core, int argc, const char **argv); // "il" RZ_IPI RzCmdStatus rz_cmd_info_libs_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state); // "iL" diff --git a/librz/core/cmd_descs/cmd_info.yaml b/librz/core/cmd_descs/cmd_info.yaml index f47acd48ed5..ae6ca7037c3 100644 --- a/librz/core/cmd_descs/cmd_info.yaml +++ b/librz/core/cmd_descs/cmd_info.yaml @@ -244,13 +244,30 @@ commands: - RZ_OUTPUT_MODE_QUIET args: [] - name: ik - cname: cmd_info_kuery - summary: Key-value database from RzBinObject - type: RZ_CMD_DESC_TYPE_OLDINPUT - args: - - name: query - type: RZ_CMD_ARG_TYPE_STRING - optional: true + summary: Query key-value database from RzBinObject + subcommands: + - name: ik + summary: Query key-value database from RzBinObject + cname: cmd_info_query + args: + - name: sdb-query + type: RZ_CMD_ARG_TYPE_STRING + optional: true + details: + - name: "Examples" + entries: + - text: "ik" + comment: "Show all key value pairs in the root namespace (same as 'ik *')." + - text: "ik" + arg_str: " **" + comment: "Show all namespaces under root" + - text: "ik" + arg_str: " versioninfo/versym/*" + comment: "Show all key value pairs in the 'versioninfo/versym/' namespace." + - name: ik* + summary: Show all binary header information. + cname: cmd_info_show_header_info + args: [] - name: il cname: cmd_info_libs summary: List libraries diff --git a/test/db/cmd/cmd_i b/test/db/cmd/cmd_i index 4242235c1cd..ea22eb494c0 100644 --- a/test/db/cmd/cmd_i +++ b/test/db/cmd/cmd_i @@ -3891,7 +3891,16 @@ no EOF RUN -NAME=ibk use after free +NAME=ik nothing +CMDS=<