Skip to content
Permalink
Browse files

Added ItemInformationDialog

  • Loading branch information...
Dax89 committed May 17, 2019
1 parent e889eb3 commit 987cffce1e2d91d6e99f750a1095495f446f9b11
@@ -0,0 +1,80 @@
#include "iteminformationdialog.h"
#include "ui_iteminformationdialog.h"
#include "../../../redasmsettings.h"
#include "../logsyntaxhighlighter.h"

#define ITEM_TYPE(x) #x
#define HEADER_STRING "="
#define HEADER_WIDTH 20
#define HEADER_PART QString(HEADER_STRING).repeated(HEADER_WIDTH)

ItemInformationDialog::ItemInformationDialog(const REDasm::DisassemblerPtr &disassembler, QWidget *parent) : QDialog(parent), ui(new Ui::ItemInformationDialog), m_disassembler(disassembler)
{
ui->setupUi(this);
ui->pteInfo->setFont(REDasmSettings::font());

new LogSyntaxHighlighter(ui->pteInfo->document());
this->displayInformation();
}

ItemInformationDialog::~ItemInformationDialog() { delete ui; }
ItemInformationDialog &ItemInformationDialog::line(const QString &s1, const QString &s2) { return this->line(s1 + ": " + s2); }
ItemInformationDialog& ItemInformationDialog::line(const QString &s) { ui->pteInfo->appendPlainText(s); return *this; }

ItemInformationDialog &ItemInformationDialog::header(const QString &s)
{
QString hdr = HEADER_PART;

if(!s.isEmpty())
hdr += QString(" %1 ").arg(s);

hdr += HEADER_PART;
return this->line(hdr);
}

ItemInformationDialog &ItemInformationDialog::string(const QString &k, const QString &s) { return this->line(k, QString("\"%1\"").arg(s)); }

QString ItemInformationDialog::itemType(const REDasm::ListingItem *item) const
{
if(item->type == REDasm::ListingItem::SegmentItem)
return ITEM_TYPE(REDasm::ListingItem::SegmentItem);
if(item->type == REDasm::ListingItem::EmptyItem)
return ITEM_TYPE(REDasm::ListingItem::EmptyItem);
if(item->type == REDasm::ListingItem::FunctionItem)
return ITEM_TYPE(REDasm::ListingItem::FunctionItem);
if(item->type == REDasm::ListingItem::TypeItem)
return ITEM_TYPE(REDasm::ListingItem::TypeItem);
if(item->type == REDasm::ListingItem::SymbolItem)
return ITEM_TYPE(REDasm::ListingItem::SymbolItem);
if(item->type == REDasm::ListingItem::MetaItem)
return ITEM_TYPE(REDasm::ListingItem::MetaItem);
if(item->type == REDasm::ListingItem::InstructionItem)
return ITEM_TYPE(REDasm::ListingItem::InstructionItem);

return QString::number(item->type);
}

void ItemInformationDialog::displayInformation()
{
const auto& document = m_disassembler->document();
const REDasm::ListingItem* item = document->currentItem();

this->line("document_index", QString::number(document->itemIndex(item)));
this->line("address", QString::fromStdString(REDasm::hex(item->address)));
this->line("type", this->itemType(item));
this->line("index", QString::number(item->index));

this->line().header("DATA");

this->array("comments", item->data->comments.begin(), item->data->comments.end(),
[&](const std::string& s) -> QString { return QString::fromStdString(s); });

this->array("auto_comments", item->data->autocomments.begin(), item->data->autocomments.end(),
[&](const std::string& s) -> QString { return QString::fromStdString(s); });

this->line("meta", QString("{ name: \"%1\", type: \"%2\"}").arg(QString::fromStdString(item->data->meta.name),
QString::fromStdString(item->data->meta.type)));

this->string("type", QString::fromStdString(item->data->type));
}

@@ -0,0 +1,54 @@
#ifndef ITEMINFORMATIONDIALOG_H
#define ITEMINFORMATIONDIALOG_H

#include <QDialog>
#include <redasm/disassembler/disassembler.h>

namespace Ui {
class ItemInformationDialog;
}

class ItemInformationDialog : public QDialog
{
Q_OBJECT

public:
explicit ItemInformationDialog(const REDasm::DisassemblerPtr& disassembler, QWidget *parent = nullptr);
~ItemInformationDialog();

private:
ItemInformationDialog& line(const QString& s1, const QString& s2);
ItemInformationDialog& line(const QString& s = QString());
ItemInformationDialog& header(const QString& s = QString());
ItemInformationDialog& string(const QString& k, const QString& s);
QString itemType(const REDasm::ListingItem* item) const;
void displayInformation();

private:
template<typename Iterator, typename Func> ItemInformationDialog& array(Iterator begin, Iterator end, const Func& cb);
template<typename Iterator, typename Func> ItemInformationDialog& array(const QString& k, Iterator begin, Iterator end, const Func& cb);

private:
Ui::ItemInformationDialog *ui;
REDasm::DisassemblerPtr m_disassembler;
};

