cutter/src/dialogs/XrefsDialog.cpp

231 lines
6.7 KiB
C++
Raw Normal View History

2017-10-01 19:09:42 +00:00
#include "XrefsDialog.h"
#include "ui_XrefsDialog.h"
#include "utils/TempConfig.h"
2017-10-01 19:09:42 +00:00
#include "MainWindow.h"
#include <QJsonArray>
XrefsDialog::XrefsDialog(QWidget *parent) :
QDialog(parent),
2017-10-01 18:08:12 +00:00
addr(0),
func_name(QString::null),
ui(new Ui::XrefsDialog),
2017-10-09 18:08:35 +00:00
core(CutterCore::getInstance())
{
ui->setupUi(this);
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
// Increase asm text edit margin
QTextDocument *asm_docu = ui->previewTextEdit->document();
asm_docu->setDocumentMargin(10);
setupPreviewFont();
setupPreviewColors();
// Highlight current line
connect(ui->previewTextEdit, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(setupPreviewFont()));
connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(setupPreviewColors()));
}
2017-10-02 09:41:28 +00:00
XrefsDialog::~XrefsDialog() {}
void XrefsDialog::fillRefs(QList<XrefDescription> refs, QList<XrefDescription> xrefs)
2017-04-09 19:55:06 +00:00
{
2017-11-27 11:18:47 +00:00
/* Fill refs */
ui->fromTreeWidget->clear();
2017-04-09 19:55:06 +00:00
for (int i = 0; i < refs.size(); ++i)
{
XrefDescription xref = refs[i];
2017-06-07 15:48:36 +00:00
QTreeWidgetItem *tempItem = new QTreeWidgetItem();
2017-11-27 16:05:10 +00:00
tempItem->setText(0, xref.to_str);
tempItem->setText(1, core->disassembleSingleInstruction(xref.to));
2017-11-27 11:18:47 +00:00
tempItem->setText(2, xrefTypeString(xref.type));
ui->fromTreeWidget->insertTopLevelItem(0, tempItem);
}
// Adjust columns to content
int count = ui->fromTreeWidget->columnCount();
2017-04-09 19:55:06 +00:00
for (int i = 0; i != count; ++i)
{
ui->fromTreeWidget->resizeColumnToContents(i);
}
2017-11-27 11:18:47 +00:00
/* Fill Xrefs */
ui->toTreeWidget->clear();
2017-04-09 19:55:06 +00:00
for (int i = 0; i < xrefs.size(); ++i)
{
XrefDescription xref = xrefs[i];
2017-06-07 15:48:36 +00:00
QTreeWidgetItem *tempItem = new QTreeWidgetItem();
2017-11-27 16:05:10 +00:00
tempItem->setText(0, xref.from_str);
2017-10-09 18:08:35 +00:00
tempItem->setText(1, core->disassembleSingleInstruction(xref.from));
2017-11-27 11:18:47 +00:00
tempItem->setText(2, xrefTypeString(xref.type));
ui->toTreeWidget->insertTopLevelItem(0, tempItem);
}
// Adjust columns to content
int count2 = ui->toTreeWidget->columnCount();
2017-04-09 19:55:06 +00:00
for (int i = 0; i != count2; ++i)
{
ui->toTreeWidget->resizeColumnToContents(i);
}
}
void XrefsDialog::on_fromTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
2017-10-01 18:08:12 +00:00
Q_UNUSED(column);
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
2017-10-22 07:31:30 +00:00
Core()->seek(xref.to);
this->close();
}
void XrefsDialog::on_toTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
{
2017-10-01 18:08:12 +00:00
Q_UNUSED(column);
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
2017-10-22 07:31:30 +00:00
Core()->seek(xref.from);
this->close();
}
2017-10-01 18:08:12 +00:00
QString XrefsDialog::normalizeAddr(const QString& addr) const
2017-04-09 19:55:06 +00:00
{
2017-11-27 11:18:47 +00:00
QString r = addr;
QString base = addr.split("0x")[1].trimmed();
int len = base.length();
2017-04-09 19:55:06 +00:00
if (len < 8)
{
int padding = 8 - len;
QString zero = "0";
QString zeroes = zero.repeated(padding);
2017-11-27 11:18:47 +00:00
r = "0x" + zeroes + base;
}
2017-11-27 11:18:47 +00:00
return r;
}
void XrefsDialog::setupPreviewFont()
{
ui->previewTextEdit->setFont(Config()->getFont());
}
void XrefsDialog::setupPreviewColors()
{
ui->previewTextEdit->setStyleSheet(QString("QPlainTextEdit { background-color: %1; color: %2; }")
.arg(ConfigColor("gui.background").name())
.arg(ConfigColor("btext").name()));
}
2017-04-09 19:55:06 +00:00
void XrefsDialog::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
2017-04-09 19:55:06 +00:00
if (ui->previewTextEdit->isReadOnly())
{
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(190, 144, 212);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = ui->previewTextEdit->textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
ui->previewTextEdit->setExtraSelections(extraSelections);
}
}
void XrefsDialog::on_fromTreeWidget_itemSelectionChanged()
{
if (ui->fromTreeWidget->selectedItems().isEmpty())
return;
ui->toTreeWidget->clearSelection();
QTreeWidgetItem *item = ui->fromTreeWidget->currentItem();
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
updatePreview(xref.to);
}
void XrefsDialog::on_toTreeWidget_itemSelectionChanged()
{
if (ui->toTreeWidget->selectedItems().isEmpty())
return;
ui->fromTreeWidget->clearSelection();
QTreeWidgetItem *item = ui->toTreeWidget->currentItem();
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
updatePreview(xref.from);
}
void XrefsDialog::updatePreview(RVA addr)
{
// is the address part of a function, so we can use pdf?
bool isFunction = !core->cmdj("afij@" + QString::number(addr)).array().isEmpty();
TempConfig tempConfig;
tempConfig.set("scr.html", true);
tempConfig.set("scr.color", true);
QString disass;
if (isFunction)
2017-10-09 18:08:35 +00:00
disass = core->cmd("pdf @ " + QString::number(addr));
else
2017-10-09 18:08:35 +00:00
disass = core->cmd("pd 10 @ " + QString::number(addr));
ui->previewTextEdit->document()->setHtml(disass);
// Does it make any sense?
ui->previewTextEdit->moveCursor(QTextCursor::End);
ui->previewTextEdit->find(this->normalizeAddr(RAddressString(addr)), QTextDocument::FindBackward);
ui->previewTextEdit->moveCursor(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
}
2017-04-09 19:55:06 +00:00
void XrefsDialog::updateLabels(QString name)
{
2017-11-27 16:05:10 +00:00
ui->label_xTo->setText(tr("X-Refs to %1:").arg(name));
ui->label_xFrom->setText(tr("X-Refs from %1:").arg(name));
}
2017-06-07 15:48:36 +00:00
void XrefsDialog::fillRefsForAddress(RVA addr, QString name, bool whole_function)
2017-06-07 15:48:36 +00:00
{
TempConfig tempConfig;
tempConfig.set("scr.html", false);
tempConfig.set("scr.color", false);
this->addr = addr;
this->func_name = func_name;
setWindowTitle(tr("X-Refs for %1").arg(name));
2017-06-07 15:48:36 +00:00
updateLabels(name);
// Get Refs and Xrefs
// refs = calls q hace esa funcion
2017-10-09 18:08:35 +00:00
QList<XrefDescription> refs = core->getXRefs(addr, false, whole_function);
2017-06-07 15:48:36 +00:00
// xrefs = calls a esa funcion
2017-10-09 18:08:35 +00:00
QList<XrefDescription> xrefs = core->getXRefs(addr, true, whole_function);
2017-06-07 15:48:36 +00:00
fillRefs(refs, xrefs);
2017-10-01 18:08:12 +00:00
}
2017-11-27 11:18:47 +00:00
QString XrefsDialog::xrefTypeString(const QString &type)
{
switch (type.toStdString()[0]) {
case R_ANAL_REF_TYPE_CALL:
return QString("Call");
case R_ANAL_REF_TYPE_CODE:
return QString("Code");
case R_ANAL_REF_TYPE_DATA:
return QString("Data");
case R_ANAL_REF_TYPE_NULL:
return QString("");
case R_ANAL_REF_TYPE_STRING:
return QString("String");
}
return type;
}