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