1 #include "BasicBlockWidget.hxx"
2 #include "CFGScene.hxx"
3 #include "gui/Mainwindow.hxx"
4 #include "gui/dialogs/SimpleStringDialog.hxx"
5 #include "core/BasicBlock.hxx"
6 #include "core/Function.hxx"
7 #include "core/Comment.hxx"
8 #include "disassembler/Instruction.hxx"
9 #include "core/InformationManager.hxx"
10 #include "core/events/RenameFunctionEvent.hxx"
11 #include "core/events/ChangeCommentEvent.hxx"
14 class CustomQGraphicsTextItem
: public QObject
, public QGraphicsTextItem
{
16 CustomQGraphicsTextItem(const QString
& text
, BasicBlockWidget
* parent
)
17 : QGraphicsTextItem(text
, parent
), parent(parent
) {}
18 void contextMenuEvent(QGraphicsSceneContextMenuEvent
*);
20 void addComment(int row
, bool global
);
22 BasicBlockWidget
* parent
;
25 void CustomQGraphicsTextItem::addComment(int row
, bool global
) {
26 SimpleStringDialog
dialog(global
? "Global comment" : "Local comment");
27 int result
= dialog
.exec();
28 uint64_t address
= parent
->instructions
[row
].getAddress();
29 if (QDialog::Accepted
== result
) {
32 comment
= parent
->block
->getManager()->newGlobalComment(address
);
34 /* TODO: 0x23 as we currently don't have the function here
35 * and setting it to null will make the comment appear
36 * global. Also means local comments are largely still
39 comment
= parent
->block
->getManager()->newLocalComment(address
, (Function
*)0x23);
41 comment
->setText(dialog
.result().toStdString());
42 parent
->block
->getManager()->finishComment(comment
);
44 LOG4CXX_DEBUG(parent
->logger
, "addComment aborted");
48 void CustomQGraphicsTextItem::contextMenuEvent(QGraphicsSceneContextMenuEvent
* event
) {
49 QTextCursor c
= textCursor();
50 c
.setPosition(document()->documentLayout()->hitTest(event
->pos(), Qt::FuzzyHit
));
51 c
.select(QTextCursor::WordUnderCursor
);
55 uint64_t address
= c
.selectedText().toLongLong(&ok
, 16);
56 QTextTable
* table
= c
.currentTable();
58 QAction
* act
= menu
.addAction(c
.selectedText() + " is a Function");
59 QObject::connect(act
, &QAction::triggered
,
61 emit parent
->mainwindow
->requestNewFunctionByAddress(address
);
62 if (NULL
== table
) return;
63 int row
= table
->cellAt(c
).row();
64 uint64_t insAddress
= parent
->instructions
[row
].getAddress();
65 Comment
* comment
= parent
->block
->getManager()->newLocalComment(insAddress
, (Function
*)0x23);
66 comment
->setText("#F<" + c
.selectedText().toStdString() + ">");
67 parent
->block
->getManager()->finishComment(comment
);
72 int row
= table
->cellAt(c
).row();
73 QAction
* globalComment
= menu
.addAction("Add global Comment");
74 QAction
* localComment
= menu
.addAction("Add local Comment");
76 QObject::connect(globalComment
, &QAction::triggered
,
77 [=]() { addComment(row
, true); });
78 QObject::connect(localComment
, &QAction::triggered
,
79 [=]() { addComment(row
, false); });
82 menu
.exec(event
->screenPos());
85 BasicBlockWidget::BasicBlockWidget(const QString
& name
, BasicBlock
* block
,
86 Mainwindow
* mainwindow
)
87 : width(200), height(45), name(name
)
88 , currentColor(defaultColor
), _table(NULL
)
89 , block(block
), mainwindow(mainwindow
)
90 , logger(log4cxx::Logger::getLogger("gui.BasicBlockWidget." + name
.toStdString())) {
91 next
[0] = NULL
; next
[1] = NULL
;
93 QObject::connect(block
->getManager(), &InformationManager::renameFunctionEvent
,
94 [=](RenameFunctionEvent
* event
) {updateFunctionName(event
);});
96 _widget
.reset(new CustomQGraphicsTextItem("", this));
97 _widget
->setPos(5, 20);
98 _widget
->setTextInteractionFlags(Qt::TextSelectableByMouse
|
99 Qt::LinksAccessibleByMouse
);
101 if (width
< 250) width
= 250;
103 QObject::connect(_widget
.get(), &QGraphicsTextItem::linkActivated
,
105 if (str
.startsWith("function:")) {
106 QString address
= str
.remove("function:");
107 mainwindow
->switchMainPlaneToAddress(address
.toInt(NULL
, 16));
108 } else if (str
.startsWith("block:")) {
109 QString address
= str
.remove("block:");
111 /* next[0] is always the jumptarget. On a
112 * conditional jump, next[1] also
113 * contains the following instruction
115 * TODO: Verify we're switching to the
116 * right block -- the target
117 * address matches the next blocks
120 LOG4CXX_TRACE(logger
, "Highlighting block at Address " << address
.toStdString()
121 << " BasicBlockWidget " << std::hex
<< next
[0]);
122 ((CFGScene
*)this->scene())->highlightBlock(next
[0]);
125 instructions
= block
->getInstructions();
127 QObject::connect(block
->getManager(), &InformationManager::changeCommentEvent
,
128 [=](ChangeCommentEvent
* e
) {changeCommentHandler(e
);});
131 void BasicBlockWidget::updateFunctionName(RenameFunctionEvent
*event
) {
132 QString search
= QString("function:") + QString::number(event
->address
, 16);
133 QTextDocument
*document
= _widget
->document();
134 QTextBlock b
= document
->begin();
135 while (b
.isValid()) {
136 for (QTextBlock::iterator i
= b
.begin(); !i
.atEnd(); ++i
) {
137 QTextCharFormat format
= i
.fragment().charFormat();
138 bool isLink
= format
.isAnchor();
141 if (search
== format
.anchorHref()) {
142 LOG4CXX_DEBUG(logger
, i
.fragment().text().toStdString() << " ---> "
143 << format
.anchorHref().toStdString());
145 /* This should select the function name. It stars
146 * by selecting the whole link fragment from back
147 * to front and then moves one word to the back
148 * again deselecting whatever mnemonic is used for
149 * the call instruction.
152 c
.setPosition(i
.fragment().position());
153 c
.movePosition(QTextCursor::Right
, QTextCursor::MoveAnchor
, i
.fragment().length());
154 c
.movePosition(QTextCursor::Left
, QTextCursor::KeepAnchor
, i
.fragment().length());
155 c
.movePosition(QTextCursor::WordRight
, QTextCursor::KeepAnchor
);
156 c
.insertText(event
->new_name
.c_str());
158 QGraphicsTextItem
* item
= _widget
.get();
167 void BasicBlockWidget::changeCommentHandler(ChangeCommentEvent
* event
) {
168 auto inst_it
= std::find_if(instructions
.begin(), instructions
.end(),
169 [=](Instruction
& inst
) {
170 return inst
.getAddress() == event
->address
;
172 if (inst_it
!= instructions
.end()) {
173 if (std::find(inst_it
->comments().begin(),
174 inst_it
->comments().begin(),
175 event
->comment
) == inst_it
->comments().end()) {
176 LOG4CXX_DEBUG(logger
, "Change Comment Event -- New Comment!");
177 inst_it
->comments().push_back(event
->comment
);
179 int row
= inst_it
- instructions
.begin();
180 LOG4CXX_DEBUG(logger
, "Inserting comment for instruction at row " << std::hex
<< row
);
181 QTextCursor cursor
= _table
->cellAt(row
, 2).lastCursorPosition();
182 while (cursor
!= _table
->cellAt(row
, 2).firstCursorPosition()) {
183 cursor
.movePosition(QTextCursor::Left
, QTextCursor::KeepAnchor
, 1);
185 cursor
.removeSelectedText();
186 cursor
.insertHtml(formatComments(&*inst_it
));
187 QGraphicsTextItem
* item
= _widget
.get();
192 void BasicBlockWidget::populateWidget() {
194 QTextTableFormat format
;
195 QTextDocument
* document
= new QTextDocument();
196 format
.setBorderStyle(QTextFrameFormat::BorderStyle_None
);
199 for (Instruction
& inst
: instructions
) {
201 row
= _table
->rows();
202 _table
->appendRows(1);
205 _table
= QTextCursor(document
).insertTable(1, 3, format
);
208 for (uint8_t byte
: inst
.getBytes()) {
209 const char * hexdigits
= "0123456789ABCDEF";
210 bytestring
+= hexdigits
[(byte
>> 4) & 0xF];
211 bytestring
+= hexdigits
[byte
& 0xF];
214 _table
->cellAt(row
, 0).firstCursorPosition().insertHtml("<nobr>" + bytestring
+ "</nobr>");
216 QString line
= inst
.getText().c_str();
217 line
= line
.replace('\t', ' ').toHtmlEscaped();
218 if (inst
.getReference() != "") {
219 QString href
= inst
.getReference().c_str();
220 QStringList list
= href
.split(":");
221 if (list
[0] == "function") {
222 uint64_t address
= href
.split(":")[1].toLongLong(NULL
, 16);
223 Function
* fun
= block
->getManager()->getFunction(address
);
226 line
= line
.split(" ")[0] + " " + QString(fun
->getName().c_str()).toHtmlEscaped();
227 LOG4CXX_DEBUG(logger
, "Naming function at " << address
<< " " << fun
->getName());
230 line
= "<a href=\"" + href
+ "\">" + line
+ "</a>";
232 _table
->cellAt(row
, 1).firstCursorPosition().insertHtml("<nobr>" + line
+ "</nobr>");
233 _table
->cellAt(row
, 2).firstCursorPosition().insertHtml(formatComments(&inst
));
235 QGraphicsTextItem
* item
= _widget
.get();
236 item
->setDocument(document
);
241 QString
BasicBlockWidget::formatComments(Instruction
* inst
) {
242 QStringList comments
;
243 for (Comment
* c
: inst
->comments()) {
244 comments
<< QString(c
->getText().c_str()).toHtmlEscaped();
246 return (comments
.empty() ? "" : ";; ") + comments
.join("<br />").trimmed();
249 void BasicBlockWidget::updateSize() {
250 prepareGeometryChange();
251 width
= 10 + _widget
->boundingRect().width();
252 height
= 25 + _widget
->boundingRect().height();
253 if (width
< 250) width
= 250;
256 void BasicBlockWidget::paint(QPainter
*painter
, const QStyleOptionGraphicsItem
*,
260 painter
->fillRect(0, 0, width
, height
, currentColor
);
261 painter
->setPen(QColor(0x00, 0x00, 0xff, 0xff));
262 painter
->drawRect(0, 0, width
, height
);
263 painter
->drawText(5, 15, name
);
266 QRectF
BasicBlockWidget::boundingRect() const {
268 QRectF
result(- penWidth
/ 2, - penWidth
/ 2,
269 width
+ penWidth
, height
+ penWidth
);
273 std::array
<QPointF
, 3> BasicBlockWidget::getExits() const {
274 return { { mapToScene(QPointF( width
/3, height
)),
275 mapToScene(QPointF( width
/2, height
)),
276 mapToScene(QPointF(2*width
/3, height
)) } };