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
|
//===-- 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"
// Project includes
#include "AdbClient.h"
#include "PlatformAndroidRemoteGDBServer.h"
#include "Utility/UriParser.h"
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 (uint16_t port, std::string& device_id)
{
Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
// Fetch the device list from ADB and if only 1 device found then use that device
// TODO: Handle the case when more device is available
AdbClient adb;
auto error = AdbClient::CreateByDeviceID (nullptr, adb);
if (error.Fail ())
return error;
if (log)
log->Printf("Connected to Android device \"%s\"", adb.GetDeviceID ().c_str ());
return adb.SetPortForwarding (port);
}
static Error
DeleteForwardPortWithAdb (uint16_t port, const std::string& device_id)
{
AdbClient adb (device_id);
return adb.DeletePortForwarding (port);
}
PlatformAndroidRemoteGDBServer::PlatformAndroidRemoteGDBServer ()
{
}
PlatformAndroidRemoteGDBServer::~PlatformAndroidRemoteGDBServer ()
{
for (const auto& it : m_port_forwards)
{
DeleteForwardPortWithAdb (it.second.first, it.second.second);
}
}
uint16_t
PlatformAndroidRemoteGDBServer::LaunchGDBserverAndGetPort (lldb::pid_t &pid)
{
uint16_t port = m_gdb_client.LaunchGDBserverAndGetPort (pid, "127.0.0.1");
if (port == 0)
return port;
std::string device_id;
Error error = ForwardPortWithAdb (port, device_id);
if (error.Fail ())
return 0;
m_port_forwards[pid] = std::make_pair (port, device_id);
return port;
}
bool
PlatformAndroidRemoteGDBServer::KillSpawnedProcess (lldb::pid_t pid)
{
auto it = m_port_forwards.find (pid);
if (it != m_port_forwards.end ())
{
DeleteForwardPortWithAdb (it->second.first, it->second.second);
m_port_forwards.erase (it);
}
return m_gdb_client.KillSpawnedProcess (pid);
}
Error
PlatformAndroidRemoteGDBServer::ConnectRemote (Args& args)
{
if (args.GetArgumentCount () != 1)
return Error ("\"platform connect\" takes a single argument: <connect-url>");
int port;
std::string scheme, host, path;
const char *url = args.GetArgumentAtIndex (0);
if (!UriParser::Parse (url, scheme, host, port, path))
return Error ("invalid uri");
std::string device_id;
Error error = ForwardPortWithAdb (port, device_id);
if (error.Fail ())
return error;
m_port_forwards[g_remote_platform_pid] = std::make_pair (port, device_id);
return PlatformRemoteGDBServer::ConnectRemote (args);
}
Error
PlatformAndroidRemoteGDBServer::DisconnectRemote ()
{
auto it = m_port_forwards.find (g_remote_platform_pid);
if (it != m_port_forwards.end ())
{
DeleteForwardPortWithAdb (it->second.first, it->second.second);
m_port_forwards.erase (it);
}
return PlatformRemoteGDBServer::DisconnectRemote ();
}
|