blob: c99aead9c8858ccb7dd3643c50a2abd1980fddaa [file] [log] [blame]
Dan Albertdb6fe642015-03-19 15:21:08 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Josh Gao9eaf33c2016-05-13 15:28:34 -070017#if !ADB_HOST
Dan Albertdb6fe642015-03-19 15:21:08 -070018
Yabin Cui19bec5b2015-09-22 15:52:57 -070019#define TRACE_TAG JDWP
Dan Albertdb6fe642015-03-19 15:21:08 -070020
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include "sysdeps.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070022
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <errno.h>
Shukang Zhou420ad552020-02-13 17:01:39 -080024#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080025#include <stdio.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070026#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080027#include <string.h>
Josh Gao9eaf33c2016-05-13 15:28:34 -070028#include <sys/socket.h>
29#include <sys/un.h>
Teddie Stenvif56e7f52010-02-15 12:20:44 +010030#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080031
Josh Gao9eaf33c2016-05-13 15:28:34 -070032#include <list>
33#include <memory>
Josh Gaoeb5e6052019-06-19 14:14:57 -070034#include <thread>
Josh Gao9eaf33c2016-05-13 15:28:34 -070035#include <vector>
36
Shukang Zhou420ad552020-02-13 17:01:39 -080037#include <adbconnection/process_info.h>
Josh Gaoeb5e6052019-06-19 14:14:57 -070038#include <adbconnection/server.h>
Josh Gao2d83b542019-01-10 14:29:29 -080039#include <android-base/cmsg.h>
Josh Gaoeb5e6052019-06-19 14:14:57 -070040#include <android-base/unique_fd.h>
Josh Gao2d83b542019-01-10 14:29:29 -080041
Dan Albertdb6fe642015-03-19 15:21:08 -070042#include "adb.h"
Josh Gao9eaf33c2016-05-13 15:28:34 -070043#include "adb_io.h"
Josh Gaoea7457b2016-08-30 15:39:25 -070044#include "adb_unique_fd.h"
Yabin Cui5fc22312015-10-06 15:10:05 -070045#include "adb_utils.h"
Shukang Zhou420ad552020-02-13 17:01:39 -080046#include "app_processes.pb.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070047
Josh Gaoeb5e6052019-06-19 14:14:57 -070048using android::base::borrowed_fd;
49using android::base::unique_fd;
50
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051/* here's how these things work.
52
53 when adbd starts, it creates a unix server socket
Josh Gao9eaf33c2016-05-13 15:28:34 -070054 named @jdwp-control (@ is a shortcut for "first byte is zero"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055 to use the private namespace instead of the file system)
56
57 when a new JDWP daemon thread starts in a new VM process, it creates
Josh Gao9eaf33c2016-05-13 15:28:34 -070058 a connection to @jdwp-control to announce its availability.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080059
60
Josh Gao9eaf33c2016-05-13 15:28:34 -070061 JDWP thread @jdwp-control
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080062 | |
63 |-------------------------------> |
64 | hello I'm in process <pid> |
65 | |
66 | |
67
68 the connection is kept alive. it will be closed automatically if
69 the JDWP process terminates (this allows adbd to detect dead
70 processes).
71
72 adbd thus maintains a list of "active" JDWP processes. it can send
73 its content to clients through the "device:debug-ports" service,
74 or even updates through the "device:track-debug-ports" service.
75
76 when a debugger wants to connect, it simply runs the command
77 equivalent to "adb forward tcp:<hostport> jdwp:<pid>"
78
79 "jdwp:<pid>" is a new forward destination format used to target
80 a given JDWP process on the device. when sutch a request arrives,
81 adbd does the following:
82
83 - first, it calls socketpair() to create a pair of equivalent
84 sockets.
85
86 - it attaches the first socket in the pair to a local socket
87 which is itself attached to the transport's remote socket:
88
89
90 - it sends the file descriptor of the second socket directly
91 to the JDWP process with the help of sendmsg()
92
93
Josh Gao9eaf33c2016-05-13 15:28:34 -070094 JDWP thread @jdwp-control
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080095 | |
96 | <----------------------|
97 | OK, try this file descriptor |
98 | |
99 | |
100
101 then, the JDWP thread uses this new socket descriptor as its
102 pass-through connection to the debugger (and receives the
103 JDWP-Handshake message, answers to it, etc...)
104
105 this gives the following graphics:
106 ____________________________________
107 | |
108 | ADB Server (host) |
109 | |
110 Debugger <---> LocalSocket <----> RemoteSocket |
111 | ^^ |
112 |___________________________||_______|
113 ||
114 Transport ||
115 (TCP for emulator - USB for device) ||
116 ||
117 ___________________________||_______
118 | || |
119 | ADBD (device) || |
120 | VV |
121 JDWP <======> LocalSocket <----> RemoteSocket |
122 | |
123 |____________________________________|
124
125 due to the way adb works, this doesn't need a special socket
126 type or fancy handling of socket termination if either the debugger
127 or the JDWP process closes the connection.
128
129 THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
130 TO HAVE A BETTER IDEA, LET ME KNOW - Digit
131
132**********************************************************************/
133
134/** JDWP PID List Support Code
135 ** for each JDWP process, we record its pid and its connected socket
136 **/
137
Shukang Zhou420ad552020-02-13 17:01:39 -0800138enum class TrackerKind {
139 kJdwp,
140 kApp,
141};
142
Josh Gao9eaf33c2016-05-13 15:28:34 -0700143static void jdwp_process_event(int socket, unsigned events, void* _proc);
144static void jdwp_process_list_updated(void);
Shukang Zhou420ad552020-02-13 17:01:39 -0800145static void app_process_list_updated(void);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800146
Josh Gao9eaf33c2016-05-13 15:28:34 -0700147struct JdwpProcess;
Josh Gao361148b2018-01-02 12:01:43 -0800148static auto& _jdwp_list = *new std::list<std::unique_ptr<JdwpProcess>>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800149
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150struct JdwpProcess {
Shukang Zhou420ad552020-02-13 17:01:39 -0800151 JdwpProcess(unique_fd socket, ProcessInfo process) {
152 CHECK(process.pid != 0);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700153
Josh Gao9eaf33c2016-05-13 15:28:34 -0700154 this->socket = socket;
Shukang Zhou420ad552020-02-13 17:01:39 -0800155 this->process = process;
Josh Gaoeb5e6052019-06-19 14:14:57 -0700156 this->fde = fdevent_create(socket.release(), jdwp_process_event, this);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800157
Josh Gao9eaf33c2016-05-13 15:28:34 -0700158 if (!this->fde) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700159 LOG(FATAL) << "could not create fdevent for new JDWP process";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700160 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700161 }
162
163 ~JdwpProcess() {
164 if (this->socket >= 0) {
165 adb_shutdown(this->socket);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700166 this->socket = -1;
167 }
168
169 if (this->fde) {
170 fdevent_destroy(this->fde);
171 this->fde = nullptr;
172 }
173
174 out_fds.clear();
175 }
176
177 void RemoveFromList() {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700178 auto pred = [this](const auto& proc) { return proc.get() == this; };
179 _jdwp_list.remove_if(pred);
180 }
181
Josh Gaoeb5e6052019-06-19 14:14:57 -0700182 borrowed_fd socket = -1;
Shukang Zhou420ad552020-02-13 17:01:39 -0800183 ProcessInfo process;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700184 fdevent* fde = nullptr;
185
186 std::vector<unique_fd> out_fds;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800187};
188
Shukang Zhou420ad552020-02-13 17:01:39 -0800189// Populate the list of processes for "track-jdwp" service.
Josh Gao9eaf33c2016-05-13 15:28:34 -0700190static size_t jdwp_process_list(char* buffer, size_t bufferlen) {
191 std::string temp;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192
Josh Gao9eaf33c2016-05-13 15:28:34 -0700193 for (auto& proc : _jdwp_list) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800194 if (!proc->process.debuggable) continue;
195 std::string next = std::to_string(proc->process.pid) + "\n";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700196 if (temp.length() + next.length() > bufferlen) {
197 D("truncating JDWP process list (max len = %zu)", bufferlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800198 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700200 temp.append(next);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800201 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700202
203 memcpy(buffer, temp.data(), temp.length());
204 return temp.length();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205}
206
Shukang Zhou420ad552020-02-13 17:01:39 -0800207// Populate the list of processes for "track-app" service.
208// The list is a protobuf message in the binary format for efficiency.
209static size_t app_process_list(char* buffer, size_t bufferlen) {
210 adb::proto::AppProcesses output; // result that's guaranteed to fit in the given buffer
211 adb::proto::AppProcesses temp; // temporary result that may be longer than the given buffer
212 std::string serialized_message;
213
214 for (auto& proc : _jdwp_list) {
215 if (!proc->process.debuggable && !proc->process.profileable) continue;
216 auto* entry = temp.add_process();
217 entry->set_pid(proc->process.pid);
218 entry->set_debuggable(proc->process.debuggable);
219 entry->set_profileable(proc->process.profileable);
220 entry->set_architecture(proc->process.arch_name, proc->process.arch_name_length);
221 temp.SerializeToString(&serialized_message);
222 if (serialized_message.size() > bufferlen) {
223 D("truncating app process list (max len = %zu)", bufferlen);
224 break;
225 }
226 output = temp;
227 }
228 output.SerializeToString(&serialized_message);
229 memcpy(buffer, serialized_message.data(), serialized_message.length());
230 return serialized_message.length();
231}
232
233// Populate the list of processes for either "track-jdwp" or "track-app" services,
234// depending on the given kind.
235static size_t process_list(TrackerKind kind, char* buffer, size_t bufferlen) {
236 switch (kind) {
237 case TrackerKind::kJdwp:
238 return jdwp_process_list(buffer, bufferlen);
239 case TrackerKind::kApp:
240 return app_process_list(buffer, bufferlen);
241 }
242}
243
244static size_t process_list_msg(TrackerKind kind, char* buffer, size_t bufferlen) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700245 // Message is length-prefixed with 4 hex digits in ASCII.
246 static constexpr size_t header_len = 4;
247 if (bufferlen < header_len) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700248 LOG(FATAL) << "invalid JDWP process list buffer size: " << bufferlen;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800249 }
250
Josh Gao9eaf33c2016-05-13 15:28:34 -0700251 char head[header_len + 1];
Shukang Zhou420ad552020-02-13 17:01:39 -0800252 size_t len = process_list(kind, buffer + header_len, bufferlen - header_len);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700253 snprintf(head, sizeof head, "%04zx", len);
254 memcpy(buffer, head, header_len);
255 return len + header_len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800256}
257
Josh Gao9eaf33c2016-05-13 15:28:34 -0700258static void jdwp_process_event(int socket, unsigned events, void* _proc) {
259 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700260 CHECK_EQ(socket, proc->socket.get());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800261
262 if (events & FDE_READ) {
Josh Gaoeb5e6052019-06-19 14:14:57 -0700263 // We already have the PID, if we can read from the socket, we've probably hit EOF.
Shukang Zhou420ad552020-02-13 17:01:39 -0800264 D("terminating JDWP connection %" PRId64, proc->process.pid);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700265 goto CloseProcess;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800266 }
267
268 if (events & FDE_WRITE) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700269 D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size());
Josh Gaocc1dcc82018-10-05 17:09:41 -0700270 CHECK(!proc->out_fds.empty());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800271
Josh Gaocc1dcc82018-10-05 17:09:41 -0700272 int fd = proc->out_fds.back().get();
Josh Gao2d83b542019-01-10 14:29:29 -0800273 if (android::base::SendFileDescriptors(socket, "", 1, fd) != 1) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800274 D("sending new file descriptor to JDWP %" PRId64 " failed: %s", proc->process.pid,
275 strerror(errno));
Josh Gaocc1dcc82018-10-05 17:09:41 -0700276 goto CloseProcess;
277 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800278
Shukang Zhou420ad552020-02-13 17:01:39 -0800279 D("sent file descriptor %d to JDWP process %" PRId64, fd, proc->process.pid);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700280
Josh Gaocc1dcc82018-10-05 17:09:41 -0700281 proc->out_fds.pop_back();
282 if (proc->out_fds.empty()) {
283 fdevent_del(proc->fde, FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800284 }
285 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700286
287 return;
288
289CloseProcess:
Shukang Zhou420ad552020-02-13 17:01:39 -0800290 bool debuggable = proc->process.debuggable;
291 bool profileable = proc->process.profileable;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700292 proc->RemoveFromList();
Shukang Zhou420ad552020-02-13 17:01:39 -0800293 if (debuggable) jdwp_process_list_updated();
294 if (debuggable || profileable) app_process_list_updated();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800295}
296
Josh Gao5607e922018-07-25 18:15:52 -0700297unique_fd create_jdwp_connection_fd(int pid) {
Yabin Cui815ad882015-09-02 17:44:28 -0700298 D("looking for pid %d in JDWP process list", pid);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700299
300 for (auto& proc : _jdwp_list) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800301 // Don't allow JDWP connection to a non-debuggable process.
302 if (!proc->process.debuggable) continue;
303 if (proc->process.pid == static_cast<uint64_t>(pid)) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700304 int fds[2];
305
306 if (adb_socketpair(fds) < 0) {
307 D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno));
Josh Gao5607e922018-07-25 18:15:52 -0700308 return unique_fd{};
Josh Gao9eaf33c2016-05-13 15:28:34 -0700309 }
310 D("socketpair: (%d,%d)", fds[0], fds[1]);
311
312 proc->out_fds.emplace_back(fds[1]);
313 if (proc->out_fds.size() == 1) {
314 fdevent_add(proc->fde, FDE_WRITE);
315 }
316
Josh Gao5607e922018-07-25 18:15:52 -0700317 return unique_fd{fds[0]};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800318 }
319 }
Yabin Cui815ad882015-09-02 17:44:28 -0700320 D("search failed !!");
Josh Gao5607e922018-07-25 18:15:52 -0700321 return unique_fd{};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800322}
323
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800324/** "jdwp" local service implementation
325 ** this simply returns the list of known JDWP process pids
326 **/
327
Josh Gao9eaf33c2016-05-13 15:28:34 -0700328struct JdwpSocket : public asocket {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800329 bool pass = false;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700330};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800331
Josh Gao9eaf33c2016-05-13 15:28:34 -0700332static void jdwp_socket_close(asocket* s) {
333 D("LS(%d): closing jdwp socket", s->id);
334
335 if (s->peer) {
336 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
337 s->peer->peer = nullptr;
338 s->peer->close(s->peer);
339 s->peer = nullptr;
340 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800341
342 remove_socket(s);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800343 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800344}
345
Josh Gaocd2a5292018-03-07 16:52:28 -0800346static int jdwp_socket_enqueue(asocket* s, apacket::payload_type) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347 /* you can't write to this asocket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700348 D("LS(%d): JDWP socket received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800349 s->peer->close(s->peer);
350 return -1;
351}
352
Josh Gao9eaf33c2016-05-13 15:28:34 -0700353static void jdwp_socket_ready(asocket* s) {
354 JdwpSocket* jdwp = (JdwpSocket*)s;
355 asocket* peer = jdwp->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800356
Josh Gao9eaf33c2016-05-13 15:28:34 -0700357 /* on the first call, send the list of pids,
358 * on the second one, close the connection
359 */
360 if (!jdwp->pass) {
Josh Gaocd2a5292018-03-07 16:52:28 -0800361 apacket::payload_type data;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800362 data.resize(s->get_max_payload());
363 size_t len = jdwp_process_list(&data[0], data.size());
364 data.resize(len);
365 peer->enqueue(peer, std::move(data));
Josh Gao9eaf33c2016-05-13 15:28:34 -0700366 jdwp->pass = true;
367 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800368 peer->close(peer);
369 }
370}
371
Josh Gao9eaf33c2016-05-13 15:28:34 -0700372asocket* create_jdwp_service_socket(void) {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800373 JdwpSocket* s = new JdwpSocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800374
Josh Gao9eaf33c2016-05-13 15:28:34 -0700375 if (!s) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700376 LOG(FATAL) << "failed to allocate JdwpSocket";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700377 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800378
Josh Gao9eaf33c2016-05-13 15:28:34 -0700379 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800380
Josh Gao9eaf33c2016-05-13 15:28:34 -0700381 s->ready = jdwp_socket_ready;
382 s->enqueue = jdwp_socket_enqueue;
383 s->close = jdwp_socket_close;
384 s->pass = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800385
Josh Gao9eaf33c2016-05-13 15:28:34 -0700386 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800387}
388
389/** "track-jdwp" local service implementation
390 ** this periodically sends the list of known JDWP process pids
391 ** to the client...
392 **/
393
Josh Gao9eaf33c2016-05-13 15:28:34 -0700394struct JdwpTracker : public asocket {
Shukang Zhou420ad552020-02-13 17:01:39 -0800395 TrackerKind kind;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700396 bool need_initial;
Shukang Zhou420ad552020-02-13 17:01:39 -0800397
398 explicit JdwpTracker(TrackerKind k, bool initial) : kind(k), need_initial(initial) {}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800399};
400
Josh Gao361148b2018-01-02 12:01:43 -0800401static auto& _jdwp_trackers = *new std::vector<std::unique_ptr<JdwpTracker>>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800402
Shukang Zhou420ad552020-02-13 17:01:39 -0800403static void process_list_updated(TrackerKind kind) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800404 std::string data;
Shukang Zhou420ad552020-02-13 17:01:39 -0800405 const int kMaxLength = kind == TrackerKind::kJdwp ? 1024 : 2048;
406 data.resize(kMaxLength);
407 data.resize(process_list_msg(kind, &data[0], data.size()));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408
Josh Gao9eaf33c2016-05-13 15:28:34 -0700409 for (auto& t : _jdwp_trackers) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800410 if (t->kind == kind && t->peer) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700411 // The tracker might not have been connected yet.
Josh Gaocd2a5292018-03-07 16:52:28 -0800412 apacket::payload_type payload(data.begin(), data.end());
413 t->peer->enqueue(t->peer, std::move(payload));
Josh Gao9eaf33c2016-05-13 15:28:34 -0700414 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800415 }
416}
417
Shukang Zhou420ad552020-02-13 17:01:39 -0800418static void jdwp_process_list_updated(void) {
419 process_list_updated(TrackerKind::kJdwp);
420}
421
422static void app_process_list_updated(void) {
423 process_list_updated(TrackerKind::kApp);
424}
425
Josh Gao9eaf33c2016-05-13 15:28:34 -0700426static void jdwp_tracker_close(asocket* s) {
427 D("LS(%d): destroying jdwp tracker service", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800428
Josh Gao9eaf33c2016-05-13 15:28:34 -0700429 if (s->peer) {
430 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
431 s->peer->peer = nullptr;
432 s->peer->close(s->peer);
433 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800434 }
435
436 remove_socket(s);
437
Josh Gao9eaf33c2016-05-13 15:28:34 -0700438 auto pred = [s](const auto& tracker) { return tracker.get() == s; };
Josh Gaoeea5fea2017-03-10 11:19:48 -0800439 _jdwp_trackers.erase(std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred),
440 _jdwp_trackers.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441}
442
Josh Gao9eaf33c2016-05-13 15:28:34 -0700443static void jdwp_tracker_ready(asocket* s) {
444 JdwpTracker* t = (JdwpTracker*)s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800445
Josh Gao9eaf33c2016-05-13 15:28:34 -0700446 if (t->need_initial) {
Josh Gaocd2a5292018-03-07 16:52:28 -0800447 apacket::payload_type data;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800448 data.resize(s->get_max_payload());
Shukang Zhou420ad552020-02-13 17:01:39 -0800449 data.resize(process_list_msg(t->kind, &data[0], data.size()));
Josh Gao9eaf33c2016-05-13 15:28:34 -0700450 t->need_initial = false;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800451 s->peer->enqueue(s->peer, std::move(data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800452 }
453}
454
Josh Gaocd2a5292018-03-07 16:52:28 -0800455static int jdwp_tracker_enqueue(asocket* s, apacket::payload_type) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800456 /* you can't write to this socket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700457 D("LS(%d): JDWP tracker received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458 s->peer->close(s->peer);
459 return -1;
460}
461
Shukang Zhou420ad552020-02-13 17:01:39 -0800462asocket* create_process_tracker_service_socket(TrackerKind kind) {
463 auto t = std::make_unique<JdwpTracker>(kind, true);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700464 if (!t) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700465 LOG(FATAL) << "failed to allocate JdwpTracker";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700466 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800467
Josh Gao9eaf33c2016-05-13 15:28:34 -0700468 memset(t.get(), 0, sizeof(asocket));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800469
Josh Gao9eaf33c2016-05-13 15:28:34 -0700470 install_local_socket(t.get());
471 D("LS(%d): created new jdwp tracker service", t->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800472
Josh Gao9eaf33c2016-05-13 15:28:34 -0700473 t->ready = jdwp_tracker_ready;
474 t->enqueue = jdwp_tracker_enqueue;
475 t->close = jdwp_tracker_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800476
Josh Gao9eaf33c2016-05-13 15:28:34 -0700477 asocket* result = t.get();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800478
Josh Gao9eaf33c2016-05-13 15:28:34 -0700479 _jdwp_trackers.emplace_back(std::move(t));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800480
Josh Gao9eaf33c2016-05-13 15:28:34 -0700481 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800482}
483
Shukang Zhou420ad552020-02-13 17:01:39 -0800484asocket* create_jdwp_tracker_service_socket() {
485 return create_process_tracker_service_socket(TrackerKind::kJdwp);
486}
487
488asocket* create_app_tracker_service_socket() {
489 return create_process_tracker_service_socket(TrackerKind::kApp);
490}
491
Josh Gao9eaf33c2016-05-13 15:28:34 -0700492int init_jdwp(void) {
Josh Gaoeb5e6052019-06-19 14:14:57 -0700493 std::thread([]() {
494 adb_thread_setname("jdwp control");
Shukang Zhou420ad552020-02-13 17:01:39 -0800495 adbconnection_listen([](int fd, ProcessInfo process) {
496 LOG(INFO) << "jdwp connection from " << process.pid;
497 fdevent_run_on_main_thread([fd, process] {
Josh Gaoeb5e6052019-06-19 14:14:57 -0700498 unique_fd ufd(fd);
Shukang Zhou420ad552020-02-13 17:01:39 -0800499 auto proc = std::make_unique<JdwpProcess>(std::move(ufd), process);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700500 if (!proc) {
501 LOG(FATAL) << "failed to allocate JdwpProcess";
502 }
503 _jdwp_list.emplace_back(std::move(proc));
Shukang Zhou420ad552020-02-13 17:01:39 -0800504 if (process.debuggable) jdwp_process_list_updated();
505 if (process.debuggable || process.profileable) app_process_list_updated();
Josh Gaoeb5e6052019-06-19 14:14:57 -0700506 });
507 });
508 }).detach();
509 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800510}
511
512#endif /* !ADB_HOST */