]> git.siccegge.de Git - frida/frida.git/commitdiff
Separate out several parts of llvm-using code ; Add logger
authorChristoph Egger <christoph@christoph-egger.org>
Mon, 10 Feb 2014 23:49:39 +0000 (00:49 +0100)
committerChristoph Egger <christoph@christoph-egger.org>
Mon, 10 Feb 2014 23:49:39 +0000 (00:49 +0100)
16 files changed:
CMakeLists.txt
src/Binary.cxx
src/Section.hxx [new file with mode: 0644]
src/disassembler/BasicBlock.hxx [new file with mode: 0644]
src/disassembler/Disassembler.cxx [new file with mode: 0644]
src/disassembler/Disassembler.hxx [new file with mode: 0644]
src/disassembler/Instruction.hxx [new file with mode: 0644]
src/disassembler/InstructionReader.cxx [new file with mode: 0644]
src/disassembler/InstructionReader.hxx [new file with mode: 0644]
src/disassembler/Target.cxx [new file with mode: 0644]
src/disassembler/Target.hxx [new file with mode: 0644]
src/gui/Mainwindow.cxx
src/gui/qt.hxx [new file with mode: 0644]
src/gui/widgets/BasicBlockWidget.cxx [new file with mode: 0644]
src/gui/widgets/BasicBlockWidget.hxx [new file with mode: 0644]
src/main.cxx

index c6219ab94efdab8bde9ce9426d4e9bc2d50a226e..c1ef103a5de1dbd2fbbdcf796f12741f421fa9a2 100644 (file)
@@ -8,6 +8,10 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
 
 find_package(Qt5Widgets)
 find_package(Qt5 CONFIG REQUIRED Widgets Gui)
+find_package(PkgConfig)
+pkg_check_modules(LOG4CXX REQUIRED liblog4cxx)
+
+add_compile_options(${LOG4CXX_CFLAGS})
 
 execute_process(COMMAND llvm-config-3.4 --cflags  OUTPUT_VARIABLE LLVM_CFLAGS  OUTPUT_STRIP_TRAILING_WHITESPACE)
 execute_process(COMMAND llvm-config-3.4 --ldflags OUTPUT_VARIABLE LLVM_LDFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE)
@@ -22,19 +26,27 @@ separate_arguments(LLVM_LIBS)
 
 set(CMAKE_CXX_COMPILER "clang++")
 
-SET(qtlldb_SOURCES src/Binary.cxx src/main.cxx src/gui/Mainwindow.cxx)
-SET(qtlldb_HEADERS src/Binary.hxx src/include.hxx src/Section.hxx src/gui/Mainwindow.h++)
+SET(qtlldb_SOURCES
+  src/Binary.cxx
+  src/main.cxx
+  src/gui/Mainwindow.cxx
+  src/gui/widgets/BasicBlockWidget.cxx
+  src/disassembler/InstructionReader.cxx
+  src/disassembler/Target.cxx
+  src/disassembler/Disassembler.cxx)
+SET(qtlldb_HEADERS
+  src/Binary.hxx
+  src/include.hxx
+  src/Section.hxx
+  src/gui/Mainwindow.hxx
+  src/gui/widgets/BasicBlockWidget.hxx)
 
 ADD_EXECUTABLE(qtlldb ${qtlldb_SOURCES} ${qtlldb_HEADERS_MOC})
 
-
-set(CMAKE_LD_FLAGS  "${LLVM_LDFLAGS}")
-set(CMAKE_LIBS  "${LLVM_LIBS}")
-
 ADD_DEFINITIONS(${QT_DEFINITIONS})
 
 qt5_use_modules(qtlldb Widgets)
 INCLUDE_DIRECTORIES("src")
 
 
-TARGET_LINK_LIBRARIES(qtlldb ${QT_LIBRARIES} ${LLVM_LDFLAGS} ${LLVM_LIBS})
+TARGET_LINK_LIBRARIES(qtlldb ${QT_LIBRARIES} ${LLVM_LDFLAGS} ${LLVM_LIBS} ${LOG4CXX_LDFLAGS})
index 1fcd908543ea767bd20affe2cc337d85ade4a72b..1d64b8445a0ef80bfcdfd3d87d1aa90302d8c6c5 100644 (file)
@@ -1,5 +1,7 @@
 #include "Binary.hxx"
 
