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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "iosrunner.h"
#include "devicectlutils.h"
#include "iosconfigurations.h"
#include "iosconstants.h"
#include "iosdevice.h"
#include "iosrunconfiguration.h"
#include "iossimulator.h"
#include "iostoolhandler.h"
#include "iostr.h"
#include <debugger/debuggerconstants.h>
#include <debugger/debuggerkitaspect.h>
#include <debugger/debuggerruncontrol.h>
#include <projectexplorer/devicesupport/devicekitaspects.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/qmldebugcommandlinearguments.h>
#include <projectexplorer/runconfigurationaspects.h>
#include <projectexplorer/target.h>
#include <projectexplorer/taskhub.h>
#include <projectexplorer/toolchain.h>
#include <qmldebug/qmloutputparser.h>
#include <utils/fileutils.h>
#include <utils/qtcprocess.h>
#include <utils/stringutils.h>
#include <utils/temporaryfile.h>
#include <utils/url.h>
#include <utils/utilsicons.h>
#include <solutions/tasking/tasktree.h>
#include <QDateTime>
#include <QDir>
#include <QJsonArray>
#include <QMessageBox>
#include <QRegularExpression>
#include <QSettings>
#include <QTcpServer>
#include <QTime>
#include <QTimer>
#include <memory>
#include <fcntl.h>
#ifdef Q_OS_UNIX
#include <unistd.h>
#else
#include <io.h>
#endif
using namespace Debugger;
using namespace ProjectExplorer;
using namespace Utils;
using namespace Tasking;
namespace Ios::Internal {
char QML_DEBUGGER_WAITING[] = "QML Debugger: Waiting for connection on port ([0-9]+)...";
static QString identifierForRunControl(RunControl *runControl)
{
const IosDeviceTypeAspect::Data *data = runControl->aspectData<IosDeviceTypeAspect>();
return data ? data->deviceType.identifier : QString();
}
static void stopRunningRunControl(RunControl *runControl)
{
static QMap<Id, QPointer<RunControl>> activeRunControls;
// clean up deleted
Utils::erase(activeRunControls, [](const QPointer<RunControl> &rc) { return !rc; });
Target *target = runControl->target();
const Id devId = RunDeviceKitAspect::deviceId(target->kit());
const QString identifier = identifierForRunControl(runControl);
// The device can only run an application at a time, if an app is running stop it.
if (QPointer<RunControl> activeRunControl = activeRunControls[devId]) {
if (identifierForRunControl(activeRunControl) == identifier) {
activeRunControl->initiateStop();
activeRunControls.remove(devId);
}
}
if (devId.isValid())
activeRunControls[devId] = runControl;
}
static QString getBundleIdentifier(const FilePath &bundlePath)
{
QSettings settings(bundlePath.pathAppended("Info.plist").toUrlishString(), QSettings::NativeFormat);
return settings.value(QString::fromLatin1("CFBundleIdentifier")).toString();
}
struct AppInfo
{
QUrl pathOnDevice;
qint64 processIdentifier = -1;
};
class DeviceCtlRunnerBase : public RunWorker
{
public:
DeviceCtlRunnerBase(RunControl *runControl);
void start();
qint64 processIdentifier() const { return m_processIdentifier; }
protected:
GroupItem findApp(const QString &bundleIdentifier, Storage<AppInfo> appInfo);
GroupItem findProcess(Storage<AppInfo> &appInfo);
void reportStoppedImpl();
IosDevice::ConstPtr m_device;
QStringList m_arguments;
qint64 m_processIdentifier = -1;
private:
GroupItem killProcess(Storage<AppInfo> &appInfo);
virtual GroupItem launchTask(const QString &bundleIdentifier) = 0;
FilePath m_bundlePath;
std::unique_ptr<TaskTree> m_startTask;
};
class DeviceCtlPollingRunner final : public DeviceCtlRunnerBase
{
public:
DeviceCtlPollingRunner(RunControl *runControl);
void stop();
private:
GroupItem launchTask(const QString &bundleIdentifier);
void checkProcess();
std::unique_ptr<TaskTree> m_stopTask;
std::unique_ptr<TaskTree> m_pollTask;
QTimer m_pollTimer;
};
class DeviceCtlRunner final : public DeviceCtlRunnerBase
{
public:
DeviceCtlRunner(RunControl *runControl);
void stop();
void setStartStopped(bool startStopped) { m_startStopped = startStopped; }
private:
GroupItem launchTask(const QString &bundleIdentifier);
Process m_process;
std::unique_ptr<TemporaryFile> m_deviceCtlOutput;
std::unique_ptr<TaskTree> m_processIdTask;
bool m_startStopped = false;
};
DeviceCtlRunnerBase::DeviceCtlRunnerBase(RunControl *runControl)
: RunWorker(runControl)
{
setId("IosDeviceCtlRunner");
const IosDeviceTypeAspect::Data *data = runControl->aspectData<IosDeviceTypeAspect>();
QTC_ASSERT(data, return);
m_bundlePath = data->bundleDirectory;
m_arguments = ProcessArgs::splitArgs(runControl->commandLine().arguments(), OsTypeMac);
m_device = std::dynamic_pointer_cast<const IosDevice>(RunDeviceKitAspect::device(runControl->kit()));
}
GroupItem DeviceCtlRunnerBase::findApp(const QString &bundleIdentifier, Storage<AppInfo> appInfo)
{
const auto onSetup = [this](Process &process) {
if (!m_device)
return SetupResult::StopWithSuccess; // don't block the following tasks
process.setCommand({FilePath::fromString("/usr/bin/xcrun"),
{"devicectl",
"device",
"info",
"apps",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
"-"}});
return SetupResult::Continue;
};
const auto onDone = [this, bundleIdentifier, appInfo](const Process &process) {
if (process.error() != QProcess::UnknownError) {
reportFailure(Tr::tr("Failed to run devicectl: %1.").arg(process.errorString()));
return DoneResult::Error;
}
const expected_str<QUrl> pathOnDevice = parseAppInfo(process.rawStdOut(), bundleIdentifier);
if (pathOnDevice) {
appInfo->pathOnDevice = *pathOnDevice;
return DoneResult::Success;
}
reportFailure(pathOnDevice.error());
return DoneResult::Error;
};
return ProcessTask(onSetup, onDone);
}
GroupItem DeviceCtlRunnerBase::findProcess(Storage<AppInfo> &appInfo)
{
const auto onSetup = [this, appInfo](Process &process) {
if (!m_device || appInfo->pathOnDevice.isEmpty())
return SetupResult::StopWithSuccess; // don't block the following tasks
process.setCommand(
{FilePath::fromString("/usr/bin/xcrun"),
{"devicectl",
"device",
"info",
"processes",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
"-",
"--filter",
QLatin1String("executable.path BEGINSWITH '%1'").arg(appInfo->pathOnDevice.path())}});
return SetupResult::Continue;
};
const auto onDone = [this, appInfo](const Process &process) {
const Utils::expected_str<qint64> pid = parseProcessIdentifier(process.rawStdOut());
if (pid) {
appInfo->processIdentifier = *pid;
return DoneResult::Success;
}
reportFailure(pid.error());
return DoneResult::Error;
};
return ProcessTask(onSetup, onDone);
}
GroupItem DeviceCtlRunnerBase::killProcess(Storage<AppInfo> &appInfo)
{
const auto onSetup = [this, appInfo](Process &process) {
if (!m_device || appInfo->processIdentifier < 0)
return SetupResult::StopWithSuccess; // don't block the following tasks
process.setCommand({FilePath::fromString("/usr/bin/xcrun"),
{"devicectl",
"device",
"process",
"signal",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
"-",
"--signal",
"SIGKILL",
"--pid",
QString::number(appInfo->processIdentifier)}});
return SetupResult::Continue;
};
return ProcessTask(onSetup, DoneResult::Success); // we tried our best and don't care at this point
}
GroupItem DeviceCtlPollingRunner::launchTask(const QString &bundleIdentifier)
{
const auto onSetup = [this, bundleIdentifier](Process &process) {
if (!m_device) {
reportFailure(Tr::tr("Running failed. No iOS device found."));
return SetupResult::StopWithError;
}
process.setCommand({FilePath::fromString("/usr/bin/xcrun"),
{"devicectl",
"device",
"process",
"launch",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
"-",
bundleIdentifier,
m_arguments}});
return SetupResult::Continue;
};
const auto onDone = [this](const Process &process, DoneWith result) {
if (result == DoneWith::Cancel) {
reportFailure(Tr::tr("Running canceled."));
return DoneResult::Error;
}
if (process.error() != QProcess::UnknownError) {
reportFailure(Tr::tr("Failed to run devicectl: %1.").arg(process.errorString()));
return DoneResult::Error;
}
const Utils::expected_str<qint64> pid = parseLaunchResult(process.rawStdOut());
if (pid) {
m_processIdentifier = *pid;
m_pollTimer.start();
reportStarted();
return DoneResult::Success;
}
// failure
reportFailure(pid.error());
return DoneResult::Error;
};
return ProcessTask(onSetup, onDone);
}
void DeviceCtlRunnerBase::reportStoppedImpl()
{
appendMessage(Tr::tr("\"%1\" exited.").arg(m_bundlePath.toUserOutput()),
Utils::NormalMessageFormat);
reportStopped();
}
void DeviceCtlRunnerBase::start()
{
const QString bundleIdentifier = getBundleIdentifier(m_bundlePath);
if (bundleIdentifier.isEmpty()) {
reportFailure(Tr::tr("Failed to determine bundle identifier."));
return;
}
appendMessage(Tr::tr("Running \"%1\" on %2...")
.arg(m_bundlePath.toUserOutput(), runControl()->device()->displayName()),
NormalMessageFormat);
// If the app is already running, we should first kill it, then launch again.
// Usually deployment already kills the running app, but we support running without
// deployment. Restarting is then e.g. needed if the app arguments changed.
// Unfortunately the information about running processes only includes the path
// on device and processIdentifier.
// So we find out if the app is installed, and its path on device.
// Check if a process is running for that path, and get its processIdentifier.
// Try to kill that.
// Then launch the app (again).
Storage<AppInfo> appInfo;
m_startTask.reset(new TaskTree(Group{
sequential,
appInfo,
findApp(bundleIdentifier, appInfo),
findProcess(appInfo),
killProcess(appInfo),
launchTask(bundleIdentifier)}));
m_startTask->start();
}
DeviceCtlPollingRunner::DeviceCtlPollingRunner(RunControl *runControl)
: DeviceCtlRunnerBase(runControl)
{
using namespace std::chrono_literals;
m_pollTimer.setInterval(500ms); // not too often since running devicectl takes time
connect(&m_pollTimer, &QTimer::timeout, this, &DeviceCtlPollingRunner::checkProcess);
}
void DeviceCtlPollingRunner::stop()
{
// stop polling, we handle the reportStopped in the done handler
m_pollTimer.stop();
if (m_pollTask)
m_pollTask.release()->deleteLater();
const auto onSetup = [this](Process &process) {
if (!m_device) {
reportStoppedImpl();
return SetupResult::StopWithError;
}
process.setCommand({FilePath::fromString("/usr/bin/xcrun"),
{"devicectl",
"device",
"process",
"signal",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
"-",
"--signal",
"SIGKILL",
"--pid",
QString::number(m_processIdentifier)}});
return SetupResult::Continue;
};
const auto onDone = [this](const Process &process) {
if (process.error() != QProcess::UnknownError) {
reportFailure(Tr::tr("Failed to run devicectl: %1.").arg(process.errorString()));
return DoneResult::Error;
}
const Utils::expected_str<QJsonValue> resultValue = parseDevicectlResult(
process.rawStdOut());
if (!resultValue) {
reportFailure(resultValue.error());
return DoneResult::Error;
}
reportStoppedImpl();
return DoneResult::Success;
};
m_stopTask.reset(new TaskTree(Group{ProcessTask(onSetup, onDone)}));
m_stopTask->start();
}
void DeviceCtlPollingRunner::checkProcess()
{
if (m_pollTask)
return;
const auto onSetup = [this](Process &process) {
if (!m_device)
return SetupResult::StopWithError;
process.setCommand(
{FilePath::fromString("/usr/bin/xcrun"),
{"devicectl",
"device",
"info",
"processes",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
"-",
"--filter",
QLatin1String("processIdentifier == %1").arg(QString::number(m_processIdentifier))}});
return SetupResult::Continue;
};
const auto onDone = [this](const Process &process) {
const Utils::expected_str<QJsonValue> resultValue = parseDevicectlResult(
process.rawStdOut());
if (!resultValue || (*resultValue)["runningProcesses"].toArray().size() < 1) {
// no process with processIdentifier found, or some error occurred, device disconnected
// or such, assume "stopped"
m_pollTimer.stop();
reportStoppedImpl();
}
if (m_pollTask)
m_pollTask.release()->deleteLater();
return DoneResult::Success;
};
m_pollTask.reset(new TaskTree(Group{ProcessTask(onSetup, onDone)}));
m_pollTask->start();
}
DeviceCtlRunner::DeviceCtlRunner(RunControl *runControl)
: DeviceCtlRunnerBase(runControl)
{}
void DeviceCtlRunner::stop()
{
if (m_process.isRunning())
m_process.stop();
}
GroupItem DeviceCtlRunner::launchTask(const QString &bundleIdentifier)
{
const auto onSetup = [this, bundleIdentifier] {
if (!m_device) {
reportFailure(Tr::tr("Running failed. No iOS device found."));
return false;
}
m_deviceCtlOutput.reset(new TemporaryFile("devicectl"));
if (!m_deviceCtlOutput->open() || m_deviceCtlOutput->fileName().isEmpty()) {
reportFailure(Tr::tr("Running failed. Failed to create the temporary output file."));
return false;
}
const QStringList startStoppedArg = m_startStopped ? QStringList("--start-stopped")
: QStringList();
const QStringList arguments = QStringList(
{"devicectl",
"device",
"process",
"launch",
"--device",
m_device->uniqueInternalDeviceId(),
"--quiet",
"--json-output",
m_deviceCtlOutput->fileName()})
+ startStoppedArg
+ QStringList({"--console", bundleIdentifier}) + m_arguments;
m_process.setCommand({FilePath::fromString("/usr/bin/xcrun"), arguments});
connect(&m_process, &Process::started, this, [this, bundleIdentifier] {
// devicectl does report the process ID in its json output, but that is broken
// for --console. When that is used, the json output is only written after the process
// finished, which is not helpful.
// Manually find the process ID for the bundle identifier.
Storage<AppInfo> appInfo;
m_processIdTask.reset(new TaskTree(Group{
sequential,
appInfo,
findApp(bundleIdentifier, appInfo),
findProcess(appInfo),
onGroupDone([this, appInfo](DoneWith doneWith) {
if (doneWith == DoneWith::Success) {
m_processIdentifier = appInfo->processIdentifier;
reportStarted();
} else {
reportFailure(Tr::tr("Failed to retrieve process ID."));
}
})}));
m_processIdTask->start();
});
connect(&m_process, &Process::done, this, [this] {
if (m_process.error() != QProcess::UnknownError)
reportFailure(Tr::tr("Failed to run devicectl: %1.").arg(m_process.errorString()));
m_deviceCtlOutput->reset();
m_processIdTask.reset();
reportStoppedImpl();
});
connect(&m_process, &Process::readyReadStandardError, this, [this] {
appendMessage(m_process.readAllStandardError(), StdErrFormat, false);
});
connect(&m_process, &Process::readyReadStandardOutput, this, [this] {
appendMessage(m_process.readAllStandardOutput(), StdOutFormat, false);
});
m_process.start();
return true;
};
return Sync(onSetup);
}
class IosRunner : public RunWorker
{
public:
IosRunner(RunControl *runControl);
~IosRunner() override;
void setCppDebugging(bool cppDebug);
void setQmlDebugging(QmlDebugServicesPreset qmlDebugServices);
void start() override;
void stop() final;
Port gdbServerPort() const;
qint64 pid() const;
bool isAppRunning() const;
private:
Utils::FilePath bundlePath() const;
QString deviceId() const;
IosToolHandler::RunKind runType() const;
bool cppDebug() const;
bool qmlDebug() const;
void handleGotServerPorts(Ios::IosToolHandler *handler, const FilePath &bundlePath,
const QString &deviceId, Port gdbPort, Port qmlPort);
void handleGotInferiorPid(Ios::IosToolHandler *handler, const FilePath &bundlePath,
const QString &deviceId, qint64 pid);
void handleAppOutput(Ios::IosToolHandler *handler, const QString &output);
void handleMessage(const QString &msg);
void handleErrorMsg(Ios::IosToolHandler *handler, const QString &msg);
void handleToolExited(Ios::IosToolHandler *handler, int code);
void handleFinished(Ios::IosToolHandler *handler);
IosToolHandler *m_toolHandler = nullptr;
FilePath m_bundleDir;
IDeviceConstPtr m_device;
IosDeviceType m_deviceType;
bool m_cppDebug = false;
QmlDebugServicesPreset m_qmlDebugServices = NoQmlDebugServices;
bool m_cleanExit = false;
Port m_qmlServerPort;
Port m_gdbServerPort;
qint64 m_pid = 0;
};
IosRunner::IosRunner(RunControl *runControl)
: RunWorker(runControl)
{
setId("IosRunner");
stopRunningRunControl(runControl);
const IosDeviceTypeAspect::Data *data = runControl->aspectData<IosDeviceTypeAspect>();
QTC_ASSERT(data, return);
m_bundleDir = data->bundleDirectory;
m_device = RunDeviceKitAspect::device(runControl->kit());
m_deviceType = data->deviceType;
}
IosRunner::~IosRunner()
{
stop();
}
void IosRunner::setCppDebugging(bool cppDebug)
{
m_cppDebug = cppDebug;
}
void IosRunner::setQmlDebugging(QmlDebugServicesPreset qmlDebugServices)
{
m_qmlDebugServices = qmlDebugServices;
}
FilePath IosRunner::bundlePath() const
{
return m_bundleDir;
}
QString IosRunner::deviceId() const
{
IosDevice::ConstPtr dev = std::dynamic_pointer_cast<const IosDevice>(m_device);
if (!dev)
return {};
return dev->uniqueDeviceID();
}
IosToolHandler::RunKind IosRunner::runType() const
{
if (m_cppDebug)
return IosToolHandler::DebugRun;
return IosToolHandler::NormalRun;
}
bool IosRunner::cppDebug() const
{
return m_cppDebug;
}
bool IosRunner::qmlDebug() const
{
return m_qmlDebugServices != NoQmlDebugServices;
}
void IosRunner::start()
{
if (m_toolHandler && isAppRunning())
m_toolHandler->stop();
m_cleanExit = false;
m_qmlServerPort = Port();
if (!m_bundleDir.exists()) {
TaskHub::addTask(DeploymentTask(Task::Warning,
Tr::tr("Could not find %1.").arg(m_bundleDir.toUserOutput())));
reportFailure();
return;
}
if (m_qmlDebugServices != NoQmlDebugServices)
m_qmlServerPort = Port(runControl()->qmlChannel().port());
m_toolHandler = new IosToolHandler(m_deviceType, this);
connect(m_toolHandler, &IosToolHandler::appOutput,
this, &IosRunner::handleAppOutput);
connect(m_toolHandler, &IosToolHandler::message, this, &IosRunner::handleMessage);
connect(m_toolHandler, &IosToolHandler::errorMsg, this, &IosRunner::handleErrorMsg);
connect(m_toolHandler, &IosToolHandler::gotServerPorts,
this, &IosRunner::handleGotServerPorts);
connect(m_toolHandler, &IosToolHandler::gotInferiorPid,
this, &IosRunner::handleGotInferiorPid);
connect(m_toolHandler, &IosToolHandler::toolExited,
this, &IosRunner::handleToolExited);
connect(m_toolHandler, &IosToolHandler::finished,
this, &IosRunner::handleFinished);
const CommandLine command = runControl()->commandLine();
QStringList args = ProcessArgs::splitArgs(command.arguments(), OsTypeMac);
if (m_qmlServerPort.isValid()) {
QUrl qmlServer;
qmlServer.setPort(m_qmlServerPort.number());
args.append(qmlDebugTcpArguments(m_qmlDebugServices, qmlServer));
}
appendMessage(Tr::tr("Starting remote process."), NormalMessageFormat);
m_toolHandler->requestRunApp(bundlePath(), args, runType(), deviceId());
}
void IosRunner::stop()
{
if (isAppRunning())
m_toolHandler->stop();
}
void IosRunner::handleGotServerPorts(IosToolHandler *handler, const FilePath &bundlePath,
const QString &deviceId, Port gdbPort,
Port qmlPort)
{
// Called when debugging on Device.
Q_UNUSED(bundlePath)
Q_UNUSED(deviceId)
if (m_toolHandler != handler)
return;
m_gdbServerPort = gdbPort;
m_qmlServerPort = qmlPort;
// The run control so far knows about the port on the device side,
// but the QML Profiler has to actually connect to a corresponding
// local port. That port is reported here, so we need to adapt the runControl's
// "qmlChannel", so the QmlProfilerRunner uses the right port.
QUrl qmlChannel = runControl()->qmlChannel();
qmlChannel.setPort(qmlPort.number());
runControl()->setQmlChannel(qmlChannel);
bool prerequisiteOk = false;
if (cppDebug() && qmlDebug())
prerequisiteOk = m_gdbServerPort.isValid() && m_qmlServerPort.isValid();
else if (cppDebug())
prerequisiteOk = m_gdbServerPort.isValid();
else if (qmlDebug())
prerequisiteOk = m_qmlServerPort.isValid();
else
prerequisiteOk = true; // Not debugging. Ports not required.
if (prerequisiteOk)
reportStarted();
else
reportFailure(Tr::tr("Could not get necessary ports for the debugger connection."));
}
void IosRunner::handleGotInferiorPid(IosToolHandler *handler, const FilePath &bundlePath,
const QString &deviceId, qint64 pid)
{
// Called when debugging on Simulator.
Q_UNUSED(bundlePath)
Q_UNUSED(deviceId)
if (m_toolHandler != handler)
return;
m_pid = pid;
bool prerequisiteOk = false;
if (m_pid > 0) {
prerequisiteOk = true;
} else {
reportFailure(Tr::tr("Could not get inferior PID."));
return;
}
if (qmlDebug())
prerequisiteOk = m_qmlServerPort.isValid();
if (prerequisiteOk)
reportStarted();
else
reportFailure(Tr::tr("Could not get necessary ports for the debugger connection."));
}
void IosRunner::handleAppOutput(IosToolHandler *handler, const QString &output)
{
Q_UNUSED(handler)
static const QRegularExpression qmlPortRe(QString::fromLatin1(QML_DEBUGGER_WAITING));
const QRegularExpressionMatch match = qmlPortRe.match(output);
QString res(output);
if (match.hasMatch() && m_qmlServerPort.isValid())
res.replace(match.captured(1), QString::number(m_qmlServerPort.number()));
appendMessage(output, StdOutFormat);
}
void IosRunner::handleMessage(const QString &msg)
{
QString res(msg);
static const QRegularExpression qmlPortRe(QString::fromLatin1(QML_DEBUGGER_WAITING));
const QRegularExpressionMatch match = qmlPortRe.match(msg);
if (match.hasMatch() && m_qmlServerPort.isValid())
res.replace(match.captured(1), QString::number(m_qmlServerPort.number()));
appendMessage(res, StdOutFormat);
}
void IosRunner::handleErrorMsg(IosToolHandler *handler, const QString &msg)
{
Q_UNUSED(handler)
QString res(msg);
QString lockedErr ="Unexpected reply: ELocked (454c6f636b6564) vs OK (4f4b)";
if (msg.contains("AMDeviceStartService returned -402653150")) {
TaskHub::addTask(DeploymentTask(Task::Warning, Tr::tr("Run failed. "
"The settings in the Organizer window of Xcode might be incorrect.")));
} else if (res.contains(lockedErr)) {
QString message = Tr::tr("The device is locked, please unlock.");
TaskHub::addTask(DeploymentTask(Task::Error, message));
res.replace(lockedErr, message);
}
appendMessage(res, StdErrFormat);
}
void IosRunner::handleToolExited(IosToolHandler *handler, int code)
{
Q_UNUSED(handler)
m_cleanExit = (code == 0);
}
void IosRunner::handleFinished(IosToolHandler *handler)
{
if (m_toolHandler == handler) {
if (m_cleanExit)
appendMessage(Tr::tr("Run ended."), NormalMessageFormat);
else
appendMessage(Tr::tr("Run ended with error."), ErrorMessageFormat);
m_toolHandler = nullptr;
}
handler->deleteLater();
reportStopped();
}
qint64 IosRunner::pid() const
{
return m_pid;
}
bool IosRunner::isAppRunning() const
{
return m_toolHandler && m_toolHandler->isRunning();
}
Port IosRunner::gdbServerPort() const
{
return m_gdbServerPort;
}
//
// IosQmlProfilerSupport
//
class IosQmlProfilerSupport : public RunWorker
{
public:
IosQmlProfilerSupport(RunControl *runControl);
private:
void start() override;
IosRunner *m_runner = nullptr;
RunWorker *m_profiler = nullptr;
};
IosQmlProfilerSupport::IosQmlProfilerSupport(RunControl *runControl)
: RunWorker(runControl)
{
setId("IosQmlProfilerSupport");
m_runner = new IosRunner(runControl);
m_runner->setQmlDebugging(QmlProfilerServices);
addStartDependency(m_runner);
m_profiler = runControl->createWorker(ProjectExplorer::Constants::QML_PROFILER_RUNNER);
m_profiler->addStartDependency(this);
}
void IosQmlProfilerSupport::start()
{
const Port qmlPort = Port(runControl()->qmlChannel().port());
if (qmlPort.isValid())
reportStarted();
else
reportFailure(Tr::tr("Could not get necessary ports for the profiler connection."));
}
//
// IosDebugSupport
//
class IosDebugSupport : public DebuggerRunTool
{
public:
IosDebugSupport(RunControl *runControl);
private:
void start() override;
IosRunner *m_iosRunner = nullptr;
DeviceCtlRunner *m_deviceCtlRunner = nullptr;
};
static expected_str<FilePath> findDeviceSdk(IosDevice::ConstPtr dev)
{
const QString osVersion = dev->osVersion();
const QString productType = dev->productType();
const QString cpuArchitecture = dev->cpuArchitecture();
const FilePath home = FilePath::fromString(QDir::homePath());
const FilePaths symbolsPathCandidates
= {home / "Library/Developer/Xcode/iOS DeviceSupport" / (productType + " " + osVersion)
/ "Symbols",
home / "Library/Developer/Xcode/iOS DeviceSupport" / (osVersion + " " + cpuArchitecture)
/ "Symbols",
home / "Library/Developer/Xcode/iOS DeviceSupport" / osVersion / "Symbols",
IosConfigurations::developerPath() / "Platforms/iPhoneOS.platform/DeviceSupport"
/ osVersion / "Symbols"};
const FilePath deviceSdk = Utils::findOrDefault(symbolsPathCandidates, &FilePath::isDir);
if (deviceSdk.isEmpty()) {
return Utils::make_unexpected(
Tr::tr("Could not find device specific debug symbols at %1. "
"Debugging initialization will be slow until you open the Organizer window of "
"Xcode with the device connected to have the symbols generated.")
.arg(symbolsPathCandidates.constFirst().toUserOutput()));
}
return deviceSdk;
}
IosDebugSupport::IosDebugSupport(RunControl *runControl)
: DebuggerRunTool(runControl)
{
setId("IosDebugSupport");
IosDevice::ConstPtr dev = std::dynamic_pointer_cast<const IosDevice>(runControl->device());
const bool isIosDeviceType = runControl->device()->type() == Ios::Constants::IOS_DEVICE_TYPE;
const bool isIosDeviceInstance = bool(dev);
// type info and device class must match
QTC_ASSERT(isIosDeviceInstance == isIosDeviceType, return);
DebuggerRunParameters &rp = runParameters();
// TODO cannot use setupPortsGatherer() from DebuggerRunTool, because that also requests
// the "debugChannel", which then results in runControl trying to retrieve ports&URL for that
// via IDevice, which doesn't really work with the iOS setup, and also completely changes
// how the DebuggerRunTool works, breaking debugging on iOS <= 16 devices.
if (rp.isQmlDebugging())
runControl->requestQmlChannel();
if (!isIosDeviceInstance /*== simulator */ || dev->handler() == IosDevice::Handler::IosTool) {
m_iosRunner = new IosRunner(runControl);
m_iosRunner->setCppDebugging(rp.isCppDebugging());
m_iosRunner->setQmlDebugging(rp.isQmlDebugging() ? QmlDebuggerServices : NoQmlDebugServices);
addStartDependency(m_iosRunner);
} else {
QTC_CHECK(rp.isCppDebugging());
m_deviceCtlRunner = new DeviceCtlRunner(runControl);
m_deviceCtlRunner->setStartStopped(true);
addStartDependency(m_deviceCtlRunner);
}
if (isIosDeviceInstance) {
if (dev->handler() == IosDevice::Handler::DeviceCtl) {
QTC_CHECK(IosDeviceManager::isDeviceCtlDebugSupported());
rp.setStartMode(AttachToIosDevice);
rp.setDeviceUuid(dev->uniqueInternalDeviceId());
} else {
rp.setStartMode(AttachToRemoteProcess);
}
rp.setLldbPlatform("remote-ios");
const expected_str<FilePath> deviceSdk = findDeviceSdk(dev);
if (!deviceSdk)
TaskHub::addTask(DeploymentTask(Task::Warning, deviceSdk.error()));
else
rp.setDeviceSymbolsRoot(deviceSdk->path());
} else {
rp.setStartMode(AttachToLocalProcess);
rp.setLldbPlatform("ios-simulator");
}
}
void IosDebugSupport::start()
{
DebuggerRunParameters &rp = runParameters();
const IosDeviceTypeAspect::Data *data = runControl()->aspectData<IosDeviceTypeAspect>();
QTC_ASSERT(data, reportFailure("Broken IosDeviceTypeAspect setup."); return);
rp.setDisplayName(data->applicationName);
rp.setContinueAfterAttach(true);
IosDevice::ConstPtr dev = std::dynamic_pointer_cast<const IosDevice>(runControl()->device());
const bool isIosDeviceType = runControl()->device()->type() == Ios::Constants::IOS_DEVICE_TYPE;
const bool isIosDeviceInstance = bool(dev);
// type info and device class must match
QTC_ASSERT(isIosDeviceInstance == isIosDeviceType, reportFailure(Tr::tr("Internal error."));
return);
if (isIosDeviceInstance && dev->handler() == IosDevice::Handler::DeviceCtl) {
const auto msgOnlyCppDebuggingSupported = [] {
return Tr::tr("Only C++ debugging is supported for devices with iOS 17 and later.");
};
if (!rp.isCppDebugging()) {
reportFailure(msgOnlyCppDebuggingSupported());
return;
}
if (rp.isQmlDebugging()) {
rp.setQmlDebugging(false);
appendMessage(msgOnlyCppDebuggingSupported(), OutputFormat::LogMessageFormat, true);
}
rp.setAttachPid(m_deviceCtlRunner->processIdentifier());
rp.setInferiorExecutable(data->localExecutable);
DebuggerRunTool::start();
return;
}
if (!m_iosRunner->isAppRunning()) {
reportFailure(Tr::tr("Application not running."));
return;
}
const Port gdbServerPort = m_iosRunner->gdbServerPort();
const Port qmlServerPort = Port(runControl()->qmlChannel().port());
rp.setAttachPid(m_iosRunner->pid());
const bool cppDebug = rp.isCppDebugging();
const bool qmlDebug = rp.isQmlDebugging();
if (cppDebug) {
rp.setInferiorExecutable(data->localExecutable);
rp.setRemoteChannel("connect://localhost:" + gdbServerPort.toString());
QString bundlePath = data->bundleDirectory.toUrlishString();
bundlePath.chop(4);
FilePath dsymPath = FilePath::fromString(bundlePath.append(".dSYM"));
if (dsymPath.exists()
&& dsymPath.lastModified() < data->localExecutable.lastModified()) {
TaskHub::addTask(DeploymentTask(Task::Warning,
Tr::tr("The dSYM %1 seems to be outdated, it might confuse the debugger.")
.arg(dsymPath.toUserOutput())));
}
}
QUrl qmlServer;
if (qmlDebug) {
QTcpServer server;
const bool isListening = server.listen(QHostAddress::LocalHost)
|| server.listen(QHostAddress::LocalHostIPv6);
QTC_ASSERT(isListening, return);
qmlServer.setHost(server.serverAddress().toString());
if (!cppDebug)
runParameters().setStartMode(AttachToRemoteServer);
}
if (qmlServerPort.isValid())
qmlServer.setPort(qmlServerPort.number());
rp.setQmlServer(qmlServer);
DebuggerRunTool::start();
}
// Factories
IosRunWorkerFactory::IosRunWorkerFactory()
{
setProducer([](RunControl *control) -> RunWorker * {
IosDevice::ConstPtr iosdevice = std::dynamic_pointer_cast<const IosDevice>(control->device());
if (iosdevice && iosdevice->handler() == IosDevice::Handler::DeviceCtl) {
if (IosDeviceManager::isDeviceCtlOutputSupported())
return new DeviceCtlRunner(control);
// TODO Remove the polling runner when we decide not to support iOS 17+ devices
// with Xcode < 15.4 at all
return new DeviceCtlPollingRunner(control);
}
control->setIcon(Icons::RUN_SMALL_TOOLBAR);
control->setDisplayName(QString("Run on %1")
.arg(iosdevice ? iosdevice->displayName() : QString()));
return new IosRunner(control);
});
addSupportedRunMode(ProjectExplorer::Constants::NORMAL_RUN_MODE);
addSupportedRunConfig(Constants::IOS_RUNCONFIG_ID);
}
IosDebugWorkerFactory::IosDebugWorkerFactory()
{
setProduct<IosDebugSupport>();
addSupportedRunMode(ProjectExplorer::Constants::DEBUG_RUN_MODE);
addSupportedRunConfig(Constants::IOS_RUNCONFIG_ID);
}
IosQmlProfilerWorkerFactory::IosQmlProfilerWorkerFactory()
{
setProduct<IosQmlProfilerSupport>();
addSupportedRunMode(ProjectExplorer::Constants::QML_PROFILER_RUN_MODE);
addSupportedRunConfig(Constants::IOS_RUNCONFIG_ID);
}
} // Ios::Internal
|