]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
Add option to comment on instructions (in GUI)
[frida/frida.git] / src / gui / widgets / BasicBlockWidget.cxx
1 #include "BasicBlockWidget.hxx"
2 #include "CustomQGraphicsTextItem.hxx"
3 #include "gui/Mainwindow.hxx"
4 #include "gui/dialogs/SimpleStringDialog.hxx"
5
6 class CustomQGraphicsTextItem : public QObject, public QGraphicsTextItem {
7 public:
8 CustomQGraphicsTextItem(const QString& text, BasicBlockWidget* parent, Mainwindow* mainwindow)
9 : QGraphicsTextItem(text, parent), parent(parent), mainwindow(mainwindow) {}
10 void contextMenuEvent(QGraphicsSceneContextMenuEvent*);
11 private:
12 BasicBlockWidget* parent;
13 Mainwindow* mainwindow;
14 };
15
16 namespace {
17 void addComment(QTextTableCell cell, const QString& title) {
18 SimpleStringDialog dialog(title);
19 int result = dialog.exec();
20 if (QDialog::Accepted == result) {
21 // LOG4CXX_DEBUG(logger, "adding comment " << dialog.result().toStdString()
22 // << " at row " << cell.row());
23 cell.firstCursorPosition().insertHtml(QString(";; ") + dialog.result());
24 } else {
25 // LOG4CXX_DEBUG(logger, "addComment aborted");
26 }
27 }
28 }
29
30 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
31 QTextCursor c = textCursor();
32 c.setPosition(document()->documentLayout()->hitTest(event->pos(), Qt::FuzzyHit));
33 c.select(QTextCursor::WordUnderCursor);
34
35 QMenu menu;
36 bool ok;
37 uint64_t address = c.selectedText().toLongLong(&ok, 16);
38 if (ok) {
39 QAction* act = menu.addAction(c.selectedText() + " is a Function");
40 QObject::connect(act, &QAction::triggered,
41 [=]() {mainwindow->requestNewFunctionByAddress(address);});
42 }
43
44 QTextTable* table = c.currentTable();
45 if (NULL != table) {
46 int row = table->cellAt(c).row();
47 QTextTableCell cell = table->cellAt(row, 2);
48 QAction* globalComment = menu.addAction("Add global Comment");
49 QAction* localComment = menu.addAction("Add local Comment");
50
51 QObject::connect(globalComment, &QAction::triggered,
52 [=]() { addComment(cell, "Global comment"); });
53 QObject::connect(localComment, &QAction::triggered,
54 [=]() { addComment(cell, "Local comment"); });
55 }
56
57 menu.exec(event->screenPos());
58 }
59
60 BasicBlockWidget::BasicBlockWidget(const QString& name, BasicBlock * block,
61 Mainwindow * mainwindow)
62 : width(270), height(45), name(name)
63 , _table(NULL)
64 , block(block), mainwindow(mainwindow) {
65 next[0] = NULL; next[1] = NULL;
66 _widget.reset(new CustomQGraphicsTextItem("", this, mainwindow));
67 _widget->setPos(5, 20);
68 _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
69 Qt::LinksAccessibleByMouse);
70
71 if (width < 250) width = 250;
72 }
73
74 void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes,
75 QString line, const QString& href) {
76 QString bytestring;
77 int row;
78
79 if (_table) {
80 row = _table->rows();
81 _table->appendRows(1);
82 } else {
83 row = 0;
84 QTextTableFormat format;
85 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
86 format.setBorder(0);
87 _table = _widget->textCursor().insertTable(1, 3, format);
88 }
89
90 for (size_t i(0); i < num_bytes; ++i) {
91 const char * hexdigits = "0123456789ABCDEF";
92 bytestring += hexdigits[(bytes[i] >> 4) & 0xF];
93 bytestring += hexdigits[bytes[i] & 0xF];
94 bytestring += ' ';
95 }
96
97 _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
98
99 line = line.replace('\t', ' ').toHtmlEscaped();
100 if (href != "") {
101 line = "<a href=\"" + href + "\">" + line + "</a>";
102 }
103
104 _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
105
106 QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
107 [=](QString str) {
108 if (str.startsWith("function:")) {
109 QString address = str.remove("function:");
110 mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
111 }
112 });
113 }
114
115 void BasicBlockWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
116 QWidget *widget) {
117 width = 10 + _widget->boundingRect().width();
118 height = 25 + _widget->boundingRect().height();
119 if (width < 250) width = 250;
120
121 painter->fillRect(0, 0, width, height, QColor(0xcc, 0xcc, 0xff, 0xff));
122 painter->setPen(QColor(0x00, 0x00, 0xff, 0xff));
123 painter->drawRect(0, 0, width, height);
124 painter->drawText(5, 15, name);
125 }
126
127 QRectF BasicBlockWidget::boundingRect() const {
128 qreal penWidth = 1;
129 QRectF result(- penWidth / 2, - penWidth / 2,
130 width + penWidth, height + penWidth);
131 return result;
132 }
133
134 std::array<QPointF, 3> BasicBlockWidget::getExits() const {
135 return { { mapToScene(QPointF( width/3, height)),
136 mapToScene(QPointF( width/2, height)),
137 mapToScene(QPointF(2*width/3, height)) } };
138 }
139