+#include "disassembler/Disassembler.hxx"
+
 #include <iostream>
 #include <string>
 #include <algorithm>
@@ -83,6 +85,7 @@ namespace qtlldb {
     Binary::Binary(const std::string& filename)
         : triple("unkown-unknown-unknown")
     {
+        ::Disassembler d(filename);
         std::string error;
 
         createBinary(filename, binary);
diff --git a/src/Section.hxx b/src/Section.hxx
new file mode 100644 (file)
index 0000000..04e8b0f
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef INCLUDE__Section_hxx
+#define INCLUDE__Section_hxx
+
+#include "include.hxx"
+
+#include <string>
+#include <vector>
+#include <functional>
+
+using llvm::OwningPtr;
+
+namespace qtlldb {
+    class Section {
+    public:
+        Section() {}
+
+    private:
+        
+    };
+}
+#endif
diff --git a/src/disassembler/BasicBlock.hxx b/src/disassembler/BasicBlock.hxx
new file mode 100644 (file)
index 0000000..22a31e0
--- /dev/null
@@ -0,0 +1,8 @@
+
+
+class BasicBlock {
+public:
+
+private:
+
+};
diff --git a/src/disassembler/Disassembler.cxx b/src/disassembler/Disassembler.cxx
new file mode 100644 (file)
index 0000000..babac82
--- /dev/null
@@ -0,0 +1,18 @@
+#include "Disassembler.hxx"
+
+namespace {
+    llvm::OwningPtr<llvm::object::Binary>&
+    constructor_helper(const std::string& filename,
+                       llvm::OwningPtr<llvm::object::Binary>& binary) {
+        createBinary(filename, binary);
+        return binary;
+    }
+}
+
+
+Disassembler::Disassembler(const std::string& filename) 
+    : _target(constructor_helper(filename, _binary))
+{
+    
+}
+
diff --git a/src/disassembler/Disassembler.hxx b/src/disassembler/Disassembler.hxx
new file mode 100644 (file)
index 0000000..5ccd9ff
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef INCLUDE__Disassembler_hxx
+#define INCLUDE__Disassembler_hxx
+
+#include "Target.hxx"
+#include "include.hxx"
+
+class Disassembler {
+public:
+    Disassembler(const std::string& filename);
+private:
+    llvm::OwningPtr<llvm::object::Binary> _binary;
+
+    Target _target;
+};
+
+#endif
diff --git a/src/disassembler/Instruction.hxx b/src/disassembler/Instruction.hxx
new file mode 100644 (file)
index 0000000..afb5193
--- /dev/null
@@ -0,0 +1,6 @@
+
+
+class Instruction {
+public:
+private:
+};
diff --git a/src/disassembler/InstructionReader.cxx b/src/disassembler/InstructionReader.cxx
new file mode 100644 (file)
index 0000000..8f8df91
--- /dev/null
@@ -0,0 +1,27 @@
+#include "InstructionReader.hxx"
+
+InstructionReader::InstructionReader(const Target& target)
+    : _logger(log4cxx::Logger::getLogger("disassembler.Target"))
+    , _target(target)
+{
+    DisAsm.reset(_target.getTarget().createMCDisassembler(_target.getSubTargetInfo()));
+    if (!DisAsm) {
+        LOG4CXX_ERROR(_logger, "error: no disassembler for target " << _target.getTripleName())
+            return;
+    }
+}
+
+void InstructionReader::readInstruction(std::string& data, size_t& offset, Instruction& inst) {
+    llvm::MCInst instr;
+    llvm::StringRefMemoryObject memoryObject(data);
+    uint64_t size;
+    
+    if (DisAsm->getInstruction(instr, size, memoryObject, offset,
+                               llvm::nulls(), llvm::nulls())) {
+        
+    } else {
+        LOG4CXX_WARN(_logger, "warning: invalid instruction encoding");
+        if (size == 0)
+            size = 1; // skip illegible bytes
+    }
+}
diff --git a/src/disassembler/InstructionReader.hxx b/src/disassembler/InstructionReader.hxx
new file mode 100644 (file)
index 0000000..a68644d
--- /dev/null
@@ -0,0 +1,18 @@
+#include "include.hxx"
+#include "Instruction.hxx"
+#include "Target.hxx"
+
+#include <string>
+
+#include <log4cxx/logger.h>
+
+
+class InstructionReader {
+public:
+    InstructionReader(const Target& target);
+    void readInstruction(std::string& data, size_t& offset, Instruction& inst);
+private:
+    log4cxx::LoggerPtr _logger;
+    const Target& _target;
+    llvm::OwningPtr<llvm::MCDisassembler> DisAsm;
+};
diff --git a/src/disassembler/Target.cxx b/src/disassembler/Target.cxx
new file mode 100644 (file)
index 0000000..56042d5
--- /dev/null
@@ -0,0 +1,29 @@
+#include "Target.hxx"
+#include "include.hxx"
+
+#include <string>
+
+Target::    Target(const llvm::OwningPtr<llvm::object::Binary>& binary)
+    : _logger(log4cxx::Logger::getLogger("disassembler.Target"))
+    , triple("unknown-unknown-unknown")
+{
+    std::string error;
+    llvm::object::ObjectFile * o = llvm::dyn_cast<llvm::object::ObjectFile>(binary.get());
+
+    triple.setArch(llvm::Triple::ArchType(o->getArch()));
+    std::string tripleName(triple.getTriple());
+    LOG4CXX_INFO(_logger, "Detected triple " << tripleName);
+
+    target = llvm::TargetRegistry::lookupTarget("", triple, error);
+    if (!target) {
+        LOG4CXX_ERROR(_logger, "Couldn't create Target: " << error);
+        return;
+    }
+    LOG4CXX_INFO(_logger, "Target: " << target->getName());
+
+    STI.reset(target->createMCSubtargetInfo(tripleName, "", ""));
+    if (!STI) {
+        LOG4CXX_ERROR(_logger, "No subtarget for target " << tripleName);
+        return;
+    }
+}
diff --git a/src/disassembler/Target.hxx b/src/disassembler/Target.hxx
new file mode 100644 (file)
index 0000000..db3a4be
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef INCLUDE__Target_hxx
+#define INCLUDE__Target_hxx
+
+#include <log4cxx/logger.h>
+
+#include "include.hxx"
+
+class Target {
+public:
+    Target(const llvm::OwningPtr<llvm::object::Binary>& binary);
+
+    const llvm::Target& getTarget() const {
+        return *target;
+    }
+
+    const llvm::MCSubtargetInfo& getSubTargetInfo() const {
+        return *STI;
+    }
+
+    std::string getTripleName() const {
+        return triple.getTriple();
+    }
+private:
+    log4cxx::LoggerPtr _logger;
+    llvm::Triple triple;
+
+    const llvm::Target * target;
+    llvm::OwningPtr<const llvm::MCSubtargetInfo> STI;
+};
+
+#endif
index 790c1a306bbba59020383a311c8b59acae5dfe8d..8d6bb6882f10ebf4f7b05e2ca767b48a285eeb1b 100644 (file)
@@ -1,24 +1,11 @@
 #include "Mainwindow.hxx"
+#include "widgets/BasicBlockWidget.hxx"
+#include "qt.hxx"
 
 #include <iostream>
 #include <sstream>
 
 #include <QtGui>
-#include <QAction>
-#include <QMenuBar>
-#include <QMenu>
-#include <QDockWidget>
-#include <QMessageBox>
-#include <QFileDialog>
-#include <QTableWidget>
-#include <QHeaderView>
-#include <QGraphicsScene>
-#include <QGraphicsItem>
-#include <QGraphicsView>
-
-namespace {
-
-}
 
 Mainwindow::Mainwindow()
 {
@@ -103,8 +90,27 @@ void Mainwindow::populateSymbolInformation(const std::string& sym) {
 
     // CFG
     QGraphicsScene * scene = new QGraphicsScene;
-    QGraphicsRectItem *rect = scene->addRect(QRectF(0, 0, 100, 100));
-    rect->setFlag(QGraphicsItem::ItemIsMovable);
+
+    BasicBlockWidget * s1 = new BasicBlockWidget;
+    scene->addItem(s1);
+    s1->setFlag(QGraphicsItem::ItemIsMovable, true);
+
+    BasicBlockWidget * s2 = new BasicBlockWidget;
+    scene->addItem(s2);
+    s2->setFlag(QGraphicsItem::ItemIsMovable, true);
+    s2->moveBy(-200, 350);
+
+    BasicBlockWidget * s3 = new BasicBlockWidget;
+    scene->addItem(s3);
+    s3->setFlag(QGraphicsItem::ItemIsMovable, true);
+    s3->moveBy(100, 350);
+
+    BasicBlockWidget * s4 = new BasicBlockWidget;
+    scene->addItem(s4);
+    s4->setFlag(QGraphicsItem::ItemIsMovable, true);
+    s4->moveBy(400, 350);
+
+
     QGraphicsView * view = new QGraphicsView(scene);
     w->addTab(view, "CFG");
 
diff --git a/src/gui/qt.hxx b/src/gui/qt.hxx
new file mode 100644 (file)
index 0000000..493e7b2
--- /dev/null
@@ -0,0 +1,16 @@
+#include <QAction>
+#include <QDockWidget>
+#include <QFileDialog>
+#include <QGraphicsItem>
+#include <QGraphicsProxyWidget>
+#include <QGraphicsScene>
+#include <QGraphicsView>
+#include <QGroupBox>
+#include <QHeaderView>
+#include <QListWidget>
+#include <QMenu>
+#include <QMenuBar>
+#include <QMessageBox>
+#include <QPainter>
+#include <QTableWidget>
+#include <QVBoxLayout>
diff --git a/src/gui/widgets/BasicBlockWidget.cxx b/src/gui/widgets/BasicBlockWidget.cxx
new file mode 100644 (file)
index 0000000..b28b04f
--- /dev/null
@@ -0,0 +1,3 @@
+
+
+
diff --git a/src/gui/widgets/BasicBlockWidget.hxx b/src/gui/widgets/BasicBlockWidget.hxx
new file mode 100644 (file)
index 0000000..6ea927b
--- /dev/null
@@ -0,0 +1,36 @@
+#include "gui/qt.hxx"
+
+class BasicBlockWidget : public QGraphicsItem
+{
+public:
+    BasicBlockWidget() {
+        x = -5;
+        y = -20;
+        dx = 250;
+        dy = 270;
+        _widget.addItem("THIS");
+        _widget.addItem("IS");
+        _widget.addItem("A");
+        _widget.addItem("TEST");
+    }
+
+    QRectF boundingRect() const
+    {
+        qreal penWidth = 1;
+        return QRectF(x - penWidth / 2, y - penWidth / 2,
+                      dx + penWidth, dy + penWidth);
+    }
+
+    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
+               QWidget *widget)
+    {
+        painter->fillRect(x, y, dx, dy, QColor(0xcc, 0xcc, 0xff, 0xff));
+        painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
+        painter->drawRect(x, y, dx, dy);
+        painter->drawText(0, -5, "BLOCK");
+        _widget.render(painter);
+    }
+private:
+    int x, y, dx, dy;
+    QListWidget _widget;
+};
index b23eb8860d1841551e2ce43a145f2d2ec09e4498..afcf93007b3e564d5bbfd74ccbdadfcee5f3a08b 100644 (file)
@@ -6,6 +6,9 @@
 #include <QApplication>
 #include <QTextEdit>
 
+#include "log4cxx/logger.h"
+#include "log4cxx/basicconfigurator.h"
+
 #include "Binary.hxx"
 #include "gui/Mainwindow.hxx"
 
@@ -15,6 +18,9 @@ using std::cerr;
 
 int main(int argc, char** argv)
 {
+    log4cxx::BasicConfigurator::configure();
+    log4cxx::LoggerPtr _logger(log4cxx::Logger::getLogger("main"));
+    LOG4CXX_DEBUG(_logger, "Initializing LLVM");
     llvm::InitializeAllTargetInfos();
     llvm::InitializeAllTargetMCs();
     llvm::InitializeAllAsmParsers();