1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 BogDan Vatra <bog_dan_ro@yahoo.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "androiddebugsupport.h"
27 
28 #include "androidconstants.h"
29 #include "androidglobal.h"
30 #include "androidrunner.h"
31 #include "androidmanager.h"
32 #include "androidqtversion.h"
33 
34 #include <debugger/debuggerkitinformation.h>
35 #include <debugger/debuggerrunconfigurationaspect.h>
36 #include <debugger/debuggerruncontrol.h>
37 
38 #include <projectexplorer/project.h>
39 #include <projectexplorer/projectnodes.h>
40 #include <projectexplorer/target.h>
41 #include <projectexplorer/toolchain.h>
42 
43 #include <qtsupport/qtkitinformation.h>
44 
45 #include <utils/hostosinfo.h>
46 
47 #include <QDirIterator>
48 #include <QHostAddress>
49 #include <QJsonDocument>
50 #include <QLoggingCategory>
51 
52 namespace {
53 static Q_LOGGING_CATEGORY(androidDebugSupportLog, "qtc.android.run.androiddebugsupport", QtWarningMsg)
54 }
55 
56 using namespace Debugger;
57 using namespace ProjectExplorer;
58 using namespace Utils;
59 
60 namespace Android {
61 namespace Internal {
62 
uniquePaths(const QStringList & files)63 static QStringList uniquePaths(const QStringList &files)
64 {
65     QSet<QString> paths;
66     for (const QString &file : files)
67         paths << QFileInfo(file).absolutePath();
68     return Utils::toList(paths);
69 }
70 
getSoLibSearchPath(const ProjectNode * node)71 static QStringList getSoLibSearchPath(const ProjectNode *node)
72 {
73     if (!node)
74         return {};
75 
76     QStringList res;
77     node->forEachProjectNode([&res](const ProjectNode *node) {
78          res.append(node->data(Constants::AndroidSoLibPath).toStringList());
79     });
80 
81     const QString jsonFile = AndroidQtVersion::androidDeploymentSettings(
82                 node->getProject()->activeTarget()).toString();
83     QFile deploymentSettings(jsonFile);
84     if (deploymentSettings.open(QIODevice::ReadOnly)) {
85         QJsonParseError error;
86         QJsonDocument doc = QJsonDocument::fromJson(deploymentSettings.readAll(), &error);
87         if (error.error == QJsonParseError::NoError) {
88             auto rootObj = doc.object();
89             auto it = rootObj.find("stdcpp-path");
90             if (it != rootObj.constEnd())
91                 res.append(QFileInfo(it.value().toString()).absolutePath());
92         }
93     }
94 
95     res.removeDuplicates();
96     return res;
97 }
98 
getExtraLibs(const ProjectNode * node)99 static QStringList getExtraLibs(const ProjectNode *node)
100 {
101     if (!node)
102         return {};
103     return node->data(Android::Constants::AndroidExtraLibs).toStringList();
104 }
105 
AndroidDebugSupport(RunControl * runControl,const QString & intentName)106 AndroidDebugSupport::AndroidDebugSupport(RunControl *runControl, const QString &intentName)
107     : Debugger::DebuggerRunTool(runControl)
108 {
109     setId("AndroidDebugger");
110     setLldbPlatform("remote-android");
111     m_runner = new AndroidRunner(runControl, intentName);
112     addStartDependency(m_runner);
113 }
114 
start()115 void AndroidDebugSupport::start()
116 {
117     Target *target = runControl()->target();
118     Kit *kit = target->kit();
119 
120     setStartMode(AttachToRemoteServer);
121     const QString packageName = AndroidManager::packageName(target);
122     setRunControlName(packageName);
123     setUseContinueInsteadOfRun(true);
124     setAttachPid(m_runner->pid());
125 
126     QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitAspect::qtVersion(kit);
127     if (!Utils::HostOsInfo::isWindowsHost()
128         && (qtVersion
129             && AndroidConfigurations::currentConfig().ndkVersion(qtVersion)
130                    >= QVersionNumber(11, 0, 0))) {
131         qCDebug(androidDebugSupportLog) << "UseTargetAsync: " << true;
132         setUseTargetAsync(true);
133     }
134 
135     if (isCppDebugging()) {
136         qCDebug(androidDebugSupportLog) << "C++ debugging enabled";
137         const ProjectNode *node = target->project()->findNodeForBuildKey(runControl()->buildKey());
138         QStringList solibSearchPath = getSoLibSearchPath(node);
139         QStringList extraLibs = getExtraLibs(node);
140         if (qtVersion)
141             solibSearchPath.append(qtVersion->qtSoPaths());
142         solibSearchPath.append(uniquePaths(extraLibs));
143 
144         const RunConfiguration *activeRunConfig = target->activeRunConfiguration();
145         FilePath buildDir;
146         if (activeRunConfig)
147             buildDir = activeRunConfig->buildTargetInfo().workingDirectory;
148         solibSearchPath.append(buildDir.toString());
149         solibSearchPath.removeDuplicates();
150         setSolibSearchPath(solibSearchPath);
151         qCDebug(androidDebugSupportLog) << "SoLibSearchPath: "<<solibSearchPath;
152         setSymbolFile(buildDir.pathAppended("app_process"));
153         setSkipExecutableValidation(true);
154         setUseExtendedRemote(true);
155         QString devicePreferredAbi = AndroidManager::apkDevicePreferredAbi(target);
156         setAbi(AndroidManager::androidAbi2Abi(devicePreferredAbi));
157 
158         QUrl debugServer;
159         debugServer.setPort(m_runner->debugServerPort().number());
160         if (cppEngineType() == LldbEngineType) {
161             debugServer.setScheme("adb");
162             debugServer.setHost(AndroidManager::deviceSerialNumber(target));
163             setRemoteChannel(debugServer.toString());
164         } else {
165             debugServer.setHost(QHostAddress(QHostAddress::LocalHost).toString());
166             setRemoteChannel(debugServer);
167         }
168 
169         auto qt = static_cast<AndroidQtVersion *>(qtVersion);
170         const int minimumNdk = qt ? qt->minimumNDK() : 0;
171 
172         int sdkVersion = qMax(AndroidManager::minimumSDK(kit), minimumNdk);
173         // TODO find a way to use the new sysroot layout
174         // instead ~/android/ndk-bundle/platforms/android-29/arch-arm64
175         // use ~/android/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot
176         if (qtVersion) {
177             Utils::FilePath sysRoot = AndroidConfigurations::currentConfig().ndkLocation(qtVersion)
178                     / "platforms"
179                     / QString("android-%1").arg(sdkVersion)
180                     / devicePreferredAbi;
181             setSysRoot(sysRoot);
182             qCDebug(androidDebugSupportLog) << "Sysroot: " << sysRoot;
183         }
184     }
185     if (isQmlDebugging()) {
186         qCDebug(androidDebugSupportLog) << "QML debugging enabled. QML server: "
187                                         << m_runner->qmlServer().toDisplayString();
188         setQmlServer(m_runner->qmlServer());
189         //TODO: Not sure if these are the right paths.
190         if (qtVersion)
191             addSearchDirectory(qtVersion->qmlPath());
192     }
193 
194     qCDebug(androidDebugSupportLog) << "Starting debugger - package name: " << packageName
195                                     << ", PID: " << m_runner->pid().pid();
196     DebuggerRunTool::start();
197 }
198 
stop()199 void AndroidDebugSupport::stop()
200 {
201     qCDebug(androidDebugSupportLog) << "Stop";
202     DebuggerRunTool::stop();
203 }
204 
205 } // namespace Internal
206 } // namespace Android
207