Use Rizin API for Comments and Instruction writing (#2856)

This commit is contained in:
billow 2021-12-19 18:00:45 +08:00 committed by GitHub
parent d85383fcfc
commit 8cc4d14a6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 20 deletions

View File

@ -392,7 +392,7 @@ bool CutterCore::isRedirectableDebugee()
RzIODesc *desc;
CutterRzListForeach (descs, it, RzIODesc, desc) {
QString URI = QString(desc->uri);
if (URI.contains("ptrace") | URI.contains("mach")) {
if (URI.contains("ptrace") || URI.contains("mach")) {
return true;
}
}
@ -747,19 +747,22 @@ QString CutterCore::getInstructionOpcode(RVA addr)
void CutterCore::editInstruction(RVA addr, const QString &inst)
{
cmdRawAt(QString("wa %1").arg(inst), addr);
CORE_LOCK();
rz_core_write_assembly(core, addr, inst.trimmed().toStdString().c_str(), false, false);
emit instructionChanged(addr);
}
void CutterCore::nopInstruction(RVA addr)
{
cmdRawAt("wao nop", addr);
CORE_LOCK();
applyAtSeek([&]() { rz_core_hack(core, "nop"); }, addr);
emit instructionChanged(addr);
}
void CutterCore::jmpReverse(RVA addr)
{
cmdRawAt("wao recj", addr);
CORE_LOCK();
applyAtSeek([&]() { rz_core_hack(core, "recj"); }, addr);
emit instructionChanged(addr);
}
@ -849,13 +852,15 @@ int CutterCore::sizeofDataMeta(RVA addr)
void CutterCore::setComment(RVA addr, const QString &cmt)
{
cmdRawAt(QString("CCu base64:%1").arg(QString(cmt.toLocal8Bit().toBase64())), addr);
CORE_LOCK();
rz_meta_set_string(core->analysis, RZ_META_TYPE_COMMENT, addr, cmt.toStdString().c_str());
emit commentsChanged(addr);
}
void CutterCore::delComment(RVA addr)
{
cmdRawAt("CC-", addr);
CORE_LOCK();
rz_meta_del(core->analysis, RZ_META_TYPE_COMMENT, addr, 1);
emit commentsChanged(addr);
}

View File

@ -15,6 +15,7 @@
#include <QErrorMessage>
#include <QMutex>
#include <QDir>
#include <functional>
class AsyncTaskManager;
class BasicInstructionHighlighter;
@ -115,6 +116,14 @@ public:
return cmdRawAt(str.toUtf8().constData(), address);
}
void applyAtSeek(std::function<void()> fn, RVA address)
{
RVA oldOffset = getOffset();
seekSilent(address);
fn();
seekSilent(oldOffset);
}
QJsonDocument cmdj(const char *str);
QJsonDocument cmdj(const QString &str) { return cmdj(str.toUtf8().constData()); }
QJsonDocument cmdjAt(const char *str, RVA address);
@ -267,9 +276,10 @@ public:
void createNewClass(const QString &cls);
void renameClass(const QString &oldName, const QString &newName);
void deleteClass(const QString &cls);
bool getAnalysisMethod(const QString &cls, const QString &meth, AnalysisMethodDescription *desc);
bool getAnalysisMethod(const QString &cls, const QString &meth,
AnalysisMethodDescription *desc);
void renameAnalysisMethod(const QString &className, const QString &oldMethodName,
const QString &newMethodName);
const QString &newMethodName);
void setAnalysisMethod(const QString &cls, const AnalysisMethodDescription &meth);
/* File related methods */

View File

@ -34,20 +34,18 @@ void CommentsDialog::setComment(const QString &comment)
void CommentsDialog::addOrEditComment(RVA offset, QWidget *parent)
{
QString oldComment = Core()->cmdRawAt("CC.", offset);
// Remove newline at the end added by cmd
oldComment.remove(oldComment.length() - 1, 1);
CommentsDialog c(parent);
QString comment = Core()->getCommentAt(offset);
CommentsDialog dialog(parent);
if (oldComment.isNull() || oldComment.isEmpty()) {
c.setWindowTitle(tr("Add Comment at %1").arg(RzAddressString(offset)));
if (comment.isNull() || comment.isEmpty()) {
dialog.setWindowTitle(tr("Add Comment at %1").arg(RzAddressString(offset)));
} else {
c.setWindowTitle(tr("Edit Comment at %1").arg(RzAddressString(offset)));
dialog.setWindowTitle(tr("Edit Comment at %1").arg(RzAddressString(offset)));
}
c.setComment(oldComment);
if (c.exec()) {
QString comment = c.getComment();
dialog.setComment(comment);
if (dialog.exec()) {
comment = dialog.getComment();
if (comment.isEmpty()) {
Core()->delComment(offset);
} else {

View File

@ -154,7 +154,7 @@ void DecompilerContextMenu::aboutToShowSlot()
{
if (this->firstOffsetInLine != RVA_MAX) {
actionShowInSubmenu.setVisible(true);
QString comment = Core()->cmdRawAt("CC.", this->firstOffsetInLine);
QString comment = Core()->getCommentAt(firstOffsetInLine);
actionAddComment.setVisible(true);
if (comment.isEmpty()) {
actionDeleteComment.setVisible(false);

View File

@ -536,7 +536,7 @@ void DisassemblyContextMenu::aboutToShowSlot()
QString stringDefinition = Core()->cmdRawAt("Cs.", offset);
actionSetAsStringRemove.setVisible(!stringDefinition.isEmpty());
QString comment = Core()->cmdRawAt("CC.", offset);
QString comment = Core()->getCommentAt(offset);
if (comment.isNull() || comment.isEmpty()) {
actionDeleteComment.setVisible(false);