Skip to content

Commit

Permalink
Port ik and ik* commands to RzShell. (#4750)
Browse files Browse the repository at this point in the history
It removed the undocumented `ik.` and `ikv` commands,
because they were an alias for `ik`.
  • Loading branch information
Rot127 authored Nov 28, 2024
1 parent df1c382 commit 7dbe8d3
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 78 deletions.
61 changes: 19 additions & 42 deletions librz/core/cmd/cmd_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
41 changes: 33 additions & 8 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -11681,19 +11682,41 @@ 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,

},
{ 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[] = {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 24 additions & 7 deletions librz/core/cmd_descs/cmd_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 22 additions & 20 deletions test/db/cmd/cmd_i
Original file line number Diff line number Diff line change
Expand Up @@ -3891,7 +3891,16 @@ no
EOF
RUN

NAME=ibk use after free
NAME=ik nothing
CMDS=<<EOF
ik
EOF
EXPECT=
EXPECT_ERR=<<EOF
EOF
RUN

NAME=ik normal query
FILE=bins/elf/analysis/x86-helloworld-gcc
CMDS=<<EOF
obR
Expand All @@ -3902,6 +3911,16 @@ EXPECT=<<EOF
EOF
RUN

NAME=ik*
FILE=bins/elf/analysis/x86-helloworld-gcc
CMDS=<<EOF
ik*~?elf_header
EOF
EXPECT=<<EOF
3
EOF
RUN

NAME=i on freebsd elf
FILE=bins/elf/analysis/hello-freebsd-x86_64
CMDS=i~os
Expand Down Expand Up @@ -4466,31 +4485,14 @@ EXPECT=<<EOF
EOF
RUN

NAME=ikv
NAME=ik
FILE=bins/mach0/fatmach0-3true
CMDS=ikv mach0.entry.vaddr
CMDS=ik mach0.entry.vaddr
EXPECT=<<EOF
0x100000ef8
EOF
RUN

NAME=ik.
FILE=bins/mach0/fatmach0-3true
CMDS=ik. mach0.entry.vaddr
EXPECT=<<EOF
0x100000ef8
EOF
RUN

NAME=ik?
BROKEN=1
FILE=bins/mach0/fatmach0-3true
CMDS=ik?~?Usage
EXPECT=<<EOF
1
EOF
RUN

NAME=iS=
FILE=bins/mach0/fatmach0-3true
CMDS=iS=
Expand Down

0 comments on commit 7dbe8d3

Please sign in to comment.