-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathDebugActions.cpp
More file actions
444 lines (399 loc) · 16.4 KB
/
DebugActions.cpp
File metadata and controls
444 lines (399 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "DebugActions.h"
#include "core/MainWindow.h"
#include "dialogs/AttachProcDialog.h"
#include "dialogs/NativeDebugDialog.h"
#include "common/Configuration.h"
#include "common/Helpers.h"
#include "shortcuts/ShortcutManager.h"
#include <QPainter>
#include <QMenu>
#include <QList>
#include <QFileInfo>
#include <QToolBar>
#include <QToolButton>
#include <QSettings>
DebugActions::DebugActions(QToolBar *toolBar, MainWindow *main) : QObject(main), main(main)
{
setObjectName("DebugActions");
// setIconSize(QSize(16, 16));
// define icons
QIcon startEmulIcon = QIcon(":/img/icons/play_light_emul.svg");
QIcon startAttachIcon = QIcon(":/img/icons/play_light_attach.svg");
QIcon startRemoteIcon = QIcon(":/img/icons/play_light_remote.svg");
QIcon continueBackIcon = QIcon(":/img/icons/reverse_continue.svg");
QIcon stepBackIcon = QIcon(":/img/icons/reverse_step.svg");
startTraceIcon = QIcon(":/img/icons/start_trace.svg");
stopTraceIcon = QIcon(":/img/icons/stop_trace.svg");
stopIcon = QIcon(":/img/icons/media-stop_light.svg");
restartIcon = QIcon(":/img/icons/spin_light.svg");
detachIcon = QIcon(":/img/icons/detach_debugger.svg");
startDebugIcon = QIcon(":/img/icons/play_light_debug.svg");
continueIcon = QIcon(":/img/icons/media-skip-forward_light.svg");
suspendIcon = QIcon(":/img/icons/media-suspend_light.svg");
// define action labels
QString startEmulLabel = tr("Start &emulation");
QString startAttachLabel = tr("&Attach to process");
QString startRemoteLabel = tr("Connect to a &remote debugger");
QString stopDebugLabel = tr("Stop debug");
QString stopEmulLabel = tr("Stop emulation");
QString restartEmulLabel = tr("Restart emulation");
QString continueUMLabel = tr("Continue until main");
QString continueUCLabel = tr("Continue until call");
QString continueUSLabel = tr("Continue until syscall");
startTraceLabel = tr("Start trace session");
stopTraceLabel = tr("Stop trace session");
suspendLabel = tr("Suspend the process");
continueLabel = tr("Continue");
restartDebugLabel = tr("Restart program");
startDebugLabel = tr("Start debug");
// define actions
actionStart = Shortcuts()->makeAction("Debug.start", this);
actionContinue = Shortcuts()->makeAction("Debug.continue", this);
actionContinueBack = Shortcuts()->makeAction("Debug.continueBack", this);
actionStep = Shortcuts()->makeAction("Debug.step", this);
actionStepOver = Shortcuts()->makeAction("Debug.stepOver", this);
actionStepOut = Shortcuts()->makeAction("Debug.stepOut", this);
actionStepBack = Shortcuts()->makeAction("Debug.stepBack", this);
actionStart->setIcon(startDebugIcon);
actionContinue->setIcon(continueIcon);
actionContinueBack->setIcon(continueBackIcon);
actionStepBack->setIcon(stepBackIcon);
actionStartEmul = new QAction(startEmulIcon, startEmulLabel, this);
actionAttach = new QAction(startAttachIcon, startAttachLabel, this);
actionStartRemote = new QAction(startRemoteIcon, startRemoteLabel, this);
actionStop = new QAction(stopIcon, stopDebugLabel, this);
actionContinueUntilMain = new QAction(continueUMLabel, this);
actionContinueUntilCall = new QAction(continueUCLabel, this);
actionContinueUntilSyscall = new QAction(continueUSLabel, this);
actionTrace = new QAction(startTraceIcon, startTraceLabel, this);
QToolButton *startButton = new QToolButton;
startButton->setPopupMode(QToolButton::MenuButtonPopup);
connect(startButton, &QToolButton::triggered, startButton, &QToolButton::setDefaultAction);
QMenu *startMenu = new QMenu(startButton);
startMenu->addAction(actionStart);
startMenu->addAction(actionStartEmul);
startMenu->addAction(actionAttach);
startMenu->addAction(actionStartRemote);
startButton->setDefaultAction(actionStart);
startButton->setMenu(startMenu);
continueUntilButton = new QToolButton;
continueUntilButton->setPopupMode(QToolButton::MenuButtonPopup);
connect(continueUntilButton, &QToolButton::triggered, continueUntilButton,
&QToolButton::setDefaultAction);
QMenu *continueUntilMenu = new QMenu(continueUntilButton);
continueUntilMenu->addAction(actionContinueUntilMain);
continueUntilMenu->addAction(actionContinueUntilCall);
continueUntilMenu->addAction(actionContinueUntilSyscall);
continueUntilButton->setMenu(continueUntilMenu);
continueUntilButton->setDefaultAction(actionContinueUntilMain);
// define toolbar widgets and actions
toolBar->addWidget(startButton);
toolBar->addAction(actionContinue);
toolBar->addAction(actionStop);
actionAllContinues = toolBar->addWidget(continueUntilButton);
toolBar->addAction(actionStepOver);
toolBar->addAction(actionStep);
toolBar->addAction(actionStepOut);
toolBar->addAction(actionStepBack);
toolBar->addAction(actionContinueBack);
toolBar->addAction(actionTrace);
allActions = { actionStop,
actionAllContinues,
actionContinue,
actionContinueUntilCall,
actionContinueUntilMain,
actionContinueUntilSyscall,
actionStep,
actionStepOut,
actionStepOver,
actionContinueBack,
actionStepBack,
actionTrace };
// Hide all actions
setAllActionsVisible(false);
// Toggle all buttons except reverse step/continue which are handled separately and
// restart, suspend(=continue) and stop since those are necessary to avoid freezing
toggleActions = { actionStepOver,
actionStep,
actionStepOut,
actionContinueUntilMain,
actionContinueUntilCall,
actionContinueUntilSyscall,
actionTrace };
toggleConnectionActions = { actionAttach, actionStartRemote };
reverseActions = { actionStepBack, actionContinueBack };
connect(Core(), &CutterCore::debugProcessFinished, this, [=](int pid) {
QMessageBox msgBox(main);
msgBox.setText(tr("Debugged process exited (") + QString::number(pid) + ")");
msgBox.exec();
});
connect(Core(), &CutterCore::debugTaskStateChanged, this, [=]() {
bool disableToolbar = Core()->isDebugTaskInProgress();
if (Core()->currentlyDebugging) {
for (QAction *a : toggleActions) {
a->setDisabled(disableToolbar);
}
// Suspend should only be available when other icons are disabled
if (disableToolbar) {
actionContinue->setText(suspendLabel);
actionContinue->setIcon(suspendIcon);
} else {
actionContinue->setText(continueLabel);
actionContinue->setIcon(continueIcon);
}
for (QAction *a : reverseActions) {
a->setVisible(Core()->currentlyTracing);
a->setDisabled(disableToolbar);
}
} else {
for (QAction *a : toggleConnectionActions) {
a->setDisabled(disableToolbar);
}
}
});
connect(actionStop, &QAction::triggered, Core(), &CutterCore::stopDebug);
connect(actionStop, &QAction::triggered, [=]() {
actionStart->setVisible(true);
actionStartEmul->setVisible(true);
actionAttach->setVisible(true);
actionStartRemote->setVisible(true);
actionStop->setText(stopDebugLabel);
actionStop->setIcon(stopIcon);
actionStart->setText(startDebugLabel);
actionStart->setIcon(startDebugIcon);
actionStartEmul->setText(startEmulLabel);
actionStartEmul->setIcon(startEmulIcon);
continueUntilButton->setDefaultAction(actionContinueUntilMain);
setAllActionsVisible(false);
});
connect(actionStep, &QAction::triggered, Core(), &CutterCore::stepDebug);
connect(actionStepBack, &QAction::triggered, Core(), &CutterCore::stepBackDebug);
connect(actionStart, &QAction::triggered, this, &DebugActions::startDebug);
connect(actionAttach, &QAction::triggered, this, &DebugActions::attachProcessDialog);
connect(actionStartRemote, &QAction::triggered, this, &DebugActions::attachRemoteDialog);
connect(Core(), &CutterCore::attachedRemote, this, &DebugActions::onAttachedRemoteDebugger);
connect(actionStartEmul, &QAction::triggered, Core(), &CutterCore::startEmulation);
connect(actionStartEmul, &QAction::triggered, [=]() {
setAllActionsVisible(true);
actionStart->setVisible(false);
actionAttach->setVisible(false);
actionStartRemote->setVisible(false);
actionContinueUntilMain->setVisible(false);
actionStepOut->setVisible(false);
continueUntilButton->setDefaultAction(actionContinueUntilSyscall);
actionStartEmul->setText(restartEmulLabel);
actionStartEmul->setIcon(restartIcon);
actionStop->setText(stopEmulLabel);
// Reverse debug actions aren't visible until we start tracing
for (QAction *a : reverseActions) {
a->setVisible(false);
}
});
connect(actionStepOver, &QAction::triggered, Core(), &CutterCore::stepOverDebug);
connect(actionStepOut, &QAction::triggered, Core(), &CutterCore::stepOutDebug);
connect(actionContinueUntilMain, &QAction::triggered, this, &DebugActions::continueUntilMain);
connect(actionContinueUntilCall, &QAction::triggered, Core(), &CutterCore::continueUntilCall);
connect(actionContinueUntilSyscall, &QAction::triggered, Core(),
&CutterCore::continueUntilSyscall);
connect(actionContinueBack, &QAction::triggered, Core(), &CutterCore::continueBackDebug);
connect(actionContinue, &QAction::triggered, Core(), [=]() {
// Switch between continue and suspend depending on the debugger's state
if (Core()->isDebugTaskInProgress()) {
Core()->suspendDebug();
} else {
Core()->continueDebug();
}
});
connect(actionTrace, &QAction::triggered, Core(), [=]() {
// Check if a debug session was created to switch between start and stop
if (!Core()->currentlyTracing) {
Core()->startTraceSession();
actionTrace->setText(stopTraceLabel);
actionTrace->setIcon(stopTraceIcon);
} else {
Core()->stopTraceSession();
actionTrace->setText(startTraceLabel);
actionTrace->setIcon(startTraceIcon);
}
});
connect(Config(), &Configuration::interfaceThemeChanged, this, &DebugActions::chooseThemeIcons);
chooseThemeIcons();
}
void DebugActions::setButtonVisibleIfMainExists()
{
RzCoreLocked core = Core()->lock();
// if main is not a flag we hide the continue until main button
if (!rz_flag_get(core->flags, "sym.main") && !rz_flag_get(core->flags, "main")) {
actionContinueUntilMain->setVisible(false);
continueUntilButton->setDefaultAction(actionContinueUntilCall);
}
}
void DebugActions::showDebugWarning()
{
if (!acceptedDebugWarning) {
acceptedDebugWarning = true;
QMessageBox msgBox(main);
msgBox.setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
msgBox.setText(tr("Debug is currently in beta.\n")
+ tr("If you encounter any problems or have suggestions, please submit an "
"issue to https://github.com/rizinorg/cutter/issues"));
msgBox.exec();
}
}
void DebugActions::continueUntilMain()
{
RzCoreLocked core(Core()->lock());
RzFlagItem *main_flag = rz_flag_get(core->flags, "sym.main");
if (!main_flag) {
main_flag = rz_flag_get(core->flags, "main");
if (!main_flag) {
return;
}
}
Core()->continueUntilDebug(main_flag->offset);
}
void DebugActions::attachRemoteDebugger()
{
QString stopAttachLabel = tr("Detach from process");
// Hide unwanted buttons
setAllActionsVisible(true);
actionStart->setVisible(false);
actionStartRemote->setVisible(false);
actionStartEmul->setVisible(false);
actionStop->setText(stopAttachLabel);
}
void DebugActions::onAttachedRemoteDebugger(bool successfully)
{
// TODO(#2829): Investigate why this is happening
if (remoteDialog == nullptr)
return;
if (!successfully) {
QMessageBox msgBox(main);
msgBox.setText(tr("Error connecting."));
msgBox.exec();
attachRemoteDialog();
} else {
QSettings settings;
QStringList ips = settings.value("recentIpList").toStringList();
ips.removeAll(remoteDialog->getUri());
ips.prepend(remoteDialog->getUri());
settings.setValue("recentIpList", ips);
delete remoteDialog;
remoteDialog = nullptr;
attachRemoteDebugger();
}
}
void DebugActions::attachRemoteDialog()
{
showDebugWarning();
if (!remoteDialog) {
remoteDialog = new RemoteDebugDialog(main);
}
QMessageBox msgBox(main);
bool success = false;
while (!success) {
success = true;
if (remoteDialog->exec()) {
if (!remoteDialog->validate()) {
success = false;
continue;
}
Core()->attachRemote(remoteDialog->getUri());
}
}
}
void DebugActions::attachProcessDialog()
{
showDebugWarning();
AttachProcDialog dialog(main);
bool success = false;
while (!success) {
success = true;
if (dialog.exec()) {
int pid = dialog.getPID();
if (pid >= 0) {
attachProcess(pid);
} else {
success = false;
QMessageBox msgBox(main);
msgBox.setText(tr("Error attaching. No process selected!"));
msgBox.exec();
}
}
}
}
void DebugActions::attachProcess(int pid)
{
QString stopAttachLabel = tr("Detach from process");
// hide unwanted buttons
setAllActionsVisible(true);
actionStart->setVisible(false);
actionStartRemote->setVisible(false);
actionStartEmul->setVisible(false);
actionStop->setText(stopAttachLabel);
actionStop->setIcon(detachIcon);
// attach
Core()->attachDebug(pid);
}
void DebugActions::startDebug()
{
// check if file is executable before starting debug
QString filename = Core()->getConfig("file.path");
QFileInfo info(filename);
if (!Core()->currentlyDebugging && !info.isExecutable()) {
QMessageBox msgBox(main);
msgBox.setText(tr("File '%1' does not have executable permissions.").arg(filename));
msgBox.exec();
return;
}
showDebugWarning();
NativeDebugDialog dialog(main);
dialog.setArgs(Core()->getConfig("dbg.args"));
QString args;
if (dialog.exec()) {
args = dialog.getArgs();
} else {
return;
}
// Update dbg.args with the new args
Core()->setConfig("dbg.args", args);
setAllActionsVisible(true);
actionAttach->setVisible(false);
actionStartRemote->setVisible(false);
actionStartEmul->setVisible(false);
actionStart->setText(restartDebugLabel);
actionStart->setIcon(restartIcon);
setButtonVisibleIfMainExists();
// Reverse debug actions aren't visible until we start tracing
for (QAction *a : reverseActions) {
a->setVisible(false);
}
actionTrace->setText(startTraceLabel);
actionTrace->setIcon(startTraceIcon);
Core()->startDebug();
}
void DebugActions::setAllActionsVisible(bool visible)
{
for (QAction *action : allActions) {
action->setVisible(visible);
}
}
/**
* @brief When theme changed, change icons which have a special version for the theme.
*/
void DebugActions::chooseThemeIcons()
{
// List of QActions which have alternative icons in different themes
const QList<QPair<void *, QString>> kSupportedIconsNames {
{ actionStep, QStringLiteral("step_into.svg") },
{ actionStepOver, QStringLiteral("step_over.svg") },
{ actionStepOut, QStringLiteral("step_out.svg") },
{ actionContinueUntilMain, QStringLiteral("continue_until_main.svg") },
{ actionContinueUntilCall, QStringLiteral("continue_until_call.svg") },
{ actionContinueUntilSyscall, QStringLiteral("continue_until_syscall.svg") },
};
// Set the correct icon for the QAction
qhelpers::setThemeIcons(kSupportedIconsNames, [](void *obj, const QIcon &icon) {
static_cast<QAction *>(obj)->setIcon(icon);
});
}