From d51782798930575415d53300340ede3a004f1661 Mon Sep 17 00:00:00 2001 From: Christoph Egger Date: Tue, 3 Jun 2014 19:05:30 +0200 Subject: [PATCH] Draw edges between BasicBlockWidgets while handling loops --- src/gui/Mainwindow.cxx | 46 +++++++++++++++++----- src/gui/Mainwindow.hxx | 3 ++ src/gui/widgets/BasicBlockWidget.cxx | 6 ++- src/gui/widgets/BasicBlockWidget.hxx | 57 ++++++++++++++++++++++++++-- 4 files changed, 96 insertions(+), 16 deletions(-) diff --git a/src/gui/Mainwindow.cxx b/src/gui/Mainwindow.cxx index 7d6b8d2..ace89f3 100644 --- a/src/gui/Mainwindow.cxx +++ b/src/gui/Mainwindow.cxx @@ -1,10 +1,10 @@ - #include "Mainwindow.hxx" -#include "widgets/BasicBlockWidget.hxx" +#include "Mainwindow.hxx" #include "qt.hxx" #include "disassembler/llvm/LLVMDisassembler.hxx" #include #include +#include #include @@ -69,12 +69,22 @@ void Mainwindow::openBinary(const std::string& filename) { } 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& 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)); @@ -84,10 +94,26 @@ namespace { 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; } } @@ -99,7 +125,7 @@ void Mainwindow::populateSymbolInformation(Function* fun) { 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"); diff --git a/src/gui/Mainwindow.hxx b/src/gui/Mainwindow.hxx index 9e9a6ca..a0ba231 100644 --- a/src/gui/Mainwindow.hxx +++ b/src/gui/Mainwindow.hxx @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,6 +7,7 @@ #include #include "disassembler/Disassembler.hxx" +#include "widgets/BasicBlockWidget.hxx" class Mainwindow : public QMainWindow { Q_OBJECT @@ -29,6 +31,7 @@ private: QAction *openAction; std::shared_ptr disassembler; + std::map blocks; private slots: void quit(); void open(); diff --git a/src/gui/widgets/BasicBlockWidget.cxx b/src/gui/widgets/BasicBlockWidget.cxx index 6d187a7..0164bdc 100644 --- a/src/gui/widgets/BasicBlockWidget.cxx +++ b/src/gui/widgets/BasicBlockWidget.cxx @@ -1,9 +1,11 @@ #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); diff --git a/src/gui/widgets/BasicBlockWidget.hxx b/src/gui/widgets/BasicBlockWidget.hxx index 10ea402..3c61121 100644 --- a/src/gui/widgets/BasicBlockWidget.hxx +++ b/src/gui/widgets/BasicBlockWidget.hxx @@ -1,21 +1,44 @@ +#ifndef INCLUDE__BasicBlockWidget_hxx +#define INCLUDE__BasicBlockWidget_hxx + #include "gui/qt.hxx" #include "disassembler/BasicBlock.hxx" +#include +#include 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, @@ -24,11 +47,37 @@ public: 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 previous; + BasicBlockWidget* next[2]; }; + +#endif -- 2.39.5