]> git.siccegge.de Git - frida/frida.git/commitdiff
Draw edges between BasicBlockWidgets while handling loops
authorChristoph Egger <siccegge@cs.fau.de>
Tue, 3 Jun 2014 17:05:30 +0000 (19:05 +0200)
committerChristoph Egger <siccegge@cs.fau.de>
Tue, 3 Jun 2014 17:05:30 +0000 (19:05 +0200)
src/gui/Mainwindow.cxx
src/gui/Mainwindow.hxx
src/gui/widgets/BasicBlockWidget.cxx
src/gui/widgets/BasicBlockWidget.hxx

index 7d6b8d28b5b244ca965f3b435858f5596ef88a43..ace89f34523d629da3bf7c1e8963c0073df03b62 100644 (file)
@@ -1,10 +1,10 @@
- #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>
 
@@ -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<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));
@@ -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");
index 9e9a6ca0ba65b3df776521d23cda7b3733164ade..a0ba231884b485ab53319196d050eab385009451 100644 (file)
@@ -1,4 +1,5 @@
 #include <memory>
+#include <map>
 #include <QTextEdit>
 #include <QPushButton>
 #include <QMainWindow>
@@ -6,6 +7,7 @@
 #include <QStackedWidget>
 
 #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> disassembler;
+       std::map<uint64_t, BasicBlockWidget*> blocks;
 private slots:
     void quit();
     void open();
index 6d187a7c1a3e57b7165789496b956bbf24d559fd..0164bdc9513d67e541a2415ae7aa8cb1f9117a1a 100644 (file)
@@ -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);
index 10ea402267f5fc1dd1aa0088e3dc533b28644e0e..3c61121c75fa3da7f7e55894dc723c3f15e2e5be 100644 (file)
@@ -1,21 +1,44 @@
+#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,
@@ -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<BasicBlockWidget*> previous;
+       BasicBlockWidget* next[2];
 };
+
+#endif