aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp
blob: f11f2874e3562e5fccd022b73178f42f53b11387 (plain) (blame)
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
//===-- PlatformAndroidRemoteGDBServer.cpp ----------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// Other libraries and framework includes
#include "lldb/Core/Error.h"
#include "lldb/Core/Log.h"
#include "lldb/Host/common/TCPSocket.h"
#include "lldb/Host/ConnectionFileDescriptor.h"

#include "PlatformAndroidRemoteGDBServer.h"
#include "Utility/UriParser.h"

#include <sstream>

using namespace lldb;
using namespace lldb_private;
using namespace platform_android;

static const lldb::pid_t g_remote_platform_pid = 0; // Alias for the process id of lldb-platform

static Error
ForwardPortWithAdb (const uint16_t local_port,
                    const uint16_t remote_port,
                    const char* remote_socket_name,
                    const llvm::Optional<AdbClient::UnixSocketNamespace>& socket_namespace,
                    std::string& device_id)
{
    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));

    AdbClient adb;
    auto error = AdbClient::CreateByDeviceID(device_id, adb);
    if (error.Fail ())
        return error;

    device_id = adb.GetDeviceID();
    if (log)
        log->Printf("Connected to Android device \"%s\"", device_id.c_str ());

    if (remote_port != 0)
    {
        if (log)
            log->Printf("Forwarding remote TCP port %d to local TCP port %d", remote_port, local_port);
        return adb.SetPortForwarding(local_port, remote_port);
    }

    if (log)
        log->Printf("Forwarding remote socket \"%s\" to local TCP port %d", remote_socket_name, local_port);

    if (!socket_namespace)
        return Error("Invalid socket namespace");

    return adb.SetPortForwarding(local_port, remote_socket_name, *socket_namespace);
}

static Error
DeleteForwardPortWithAdb (uint16_t local_port, const std::string& device_id)
{
    AdbClient adb (device_id);
    return adb.DeletePortForwarding (local_port);
}

static Error
FindUnusedPort (uint16_t& port)
{
    Error error;
    std::unique_ptr<TCPSocket> tcp_socket(new TCPSocket(false, error));
    if (error.Fail())
        return error;

    error = tcp_socket->Listen("127.0.0.1:0", 1);
    if (error.Success())
        port = tcp_socket->GetLocalPortNumber();

    return error;
}

PlatformAndroidRemoteGDBServer::PlatformAndroidRemoteGDBServer ()
{
}

PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer ()
{
    for (const auto& it : m_port_forwards)
        DeleteForwardPortWithAdb(it.second, m_device_id);
}

bool
PlatformAndroidRemoteGDBServer::LaunchGDBServer (lldb::pid_t &pid, std::string &connect_url)
{
    uint16_t remote_port = 0;
    std::string socket_name;
    if (!m_gdb_client.LaunchGDBServer ("127.0.0.1", pid, remote_port, socket_name))
        return false;

    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));

    auto error = MakeConnectURL (pid,
                                 remote_port,
                                 socket_name.c_str (),
                                 connect_url);
    if (error.Success() && log)
        log->Printf("gdbserver connect URL: %s", connect_url.c_str());

    return error.Success();
}

bool
PlatformAndroidRemoteGDBServer::KillSpawnedProcess (lldb::pid_t pid)
{
    DeleteForwardPort (pid);
    return m_gdb_client.KillSpawnedProcess (pid);
}

