]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
Remove adjustSize() hack -- doesn't seem to be necessary any more
[frida/frida.git] / src / gui / widgets / BasicBlockWidget.cxx
1 #include "BasicBlockWidget.hxx"
2 #include "CFGScene.hxx"
3 #include "gui/Mainwindow.hxx"
4 #include "gui/dialogs/SimpleStringDialog.hxx"
5 #include "core/BasicBlock.hxx"
6 #include "core/Function.hxx"
7 #include "core/Comment.hxx"
8 #include "disassembler/Instruction.hxx"
9 #include "core/InformationManager.hxx"
10 #include "core/events/RenameFunctionEvent.hxx"
11 #include "core/events/ChangeCommentEvent.hxx"
12 #include <algorithm>
13
14 class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem {
15 public:
16 CustomQGraphicsTextItem(const QString& text, BasicBlockWidget* parent)
17 : QGraphicsTextItem(text, parent), parent(parent) {}
18 void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
19 private:
20 void addComment(int row, bool global);
21
22 BasicBlockWidget* parent;
23 };
24
25 void CustomQGraphicsTextItem::addComment(int row, bool global) {
26 SimpleStringDialog dialog(global ? "Global comment" : "Local comment");
27 int result = dialog.exec();
28 uint64_t address = parent->instructions[row].getAddress();
29 if (QDialog::Accepted == result) {
30 Comment* comment;
31 if (global) {
32 comment = parent->block->getManager()->newGlobalComment(address);
33 } else {
34 /* TODO: 0x23 as we currently don't have the function here
35 * and setting it to null will make the comment appear
36 * global. Also means local comments are largely still
37 * broken.
38 */
39 comment = parent->block->getManager()->newLocalComment(address, (Function*)0x23);
40 }
41 comment->setText(dialog.result().toStdString());
42 parent->block->getManager()->finishComment(comment);
43 } else {
44 LOG4CXX_DEBUG(parent->logger, "addComment aborted");
45 }
46 }
47
48 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
49 QTextCursor c = textCursor();
50 c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
51 c.select(QTextCursor::WordUnderCursor);
52
53 QMenu menu;
54 bool ok;
55 uint64_t address = c.selectedText().toLongLong(&ok, 16);
56 QTextTable* table = c.currentTable();
57 if (ok) {
58 QAction* act = menu.addAction(c.selectedText() + " is a Function");
59 QObject::connect(act, &QAction::triggered,
60 [=]() {
61 parent->mainwindow->requestNewFunctionByAddress(address);
62 if (NULL == table) return;
63 int row = table->cellAt(c).row();
64 uint64_t insAddress = parent->instructions[row].getAddress();
65 Comment* comment = parent->block->getManager()->newLocalComment(insAddress, (Function*)0x23);
66 comment->setText("#F<" + c.selectedText().toStdString() + ">");
67 parent->block->getManager()->finishComment(comment);
68 });
69 }
70
71 if (NULL != table) {
72 int row = table->cellAt(c).row();
73 QAction* globalComment = menu.addAction("Add global Comment");
74 QAction* localComment = menu.addAction("Add local Comment");
75
76 QObject::connect(globalComment, &QAction::triggered,
77 [=]() { addComment(row, true); });
78 QObject::connect(localComment, &QAction::triggered,
79 [=]() { addComment(row, false); });
80 }
81
82 menu.exec(event->screenPos());
83 }
84
85 BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block,
86 Mainwindow * mainwindow)
87 : width(200), height(45), name(name)
88 , currentColor(defaultColor), _table(NULL)
89 , block(block), mainwindow(mainwindow)
90 , logger(log4cxx::Logger::getLogger("gui.BasicBlockWidget." + name.toStdString())) {
91 next[0] = NULL; next[1] = NULL;
92
93 QObject::connect(block->getManager(), &InformationManager::renameFunctionEvent,
94 [=](RenameFunctionEvent* event) {updateFunctionName(event);});
95
96 _widget.reset(new CustomQGraphicsTextItem("", this));
97 _widget->setPos(5, 20);
98 _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
99 Qt::LinksAccessibleByMouse);
100
101 if (width < 250) width = 250;
102
103 QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
104 [=](QString str) {
105 if (str.startsWith("function:")) {
106 QString address = str.remove("function:");
107 mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
108 } else if (str.startsWith("block:")) {
109 QString address = str.remove("block:");
110
111 /* next[0] is always the jumptarget. On a
112 * conditional jump, next[1] also
113 * contains the following instruction
114 *
115 * TODO: Verify we're switching to the
116 * right block -- the target
117 * address matches the next blocks
118 * start address
119 */
120 LOG4CXX_TRACE(logger, "Highlighting block at Address " << address.toStdString()
121 << " BasicBlockWidget " << std::hex << next[0]);
122 ((CFGScene*)this->scene())->highlightBlock(next[0]);
123 }
124 });
125 instructions = block->getInstructions();
126 populateWidget();
127 QObject::connect(block->getManager(), &InformationManager::changeCommentEvent,
128 [=](ChangeCommentEvent* e) {changeCommentHandler(e);});
129 }
130
131 void BasicBlockWidget::updateFunctionName(RenameFunctionEvent *event) {
132 QString search = QString("function:") + QString::number(event->address, 16);
133 QTextDocument *document = _widget->document();
134 QTextBlock b = document->begin();
135 while (b.isValid()) {
136 for (QTextBlock::iterator i = b.begin(); !i.atEnd(); ++i) {
137 QTextCharFormat format = i.fragment().charFormat();
138 bool isLink = format.isAnchor();
139 if (isLink)
140 {
141 if (search == format.anchorHref()) {
142 LOG4CXX_DEBUG(logger, i.fragment().text().toStdString() << " ---> "
143 << format.anchorHref().toStdString());
144
145 /* This should select the function name. It stars
146 * by selecting the whole link fragment from back
147 * to front and then moves one word to the back
148 * again deselecting whatever mnemonic is used for
149 * the call instruction.
150 */
151 QTextCursor c(b);
152 c.setPosition(i.fragment().position());
153 c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, i.fragment().length());
154 c.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, i.fragment().length());
155 c.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor);
156 c.insertText(event->new_name.c_str());
157
158 QGraphicsTextItem* item = _widget.get();
159 item->adjustSize();
160 }
161 }
162 }
163 b = b.next();
164 }
165 }
166
167 void BasicBlockWidget::changeCommentHandler(ChangeCommentEvent* event) {
168 auto inst_it = std::find_if(instructions.begin(), instructions.end(),
169 [=](Instruction& inst) {
170 return inst.getAddress() == event->address;
171 });
172 if (inst_it != instructions.end()) {
173 if (std::find(inst_it->comments().begin(),
174 inst_it->comments().begin(),
175 event->comment) == inst_it->comments().end()) {
176 LOG4CXX_DEBUG(logger, "Change Comment Event -- New Comment!");
177 inst_it->comments().push_back(event->comment);
178 }
179 int row = inst_it - instructions.begin();
180 LOG4CXX_DEBUG(logger, "Inserting comment for instruction at row " << std::hex << row);
181 QTextCursor cursor = _table->cellAt(row, 2).lastCursorPosition();
182 while (cursor != _table->cellAt(row, 2).firstCursorPosition()) {
183 cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 1);
184 }
185 cursor.removeSelectedText();
186 cursor.insertHtml(formatComments(&*inst_it));
187 QGraphicsTextItem* item = _widget.get();
188 item->adjustSize();
189 }
190 }
191
192 void BasicBlockWidget::populateWidget() {
193 int row;
194 QTextTableFormat format;
195 QTextDocument* document = new QTextDocument();
196 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
197 format.setBorder(0);
198
199 for (Instruction& inst : instructions) {
200 if (_table) {
201 row = _table->rows();
202 _table->appendRows(1);
203 } else {
204 row = 0;
205 _table = QTextCursor(document).insertTable(1, 3, format);
206 }
207 QString bytestring;
208 for (uint8_t byte : inst.getBytes()) {
209 const char * hexdigits = "0123456789ABCDEF";
210 bytestring += hexdigits[(byte >> 4) & 0xF];
211 bytestring += hexdigits[byte & 0xF];
212 bytestring += ' ';
213 }
214 _table->cellAt(row, 0).firstCursorPosition().insertHtml("<nobr>" + bytestring + "</nobr>");
215
216 QString line = inst.getText().c_str();
217 line = line.replace('\t', ' ').toHtmlEscaped();
218 if (inst.getReference() != "") {
219 QString href = inst.getReference().c_str();
220 QStringList list = href.split(":");
221 if (list[0] == "function") {
222 uint64_t address = href.split(":")[1].toLongLong(NULL, 16);
223 Function* fun = block->getManager()->getFunction(address);
224
225 if (fun) {
226 line = line.split(" ")[0] + " " + QString(fun->getName().c_str()).toHtmlEscaped();
227 LOG4CXX_DEBUG(logger, "Naming function at " << address << " " << fun->getName());
228 }
229 }
230 line = "<a href=\"" + href + "\">" + line + "</a>";
231 }
232 _table->cellAt(row, 1).firstCursorPosition().insertHtml("<nobr>" + line + "</nobr>");
233 _table->cellAt(row, 2).firstCursorPosition().insertHtml(formatComments(&inst));
234 }
235 QGraphicsTextItem* item = _widget.get();
236 item->setDocument(document);
237 item->adjustSize();
238 }
239
240 QString BasicBlockWidget::formatComments(Instruction* inst) {
241 QStringList comments;
242 for (Comment* c: inst->comments()) {
243 comments << QString(c->getText().c_str()).toHtmlEscaped();
244 }
245 return (comments.empty() ? "" : ";; ") + comments.join("<br />").trimmed();
246 }
247
248 void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem*,
249 QWidget*) {
250 width = 10 + _widget->boundingRect().width();
251 height = 25 + _widget->boundingRect().height();
252 if (width < 250) width = 250;
253
254 painter->fillRect(0, 0, width, height, currentColor);
255 painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
256 painter->drawRect(0, 0, width, height);
257 painter->drawText(5, 15, name);
258 }
259
260 QRectF BasicBlockWidget::boundingRect() const {
261 qreal penWidth = 1;
262 QRectF result(- penWidth / 2, - penWidth / 2,
263 width + penWidth, height + penWidth);
264 return result;
265 }
266
267 std::array<QPointF, 3> BasicBlockWidget::getExits() const {
268 return { { mapToScene(QPointF( width/3, height)),
269 mapToScene(QPointF( width/2, height)),
270 mapToScene(QPointF(2*width/3, height)) } };
271 }
272