]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
128b9fb5401d0e4b3977057af4d5ffe5212e8315
[frida/frida.git] / src / gui / widgets / BasicBlockWidget.cxx
1 #include "BasicBlockWidget.hxx"
2 #include "gui/Mainwindow.hxx"
3 #include "gui/dialogs/SimpleStringDialog.hxx"
4 #include "core/BasicBlock.hxx"
5 #include "core/Function.hxx"
6 #include "core/InformationManager.hxx"
7 #include "core/events/RenameFunctionEvent.hxx"
8 #include <iostream>
9
10 class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem {
11 public:
12 CustomQGraphicsTextItem(const QString& text, BasicBlockWidget* parent)
13 : QGraphicsTextItem(text, parent), parent(parent) {}
14 void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
15 private:
16 void addComment(QTextTableCell cell, const QString& title);
17
18 BasicBlockWidget* parent;
19 };
20
21 void CustomQGraphicsTextItem::addComment(QTextTableCell cell, const QString& title) {
22 SimpleStringDialog dialog(title);
23 int result = dialog.exec();
24 if (QDialog::Accepted == result) {
25 LOG4CXX_DEBUG(parent->logger, "adding comment " << dialog.result().toStdString()
26 << " at row " << cell.row());
27 cell.firstCursorPosition().insertHtml(QString(";; ") + dialog.result());
28 } else {
29 LOG4CXX_DEBUG(parent->logger, "addComment aborted");
30 }
31 }
32
33 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
34 QTextCursor c = textCursor();
35 c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
36 c.select(QTextCursor::WordUnderCursor);
37
38 QMenu menu;
39 bool ok;
40 uint64_t address = c.selectedText().toLongLong(&ok, 16);
41 if (ok) {
42 QAction* act = menu.addAction(c.selectedText() + " is a Function");
43 QObject::connect(act, &QAction::triggered,
44 [=]() {parent->mainwindow->requestNewFunctionByAddress(address);});
45 }
46
47 QTextTable* table = c.currentTable();
48 if (NULL != table) {
49 int row = table->cellAt(c).row();
50 QTextTableCell cell = table->cellAt(row, 2);
51 QAction* globalComment = menu.addAction("Add global Comment");
52 QAction* localComment = menu.addAction("Add local Comment");
53
54 QObject::connect(globalComment, &QAction::triggered,
55 [=]() { addComment(cell, "Global comment"); });
56 QObject::connect(localComment, &QAction::triggered,
57 [=]() { addComment(cell, "Local comment"); });
58 }
59
60 menu.exec(event->screenPos());
61 }
62
63 BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block,
64 Mainwindow * mainwindow)
65 : width(270), height(45), name(name)
66 , _table(NULL)
67 , block(block), mainwindow(mainwindow)
68 , logger(log4cxx::Logger::getLogger("gui.BasicBlockWidget." + name.toStdString())) {
69 next[0] = NULL; next[1] = NULL;
70
71 block->getManager()->registerRenameFunctionEvent([=](RenameFunctionEvent* event) {updateFunctionName(event);});
72
73 _widget.reset(new CustomQGraphicsTextItem("", this));
74 _widget->setPos(5, 20);
75 _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
76 Qt::LinksAccessibleByMouse);
77
78 if (width < 250) width = 250;
79
80 QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
81 [=](QString str) {
82 if (str.startsWith("function:")) {
83 QString address = str.remove("function:");
84 mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
85 }
86 });
87 }
88
89 void BasicBlockWidget::updateFunctionName(RenameFunctionEvent *event) {
90 QString search = QString("function:") + QString::number(event->address, 16);
91 QTextDocument *document = _widget->document();
92 QTextBlock b = document->begin();
93 while (b.isValid()) {
94 for (QTextBlock::iterator i = b.begin(); !i.atEnd(); ++i) {
95 QTextCharFormat format = i.fragment().charFormat();
96 bool isLink = format.isAnchor();
97 if (isLink)
98 {
99 if (search == format.anchorHref()) {
100 LOG4CXX_DEBUG(logger, i.fragment().text().toStdString() << " ---> " << format.anchorHref().toStdString());
101 QTextCursor c(b);
102 c.setPosition(i.fragment().position());
103 c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, i.fragment().length());
104 c.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, i.fragment().length());
105 c.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor);
106 c.insertText(event->new_name.c_str());
107 }
108 }
109 }
110 b = b.next();
111 }
112 }
113
114 void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes,
115 QString line, const QString& href) {
116 QString bytestring;
117 int row;
118
119 if (_table) {
120 row = _table->rows();
121 _table->appendRows(1);
122 } else {
123 row = 0;
124 QTextTableFormat format;
125 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
126 format.setBorder(0);
127 _table = _widget->textCursor().insertTable(1, 3, format);
128 }
129
130 for (size_t i(0); i < num_bytes; ++i) {
131 const char * hexdigits = "0123456789ABCDEF";
132 bytestring += hexdigits[(bytes[i] >> 4) & 0xF];
133 bytestring += hexdigits[bytes[i] & 0xF];
134 bytestring += ' ';
135 }
136
137 _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
138
139 line = line.replace('\t', ' ').toHtmlEscaped();
140 if (href != "") {
141 QStringList list = href.split(":");
142 if (list[0] == "function") {
143 uint64_t address = href.split(":")[1].toLongLong(NULL, 16);
144 Function* fun = block->getManager()->getFunction(address);
145
146 if (fun) {
147 line = line.split(" ")[0] + " " + QString(fun->getName().c_str()).toHtmlEscaped();
148 LOG4CXX_DEBUG(logger, "Naming function at " << address << " " << fun->getName());
149 }
150 }
151 line = "<a href=\"" + href + "\">" + line + "</a>";
152 }
153
154 _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
155 }
156
157 void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem*,
158 QWidget*) {
159 _widget->adjustSize();
160 width = 10 + _widget->boundingRect().width();
161 height = 25 + _widget->boundingRect().height();
162 if (width < 250) width = 250;
163
164 painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
165 painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
166 painter->drawRect(0, 0, width, height);
167 painter->drawText(5, 15, name);
168 }
169
170 QRectF BasicBlockWidget::boundingRect() const {
171 qreal penWidth = 1;
172 QRectF result(- penWidth / 2, - penWidth / 2,
173 width + penWidth, height + penWidth);
174 return result;
175 }
176
177 std::array<QPointF, 3> BasicBlockWidget::getExits() const {
178 return { { mapToScene(QPointF( width/3, height)),
179 mapToScene(QPointF( width/2, height)),
180 mapToScene(QPointF(2*width/3, height)) } };
181 }
182