Ios: Convert to RunWorkers

This moves all of the RunControl implementation into a single
RunWorker, not yet splitting it up into separate RunWorkers
which is the final goal of this series.

Done-with: Vikas Pachdha
Change-Id: I9deaef48735c1c63c41dfae39d67f59387295273
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2017-05-19 16:59:02 +02:00
parent a3735eada4
commit 4c29a31930
12 changed files with 413 additions and 708 deletions

View File

@@ -32,29 +32,51 @@
#include "iossimulator.h"
#include "iosconstants.h"
#include <debugger/analyzer/analyzerstartparameters.h>
#include <debugger/debuggerplugin.h>
#include <debugger/debuggerkitinformation.h>
#include <debugger/debuggerruncontrol.h>
#include <projectexplorer/kitinformation.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
#include <projectexplorer/taskhub.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <debugger/debuggerrunconfigurationaspect.h>
#include <projectexplorer/toolchain.h>
#include <qmldebug/qmldebugcommandlinearguments.h>
#include <qtsupport/qtkitinformation.h>
#include <utils/fileutils.h>
#include <utils/qtcprocess.h>
#include <utils/utilsicons.h>
#include <QDateTime>
#include <QDir>
#include <QTime>
#include <QMessageBox>
#include <QRegExp>
#include <QSettings>
#include <QTcpServer>
#include <QTime>
#include <stdio.h>
#include <fcntl.h>
#ifdef Q_OS_UNIX
#include <unistd.h>
#else
#include <io.h>
#endif
#include <signal.h>
using namespace Debugger;
using namespace ProjectExplorer;
using namespace Utils;
namespace Ios {
namespace Internal {
IosRunner::IosRunner(QObject *parent, RunControl *runControl, bool cppDebug,
QmlDebug::QmlDebugServicesPreset qmlDebugServices)
: QObject(parent),
m_cppDebug(cppDebug), m_qmlDebugServices(qmlDebugServices)
IosRunner::IosRunner(RunControl *runControl)
: RunWorker(runControl)
{
auto runConfig = qobject_cast<IosRunConfiguration *>(runControl->runConfiguration());
m_bundleDir = runConfig->bundleDirectory().toString();
@@ -68,6 +90,16 @@ IosRunner::~IosRunner()
stop();
}
void IosRunner::setCppDebugging(bool cppDebug)
{
m_cppDebug = cppDebug;
}
void IosRunner::setQmlDebugging(QmlDebug::QmlDebugServicesPreset qmlDebugServices)
{
m_qmlDebugServices = qmlDebugServices;
}
QString IosRunner::bundlePath()
{
return m_bundleDir;
@@ -76,8 +108,8 @@ QString IosRunner::bundlePath()
QStringList IosRunner::extraArgs()
{
QStringList res = m_arguments;
if (m_qmlPort.isValid())
res << QmlDebug::qmlDebugTcpArguments(m_qmlDebugServices, m_qmlPort);
if (m_qmlServerPort.isValid())
res << QmlDebug::qmlDebugTcpArguments(m_qmlDebugServices, m_qmlServerPort);
return res;
}
@@ -108,35 +140,34 @@ QmlDebug::QmlDebugServicesPreset IosRunner::qmlDebugServices() const
void IosRunner::start()
{
if (m_toolHandler) {
if (m_toolHandler && isAppRunning())
m_toolHandler->stop();
emit finished(m_cleanExit);
}
m_cleanExit = false;
m_qmlPort = Utils::Port();
m_qmlServerPort = Port();
if (!QFileInfo::exists(m_bundleDir)) {
TaskHub::addTask(Task::Warning,
tr("Could not find %1.").arg(m_bundleDir),
ProjectExplorer::Constants::TASK_CATEGORY_DEPLOYMENT);
emit finished(m_cleanExit);
reportFailure();
return;
}
if (m_device->type() == Ios::Constants::IOS_DEVICE_TYPE) {
IosDevice::ConstPtr iosDevice = m_device.dynamicCast<const IosDevice>();
if (m_device.isNull()) {
emit finished(m_cleanExit);
reportFailure();
return;
}
if (m_qmlDebugServices != QmlDebug::NoQmlDebugServices)
m_qmlPort = iosDevice->nextPort();
m_qmlServerPort = iosDevice->nextPort();
} else {
IosSimulator::ConstPtr sim = m_device.dynamicCast<const IosSimulator>();
if (sim.isNull()) {
emit finished(m_cleanExit);
reportFailure();
return;
}
if (m_qmlDebugServices != QmlDebug::NoQmlDebugServices)
m_qmlPort = sim->nextPort();
m_qmlServerPort = sim->nextPort();
}
m_toolHandler = new IosToolHandler(m_deviceType, this);
@@ -159,7 +190,7 @@ void IosRunner::start()
void IosRunner::stop()
{
if (m_toolHandler && m_toolHandler->isRunning())
if (isAppRunning())
m_toolHandler->stop();
}
@@ -167,46 +198,73 @@ void IosRunner::handleDidStartApp(IosToolHandler *handler, const QString &bundle
const QString &deviceId, IosToolHandler::OpStatus status)
{
Q_UNUSED(bundlePath); Q_UNUSED(deviceId);
if (m_toolHandler == handler)
emit didStartApp(status);
if (m_toolHandler == handler && runType() == IosToolHandler::NormalRun) {
// For normal run type the notify reportStarted here for debug type wait for
// server ports or PID for device and simulator respectivelly.
if (status == IosToolHandler::Success)
reportStarted();
else
reportFailure();
}
}
void IosRunner::handleGotServerPorts(IosToolHandler *handler, const QString &bundlePath,
const QString &deviceId, Utils::Port gdbPort,
Utils::Port qmlPort)
const QString &deviceId, Port gdbPort,
Port qmlPort)
{
// Called when debugging on Device.
Q_UNUSED(bundlePath); Q_UNUSED(deviceId);
m_qmlPort = qmlPort;
if (m_toolHandler == handler)
emit gotServerPorts(gdbPort, qmlPort);
if (m_toolHandler == handler && runType() == IosToolHandler::DebugRun) {
m_gdbServerPort = gdbPort;
m_qmlServerPort = qmlPort;
bool portsValid = m_gdbServerPort.isValid() && m_qmlServerPort.isValid();
bool qmlDebugging = m_qmlDebugServices != QmlDebug::NoQmlDebugServices;
if ( (qmlDebugging && m_cppDebug && portsValid) // Mixed mode debuggin & valid ports
|| (qmlDebugging && !m_cppDebug && m_qmlServerPort.isValid()) // Qml debugging only
|| (m_cppDebug && !qmlDebugging && m_gdbServerPort.isValid())) { // C++ debugging only
reportStarted();
} else {
reportFailure(tr("Could not get debug server file descriptor."));
}
}
}
void IosRunner::handleGotInferiorPid(IosToolHandler *handler, const QString &bundlePath,
const QString &deviceId, qint64 pid)
{
// Called when debugging on Simulator.
Q_UNUSED(bundlePath); Q_UNUSED(deviceId);
m_pid = pid;
if (m_toolHandler == handler)
emit gotInferiorPid(pid, m_qmlPort);
if (m_pid <= 0)
reportFailure(tr("Could not get inferior PID."));
if (m_toolHandler == handler && runType() == IosToolHandler::DebugRun) {
bool qmlDebugging = m_qmlDebugServices != QmlDebug::NoQmlDebugServices;
if ((qmlDebugging && m_qmlServerPort.isValid()) || (m_cppDebug && !qmlDebugging))
reportStarted();
else
reportFailure(tr("Could not get debug server file descriptor."));
}
}
void IosRunner::handleAppOutput(IosToolHandler *handler, const QString &output)
{
Q_UNUSED(handler);
QRegExp qmlPortRe(QLatin1String("QML Debugger: Waiting for connection on port ([0-9]+)..."));
QRegExp qmlPortRe("QML Debugger: Waiting for connection on port ([0-9]+)...");
int index = qmlPortRe.indexIn(output);
QString res(output);
if (index != -1 && m_qmlPort.isValid())
res.replace(qmlPortRe.cap(1), QString::number(m_qmlPort.number()));
emit appOutput(res);
if (index != -1 && m_qmlServerPort.isValid())
res.replace(qmlPortRe.cap(1), QString::number(m_qmlServerPort.number()));
appendMessage(output, StdOutFormat);
appOutput(res);
}
void IosRunner::handleErrorMsg(IosToolHandler *handler, const QString &msg)
{
Q_UNUSED(handler);
QString res(msg);
QLatin1String lockedErr = QLatin1String("Unexpected reply: ELocked (454c6f636b6564) vs OK (4f4b)");
if (msg.contains(QLatin1String("AMDeviceStartService returned -402653150"))) {
QString lockedErr ="Unexpected reply: ELocked (454c6f636b6564) vs OK (4f4b)";
if (msg.contains("AMDeviceStartService returned -402653150")) {
TaskHub::addTask(Task::Warning,
tr("Run failed. The settings in the Organizer window of Xcode might be incorrect."),
ProjectExplorer::Constants::TASK_CATEGORY_DEPLOYMENT);
@@ -216,11 +274,13 @@ void IosRunner::handleErrorMsg(IosToolHandler *handler, const QString &msg)
ProjectExplorer::Constants::TASK_CATEGORY_DEPLOYMENT);
res.replace(lockedErr, message);
}
QRegExp qmlPortRe(QLatin1String("QML Debugger: Waiting for connection on port ([0-9]+)..."));
QRegExp qmlPortRe("QML Debugger: Waiting for connection on port ([0-9]+)...");
int index = qmlPortRe.indexIn(msg);
if (index != -1 && m_qmlPort.isValid())
res.replace(qmlPortRe.cap(1), QString::number(m_qmlPort.number()));
emit errorMsg(res);
if (index != -1 && m_qmlServerPort.isValid())
res.replace(qmlPortRe.cap(1), QString::number(m_qmlServerPort.number()));
appendMessage(res, StdErrFormat);
errorMsg(res);
}
void IosRunner::handleToolExited(IosToolHandler *handler, int code)
@@ -232,16 +292,243 @@ void IosRunner::handleToolExited(IosToolHandler *handler, int code)
void IosRunner::handleFinished(IosToolHandler *handler)
{
if (m_toolHandler == handler) {
emit finished(m_cleanExit);
m_toolHandler = 0;
if (m_cleanExit)
appendMessage(tr("Run ended."), NormalMessageFormat);
else
appendMessage(tr("Run ended with error."), ErrorMessageFormat);
m_toolHandler = nullptr;
}
handler->deleteLater();
reportStopped();
}
QString IosRunner::displayName() const
qint64 IosRunner::pid() const
{
return QString::fromLatin1("Run on %1").arg(m_device.isNull() ? QString()
: m_device->displayName());
return m_pid;
}
bool IosRunner::isAppRunning() const
{
return m_toolHandler && m_toolHandler->isRunning();
}
Utils::Port IosRunner::gdbServerPort() const
{
return m_gdbServerPort;
}
Utils::Port IosRunner::qmlServerPort() const
{
return m_qmlServerPort;
}
//
// IosRunner
//
IosRunSupport::IosRunSupport(RunControl *runControl)
: IosRunner(runControl)
{
runControl->setIcon(Icons::RUN_SMALL_TOOLBAR);
QString displayName = QString("Run on %1").arg(device().isNull() ? QString() : device()->displayName());
runControl->setDisplayName(displayName);
}
IosRunSupport::~IosRunSupport()
{
stop();
}
void IosRunSupport::start()
{
appendMessage(tr("Starting remote process."), NormalMessageFormat);
IosRunner::start();
}
void IosRunSupport::stop()
{
IosRunner::stop();
}
//
// IosAnalyzeSupport
//
IosAnalyzeSupport::IosAnalyzeSupport(RunControl *runControl)
: IosRunner(runControl)
{
m_runner = new IosRunner(runControl);
addDependency(m_runner);
setDisplayName("IosAnalyzeSupport");
setQmlDebugging(QmlDebug::QmlProfilerServices);
auto iosRunConfig = qobject_cast<IosRunConfiguration *>(runControl->runConfiguration());
StandardRunnable runnable;
runnable.executable = iosRunConfig->localExecutable().toUserOutput();
runnable.commandLineArguments = iosRunConfig->commandLineArguments();
Debugger::AnalyzerConnection connection;
connection.analyzerHost = "localhost";
runControl->setRunnable(runnable);
runControl->setConnection(connection);
runControl->setDisplayName(iosRunConfig->applicationName());
connect(&m_outputParser, &QmlDebug::QmlOutputParser::waitingForConnectionOnPort,
this, &IosAnalyzeSupport::qmlServerReady);
}
void IosAnalyzeSupport::start()
{
// Use m_runner->qmlServerPort() // FIXME
}
void IosAnalyzeSupport::qmlServerReady()
{
// runControl()->notifyRemoteSetupDone(m_qmlServerPort);
}
void IosAnalyzeSupport::appOutput(const QString &output)
{
m_outputParser.processOutput(output);
}
void IosAnalyzeSupport::errorMsg(const QString &output)
{
m_outputParser.processOutput(output);
}
//
// IosDebugSupport
//
IosDebugSupport::IosDebugSupport(RunControl *runControl)
: Debugger::DebuggerRunTool(runControl)
{
m_runner = new IosRunner(runControl);
m_runner->setCppDebugging(isCppDebugging());
m_runner->setQmlDebugging(isQmlDebugging() ? QmlDebug::QmlDebuggerServices : QmlDebug::NoQmlDebugServices);
addDependency(m_runner);
}
void IosDebugSupport::start()
{
RunConfiguration *runConfig = runControl()->runConfiguration();
DebuggerStartParameters params;
if (device()->type() == Ios::Constants::IOS_DEVICE_TYPE) {
IosDevice::ConstPtr dev = device().dynamicCast<const IosDevice>();
params.startMode = AttachToRemoteProcess;
params.platform = "remote-ios";
QString osVersion = dev->osVersion();
FileName deviceSdk1 = FileName::fromString(QDir::homePath()
+ "/Library/Developer/Xcode/iOS DeviceSupport/"
+ osVersion + "/Symbols");
QString deviceSdk;
if (deviceSdk1.toFileInfo().isDir()) {
deviceSdk = deviceSdk1.toString();
} else {
FileName deviceSdk2 = IosConfigurations::developerPath()
.appendPath("Platforms/iPhoneOS.platform/DeviceSupport/")
.appendPath(osVersion).appendPath("Symbols");
if (deviceSdk2.toFileInfo().isDir()) {
deviceSdk = deviceSdk2.toString();
} else {
TaskHub::addTask(Task::Warning, 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(deviceSdk1.toUserOutput()),
ProjectExplorer::Constants::TASK_CATEGORY_DEPLOYMENT);
}
}
params.deviceSymbolsRoot = deviceSdk;
} else {
params.startMode = AttachExternal;
params.platform = "ios-simulator";
}
auto iosRunConfig = qobject_cast<IosRunConfiguration *>(runConfig);
params.displayName = iosRunConfig->applicationName();
params.remoteSetupNeeded = true;
params.continueAfterAttach = true;
Utils::Port gdbServerPort = m_runner->gdbServerPort();
Utils::Port qmlServerPort = m_runner->qmlServerPort();
params.attachPID = ProcessHandle(m_runner->pid());
const bool cppDebug = isCppDebugging();
const bool qmlDebug = isQmlDebugging();
if (cppDebug) {
params.inferior.executable = iosRunConfig->localExecutable().toString();
if (gdbServerPort.isValid())
params.remoteChannel = "connect://localhost:" + gdbServerPort.toString();
else
params.remoteChannel = "connect://localhost:0";
FileName xcodeInfo = IosConfigurations::developerPath().parentDir().appendPath("Info.plist");
bool buggyLldb = false;
if (xcodeInfo.exists()) {
QSettings settings(xcodeInfo.toString(), QSettings::NativeFormat);
QStringList version = settings.value(QLatin1String("CFBundleShortVersionString")).toString()
.split('.');
if (version.value(0).toInt() == 5 && version.value(1, QString::number(1)).toInt() == 0)
buggyLldb = true;
}
QString bundlePath = iosRunConfig->bundleDirectory().toString();
bundlePath.chop(4);
FileName dsymPath = FileName::fromString(bundlePath.append(".dSYM"));
if (!dsymPath.exists()) {
if (buggyLldb)
TaskHub::addTask(Task::Warning,
tr("Debugging with Xcode 5.0.x can be unreliable without a dSYM. "
"To create one, add a dsymutil deploystep."),
ProjectExplorer::Constants::TASK_CATEGORY_DEPLOYMENT);
} else if (dsymPath.toFileInfo().lastModified()
< QFileInfo(iosRunConfig->localExecutable().toUserOutput()).lastModified()) {
TaskHub::addTask(Task::Warning,
tr("The dSYM %1 seems to be outdated, it might confuse the debugger.")
.arg(dsymPath.toUserOutput()),
ProjectExplorer::Constants::TASK_CATEGORY_DEPLOYMENT);
}
}
if (qmlDebug) {
QTcpServer server;
QTC_ASSERT(server.listen(QHostAddress::LocalHost)
|| server.listen(QHostAddress::LocalHostIPv6), return);
params.qmlServer.host = server.serverAddress().toString();
if (!cppDebug)
params.startMode = AttachToRemoteServer;
}
if (qmlServerPort.isValid()) {
params.qmlServer.port = qmlServerPort;
params.inferior.commandLineArguments.replace("%qml_port%", qmlServerPort.toString());
}
setStartParameters(params);
if (!m_runner->isAppRunning()) {
reportFailure(tr("Application not running."));
return;
}
RemoteSetupResult result;
if (m_runner->pid() > 0)
result.inferiorPid = m_runner->pid();
else
result.gdbServerPort = gdbServerPort;
result.qmlServerPort = qmlServerPort;
result.success = true; // Port validation already checked.
notifyEngineRemoteSetupFinished(result);
DebuggerRunTool::start();
}
void IosDebugSupport::onFinished()
{
abortDebugger();
}
} // namespace Internal