From d65ad504caac26893067d8b5507c6dcd14e91e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Mon, 28 Mar 2022 20:43:47 +0200 Subject: [PATCH] Rewrite getVariables with API This also avoids annoying "ERROR: No function found in ..." beging printed by afvj when right-clicking anywhere outside a function. The heuristics in functionIn() also have been adjusted to prefer the function that has its entrypoint at the given addr, if there is any. --- src/core/Cutter.cpp | 54 ++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index 85924471..5c9ba1d0 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -1221,10 +1221,13 @@ QString CutterCore::disassembleSingleInstruction(RVA addr) RzAnalysisFunction *CutterCore::functionIn(ut64 addr) { CORE_LOCK(); + RzAnalysisFunction *fcn = rz_analysis_get_function_at(core->analysis, addr); + if (fcn) { + return fcn; + } RzList *fcns = rz_analysis_get_functions_in(core->analysis, addr); - RzAnalysisFunction *fcn = !rz_list_empty(fcns) - ? reinterpret_cast(rz_list_first(fcns)) - : nullptr; + fcn = !rz_list_empty(fcns) ? reinterpret_cast(rz_list_first(fcns)) + : nullptr; rz_list_free(fcns); return fcn; } @@ -1750,22 +1753,37 @@ CutterJson CutterCore::getRegisterValues() QList CutterCore::getVariables(RVA at) { QList ret; - CutterJson varsObject = cmdj(QString("afvj @ %1").arg(at)); - - auto addVars = [&](VariableDescription::RefType refType, const CutterJson &array) { - for (CutterJson varObject : array) { - VariableDescription desc; - desc.refType = refType; - desc.name = varObject["name"].toString(); - desc.type = varObject["type"].toString(); - ret << desc; + CORE_LOCK(); + RzAnalysisFunction *fcn = functionIn(at); + if (!fcn) { + return ret; + } + for (auto var : CutterPVector(&fcn->vars)) { + VariableDescription desc; + switch (var->kind) { + case RZ_ANALYSIS_VAR_KIND_BPV: + desc.refType = VariableDescription::RefType::BP; + break; + case RZ_ANALYSIS_VAR_KIND_SPV: + desc.refType = VariableDescription::RefType::SP; + break; + case RZ_ANALYSIS_VAR_KIND_REG: + default: + desc.refType = VariableDescription::RefType::Reg; + break; } - }; - - addVars(VariableDescription::RefType::SP, varsObject["sp"]); - addVars(VariableDescription::RefType::BP, varsObject["bp"]); - addVars(VariableDescription::RefType::Reg, varsObject["reg"]); - + if (!var->name || !var->type) { + continue; + } + desc.name = QString::fromUtf8(var->name); + char *tn = rz_type_as_string(core->analysis->typedb, var->type); + if (!tn) { + continue; + } + desc.type = QString::fromUtf8(tn); + rz_mem_free(tn); + ret.push_back(desc); + } return ret; }