2019-09-06 05:40:20 +00:00
|
|
|
#include "DecompilerWidget.h"
|
|
|
|
#include "ui_DecompilerWidget.h"
|
2020-06-22 20:25:30 +00:00
|
|
|
#include "menus/DecompilerContextMenu.h"
|
2017-12-15 10:52:47 +00:00
|
|
|
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/Configuration.h"
|
|
|
|
#include "common/Helpers.h"
|
|
|
|
#include "common/TempConfig.h"
|
2019-07-12 08:57:07 +00:00
|
|
|
#include "common/SelectionHighlight.h"
|
2019-07-15 12:08:44 +00:00
|
|
|
#include "common/Decompiler.h"
|
2019-12-14 12:57:36 +00:00
|
|
|
#include "common/CutterSeekable.h"
|
2020-08-29 05:15:47 +00:00
|
|
|
#include "core/MainWindow.h"
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-07-11 13:21:54 +00:00
|
|
|
#include <QTextEdit>
|
2019-07-12 08:57:07 +00:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
#include <QTextBlock>
|
2020-07-30 08:41:23 +00:00
|
|
|
#include <QClipboard>
|
2019-07-12 08:57:07 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include <QTextBlockUserData>
|
2020-08-02 09:51:56 +00:00
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QAbstractSlider>
|
2019-07-12 08:57:07 +00:00
|
|
|
|
2020-05-22 11:49:34 +00:00
|
|
|
DecompilerWidget::DecompilerWidget(MainWindow *main) :
|
|
|
|
MemoryDockWidget(MemoryWidgetType::Decompiler, main),
|
2020-06-22 20:25:30 +00:00
|
|
|
mCtxMenu(new DecompilerContextMenu(this, main)),
|
2020-06-08 21:29:26 +00:00
|
|
|
ui(new Ui::DecompilerWidget),
|
2020-08-29 05:15:47 +00:00
|
|
|
decompilerBusy(false),
|
|
|
|
seekFromCursor(false),
|
2020-08-02 09:51:56 +00:00
|
|
|
scrollerHorizontal(0),
|
|
|
|
scrollerVertical(0),
|
|
|
|
previousFunctionAddr(RVA_INVALID),
|
|
|
|
decompiledFunctionAddr(RVA_INVALID),
|
2020-06-29 19:08:02 +00:00
|
|
|
code(Decompiler::makeWarning(tr("Choose an offset and refresh to get decompiled code")),
|
|
|
|
&r_annotated_code_free)
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
2017-12-21 14:23:44 +00:00
|
|
|
ui->setupUi(this);
|
2020-08-29 05:15:47 +00:00
|
|
|
setObjectName(main
|
|
|
|
? main->getUniqueObjectName(tr("Decompiler"))
|
|
|
|
: tr("Decompiler"));
|
|
|
|
updateWindowTitle();
|
|
|
|
|
2019-07-11 13:21:54 +00:00
|
|
|
syntaxHighlighter = Config()->createSyntaxHighlighter(ui->textEdit->document());
|
2020-08-15 08:01:27 +00:00
|
|
|
// Event filter to intercept double click and right click in the textbox
|
2019-12-14 12:57:36 +00:00
|
|
|
ui->textEdit->viewport()->installEventFilter(this);
|
|
|
|
|
2017-12-06 23:19:14 +00:00
|
|
|
setupFonts();
|
|
|
|
colorsUpdatedSlot();
|
|
|
|
|
2020-08-03 09:13:39 +00:00
|
|
|
connect(Config(), &Configuration::fontsUpdated, this, &DecompilerWidget::fontsUpdatedSlot);
|
|
|
|
connect(Config(), &Configuration::colorsUpdated, this, &DecompilerWidget::colorsUpdatedSlot);
|
|
|
|
connect(Core(), &CutterCore::registersChanged, this, &DecompilerWidget::highlightPC);
|
2020-07-30 08:41:23 +00:00
|
|
|
connect(mCtxMenu, &DecompilerContextMenu::copy, this, &DecompilerWidget::copy);
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-09-01 09:06:54 +00:00
|
|
|
refreshDeferrer = createRefreshDeferrer([this]() {
|
|
|
|
doRefresh();
|
|
|
|
});
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-07-15 12:08:44 +00:00
|
|
|
auto decompilers = Core()->getDecompilers();
|
2020-08-29 05:15:47 +00:00
|
|
|
QString selectedDecompilerId = Config()->getSelectedDecompiler();
|
2020-01-21 16:43:30 +00:00
|
|
|
if (selectedDecompilerId.isEmpty()) {
|
|
|
|
// If no decompiler was previously chosen. set r2ghidra as default decompiler
|
|
|
|
selectedDecompilerId = "r2ghidra";
|
|
|
|
}
|
2020-08-29 05:15:47 +00:00
|
|
|
for (Decompiler *dec : decompilers) {
|
2019-07-15 12:08:44 +00:00
|
|
|
ui->decompilerComboBox->addItem(dec->getName(), dec->getId());
|
2019-07-16 18:33:05 +00:00
|
|
|
if (dec->getId() == selectedDecompilerId) {
|
|
|
|
ui->decompilerComboBox->setCurrentIndex(ui->decompilerComboBox->count() - 1);
|
|
|
|
}
|
2019-07-15 12:08:44 +00:00
|
|
|
}
|
2019-08-28 17:01:12 +00:00
|
|
|
decompilerSelectionEnabled = decompilers.size() > 1;
|
|
|
|
ui->decompilerComboBox->setEnabled(decompilerSelectionEnabled);
|
|
|
|
if (decompilers.isEmpty()) {
|
|
|
|
ui->textEdit->setPlainText(tr("No Decompiler available."));
|
2018-09-08 07:12:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:08:02 +00:00
|
|
|
connect(ui->decompilerComboBox,
|
|
|
|
static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
|
|
|
|
&DecompilerWidget::decompilerSelected);
|
2020-08-29 05:15:47 +00:00
|
|
|
connectCursorPositionChanged(true);
|
|
|
|
connect(seekable, &CutterSeekable::seekableSeekChanged, this, &DecompilerWidget::seekChanged);
|
2019-08-07 11:38:22 +00:00
|
|
|
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
|
2020-08-03 09:13:39 +00:00
|
|
|
connect(ui->textEdit, &QWidget::customContextMenuRequested,
|
2020-08-15 08:01:27 +00:00
|
|
|
this, &DecompilerWidget::showDecompilerContextMenu);
|
2019-08-07 11:38:22 +00:00
|
|
|
|
2020-08-02 09:51:56 +00:00
|
|
|
connect(Core(), &CutterCore::breakpointsChanged, this, &DecompilerWidget::updateBreakpoints);
|
2020-08-29 05:15:47 +00:00
|
|
|
mCtxMenu->addSeparator();
|
|
|
|
mCtxMenu->addAction(&syncAction);
|
2019-08-07 11:38:22 +00:00
|
|
|
addActions(mCtxMenu->actions());
|
2019-07-12 08:57:07 +00:00
|
|
|
|
2019-08-28 17:01:12 +00:00
|
|
|
ui->progressLabel->setVisible(false);
|
2020-08-29 05:15:47 +00:00
|
|
|
doRefresh();
|
2019-09-01 09:06:54 +00:00
|
|
|
|
2020-08-29 05:15:47 +00:00
|
|
|
connect(Core(), &CutterCore::refreshAll, this, &DecompilerWidget::doRefresh);
|
|
|
|
connect(Core(), &CutterCore::functionRenamed, this, &DecompilerWidget::doRefresh);
|
|
|
|
connect(Core(), &CutterCore::varsChanged, this, &DecompilerWidget::doRefresh);
|
|
|
|
connect(Core(), &CutterCore::functionsChanged, this, &DecompilerWidget::doRefresh);
|
|
|
|
connect(Core(), &CutterCore::flagsChanged, this, &DecompilerWidget::doRefresh);
|
|
|
|
connect(Core(), &CutterCore::commentsChanged, this, &DecompilerWidget::refreshIfChanged);
|
|
|
|
connect(Core(), &CutterCore::instructionChanged, this, &DecompilerWidget::refreshIfChanged);
|
|
|
|
connect(Core(), &CutterCore::refreshCodeViews, this, &DecompilerWidget::doRefresh);
|
2019-12-14 12:57:36 +00:00
|
|
|
|
|
|
|
// Esc to seek backward
|
|
|
|
QAction *seekPrevAction = new QAction(this);
|
|
|
|
seekPrevAction->setShortcut(Qt::Key_Escape);
|
|
|
|
seekPrevAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
|
|
|
|
addAction(seekPrevAction);
|
|
|
|
connect(seekPrevAction, &QAction::triggered, seekable, &CutterSeekable::seekPrev);
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
DecompilerWidget::~DecompilerWidget() = default;
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
Decompiler *DecompilerWidget::getCurrentDecompiler()
|
2019-09-01 09:06:54 +00:00
|
|
|
{
|
|
|
|
return Core()->getDecompilerById(ui->decompilerComboBox->currentData().toString());
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:01:27 +00:00
|
|
|
ut64 DecompilerWidget::offsetForPosition(size_t pos)
|
2020-06-08 21:29:26 +00:00
|
|
|
{
|
|
|
|
size_t closestPos = SIZE_MAX;
|
2020-08-15 08:01:27 +00:00
|
|
|
ut64 closestOffset = mCtxMenu->getFirstOffsetInLine();
|
|
|
|
void *iter;
|
|
|
|
r_vector_foreach(&code->annotations, iter) {
|
|
|
|
RCodeAnnotation *annotation = (RCodeAnnotation *)iter;
|
2020-06-08 21:29:26 +00:00
|
|
|
if (annotation->type != R_CODE_ANNOTATION_TYPE_OFFSET || annotation->start > pos
|
|
|
|
|| annotation->end <= pos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (closestPos != SIZE_MAX && closestPos >= annotation->start) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
closestPos = annotation->start;
|
|
|
|
closestOffset = annotation->offset.offset;
|
|
|
|
}
|
|
|
|
return closestOffset;
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:01:27 +00:00
|
|
|
size_t DecompilerWidget::positionForOffset(ut64 offset)
|
2020-06-08 21:29:26 +00:00
|
|
|
{
|
|
|
|
size_t closestPos = SIZE_MAX;
|
|
|
|
ut64 closestOffset = UT64_MAX;
|
2020-08-15 08:01:27 +00:00
|
|
|
void *iter;
|
|
|
|
r_vector_foreach(&code->annotations, iter) {
|
|
|
|
RCodeAnnotation *annotation = (RCodeAnnotation *)iter;
|
2020-06-08 21:29:26 +00:00
|
|
|
if (annotation->type != R_CODE_ANNOTATION_TYPE_OFFSET || annotation->offset.offset > offset) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (closestOffset != UT64_MAX && closestOffset >= annotation->offset.offset) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
closestPos = annotation->start;
|
|
|
|
closestOffset = annotation->offset.offset;
|
|
|
|
}
|
|
|
|
return closestPos;
|
|
|
|
}
|
|
|
|
|
2020-08-29 05:15:47 +00:00
|
|
|
void DecompilerWidget::updateBreakpoints(RVA addr)
|
2020-08-02 09:51:56 +00:00
|
|
|
{
|
2020-08-29 05:15:47 +00:00
|
|
|
if (!addressInRange(addr)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-02 09:51:56 +00:00
|
|
|
setInfoForBreakpoints();
|
|
|
|
QTextCursor cursor = ui->textEdit->textCursor();
|
|
|
|
cursor.select(QTextCursor::Document);
|
|
|
|
cursor.setCharFormat(QTextCharFormat());
|
|
|
|
cursor.setBlockFormat(QTextBlockFormat());
|
|
|
|
ui->textEdit->setExtraSelections({});
|
|
|
|
highlightPC();
|
|
|
|
highlightBreakpoints();
|
|
|
|
updateSelection();
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:08:02 +00:00
|
|
|
void DecompilerWidget::setInfoForBreakpoints()
|
|
|
|
{
|
2020-08-29 05:15:47 +00:00
|
|
|
if (mCtxMenu->getIsTogglingBreakpoints()) {
|
2020-06-29 19:08:02 +00:00
|
|
|
return;
|
2020-08-29 05:15:47 +00:00
|
|
|
}
|
2020-06-29 19:08:02 +00:00
|
|
|
// Get the range of the line
|
|
|
|
QTextCursor cursorForLine = ui->textEdit->textCursor();
|
|
|
|
cursorForLine.movePosition(QTextCursor::StartOfLine);
|
|
|
|
size_t startPos = cursorForLine.position();
|
|
|
|
cursorForLine.movePosition(QTextCursor::EndOfLine);
|
|
|
|
size_t endPos = cursorForLine.position();
|
|
|
|
gatherBreakpointInfo(*code, startPos, endPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecompilerWidget::gatherBreakpointInfo(RAnnotatedCode &codeDecompiled, size_t startPos,
|
|
|
|
size_t endPos)
|
|
|
|
{
|
|
|
|
RVA firstOffset = RVA_MAX;
|
2020-08-15 08:01:27 +00:00
|
|
|
void *iter;
|
|
|
|
r_vector_foreach(&codeDecompiled.annotations, iter) {
|
|
|
|
RCodeAnnotation *annotation = (RCodeAnnotation *)iter;
|
2020-06-29 19:08:02 +00:00
|
|
|
if (annotation->type != R_CODE_ANNOTATION_TYPE_OFFSET) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-15 08:01:27 +00:00
|
|
|
if ((startPos <= annotation->start && annotation->start < endPos) || (startPos < annotation->end
|
2020-06-29 19:08:02 +00:00
|
|
|
&& annotation->end < endPos)) {
|
|
|
|
firstOffset = (annotation->offset.offset < firstOffset) ? annotation->offset.offset : firstOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mCtxMenu->setFirstOffsetInLine(firstOffset);
|
|
|
|
QList<RVA> functionBreakpoints = Core()->getBreakpointsInFunction(decompiledFunctionAddr);
|
|
|
|
QVector<RVA> offsetList;
|
2020-08-29 05:15:47 +00:00
|
|
|
for (RVA bpOffset : functionBreakpoints) {
|
2020-08-15 08:01:27 +00:00
|
|
|
size_t pos = positionForOffset(bpOffset);
|
2020-06-29 19:08:02 +00:00
|
|
|
if (startPos <= pos && pos <= endPos) {
|
|
|
|
offsetList.push_back(bpOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(offsetList.begin(), offsetList.end());
|
|
|
|
mCtxMenu->setAvailableBreakpoints(offsetList);
|
|
|
|
}
|
|
|
|
|
2020-08-29 05:15:47 +00:00
|
|
|
void DecompilerWidget::refreshIfChanged(RVA addr)
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
2020-08-29 05:15:47 +00:00
|
|
|
if (addressInRange(addr)) {
|
|
|
|
doRefresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DecompilerWidget::doRefresh()
|
|
|
|
{
|
|
|
|
RVA addr = seekable->getOffset();
|
2019-09-01 09:06:54 +00:00
|
|
|
if (!refreshDeferrer->attemptRefresh(nullptr)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-15 12:08:44 +00:00
|
|
|
if (ui->decompilerComboBox->currentIndex() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-01 09:06:54 +00:00
|
|
|
Decompiler *dec = getCurrentDecompiler();
|
|
|
|
if (!dec) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-29 05:15:47 +00:00
|
|
|
// Disabling decompiler selection combo box and making progress label visible ahead of decompilation.
|
|
|
|
ui->progressLabel->setVisible(true);
|
|
|
|
ui->decompilerComboBox->setEnabled(false);
|
2019-09-01 09:06:54 +00:00
|
|
|
if (dec->isRunning()) {
|
2020-08-29 05:15:47 +00:00
|
|
|
if (!decompilerBusy) {
|
|
|
|
connect(dec, &Decompiler::finished, this, &DecompilerWidget::doRefresh);
|
|
|
|
}
|
2017-12-21 14:23:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-29 05:15:47 +00:00
|
|
|
disconnect(dec, &Decompiler::finished, this, &DecompilerWidget::doRefresh);
|
2019-12-19 17:58:30 +00:00
|
|
|
// Clear all selections since we just refreshed
|
|
|
|
ui->textEdit->setExtraSelections({});
|
2020-08-02 09:51:56 +00:00
|
|
|
previousFunctionAddr = decompiledFunctionAddr;
|
2019-09-03 14:25:28 +00:00
|
|
|
decompiledFunctionAddr = Core()->getFunctionStart(addr);
|
2020-08-29 05:15:47 +00:00
|
|
|
updateWindowTitle();
|
|
|
|
if (decompiledFunctionAddr == RVA_INVALID) {
|
|
|
|
// No function was found, so making the progress label invisible and enabling
|
|
|
|
// the decompiler selection combo box as we are not waiting for any decompilation to finish.
|
|
|
|
ui->progressLabel->setVisible(false);
|
|
|
|
ui->decompilerComboBox->setEnabled(true);
|
|
|
|
connectCursorPositionChanged(false);
|
|
|
|
ui->textEdit->setPlainText(
|
|
|
|
tr("No function found at this offset. Seek to a function or define one in order to decompile it."));
|
|
|
|
connectCursorPositionChanged(true);
|
2019-08-28 17:01:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-29 05:15:47 +00:00
|
|
|
mCtxMenu->setDecompiledFunctionAddress(decompiledFunctionAddr);
|
|
|
|
connect(dec, &Decompiler::finished, this, &DecompilerWidget::decompilationFinished);
|
|
|
|
decompilerBusy = true;
|
|
|
|
dec->decompileAt(addr);
|
2019-08-28 17:01:12 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::refreshDecompiler()
|
2019-08-28 17:01:12 +00:00
|
|
|
{
|
2019-09-01 09:06:54 +00:00
|
|
|
doRefresh();
|
2020-06-29 19:08:02 +00:00
|
|
|
setInfoForBreakpoints();
|
2019-08-28 17:01:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 17:58:30 +00:00
|
|
|
QTextCursor DecompilerWidget::getCursorForAddress(RVA addr)
|
|
|
|
{
|
2020-08-15 08:01:27 +00:00
|
|
|
size_t pos = positionForOffset(addr);
|
2019-12-19 17:58:30 +00:00
|
|
|
if (pos == SIZE_MAX || pos == 0) {
|
|
|
|
return QTextCursor();
|
|
|
|
}
|
|
|
|
QTextCursor cursor = ui->textEdit->textCursor();
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2020-06-08 21:29:26 +00:00
|
|
|
void DecompilerWidget::decompilationFinished(RAnnotatedCode *codeDecompiled)
|
2019-08-28 17:01:12 +00:00
|
|
|
{
|
2020-08-02 09:51:56 +00:00
|
|
|
bool isDisplayReset = false;
|
|
|
|
if (previousFunctionAddr == decompiledFunctionAddr) {
|
|
|
|
scrollerHorizontal = ui->textEdit->horizontalScrollBar()->sliderPosition();
|
|
|
|
scrollerVertical = ui->textEdit->verticalScrollBar()->sliderPosition();
|
|
|
|
isDisplayReset = true;
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:01:12 +00:00
|
|
|
ui->progressLabel->setVisible(false);
|
|
|
|
ui->decompilerComboBox->setEnabled(decompilerSelectionEnabled);
|
2020-07-30 08:41:23 +00:00
|
|
|
|
2020-07-16 08:56:38 +00:00
|
|
|
mCtxMenu->setAnnotationHere(nullptr);
|
2020-06-08 21:29:26 +00:00
|
|
|
this->code.reset(codeDecompiled);
|
2020-08-29 05:15:47 +00:00
|
|
|
|
|
|
|
Decompiler *dec = getCurrentDecompiler();
|
|
|
|
QObject::disconnect(dec, &Decompiler::finished, this, &DecompilerWidget::decompilationFinished);
|
|
|
|
decompilerBusy = false;
|
|
|
|
|
2020-06-08 21:29:26 +00:00
|
|
|
QString codeString = QString::fromUtf8(this->code->code);
|
|
|
|
if (codeString.isEmpty()) {
|
2020-08-29 05:15:47 +00:00
|
|
|
connectCursorPositionChanged(false);
|
2019-08-28 17:01:12 +00:00
|
|
|
ui->textEdit->setPlainText(tr("Cannot decompile at this address (Not a function?)"));
|
2020-08-29 05:15:47 +00:00
|
|
|
connectCursorPositionChanged(true);
|
|
|
|
lowestOffsetInCode = RVA_MAX;
|
|
|
|
highestOffsetInCode = 0;
|
2017-12-15 10:52:47 +00:00
|
|
|
return;
|
2019-07-12 08:57:07 +00:00
|
|
|
} else {
|
|
|
|
connectCursorPositionChanged(false);
|
2020-08-29 05:15:47 +00:00
|
|
|
ui->textEdit->setPlainText(codeString);
|
|
|
|
connectCursorPositionChanged(true);
|
2019-09-01 09:06:54 +00:00
|
|
|
updateCursorPosition();
|
2019-12-19 17:58:30 +00:00
|
|
|
highlightPC();
|
|
|
|
highlightBreakpoints();
|
2020-08-29 05:15:47 +00:00
|
|
|
lowestOffsetInCode = RVA_MAX;
|
|
|
|
highestOffsetInCode = 0;
|
|
|
|
void *iter;
|
|
|
|
r_vector_foreach(&code->annotations, iter) {
|
|
|
|
RCodeAnnotation *annotation = (RCodeAnnotation *)iter;
|
|
|
|
if (annotation->type == R_CODE_ANNOTATION_TYPE_OFFSET) {
|
|
|
|
if (lowestOffsetInCode > annotation->offset.offset) {
|
|
|
|
lowestOffsetInCode = annotation->offset.offset;
|
|
|
|
}
|
|
|
|
if (highestOffsetInCode < annotation->offset.offset) {
|
|
|
|
highestOffsetInCode = annotation->offset.offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
2020-08-02 09:51:56 +00:00
|
|
|
|
|
|
|
if (isDisplayReset) {
|
|
|
|
ui->textEdit->horizontalScrollBar()->setSliderPosition(scrollerHorizontal);
|
|
|
|
ui->textEdit->verticalScrollBar()->setSliderPosition(scrollerVertical);
|
|
|
|
}
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 08:56:38 +00:00
|
|
|
void DecompilerWidget::setAnnotationsAtCursor(size_t pos)
|
|
|
|
{
|
|
|
|
RCodeAnnotation *annotationAtPos = nullptr;
|
2020-08-15 08:01:27 +00:00
|
|
|
void *iter;
|
|
|
|
r_vector_foreach(&this->code->annotations, iter) {
|
|
|
|
RCodeAnnotation *annotation = (RCodeAnnotation *)iter;
|
2020-07-16 08:56:38 +00:00
|
|
|
if (annotation->type == R_CODE_ANNOTATION_TYPE_OFFSET ||
|
|
|
|
annotation->type == R_CODE_ANNOTATION_TYPE_SYNTAX_HIGHLIGHT ||
|
|
|
|
annotation->start > pos || annotation->end <= pos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
annotationAtPos = annotation;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mCtxMenu->setAnnotationHere(annotationAtPos);
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::decompilerSelected()
|
2019-07-16 18:33:05 +00:00
|
|
|
{
|
2019-08-23 20:06:44 +00:00
|
|
|
Config()->setSelectedDecompiler(ui->decompilerComboBox->currentData().toString());
|
2020-08-29 05:15:47 +00:00
|
|
|
doRefresh();
|
2019-07-16 18:33:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 05:15:47 +00:00
|
|
|
void DecompilerWidget::connectCursorPositionChanged(bool connectPositionChange)
|
2019-07-12 08:57:07 +00:00
|
|
|
{
|
2020-08-29 05:15:47 +00:00
|
|
|
if (!connectPositionChange) {
|
|
|
|
disconnect(ui->textEdit, &QPlainTextEdit::cursorPositionChanged, this,
|
|
|
|
&DecompilerWidget::cursorPositionChanged);
|
2019-07-12 08:57:07 +00:00
|
|
|
} else {
|
2020-06-29 19:08:02 +00:00
|
|
|
connect(ui->textEdit, &QPlainTextEdit::cursorPositionChanged, this,
|
|
|
|
&DecompilerWidget::cursorPositionChanged);
|
2019-07-12 08:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::cursorPositionChanged()
|
2019-07-12 08:57:07 +00:00
|
|
|
{
|
2019-12-14 10:42:24 +00:00
|
|
|
// Do not perform seeks along with the cursor while selecting multiple lines
|
2020-06-22 20:25:30 +00:00
|
|
|
if (!ui->textEdit->textCursor().selectedText().isEmpty()) {
|
2019-12-14 10:42:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-29 19:08:02 +00:00
|
|
|
|
2019-08-27 15:27:39 +00:00
|
|
|
size_t pos = ui->textEdit->textCursor().position();
|
2020-07-16 08:56:38 +00:00
|
|
|
setAnnotationsAtCursor(pos);
|
2020-06-29 19:08:02 +00:00
|
|
|
setInfoForBreakpoints();
|
|
|
|
|
2020-08-15 08:01:27 +00:00
|
|
|
RVA offset = offsetForPosition(pos);
|
2020-08-29 05:15:47 +00:00
|
|
|
if (offset != RVA_INVALID && offset != seekable->getOffset()) {
|
2019-07-12 08:57:07 +00:00
|
|
|
seekFromCursor = true;
|
2020-08-29 05:15:47 +00:00
|
|
|
seekable->seek(offset);
|
2019-08-07 11:38:22 +00:00
|
|
|
mCtxMenu->setOffset(offset);
|
2019-07-12 08:57:07 +00:00
|
|
|
seekFromCursor = false;
|
|
|
|
}
|
|
|
|
updateSelection();
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::seekChanged()
|
2019-07-12 08:57:07 +00:00
|
|
|
{
|
|
|
|
if (seekFromCursor) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-29 05:15:47 +00:00
|
|
|
RVA fcnAddr = Core()->getFunctionStart(seekable->getOffset());
|
|
|
|
if (fcnAddr == RVA_INVALID || fcnAddr != decompiledFunctionAddr) {
|
|
|
|
doRefresh();
|
|
|
|
return;
|
2019-09-01 09:06:54 +00:00
|
|
|
}
|
2019-07-12 08:57:07 +00:00
|
|
|
updateCursorPosition();
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::updateCursorPosition()
|
2019-07-12 08:57:07 +00:00
|
|
|
{
|
2020-08-29 05:15:47 +00:00
|
|
|
RVA offset = seekable->getOffset();
|
2020-08-15 08:01:27 +00:00
|
|
|
size_t pos = positionForOffset(offset);
|
2019-08-27 15:27:39 +00:00
|
|
|
if (pos == SIZE_MAX) {
|
|
|
|
return;
|
2019-07-12 08:57:07 +00:00
|
|
|
}
|
2019-10-20 08:58:58 +00:00
|
|
|
mCtxMenu->setOffset(offset);
|
2020-08-29 05:15:47 +00:00
|
|
|
connectCursorPositionChanged(false);
|
2019-08-27 15:27:39 +00:00
|
|
|
QTextCursor cursor = ui->textEdit->textCursor();
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
ui->textEdit->setTextCursor(cursor);
|
|
|
|
updateSelection();
|
2020-08-29 05:15:47 +00:00
|
|
|
connectCursorPositionChanged(true);
|
2019-07-12 08:57:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::setupFonts()
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
2019-10-12 05:50:10 +00:00
|
|
|
ui->textEdit->setFont(Config()->getFont());
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::updateSelection()
|
2019-07-12 08:57:07 +00:00
|
|
|
{
|
|
|
|
QList<QTextEdit::ExtraSelection> extraSelections;
|
|
|
|
|
|
|
|
// Highlight the current line
|
2020-08-29 05:15:47 +00:00
|
|
|
QTextCursor cursor = ui->textEdit->textCursor();
|
2019-08-27 15:27:39 +00:00
|
|
|
extraSelections.append(createLineHighlightSelection(cursor));
|
2019-07-12 08:57:07 +00:00
|
|
|
|
|
|
|
// Highlight all the words in the document same as the current one
|
|
|
|
cursor.select(QTextCursor::WordUnderCursor);
|
|
|
|
QString searchString = cursor.selectedText();
|
2020-07-30 08:41:23 +00:00
|
|
|
mCtxMenu->setCurHighlightedWord(searchString);
|
2019-07-12 08:57:07 +00:00
|
|
|
extraSelections.append(createSameWordsSelections(ui->textEdit, searchString));
|
|
|
|
|
|
|
|
ui->textEdit->setExtraSelections(extraSelections);
|
2019-12-19 17:58:30 +00:00
|
|
|
// Highlight PC after updating the selected line
|
|
|
|
highlightPC();
|
2019-07-12 08:57:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
QString DecompilerWidget::getWindowTitle() const
|
2019-06-18 13:02:41 +00:00
|
|
|
{
|
2020-08-29 05:15:47 +00:00
|
|
|
RAnalFunction *fcn = Core()->functionAt(decompiledFunctionAddr);
|
|
|
|
QString windowTitle = tr("Decompiler");
|
|
|
|
if (fcn != NULL) {
|
|
|
|
windowTitle += " (" + QString(fcn->name) + ")";
|
|
|
|
} else {
|
|
|
|
windowTitle += " (Empty)";
|
|
|
|
}
|
|
|
|
return windowTitle;
|
2019-06-18 13:02:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 05:50:10 +00:00
|
|
|
void DecompilerWidget::fontsUpdatedSlot()
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
|
|
|
setupFonts();
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
void DecompilerWidget::colorsUpdatedSlot()
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
|
|
|
}
|
2019-08-07 11:38:22 +00:00
|
|
|
|
2020-08-15 08:01:27 +00:00
|
|
|
void DecompilerWidget::showDecompilerContextMenu(const QPoint &pt)
|
2019-08-07 11:38:22 +00:00
|
|
|
{
|
|
|
|
mCtxMenu->exec(ui->textEdit->mapToGlobal(pt));
|
2019-12-14 12:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecompilerWidget::seekToReference()
|
|
|
|
{
|
|
|
|
size_t pos = ui->textEdit->textCursor().position();
|
2020-08-15 08:01:27 +00:00
|
|
|
RVA offset = offsetForPosition(pos);
|
2019-12-14 12:57:36 +00:00
|
|
|
seekable->seekToReference(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecompilerWidget::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::MouseButtonDblClick
|
2020-06-29 19:08:02 +00:00
|
|
|
&& (obj == ui->textEdit || obj == ui->textEdit->viewport())) {
|
2019-12-14 12:57:36 +00:00
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
2020-07-11 05:54:16 +00:00
|
|
|
ui->textEdit->setTextCursor(ui->textEdit->cursorForPosition(mouseEvent->pos()));
|
2019-12-14 12:57:36 +00:00
|
|
|
seekToReference();
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-11 05:54:16 +00:00
|
|
|
if (event->type() == QEvent::MouseButtonPress
|
|
|
|
&& (obj == ui->textEdit || obj == ui->textEdit->viewport())) {
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
2020-07-30 08:41:23 +00:00
|
|
|
if (mouseEvent->button() == Qt::RightButton && !ui->textEdit->textCursor().hasSelection()) {
|
2020-07-11 05:54:16 +00:00
|
|
|
ui->textEdit->setTextCursor(ui->textEdit->cursorForPosition(mouseEvent->pos()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-14 12:57:36 +00:00
|
|
|
return MemoryDockWidget::eventFilter(obj, event);
|
|
|
|
}
|
2019-12-19 17:58:30 +00:00
|
|
|
|
|
|
|
void DecompilerWidget::highlightPC()
|
|
|
|
{
|
|
|
|
RVA PCAddress = Core()->getProgramCounterValue();
|
|
|
|
if (PCAddress == RVA_INVALID || (Core()->getFunctionStart(PCAddress) != decompiledFunctionAddr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCursor cursor = getCursorForAddress(PCAddress);
|
|
|
|
if (!cursor.isNull()) {
|
|
|
|
colorLine(createLineHighlightPC(cursor));
|
|
|
|
}
|
2020-06-22 20:25:30 +00:00
|
|
|
|
2019-12-19 17:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecompilerWidget::highlightBreakpoints()
|
|
|
|
{
|
|
|
|
|
|
|
|
QList<RVA> functionBreakpoints = Core()->getBreakpointsInFunction(decompiledFunctionAddr);
|
|
|
|
QTextCursor cursor;
|
2020-08-29 05:15:47 +00:00
|
|
|
for (RVA &bp : functionBreakpoints) {
|
2019-12-19 17:58:30 +00:00
|
|
|
if (bp == RVA_INVALID) {
|
|
|
|
continue;;
|
|
|
|
}
|
|
|
|
cursor = getCursorForAddress(bp);
|
|
|
|
if (!cursor.isNull()) {
|
|
|
|
// Use a Block formatting since these lines are not updated frequently as selections and PC
|
|
|
|
QTextBlockFormat f;
|
|
|
|
f.setBackground(ConfigColor("gui.breakpoint_background"));
|
|
|
|
cursor.setBlockFormat(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecompilerWidget::colorLine(QTextEdit::ExtraSelection extraSelection)
|
|
|
|
{
|
|
|
|
QList<QTextEdit::ExtraSelection> extraSelections = ui->textEdit->extraSelections();
|
|
|
|
extraSelections.append(extraSelection);
|
|
|
|
ui->textEdit->setExtraSelections(extraSelections);
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-30 08:41:23 +00:00
|
|
|
|
|
|
|
void DecompilerWidget::copy()
|
|
|
|
{
|
|
|
|
if (ui->textEdit->textCursor().hasSelection()) {
|
|
|
|
ui->textEdit->copy();
|
|
|
|
} else {
|
|
|
|
QTextCursor cursor = ui->textEdit->textCursor();
|
|
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
|
|
cursor.select(QTextCursor::WordUnderCursor);
|
|
|
|
if (!cursor.selectedText().isEmpty()) {
|
|
|
|
clipboard->setText(cursor.selectedText());
|
|
|
|
} else {
|
|
|
|
cursor.select(QTextCursor::LineUnderCursor);
|
|
|
|
clipboard->setText(cursor.selectedText());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 05:15:47 +00:00
|
|
|
|
|
|
|
bool DecompilerWidget::addressInRange(RVA addr)
|
|
|
|
{
|
|
|
|
if (lowestOffsetInCode <= addr && addr <= highestOffsetInCode) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|