Error
PlatformAndroidRemoteGDBServer::ConnectRemote (Args& args)
{
    m_device_id.clear();

    if (args.GetArgumentCount() != 1)
        return Error("\"platform connect\" takes a single argument: <connect-url>");

    int remote_port;
    std::string scheme, host, path;
    const char *url = args.GetArgumentAtIndex (0);
    if (!url)
        return Error("URL is null.");
    if (!UriParser::Parse (url, scheme, host, remote_port, path))
        return Error("Invalid URL: %s", url);
    if (host != "localhost")
        m_device_id = host;

    m_socket_namespace.reset();
    if (scheme == ConnectionFileDescriptor::UNIX_CONNECT_SCHEME)
        m_socket_namespace = AdbClient::UnixSocketNamespaceFileSystem;
    else if (scheme == ConnectionFileDescriptor::UNIX_ABSTRACT_CONNECT_SCHEME)
        m_socket_namespace = AdbClient::UnixSocketNamespaceAbstract;

    std::string connect_url;
    auto error = MakeConnectURL (g_remote_platform_pid,
                                 (remote_port < 0) ? 0 : remote_port,
                                 path.c_str (),
                                 connect_url);

    if (error.Fail ())
        return error;

    args.ReplaceArgumentAtIndex (0, connect_url.c_str ());

    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
    if (log)
        log->Printf("Rewritten platform connect URL: %s", connect_url.c_str());

    error = PlatformRemoteGDBServer::ConnectRemote(args);
    if (error.Fail ())
        DeleteForwardPort (g_remote_platform_pid);

    return error;
}

Error
PlatformAndroidRemoteGDBServer::DisconnectRemote ()
{
    DeleteForwardPort (g_remote_platform_pid);
    return PlatformRemoteGDBServer::DisconnectRemote ();
}

void
PlatformAndroidRemoteGDBServer::DeleteForwardPort (lldb::pid_t pid)
{
    Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));

    auto it = m_port_forwards.find(pid);
    if (it == m_port_forwards.end())
        return;

    const auto port = it->second;
    const auto error = DeleteForwardPortWithAdb(port, m_device_id);
    if (error.Fail()) {
        if (log)
            log->Printf("Failed to delete port forwarding (pid=%" PRIu64 ", port=%d, device=%s): %s",
                         pid, port, m_device_id.c_str(), error.AsCString());
    }
    m_port_forwards.erase(it);
}

Error
PlatformAndroidRemoteGDBServer::MakeConnectURL(const lldb::pid_t pid,
                                               const uint16_t remote_port,
                                               const char* remote_socket_name,
                                               std::string& connect_url)
{
    static const int kAttempsNum = 5;

    Error error;
    // There is a race possibility that somebody will occupy
    // a port while we're in between FindUnusedPort and ForwardPortWithAdb -
    // adding the loop to mitigate such problem.
    for (auto i = 0; i < kAttempsNum; ++i)
    {
        uint16_t local_port = 0;
        error = FindUnusedPort(local_port);
        if (error.Fail())
            return error;

        error = ForwardPortWithAdb(local_port,
                                   remote_port,
                                   remote_socket_name,
                                   m_socket_namespace,
                                   m_device_id);
        if (error.Success())
        {
            m_port_forwards[pid] = local_port;
            std::ostringstream url_str;
            url_str << "connect://localhost:" << local_port;
            connect_url = url_str.str();
            break;
        }
    }

    return error;
}

lldb::ProcessSP
PlatformAndroidRemoteGDBServer::ConnectProcess(const char* connect_url,
                                               const char* plugin_name,
                                               lldb_private::Debugger &debugger,
                                               lldb_private::Target *target,
                                               lldb_private::Error &error)
{
    // We don't have the pid of the remote gdbserver when it isn't started by us but we still want
    // to store the list of port forwards we set up in our port forward map. Generate a fake pid for
    // these cases what won't collide with any other valid pid on android.
    static lldb::pid_t s_remote_gdbserver_fake_pid = 0xffffffffffffffffULL;

    int remote_port;
    std::string scheme, host, path;
    if (!UriParser::Parse(connect_url, scheme, host, remote_port, path))
    {
        error.SetErrorStringWithFormat("Invalid URL: %s", connect_url);
        return nullptr;
    }

    std::string new_connect_url;
    error = MakeConnectURL(s_remote_gdbserver_fake_pid--,
                           (remote_port < 0) ? 0 : remote_port,
                           path.c_str(),
                           new_connect_url);
    if (error.Fail())
        return nullptr;

    return PlatformRemoteGDBServer::ConnectProcess(new_connect_url.c_str(),
                                                   plugin_name,
                                                   debugger,
                                                   target,
                                                   error);
}