template<typename Iterator, typename Func> ItemInformationDialog& ItemInformationDialog::array(Iterator begin, Iterator end, const Func& cb) { return this->array(QString(), begin, end, cb); }

template<typename Iterator, typename Func> ItemInformationDialog& ItemInformationDialog::array(const QString& k, Iterator begin, Iterator end, const Func& cb)
{
QString s;

for(Iterator it = begin; it != end; it++)
{
if(!s.isEmpty())
s += ", ";

s += cb(*it);
}

return k.isEmpty() ? this->line("[" + s + "]") :
this->line(k, "[" + s + "]");
}

#endif // ITEMINFORMATIONDIALOG_H
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ItemInformationDialog</class>
<widget class="QDialog" name="ItemInformationDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>525</width>
<height>305</height>
</rect>
</property>
<property name="windowTitle">
<string>Item Information</string>
</property>
<property name="modal">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="pteInfo">
<property name="undoRedoEnabled">
<bool>false</bool>
</property>
<property name="lineWrapMode">
<enum>QPlainTextEdit::NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
@@ -0,0 +1,32 @@
#include "logsyntaxhighlighter.h"
#include "../../themeprovider.h"

#define RULE_NAME(name) r_##name

#define CREATE_RULE(name, r, f) Rule RULE_NAME(name); \
RULE_NAME(name).regex = QRegularExpression(r); \
RULE_NAME(name).format.setForeground(THEME_VALUE(f)); \
m_rules.push_back(RULE_NAME(name));

LogSyntaxHighlighter::LogSyntaxHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
CREATE_RULE(hexdigits, "\\b[0-9a-fA-F]+\\b", "immediate_fg");
CREATE_RULE(strings, "\"[^\"]*\"", "string_fg");
}

void LogSyntaxHighlighter::highlightBlock(const QString &text)
{
for(auto it = m_rules.begin(); it != m_rules.end(); it++)
this->applyRule(*it, text);
}

void LogSyntaxHighlighter::applyRule(const LogSyntaxHighlighter::Rule &rule, const QString &text)
{
auto it = rule.regex.globalMatch(text);

while(it.hasNext())
{
auto m = it.next();
this->setFormat(m.capturedStart(), m.capturedLength(), rule.format);
}
}
@@ -0,0 +1,28 @@
#ifndef LOGSYNTAXHIGHLIGHTER_H
#define LOGSYNTAXHIGHLIGHTER_H

#include <QSyntaxHighlighter>
#include <QRegularExpression>
#include <QLinkedList>

class LogSyntaxHighlighter : public QSyntaxHighlighter
{
Q_OBJECT

private:
struct Rule { QRegularExpression regex; QTextCharFormat format; };

public:
explicit LogSyntaxHighlighter(QTextDocument *parent);

protected:
void highlightBlock(const QString &text) override;

private:
void applyRule(const Rule& rule, const QString& text);

private:
QLinkedList<Rule> m_rules;
};

#endif // LOGSYNTAXHIGHLIGHTER_H
@@ -29,6 +29,7 @@ void DisassemblerActions::adjustActions()
m_actions[DisassemblerActions::Forward]->setVisible(lock->cursor()->canGoForward());
m_actions[DisassemblerActions::Copy]->setVisible(lock->cursor()->hasSelection());
m_actions[DisassemblerActions::Goto]->setVisible(!m_renderer->disassembler()->busy());
m_actions[DisassemblerActions::ItemInformation]->setVisible(!m_renderer->disassembler()->busy());

