2017-10-01 19:09:42 +00:00
|
|
|
#include "XrefsDialog.h"
|
|
|
|
#include "ui_XrefsDialog.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-10-17 07:55:53 +00:00
|
|
|
#include "common/TempConfig.h"
|
|
|
|
#include "common/Helpers.h"
|
2017-11-20 20:11:56 +00:00
|
|
|
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/MainWindow.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-06-07 19:35:38 +00:00
|
|
|
#include <QJsonArray>
|
|
|
|
|
2017-10-10 10:17:05 +00:00
|
|
|
XrefsDialog::XrefsDialog(QWidget *parent) :
|
2017-03-29 10:18:37 +00:00
|
|
|
QDialog(parent),
|
2017-10-01 18:08:12 +00:00
|
|
|
addr(0),
|
|
|
|
func_name(QString::null),
|
2019-01-16 11:50:27 +00:00
|
|
|
ui(new Ui::XrefsDialog)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2017-03-31 00:51:14 +00:00
|
|
|
setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2019-01-02 19:58:02 +00:00
|
|
|
// Modify the splitter's location to show more Disassembly instead of empty space. Not possible via Designer
|
|
|
|
ui->splitter->setSizes(QList<int>() << 100 << 200);
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
// Increase asm text edit margin
|
|
|
|
QTextDocument *asm_docu = ui->previewTextEdit->document();
|
|
|
|
asm_docu->setDocumentMargin(10);
|
|
|
|
|
2017-11-20 20:11:56 +00:00
|
|
|
setupPreviewFont();
|
|
|
|
setupPreviewColors();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
// Highlight current line
|
|
|
|
connect(ui->previewTextEdit, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
|
2017-11-20 20:11:56 +00:00
|
|
|
connect(Config(), SIGNAL(fontsUpdated()), this, SLOT(setupPreviewFont()));
|
|
|
|
connect(Config(), SIGNAL(colorsUpdated()), this, SLOT(setupPreviewColors()));
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 11:50:27 +00:00
|
|
|
XrefsDialog::~XrefsDialog() { }
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-06-07 19:35:38 +00:00
|
|
|
void XrefsDialog::fillRefs(QList<XrefDescription> refs, QList<XrefDescription> xrefs)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2019-01-16 11:50:27 +00:00
|
|
|
// Fill refs
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->fromTreeWidget->clear();
|
2019-01-16 11:50:27 +00:00
|
|
|
for (const auto &xref : refs) {
|
|
|
|
auto *tempItem = new QTreeWidgetItem();
|
2017-11-27 16:05:10 +00:00
|
|
|
tempItem->setText(0, xref.to_str);
|
2019-01-16 11:50:27 +00:00
|
|
|
tempItem->setText(1, Core()->disassembleSingleInstruction(xref.to));
|
2017-11-27 11:18:47 +00:00
|
|
|
tempItem->setText(2, xrefTypeString(xref.type));
|
2017-11-28 11:26:52 +00:00
|
|
|
tempItem->setData(0, Qt::UserRole, QVariant::fromValue(xref));
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->fromTreeWidget->insertTopLevelItem(0, tempItem);
|
|
|
|
}
|
2019-01-16 11:50:27 +00:00
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
// Adjust columns to content
|
2018-04-01 08:25:31 +00:00
|
|
|
qhelpers::adjustColumns(ui->fromTreeWidget, 0);
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2019-01-16 11:50:27 +00:00
|
|
|
// Fill Xrefs
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->toTreeWidget->clear();
|
2019-01-16 11:50:27 +00:00
|
|
|
for (const auto &xref : xrefs) {
|
|
|
|
auto *tempItem = new QTreeWidgetItem();
|
2017-11-27 16:05:10 +00:00
|
|
|
tempItem->setText(0, xref.from_str);
|
2019-01-16 11:50:27 +00:00
|
|
|
tempItem->setText(1, Core()->disassembleSingleInstruction(xref.from));
|
2017-11-27 11:18:47 +00:00
|
|
|
tempItem->setText(2, xrefTypeString(xref.type));
|
2017-11-28 11:26:52 +00:00
|
|
|
tempItem->setData(0, Qt::UserRole, QVariant::fromValue(xref));
|
2017-03-29 10:18:37 +00:00
|
|
|
ui->toTreeWidget->insertTopLevelItem(0, tempItem);
|
|
|
|
}
|
2019-01-16 11:50:27 +00:00
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
// Adjust columns to content
|
2018-04-01 08:25:31 +00:00
|
|
|
qhelpers::adjustColumns(ui->toTreeWidget, 0);
|
2019-03-27 20:40:54 +00:00
|
|
|
|
|
|
|
// try to select first item from refs or xrefs
|
|
|
|
if (!qhelpers::selectFirstItem(ui->toTreeWidget)) {
|
|
|
|
qhelpers::selectFirstItem(ui->fromTreeWidget);
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XrefsDialog::on_fromTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
|
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(column);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2017-06-07 19:35:38 +00:00
|
|
|
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
|
2017-10-22 07:31:30 +00:00
|
|
|
Core()->seek(xref.to);
|
2017-03-29 10:18:37 +00:00
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XrefsDialog::on_toTreeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
|
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(column);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2017-06-07 19:35:38 +00:00
|
|
|
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
|
2017-10-22 07:31:30 +00:00
|
|
|
Core()->seek(xref.from);
|
2017-03-29 10:18:37 +00:00
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
QString XrefsDialog::normalizeAddr(const QString &addr) const
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2019-03-23 10:54:34 +00:00
|
|
|
QString ret = addr;
|
|
|
|
if (addr.length() < 10) {
|
|
|
|
ret = ret.mid(3).rightJustified(8, QLatin1Char('0'));
|
|
|
|
ret.prepend(QLatin1Literal("0x"));
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2019-03-23 10:54:34 +00:00
|
|
|
return ret;
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 20:11:56 +00:00
|
|
|
void XrefsDialog::setupPreviewFont()
|
|
|
|
{
|
|
|
|
ui->previewTextEdit->setFont(Config()->getFont());
|
|
|
|
}
|
|
|
|
|
|
|
|
void XrefsDialog::setupPreviewColors()
|
|
|
|
{
|
|
|
|
ui->previewTextEdit->setStyleSheet(QString("QPlainTextEdit { background-color: %1; color: %2; }")
|
2018-03-21 20:32:32 +00:00
|
|
|
.arg(ConfigColor("gui.background").name())
|
|
|
|
.arg(ConfigColor("btext").name()));
|
2017-11-20 20:11:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
void XrefsDialog::highlightCurrentLine()
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
QList<QTextEdit::ExtraSelection> extraSelections;
|
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
if (ui->previewTextEdit->isReadOnly()) {
|
2019-01-16 11:50:27 +00:00
|
|
|
QTextEdit::ExtraSelection selection = QTextEdit::ExtraSelection();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-07-02 09:11:06 +00:00
|
|
|
selection.format.setBackground(ConfigColor("highlight"));
|
2017-03-29 10:18:37 +00:00
|
|
|
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()
|
|
|
|
{
|
2019-01-16 11:50:27 +00:00
|
|
|
if (ui->fromTreeWidget->selectedItems().isEmpty()) {
|
2017-06-07 19:35:38 +00:00
|
|
|
return;
|
2019-01-16 11:50:27 +00:00
|
|
|
}
|
2017-06-07 19:35:38 +00:00
|
|
|
ui->toTreeWidget->clearSelection();
|
2017-03-29 10:18:37 +00:00
|
|
|
QTreeWidgetItem *item = ui->fromTreeWidget->currentItem();
|
2017-06-07 19:35:38 +00:00
|
|
|
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
|
|
|
|
updatePreview(xref.to);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void XrefsDialog::on_toTreeWidget_itemSelectionChanged()
|
|
|
|
{
|
2019-01-16 11:50:27 +00:00
|
|
|
if (ui->toTreeWidget->selectedItems().isEmpty()) {
|
2017-06-07 19:35:38 +00:00
|
|
|
return;
|
2019-01-16 11:50:27 +00:00
|
|
|
}
|
2017-06-07 19:35:38 +00:00
|
|
|
ui->fromTreeWidget->clearSelection();
|
2017-03-29 10:18:37 +00:00
|
|
|
QTreeWidgetItem *item = ui->toTreeWidget->currentItem();
|
2017-06-07 19:35:38 +00:00
|
|
|
XrefDescription xref = item->data(0, Qt::UserRole).value<XrefDescription>();
|
|
|
|
updatePreview(xref.from);
|
|
|
|
}
|
|
|
|
|
|
|
|
void XrefsDialog::updatePreview(RVA addr)
|
|
|
|
{
|
2017-11-20 20:11:56 +00:00
|
|
|
TempConfig tempConfig;
|
|
|
|
tempConfig.set("scr.html", true);
|
2018-03-01 16:06:13 +00:00
|
|
|
tempConfig.set("scr.color", COLOR_MODE_16M);
|
2017-11-20 20:11:56 +00:00
|
|
|
|
2019-01-16 11:50:27 +00:00
|
|
|
QString disas = Core()->cmd("pd--20 @ " + QString::number(addr));
|
|
|
|
ui->previewTextEdit->document()->setHtml(disas);
|
2017-06-07 19:35:38 +00:00
|
|
|
|
|
|
|
// Does it make any sense?
|
2019-01-16 11:50:27 +00:00
|
|
|
ui->previewTextEdit->find(normalizeAddr(RAddressString(addr)), QTextDocument::FindBackward);
|
2017-11-20 20:11:56 +00:00
|
|
|
ui->previewTextEdit->moveCursor(QTextCursor::StartOfLine, QTextCursor::MoveAnchor);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2017-04-04 11:19:34 +00:00
|
|
|
|
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-04-04 11:19:34 +00:00
|
|
|
}
|
2017-06-07 15:48:36 +00:00
|
|
|
|
2017-06-08 22:40:43 +00:00
|
|
|
void XrefsDialog::fillRefsForAddress(RVA addr, QString name, bool whole_function)
|
2017-06-07 15:48:36 +00:00
|
|
|
{
|
2017-11-20 20:11:56 +00:00
|
|
|
TempConfig tempConfig;
|
|
|
|
tempConfig.set("scr.html", false);
|
2018-03-01 16:06:13 +00:00
|
|
|
tempConfig.set("scr.color", COLOR_MODE_DISABLED);
|
2017-11-20 20:11:56 +00:00
|
|
|
|
2017-06-08 22:40:43 +00:00
|
|
|
setWindowTitle(tr("X-Refs for %1").arg(name));
|
2017-06-07 15:48:36 +00:00
|
|
|
updateLabels(name);
|
|
|
|
|
2019-01-16 11:50:27 +00:00
|
|
|
// Get Refs and Xrefs
|
|
|
|
QList<XrefDescription> refs = Core()->getXRefs(addr, false, whole_function);
|
|
|
|
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:
|
2019-03-23 10:54:34 +00:00
|
|
|
return QString();
|
2017-11-27 11:18:47 +00:00
|
|
|
case R_ANAL_REF_TYPE_STRING:
|
|
|
|
return QString("String");
|
2019-01-16 11:50:27 +00:00
|
|
|
default:
|
|
|
|
break;
|
2017-11-27 11:18:47 +00:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
2019-01-16 11:50:27 +00:00
|
|
|
|