]> git.siccegge.de Git - frida/frida.git/blob - src/gui/widgets/BasicBlockWidget.cxx
8b6dbe56d7ff336ac5331b7f2e5842824ba69384
[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, 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 , logger(log4cxx::Logger::getLogger(name.toStdString() + "BasicBlock")) {
66 next[0] = NULL; next[1] = NULL;
67 _widget.reset(new CustomQGraphicsTextItem("", this, mainwindow));
68 _widget->setPos(5, 20);
69 _widget->setTextInteractionFlags(Qt::TextSelectableByMouse|
70 Qt::LinksAccessibleByMouse);
71
72 if (width < 250) width = 250;
73 }
74
75 void BasicBlockWidget::addItem(uint8_t* bytes, size_t num_bytes,
76 QString line, const QString& href) {
77 QString bytestring;
78 int row;
79
80 if (_table) {
81 row = _table->rows();
82 _table->appendRows(1);
83 } else {
84 row = 0;
85 QTextTableFormat format;
86 format.setBorderStyle(QTextFrameFormat::BorderStyle_None);
87 format.setBorder(0);
88 _table = _widget->textCursor().insertTable(1, 3, format);
89 }
90
91 for (size_t i(0); i < num_bytes; ++i) {
92 const char * hexdigits = "0123456789ABCDEF";
93 bytestring += hexdigits[(bytes[i] >> 4) & 0xF];
94 bytestring += hexdigits[bytes[i] & 0xF];
95 bytestring += ' ';
96 }
97
98 _table->cellAt(row, 0).firstCursorPosition().insertText(bytestring);
99
100 line = line.replace('\t', ' ').toHtmlEscaped();
101 if (href != "") {
102 line = "<a href=\"" + href + "\">" + line + "</a>";
103 }
104
105 _table->cellAt(row, 1).firstCursorPosition().insertHtml(line);
106
107 QObject::connect(_widget.get(), &QGraphicsTextItem::linkActivated,
108 [=](QString str) {
109 if (str.startsWith("function:")) {
110 QString address = str.remove("function:");
111 mainwindow->switchMainPlaneToAddress(address.toInt(NULL, 16));
112 }
113 });
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