cutter/src/AnalTask.cpp

113 lines
3.5 KiB
C++
Raw Normal View History

2018-03-04 17:42:02 +00:00
#include "Cutter.h"
2018-05-26 18:09:20 +00:00
#include "AnalTask.h"
2017-10-01 19:09:42 +00:00
#include "MainWindow.h"
#include "dialogs/OptionsDialog.h"
#include <QJsonArray>
2018-05-05 13:20:14 +00:00
#include <QDebug>
#include <QCheckBox>
2018-05-26 18:09:20 +00:00
AnalTask::AnalTask(OptionsDialog *parent) :
AsyncTask(parent),
level(2),
2018-05-26 18:09:20 +00:00
main(nullptr)
{
}
2018-05-26 18:09:20 +00:00
AnalTask::~AnalTask()
{
}
2018-05-26 18:09:20 +00:00
void AnalTask::setSettings(MainWindow *main, int level, QList<QString> advanced)
{
2018-05-26 18:09:20 +00:00
this->main = main;
this->level = level;
this->advanced = advanced;
}
2018-05-26 18:09:20 +00:00
void AnalTask::interrupt()
{
2018-05-26 18:09:20 +00:00
AsyncTask::interrupt();
r_cons_singleton()->breaked = true;
}
2018-05-26 18:09:20 +00:00
void AnalTask::interruptAndWait()
{
do {
interrupt();
} while(!wait(10));
}
2018-05-26 18:09:20 +00:00
void AnalTask::runTask()
{
const auto optionsDialog = dynamic_cast<OptionsDialog *>(parent());
const auto &ui = optionsDialog->ui;
2018-05-05 13:20:14 +00:00
bool va = ui->vaCheckBox->isChecked();
2018-05-07 15:16:51 +00:00
ut64 binLoadAddr = UT64_MAX; // Where the bin header is located in the file (-B)
if (ui->entry_loadOffset->text().length() > 0)
binLoadAddr = Core()->math(ui->entry_loadOffset->text());
2018-05-05 13:20:14 +00:00
ut64 mapAddr = Core()->math(ui->entry_mapOffset->text()); // Where to map the file once loaded (-m)
2018-05-26 18:09:20 +00:00
2018-05-05 13:20:14 +00:00
emit updateProgress(tr("Loading binary..."));
2018-05-05 13:20:14 +00:00
// Set the CPU details (handle auto)
Core()->setCPU(optionsDialog->getSelectedArch(), optionsDialog->getSelectedCPU(),
2018-03-21 20:32:32 +00:00
optionsDialog->getSelectedBits());
2018-05-05 13:20:14 +00:00
// Binary opening permissions (read/write/execute)
2018-02-27 10:20:48 +00:00
int perms = R_IO_READ | R_IO_EXEC;
if (ui->writeCheckBox->isChecked())
perms |= R_IO_WRITE;
2018-05-05 13:20:14 +00:00
// Check if we must load and parse binary header (ELF, PE, ...)
bool loadBinInfo = !ui->binCheckBox->isChecked();
QString forceBinPlugin = nullptr;
QVariant forceBinPluginData = ui->formatComboBox->currentData();
2018-03-21 20:32:32 +00:00
if (!forceBinPluginData.isNull()) {
RBinPluginDescription pluginDesc = forceBinPluginData.value<RBinPluginDescription>();
forceBinPlugin = pluginDesc.name;
}
2018-05-05 13:20:14 +00:00
// Demangle (must be before file Core()->loadFile)
Core()->setConfig("bin.demangle", ui->demangleCheckBox->isChecked());
2018-05-05 13:20:14 +00:00
// Do not reload the file if already loaded
QJsonArray openedFiles = Core()->getOpenedFiles();
2018-03-21 20:32:32 +00:00
if (!openedFiles.size()) {
bool fileLoaded = Core()->loadFile(main->getFilename(), binLoadAddr, mapAddr, perms, va, loadBinInfo,
2018-03-21 20:32:32 +00:00
forceBinPlugin);
if (!fileLoaded) {
// Something wrong happened, fallback to open dialog
emit openFileFailed();
2018-05-26 18:09:20 +00:00
AsyncTask::interrupt();
return;
}
}
2018-05-05 13:20:14 +00:00
// Set asm OS configuration
QString os = optionsDialog->getSelectedOS();
2018-03-21 20:32:32 +00:00
if (!os.isNull()) {
2018-05-05 13:20:14 +00:00
Core()->cmd("e asm.os=" + os);
}
2018-05-05 13:20:14 +00:00
// Load PDB and/or scripts
2018-03-21 20:32:32 +00:00
if (ui->pdbCheckBox->isChecked()) {
2018-05-05 13:20:14 +00:00
Core()->loadPDB(ui->pdbLineEdit->text());
}
if (ui->scriptCheckBox->isChecked()) {
2018-05-05 13:20:14 +00:00
Core()->loadScript(ui->scriptLineEdit->text());
}
2018-05-05 13:20:14 +00:00
// Set various options
2018-03-21 20:32:32 +00:00
if (optionsDialog->getSelectedEndianness() != OptionsDialog::Endianness::Auto) {
2018-05-05 13:20:14 +00:00
Core()->setEndianness(optionsDialog->getSelectedEndianness() == OptionsDialog::Endianness::Big);
}
2018-05-05 13:20:14 +00:00
Core()->setBBSize(optionsDialog->getSelectedBBSize());
// Use prj.simple as default as long as regular projects are broken
Core()->setConfig("prj.simple", true);
// Start analysis
emit updateProgress(tr("Analysis in progress..."));
Core()->analyze(this->level, this->advanced);
emit updateProgress(tr("Analysis complete!"));
}