-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainwindow.cpp
559 lines (489 loc) · 18 KB
/
mainwindow.cpp
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
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "help.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setFixedSize(QSize(1115, 650)); //Resizing to be added soon :D
ui->pidBox->hide(); //Comment if you want to see the pid of GDB.
ui->fileArchBox->setReadOnly(true);
ui->fileNameBox->setReadOnly(true);
/*The GDB class emits a signal newOutputReady() when readyRead() is emitted from the QProcess instance.
The slot updateOutput() is called everytime, and parses any output from GDB in order to make gdb and Disass
coherent.
updateOutput() parses the output based on the last command sent to GDB. Each send function sets the lastCommand
variable in the GDB class.
*/
connect(&gdbInstance, SIGNAL(newOutputReady()), this, SLOT(updateOutput()));
//Check for the Disass condif directory
QFile config(configFilePath);
if (config.exists())
{
if(config.open(QIODevice::ReadOnly))
{
QTextStream input(&config);
lastDirectoryPath = input.readLine();
}
}
else
{
lastDirectoryPath = QDir::homePath();
}
config.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setUIInteraction(bool state)
{
//This function disables/enables UI buttons.
if (state == true) //GDB is running.
{
ui->atNtButton->setEnabled(true);
ui->intelButton->setEnabled(true);
ui->breakButton->setEnabled(true);
ui->runButton->setEnabled(true);
ui->sendButton->setEnabled(true);
ui->stopButton->setEnabled(true);
ui->commandLineArgumentsBox->setEnabled(true);
ui->stepOverButton->setEnabled(true);
ui->stepIntoButton->setEnabled(true);
ui->nextCodeLineButton->setEnabled(true);
ui->continueButton->setEnabled(true);
ui->actionOpen->setEnabled(false);
ui->action32_Bit->setEnabled(true);
ui->action64_Bit->setEnabled(true);
}
else //GDB isn't running.
{
ui->atNtButton->setEnabled(false);
ui->intelButton->setEnabled(false);
ui->breakButton->setEnabled(false);
ui->runButton->setEnabled(false);
ui->sendButton->setEnabled(false);
ui->stopButton->setEnabled(false);
ui->commandLineArgumentsBox->setEnabled(false);
ui->stepOverButton->setEnabled(false);
ui->stepIntoButton->setEnabled(false);
ui->nextCodeLineButton->setEnabled(false);
ui->continueButton->setEnabled(false);
ui->actionOpen->setEnabled(true);
ui->action32_Bit->setEnabled(false);
ui->action64_Bit->setEnabled(false);
}
}
int MainWindow::checkForArguments(QStringList args)
{
if (args.size() > 1) //If an argument is passed
{
QFileInfo file(args[1]);
if (file.exists())
{ //If the argument passed is an existent file
gdbInstance.start(args[1]);
ui->pidBox->setText(gdbInstance.getPIDString());
setUIInteraction(true); //Enable the UI
//Set the architecture in the ArchBox
ui->fileArchBox->setText(gdbInstance.getArch());
//Get filename and set it in the FileNameBox
ui->fileNameBox->setText(file.fileName());
ui->fileNameBox->setToolTip(args[1]);
ui->gdbOutputBox->append(gdbInstance.getCurrentGDBOutput());
showFileOpenedNotification(file.fileName());
return 1; //Opened GDB to a file being debugged in a GDB instance
}
//If it isn't a file and isn't a help argument
else if (args[1] != "--help" && args[1] != "-h")
{
QMessageBox::information(this, "Invalid Argument", "The specified file doesn't exist, starting to defaults.");
return 2; //Opened Disass but to a default GDB instance
}
}
return 0; //Opened Disass to defaults (no instance running)
}
//Parses the output of "info reg" from gdb
void MainWindow::parseandSetRegistersOutput(QString registersOutput)
{
ui->registerBox->clear();
//Filtering the output
QVector <QString> lines; //Vector to hold lines
QString line; //A single output line
//TODO: Parse these individually and format them better
if (gdbInstance.isx86())
{
for (int i = 0; i < registersOutput.size(); i++)
{
//Push the character if it isn't a newline
if (registersOutput[i] != '\n') line.push_back(registersOutput[i]);
else
{
//Delete the duplicate values of eip
if (line.contains("eip")) line.remove(35, 10);
//Delete the extra spaces in all lines
if (line.contains("eflag")) line.remove(6,7);
else line.remove(3,7);
lines.push_back(line);
line.clear();
}
}
}
else
{
for (int i = 0; i < registersOutput.size(); i++)
{
//Push the character if it isn't a newline
if (registersOutput[i] != '\n') line.push_back(registersOutput[i]);
else
{
//Delete the duplicate values of rip
if (line.contains("rip")) line.remove(35, 14);
//Delete the extra spaces in all lines
if (line.contains("eflag")) line.remove(6,7);
else line.remove(3,7);
lines.push_back(line);
line.clear();
}
}
}
for (int i = 0; i < lines.size(); i++)
ui->registerBox->append(lines[i]);
}
//Parses the output of examining memory before the stack pointer ($rsp/$esp) in gdb
void MainWindow::parseandSetStackOutput(QString stackOutput)
{
ui->stackBox->clear();
//TODO: Verify this parsing
QString word; //Single word from the stack
for (int i = 0; i < stackOutput.size(); i++)
{
//Append the character if it isn't a newline, space, or tab.
if (stackOutput[i] != '\n' && stackOutput[i] != ' ' && stackOutput[i] != '\t') word += stackOutput[i];
else
{
//Append it if it isn't a stack address and isn't any other type of output,
//put a few spaces before it.
if (word.size() != 0 && word != "(gdb)" &&
word[word.size()-1] != ':' && word[0] == '0' && word[1] == 'x')
ui->stackBox->append(" " + word);
//Append only if it is a stack address.
else if (word.size() != 0 && word[word.size()-1] == ':') ui->stackBox->append(word);
word.clear();
}
}
}
//Parses the output of examining instructions after the instuctipn pointer ($rip/$eip) in gdb
void MainWindow::parseandSetAssemblyOutput(QString assemblyOutput)
{
ui->gdbAsmOutputBox->clear();
//TODO: Properly parse this and make it clickable per line.
QVector <QString> lines;
QString line;
for (int i = 0; i < assemblyOutput.size(); i++)
{
if (assemblyOutput[i] != '\n') line.push_back(assemblyOutput[i]);
else
{
lines.push_back(line);
line.clear();
}
}
for (int i = 0; i < lines.size(); i++)
ui->gdbAsmOutputBox->append(lines[i]);
}
//Parses the output of examining code lines (list) after the current line (frame) in gdb
void MainWindow::parseandSetCodeOutput(QString codeOutput)
{
ui->gdbCodeOutputBox->clear();
//TODO: Properly parse this and make it clickable per line.
QVector <QString> lines;
QString line;
for (int i = 0; i < codeOutput.size(); i++)
{
if (codeOutput[i] != '\n') line.push_back(codeOutput[i]);
else
{
lines.push_back(line);
line.clear();
}
}
for (int i = 0; i < lines.size(); i++)
{
//Print "=>" before the current line being executed.
if (lines[i].contains(currentLine)) lines[i].insert(0, "=>");
else lines[i].insert(0, " "); //Align the output by adding 2 spaces.
ui->gdbCodeOutputBox->append(lines[i]);
}
}
//Parses the output of "frame" in gdb to get the current code line being executed
void MainWindow::parseandSetCodeLine(QString codeFrameOutput)
{
//Splitting the string to get rid of the first part of the output.
int newLineIndex = codeFrameOutput.indexOf('\n', 0);
codeFrameOutput.remove(0, newLineIndex+1);
//Stripping anything except the code line from the string.
newLineIndex = codeFrameOutput.indexOf('\t', 0);
codeFrameOutput.remove(newLineIndex,codeFrameOutput.size()-(newLineIndex));
currentLine = codeFrameOutput; //Save the line number
}
//This function examines the stack, assembly instructions, registers, current code line, and code lines from gdb.
void MainWindow::retrieveGDBContext()
{
gdbInstance.examineStack(nStackWords);
gdbInstance.examineAssembly(nInstructions);
gdbInstance.examineRegisters();
// gdbInstance.examineCurrentCodeLine();
// gdbInstance.examineCodeLines(currentLine);
}
//Parses the output of gdb and updates the UI based on the last command executed.
void MainWindow::updateOutput()
{
ui->gdbOutputBox->append(gdbInstance.getCurrentGDBOutput());
//To seperate the output of every step.
ui->gdbOutputBox->append("\n");
if (gdbInstance.getCurrentGDBOutput().contains("exited with code"))
{
on_stopButton_clicked(false);
}
if (gdbInstance.lastCommand == GDBCommands::ExamineStack)
{
parseandSetStackOutput(gdbInstance.getCurrentGDBOutput());
}
else if (gdbInstance.lastCommand == GDBCommands::ExamineAssembly)
{
parseandSetAssemblyOutput(gdbInstance.getCurrentGDBOutput());
}
else if (gdbInstance.lastCommand == GDBCommands::ExamineRegisters)
{
parseandSetRegistersOutput(gdbInstance.getCurrentGDBOutput());
}
// else if (gdbInstance.lastCommand == GDBCommands::ExamineCodeLine)
// {
// parseandSetCodeLine(gdbInstance.getCurrentGDBOutput());
// }
// else if (gdbInstance.lastCommand == GDBCommands::ExamineCode)
// {
// parseandSetCodeOutput(gdbInstance.getCurrentGDBOutput());
// }
}
//Shows a welcome message to the user
void MainWindow::showWelcome()
{
QMessageBox welcome(QMessageBox::Information, "Welcome", "\tWelcome to Disass!\nYou can load an executable from the file menu.");
welcome.setFont(QFont("Sans Serif",10,0,false));
welcome.show();
welcome.exec();
}
//Shows when a file is opened to guide the user. This is temporary and will be removed when a help section is properly created
void MainWindow::showFileOpenedNotification(QString fileName)
{
QMessageBox notification(QMessageBox::Information,"Success",
"\tFile " + fileName + " loaded successfuly.\n"
"You should now:\n"
"- Set a breakpoint from the textbox saying Address/Line, and click on the button next to it.\n"
"- Run the program using the green button at the bottom right.\n"
"- Step through the program using the buttons (Tooltips are available).\n",
QMessageBox::Ok);
notification.setFont(QFont("Sans Serif",10,0,false));
notification.show();
notification.exec();
}
//Sends the typed command to GDB, we don't need to read any output as that already happens using the connected signal and slot
void MainWindow::on_sendButton_clicked()
{
gdbInstance.sendCommand(ui->commandBox->text());
ui->commandBox->clear();
}
//Sets the disassembly to Intel syntax
void MainWindow::on_intelButton_clicked()
{
gdbInstance.sendCommand("set disassembly-flavor intel");
retrieveGDBContext();
}
//Sets the disassembly to AT&T syntax
void MainWindow::on_atNtButton_clicked()
{
gdbInstance.sendCommand("set disassembly-flavor att");
retrieveGDBContext();
}
//Sets a breakpoint on the specified address/line number
void MainWindow::on_breakButton_clicked()
{
QString address = ui->breakBox->text();
gdbInstance.sendCommand("break " + address);
ui->breakBox->clear();
}
//Steps over the current instruction/line
void MainWindow::on_stepOverButton_clicked()
{
if (ui->codeOutputTabs->currentIndex() == outputTabs::Assembly)
{
gdbInstance.sendCommand("nexti");
}
else if (ui->codeOutputTabs->currentIndex() == outputTabs::Code)
{
gdbInstance.sendCommand("next");
}
retrieveGDBContext();
}
//Steps into the current instruction/line
void MainWindow::on_stepIntoButton_clicked()
{
if (ui->codeOutputTabs->currentIndex() == outputTabs::Assembly)
{
gdbInstance.sendCommand("stepi");
}
else if (ui->codeOutputTabs->currentIndex() == outputTabs::Code)
{
gdbInstance.sendCommand("step");
}
retrieveGDBContext();
}
//Executes until the next code line
void MainWindow::on_nextCodeLineButton_clicked()
{
gdbInstance.sendCommand("until");
retrieveGDBContext();
}
//Continues execution until the debuggee exists or a breakpoint is hit
void MainWindow::on_continueButton_clicked()
{
gdbInstance.sendCommand("continue");
retrieveGDBContext();
}
//Runs the specified debuggee
void MainWindow::on_runButton_clicked()
{
QString args = ui->commandLineArgumentsBox->text();
gdbInstance.sendCommand("run " + args);
ui->commandLineArgumentsBox->clear();
ui->runButton->setIcon(QPixmap(":/Controls/Icons/Controls/Restart_Button.png"));
on_intelButton_clicked();
retrieveGDBContext();
retrieveGDBContext();
retrieveGDBContext();
}
//Chooses a binary to be debugged and updates the UI and config files accordingly
void MainWindow::on_actionOpen_triggered()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open Binary"), lastDirectoryPath);
if (filePath.size() == 0)
{
QMessageBox::information(this, tr("Failed"), "Failed to open file, opening GDB to defaults.");
gdbInstance.start();
}
else
{
QFileInfo file(filePath);
showFileOpenedNotification(file.fileName());
gdbInstance.start(filePath);
ui->fileArchBox->setText(gdbInstance.getArch());
ui->fileNameBox->setText(file.fileName());
ui->fileNameBox->setToolTip(filePath);
QFile config(configFilePath);
lastDirectoryPath = file.canonicalPath();
if(config.open(QIODevice::WriteOnly))
{
QTextStream output(&config);
output << lastDirectoryPath;
}
config.close();
}
ui->pidBox->setText(gdbInstance.getPIDString());
setUIInteraction(true); //Enable the UI
}
//Stops a binary from being debugged and updates the UI accordingly
void MainWindow::on_stopButton_clicked(bool clearGDB)
{
//This function kills GDB and clears the UI.
gdbInstance.getQProcess()->close();
gdbInstance.getQProcess()->kill(); //Kill the GDB process
gdbInstance.lastCommand = -1;
setUIInteraction(false); //Disable UI interaction
ui->pidBox->clear();
ui->breakBox->clear();
ui->commandLineArgumentsBox->clear();
ui->gdbAsmOutputBox->clear();
ui->gdbCodeOutputBox->clear();
if (clearGDB) ui->gdbOutputBox->clear();
ui->registerBox->clear();
ui->stackBox->clear();
ui->fileArchBox->clear();
ui->fileNameBox->clear();
ui->fileNameBox->setToolTip("");
ui->runButton->setIcon(QPixmap(":/Controls/Icons/Controls/Start_Button.png"));
}
//Shows a license prompt to the user
void MainWindow::on_actionLicense_triggered()
{
QMessageBox license;
license.setWindowTitle("License and Credit");
license.setText("Disass - Copyright (C) 2022 Ahmed Elmayyah (Satharus)\n\n"
"This program is licensed under GNU GPLv3.\n\n"
"This program comes with ABSOLUTELY NO WARRANTY\n"
"This is free software, and you are welcome to redistribute it "
"under certain conditions.\nFor more info, check the GNU GPLv3 license.");
license.setStandardButtons(QMessageBox::Ok);
license.setDefaultButton(QMessageBox::Ok);
license.setIconPixmap(QPixmap(":/Artwork/Icons/GPLv3LogoSmall.png"));
license.show();
license.exec();
}
//Opens the URL for the repo to the user as well as a messagebox
void MainWindow::on_actionContribute_triggered()
{
QMessageBox::information(this, tr("Contibute"), "Contribute at github.com/Satharus/Disass");
QDesktopServices::openUrl(QUrl("https://github.com/Satharus/Disass"));
}
//Info about Disass
void MainWindow::on_actionAbout_triggered()
{
QMessageBox about;
about.setWindowTitle("About");
about.setText("\t Disass v0.07\nDisass is a free and open source frontend to GDB that aims to make it easier "
"to use for beginners.\n\nDisass uses GDB and the Qt application framework.");
about.setStandardButtons(QMessageBox::Ok);
about.setDefaultButton(QMessageBox::Ok);
about.setIconPixmap(QPixmap(":/Artwork/Icons/BannerSmall.png"));
about.show();
about.exec();
}
void MainWindow::on_actionQuit_triggered()
{
QApplication::quit();
}
//Sets the architecture to 64bit
void MainWindow::on_action64_Bit_triggered()
{
gdbInstance.setArch(ELF_64_STR);
ui->fileArchBox->setText(gdbInstance.getArch());
}
//Sets the architecture to 32bit
void MainWindow::on_action32_Bit_triggered()
{
gdbInstance.setArch(ELF_32_STR);
ui->fileArchBox->setText(gdbInstance.getArch());
}
//Shows the help GIF
void MainWindow::on_actionHow_to_Use_triggered()
{
Help help;
help.setModal(true);
help.exec();
}
void MainWindow::on_codeOutputTabs_currentChanged(int index)
{
//If GDB Output is selected, and a program is being debugged, sendButton is enabled only when a program is being debugged
if (index == outputTabs::GDBOutput && ui->sendButton->isEnabled())
{
ui->stepIntoButton->setEnabled(false);
ui->stepOverButton->setEnabled(false);
ui->nextCodeLineButton->setEnabled(false);
}
//If a program is being debugged.
else if (ui->sendButton->isEnabled())
{
ui->stepIntoButton->setEnabled(true);
ui->stepOverButton->setEnabled(true);
ui->nextCodeLineButton->setEnabled(true);
}
}