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.
This commit is contained in:
Florian Märkl 2022-03-28 20:43:47 +02:00
parent 72a3815c01
commit 2c31d38d85

View File

@ -1221,10 +1221,13 @@ QString CutterCore::disassembleSingleInstruction(RVA addr)
RzAnalysisFunction *CutterCore::functionIn(ut64 addr) RzAnalysisFunction *CutterCore::functionIn(ut64 addr)
{ {
CORE_LOCK(); 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); RzList *fcns = rz_analysis_get_functions_in(core->analysis, addr);
RzAnalysisFunction *fcn = !rz_list_empty(fcns) fcn = !rz_list_empty(fcns) ? reinterpret_cast<RzAnalysisFunction *>(rz_list_first(fcns))
? reinterpret_cast<RzAnalysisFunction *>(rz_list_first(fcns)) : nullptr;
: nullptr;
rz_list_free(fcns); rz_list_free(fcns);
return fcn; return fcn;
} }
@ -1750,22 +1753,37 @@ CutterJson CutterCore::getRegisterValues()
QList<VariableDescription> CutterCore::getVariables(RVA at) QList<VariableDescription> CutterCore::getVariables(RVA at)
{ {
QList<VariableDescription> ret; QList<VariableDescription> ret;
CutterJson varsObject = cmdj(QString("afvj @ %1").arg(at)); CORE_LOCK();
RzAnalysisFunction *fcn = functionIn(at);
auto addVars = [&](VariableDescription::RefType refType, const CutterJson &array) { if (!fcn) {
for (CutterJson varObject : array) { return ret;
VariableDescription desc; }
desc.refType = refType; for (auto var : CutterPVector<RzAnalysisVar>(&fcn->vars)) {
desc.name = varObject["name"].toString(); VariableDescription desc;
desc.type = varObject["type"].toString(); switch (var->kind) {
ret << desc; 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;
} }
}; if (!var->name || !var->type) {
continue;
addVars(VariableDescription::RefType::SP, varsObject["sp"]); }
addVars(VariableDescription::RefType::BP, varsObject["bp"]); desc.name = QString::fromUtf8(var->name);
addVars(VariableDescription::RefType::Reg, varsObject["reg"]); 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; return ret;
} }