]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
Rework API for getting at instructions
[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 instructions = block->getInstructions();
88 populateWidget();
89 }
90
91 void BasicBlockWidget::updateFunctionName(RenameFunctionEvent *event) {
92 QString search = QString("function:") + QString::number(event->address, 16);
93 QTextDocument *document = _widget->document();
94 QTextBlock b = document->begin();
95 while (b.isValid()) {
96 for (QTextBlock::iterator i = b.begin(); !i.atEnd(); ++i) {
97 QTextCharFormat format = i.fragment().charFormat();
98 bool isLink = format.isAnchor();
99 if (isLink)
100 {
101 if (search == format.anchorHref()) {
102 LOG4CXX_DEBUG(logger, i.fragment().text().toStdString() << " ---> " << format.anchorHref().toStdString());
103 QTextCursor c(b);
104 c.setPosition(i.fragment().position());
105 c.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, i.fragment().length());
106 c.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, i.fragment().length());
107 c.movePosition(QTextCursor::WordRight, QTextCursor::KeepAnchor);
108 c.insertText(event->new_name.c_str());
109 }
110 }
111 }
112 b = b.next();
113 }
114 }
115
116 void BasicBlockWidget::populateWidget() {
117 int row;
118 QTextTableFormat format;
119 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
120 format.setBorder(0);
121
122 for (Instruction& inst : instructions) {
123 if (_table) {
124 row = _table->rows();
125 _table->appendRows(1);
126 } else {
127 row = 0;
128 _table = _widget->textCursor().insertTable(1, 3, format);
129 }
130 QString bytestring;
131 for (uint8_t byte : inst.getBytes()) {
132 const char * hexdigits = "0123456789ABCDEF";
133 bytestring += hexdigits[(byte >> 4) & 0xF];
134 bytestring += hexdigits[byte & 0xF];
135 bytestring += ' ';
136 }
137 _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
138
139 QString line = inst.getText().c_str();
140 line = line.replace('\t', ' ').toHtmlEscaped();
141 if (inst.getReference() != "") {
142 QString href = inst.getReference().c_str();
143 QStringList list = href.split(":");
144 if (list[0] == "function") {
145 uint64_t address = href.split(":")[1].toLongLong(NULL, 16);
146 Function* fun = block->getManager()->getFunction(address);
147
148 if (fun) {
149 line = line.split(" ")[0] + " " + QString(fun->getName().c_str()).toHtmlEscaped();
150 LOG4CXX_DEBUG(logger, "Naming function at " << address << " " << fun->getName());
151 }
152 }
153 line = "<a href=\"" + href + "\">" + line + "</a>";
154 }
155
156 _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
157 }
158 }
159
160 void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem*,
161 QWidget*) {
162 _widget->adjustSize();
163 width = 10 + _widget->boundingRect().width();
164 height = 25 + _widget->boundingRect().height();
165 if (width < 250) width = 250;
166
167 painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
168 painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
169 painter->drawRect(0, 0, width, height);
170 painter->drawText(5, 15, name);
171 }
172
173 QRectF BasicBlockWidget::boundingRect() const {
174 qreal penWidth = 1;
175 QRectF result(- penWidth / 2, - penWidth / 2,
176 width + penWidth, height + penWidth);
177 return result;
178 }
179
180 std::array<QPointF, 3> BasicBlockWidget::getExits() const {
181 return { { mapToScene(QPointF( width/3, height)),
182 mapToScene(QPointF( width/2, height)),
183 mapToScene(QPointF(2*width/3, height)) } };
184 }
185