Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mainwindow.cpp #6

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions src/disassemblycore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ void DisassemblyCore::disassemble(QString file){
xrefStrings();

fileLoaded = true;
fileName = file;
}

QString DisassemblyCore::getFileName() {
if(fileLoaded == true) {
return fileName;
} else {
return "NaN";
}
}

bool DisassemblyCore::disassemblyIsLoaded(){
Expand Down
2 changes: 2 additions & 0 deletions src/disassemblycore.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DisassemblyCore
bool disassemblyIsLoaded();
void xrefStrings();
static QString extractAddress(const QByteArray& s);
QString getFileName();
QVector<QString> getBaseOffsets();
QString getObjdumpErrorMsg(QString file);
QString getSymbolsTable(QString file);
Expand Down Expand Up @@ -59,6 +60,7 @@ class DisassemblyCore
Strings strings;
QVector<QString> baseOffsets;
bool fileLoaded;
QString fileName;


ObjDumper objDumper;
Expand Down
122 changes: 120 additions & 2 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
#include "QInputDialog"
#include "QProgressDialog"
#include "QFuture"
#include "QFile"
#include "QTextStream"
#include "QtConcurrent/QtConcurrent"

#include "QDebug"

#include "resultsdialog.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
Expand Down Expand Up @@ -196,9 +199,33 @@ void MainWindow::loadBinary(QString file){
ui->codeBrowser->setPlainText("File format not recognized.");
ui->addressLabel->setText("");
ui->functionLabel->setText("");
int num1 = 0;
std::string s1 = ("Instruction count: "+std::to_string(num1));
QString arg1 = QString::fromLocal8Bit(s1.c_str());
ui->fileInstructionCountlabel->setText(arg1);
} else {
// If all good, display disassembly data
displayFunctionData();

//Display number of instructions detected
int num1 = 0;
QStringList arg;
arg << "-d" << file;
QProcess *proc = new QProcess();
proc->start(ui->customBinaryLineEdit->text(), arg);
proc->waitForFinished();
QString result=proc->readAllStandardOutput();
QRegularExpression re("[0-9a-fA-F]+:\t");
QRegularExpressionMatchIterator i = re.globalMatch(result);
while(i.hasNext()) {
QRegularExpressionMatch match = i.next();
(void)match; //Suppress -Wunused-parameter
num1=num1+1;
}
//stops here
std::string s1 = ("Instruction count: "+std::to_string(num1));
QString arg1 = QString::fromLocal8Bit(s1.c_str());
ui->fileInstructionCountlabel->setText(arg1);

// Add initial location to history
addToHistory(currentFunctionIndex, 0);
Expand All @@ -215,6 +242,7 @@ void MainWindow::loadBinary(QString file){
setUpdatesEnabled(false);

ui->fileFormatlabel->setText(disassemblyCore.getFileFormat(file));

ui->symbolsBrowser->setPlainText(disassemblyCore.getSymbolsTable(file));
ui->relocationsBrowser->setPlainText(disassemblyCore.getRelocationEntries(file));
ui->headersBrowser->setPlainText(disassemblyCore.getHeaders(file));
Expand Down Expand Up @@ -249,6 +277,87 @@ void MainWindow::on_actionOpen_triggered()

}

//Dump File
void MainWindow::on_actionDumpFile_triggered()
{
QString filename = "objdumpOutput.txt";
QFile file2(filename);
if(file2.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) {
QTextStream stream(&file2);

//dump functions
QStringList funcs = disassemblyCore.getFunctionNames();
QVector<QString> baseOffsets = disassemblyCore.getBaseOffsets();
for(const auto& func : funcs) {
Function currFunc = disassemblyCore.getFunction(func);
stream << "F|"+currFunc.getName().remove("@plt")+"|"+currFunc.getAddress()<<endl;
//write here using stream << "something" << endl;
}

// dump instructions
// regex for parsing instruction nmeumonics: [\s]+\t(...)[.]*[a-z]*
// after regexing for those, regex for: (...)[.]*[a-z]*
// use [\s][a-fA-F0-9]+[:] regex to grab address
// out of those regex matches to grab JUST the instruction mnemonic
// TO-DO AFTER IMPLEMENTING ABOVE: Make a regex to grab the instruction address on the first column of objdump output

QStringList arg;
arg << "-d" << disassemblyCore.getFileName();
QProcess *proc = new QProcess();
proc->start(ui->customBinaryLineEdit->text(), arg);
proc->waitForFinished();
QString result=proc->readAllStandardOutput();
QString line;
QTextStream stream2(&result);
while (stream2.readLineInto(&line)) {
QString address;
QString nmeumonic;
QRegularExpression addressRegex("[\\s][a-fA-F0-9]+[:]");

QRegularExpressionMatch match = addressRegex.match(line);
if(match.hasMatch()) {
QString matched = match.captured(0);
address = matched.mid(1, (matched.length()-2));
} else {
continue;
}


QRegularExpression nmeumonicRegex("[\\s]+\t(...)[.]*[a-z]*");
QRegularExpressionMatch match2 = nmeumonicRegex.match(line);
if(match2.hasMatch()) {
nmeumonic = match2.captured(0).simplified();
nmeumonic.remove("\t");
if(nmeumonic.contains(" ")) {
nmeumonic = nmeumonic.split(" ").at(0);
}
/*
QRegularExpression nmeumonicRegex2("(...)[.]*[a-z]*");
QRegularExpressionMatch match3 = nmeumonicRegex2.match(line2);
if(match3.hasMatch()) {
qDebug() << "MATCH 3 BEFORE: "<<match3.captured(0)<<endl;
nmeumonic = match3.captured(0).simplified();
nmeumonic.remove('\t');
qDebug() << "MATCH 3 AFTER: "<<nmeumonic<<endl;
} else {
continue;
}
*/
} else {
continue;
}
while(address.size() < 8) {
address = "0"+address;
}
stream << "I|"+nmeumonic<<"|"<<address<<endl;
}


}
file2.close();
}


bool MainWindow::canDisassemble(QString file){
// Check for errors or invalid file
QString errorMsg = disassemblyCore.getObjdumpErrorMsg(file);
Expand Down Expand Up @@ -323,13 +432,21 @@ void MainWindow::displayFunctionText(int functionIndex){
}
}
}

// Setup functionlist and display function data
void MainWindow::displayFunctionData(){
if (disassemblyCore.disassemblyIsLoaded()){
// Populate function list in sidebar
ui->functionList->addItems(disassemblyCore.getFunctionNames());

int num = 0;
for(const auto& i : disassemblyCore.getFunctionNames()) {
(void)i; //Suppress -Wunused-parameter
num = num + 1;
}
std::string s = ("Functions ["+std::to_string(num)+"]");
QString arg = QString::fromLocal8Bit(s.c_str());
ui->functionListLabel->setText(arg);

// Display main function by default if it exists
if (disassemblyCore.functionExists("main"))
displayFunctionText("main");
Expand Down Expand Up @@ -1055,6 +1172,8 @@ void MainWindow::setMenuStyle(QString foregroundColor, QString backgroundColor,
}

void MainWindow::setNavbarStyle(QString foregroundColor, QString backgroundColor){
(void)foregroundColor; //Suppress -Wunused-parameter

QString navBarStyle = "#navBar {background-color: " + backgroundColor + "; border-bottom: 1px solid #d4d4d4;}";
ui->navBar->setStyleSheet(navBarStyle);

Expand Down Expand Up @@ -1211,4 +1330,3 @@ void MainWindow::on_actionFullscreen_triggered()
MainWindow::showFullScreen();
}
}

2 changes: 2 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ private slots:

void on_actionOpen_triggered();

void on_actionDumpFile_triggered();

void loadBinary(QString file);

bool canDisassemble(QString file);
Expand Down
29 changes: 29 additions & 0 deletions src/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ font-size: 11pt;</string>
<number>10</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="fileInstructionCountlabel">
<property name="font">
<font/>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignLeft|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="indent">
<number>10</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="fileFormatlabel">
Expand Down Expand Up @@ -1357,6 +1379,8 @@ color: rgb(85, 85, 85);</string>
</property>
<addaction name="actionOpen"/>
<addaction name="separator"/>
<addaction name="actionDumpFile"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuTools">
Expand Down Expand Up @@ -1391,6 +1415,11 @@ color: rgb(85, 85, 85);</string>
<string>Ctrl+O</string>
</property>
</action>
<action name="actionDumpFile">
<property name="text">
<string>Dump Info File</string>
</property>
</action>
<action name="actionProject">
<property name="text">
<string>Project</string>
Expand Down
30 changes: 18 additions & 12 deletions src/objdumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ObjDumper::ObjDumper()
insnwidth = 10;

addressRegex.setPattern("[0-9a-f]+");

hexBytesRegex.setPattern("[0-9a-f ]+");
}

Expand Down Expand Up @@ -138,9 +139,8 @@ QVector<QByteArray> ObjDumper::parseFunctionLine(QStringRef line){
}

// Get hex
QByteArray hexBytes = line.mid(pos, insnwidth * 3).toLocal8Bit();
QByteArray hexBytes = line.mid(pos, insnwidth * 4).toLocal8Bit();
row[1] = parseHexBytes(hexBytes);

pos += insnwidth * 3;

// Skip whitespace
Expand All @@ -157,6 +157,16 @@ QVector<QByteArray> ObjDumper::parseFunctionLine(QStringRef line){
pos++;

row[2] = opt;
QString temp2 = QString(row[2]);
if(temp2.contains("\t")) {
QByteArray temp3;
temp3 += temp2.split("\t")[1];
row[2] = temp3;
}
if(temp2.size() == 1) {
QByteArray temp3;
row[2] = temp3;
}

while (pos < line.length() && line.at(pos) == QChar(' ')){
pos++;
Expand Down Expand Up @@ -199,16 +209,12 @@ QByteArray ObjDumper::parseAddress(QByteArray address){
}

QByteArray ObjDumper::parseHexBytes(QByteArray byteString){
QRegularExpressionMatch hexMatch = hexBytesRegex.match(byteString);

if (hexMatch.hasMatch() && hexMatch.capturedLength(0) == byteString.length()) {
byteString.replace(" ", "");
int paddingLength = (insnwidth * 2) - byteString.length();
QString padding = "";
padding.fill(' ', paddingLength);
byteString.append(padding);

return byteString;
QString temp(byteString);
if (temp.contains("\t")) {
QStringList lst = temp.split("\t");
QByteArray ba;
ba += lst[1];
return ba;

} else {
return "";
Expand Down