Name Blocks according to start *and* end address
[frida/frida.git] / src / gui / Mainwindow.cxx
1 #include "Mainwindow.hxx"
2 #include "widgets/BasicBlockWidget.hxx"
3 #include "qt.hxx"
4 #include "disassembler/llvm/LLVMDisassembler.hxx"
5
6 #include <iostream>
7 #include <sstream>
8
9 #include <QtGui>
10
11 Mainwindow::Mainwindow(const std::string& filename)
12 {
13 openAction = new QAction(tr("&Open"), this);
14 // saveAction = new QAction(tr("&Save"), this);
15 exitAction = new QAction(tr("E&xit"), this);
16
17 connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
18 // connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
19 connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
20
21 fileMenu = menuBar()->addMenu(tr("&File"));
22 fileMenu->addAction(openAction);
23 // fileMenu->addAction(saveAction);
24 fileMenu->addSeparator();
25 fileMenu->addAction(exitAction);
26
27 listWidget = new QListWidget();
28 stackedWidget = new QStackedWidget();
29 dockWidget = new QDockWidget(tr("Functions"), this);
30 dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea |
31 Qt::RightDockWidgetArea);
32 dockWidget->setWidget(listWidget);
33 addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
34 setCentralWidget(stackedWidget);
35
36 connect(listWidget, SIGNAL(currentRowChanged(int)),
37 stackedWidget, SLOT(setCurrentIndex(int)));
38
39 setWindowTitle(tr("FRIDA"));
40
41 openBinary(filename);
42 }
43
44 void Mainwindow::quit()
45 {
46 QMessageBox messageBox;
47 messageBox.setWindowTitle(tr("Notepad"));
48 messageBox.setText(tr("Do you really want to quit?"));
49 messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
50 messageBox.setDefaultButton(QMessageBox::No);
51 if (messageBox.exec() == QMessageBox::Yes)
52 qApp->quit();
53 }
54
55 void Mainwindow::open() {
56 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "",
57 tr("Binaries (*)"));
58
59 openBinary(fileName.toStdString());
60 }
61
62 void Mainwindow::openBinary(const std::string& filename) {
63 if (filename != "") {
64 disassembler.reset(new LLVMDisassembler(filename));
65 disassembler->forEachFunction([&](uint64_t address, Function* fun) {
66 populateSymbolInformation(fun);
67 });
68 }
69 }
70
71 namespace {
72 void local__add_basic_block(BasicBlock * block, Disassembler * dis, QGraphicsScene * scene,
73 uint64_t starty, uint64_t startx) {
74 std::stringstream s;
75 s << "BLOCK_" << std::hex << block->getStartAddress()
76 << "_" << block->getEndAddress();
77 BasicBlockWidget * widget = new BasicBlockWidget(s.str().c_str());
78 scene->addItem(widget);
79 widget->setFlag(QGraphicsItem::ItemIsMovable, true);
80 widget->moveBy(100*startx, 10*(block->getStartAddress() - starty));
81
82 dis->printEachInstruction(block->getStartAddress(), block->getEndAddress(),
83 [&](uint8_t* bytes, size_t byte_count, const std::string& line) {
84 widget->addItem(bytes, byte_count, line.c_str() + 1);
85 });
86
87 if (block->getNextBlock(0) != 0)
88 local__add_basic_block(dis->getBasicBlock(block->getNextBlock(0)), dis, scene, starty, startx+1);
89 if (block->getNextBlock(1) != 0)
90 local__add_basic_block(dis->getBasicBlock(block->getNextBlock(1)), dis, scene, starty, startx-1);
91 }
92 }
93
94 void Mainwindow::populateSymbolInformation(Function* fun) {
95 QTabWidget * w = new QTabWidget();
96
97 // CFG
98 QGraphicsScene * scene = new QGraphicsScene;
99
100 BasicBlock * block = disassembler->getBasicBlock(fun->getStartAddress());
101
102 local__add_basic_block(block, disassembler.get(), scene, block->getStartAddress(), 100);
103
104 QGraphicsView * view = new QGraphicsView(scene);
105 w->addTab(view, "CFG");
106
107 // Listing
108 QTableWidget * t = new QTableWidget();
109 t->setColumnCount(3);
110 t->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
111 // curBin->for_each_instruction(sym, [&t](long add, std::string bytes, std::string mnemonic) {
112 // int row = t->rowCount();
113 // std::stringstream s;
114 // t->setRowCount(t->rowCount() + 1);
115 // s << std::hex << add;
116 // t->setItem(row,0,new QTableWidgetItem(s.str().c_str()));
117 // s.str("");
118 // s << std::hex;
119 // for_each(bytes.begin(), bytes.end(), [&s](char c){s << (unsigned int)((unsigned char)c) << ' ';});
120 // t->setItem(row,1,new QTableWidgetItem(s.str().c_str()));
121 // t->setItem(row,2,new QTableWidgetItem(mnemonic.c_str() + 1));
122 // });
123 w->addTab(t, "Listing");
124
125 listWidget->addItem(fun->getName().c_str());
126 stackedWidget->addWidget(w);
127 }