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