2017-10-09 09:38:57 +00:00
|
|
|
#include "DisassemblerGraphView.h"
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPropertyAnimation>
|
|
|
|
#include <QShortcut>
|
2017-12-19 16:59:39 +00:00
|
|
|
#include <QToolTip>
|
2018-02-01 14:15:16 +00:00
|
|
|
#include <QTextDocument>
|
2018-02-14 09:33:34 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFile>
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
#include "cutter.h"
|
|
|
|
#include "utils/Colors.h"
|
|
|
|
#include "utils/Configuration.h"
|
|
|
|
#include "utils/CachedFontMetrics.h"
|
2018-02-01 14:15:16 +00:00
|
|
|
#include "utils/TempConfig.h"
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblerGraphView::DisassemblerGraphView(QWidget *parent)
|
|
|
|
: GraphView(parent),
|
|
|
|
mFontMetrics(nullptr),
|
|
|
|
mMenu(new DisassemblyContextMenu(this))
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
highlight_token = nullptr;
|
2017-12-13 22:38:46 +00:00
|
|
|
// Signals that require a refresh all
|
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(refreshView()));
|
|
|
|
connect(Core(), SIGNAL(commentsChanged()), this, SLOT(refreshView()));
|
2017-12-20 22:50:26 +00:00
|
|
|
connect(Core(), SIGNAL(functionRenamed(const QString &, const QString &)), this, SLOT(refreshView()));
|
2017-12-13 22:38:46 +00:00
|
|
|
connect(Core(), SIGNAL(flagsChanged()), this, SLOT(refreshView()));
|
|
|
|
connect(Core(), SIGNAL(varsChanged()), this, SLOT(refreshView()));
|
|
|
|
connect(Core(), SIGNAL(instructionChanged(RVA)), this, SLOT(refreshView()));
|
2017-12-14 21:54:57 +00:00
|
|
|
connect(Core(), SIGNAL(functionsChanged()), this, SLOT(refreshView()));
|
2017-12-19 16:13:44 +00:00
|
|
|
connect(Core(), SIGNAL(graphOptionsChanged()), this, SLOT(refreshView()));
|
2018-01-31 09:17:06 +00:00
|
|
|
connect(Core(), SIGNAL(asmOptionsChanged()), this, SLOT(refreshView()));
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(colorsUpdatedSlot()));
|
|
|
|
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(fontsUpdatedSlot()));
|
|
|
|
connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(onSeekChanged(RVA)));
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
// Space to switch to disassembly
|
2017-12-14 21:07:48 +00:00
|
|
|
QShortcut *shortcut_disassembly = new QShortcut(QKeySequence(Qt::Key_Space), this);
|
|
|
|
shortcut_disassembly->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_disassembly, &QShortcut::activated, this, []{
|
2017-12-13 22:38:46 +00:00
|
|
|
Core()->setMemoryWidgetPriority(CutterCore::MemoryWidgetType::Disassembly);
|
|
|
|
Core()->triggerRaisePrioritizedMemoryWidget();
|
|
|
|
});
|
|
|
|
// ESC for previous
|
|
|
|
QShortcut *shortcut_escape = new QShortcut(QKeySequence(Qt::Key_Escape), this);
|
|
|
|
shortcut_escape->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_escape, SIGNAL(activated()), this, SLOT(seekPrev()));
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:57:36 +00:00
|
|
|
// Zoom shortcuts
|
2017-12-13 22:38:46 +00:00
|
|
|
QShortcut *shortcut_zoom_in = new QShortcut(QKeySequence(Qt::Key_Plus), this);
|
|
|
|
shortcut_zoom_in->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_zoom_in, SIGNAL(activated()), this, SLOT(zoomIn()));
|
|
|
|
QShortcut *shortcut_zoom_out = new QShortcut(QKeySequence(Qt::Key_Minus), this);
|
2017-12-13 22:57:36 +00:00
|
|
|
shortcut_zoom_out->setContext(Qt::WidgetShortcut);
|
2017-12-13 22:38:46 +00:00
|
|
|
connect(shortcut_zoom_out, SIGNAL(activated()), this, SLOT(zoomOut()));
|
2017-12-13 22:57:36 +00:00
|
|
|
QShortcut *shortcut_zoom_reset = new QShortcut(QKeySequence(Qt::Key_Equal), this);
|
|
|
|
shortcut_zoom_reset->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_zoom_reset, SIGNAL(activated()), this, SLOT(zoomReset()));
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:57:36 +00:00
|
|
|
// Branch shortcuts
|
2017-12-13 22:38:46 +00:00
|
|
|
QShortcut *shortcut_take_true = new QShortcut(QKeySequence(Qt::Key_T), this);
|
|
|
|
shortcut_take_true->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_take_true, SIGNAL(activated()), this, SLOT(takeTrue()));
|
|
|
|
QShortcut *shortcut_take_false = new QShortcut(QKeySequence(Qt::Key_F), this);
|
|
|
|
shortcut_take_false->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_take_false, SIGNAL(activated()), this, SLOT(takeFalse()));
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
// Navigation shortcuts
|
|
|
|
QShortcut *shortcut_next_instr = new QShortcut(QKeySequence(Qt::Key_J), this);
|
|
|
|
shortcut_next_instr->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_next_instr, SIGNAL(activated()), this, SLOT(nextInstr()));
|
|
|
|
QShortcut *shortcut_prev_instr = new QShortcut(QKeySequence(Qt::Key_K), this);
|
|
|
|
shortcut_prev_instr->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_prev_instr, SIGNAL(activated()), this, SLOT(prevInstr()));
|
2018-01-13 13:49:20 +00:00
|
|
|
QShortcut *shortcut_next_instr_arrow = new QShortcut(QKeySequence::MoveToNextLine, this);
|
|
|
|
shortcut_next_instr_arrow->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_next_instr_arrow, SIGNAL(activated()), this, SLOT(nextInstr()));
|
|
|
|
QShortcut *shortcut_prev_instr_arrow = new QShortcut(QKeySequence::MoveToPreviousLine, this);
|
|
|
|
shortcut_prev_instr_arrow->setContext(Qt::WidgetShortcut);
|
|
|
|
connect(shortcut_prev_instr_arrow, SIGNAL(activated()), this, SLOT(prevInstr()));
|
2017-12-14 21:07:48 +00:00
|
|
|
shortcuts.append(shortcut_disassembly);
|
|
|
|
shortcuts.append(shortcut_escape);
|
|
|
|
shortcuts.append(shortcut_zoom_in);
|
|
|
|
shortcuts.append(shortcut_zoom_out);
|
|
|
|
shortcuts.append(shortcut_zoom_reset);
|
|
|
|
shortcuts.append(shortcut_next_instr);
|
|
|
|
shortcuts.append(shortcut_prev_instr);
|
2018-01-13 13:49:20 +00:00
|
|
|
shortcuts.append(shortcut_next_instr_arrow);
|
|
|
|
shortcuts.append(shortcut_prev_instr_arrow);
|
2017-12-14 21:07:48 +00:00
|
|
|
|
2018-02-14 09:33:34 +00:00
|
|
|
//Export Graph menu
|
|
|
|
mMenu->addSeparator();
|
|
|
|
actionExportGraph.setText(tr("Export Graph"));
|
|
|
|
mMenu->addAction(&actionExportGraph);
|
|
|
|
connect(&actionExportGraph, SIGNAL(triggered(bool)), this, SLOT(on_actionExportGraph_triggered()));
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
initFont();
|
|
|
|
colorsUpdatedSlot();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
DisassemblerGraphView::~DisassemblerGraphView()
|
|
|
|
{
|
|
|
|
for(QShortcut *shortcut : shortcuts)
|
|
|
|
{
|
|
|
|
delete shortcut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::refreshView()
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
initFont();
|
2017-10-09 09:38:57 +00:00
|
|
|
loadCurrentGraph();
|
2017-12-14 21:07:48 +00:00
|
|
|
viewport()->update();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerGraphView::loadCurrentGraph()
|
|
|
|
{
|
2018-02-01 14:15:16 +00:00
|
|
|
TempConfig tempConfig;
|
2018-02-01 14:51:03 +00:00
|
|
|
tempConfig.set("scr.html", true)
|
2018-02-01 16:31:17 +00:00
|
|
|
.set("scr.color", true)
|
2018-02-04 19:34:52 +00:00
|
|
|
.set("asm.bbline", false)
|
|
|
|
.set("asm.lines", false)
|
|
|
|
.set("asm.fcnlines", false);
|
2018-02-01 14:51:03 +00:00
|
|
|
QJsonDocument functionsDoc = Core()->cmdj("agJ");
|
2017-10-09 09:38:57 +00:00
|
|
|
QJsonArray functions = functionsDoc.array();
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
disassembly_blocks.clear();
|
2017-12-14 21:07:48 +00:00
|
|
|
blocks.clear();
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2017-10-09 09:38:57 +00:00
|
|
|
Analysis anal;
|
|
|
|
anal.ready = true;
|
|
|
|
|
|
|
|
QJsonValue funcRef = functions.first();
|
|
|
|
QJsonObject func = funcRef.toObject();
|
|
|
|
Function f;
|
|
|
|
f.ready = true;
|
2017-12-07 23:37:48 +00:00
|
|
|
f.entry = func["offset"].toVariant().toULongLong();
|
2017-11-21 17:48:01 +00:00
|
|
|
|
|
|
|
QString windowTitle = tr("Graph");
|
|
|
|
QString funcName = func["name"].toString().trimmed();
|
|
|
|
if (!funcName.isEmpty())
|
|
|
|
{
|
|
|
|
windowTitle += " (" + funcName + ")";
|
|
|
|
}
|
2017-12-14 21:07:48 +00:00
|
|
|
parentWidget()->setWindowTitle(windowTitle);
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
RVA entry = func["offset"].toVariant().toULongLong();
|
|
|
|
|
|
|
|
setEntry(entry);
|
2017-10-09 09:38:57 +00:00
|
|
|
for (QJsonValueRef blockRef : func["blocks"].toArray()) {
|
|
|
|
QJsonObject block = blockRef.toObject();
|
2017-12-13 22:38:46 +00:00
|
|
|
RVA block_entry = block["offset"].toVariant().toULongLong();
|
2018-02-01 14:15:16 +00:00
|
|
|
RVA block_size = block["size"].toVariant().toULongLong();
|
2017-12-13 22:38:46 +00:00
|
|
|
RVA block_fail = block["fail"].toVariant().toULongLong();
|
|
|
|
RVA block_jump = block["jump"].toVariant().toULongLong();
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock db;
|
|
|
|
GraphBlock gb;
|
|
|
|
gb.entry = block_entry;
|
|
|
|
db.entry = block_entry;
|
|
|
|
db.true_path = RVA_INVALID;
|
|
|
|
db.false_path = RVA_INVALID;
|
|
|
|
if(block_fail)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
db.false_path = block_fail;
|
|
|
|
gb.exits.push_back(block_fail);
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
if(block_jump)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
if(block_fail)
|
|
|
|
{
|
|
|
|
db.true_path = block_jump;
|
2017-10-12 12:14:33 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
gb.exits.push_back(block_jump);
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2018-02-01 14:15:16 +00:00
|
|
|
QJsonArray opArray = block["ops"].toArray();
|
|
|
|
for (int opIndex=0; opIndex<opArray.size(); opIndex++)
|
2018-01-31 09:17:06 +00:00
|
|
|
{
|
2018-02-01 14:15:16 +00:00
|
|
|
QJsonObject op = opArray[opIndex].toObject();
|
2017-10-09 09:38:57 +00:00
|
|
|
Instr i;
|
2017-12-07 23:37:48 +00:00
|
|
|
i.addr = op["offset"].toVariant().toULongLong();
|
2018-01-31 09:17:06 +00:00
|
|
|
|
2018-02-01 14:15:16 +00:00
|
|
|
if (opIndex < opArray.size() - 1)
|
|
|
|
{
|
|
|
|
// get instruction size from distance to next instruction ...
|
|
|
|
RVA nextOffset = opArray[opIndex+1].toObject()["offset"].toVariant().toULongLong();
|
|
|
|
i.size = nextOffset - i.addr;
|
|
|
|
}
|
2018-01-31 09:59:01 +00:00
|
|
|
else
|
2018-01-31 09:17:06 +00:00
|
|
|
{
|
2018-02-01 14:15:16 +00:00
|
|
|
// or to the end of the block.
|
|
|
|
i.size = (block_entry + block_size) - i.addr;
|
2017-10-14 11:00:23 +00:00
|
|
|
}
|
2018-01-31 09:17:06 +00:00
|
|
|
|
2018-02-01 14:15:16 +00:00
|
|
|
// Skip last byte, otherwise it will overlap with next instruction
|
|
|
|
i.size -= 1;
|
2018-01-31 09:17:06 +00:00
|
|
|
|
2018-02-01 14:15:16 +00:00
|
|
|
QString disas;
|
|
|
|
disas = op["text"].toString();
|
2018-01-31 09:17:06 +00:00
|
|
|
|
2018-02-01 14:15:16 +00:00
|
|
|
QTextDocument textDoc;
|
|
|
|
textDoc.setHtml(disas);
|
2018-01-31 09:17:06 +00:00
|
|
|
|
2018-02-01 14:51:03 +00:00
|
|
|
RichTextPainter::List richText = RichTextPainter::fromTextDocument(textDoc);
|
|
|
|
//Colors::colorizeAssembly(richText, textDoc.toPlainText(), 0);
|
2018-01-31 09:17:06 +00:00
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
bool cropped;
|
2018-01-31 09:59:01 +00:00
|
|
|
int blockLength = Config()->getGraphBlockMaxChars() + Core()->getConfigb("asm.bytes") * 24 + Core()->getConfigb("asm.emu") * 10;
|
|
|
|
i.text = Text(RichTextPainter::cropped(richText, blockLength, "...", &cropped));
|
2017-12-19 16:59:39 +00:00
|
|
|
if(cropped)
|
|
|
|
i.fullText = richText;
|
|
|
|
else
|
|
|
|
i.fullText = Text();
|
2017-12-13 22:38:46 +00:00
|
|
|
db.instrs.push_back(i);
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
disassembly_blocks[db.entry] = db;
|
|
|
|
prepareGraphNode(gb);
|
|
|
|
f.blocks.push_back(db);
|
|
|
|
|
|
|
|
addBlock(gb);
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
|
2017-10-09 09:38:57 +00:00
|
|
|
anal.functions[f.entry] = f;
|
|
|
|
anal.status = "Ready.";
|
|
|
|
anal.entry = f.entry;
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
if(func["blocks"].toArray().size() > 0)
|
|
|
|
{
|
|
|
|
computeGraph(entry);
|
2017-12-14 21:07:48 +00:00
|
|
|
viewport()->update();
|
|
|
|
|
|
|
|
if(first_draw)
|
|
|
|
{
|
|
|
|
showBlock(blocks[entry]);
|
|
|
|
first_draw = false;
|
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::prepareGraphNode(GraphBlock &block)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock &db = disassembly_blocks[block.entry];
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
for(auto & line : db.header_text.lines)
|
|
|
|
{
|
|
|
|
int lw = 0;
|
|
|
|
for(auto & part : line)
|
|
|
|
lw += mFontMetrics->width(part.text);
|
|
|
|
if(lw > width)
|
|
|
|
width = lw;
|
|
|
|
height += 1;
|
|
|
|
}
|
|
|
|
for(Instr & instr : db.instrs)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
for(auto & line : instr.text.lines)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
int lw = 0;
|
|
|
|
for(auto & part : line)
|
|
|
|
lw += mFontMetrics->width(part.text);
|
|
|
|
if(lw > width)
|
|
|
|
width = lw;
|
|
|
|
height += 1;
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 21:07:48 +00:00
|
|
|
int extra = 4 * charWidth + 4;
|
|
|
|
block.width = width + extra + charWidth;
|
|
|
|
block.height = (height * charHeight) + extra;
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::initFont()
|
2017-11-05 15:08:17 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
setFont(Config()->getFont());
|
2017-12-14 21:07:48 +00:00
|
|
|
QFontMetricsF metrics(font());
|
|
|
|
baseline = int(metrics.ascent());
|
|
|
|
charWidth = metrics.width('X');
|
|
|
|
charHeight = metrics.height();
|
|
|
|
charOffset = 0;
|
2017-12-13 22:38:46 +00:00
|
|
|
if(mFontMetrics)
|
|
|
|
delete mFontMetrics;
|
|
|
|
mFontMetrics = new CachedFontMetrics(this, font());
|
2017-11-05 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::drawBlock(QPainter & p, GraphView::GraphBlock &block)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
p.setPen(Qt::black);
|
|
|
|
p.setBrush(Qt::gray);
|
|
|
|
p.drawRect(block.x, block.y, block.width, block.height);
|
2017-10-09 09:38:57 +00:00
|
|
|
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
// Render node
|
|
|
|
DisassemblyBlock &db = disassembly_blocks[block.entry];
|
|
|
|
bool block_selected = false;
|
|
|
|
RVA selected_instruction = RVA_INVALID;
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
// Figure out if the current block is selected
|
|
|
|
for(const Instr & instr : db.instrs)
|
|
|
|
{
|
|
|
|
RVA addr = Core()->getOffset();
|
|
|
|
if((instr.addr <= addr) && (addr <= instr.addr+instr.size))
|
|
|
|
{
|
|
|
|
block_selected = true;
|
|
|
|
selected_instruction = instr.addr;
|
|
|
|
}
|
|
|
|
// TODO: L219
|
|
|
|
}
|
2017-10-16 19:00:47 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
p.setPen(QColor(0, 0, 0, 0));
|
|
|
|
if(db.terminal)
|
|
|
|
{
|
|
|
|
p.setBrush(retShadowColor);
|
|
|
|
} else if(db.indirectcall) {
|
|
|
|
p.setBrush(indirectcallShadowColor);
|
|
|
|
} else {
|
|
|
|
p.setBrush(QColor(0, 0, 0, 128));
|
|
|
|
}
|
|
|
|
|
|
|
|
p.drawRect(block.x + 4, block.y + 4,
|
|
|
|
block.width + 4, block.height + 4);
|
|
|
|
p.setPen(graphNodeColor);
|
|
|
|
|
|
|
|
if(block_selected)
|
|
|
|
{
|
|
|
|
p.setBrush(disassemblySelectedBackgroundColor);
|
|
|
|
} else {
|
|
|
|
p.setBrush(disassemblyBackgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.drawRect(block.x, block.y,
|
|
|
|
block.width, block.height);
|
|
|
|
|
|
|
|
// Draw different background for selected instruction
|
|
|
|
if(selected_instruction != RVA_INVALID)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
int y = block.y + (2 * charWidth) + (db.header_text.lines.size() * charHeight);
|
2017-12-13 22:38:46 +00:00
|
|
|
for(Instr & instr : db.instrs)
|
|
|
|
{
|
|
|
|
auto selected = instr.addr == selected_instruction;
|
|
|
|
//auto traceCount = dbgfunctions->GetTraceRecordHitCount(instr.addr);
|
|
|
|
auto traceCount = 0;
|
|
|
|
if(selected && traceCount)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
p.fillRect(QRect(block.x + charWidth, y, block.width - (10 + 2 * charWidth),
|
|
|
|
int(instr.text.lines.size()) * charHeight), disassemblyTracedSelectionColor);
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
else if(selected)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
p.fillRect(QRect(block.x + charWidth, y, block.width - (10 + 2 * charWidth),
|
|
|
|
int(instr.text.lines.size()) * charHeight), disassemblySelectionColor);
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
else if(traceCount)
|
|
|
|
{
|
|
|
|
// Color depending on how often a sequence of code is executed
|
|
|
|
int exponent = 1;
|
|
|
|
while(traceCount >>= 1) //log2(traceCount)
|
|
|
|
exponent++;
|
|
|
|
int colorDiff = (exponent * exponent) / 2;
|
|
|
|
|
|
|
|
// If the user has a light trace background color, substract
|
|
|
|
if(disassemblyTracedColor.blue() > 160)
|
|
|
|
colorDiff *= -1;
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
p.fillRect(QRect(block.x + charWidth, y, block.width - (10 + 2 * charWidth), int(instr.text.lines.size()) * charHeight),
|
2017-12-13 22:38:46 +00:00
|
|
|
QColor(disassemblyTracedColor.red(),
|
|
|
|
disassemblyTracedColor.green(),
|
|
|
|
std::max(0, std::min(256, disassemblyTracedColor.blue() + colorDiff))));
|
|
|
|
}
|
2017-12-14 21:07:48 +00:00
|
|
|
y += int(instr.text.lines.size()) * charHeight;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Render node text
|
2017-12-14 21:07:48 +00:00
|
|
|
auto x = block.x + (2 * charWidth);
|
|
|
|
int y = block.y + (2 * charWidth);
|
2017-12-13 22:38:46 +00:00
|
|
|
for(auto & line : db.header_text.lines)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
RichTextPainter::paintRichText(&p, x, y, block.width, charHeight, 0, line, mFontMetrics);
|
|
|
|
y += charHeight;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
for(Instr & instr : db.instrs)
|
|
|
|
{
|
|
|
|
for(auto & line : instr.text.lines)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
int rectSize = qRound(charWidth);
|
2017-12-13 22:38:46 +00:00
|
|
|
if(rectSize % 2)
|
|
|
|
{
|
|
|
|
rectSize++;
|
|
|
|
}
|
|
|
|
// Assume charWidth <= charHeight
|
2017-12-14 21:07:48 +00:00
|
|
|
QRectF bpRect(x - rectSize / 3.0, y + (charHeight - rectSize) / 2.0, rectSize, rectSize);
|
2017-12-13 22:38:46 +00:00
|
|
|
|
|
|
|
// TODO: Breakpoint/Cip stuff
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
RichTextPainter::paintRichText(&p, x + charWidth, y, block.width - charWidth, charHeight, 0, line, mFontMetrics);
|
|
|
|
y += charHeight;
|
2017-12-13 22:38:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
GraphView::EdgeConfiguration DisassemblerGraphView::edgeConfiguration(GraphView::GraphBlock &from, GraphView::GraphBlock *to)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
EdgeConfiguration ec;
|
|
|
|
DisassemblyBlock &db = disassembly_blocks[from.entry];
|
|
|
|
if(to->entry == db.true_path)
|
|
|
|
{
|
|
|
|
ec.color = brtrueColor;
|
|
|
|
}
|
|
|
|
else if(to->entry == db.false_path)
|
|
|
|
{
|
|
|
|
ec.color = brfalseColor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ec.color = jmpColor;
|
|
|
|
}
|
|
|
|
ec.start_arrow = false;
|
|
|
|
ec.end_arrow = true;
|
|
|
|
return ec;
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
RVA DisassemblerGraphView::getAddrForMouseEvent(GraphBlock &block, QPoint *point)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock &db = disassembly_blocks[block.entry];
|
2017-12-14 21:07:48 +00:00
|
|
|
|
|
|
|
// Remove header and margin
|
|
|
|
int off_y = (2 * charWidth) + (db.header_text.lines.size() * charHeight);
|
|
|
|
// Get mouse coordinate over the actual text
|
|
|
|
int text_point_y = point->y() - off_y;
|
|
|
|
int mouse_row = text_point_y / charHeight;
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
int cur_row = db.header_text.lines.size();
|
|
|
|
if (mouse_row < cur_row)
|
|
|
|
{
|
|
|
|
return db.entry;
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
Instr *instr = getInstrForMouseEvent(block, point);
|
|
|
|
if(instr)
|
|
|
|
{
|
|
|
|
return instr->addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RVA_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DisassemblerGraphView::Instr *DisassemblerGraphView::getInstrForMouseEvent(GraphView::GraphBlock &block, QPoint *point)
|
|
|
|
{
|
|
|
|
DisassemblyBlock &db = disassembly_blocks[block.entry];
|
|
|
|
|
|
|
|
// Remove header and margin
|
|
|
|
int off_y = (2 * charWidth) + (db.header_text.lines.size() * charHeight);
|
|
|
|
// Get mouse coordinate over the actual text
|
|
|
|
int text_point_y = point->y() - off_y;
|
|
|
|
int mouse_row = text_point_y / charHeight;
|
|
|
|
|
|
|
|
int cur_row = db.header_text.lines.size();
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
for(Instr & instr : db.instrs)
|
|
|
|
{
|
|
|
|
if(mouse_row < cur_row + (int)instr.text.lines.size())
|
|
|
|
{
|
2017-12-19 16:59:39 +00:00
|
|
|
return &instr;
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
|
|
|
cur_row += instr.text.lines.size();
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
|
|
|
|
// Public Slots
|
|
|
|
|
2017-10-09 09:38:57 +00:00
|
|
|
void DisassemblerGraphView::colorsUpdatedSlot()
|
|
|
|
{
|
2017-11-20 11:23:37 +00:00
|
|
|
disassemblyBackgroundColor = ConfigColor("gui.alt_background");
|
2017-12-13 22:38:46 +00:00
|
|
|
disassemblySelectedBackgroundColor = ConfigColor("gui.background");
|
2017-10-22 10:21:44 +00:00
|
|
|
mDisabledBreakpointColor = disassemblyBackgroundColor;
|
2017-10-15 07:14:05 +00:00
|
|
|
graphNodeColor = ConfigColor("gui.border");
|
2017-11-20 11:23:37 +00:00
|
|
|
backgroundColor = ConfigColor("gui.background");
|
2017-12-02 15:22:05 +00:00
|
|
|
disassemblySelectionColor = ConfigColor("highlight");
|
2017-10-15 07:14:05 +00:00
|
|
|
|
|
|
|
jmpColor = ConfigColor("graph.trufae");
|
|
|
|
brtrueColor = ConfigColor("graph.true");
|
|
|
|
brfalseColor = ConfigColor("graph.false");
|
2017-11-05 15:08:17 +00:00
|
|
|
|
|
|
|
mCommentColor = ConfigColor("comment");
|
2017-12-13 22:38:46 +00:00
|
|
|
initFont();
|
2017-11-21 14:33:15 +00:00
|
|
|
refreshView();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerGraphView::fontsUpdatedSlot()
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
initFont();
|
2017-12-13 22:38:46 +00:00
|
|
|
refreshView();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblerGraphView::DisassemblyBlock *DisassemblerGraphView::blockForAddress(RVA addr)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
for(auto & blockIt : disassembly_blocks)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock &db = blockIt.second;
|
|
|
|
for(Instr i : db.instrs)
|
|
|
|
{
|
|
|
|
if((i.addr <= addr) && (addr <= i.addr + i.size))
|
|
|
|
{
|
|
|
|
return &db;
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
return nullptr;
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::onSeekChanged(RVA addr)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
mMenu->setOffset(addr);
|
|
|
|
// If this seek was NOT done by us...
|
|
|
|
if(!sent_seek)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock *db = blockForAddress(addr);
|
|
|
|
if(db)
|
|
|
|
{
|
|
|
|
// This is a local address! We animated to it.
|
|
|
|
transition_dont_seek = true;
|
|
|
|
showBlock(&blocks[db->entry], true);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
refreshView();
|
|
|
|
DisassemblyBlock *db = blockForAddress(addr);
|
|
|
|
if(db)
|
|
|
|
{
|
|
|
|
// This is a local address! We animated to it.
|
|
|
|
transition_dont_seek = true;
|
|
|
|
showBlock(&blocks[db->entry], false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
sent_seek = false;
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::zoomIn()
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
current_scale += 0.1;
|
2017-12-14 21:07:48 +00:00
|
|
|
auto areaSize = viewport()->size();
|
|
|
|
adjustSize(areaSize.width(), areaSize.height());
|
|
|
|
viewport()->update();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::zoomOut()
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
current_scale -= 0.1;
|
|
|
|
current_scale = std::max(current_scale, 0.3);
|
2017-12-14 21:07:48 +00:00
|
|
|
auto areaSize = viewport()->size();
|
|
|
|
adjustSize(areaSize.width(), areaSize.height());
|
|
|
|
viewport()->update();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:57:36 +00:00
|
|
|
void DisassemblerGraphView::zoomReset()
|
|
|
|
{
|
|
|
|
current_scale = 1.0;
|
2017-12-14 21:07:48 +00:00
|
|
|
auto areaSize = viewport()->size();
|
|
|
|
adjustSize(areaSize.width(), areaSize.height());
|
|
|
|
viewport()->update();
|
2017-12-13 22:57:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::takeTrue()
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock *db = blockForAddress(Core()->getOffset());
|
|
|
|
if(db->true_path != RVA_INVALID)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
Core()->seek(db->true_path);
|
|
|
|
}
|
|
|
|
else if(blocks[db->entry].exits.size())
|
|
|
|
{
|
|
|
|
Core()->seek(blocks[db->entry].exits[0]);
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::takeFalse()
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
DisassemblyBlock *db = blockForAddress(Core()->getOffset());
|
|
|
|
if(db->false_path != RVA_INVALID)
|
|
|
|
{
|
|
|
|
Core()->seek(db->false_path);
|
|
|
|
}
|
|
|
|
else if(blocks[db->entry].exits.size())
|
|
|
|
{
|
|
|
|
Core()->seek(blocks[db->entry].exits[0]);
|
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
void DisassemblerGraphView::seekInstruction(bool previous_instr)
|
|
|
|
{
|
|
|
|
RVA addr = Core()->getOffset();
|
|
|
|
DisassemblyBlock *db = blockForAddress(addr);
|
|
|
|
if(!db)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i=0; i < db->instrs.size(); i++)
|
|
|
|
{
|
|
|
|
Instr &instr = db->instrs[i];
|
|
|
|
if(!((instr.addr <= addr) && (addr <= instr.addr + instr.size)))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found the instructon. Check if a next one exists
|
|
|
|
if(!previous_instr && (i < db->instrs.size()-1))
|
|
|
|
{
|
|
|
|
seek(db->instrs[i+1].addr, true);
|
|
|
|
}
|
|
|
|
else if(previous_instr && (i > 0))
|
|
|
|
{
|
|
|
|
seek(db->instrs[i-1].addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerGraphView::nextInstr()
|
|
|
|
{
|
|
|
|
seekInstruction(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisassemblerGraphView::prevInstr()
|
|
|
|
{
|
|
|
|
seekInstruction(true);
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::seek(RVA addr, bool update_viewport)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
sent_seek = true;
|
|
|
|
Core()->seek(addr);
|
|
|
|
if(update_viewport)
|
|
|
|
{
|
2017-12-14 21:07:48 +00:00
|
|
|
viewport()->update();
|
2017-12-13 22:38:46 +00:00
|
|
|
}
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::seekPrev()
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
Core()->seekPrev();
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::blockClicked(GraphView::GraphBlock &block, QMouseEvent *event, QPoint pos)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-19 16:59:39 +00:00
|
|
|
RVA instr = getAddrForMouseEvent(block, &pos);
|
2017-12-13 22:38:46 +00:00
|
|
|
if(instr == RVA_INVALID)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
seek(instr, true);
|
2017-10-09 09:38:57 +00:00
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
if(event->button() == Qt::RightButton)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
mMenu->setOffset(instr);
|
|
|
|
mMenu->exec(event->globalPos());
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 21:07:48 +00:00
|
|
|
void DisassemblerGraphView::blockDoubleClicked(GraphView::GraphBlock &block, QMouseEvent *event, QPoint pos)
|
|
|
|
{
|
2017-12-14 21:54:57 +00:00
|
|
|
Q_UNUSED(event);
|
2017-12-19 16:59:39 +00:00
|
|
|
RVA instr = getAddrForMouseEvent(block, &pos);
|
2017-12-14 21:07:48 +00:00
|
|
|
if(instr == RVA_INVALID)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QList<XrefDescription> refs = Core()->getXRefs(instr, false, false);
|
|
|
|
if (refs.length()) {
|
|
|
|
sent_seek = false;
|
|
|
|
Core()->seek(refs.at(0).to);
|
|
|
|
}
|
|
|
|
if (refs.length() > 1) {
|
|
|
|
qWarning() << "Too many references here. Weird behaviour expected.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 16:59:39 +00:00
|
|
|
void DisassemblerGraphView::blockHelpEvent(GraphView::GraphBlock &block, QHelpEvent *event, QPoint pos)
|
|
|
|
{
|
|
|
|
Instr *instr = getInstrForMouseEvent(block, &pos);
|
|
|
|
if(!instr || instr->fullText.lines.empty())
|
|
|
|
{
|
|
|
|
QToolTip::hideText();
|
|
|
|
event->ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QToolTip::showText(event->globalPos(), instr->fullText.ToQString());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DisassemblerGraphView::helpEvent(QHelpEvent *event)
|
|
|
|
{
|
|
|
|
if(!GraphView::helpEvent(event))
|
|
|
|
{
|
|
|
|
QToolTip::hideText();
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-13 22:38:46 +00:00
|
|
|
void DisassemblerGraphView::blockTransitionedTo(GraphView::GraphBlock *to)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
if(transition_dont_seek)
|
2017-10-09 09:38:57 +00:00
|
|
|
{
|
2017-12-13 22:38:46 +00:00
|
|
|
transition_dont_seek = false;
|
|
|
|
return;
|
2017-10-09 09:38:57 +00:00
|
|
|
}
|
2017-12-13 22:38:46 +00:00
|
|
|
seek(to->entry);
|
2017-10-16 19:00:47 +00:00
|
|
|
}
|
2018-02-14 09:33:34 +00:00
|
|
|
|
|
|
|
void DisassemblerGraphView::on_actionExportGraph_triggered()
|
|
|
|
{
|
|
|
|
QString fileName = QFileDialog::getSaveFileName(this,
|
|
|
|
tr("Export Graph"), "", tr("Dot file (*.dot)"));
|
|
|
|
QFile file(fileName);
|
|
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
|
|
|
|
qWarning() << "Can't open file";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTextStream fileOut(&file);
|
|
|
|
fileOut << Core()->cmd("ag -");
|
|
|
|
}
|