Skip to content

Commit

Permalink
Print value of global variable - avgp (#4014)
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka authored Dec 2, 2023
1 parent 953e1fb commit efb31b9
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions librz/core/cmd/cmd_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,24 @@ RZ_IPI RzCmdStatus rz_analysis_global_variable_rename_handler(RzCore *core, int
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_analysis_global_variable_print_handler(RzCore *core, int argc, const char **argv) {
const char *name = argv[1];
RzAnalysisVarGlobal *glob = rz_analysis_var_global_get_byname(core->analysis, name);
if (!glob) {
RZ_LOG_ERROR("Global variable '%s' does not exist!\n", name);
return RZ_CMD_STATUS_ERROR;
}
char *fmt = rz_type_as_format_pair(core->analysis->typedb, glob->type);
if (RZ_STR_ISEMPTY(fmt)) {
free(fmt);
return RZ_CMD_STATUS_ERROR;
}
// TODO: Convert to the API
rz_core_cmdf(core, "pf %s @ 0x%08" PFMT64x "\n", fmt, glob->addr);
free(fmt);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_analysis_global_variable_retype_handler(RzCore *core, int argc, const char **argv) {
const char *name = argv[1];
const char *type = argv[2];
Expand Down
4 changes: 4 additions & 0 deletions librz/core/cmd/cmd_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static RzCmdStatus type_format_print(RzCore *core, const char *type, ut64 addres
free(fmt);
return RZ_CMD_STATUS_ERROR;
}
// TODO: Convert to the API
rz_core_cmdf(core, "pf %s @ 0x%08" PFMT64x "\n", fmt, address);
free(fmt);
return RZ_CMD_STATUS_OK;
Expand All @@ -117,6 +118,7 @@ static RzCmdStatus type_format_print_variable(RzCore *core, const char *type, co
return RZ_CMD_STATUS_ERROR;
}
ut64 addr = rz_core_analysis_var_addr(core, var);
// TODO: Convert to the API
rz_core_cmdf(core, "pf %s @ 0x%08" PFMT64x "\n", fmt, addr);
free(fmt);
return RZ_CMD_STATUS_OK;
Expand All @@ -129,6 +131,7 @@ static RzCmdStatus type_format_print_value(RzCore *core, const char *type, ut64
free(fmt);
return RZ_CMD_STATUS_ERROR;
}
// TODO: Convert to the API
rz_core_cmdf(core, "pf %s @v:0x%08" PFMT64x "\n", fmt, val);
free(fmt);
return RZ_CMD_STATUS_OK;
Expand All @@ -141,6 +144,7 @@ static RzCmdStatus type_format_print_hexstring(RzCore *core, const char *type, c
free(fmt);
return RZ_CMD_STATUS_ERROR;
}
// TODO: Convert to the API
rz_core_cmdf(core, "pf %s @x:%s", fmt, hexpairs);
free(fmt);
return RZ_CMD_STATUS_OK;
Expand Down
7 changes: 7 additions & 0 deletions librz/core/cmd_descs/cmd_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,13 @@ commands:
type: RZ_CMD_ARG_TYPE_GLOBAL_VAR
- name: new_var_name
type: RZ_CMD_ARG_TYPE_STRING
- name: avgp
summary: print the global variable value
type: RZ_CMD_DESC_TYPE_ARGV
cname: analysis_global_variable_print
args:
- name: name
type: RZ_CMD_ARG_TYPE_GLOBAL_VAR
- name: avgt
summary: change the global variable type
type: RZ_CMD_DESC_TYPE_ARGV
Expand Down
17 changes: 17 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ static const RzCmdDescArg analysis_global_variable_add_args[3];
static const RzCmdDescArg analysis_global_variable_delete_byaddr_args[2];
static const RzCmdDescArg analysis_global_variable_delete_byname_args[2];
static const RzCmdDescArg analysis_global_variable_rename_args[3];
static const RzCmdDescArg analysis_global_variable_print_args[2];
static const RzCmdDescArg analysis_global_variable_retype_args[3];
static const RzCmdDescArg analysis_rtti_demangle_class_name_args[2];
static const RzCmdDescArg analysis_xrefs_set_0_args[2];
Expand Down Expand Up @@ -4848,6 +4849,19 @@ static const RzCmdDescHelp analysis_global_variable_rename_help = {
.args = analysis_global_variable_rename_args,
};

static const RzCmdDescArg analysis_global_variable_print_args[] = {
{
.name = "name",
.type = RZ_CMD_ARG_TYPE_GLOBAL_VAR,

},
{ 0 },
};
static const RzCmdDescHelp analysis_global_variable_print_help = {
.summary = "print the global variable value",
.args = analysis_global_variable_print_args,
};

static const RzCmdDescArg analysis_global_variable_retype_args[] = {
{
.name = "var_name",
Expand Down Expand Up @@ -19478,6 +19492,9 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *analysis_global_variable_rename_cd = rz_cmd_desc_argv_new(core->rcmd, avg_cd, "avgn", rz_analysis_global_variable_rename_handler, &analysis_global_variable_rename_help);
rz_warn_if_fail(analysis_global_variable_rename_cd);

RzCmdDesc *analysis_global_variable_print_cd = rz_cmd_desc_argv_new(core->rcmd, avg_cd, "avgp", rz_analysis_global_variable_print_handler, &analysis_global_variable_print_help);
rz_warn_if_fail(analysis_global_variable_print_cd);

RzCmdDesc *analysis_global_variable_retype_cd = rz_cmd_desc_argv_new(core->rcmd, avg_cd, "avgt", rz_analysis_global_variable_retype_handler, &analysis_global_variable_retype_help);
rz_warn_if_fail(analysis_global_variable_retype_cd);

Expand Down
2 changes: 2 additions & 0 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ RZ_IPI RzCmdStatus rz_analysis_global_variable_delete_byaddr_handler(RzCore *cor
RZ_IPI RzCmdStatus rz_analysis_global_variable_delete_byname_handler(RzCore *core, int argc, const char **argv);
// "avgn"
RZ_IPI RzCmdStatus rz_analysis_global_variable_rename_handler(RzCore *core, int argc, const char **argv);
// "avgp"
RZ_IPI RzCmdStatus rz_analysis_global_variable_print_handler(RzCore *core, int argc, const char **argv);
// "avgt"
RZ_IPI RzCmdStatus rz_analysis_global_variable_retype_handler(RzCore *core, int argc, const char **argv);
// "avr"
Expand Down
29 changes: 29 additions & 0 deletions test/db/cmd/cmd_avg
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,32 @@ EXPECT_ERR=<<EOF
ERROR: Global variable 'foo' does not exist!
EOF
RUN

NAME=avgp - print global variable value
FILE==
CMDS=<<EOF
wx 40102030405050230590909090
w alfdjsdfgkjdhfgkdghdfgdfg @ 0x20
avga foo int @ 0x10
avgp foo
avga boo char @ 0x0
avgp boo
avga goo "struct aaa { int a; char b; };" @ 0x5
avgp goo
avga qoo "enum qwe { A=0,B,C,D };" @ 0x15
avgp qoo
avga poo "char *" @ 0x25
avgp poo
avga xoo "char [5]" @ 0x30
avgp xoo
EOF
EXPECT=<<EOF
int : 0x00000010 = 0
char : 0x00000000 = '@'
a : 0x00000005 = 86200400
b : 0x00000009 = '.'
qwe : 0x00000015 = qwe (enum qwe) = 0x0 ; A
char : (*0x68646a6b67666473)0x00000025 = '.'
char : 0x00000030 = [ 'd', 'g', 'h', 'd', 'f' ]
EOF
RUN

0 comments on commit efb31b9

Please sign in to comment.