androiddebugsupport.cpp 8.29 KB
Newer Older
BogDan Vatra's avatar
BogDan Vatra committed
1
2
/**************************************************************************
**
3
** Copyright (c) 2014 BogDan Vatra <bog_dan_ro@yahoo.com>
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
BogDan Vatra's avatar
BogDan Vatra committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
BogDan Vatra's avatar
BogDan Vatra committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
BogDan Vatra's avatar
BogDan Vatra committed
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
BogDan Vatra's avatar
BogDan Vatra committed
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
BogDan Vatra's avatar
BogDan Vatra committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
BogDan Vatra's avatar
BogDan Vatra committed
29
30
31
32
33
34

#include "androiddebugsupport.h"

#include "androiddeploystep.h"
#include "androidglobal.h"
#include "androidrunner.h"
Tobias Hunger's avatar
Tobias Hunger committed
35
#include "androidmanager.h"
BogDan Vatra's avatar
BogDan Vatra committed
36

hjk's avatar
hjk committed
37
#include <debugger/debuggerengine.h>
BogDan Vatra's avatar
BogDan Vatra committed
38
#include <debugger/debuggerplugin.h>
Tobias Hunger's avatar
Tobias Hunger committed
39
#include <debugger/debuggerkitinformation.h>
40
#include <debugger/debuggerrunconfigurationaspect.h>
BogDan Vatra's avatar
BogDan Vatra committed
41
42
43
#include <debugger/debuggerrunner.h>
#include <debugger/debuggerstartparameters.h>

Tobias Hunger's avatar
Tobias Hunger committed
44
#include <projectexplorer/target.h>
45
#include <projectexplorer/toolchain.h>
46
47
48
#include <qmakeprojectmanager/qmakebuildconfiguration.h>
#include <qmakeprojectmanager/qmakenodes.h>
#include <qmakeprojectmanager/qmakeproject.h>
Tobias Hunger's avatar
Tobias Hunger committed
49
#include <qtsupport/qtkitinformation.h>
BogDan Vatra's avatar
BogDan Vatra committed
50
51

#include <QDir>
Aurindam Jana's avatar
Aurindam Jana committed
52
#include <QTcpServer>
BogDan Vatra's avatar
BogDan Vatra committed
53
54
55

using namespace Debugger;
using namespace ProjectExplorer;
56
using namespace QmakeProjectManager;
BogDan Vatra's avatar
BogDan Vatra committed
57
58
59
60
61
62
63
64
65
66

namespace Android {
namespace Internal {

static const char * const qMakeVariables[] = {
         "QT_INSTALL_LIBS",
         "QT_INSTALL_PLUGINS",
         "QT_INSTALL_IMPORTS"
};

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
static QStringList qtSoPaths(QtSupport::BaseQtVersion *qtVersion)
{
    if (!qtVersion)
        return QStringList();

    QSet<QString> paths;
    for (uint i = 0; i < sizeof qMakeVariables / sizeof qMakeVariables[0]; ++i) {
        QString path = qtVersion->qmakeProperty(qMakeVariables[i]);
        if (path.isNull())
            continue;
        QDirIterator it(path, QStringList() << QLatin1String("*.so"), QDir::Files, QDirIterator::Subdirectories);
        while (it.hasNext()) {
            it.next();
            paths.insert(it.fileInfo().absolutePath());
        }
    }
    return paths.toList();
}
BogDan Vatra's avatar
BogDan Vatra committed
85

86
RunControl *AndroidDebugSupport::createDebugRunControl(AndroidRunConfiguration *runConfig, QString *errorMessage)
BogDan Vatra's avatar
BogDan Vatra committed
87
{
88
    Target *target = runConfig->target();
89
    QmakeProject *project = static_cast<QmakeProject *>(target->project());
90

BogDan Vatra's avatar
BogDan Vatra committed
91
92
    DebuggerStartParameters params;
    params.startMode = AttachToRemoteServer;
93
    params.displayName = AndroidManager::packageName(target);
94
    params.remoteSetupNeeded = true;
95

96
97
    Debugger::DebuggerRunConfigurationAspect *aspect
            = runConfig->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
98
    if (aspect->useCppDebugger()) {
99
        params.languages |= CppLanguage;
Tobias Hunger's avatar
Tobias Hunger committed
100
101
102
103
        Kit *kit = target->kit();
        params.sysRoot = SysRootKitInformation::sysRoot(kit).toString();
        params.debuggerCommand = DebuggerKitInformation::debuggerCommand(kit).toString();
        if (ToolChain *tc = ToolChainKitInformation::toolChain(kit))
104
            params.toolChainAbi = tc->targetAbi();
105
        params.executable = project->rootQmakeProjectNode()->buildDir() + QLatin1String("/app_process");
106
107
        params.remoteChannel = runConfig->remoteChannel();
        params.solibSearchPath.clear();
108
109
        QList<QmakeProFileNode *> nodes = project->allProFiles();
        foreach (QmakeProFileNode *node, nodes)
110
            params.solibSearchPath.append(node->targetInformation().buildDir);
Tobias Hunger's avatar
Tobias Hunger committed
111
        QtSupport::BaseQtVersion *version = QtSupport::QtKitInformation::qtVersion(kit);
112
113
        params.solibSearchPath.append(qtSoPaths(version));
    }
114
    if (aspect->useQmlDebugger()) {
115
        params.languages |= QmlLanguage;
Aurindam Jana's avatar
Aurindam Jana committed
116
117
118
119
120
        QTcpServer server;
        QTC_ASSERT(server.listen(QHostAddress::LocalHost)
                   || server.listen(QHostAddress::LocalHostIPv6), return 0);
        params.qmlServerAddress = server.serverAddress().toString();
        params.remoteSetupNeeded = true;
121
        //TODO: Not sure if these are the right paths.
122
        params.projectSourceDirectory = project->projectDirectory();
123
124
        params.projectSourceFiles = project->files(QmakeProject::ExcludeGeneratedFiles);
        params.projectBuildDirectory = project->rootQmakeProjectNode()->buildDir();
125
    }
BogDan Vatra's avatar
BogDan Vatra committed
126
127

    DebuggerRunControl * const debuggerRunControl
128
        = DebuggerPlugin::createDebugger(params, runConfig, errorMessage);
BogDan Vatra's avatar
BogDan Vatra committed
129
130
131
132
133
134
    new AndroidDebugSupport(runConfig, debuggerRunControl);
    return debuggerRunControl;
}

AndroidDebugSupport::AndroidDebugSupport(AndroidRunConfiguration *runConfig,
    DebuggerRunControl *runControl)
Aurindam Jana's avatar
Aurindam Jana committed
135
136
    : AndroidRunSupport(runConfig, runControl),
      m_engine(0)
BogDan Vatra's avatar
BogDan Vatra committed
137
{
138
139
    Debugger::DebuggerRunConfigurationAspect *aspect
            = runConfig->extraAspect<Debugger::DebuggerRunConfigurationAspect>();
140
    Q_ASSERT(aspect->useCppDebugger() || aspect->useQmlDebugger());
Orgad Shaneh's avatar
Orgad Shaneh committed
141
    Q_UNUSED(aspect)
142

Aurindam Jana's avatar
Aurindam Jana committed
143
144
145
146
147
148
149
150
151
    if (runControl)
        m_engine = runControl->engine();

    if (m_engine) {
        connect(m_engine, SIGNAL(requestRemoteSetup()),
                m_runner, SLOT(start()));
        connect(m_engine, SIGNAL(aboutToNotifyInferiorSetupOk()),
                m_runner, SLOT(handleRemoteDebuggerRunning()));
    }
152
153
154
155
    connect(m_runner, SIGNAL(remoteServerRunning(QByteArray,int)),
            SLOT(handleRemoteServerRunning(QByteArray,int)));
    connect(m_runner, SIGNAL(remoteProcessStarted(int,int)),
            SLOT(handleRemoteProcessStarted(int,int)));
156
157
158
159
160
161
162
    connect(m_runner, SIGNAL(remoteProcessFinished(QString)),
            SLOT(handleRemoteProcessFinished(QString)));

    connect(m_runner, SIGNAL(remoteErrorOutput(QByteArray)),
            SLOT(handleRemoteErrorOutput(QByteArray)));
    connect(m_runner, SIGNAL(remoteOutput(QByteArray)),
            SLOT(handleRemoteOutput(QByteArray)));
BogDan Vatra's avatar
BogDan Vatra committed
163
164
}

hjk's avatar
hjk committed
165
166
void AndroidDebugSupport::handleRemoteServerRunning(const QByteArray &serverChannel, int pid)
{
Aurindam Jana's avatar
Aurindam Jana committed
167
168
    if (m_engine)
        m_engine->notifyEngineRemoteServerRunning(serverChannel, pid);
hjk's avatar
hjk committed
169
170
}

BogDan Vatra's avatar
BogDan Vatra committed
171
172
173
174
void AndroidDebugSupport::handleRemoteProcessStarted(int gdbServerPort, int qmlPort)
{
    disconnect(m_runner, SIGNAL(remoteProcessStarted(int,int)),
        this, SLOT(handleRemoteProcessStarted(int,int)));
Aurindam Jana's avatar
Aurindam Jana committed
175
176
    if (m_engine)
        m_engine->notifyEngineRemoteSetupDone(gdbServerPort, qmlPort);
BogDan Vatra's avatar
BogDan Vatra committed
177
178
179
180
}

void AndroidDebugSupport::handleRemoteProcessFinished(const QString &errorMsg)
{
Aurindam Jana's avatar
Aurindam Jana committed
181
182
183
184
185
    DebuggerRunControl *runControl = qobject_cast<DebuggerRunControl *>(m_runControl);
    if (runControl)
        runControl->showMessage(errorMsg, AppStuff);
    else
        AndroidRunSupport::handleRemoteProcessFinished(errorMsg);
BogDan Vatra's avatar
BogDan Vatra committed
186
187
188
189
}

void AndroidDebugSupport::handleRemoteOutput(const QByteArray &output)
{
Aurindam Jana's avatar
Aurindam Jana committed
190
191
192
193
194
195
    if (m_engine) {
        m_engine->showMessage(QString::fromUtf8(output), AppOutput);
    } else {
        DebuggerRunControl *runControl = qobject_cast<DebuggerRunControl *>(m_runControl);
        if (runControl)
            runControl->showMessage(QString::fromUtf8(output), AppOutput);
196
        else
Aurindam Jana's avatar
Aurindam Jana committed
197
            AndroidRunSupport::handleRemoteOutput(output);
198
    }
BogDan Vatra's avatar
BogDan Vatra committed
199
200
201
202
}

void AndroidDebugSupport::handleRemoteErrorOutput(const QByteArray &output)
{
Aurindam Jana's avatar
Aurindam Jana committed
203
204
205
206
207
208
    if (m_engine) {
        m_engine->showMessage(QString::fromUtf8(output), AppError);
    } else {
        DebuggerRunControl *runControl = qobject_cast<DebuggerRunControl *>(m_runControl);
        if (runControl)
            runControl->showMessage(QString::fromUtf8(output), AppError);
209
        else
Aurindam Jana's avatar
Aurindam Jana committed
210
            AndroidRunSupport::handleRemoteErrorOutput(output);
211
    }
BogDan Vatra's avatar
BogDan Vatra committed
212
213
214
}

} // namespace Internal
hjk's avatar
hjk committed
215
} // namespace Android