- #include "Mainwindow.hxx"
-#include "widgets/BasicBlockWidget.hxx"
+#include "Mainwindow.hxx"
#include "qt.hxx"
#include "disassembler/llvm/LLVMDisassembler.hxx"
#include <iostream>
#include <sstream>
+#include <map>
#include <QtGui>
}
namespace {
- void local__add_basic_block(BasicBlock * block, Disassembler * dis, QGraphicsScene * scene,
- uint64_t starty, uint64_t startx) {
+ BasicBlockWidget *
+ local__add_basic_block(BasicBlock * block, Disassembler * dis,
+ std::map<uint64_t, BasicBlockWidget*>& known_blocks,
+ QGraphicsScene * scene, uint64_t starty, uint64_t startx) {
+
+ decltype(known_blocks.begin()) old;
+ if ((old = known_blocks.find(block->getStartAddress())) != known_blocks.end())
+ return old->second;
+
std::stringstream s;
s << "BLOCK_" << std::hex << block->getStartAddress()
<< "_" << block->getEndAddress();
- BasicBlockWidget * widget = new BasicBlockWidget(s.str().c_str());
+ BasicBlockWidget * widget = new BasicBlockWidget(s.str().c_str(), block);
+
+ known_blocks.insert(std::make_pair(block->getStartAddress(), widget));
+
scene->addItem(widget);
widget->setFlag(QGraphicsItem::ItemIsMovable, true);
widget->moveBy(100*startx, 10*(block->getStartAddress() - starty));
widget->addItem(bytes, byte_count, line.c_str() + 1);
});
- if (block->getNextBlock(0) != 0)
- local__add_basic_block(dis->getBasicBlock(block->getNextBlock(0)), dis, scene, starty, startx+1);
- if (block->getNextBlock(1) != 0)
- local__add_basic_block(dis->getBasicBlock(block->getNextBlock(1)), dis, scene, starty, startx-1);
+ BasicBlockWidget *tmp, *nextl(NULL), *nextr(NULL);
+ BasicBlock * tmpblock;
+ if (block->getNextBlock(0) != 0) {
+ tmpblock = dis->getBasicBlock(block->getNextBlock(0));
+ tmp = local__add_basic_block(tmpblock, dis,
+ known_blocks,
+ scene, starty, startx+1);
+ nextl = tmp;
+ tmp->addPrevious(widget);
+ }
+ if (block->getNextBlock(1) != 0) {
+ tmpblock = dis->getBasicBlock(block->getNextBlock(1));
+ tmp = local__add_basic_block(tmpblock, dis,
+ known_blocks,
+ scene, starty, startx-1);
+ nextr = tmp;
+ tmp->addPrevious(widget);
+ }
+ widget->addNext(nextl, nextr);
+ return widget;
}
}
BasicBlock * block = disassembler->getBasicBlock(fun->getStartAddress());
- local__add_basic_block(block, disassembler.get(), scene, block->getStartAddress(), 100);
+ local__add_basic_block(block, disassembler.get(), blocks, scene, block->getStartAddress(), 100);
QGraphicsView * view = new QGraphicsView(scene);
w->addTab(view, "CFG");
#include <memory>
+#include <map>
#include <QTextEdit>
#include <QPushButton>
#include <QMainWindow>
#include <QStackedWidget>
#include "disassembler/Disassembler.hxx"
+#include "widgets/BasicBlockWidget.hxx"
class Mainwindow : public QMainWindow {
Q_OBJECT
QAction *openAction;
std::shared_ptr<Disassembler> disassembler;
+ std::map<uint64_t, BasicBlockWidget*> blocks;
private slots:
void quit();
void open();
#include "BasicBlockWidget.hxx"
-BasicBlockWidget::BasicBlockWidget(const QString& name)
+BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block)
: x(-5), y(-20)
, dx(270), dy(45)
- , name(name) {
+ , name(name)
+ , block(block) {
+ next[0] = NULL; next[1] = NULL;
_widget.setGridStyle(Qt::NoPen);
_widget.setMinimumHeight(_widget.rowHeight(0) + 10);
_widget.setMaximumHeight(20);
+#ifndef INCLUDE__BasicBlockWidget_hxx
+#define INCLUDE__BasicBlockWidget_hxx
+
#include "gui/qt.hxx"
#include "disassembler/BasicBlock.hxx"
+#include <vector>
+#include <cassert>
class BasicBlockWidget : public QGraphicsItem
{
public:
- BasicBlockWidget(const QString& name);
+ BasicBlockWidget(const QString& name, BasicBlock * block);
void addItem(uint8_t* bytes, size_t num_bytes, const QString& line);
QRectF boundingRect() const {
qreal penWidth = 1;
- return QRectF(x - penWidth / 2, y - penWidth / 2,
+ QRectF result(x - penWidth / 2, y - penWidth / 2,
dx + penWidth, dy + penWidth);
+
+
+ if (next[0])
+ result |= QRectF(QPointF(x + dx/3, y+dy),
+ mapFromScene(next[0]->getEntry()));
+
+ if (next[1])
+ result |= QRectF(QPointF(x + 2*dx/3, y+dy),
+ mapFromScene(next[1]->getEntry()));
+
+ return result;
}
+ void mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+ QGraphicsItem::mouseMoveEvent(event);
+ std::for_each(previous.begin(), previous.end(),
+ [&] (BasicBlockWidget * item) {
+ item->update(boundingRect() | item->boundingRect());
+ });
+ }
QPointF getEntry() const {
- return QPointF(x + dx/2, y);
+ return mapToScene(QPointF(x + dx/2, y));
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
painter->drawRect(x, y, dx, dy);
painter->drawText(0, -5, name);
- _widget.render(painter);
+ if (_widget.rowCount() != 0)
+ _widget.render(painter);
+
+ painter->setPen(QColor(0x00, 0xff, 0x00, 0xff));
+ if (next[0])
+ painter->drawLine(QPointF(x + dx/3, y+dy),
+ mapFromScene(next[0]->getEntry()));
+
+ painter->setPen(QColor(0xff, 0x00, 0x00, 0xff));
+ if (next[1])
+ painter->drawLine(QPointF(x + 2*dx/3, y+dy),
+ mapFromScene(next[1]->getEntry()));
+
}
+
+ void addPrevious(BasicBlockWidget * widget) {
+ previous.push_back(widget);
+ }
+
+ void addNext(BasicBlockWidget * left, BasicBlockWidget * right) {
+ next[0] = left;
+ next[1] = right;
+ }
+
private:
int x, y, dx, dy;
QTableWidget _widget;
QString name;
BasicBlock * block;
+ std::vector<BasicBlockWidget*> previous;
+ BasicBlockWidget* next[2];
};
+
+#endif