]> git.siccegge.de Git - frida/frida.git/blobdiff - src/gui/widgets/BasicBlockWidget.cxx
Properly HTML Escape function names
[frida/frida.git] / src / gui / widgets / BasicBlockWidget.cxx
index 2003458d8733a2deb3e7e15d4b5a426209833c0c..1f34255343aafbf12d2d80c1e2d2169b83b31a87 100644 (file)
 #include "BasicBlockWidget.hxx"
+#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 <iostream>
 
-BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block)
-       : width(270), height(45)
-       , name(name), block(block)
-       , _proxy(this) {
+class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem {
+public:
+       CustomQGraphicsTextItem(const QString& text, BasicBlockWidget* parent)
+               : QGraphicsTextItem(text, parent), parent(parent) {}
+       void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
+private:
+       void addComment(QTextTableCell cell, const QString& title);
+
+       BasicBlockWidget* parent;
+};
+
+void CustomQGraphicsTextItem::addComment(QTextTableCell cell, const QString& title) {
+       SimpleStringDialog dialog(title);
+       int result = dialog.exec();
+       if (QDialog::Accepted == result) {
+               LOG4CXX_DEBUG(parent->logger, "adding comment " << dialog.result().toStdString()
+                             << " at row " << cell.row());
+               cell.firstCursorPosition().insertHtml(QString(";; ") + dialog.result());
+       } else {
+               LOG4CXX_DEBUG(parent->logger, "addComment aborted");
+       }
+}
+
+void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
+       QTextCursor c = textCursor();
+       c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
+       c.select(QTextCursor::WordUnderCursor);
+
+       QMenu menu;
+       bool ok;
+       uint64_t address = c.selectedText().toLongLong(&ok, 16);
+       if (ok) {
+               QAction* act = menu.addAction(c.selectedText() + " is a Function");
+               QObject::connect(act, &QAction::triggered,
+                                [=]() {parent->mainwindow->requestNewFunctionByAddress(address);});
+       }
+
+       QTextTable* table = c.currentTable();
+       if (NULL != table) {
+               int row = table->cellAt(c).row();
+               QTextTableCell cell = table->cellAt(row, 2);
+               QAction* globalComment = menu.addAction("Add global Comment");
+               QAction* localComment = menu.addAction("Add local Comment");
+
+               QObject::connect(globalComment, &QAction::triggered,
+                                [=]() { addComment(cell, "Global comment"); });
+               QObject::connect(localComment, &QAction::triggered,
+                                [=]() { addComment(cell, "Local comment"); });
+       }
+
+       menu.exec(event->screenPos());
+}
+
+BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block,
+                                   Mainwindow * mainwindow)
+       : width(270), height(45), name(name)
+       , _table(NULL)
+       , block(block), mainwindow(mainwindow)
+       , logger(log4cxx::Logger::getLogger(name.toStdString() + " BasicBlockWidget")) {
        next[0] = NULL; next[1] = NULL;
-       _proxy.setWidget(&_widget);
-       _widget.setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::LinksAccessibleByMouse);
 
-       _proxy.setPos(5, 20);
+       block->getManager()->connect_rename_function_signal([=](RenameFunctionEvent* event) {updateFunctionName(event);});
+
+       _widget.reset(new CustomQGraphicsTextItem("", this));
+       _widget->setPos(5, 20);
+       _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
+                                       Qt::LinksAccessibleByMouse);
+
+       if (width < 250) width = 250;
 
-       if (width < 270) width = 270;
+       QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
+                        [=](QString str) {
+                                if (str.startsWith("function:")) {
+                                        QString address = str.remove("function:");
+                                        mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
+                                }
+                        });
+}
+
+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,
-                               const QString& line) {
+                               QString line, const QString& href) {
        QString bytestring;
+       int row;
+
+       if (_table) {
+               row = _table->rows();
+               _table->appendRows(1);
+       } else {
+               row = 0;
+               QTextTableFormat format;
+               format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
+               format.setBorder(0);
+               _table = _widget->textCursor().insertTable(1, 3, format);
+       }
 
        for (size_t i(0); i < num_bytes; ++i) {
                const char * hexdigits = "0123456789ABCDEF";
@@ -24,21 +134,48 @@ void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes,
                bytestring += ' ';
        }
 
-       QString old_text = _widget.text();
-       if (old_text == "") {
-               old_text = "<table style=\"background-color: #ddddff;\">";
+       _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
+
+       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] + " " + QString(fun->getName().c_str()).toHtmlEscaped();
+                               LOG4CXX_DEBUG(logger, "Naming function at " << address << " " << fun->getName());
+                       }
+               }
+               line = "<a href=\"" + href + "\">" + line + "</a>";
        }
-       QString new_text =
-               old_text.remove("</table>") +
-               "<tr><td>" + bytestring +
-               "</td><td>" + line +
-               "</td><td>" + "" +
-               "</td></tr></table>";
-
-       _widget.setText(new_text);
-       _widget.resize(_widget.sizeHint().width(), _widget.sizeHint().height());
-       width = 12 + _widget.sizeHint().width();
-       height = 25 + _widget.sizeHint().height();
 
+       _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
+}
+
+void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
+                  QWidget *widget) {
+       width = 10 + _widget->boundingRect().width();
+       height = 25 + _widget->boundingRect().height();
        if (width < 250) width = 250;
+
+       painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
+       painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
+       painter->drawRect(0, 0, width, height);
+       painter->drawText(5, 15, name);
+}
+
+QRectF BasicBlockWidget::boundingRect() const  {
+       qreal penWidth = 1;
+       QRectF result(- penWidth / 2, - penWidth / 2,
+                     width + penWidth, height + penWidth);
+       return result;
 }
+
+std::array<QPointF, 3> BasicBlockWidget::getExits() const {
+       return { { mapToScene(QPointF(  width/3, height)),
+                          mapToScene(QPointF(  width/2, height)),
+                          mapToScene(QPointF(2*width/3, height)) } };
+}
+