-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
1332 lines (1118 loc) · 44.6 KB
/
mainwindow.cpp
File metadata and controls
1332 lines (1118 loc) · 44.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qfiledialog.h"
#include "qmessagebox.h"
#include "QScrollBar"
#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)
{
ui->setupUi(this);
QObject::connect(ui->hexAddressBrowser->verticalScrollBar(), SIGNAL(valueChanged(int)), ui->hexBrowser->verticalScrollBar(), SLOT(setValue(int)));
QObject::connect(ui->hexBrowser->verticalScrollBar(), SIGNAL(valueChanged(int)), ui->hexAddressBrowser->verticalScrollBar(), SLOT(setValue(int)));
QObject::connect(ui->stringsAddressBrowser->verticalScrollBar(), SIGNAL(valueChanged(int)), ui->stringsBrowser->verticalScrollBar(), SLOT(setValue(int)));
QObject::connect(ui->stringsBrowser->verticalScrollBar(), SIGNAL(valueChanged(int)), ui->stringsAddressBrowser->verticalScrollBar(), SLOT(setValue(int)));
/*
* Setup builtin fonts
*/
// Sans serif
int sansid = QFontDatabase::addApplicationFont(":/fonts/NotoSans-Regular.ttf");
QString sansfamily = QFontDatabase::applicationFontFamilies(sansid).at(0);
QFont sans(sansfamily);
sans.setPointSize(11);
this->setFont(sans);
ui->disTabWidget->setFont(sans);
ui->syntaxLabel->setFont(sans);
ui->disassemblyFlagLabel->setFont(sans);
ui->functionListLabel->setFont(sans);
ui->functionList->setFont(sans);
ui->customBinaryButton->setFont(sans);
ui->stringsAddressBrowser->setFont(sans);
ui->stringsBrowser->setFont(sans);
ui->symbolsBrowser->setFont(sans);
ui->relocationsBrowser->setFont(sans);
ui->headersBrowser->setFont(sans);
// Sans serif bold
int sansBoldId = QFontDatabase::addApplicationFont(":/fonts/NotoSans-Bold.ttf");
QString sansBoldFamily = QFontDatabase::applicationFontFamilies(sansBoldId).at(0);
QFont sansBold(sansBoldFamily);
sansBold.setPointSize(11);
sansBold.setBold(true);
ui->syntaxLabel->setFont(sansBold);
ui->disassemblyFlagLabel->setFont(sansBold);
ui->functionLabel->setFont(sansBold);
ui->addressLabel->setFont(sansBold);
ui->fileOffsetLabel->setFont(sansBold);
ui->sectionLabel->setFont(sansBold);
ui->hexAddressLabel->setFont(sansBold);
ui->hexLabel->setFont(sansBold);
ui->symbolsTableLabel->setFont(sansBold);
ui->relocationsLabel->setFont(sansBold);
ui->stringsAddressLabel->setFont(sansBold);
ui->stringsLabel->setFont(sansBold);
// Monospace
int monoid = QFontDatabase::addApplicationFont(":/fonts/Anonymous Pro.ttf");
QString monofamily = QFontDatabase::applicationFontFamilies(monoid).at(0);
QFont mono(monofamily);
mono.setPointSize(12);
ui->codeBrowser->setFont(mono);
ui->hexAddressBrowser->setFont(mono);
ui->hexBrowser->setFont(mono);
ui->addressValueLabel->setFont(mono);
ui->fileOffsetValueLabel->setFont(mono);
ui->sectionValueLabel->setFont(mono);
// Monospace Bold
int monoBoldId = QFontDatabase::addApplicationFont(":/fonts/Anonymous Pro B.ttf");
QString monoBoldFamily = QFontDatabase::applicationFontFamilies(monoBoldId).at(0);
QFont monoBold(monoBoldFamily);
monoBold.setPointSize(13);
monoBold.setBold(true);
this->setWindowTitle("ObjGUI");
// Set Window Size
MainWindow::resize(settings.value("windowWidth", 1000).toInt(), settings.value("windowHeight", 600).toInt());
ui->splitter->restoreState(settings.value("splitterSizes").toByteArray());
ui->searchBar->hide();
currentSearchTerm = "";
/*
* Set options from saved settings
*/
// Syntax
if (settings.value("syntax", "intel") == "intel"){
ui->actionIntel->setChecked(true);
ui->syntaxComboBox->setCurrentIndex(0);
disassemblyCore.setOutputSyntax("intel");
}else if (settings.value("syntax", "intel") == "att"){
ui->actionAtt->setChecked(true);
ui->syntaxComboBox->setCurrentIndex(1);
disassemblyCore.setOutputSyntax("att");
}
// Optional flags
if (settings.value("demangle", false) == true){
ui->demanlgeCheckBox->setChecked(true);
disassemblyCore.setDemangleFlag("-C");
}
// Custom binary
if (settings.value("useCustomBinary", false).toBool()){
ui->customBinaryCheckBox->setChecked(true);
disassemblyCore.setUseCustomBinary(true);
}
disassemblyCore.setobjdumpBinary(settings.value("customBinary", "").toString());
ui->customBinaryLineEdit->setText(settings.value("customBinary", "").toString());
// Style
disHighlighter = new DisassemblyHighlighter(ui->codeBrowser->document(), "Default");
headerHighlighter = new HeaderHighlighter(ui->headersBrowser->document());
QString theme = settings.value("theme", "default").toString();
if (theme == "dark"){
on_actionDark_triggered();
} else if (theme == "solarized"){
on_actionSolarized_triggered();
}else if (theme == "solarizedDark"){
on_actionSolarized_Dark_triggered();
} else {
on_actionDefault_triggered();
}
connect(ui->codeBrowser, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
currentFunctionIndex = 0;
}
MainWindow::~MainWindow()
{
/*
* Save Settings
*/
// Get Window Size
QRect windowRect = MainWindow::normalGeometry();
settings.setValue("windowWidth", windowRect.width());
settings.setValue("windowHeight", windowRect.height());
settings.setValue("splitterSizes", ui->splitter->saveState());
delete ui;
}
/*
* Load Disassembly
*/
// Load binary and display disassembly
void MainWindow::loadBinary(QString file){
if (file != ""){
this->setWindowTitle("ObjGUI - " + file);
clearUi();
if (canDisassemble(file)) {
QProgressDialog progress("Loading Disassembly", "", 0, 4, this);
progress.setCancelButton(0);
progress.setWindowModality(Qt::WindowModal);
progress.setMinimumDuration(500);
progress.setValue(0);
// Disassemble in seperate thread
QFuture<void> disassemblyThread = QtConcurrent::run(&disassemblyCore, &DisassemblyCore::disassemble, file);
while (!disassemblyThread.isFinished()){
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
}
progress.setValue(1);
if (!disassemblyCore.disassemblyIsLoaded()){
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);
enableMenuItems();
}
progress.setValue(2);
displayHexData();
progress.setValue(3);
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));
setUpdatesEnabled(true);
// Clear specified target
disassemblyCore.setTarget("");
// Load strings data
ui->stringsAddressBrowser->setPlainText(disassemblyCore.getStringsAddresses());
ui->stringsBrowser->setPlainText(disassemblyCore.getStrings());
progress.setValue(4);
}
}
}
// Disassemble
void MainWindow::on_actionOpen_triggered()
{
// Prompt user for file
QString file = QFileDialog::getOpenFileName(this, tr("Open File"), files.getCurrentDirectory(), tr("All (*)"));
// Update current directory and load file
if (file != ""){
files.setCurrentDirectory(file);
loadBinary(file);
ui->disTabWidget->setCurrentIndex(0);
ui->codeBrowser->setFocus();
}
}
//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);
bool canDisassemble = true;
// If format is ambigous message, let user user select format from list of matching formats
if (!errorMsg.isEmpty()){
if (errorMsg.contains("Matching formats")){
QStringList formats = errorMsg.split(":");
if (formats.length() == 2){
formats = formats.at(1).split(" ", QString::SkipEmptyParts);
if (!formats.isEmpty()){
// Get target format and set flag
QString format = QInputDialog::getItem(this, "Select matching format", "Format is ambigous, select matching format:", formats, 0, false);
disassemblyCore.setTarget("--target=" + format);
} else {
// Display error message
ui->codeBrowser->setPlainText(errorMsg);
canDisassemble = false;
}
}
} else {
// Display error message
ui->codeBrowser->setPlainText(errorMsg);
canDisassemble = false;
}
}
return canDisassemble;
}
/*
* Display Disassembly Data
*/
// Set lables and code browser to display function info and contents
void MainWindow::displayFunctionText(QString functionName){
if (disassemblyCore.disassemblyIsLoaded()){
Function function = disassemblyCore.getFunction(functionName);
setUpdatesEnabled(false);
ui->addressValueLabel->setText(function.getAddress());
ui->fileOffsetValueLabel->setText(function.getFileOffset());
ui->functionLabel->setText(function.getName());
ui->sectionValueLabel->setText(function.getSection());
ui->codeBrowser->setPlainText(function.getContents());
setUpdatesEnabled(true);
int index = disassemblyCore.getFunctionIndex(functionName);
if (index >= 0){
currentFunctionIndex = index;
}
}
}
void MainWindow::displayFunctionText(int functionIndex){
if (disassemblyCore.disassemblyIsLoaded()){
Function function = disassemblyCore.getFunction(functionIndex);
// If index is out of range an empty function will be returned
if (function.getAddress() != ""){
setUpdatesEnabled(false);
ui->addressValueLabel->setText(function.getAddress());
ui->fileOffsetValueLabel->setText(function.getFileOffset());
ui->functionLabel->setText(function.getName());
ui->sectionValueLabel->setText(function.getSection());
ui->codeBrowser->setPlainText(function.getContents());
setUpdatesEnabled(true);
currentFunctionIndex = 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");
else {
QString firstIndexName = disassemblyCore.getFunction(0).getName();
displayFunctionText(firstIndexName);
}
}
}
// Highlight current line of function
void MainWindow::highlightCurrentLine(){
QList<QTextEdit::ExtraSelection> extraSelections;
QTextEdit::ExtraSelection selections;
selections.format.setBackground(lineColor);
selections.format.setProperty(QTextFormat::FullWidthSelection, true);
selections.cursor = ui->codeBrowser->textCursor();
selections.cursor.clearSelection();
extraSelections.append(selections);
ui->codeBrowser->setExtraSelections(extraSelections);
}
void MainWindow::displayHexData(){
// Set hex view values
setUpdatesEnabled(false);
ui->hexAddressBrowser->setPlainText(disassemblyCore.getSectionAddressDump());
ui->hexBrowser->setPlainText(disassemblyCore.getSectionHexDump());
setUpdatesEnabled(true);
}
void MainWindow::clearUi(){
while (ui->functionList->count() > 0){
ui->functionList->takeItem(0);
}
ui->addressValueLabel->clear();
ui->fileOffsetValueLabel->clear();
ui->functionLabel->clear();
ui->sectionValueLabel->clear();
ui->codeBrowser->clear();
ui->hexAddressBrowser->clear();
ui->hexBrowser->clear();
ui->fileFormatlabel->clear();
ui->symbolsBrowser->clear();
ui->relocationsBrowser->clear();
ui->headersBrowser->clear();
ui->stringsAddressBrowser->clear();
ui->stringsBrowser->clear();
// Clear history
history.clear();
}
void MainWindow::enableMenuItems(){
// Enable navigation and tools
ui->actionGo_To_Address->setEnabled(true);
ui->actionGo_to_Address_at_Cursor->setEnabled(true);
ui->actionGet_Offset->setEnabled(true);
ui->actionGet_File_Offset_of_Current_Line->setEnabled(true);
ui->actionFind_References->setEnabled(true);
ui->actionFind_Calls_to_Current_Function->setEnabled(true);
ui->actionFind_Calls_to_Current_Location->setEnabled(true);
}
/*
* Navigation
*/
// Go to virtual memory address
void MainWindow::goToAddress(QString targetAddress){
if (targetAddress != ""){
if (!targetAddress.startsWith("0x")){
targetAddress = "0x" + targetAddress;
}
// Find address index
QVector<int> location = disassemblyCore.getAddressLocation(targetAddress);
// Check if address was found
if (location[0] >= 0){
// Add old location to history
QTextCursor prevCursor = ui->codeBrowser->textCursor();
int lineNum = prevCursor.blockNumber();
addToHistory(currentFunctionIndex, lineNum);
setUpdatesEnabled(false);
// Display function
if (location[0] != currentFunctionIndex){
displayFunctionText(location[0]);
ui->functionList->setCurrentRow(location[0]);
}
// Go to Line
QTextCursor cursor(ui->codeBrowser->document()->findBlockByLineNumber(location[1]));
ui->codeBrowser->setTextCursor(cursor);
ui->disTabWidget->setCurrentIndex(0);
ui->codeBrowser->setFocus();
setUpdatesEnabled(true);
// Add new location to history
addToHistory(currentFunctionIndex, location[1]);
} else {
// Search strings
int stringsIndex = disassemblyCore.getStringIndexByAddress(targetAddress);
if (stringsIndex >= 0){
ui->infoTabWidget->setCurrentIndex(0);
QTextCursor cursor(ui->stringsBrowser->document()->findBlockByLineNumber(stringsIndex));
cursor.select(QTextCursor::LineUnderCursor);
ui->stringsBrowser->setTextCursor(cursor);
ui->stringsBrowser->setFocus();
} else {
QMessageBox::warning(this, tr("Go to Address"), "Address not found.",QMessageBox::Ok);
}
}
}
}
// Go to Address triggered
void MainWindow::on_actionGo_To_Address_triggered()
{
bool ok = true;
QString targetAddress = QInputDialog::getText(this, tr("Go to Address"),tr("Address"), QLineEdit::Normal,"", &ok).trimmed();
if (ok)
goToAddress(targetAddress);
}
// Go to Address at Cursor triggered
void MainWindow::on_actionGo_to_Address_at_Cursor_triggered()
{
QTextCursor cursor = ui->codeBrowser->textCursor();
cursor.select(QTextCursor::WordUnderCursor);
QString targetAddress = cursor.selectedText();
goToAddress(targetAddress);
}
// Display function clicked in sidebar
void MainWindow::on_functionList_itemDoubleClicked(QListWidgetItem *item)
{
// Display function
displayFunctionText(item->text());
ui->disTabWidget->setCurrentIndex(0);
// Add new location to history
addToHistory(currentFunctionIndex, 0);
}
// Get file offset of current line of disassembly
void MainWindow::on_actionGet_Offset_triggered()
{
bool ok;
QString targetAddress = QInputDialog::getText(this, tr("Get File Offset"),tr("Address"), QLineEdit::Normal,"", &ok).trimmed();
if (ok && !targetAddress.isEmpty()){
// Get file offset of address
QVector<QString> offset = disassemblyCore.getFileOffset(targetAddress);
if(!offset[0].isEmpty()){
QString offsetMsg = "File Offset of Address " + targetAddress+ "\nHex: " + offset[0] + "\nInt: " + offset[1];
QMessageBox::information(this, tr("File Offset"), offsetMsg,QMessageBox::Close);
} else {
QMessageBox::warning(this, tr("File Offset"), "Invalid address.",QMessageBox::Close);
}
} else {
QMessageBox::warning(this, tr("File Offset"), "No address entered.",QMessageBox::Close);
}
}
// Get Offset of Current Line triggered
void MainWindow::on_actionGet_File_Offset_of_Current_Line_triggered()
{
if (disassemblyCore.disassemblyIsLoaded()){
int currentTab = ui->disTabWidget->currentIndex();
QString offsetMsg = "";
if (currentTab == 0 && ui->codeBrowser->hasFocus()){
Function function = disassemblyCore.getFunction(currentFunctionIndex);
QTextCursor cursor = ui->codeBrowser->textCursor();
int lineNum = cursor.blockNumber();
// Get address
QString currentLineAddressStr = function.getAddressAt(lineNum);
if (!currentLineAddressStr.isEmpty()){
// Get file offset of address
QVector<QString> offset = disassemblyCore.getFileOffset(currentLineAddressStr);
offsetMsg = "File Offset of Address " + currentLineAddressStr + "\nHex: " + offset[0] + "\nInt: " + offset[1];
}
} else if (ui->infoTabWidget->currentIndex() == 0 && ui->stringsBrowser->hasFocus()){
QTextCursor cursor = ui->stringsBrowser->textCursor();
int lineNum = cursor.blockNumber();
// Get address
QString currentLineAddressStr = disassemblyCore.getStringAddressAt(lineNum);
if (!currentLineAddressStr.isEmpty()){
// Get file offset of address
QVector<QString> offset = disassemblyCore.getFileOffset(currentLineAddressStr);
offsetMsg = "File Offset of Address " + currentLineAddressStr + "\nHex: " + offset[0] + "\nInt: " + offset[1];
}
}
if (!offsetMsg.isEmpty())
QMessageBox::information(this, tr("File Offset"), offsetMsg,QMessageBox::Close);
}
}
/*
* History
*/
// Add location to history and update iterator
void MainWindow::addToHistory(int functionIndex, int lineNum){
QVector<int> item(2);
item[0] = functionIndex;
item[1] = lineNum;
// Note: constEnd() points to imaginary item after last item
if (historyIterator != history.constEnd() - 1)
history = history.mid(0, historyIterator - history.constBegin() + 1);
history.append(item);
historyIterator = history.constEnd() - 1;
}
// Back button
void MainWindow::on_backButton_clicked()
{
if (!history.isEmpty() && historyIterator != history.constBegin()){
historyIterator--;
QVector<int> prevLocation = historyIterator.i->t();
// Display prev function
setUpdatesEnabled(false);
if (currentFunctionIndex != prevLocation[0]){
displayFunctionText(prevLocation[0]);
ui->functionList->setCurrentRow(prevLocation[0]);
}
// Go to prev line
QTextCursor cursor(ui->codeBrowser->document()->findBlockByLineNumber(prevLocation[1]));
ui->codeBrowser->setTextCursor(cursor);
ui->disTabWidget->setCurrentIndex(0);
ui->codeBrowser->setFocus();
setUpdatesEnabled(true);
}
}
// Forward button
void MainWindow::on_forwardButton_clicked()
{
if (!history.isEmpty() && historyIterator != history.constEnd() - 1){
historyIterator++;
QVector<int> nextLocation = historyIterator.i->t();
// Display prev function
setUpdatesEnabled(false);
if (currentFunctionIndex != nextLocation[0]){
displayFunctionText(nextLocation[0]);
ui->functionList->setCurrentRow(nextLocation[0]);
}
// Go to prev line
QTextCursor cursor(ui->codeBrowser->document()->findBlockByLineNumber(nextLocation[1]));
ui->codeBrowser->setTextCursor(cursor);
ui->disTabWidget->setCurrentIndex(0);
ui->codeBrowser->setFocus();
setUpdatesEnabled(true);
}
}
void MainWindow::on_actionBack_triggered()
{
on_backButton_clicked();
}
void MainWindow::on_actionForward_triggered()
{
on_forwardButton_clicked();
}
/*
* Searching
*/
void MainWindow::displayResults(QVector< QVector<QString> > results, QString resultsLabel){
if (!results.isEmpty()){
QString resultsStr = "";
for (int i = 0; i < results.length(); i++){
QVector<QString> result = results[i];
if (result.length() == 2)
resultsStr.append(result[1] + " " + result[0] + "\n");
}
// Display results
ResultsDialog resultsDialog;
resultsDialog.setWindowModality(Qt::WindowModal);
resultsDialog.setResultsLabelText(resultsLabel);
resultsDialog.setResultsText(resultsStr);
resultsDialog.exec();
}
}
// Find calls to the current function
void MainWindow::on_actionFind_Calls_to_Current_Function_triggered()
{
QString functionName = disassemblyCore.getFunction(currentFunctionIndex).getName();
QVector< QVector<QString> > results = disassemblyCore.findCallsToFunction(functionName);
if (!results.isEmpty()){
// Display results
displayResults(results, "Calls to function " + functionName);
} else {
QMessageBox::information(this, tr("Calls to Function"), "No calls found to function " + functionName,QMessageBox::Close);
}
}
// Find all references to a target location
void MainWindow::findReferencesToLocation(QString target){
if (!target.isEmpty()){
QVector< QVector<QString> > results = disassemblyCore.findReferences(target);
if (!results.isEmpty()){
// Display results
displayResults(results, "References to " + target);
} else {
QMessageBox::information(this, tr("References"), "No references found to " + target,QMessageBox::Close);
}
} else {
QMessageBox::warning(this, tr("Search failed"), "Cannot search for empty string.",QMessageBox::Close);
}
}
// Find References
void MainWindow::on_actionFind_References_triggered()
{
bool ok = true;
QString targetAddress = QInputDialog::getText(this, tr("Find References"),tr("Find References to"), QLineEdit::Normal,"", &ok).trimmed();
if (ok)
findReferencesToLocation(targetAddress);
}
// Find all calls to current location
void MainWindow::on_actionFind_Calls_to_Current_Location_triggered(){
if (disassemblyCore.disassemblyIsLoaded()){
QTextCursor cursor = ui->codeBrowser->textCursor();
int lineNum = cursor.blockNumber();
QString targetLocation = disassemblyCore.getFunction(currentFunctionIndex).getAddressAt(lineNum).mid(2);
QVector< QVector<QString> > results = disassemblyCore.findReferences(targetLocation);
if (!results.isEmpty()){
// Display results
displayResults(results, "Calls to address " + targetLocation);
} else {
QMessageBox::information(this, tr("Calls to address"), "No calls found to address " + targetLocation,QMessageBox::Close);
}
}
}
// Toggle searchbar
void MainWindow::on_actionFind_2_triggered()
{
if (ui->searchBar->isHidden()){
ui->searchBar->show();
ui->findLineEdit->setFocus();
}else {
ui->searchBar->hide();
}
}
// Find and highlight search term in the target widget
void MainWindow::find(QString searchTerm, QPlainTextEdit *targetWidget, bool searchBackwords){
if (targetWidget != NULL){
QTextCursor cursor = targetWidget->textCursor();
int currentPosition = cursor.position();
bool found = false;
// Start new search from begining of document
if (searchTerm != currentSearchTerm){
if (!searchBackwords)
cursor.movePosition(QTextCursor::Start);
else
cursor.movePosition(QTextCursor::End);
targetWidget->setTextCursor(cursor);
currentSearchTerm = searchTerm;
if (!searchBackwords)
found = targetWidget->find(searchTerm);
else
found = targetWidget->find(searchTerm, QTextDocument::FindBackward);
// Call vertical scrollbar value changed to keep widgets scrolling synced
targetWidget->verticalScrollBar()->valueChanged(targetWidget->verticalScrollBar()->value());
// If not found move cursor back to original position and display not found message
if(!found){
cursor.setPosition(currentPosition);
targetWidget->setTextCursor(cursor);
QMessageBox::information(this, tr("Not Found"), "\"" + searchTerm + "\" not found.", QMessageBox::Close);
}
} else {
if (!searchBackwords)
found = targetWidget->find(searchTerm);
else
found = targetWidget->find(searchTerm, QTextDocument::FindBackward);
// Call vertical scrollbar value changed to keep widgets scrolling synced
targetWidget->verticalScrollBar()->valueChanged(targetWidget->verticalScrollBar()->value());
// If not found wrap to begining and search again
if (!found){
if (!searchBackwords)
cursor.movePosition(QTextCursor::Start);
else
cursor.movePosition(QTextCursor::End);
targetWidget->setTextCursor(cursor);
if (!searchBackwords)
found = targetWidget->find(searchTerm);
else
found = targetWidget->find(searchTerm, QTextDocument::FindBackward);
// Call vertical scrollbar value changed to keep widgets scrolling synced
targetWidget->verticalScrollBar()->valueChanged(targetWidget->verticalScrollBar()->value());
if (!found){
cursor.setPosition(currentPosition);
targetWidget->setTextCursor(cursor);
QMessageBox::information(this, tr("Not Found"), "\"" + searchTerm + "\" not found.", QMessageBox::Close);
}
}
}
}
}
void MainWindow::on_findButton_clicked()
{
QString searchTerm = ui->findLineEdit->text();
int currentTabIndex = ui->disTabWidget->currentIndex();
QPlainTextEdit *targetWidget = NULL;
// Set pointer to target widget given current tab index
switch (currentTabIndex) {
case 0:
targetWidget = ui->codeBrowser;
break;
case 1:
targetWidget = ui->hexBrowser;
break;
default:
break;
}
find(searchTerm, targetWidget, false);
}
void MainWindow::on_findLineEdit_returnPressed()
{
on_findButton_clicked();
}
// Find Prev (search backwards)
void MainWindow::on_findPrevButton_clicked()
{
QString searchTerm = ui->findLineEdit->text();
int currentTabIndex = ui->disTabWidget->currentIndex();
QPlainTextEdit *targetWidget = NULL;
// Set pointer to target widget given current tab index
switch (currentTabIndex) {
case 0:
targetWidget = ui->codeBrowser;
break;
case 1:
targetWidget = ui->hexBrowser;
break;
default:
break;
}
find(searchTerm, targetWidget, true);
}
void MainWindow::on_stringsSearchBar_returnPressed()
{
QString searchTerm = ui->stringsSearchBar->text();
QPlainTextEdit *stringsBrowser = ui->stringsBrowser;
find(searchTerm, stringsBrowser, false);
}
/*
* Options
*/
void MainWindow::on_actionIntel_triggered()
{
settings.setValue("syntax", "intel");
disassemblyCore.setOutputSyntax("intel");
ui->actionIntel->setChecked(true);
ui->actionAtt->setChecked(false);
ui->syntaxComboBox->setCurrentIndex(0);
}
void MainWindow::on_actionAtt_triggered()
{
settings.setValue("syntax", "att");
disassemblyCore.setOutputSyntax("att");
ui->actionAtt->setChecked(true);
ui->actionIntel->setChecked(false);
ui->syntaxComboBox->setCurrentIndex(1);
}
// Syntax Option ComboBox
void MainWindow::on_syntaxComboBox_currentIndexChanged(int index)
{
if (index == 0){
on_actionIntel_triggered();
}else if(index == 1){
on_actionAtt_triggered();
}
}
void MainWindow::on_disassemblyFlagcheckBox_toggled(bool checked)
{
if (checked)
disassemblyCore.setDisassemblyFlag("-D");
else
disassemblyCore.setDisassemblyFlag("-d");
}
void MainWindow::on_demanlgeCheckBox_toggled(bool checked)
{
if (checked){
disassemblyCore.setDemangleFlag("-C");