From 440c4598343d9e46d8963f7753c122b121826334 Mon Sep 17 00:00:00 2001 From: Christoph Egger Date: Mon, 23 Feb 2015 16:36:31 +0100 Subject: [PATCH] Name functions in BasicBlock view Now call instructions have a symbolic name in the CFG view which is updated upon function renaming --- CMakeLists.txt | 1 + src/core/Function.cxx | 20 ++++++++++++ src/core/Function.hxx | 9 ++---- src/core/InformationManager.cxx | 19 ++++++----- src/core/InformationManager.hxx | 30 ++++++++++++----- src/core/events/RenameFunctionEvent.hxx | 15 +++++++++ src/gui/widgets/BasicBlockWidget.cxx | 43 ++++++++++++++++++++++++- src/gui/widgets/BasicBlockWidget.hxx | 4 +++ src/gui/widgets/FunctionWidget.cxx | 1 + src/gui/widgets/FunctionWidget.hxx | 23 +++++++++++++ 10 files changed, 141 insertions(+), 24 deletions(-) create mode 100644 src/core/Function.cxx create mode 100644 src/core/events/RenameFunctionEvent.hxx create mode 100644 src/gui/widgets/FunctionWidget.cxx create mode 100644 src/gui/widgets/FunctionWidget.hxx diff --git a/CMakeLists.txt b/CMakeLists.txt index d948879..0fe40fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ separate_arguments(LLVM_LIBS) SET(frida_SOURCES src/main.cxx src/core/InformationManager.cxx + src/core/Function.cxx src/gui/Mainwindow.cxx src/gui/widgets/BasicBlockWidget.cxx src/gui/widgets/CFGScene.cxx diff --git a/src/core/Function.cxx b/src/core/Function.cxx new file mode 100644 index 0000000..e9bb173 --- /dev/null +++ b/src/core/Function.cxx @@ -0,0 +1,20 @@ +#include "Function.hxx" +#include "core/events/RenameFunctionEvent.hxx" +#include "InformationManager.hxx" + +#include + +Function::Function(const std::string& name, uint64_t start_address, + InformationManager* manager) + : start_address(start_address) + , manager(manager) { + setName(name); +} + + +void Function::setName(const std::string& new_name) { + name = new_name; + RenameFunctionEvent event(new_name, start_address); + manager->dispatch(&event); +} + diff --git a/src/core/Function.hxx b/src/core/Function.hxx index aebfe8f..b57dca2 100644 --- a/src/core/Function.hxx +++ b/src/core/Function.hxx @@ -8,11 +8,7 @@ class InformationManager; class Function { public: - Function(const std::string& name, uint64_t start_address, - InformationManager* manager) - : name(name) - , start_address(start_address) - ,manager(manager) {} + Function(const std::string& name, uint64_t start_address, InformationManager* manager); uint64_t getStartAddress() const { return start_address; @@ -20,8 +16,7 @@ public: std::string getName() const { return name; } - void setName(const std::string& new_name) - { name = new_name; } + void setName(const std::string& new_name); InformationManager* getManager() const { return manager; diff --git a/src/core/InformationManager.cxx b/src/core/InformationManager.cxx index 1234ac9..a274c41 100644 --- a/src/core/InformationManager.cxx +++ b/src/core/InformationManager.cxx @@ -8,11 +8,11 @@ #include InformationManager::~InformationManager() { - for (BasicBlock * b : blocks) - delete b; + for (auto b : blocks) + delete b.second; - for (Function * f : functions) - delete f; + for (auto f : functions) + delete f.second; } void InformationManager::reset(const std::string& filename) { @@ -27,7 +27,8 @@ void InformationManager::save(const QString& filename) { zip.setComment("FRIDA 0.0"); QuaZipFile outZipFile(&zip); - for (Function* fun : functions) { + for (auto funpair : functions) { + Function* fun = funpair.second; QuaZipNewInfo zipinfo(fun->getName().c_str()); zipinfo.setPermissions(static_cast(0x6444)); outZipFile.open(QIODevice::WriteOnly, zipinfo); @@ -60,8 +61,10 @@ void InformationManager::save(const QString& filename) { } void InformationManager::signal_new_function(Function* fun) { - functions.insert(fun); - for (auto b : fun->blocks()) - blocks.insert(b.second); + functions.insert(std::make_pair(fun->getStartAddress(), fun)); + for (auto b : fun->blocks()) { + BasicBlock* bl = b.second; + blocks.insert(std::make_pair(bl->getStartAddress(), bl)); + } new_function_signal(fun); } diff --git a/src/core/InformationManager.hxx b/src/core/InformationManager.hxx index a43b118..dc78128 100644 --- a/src/core/InformationManager.hxx +++ b/src/core/InformationManager.hxx @@ -4,7 +4,7 @@ #include #include #include -#include +#include class Disassembler; class Function; @@ -12,6 +12,8 @@ class BasicBlock; class QString; +class RenameFunctionEvent; + class InformationManager { public: ~InformationManager(); @@ -20,33 +22,45 @@ public: void save(const QString& filename); void signal_new_function(Function* f); + void signal_new_dyn_symbol(const std::string& f) + { new_dyn_symbol_signal(f); } boost::signals2::connection connect_new_function_signal(std::function f) { return new_function_signal.connect(f); } - - boost::signals2::connection connect_new_dyn_symbol_signal(std::function f) { return new_dyn_symbol_signal.connect(f); } - void signal_new_dyn_symbol(const std::string& f) - { new_dyn_symbol_signal(f); } - boost::signals2::connection connect_reset_signal(std::function f) { return reset_signal.connect(f); } + boost::signals2::connection + connect_rename_function_signal(std::function f) + { return rename_function_signal.connect(f); } + Disassembler* getDisassembler() { return disassembler.get(); } + + void dispatch(RenameFunctionEvent* event) + { rename_function_signal(event); } + + Function* getFunction(uint64_t address) + { return functions[address]; } + + BasicBlock* getBlock(uint64_t address) + { return blocks[address]; } + private: boost::signals2::signal reset_signal; boost::signals2::signal new_function_signal; boost::signals2::signal new_dyn_symbol_signal; + boost::signals2::signal rename_function_signal; std::unique_ptr disassembler; - std::set functions; - std::set blocks; + std::map functions; + std::map blocks; }; #endif /* INCLUDE__InformationManager_hxx */ diff --git a/src/core/events/RenameFunctionEvent.hxx b/src/core/events/RenameFunctionEvent.hxx new file mode 100644 index 0000000..a8c78d5 --- /dev/null +++ b/src/core/events/RenameFunctionEvent.hxx @@ -0,0 +1,15 @@ +#ifndef INCLUDE__RenameFunctionEvent_hxx_ +#define INCLUDE__RenameFunctionEvent_hxx_ + +#include + +class RenameFunctionEvent { +public: + RenameFunctionEvent(const std::string& name, uint64_t address) + : new_name(name), address(address) {} + + std::string new_name; + uint64_t address; +}; + +#endif /* INCLUDE__RenameFunctionEvent_hxx_ */ diff --git a/src/gui/widgets/BasicBlockWidget.cxx b/src/gui/widgets/BasicBlockWidget.cxx index 0036bda..f66a575 100644 --- a/src/gui/widgets/BasicBlockWidget.cxx +++ b/src/gui/widgets/BasicBlockWidget.cxx @@ -2,6 +2,10 @@ #include "gui/Mainwindow.hxx" #include "gui/dialogs/SimpleStringDialog.hxx" #include "core/BasicBlock.hxx" +#include "core/Function.hxx" +#include "core/InformationManager.hxx" +#include "core/events/RenameFunctionEvent.hxx" +#include class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem { public: @@ -61,8 +65,11 @@ BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block, : width(270), height(45), name(name) , _table(NULL) , block(block), mainwindow(mainwindow) - , logger(log4cxx::Logger::getLogger(name.toStdString() + "BasicBlock")) { + , logger(log4cxx::Logger::getLogger(name.toStdString() + " BasicBlockWidget")) { next[0] = NULL; next[1] = NULL; + + block->getManager()->connect_rename_function_signal([=](RenameFunctionEvent* event) {updateFunctionName(event);}); + _widget.reset(new CustomQGraphicsTextItem("", this)); _widget->setPos(5, 20); _widget->setTextInteractionFlags(Qt::TextSelectableByMouse| @@ -78,6 +85,30 @@ BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block, } }); } + +void BasicBlockWidget::updateFunctionName(RenameFunctionEvent *event) { + QString search = QString("function:") + QString::number(event->address, 16); + QTextDocument *document = _widget->document(); + QTextBlock b = document->begin(); + while (b.isValid()) { + for (QTextBlock::iterator i = b.begin(); !i.atEnd(); ++i) { + QTextCharFormat format = i.fragment().charFormat(); + bool isLink = format.isAnchor(); + if (isLink) + { + if (search == format.anchorHref()) { + LOG4CXX_DEBUG(logger, i.fragment().text().toStdString() << " ---> " << format.anchorHref().toStdString()); + QTextCursor c(b); + c.setPosition(i.fragment().position()); + c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, i.fragment().length()); + c.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, i.fragment().length()); + c.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor); + c.insertText(event->new_name.c_str()); + } + } + } + b = b.next(); + } } void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes, @@ -107,6 +138,16 @@ void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes, line = line.replace('\t', ' ').toHtmlEscaped(); if (href != "") { + QStringList list = href.split(":"); + if (list[0] == "function") { + uint64_t address = href.split(":")[1].toLongLong(NULL, 16); + Function* fun = block->getManager()->getFunction(address); + + if (fun) { + line = line.split(" ")[0] + " " + fun->getName().c_str(); + LOG4CXX_DEBUG(logger, "Naming function at " << address << " " << fun->getName()); + } + } line = "" + line + ""; } diff --git a/src/gui/widgets/BasicBlockWidget.hxx b/src/gui/widgets/BasicBlockWidget.hxx index ce1c833..8cf709c 100644 --- a/src/gui/widgets/BasicBlockWidget.hxx +++ b/src/gui/widgets/BasicBlockWidget.hxx @@ -14,6 +14,8 @@ class CustomQGraphicsTextItem; class BasicBlock; +class RenameFunctionEvent; + class BasicBlockWidget : public QObject, public QGraphicsItem { Q_OBJECT @@ -45,6 +47,8 @@ public: QString getName() const { return name; } private: + void updateFunctionName(RenameFunctionEvent* event); + uint32_t width, height; QString name; std::unique_ptr _widget; diff --git a/src/gui/widgets/FunctionWidget.cxx b/src/gui/widgets/FunctionWidget.cxx new file mode 100644 index 0000000..e4c1c0b --- /dev/null +++ b/src/gui/widgets/FunctionWidget.cxx @@ -0,0 +1 @@ +#include "FunctionWidget.hxx" diff --git a/src/gui/widgets/FunctionWidget.hxx b/src/gui/widgets/FunctionWidget.hxx new file mode 100644 index 0000000..fbd3638 --- /dev/null +++ b/src/gui/widgets/FunctionWidget.hxx @@ -0,0 +1,23 @@ +#ifndef INCLUDE__FunctionWidget_hxx_ +#define INCLUDE__FunctionWidget_hxx_ + +#include "gui/qt.hxx" + +class Function; + +class FunctionWidget : public QTabWidget { + Q_OBJECT +public: + FunctionWidget(Function* function) + : function(function) {} + + virtual ~FunctionWidget() {} + + Function* getFunction() const + { return function; } + +private: + Function * function; +}; + +#endif /* INCLUDE__FunctionWidget_hxx_ */ -- 2.39.5