2017-12-08 15:00:52 +00:00
|
|
|
#include "VisualNavbar.h"
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/MainWindow.h"
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/TempConfig.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-04-13 15:14:02 +00:00
|
|
|
#include <QGraphicsView>
|
2017-03-29 10:18:37 +00:00
|
|
|
#include <QComboBox>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QGraphicsRectItem>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonParseError>
|
2017-12-09 23:22:16 +00:00
|
|
|
#include <QToolTip>
|
2019-03-14 09:28:42 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cmath>
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
VisualNavbar::VisualNavbar(MainWindow *main, QWidget *parent) :
|
2017-10-01 18:08:12 +00:00
|
|
|
QToolBar(main),
|
2017-12-08 15:00:52 +00:00
|
|
|
graphicsView(new QGraphicsView),
|
2019-01-12 09:02:07 +00:00
|
|
|
seekGraphicsItem(nullptr),
|
|
|
|
PCGraphicsItem(nullptr),
|
2017-10-01 18:08:12 +00:00
|
|
|
main(main)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(parent);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
setObjectName("visualNavbar");
|
|
|
|
setWindowTitle(tr("Visual navigation bar"));
|
2017-04-09 19:55:06 +00:00
|
|
|
// setMovable(false);
|
2017-03-29 10:18:37 +00:00
|
|
|
setContentsMargins(0, 0, 0, 0);
|
2017-04-07 15:34:24 +00:00
|
|
|
// If line below is used, with the dark theme the paintEvent is not called
|
|
|
|
// and the result is wrong. Something to do with overwriting the style sheet :/
|
|
|
|
//setStyleSheet("QToolBar { border: 0px; border-bottom: 0px; border-top: 0px; border-width: 0px;}");
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
QComboBox *addsCombo = new QComboBox();
|
|
|
|
addsCombo->addItem("");
|
|
|
|
addsCombo->addItem("Entry points");
|
|
|
|
addsCombo->addItem("Marks");
|
|
|
|
*/
|
2017-12-08 15:00:52 +00:00
|
|
|
addWidget(this->graphicsView);
|
2017-03-29 10:18:37 +00:00
|
|
|
//addWidget(addsCombo);
|
2017-11-19 12:56:10 +00:00
|
|
|
|
2017-12-07 23:41:15 +00:00
|
|
|
connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA)));
|
2019-01-12 09:02:07 +00:00
|
|
|
connect(Core(), SIGNAL(registersChanged()), this, SLOT(drawPCCursor()));
|
2017-12-07 23:41:15 +00:00
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fetchAndPaintData()));
|
2018-06-29 12:35:02 +00:00
|
|
|
connect(Core(), SIGNAL(functionsChanged()), this, SLOT(fetchAndPaintData()));
|
|
|
|
connect(Core(), SIGNAL(flagsChanged()), this, SLOT(fetchAndPaintData()));
|
2017-12-08 09:55:47 +00:00
|
|
|
|
|
|
|
graphicsScene = new QGraphicsScene(this);
|
2017-12-08 15:00:52 +00:00
|
|
|
|
|
|
|
const QBrush bg = QBrush(QColor(74, 74, 74));
|
|
|
|
|
|
|
|
graphicsScene->setBackgroundBrush(bg);
|
|
|
|
|
|
|
|
this->graphicsView->setAlignment(Qt::AlignLeft);
|
2018-10-04 13:37:12 +00:00
|
|
|
this->graphicsView->setMinimumHeight(15);
|
|
|
|
this->graphicsView->setMaximumHeight(15);
|
2017-12-08 15:00:52 +00:00
|
|
|
this->graphicsView->setFrameShape(QFrame::NoFrame);
|
|
|
|
this->graphicsView->setRenderHints(0);
|
|
|
|
this->graphicsView->setScene(graphicsScene);
|
|
|
|
this->graphicsView->setRenderHints(QPainter::Antialiasing);
|
2017-12-09 23:22:16 +00:00
|
|
|
this->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
this->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
// So the graphicsView doesn't intercept mouse events.
|
|
|
|
this->graphicsView->setEnabled(false);
|
|
|
|
this->graphicsView->setMouseTracking(true);
|
|
|
|
setMouseTracking(true);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:00:26 +00:00
|
|
|
unsigned int nextPow2(unsigned int n)
|
|
|
|
{
|
|
|
|
unsigned int b = 0;
|
|
|
|
while (n) {
|
|
|
|
n >>= 1;
|
|
|
|
b++;
|
|
|
|
}
|
|
|
|
return (1u << b);
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::paintEvent(QPaintEvent *event)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(event);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
|
|
|
QPainter painter(this);
|
2018-07-06 16:00:26 +00:00
|
|
|
|
|
|
|
auto w = static_cast<unsigned int>(width());
|
|
|
|
bool fetch = false;
|
|
|
|
if (statsWidth < w) {
|
|
|
|
statsWidth = nextPow2(w);
|
|
|
|
fetch = true;
|
2018-09-30 20:00:53 +00:00
|
|
|
} else if (statsWidth > w * 4) {
|
2018-07-06 16:00:26 +00:00
|
|
|
statsWidth = statsWidth > 0 ? statsWidth / 2 : 0;
|
|
|
|
fetch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetch) {
|
2018-07-01 12:29:01 +00:00
|
|
|
fetchAndPaintData();
|
2018-07-06 16:00:26 +00:00
|
|
|
} else if (previousWidth != w) {
|
|
|
|
this->previousWidth = w;
|
|
|
|
updateGraphicsScene();
|
2017-12-08 09:55:47 +00:00
|
|
|
}
|
2017-04-09 17:09:35 +00:00
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::fetchAndPaintData()
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
2018-07-06 16:00:26 +00:00
|
|
|
fetchStats();
|
|
|
|
updateGraphicsScene();
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:00:26 +00:00
|
|
|
void VisualNavbar::fetchStats()
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
2018-07-06 16:00:26 +00:00
|
|
|
stats = Core()->getBlockStatistics(statsWidth);
|
2017-12-09 23:22:16 +00:00
|
|
|
}
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2018-09-30 20:00:53 +00:00
|
|
|
enum class DataType : int { Empty, Code, String, Symbol, Count };
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2018-07-06 16:00:26 +00:00
|
|
|
void VisualNavbar::updateGraphicsScene()
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2018-06-29 12:35:02 +00:00
|
|
|
graphicsScene->clear();
|
|
|
|
xToAddress.clear();
|
2019-01-12 09:02:07 +00:00
|
|
|
seekGraphicsItem = nullptr;
|
|
|
|
PCGraphicsItem = nullptr;
|
2018-06-29 12:35:02 +00:00
|
|
|
graphicsScene->setBackgroundBrush(QBrush(Config()->getColor("gui.navbar.empty")));
|
2017-12-10 18:12:22 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
if (stats.to <= stats.from) {
|
|
|
|
return;
|
2017-12-09 23:22:16 +00:00
|
|
|
}
|
2017-11-19 12:56:10 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
int w = graphicsView->width();
|
|
|
|
int h = graphicsView->height();
|
2017-12-11 13:07:12 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
RVA totalSize = stats.to - stats.from;
|
2018-07-06 15:32:35 +00:00
|
|
|
RVA beginAddr = stats.from;
|
2018-07-01 12:29:01 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
double widthPerByte = (double)w / (double)totalSize;
|
2018-07-06 15:32:35 +00:00
|
|
|
auto xFromAddr = [widthPerByte, beginAddr] (RVA addr) -> double {
|
|
|
|
return (addr - beginAddr) * widthPerByte;
|
2018-06-29 12:35:02 +00:00
|
|
|
};
|
2017-12-11 13:07:12 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
std::array<QBrush, static_cast<int>(DataType::Count)> dataTypeBrushes;
|
|
|
|
dataTypeBrushes[static_cast<int>(DataType::Code)] = QBrush(Config()->getColor("gui.navbar.code"));
|
|
|
|
dataTypeBrushes[static_cast<int>(DataType::String)] = QBrush(Config()->getColor("gui.navbar.str"));
|
|
|
|
dataTypeBrushes[static_cast<int>(DataType::Symbol)] = QBrush(Config()->getColor("gui.navbar.sym"));
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
DataType lastDataType = DataType::Empty;
|
|
|
|
QGraphicsRectItem *dataItem = nullptr;
|
|
|
|
QRectF dataItemRect(0.0, 0.0, 0.0, h);
|
|
|
|
for (const BlockDescription &block : stats.blocks) {
|
|
|
|
// Keep track of where which memory segment is mapped so we are able to convert from
|
|
|
|
// address to X coordinate and vice versa.
|
|
|
|
XToAddress x2a;
|
|
|
|
x2a.x_start = xFromAddr(block.addr);
|
|
|
|
x2a.x_end = xFromAddr(block.addr + block.size);
|
|
|
|
x2a.address_from = block.addr;
|
|
|
|
x2a.address_to = block.addr + block.size;
|
|
|
|
xToAddress.append(x2a);
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
DataType dataType;
|
2018-07-06 15:32:35 +00:00
|
|
|
if (block.functions > 0) {
|
2018-06-29 12:35:02 +00:00
|
|
|
dataType = DataType::Code;
|
|
|
|
} else if (block.strings > 0) {
|
|
|
|
dataType = DataType::String;
|
|
|
|
} else if (block.symbols > 0) {
|
|
|
|
dataType = DataType::Symbol;
|
2018-07-06 15:32:35 +00:00
|
|
|
} else if (block.inFunctions > 0) {
|
|
|
|
dataType = DataType::Code;
|
2018-06-29 12:35:02 +00:00
|
|
|
} else {
|
2018-07-01 12:29:01 +00:00
|
|
|
lastDataType = DataType::Empty;
|
2018-06-29 12:35:02 +00:00
|
|
|
continue;
|
2017-12-08 09:55:47 +00:00
|
|
|
}
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
if (dataType == lastDataType) {
|
2018-07-01 12:29:01 +00:00
|
|
|
double r = xFromAddr(block.addr + block.size);
|
|
|
|
if (r > dataItemRect.right()) {
|
|
|
|
dataItemRect.setRight(r);
|
|
|
|
dataItem->setRect(dataItemRect);
|
|
|
|
}
|
2018-06-29 12:35:02 +00:00
|
|
|
dataItem->setRect(dataItemRect);
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
dataItemRect.setX(xFromAddr(block.addr));
|
|
|
|
dataItemRect.setRight(xFromAddr(block.addr + block.size));
|
2017-12-09 23:22:16 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
dataItem = new QGraphicsRectItem();
|
|
|
|
dataItem->setPen(Qt::NoPen);
|
|
|
|
dataItem->setBrush(dataTypeBrushes[static_cast<int>(dataType)]);
|
|
|
|
graphicsScene->addItem(dataItem);
|
2017-12-09 23:22:16 +00:00
|
|
|
|
2018-06-29 12:35:02 +00:00
|
|
|
lastDataType = dataType;
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
2017-12-09 23:22:16 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
// Update scene width
|
2018-07-01 12:29:01 +00:00
|
|
|
graphicsScene->setSceneRect(0, 0, w, h);
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2019-01-12 09:02:07 +00:00
|
|
|
drawSeekCursor();
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 09:02:07 +00:00
|
|
|
void VisualNavbar::drawCursor(RVA addr, QColor color, QGraphicsRectItem *&graphicsItem)
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
2019-01-12 09:02:07 +00:00
|
|
|
double cursor_x = addressToLocalX(addr);
|
|
|
|
if (graphicsItem != nullptr) {
|
|
|
|
graphicsScene->removeItem(graphicsItem);
|
|
|
|
delete graphicsItem;
|
|
|
|
graphicsItem = nullptr;
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
2018-03-21 20:32:32 +00:00
|
|
|
if (std::isnan(cursor_x)) {
|
2017-12-08 12:21:24 +00:00
|
|
|
return;
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
2017-12-08 15:00:52 +00:00
|
|
|
int h = this->graphicsView->height();
|
2019-01-12 09:02:07 +00:00
|
|
|
graphicsItem = new QGraphicsRectItem(cursor_x, 0, 2, h);
|
|
|
|
graphicsItem->setPen(Qt::NoPen);
|
|
|
|
graphicsItem->setBrush(QBrush(color));
|
|
|
|
graphicsScene->addItem(graphicsItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualNavbar::drawPCCursor()
|
|
|
|
{
|
|
|
|
drawCursor(Core()->getProgramCounterValue(), Config()->getColor("gui.navbar.pc"), PCGraphicsItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisualNavbar::drawSeekCursor()
|
|
|
|
{
|
2019-03-07 05:26:26 +00:00
|
|
|
drawCursor(Core()->getOffset(), Config()->getColor("gui.navbar.seek"), seekGraphicsItem);
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::on_seekChanged(RVA addr)
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(addr);
|
2018-02-01 16:06:30 +00:00
|
|
|
// Update cursor
|
2019-01-12 09:02:07 +00:00
|
|
|
this->drawSeekCursor();
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::mousePressEvent(QMouseEvent *event)
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
qreal x = event->localPos().x();
|
2017-12-08 12:21:24 +00:00
|
|
|
RVA address = localXToAddress(x);
|
2018-03-21 20:32:32 +00:00
|
|
|
if (address != RVA_INVALID) {
|
2017-12-09 23:22:16 +00:00
|
|
|
QToolTip::showText(event->globalPos(), toolTipForAddress(address), this);
|
2018-03-21 20:32:32 +00:00
|
|
|
if (event->buttons() & Qt::LeftButton) {
|
2017-12-09 23:22:16 +00:00
|
|
|
event->accept();
|
|
|
|
Core()->seek(address);
|
|
|
|
}
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
2017-12-09 23:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisualNavbar::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
mousePressEvent(event);
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
RVA VisualNavbar::localXToAddress(double x)
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const XToAddress &x2a : xToAddress) {
|
2018-03-21 20:32:32 +00:00
|
|
|
if ((x2a.x_start <= x) && (x <= x2a.x_end)) {
|
2017-12-07 23:41:15 +00:00
|
|
|
double offset = (x - x2a.x_start) / (x2a.x_end - x2a.x_start);
|
|
|
|
double size = x2a.address_to - x2a.address_from;
|
2017-12-08 12:21:24 +00:00
|
|
|
return x2a.address_from + (offset * size);
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 15:00:52 +00:00
|
|
|
return RVA_INVALID;
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
double VisualNavbar::addressToLocalX(RVA address)
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const XToAddress &x2a : xToAddress) {
|
2018-03-21 20:32:32 +00:00
|
|
|
if ((x2a.address_from <= address) && (address < x2a.address_to)) {
|
2017-12-08 12:21:24 +00:00
|
|
|
double offset = (double)(address - x2a.address_from) / (double)(x2a.address_to - x2a.address_from);
|
|
|
|
double size = x2a.x_end - x2a.x_start;
|
|
|
|
return x2a.x_start + (offset * size);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 12:21:24 +00:00
|
|
|
return nan("");
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2017-12-09 23:22:16 +00:00
|
|
|
|
|
|
|
QList<QString> VisualNavbar::sectionsForAddress(RVA address)
|
|
|
|
{
|
|
|
|
QList<QString> ret;
|
2018-06-29 12:35:02 +00:00
|
|
|
QList<SectionDescription> sections = Core()->getAllSections();
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const SectionDescription §ion : sections) {
|
2018-06-29 12:35:02 +00:00
|
|
|
if (address >= section.vaddr && address < section.vaddr + section.vsize) {
|
|
|
|
ret << section.name;
|
2017-12-09 23:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString VisualNavbar::toolTipForAddress(RVA address)
|
|
|
|
{
|
2017-12-10 18:12:22 +00:00
|
|
|
QString ret = "Address: " + RAddressString(address);
|
2017-12-09 23:22:16 +00:00
|
|
|
auto sections = sectionsForAddress(address);
|
2018-03-21 20:32:32 +00:00
|
|
|
if (sections.count()) {
|
2017-12-10 18:12:22 +00:00
|
|
|
ret += "\nSections: \n";
|
2018-07-06 15:37:27 +00:00
|
|
|
bool first = true;
|
2018-11-26 22:34:34 +00:00
|
|
|
for (const QString §ion : sections) {
|
2018-07-06 15:37:27 +00:00
|
|
|
if (!first) {
|
2019-03-23 10:54:34 +00:00
|
|
|
ret.append(QLatin1Char('\n'));
|
2018-07-06 15:37:27 +00:00
|
|
|
} else {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
ret += " " + section;
|
2017-12-10 18:12:22 +00:00
|
|
|
}
|
2017-12-09 23:22:16 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|