if(!symbol)
{
@@ -95,6 +96,7 @@ void DisassemblerActions::createActions()
m_actions[DisassemblerActions::Forward] = m_contextmenu->addAction("Forward", this, &DisassemblerActions::goForward, QKeySequence(Qt::CTRL + Qt::Key_Right));
m_contextmenu->addSeparator();
m_actions[DisassemblerActions::Copy] = m_contextmenu->addAction("Copy", this, &DisassemblerActions::copy, QKeySequence(QKeySequence::Copy));
m_actions[DisassemblerActions::ItemInformation] = m_contextmenu->addAction("Item Information", this, &DisassemblerActions::itemInformationRequested);

if(pw)
{
@@ -13,7 +13,8 @@ class DisassemblerActions : public QObject
public:
enum { Rename = 0, XRefs, Follow, FollowPointerHexDump,
CallGraph, Goto, HexDump, HexDumpFunction, Comment,
Back, Forward, Copy };
Back, Forward, Copy,
ItemInformation };

public:
explicit DisassemblerActions(QWidget *parent = nullptr);
@@ -48,6 +49,7 @@ class DisassemblerActions : public QObject
void hexDumpRequested(address_t address, u64 len);
void referencesRequested(address_t address);
void callGraphRequested(address_t address);
void itemInformationRequested();
void gotoDialogRequested();
void switchToHexDump();

@@ -1,5 +1,6 @@
#include "disassemblerview.h"
#include "ui_disassemblerview.h"
#include "../../dialogs/dev/iteminformationdialog/iteminformationdialog.h"
#include "../../dialogs/referencesdialog/referencesdialog.h"
#include "../../themeprovider.h"
#include "../../redasmsettings.h"
@@ -84,6 +85,7 @@ DisassemblerView::DisassemblerView(QLineEdit *lefilter, QWidget *parent) : QWidg
connect(m_graphview, &DisassemblerGraphView::hexDumpRequested, this, &DisassemblerView::selectToHexDump);
connect(m_graphview, &DisassemblerGraphView::referencesRequested, this, &DisassemblerView::showReferences);
connect(m_graphview, &DisassemblerGraphView::switchToHexDump, this, &DisassemblerView::switchToHexDump);
connect(m_graphview, &DisassemblerGraphView::itemInformationRequested, this, &DisassemblerView::showCurrentItemInfo);
connect(m_graphview, &DisassemblerGraphView::callGraphRequested, m_docks, &DisassemblerViewDocks::initializeCallGraph);

connect(m_actions, &DisassemblerViewActions::backRequested, this, &DisassemblerView::goBack);
@@ -152,6 +154,7 @@ void DisassemblerView::bindDisassembler(REDasm::DisassemblerAPI *disassembler, b
connect(m_listingview->textView()->disassemblerActions(), &DisassemblerActions::hexDumpRequested, this, &DisassemblerView::selectToHexDump);
connect(m_listingview->textView()->disassemblerActions(), &DisassemblerActions::referencesRequested, this, &DisassemblerView::showReferences);
connect(m_listingview->textView()->disassemblerActions(), &DisassemblerActions::switchToHexDump, this, &DisassemblerView::switchToHexDump);
connect(m_listingview->textView()->disassemblerActions(), &DisassemblerActions::itemInformationRequested, this, &DisassemblerView::showCurrentItemInfo);
connect(m_listingview->textView()->disassemblerActions(), &DisassemblerActions::callGraphRequested, m_docks, &DisassemblerViewDocks::initializeCallGraph);

m_actions->setEnabled(DisassemblerViewActions::BackAction, m_disassembler->document()->cursor()->canGoBack());
@@ -267,6 +270,12 @@ void DisassemblerView::showModelReferences()
this->showReferences(symbol->address);
}

void DisassemblerView::showCurrentItemInfo()
{
ItemInformationDialog dlgiteminfo(m_disassembler, this);
dlgiteminfo.exec();
}

void DisassemblerView::showReferences(address_t address)
{
const REDasm::Symbol* symbol = m_disassembler->document()->symbol(address);
@@ -42,6 +42,7 @@ class DisassemblerView : public QWidget
void gotoXRef(const QModelIndex &index);
void goTo(const QModelIndex &index);
void showModelReferences();
void showCurrentItemInfo();
void showReferences(address_t address);
void displayAddress(address_t address);
void displayCurrentReferences();
@@ -19,6 +19,7 @@ DisassemblerGraphView::DisassemblerGraphView(QWidget *parent): GraphView(parent)
connect(m_disassembleractions, &DisassemblerActions::referencesRequested, this, &DisassemblerGraphView::referencesRequested);
connect(m_disassembleractions, &DisassemblerActions::switchToHexDump, this, &DisassemblerGraphView::switchToHexDump);
connect(m_disassembleractions, &DisassemblerActions::callGraphRequested, this, &DisassemblerGraphView::callGraphRequested);
connect(m_disassembleractions, &DisassemblerActions::itemInformationRequested, this, &DisassemblerGraphView::itemInformationRequested);
}

DisassemblerGraphView::~DisassemblerGraphView()
@@ -47,6 +47,7 @@ class DisassemblerGraphView : public GraphView
void hexDumpRequested(address_t address, u64 len);
void referencesRequested(address_t address);
void callGraphRequested(address_t address);
void itemInformationRequested();
void gotoDialogRequested();
void switchToHexDump();

0 comments on commit 987cffc

Please sign in to comment.
You can’t perform that action at this time.