From fde03584a0d889846eb4df219bd27ed1f3d75ccd Mon Sep 17 00:00:00 2001 From: billow Date: Wed, 1 Nov 2023 00:00:29 +0800 Subject: [PATCH] `afvl` command improvements (#3945) - in SHORT mode only print `@ COMPOSITE` or `@ LOCLIST` and omit the details - in LONG and TABLE mode `[(0x8010fb1c, 0x8010fb3f): a4, (0x8010fb42, 0x8010fb48): a13, (0x8010fb48, 0x8010fb5e): a4, (0x8010fb5e, 0x8010fb64): a13, (0x8010fb64, 0x8010fb6d): a4]` => `[(0x8010fb1c, 23): a4, (0x8010fb42, 6): a13, (0x8010fb48, 16): a4, (0x8010fb5e, 6): a13, (0x8010fb64, 9): a4]` --- librz/analysis/var.c | 32 +++++-------- librz/bin/dwarf/op.c | 3 +- librz/core/canalysis.c | 2 +- librz/core/cmd/cmd_analysis.c | 4 +- librz/core/tui/vmenus.c | 2 +- librz/include/rz_analysis.h | 2 + test/db/analysis/vars | 88 +++++++++++++++++------------------ 7 files changed, 63 insertions(+), 70 deletions(-) diff --git a/librz/analysis/var.c b/librz/analysis/var.c index 943517522a6..b16bb0b2eb3 100644 --- a/librz/analysis/var.c +++ b/librz/analysis/var.c @@ -105,27 +105,10 @@ static void strbuf_append_sign_hex(RzStrBuf *sb, st64 x) { rz_strbuf_appendf(sb, " %c 0x%" PFMT64x, sign, RZ_ABS(x)); } -static void composite_dump(RZ_NONNULL RZ_BORROW RzAnalysis *a, - RZ_NONNULL RZ_BORROW RZ_OUT RzStrBuf *sb, - RZ_NONNULL RZ_BORROW const RzVector /**/ *composite) { - rz_strbuf_append(sb, "composite: ["); - ut32 i; - ut32 end = rz_vector_len(composite) - 1; - RzAnalysisVarStoragePiece *piece = NULL; - rz_vector_enumerate(composite, piece, i) { - rz_strbuf_appendf(sb, "(.%" PFMT32u ", %" PFMT32u "): ", - piece->offset_in_bits, piece->size_in_bits); - rz_analysis_var_storage_dump(a, sb, piece->storage); - if (i < end) { - rz_strbuf_append(sb, ", "); - } - } - rz_strbuf_append(sb, "]"); -} - RZ_API void rz_analysis_var_storage_dump( RZ_NONNULL RZ_BORROW RzAnalysis *a, RZ_NONNULL RZ_BORROW RZ_OUT RzStrBuf *sb, + RZ_NULLABLE RZ_BORROW const RzAnalysisVar *var, RZ_NONNULL RZ_BORROW const RzAnalysisVarStorage *storage) { rz_return_if_fail(a && sb && storage); switch (storage->type) { @@ -139,12 +122,18 @@ RZ_API void rz_analysis_var_storage_dump( break; } case RZ_ANALYSIS_VAR_STORAGE_COMPOSITE: { - composite_dump(a, sb, storage->composite); + rz_strbuf_append(sb, "COMPOSITE"); break; } case RZ_ANALYSIS_VAR_STORAGE_EVAL_PENDING: /// Omit storage information - rz_strbuf_append(sb, "..."); + if (var && var->origin.kind == RZ_ANALYSIS_VAR_ORIGIN_DWARF && + var->origin.dw_var && var->origin.dw_var->location && + var->origin.dw_var->location->kind == RzBinDwarfLocationKind_LOCLIST) { + rz_strbuf_append(sb, "LOCLIST"); + } else { + rz_strbuf_append(sb, "..."); + } break; default: rz_warn_if_reached(); @@ -154,10 +143,11 @@ RZ_API void rz_analysis_var_storage_dump( RZ_API RZ_OWN char *rz_analysis_var_storage_to_string( RZ_NONNULL RZ_BORROW RzAnalysis *a, + RZ_NULLABLE RZ_BORROW const RzAnalysisVar *var, RZ_NONNULL RZ_BORROW const RzAnalysisVarStorage *storage) { rz_return_val_if_fail(a && storage, NULL); RzStrBuf *sb = rz_strbuf_new(NULL); - rz_analysis_var_storage_dump(a, sb, storage); + rz_analysis_var_storage_dump(a, sb, var, storage); return rz_strbuf_drain(sb); } diff --git a/librz/bin/dwarf/op.c b/librz/bin/dwarf/op.c index b94ab6f5000..816a2b035c1 100644 --- a/librz/bin/dwarf/op.c +++ b/librz/bin/dwarf/op.c @@ -1626,7 +1626,8 @@ RZ_API void rz_bin_dwarf_loclist_dump( void **it = NULL; rz_pvector_foreach (&loclist->entries, it) { RzBinDwarfLocListEntry *entry = *it; - rz_strbuf_appendf(sb, "%s(0x%" PFMT64x ", 0x%" PFMT64x "):", rz_str_get(opt->loclist_indent), entry->range->begin, entry->range->end); + rz_strbuf_appendf(sb, "%s(0x%" PFMT64x ", %" PFMT64d "):", + rz_str_get(opt->loclist_indent), entry->range->begin, entry->range->end - entry->range->begin); if (entry->location) { rz_strbuf_append(sb, " "); diff --git a/librz/core/canalysis.c b/librz/core/canalysis.c index f1fa6b54814..4969036b545 100644 --- a/librz/core/canalysis.c +++ b/librz/core/canalysis.c @@ -6519,6 +6519,6 @@ RZ_API RZ_OWN char *rz_core_analysis_var_to_string(RZ_NONNULL RzCore *core, RZ_N constr ? "} " : ""); free(vartype); free(constr); - rz_analysis_var_storage_dump(core->analysis, sb, &var->storage); + rz_analysis_var_storage_dump(core->analysis, sb, var, &var->storage); return rz_strbuf_drain(sb); } diff --git a/librz/core/cmd/cmd_analysis.c b/librz/core/cmd/cmd_analysis.c index b3c5a40f6a2..604d93d7ed8 100644 --- a/librz/core/cmd/cmd_analysis.c +++ b/librz/core/cmd/cmd_analysis.c @@ -2510,7 +2510,7 @@ static void var_show( RZ_NONNULL RzAnalysisVar *var) { char *constr = rz_analysis_var_get_constraints_readable(var); char *var_type_string = rz_type_as_string(ctx->core->analysis->typedb, var->type); - char *storage_string = rz_analysis_var_storage_to_string(ctx->core->analysis, &var->storage); + char *storage_string = rz_analysis_var_storage_to_string(ctx->core->analysis, var, &var->storage); RzBinDWARFDumpOption dump_opt = { .dwarf_register_mapping = ctx->core->analysis->debug_info->dwarf_register_mapping, .loclist_sep = ",\t", @@ -2949,7 +2949,7 @@ static RzCmdStatus analysis_function_vars_getsetref(RzCore *core, RzAnalysisVarS RzAnalysisVar *var = rz_analysis_function_get_var_at(fcn, stor); if (!var) { - char *stor_str = rz_analysis_var_storage_to_string(core->analysis, stor); + char *stor_str = rz_analysis_var_storage_to_string(core->analysis, NULL, stor); RZ_LOG_ERROR("core: Cannot find variable with %s\n", stor_str); free(stor_str); return RZ_CMD_STATUS_ERROR; diff --git a/librz/core/tui/vmenus.c b/librz/core/tui/vmenus.c index fe4908379ba..6dbcdf533ec 100644 --- a/librz/core/tui/vmenus.c +++ b/librz/core/tui/vmenus.c @@ -239,7 +239,7 @@ static ut64 var_variables_show(RzCore *core, int idx, int *vindex, int show, int rz_cons_printf("%s%s %s %s @ ", i == *vindex ? "* " : " ", rz_analysis_var_is_arg(var) ? "arg" : "var", vartype, var->name); free(vartype); - char *storage_str = rz_analysis_var_storage_to_string(core->analysis, &var->storage); + char *storage_str = rz_analysis_var_storage_to_string(core->analysis, var, &var->storage); rz_cons_strcat(storage_str); free(storage_str); } diff --git a/librz/include/rz_analysis.h b/librz/include/rz_analysis.h index cbbefc0a4cd..a81620cd032 100644 --- a/librz/include/rz_analysis.h +++ b/librz/include/rz_analysis.h @@ -1804,6 +1804,7 @@ RZ_API bool rz_analysis_var_storage_type_from_string( RZ_API void rz_analysis_var_storage_dump( RZ_NONNULL RZ_BORROW RzAnalysis *a, RZ_NONNULL RZ_BORROW RZ_OUT RzStrBuf *sb, + RZ_NULLABLE RZ_BORROW const RzAnalysisVar *var, RZ_NONNULL RZ_BORROW const RzAnalysisVarStorage *storage); RZ_API void rz_analysis_var_storage_dump_pj( RZ_NONNULL RZ_BORROW RZ_OUT PJ *pj, @@ -1811,6 +1812,7 @@ RZ_API void rz_analysis_var_storage_dump_pj( RZ_NONNULL RZ_BORROW const RzAnalysisVarStorage *storage); RZ_API RZ_OWN char *rz_analysis_var_storage_to_string( RZ_NONNULL RZ_BORROW RzAnalysis *a, + RZ_NULLABLE RZ_BORROW const RzAnalysisVar *var, RZ_NONNULL RZ_BORROW const RzAnalysisVarStorage *storage); RZ_API void rz_analysis_var_storage_poolify( RZ_NONNULL RZ_BORROW RzAnalysis *analysis, diff --git a/test/db/analysis/vars b/test/db/analysis/vars index 485cb29895c..1f62e553a6f 100644 --- a/test/db/analysis/vars +++ b/test/db/analysis/vars @@ -892,10 +892,10 @@ EXPECT=<] +is_arg name type constraints origin addr +----------------------------------------------------------------------------------------------------------------------------------------- +false ap va_list DWARF loclist: [(0x80000c22, 20): CFA+0, (0x80000c36, 5): a7, (0x80000c3b, 1): CFA+0] +true fmt const char * DWARF loclist: [(0x80000c22, 10): a4, (0x80000c2c, 15): a6, (0x80000c3b, 1): ] false ans int DWARF empty int printf(const char *fmt, ...); ; CALL XREFS from dbg.main @ 0x8000054e, 0x80000636 @@ -910,12 +910,12 @@ arg void *str @ a4 arg const char *buf @ a4 var int32_t arg6 @ a5 arg size_t n @ d5 -is_arg name type constraints origin addr -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -true str void * DWARF loclist: [(0x80000c04, 0x80000c0a): a4, (0x80000c0a, 0x80000c22): a15] -true buf const char * DWARF loclist: [(0x80000c04, 0x80000c10): a5, (0x80000c10, 0x80000c13): a4, (0x80000c13, 0x80000c22): ] +is_arg name type constraints origin addr +-------------------------------------------------------------------------------------------------------------------------------------------------------------- +true str void * DWARF loclist: [(0x80000c04, 6): a4, (0x80000c0a, 24): a15] +true buf const char * DWARF loclist: [(0x80000c04, 12): a5, (0x80000c10, 3): a4, (0x80000c13, 15): ] false arg6 int32_t rizin a5 -true n size_t DWARF loclist: [(0x80000c04, 0x80000c0e): d4, (0x80000c0e, 0x80000c13): d5, (0x80000c13, 0x80000c1a): d15, (0x80000c1a, 0x80000c22): ] +true n size_t DWARF loclist: [(0x80000c04, 10): d4, (0x80000c0e, 5): d5, (0x80000c13, 7): d15, (0x80000c1a, 8): ] void * prout(void *str, const char *buf, size_t n); ;-- prout: / void * prout(void *str, const char *buf, size_t n) @@ -930,21 +930,21 @@ arg fp_number_type *a @ a4 var int32_t arg6 @ a5 arg fp_number_type *tmp @ a6 var int b_normal_exp @ d2 -var intfrac tfraction @ composite: [(.0, 32): d2, (.0, 32): d3] -var fractype b_fraction @ composite: [(.0, 32): d8, (.0, 32): d9] -var int a_normal_exp @ ... -var fractype a_fraction @ ... -is_arg name type constraints origin addr ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -true b fp_number_type * DWARF loclist: [(0x80003c60, 0x80003c8c): a5, (0x80003c8c, 0x80003c8e): a2, (0x80003c8e, 0x80003c92): a5, (0x80003c92, 0x80003c98): a2, (0x80003c98, 0x80003cb4): a5, (0x80003cb4, 0x80003cb6): a2, (0x80003cb6, 0x80003cba): a5, (0x80003cba, 0x80003ce9): a2, (0x80003ce9, 0x80003d10): a12, (0x80003d10, 0x80003d1d): a2, (0x80003d1d, 0x80003d46): a12, (0x80003d46, 0x80003d56): a2, (0x80003d56, 0x80003e02): a12, (0x80003e02, 0x80003e04): ] -true a fp_number_type * DWARF loclist: [(0x80003c60, 0x80003c9c): a4, (0x80003c9c, 0x80003cae): a2, (0x80003cae, 0x80003cb4): , (0x80003cb4, 0x80003ce9): a4, (0x80003ce9, 0x80003d10): a13, (0x80003d10, 0x80003d1d): a4, (0x80003d1d, 0x80003d46): a13, (0x80003d46, 0x80003d56): a4, (0x80003d56, 0x80003e02): a13, (0x80003e02, 0x80003e04): ] +var intfrac tfraction @ COMPOSITE +var fractype b_fraction @ COMPOSITE +var int a_normal_exp @ LOCLIST +var fractype a_fraction @ LOCLIST +is_arg name type constraints origin addr +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +true b fp_number_type * DWARF loclist: [(0x80003c60, 44): a5, (0x80003c8c, 2): a2, (0x80003c8e, 4): a5, (0x80003c92, 6): a2, (0x80003c98, 28): a5, (0x80003cb4, 2): a2, (0x80003cb6, 4): a5, (0x80003cba, 47): a2, (0x80003ce9, 39): a12, (0x80003d10, 13): a2, (0x80003d1d, 41): a12, (0x80003d46, 16): a2, (0x80003d56, 172): a12, (0x80003e02, 2): ] +true a fp_number_type * DWARF loclist: [(0x80003c60, 60): a4, (0x80003c9c, 18): a2, (0x80003cae, 6): , (0x80003cb4, 53): a4, (0x80003ce9, 39): a13, (0x80003d10, 13): a4, (0x80003d1d, 41): a13, (0x80003d46, 16): a4, (0x80003d56, 172): a13, (0x80003e02, 2): ] false arg6 int32_t rizin a5 -true tmp fp_number_type * DWARF loclist: [(0x80003c60, 0x80003ce9): a6, (0x80003ce9, 0x80003d10): a15, (0x80003d10, 0x80003d1d): a6, (0x80003d1d, 0x80003d46): a15, (0x80003d46, 0x80003d56): a6, (0x80003d56, 0x80003dea): a15, (0x80003dea, 0x80003e02): a2, (0x80003e02, 0x80003e04): ] -false b_normal_exp int DWARF loclist: [(0x80003cc0, 0x80003ce0): d2, (0x80003ce0, 0x80003ce9): , (0x80003d10, 0x80003d1d): d2, (0x80003d46, 0x80003d56): d2] +true tmp fp_number_type * DWARF loclist: [(0x80003c60, 137): a6, (0x80003ce9, 39): a15, (0x80003d10, 13): a6, (0x80003d1d, 41): a15, (0x80003d46, 16): a6, (0x80003d56, 148): a15, (0x80003dea, 24): a2, (0x80003e02, 2): ] +false b_normal_exp int DWARF loclist: [(0x80003cc0, 32): d2, (0x80003ce0, 9): , (0x80003d10, 13): d2, (0x80003d46, 16): d2] false tfraction intfrac DWARF composite: [(.0, 32): d2, (.0, 32): d3] -false b_fraction fractype DWARF loclist: [(0x80003cd8, 0x80003d08): composite: [(.0, 32): d8, (.0, 32): d9], (0x80003d0e, 0x80003e02): composite: [(.0, 32): d8, (.0, 32): d9]] -false a_normal_exp int DWARF loclist: [(0x80003cbc, 0x80003d4c): d15, (0x80003d4c, 0x80003d52): a4+8, (0x80003d52, 0x80003d88): d15, (0x80003d88, 0x80003d98): a15+8, (0x80003dc6, 0x80003dd0): d15, (0x80003dd0, 0x80003dd6): a15+8] -false a_fraction fractype DWARF loclist: [(0x80003cd4, 0x80003d3c): composite: [(.0, 32): d10, (.0, 32): d11], (0x80003d44, 0x80003d50): composite: [(.0, 32): d10, (.0, 32): d11], (0x80003d50, 0x80003d52): a4+12, (0x80003d52, 0x80003e02): composite: [(.0, 32): d10, (.0, 32): d11]] +false b_fraction fractype DWARF loclist: [(0x80003cd8, 48): composite: [(.0, 32): d8, (.0, 32): d9], (0x80003d0e, 244): composite: [(.0, 32): d8, (.0, 32): d9]] +false a_normal_exp int DWARF loclist: [(0x80003cbc, 144): d15, (0x80003d4c, 6): a4+8, (0x80003d52, 54): d15, (0x80003d88, 16): a15+8, (0x80003dc6, 10): d15, (0x80003dd0, 6): a15+8] +false a_fraction fractype DWARF loclist: [(0x80003cd4, 104): composite: [(.0, 32): d10, (.0, 32): d11], (0x80003d44, 12): composite: [(.0, 32): d10, (.0, 32): d11], (0x80003d50, 2): a4+12, (0x80003d52, 176): composite: [(.0, 32): d10, (.0, 32): d11]] fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_type *tmp); ; CALL XREF from dbg.__adddf3 @ 0x80003e32 ; CALL XREF from dbg.__subdf3 @ 0x80003e72 @@ -954,23 +954,23 @@ fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_ty | ; arg fp_number_type *a @ a4 | ; arg fp_number_type *b @ a2 | ; arg fp_number_type *tmp @ a6 -| ; var intfrac tfraction @ composite: [(.0, 32): d2, (.0, 32): d3] -| ; var int a_normal_exp @ ... +| ; var intfrac tfraction @ COMPOSITE +| ; var int a_normal_exp @ LOCLIST | ; var int b_normal_exp @ d2 -| ; var fractype a_fraction @ ... -| ; var fractype b_fraction @ composite: [(.0, 32): d8, (.0, 32): d9] +| ; var fractype a_fraction @ LOCLIST +| ; var fractype b_fraction @ COMPOSITE | 0x80003c60 ld.bu d15, [a4]#0 ; fp-bit.c:604 ; arg5 ; fp_number_type * _fpadd_parts(fp_number_type *a, fp_number_type *b, fp_number_type *tmp) --------- arg const char *s @ a2 var const char *sc @ a2 var int32_t arg5 @ a4 arg size_t maxsize @ d4 -is_arg name type constraints origin addr ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -true s const char * DWARF loclist: [(0x800030ca, 0x800030dc): a4, (0x800030dc, 0x800030e0): , (0x800030e0, 0x800030ea): a4, (0x800030ea, 0x800030ec): a2] +is_arg name type constraints origin addr +----------------------------------------------------------------------------------------------------------------------------------------------------------------- +true s const char * DWARF loclist: [(0x800030ca, 18): a4, (0x800030dc, 4): , (0x800030e0, 10): a4, (0x800030ea, 2): a2] false sc const char * DWARF a2 false arg5 int32_t rizin a4 -true maxsize size_t DWARF loclist: [(0x800030ca, 0x800030d2): d4, (0x800030ea, 0x800030ec): d4] +true maxsize size_t DWARF loclist: [(0x800030ca, 8): d4, (0x800030ea, 2): d4] size_t strnlen_s(const char *s, size_t maxsize); ; CALL XREF from dbg._Fail_s @ 0x8000191e ;-- strnlen_s: @@ -986,12 +986,12 @@ arg char *s @ a4 arg mbstate_t *pst @ a5 arg wchar_t wc @ d4 var _Statab *pmbstate @ ... -is_arg name type constraints origin addr ----------------------------------------------------------------------------------------------------------------------- +is_arg name type constraints origin addr +------------------------------------------------------------------------------------------------------ false pwcstate _Statab * DWARF a13 -true s char * DWARF loclist: [(0x800018a6, 0x800018bb): a4, (0x800018bb, 0x800018ec): a15] -true pst mbstate_t * DWARF loclist: [(0x800018a6, 0x800018bb): a5, (0x800018bb, 0x800018ec): a12] -true wc wchar_t DWARF loclist: [(0x800018a6, 0x800018bb): d4, (0x800018bb, 0x800018ec): d15] +true s char * DWARF loclist: [(0x800018a6, 21): a4, (0x800018bb, 49): a15] +true pst mbstate_t * DWARF loclist: [(0x800018a6, 21): a5, (0x800018bb, 49): a12] +true wc wchar_t DWARF loclist: [(0x800018a6, 21): d4, (0x800018bb, 49): d15] false pmbstate _Statab * DWARF empty int _Wctomb(char *s, wchar_t wc, mbstate_t *pst); ; CALL XREF from dbg._Putstr @ 0x800014d2 @@ -1011,15 +1011,15 @@ var int32_t arg5 @ a4 var int32_t arg6 @ a5 var int32_t arg7 @ a6 arg size_t nin @ d15 -is_arg name type constraints origin addr -------------------------------------------------------------------------------------------------------------------- -true pst mbstate_t * DWARF loclist: [(0x80003084, 0x8000309b): a6, (0x8000309b, 0x800030b0): a12] -true s const char * DWARF loclist: [(0x80003084, 0x8000309b): a5, (0x8000309b, 0x800030b0): a13] -true pwc wchar_t * DWARF loclist: [(0x80003084, 0x8000309b): a4, (0x8000309b, 0x800030b0): a14] +is_arg name type constraints origin addr +--------------------------------------------------------------------------------------------------- +true pst mbstate_t * DWARF loclist: [(0x80003084, 23): a6, (0x8000309b, 21): a12] +true s const char * DWARF loclist: [(0x80003084, 23): a5, (0x8000309b, 21): a13] +true pwc wchar_t * DWARF loclist: [(0x80003084, 23): a4, (0x8000309b, 21): a14] false arg5 int32_t rizin a4 false arg6 int32_t rizin a5 false arg7 int32_t rizin a6 -true nin size_t DWARF loclist: [(0x80003084, 0x8000309b): d4, (0x8000309b, 0x800030b0): d15] +true nin size_t DWARF loclist: [(0x80003084, 23): d4, (0x8000309b, 21): d15] int _Mbtowc(wchar_t *pwc, const char *s, size_t nin, mbstate_t *pst); ; CALL XREF from dbg._Printf @ 0x80000d02 ;-- _Mbtowc: @@ -1047,10 +1047,10 @@ int feraiseexcept(int except); var size_t size_arg @ a0 var Ppvoidfn newfuns @ a2 var size_t inc @ d15 -is_arg name type constraints origin addr -------------------------------------------------------------------------------------------------------------------- +is_arg name type constraints origin addr +---------------------------------------------------------------------------------------------------- false size_arg size_t rizin a0 -false newfuns Ppvoidfn DWARF loclist: [(0x800019aa, 0x800019b8): a2, (0x800019b8, 0x80001a1c): a12] +false newfuns Ppvoidfn DWARF loclist: [(0x800019aa, 14): a2, (0x800019b8, 100): a12] false inc size_t DWARF d15 int _Atrealloc(); ; CALL XREF from dbg.atexit @ 0x80001a4c