2017-12-06 23:19:14 +00:00
|
|
|
#include "PseudocodeWidget.h"
|
2017-12-21 14:23:44 +00:00
|
|
|
#include "ui_PseudocodeWidget.h"
|
2019-08-07 11:38:22 +00:00
|
|
|
#include "menus/DisassemblyContextMenu.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"
|
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>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QTextBlockUserData>
|
|
|
|
|
2018-03-16 21:46:57 +00:00
|
|
|
PseudocodeWidget::PseudocodeWidget(MainWindow *main, QAction *action) :
|
2019-07-19 19:21:12 +00:00
|
|
|
MemoryDockWidget(MemoryWidgetType::Pseudocode, main, action),
|
2019-08-07 11:38:22 +00:00
|
|
|
mCtxMenu(new DisassemblyContextMenu(this, main)),
|
2018-03-21 20:32:32 +00:00
|
|
|
ui(new Ui::PseudocodeWidget)
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
2017-12-21 14:23:44 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
2019-07-11 13:21:54 +00:00
|
|
|
syntaxHighlighter = Config()->createSyntaxHighlighter(ui->textEdit->document());
|
2017-12-16 13:22:56 +00:00
|
|
|
|
2017-12-06 23:19:14 +00:00
|
|
|
setupFonts();
|
|
|
|
colorsUpdatedSlot();
|
|
|
|
|
|
|
|
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(fontsUpdated()));
|
|
|
|
connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(colorsUpdatedSlot()));
|
|
|
|
|
2019-01-13 15:41:08 +00:00
|
|
|
// TODO Use RefreshDeferrer and remove the refresh button
|
2017-12-21 14:23:44 +00:00
|
|
|
connect(ui->refreshButton, &QAbstractButton::clicked, this, [this]() {
|
2019-01-12 17:02:51 +00:00
|
|
|
doRefresh(Core()->getOffset());
|
2017-12-06 23:19:14 +00:00
|
|
|
});
|
|
|
|
|
2019-07-15 12:08:44 +00:00
|
|
|
auto decompilers = Core()->getDecompilers();
|
2019-07-16 18:33:05 +00:00
|
|
|
auto selectedDecompilerId = Config()->getSelectedDecompiler();
|
2019-07-15 12:08:44 +00:00
|
|
|
for (auto dec : decompilers) {
|
|
|
|
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-08-28 17:01:12 +00:00
|
|
|
connect(dec, &Decompiler::finished, this, &PseudocodeWidget::decompilationFinished);
|
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
|
|
|
}
|
|
|
|
|
2019-07-16 18:33:05 +00:00
|
|
|
connect(ui->decompilerComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &PseudocodeWidget::decompilerSelected);
|
2019-07-12 08:57:07 +00:00
|
|
|
connectCursorPositionChanged(false);
|
|
|
|
connect(Core(), &CutterCore::seekChanged, this, &PseudocodeWidget::seekChanged);
|
2019-08-07 11:38:22 +00:00
|
|
|
ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(ui->textEdit, SIGNAL(customContextMenuRequested(const QPoint &)),
|
|
|
|
this, SLOT(showDisasContextMenu(const QPoint &)));
|
|
|
|
|
|
|
|
// refresh the widget when an action in this menu is triggered
|
|
|
|
connect(mCtxMenu, &QMenu::triggered, this, &PseudocodeWidget::refreshPseudocode);
|
|
|
|
addActions(mCtxMenu->actions());
|
2019-07-12 08:57:07 +00:00
|
|
|
|
2019-08-28 17:01:12 +00:00
|
|
|
ui->progressLabel->setVisible(false);
|
2019-01-12 17:02:51 +00:00
|
|
|
doRefresh(RVA_INVALID);
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 15:41:08 +00:00
|
|
|
PseudocodeWidget::~PseudocodeWidget() = default;
|
2017-12-06 23:19:14 +00:00
|
|
|
|
|
|
|
|
2019-01-12 17:02:51 +00:00
|
|
|
void PseudocodeWidget::doRefresh(RVA addr)
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
2019-07-15 12:08:44 +00:00
|
|
|
if (ui->decompilerComboBox->currentIndex() < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decompiler *dec = Core()->getDecompilerById(ui->decompilerComboBox->currentData().toString());
|
2019-08-28 17:01:12 +00:00
|
|
|
if (!dec || dec->isRunning()) {
|
2019-07-15 12:08:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
if (addr == RVA_INVALID) {
|
2019-07-12 08:57:07 +00:00
|
|
|
ui->textEdit->setPlainText(tr("Click Refresh to generate Pseudocode from current offset."));
|
2017-12-21 14:23:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:01:12 +00:00
|
|
|
dec->decompileAt(addr);
|
|
|
|
if (dec->isRunning()) {
|
|
|
|
ui->progressLabel->setVisible(true);
|
|
|
|
ui->decompilerComboBox->setEnabled(false);
|
|
|
|
if (dec->isCancelable()) {
|
|
|
|
ui->refreshButton->setText(tr("Cancel"));
|
|
|
|
} else {
|
|
|
|
ui->refreshButton->setEnabled(false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudocodeWidget::refreshPseudocode()
|
|
|
|
{
|
|
|
|
doRefresh(Core()->getOffset());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudocodeWidget::decompilationFinished(AnnotatedCode code)
|
|
|
|
{
|
|
|
|
ui->progressLabel->setVisible(false);
|
|
|
|
ui->decompilerComboBox->setEnabled(decompilerSelectionEnabled);
|
|
|
|
ui->refreshButton->setText(tr("Refresh"));
|
|
|
|
ui->refreshButton->setEnabled(true);
|
|
|
|
|
|
|
|
this->code = code;
|
2019-08-27 15:27:39 +00:00
|
|
|
if (code.code.isEmpty()) {
|
2019-08-28 17:01:12 +00:00
|
|
|
ui->textEdit->setPlainText(tr("Cannot decompile at this address (Not a function?)"));
|
2017-12-15 10:52:47 +00:00
|
|
|
return;
|
2019-07-12 08:57:07 +00:00
|
|
|
} else {
|
|
|
|
connectCursorPositionChanged(true);
|
2019-08-27 15:27:39 +00:00
|
|
|
ui->textEdit->setPlainText(code.code);
|
2019-07-12 08:57:07 +00:00
|
|
|
connectCursorPositionChanged(false);
|
|
|
|
seekChanged();
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 18:33:05 +00:00
|
|
|
void PseudocodeWidget::decompilerSelected()
|
|
|
|
{
|
2019-08-23 20:06:44 +00:00
|
|
|
Config()->setSelectedDecompiler(ui->decompilerComboBox->currentData().toString());
|
2019-07-16 18:33:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:57:07 +00:00
|
|
|
void PseudocodeWidget::connectCursorPositionChanged(bool disconnect)
|
|
|
|
{
|
|
|
|
if (disconnect) {
|
|
|
|
QObject::disconnect(ui->textEdit, &QPlainTextEdit::cursorPositionChanged, this, &PseudocodeWidget::cursorPositionChanged);
|
|
|
|
} else {
|
|
|
|
connect(ui->textEdit, &QPlainTextEdit::cursorPositionChanged, this, &PseudocodeWidget::cursorPositionChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudocodeWidget::cursorPositionChanged()
|
|
|
|
{
|
2019-08-27 15:27:39 +00:00
|
|
|
size_t pos = ui->textEdit->textCursor().position();
|
|
|
|
RVA offset = code.OffsetForPosition(pos);
|
2019-07-12 08:57:07 +00:00
|
|
|
if (offset != RVA_INVALID && offset != Core()->getOffset()) {
|
|
|
|
seekFromCursor = true;
|
|
|
|
Core()->seek(offset);
|
2019-08-07 11:38:22 +00:00
|
|
|
mCtxMenu->setOffset(offset);
|
2019-07-12 08:57:07 +00:00
|
|
|
seekFromCursor = false;
|
|
|
|
}
|
|
|
|
updateSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudocodeWidget::seekChanged()
|
|
|
|
{
|
|
|
|
if (seekFromCursor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateCursorPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudocodeWidget::updateCursorPosition()
|
|
|
|
{
|
|
|
|
RVA offset = Core()->getOffset();
|
2019-08-27 15:27:39 +00:00
|
|
|
size_t pos = code.PositionForOffset(offset);
|
|
|
|
if (pos == SIZE_MAX) {
|
|
|
|
return;
|
2019-07-12 08:57:07 +00:00
|
|
|
}
|
2019-08-27 15:27:39 +00:00
|
|
|
connectCursorPositionChanged(true);
|
|
|
|
QTextCursor cursor = ui->textEdit->textCursor();
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
ui->textEdit->setTextCursor(cursor);
|
|
|
|
updateSelection();
|
2019-07-12 08:57:07 +00:00
|
|
|
connectCursorPositionChanged(false);
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:19:14 +00:00
|
|
|
void PseudocodeWidget::setupFonts()
|
|
|
|
{
|
|
|
|
QFont font = Config()->getFont();
|
2017-12-21 14:23:44 +00:00
|
|
|
ui->textEdit->setFont(font);
|
2017-12-06 23:19:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 08:57:07 +00:00
|
|
|
void PseudocodeWidget::updateSelection()
|
|
|
|
{
|
|
|
|
QList<QTextEdit::ExtraSelection> extraSelections;
|
|
|
|
|
|
|
|
// Highlight the current line
|
|
|
|
auto 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();
|
|
|
|
extraSelections.append(createSameWordsSelections(ui->textEdit, searchString));
|
|
|
|
|
|
|
|
ui->textEdit->setExtraSelections(extraSelections);
|
2019-08-07 11:38:22 +00:00
|
|
|
mCtxMenu->setCurHighlightedWord(searchString);
|
2019-07-12 08:57:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 13:02:41 +00:00
|
|
|
QString PseudocodeWidget::getWindowTitle() const
|
|
|
|
{
|
|
|
|
return tr("Pseudocode");
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:19:14 +00:00
|
|
|
void PseudocodeWidget::fontsUpdated()
|
|
|
|
{
|
|
|
|
setupFonts();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PseudocodeWidget::colorsUpdatedSlot()
|
|
|
|
{
|
|
|
|
}
|
2019-08-07 11:38:22 +00:00
|
|
|
|
|
|
|
void PseudocodeWidget::showDisasContextMenu(const QPoint &pt)
|
|
|
|
{
|
|
|
|
mCtxMenu->exec(ui->textEdit->mapToGlobal(pt));
|
|
|
|
doRefresh(Core()->getOffset());
|
|
|
|
}
|