blob: adae9f7f46ef437dcbbc57bc27444d24692d0867 [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
Josh Gaofa3605a2020-03-16 11:30:09 -070019#if !defined(__ANDROID_RECOVERY__)
Yabin Cui19bec5b2015-09-22 15:52:57 -070020#define TRACE_TAG JDWP
Dan Albertdb6fe642015-03-19 15:21:08 -070021
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022#include "sysdeps.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070023
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080024#include <errno.h>
Shukang Zhou420ad552020-02-13 17:01:39 -080025#include <inttypes.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080026#include <stdio.h>
Elliott Hughes43df1092015-07-23 17:12:58 -070027#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028#include <string.h>
Josh Gao9eaf33c2016-05-13 15:28:34 -070029#include <sys/socket.h>
30#include <sys/un.h>
Teddie Stenvif56e7f52010-02-15 12:20:44 +010031#include <unistd.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080032
Josh Gao9eaf33c2016-05-13 15:28:34 -070033#include <list>
34#include <memory>
Josh Gaoeb5e6052019-06-19 14:14:57 -070035#include <thread>
Josh Gao9eaf33c2016-05-13 15:28:34 -070036#include <vector>
37
Shukang Zhou420ad552020-02-13 17:01:39 -080038#include <adbconnection/process_info.h>
Josh Gaoeb5e6052019-06-19 14:14:57 -070039#include <adbconnection/server.h>
Josh Gao2d83b542019-01-10 14:29:29 -080040#include <android-base/cmsg.h>
Josh Gaoeb5e6052019-06-19 14:14:57 -070041#include <android-base/unique_fd.h>
Josh Gao2d83b542019-01-10 14:29:29 -080042
Dan Albertdb6fe642015-03-19 15:21:08 -070043#include "adb.h"
Josh Gao9eaf33c2016-05-13 15:28:34 -070044#include "adb_io.h"
Josh Gaoea7457b2016-08-30 15:39:25 -070045#include "adb_unique_fd.h"
Yabin Cui5fc22312015-10-06 15:10:05 -070046#include "adb_utils.h"
Shukang Zhou420ad552020-02-13 17:01:39 -080047#include "app_processes.pb.h"
Dan Albertdb6fe642015-03-19 15:21:08 -070048
Josh Gaoeb5e6052019-06-19 14:14:57 -070049using android::base::borrowed_fd;
50using android::base::unique_fd;
51
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080052/* here's how these things work.
53
54 when adbd starts, it creates a unix server socket
Josh Gao9eaf33c2016-05-13 15:28:34 -070055 named @jdwp-control (@ is a shortcut for "first byte is zero"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056 to use the private namespace instead of the file system)
57
58 when a new JDWP daemon thread starts in a new VM process, it creates
Josh Gao9eaf33c2016-05-13 15:28:34 -070059 a connection to @jdwp-control to announce its availability.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080060
61
Josh Gao9eaf33c2016-05-13 15:28:34 -070062 JDWP thread @jdwp-control
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080063 | |
64 |-------------------------------> |
65 | hello I'm in process <pid> |
66 | |
67 | |
68
69 the connection is kept alive. it will be closed automatically if
70 the JDWP process terminates (this allows adbd to detect dead
71 processes).
72
73 adbd thus maintains a list of "active" JDWP processes. it can send
74 its content to clients through the "device:debug-ports" service,
75 or even updates through the "device:track-debug-ports" service.
76
77 when a debugger wants to connect, it simply runs the command
78 equivalent to "adb forward tcp:<hostport> jdwp:<pid>"
79
80 "jdwp:<pid>" is a new forward destination format used to target
81 a given JDWP process on the device. when sutch a request arrives,
82 adbd does the following:
83
84 - first, it calls socketpair() to create a pair of equivalent
85 sockets.
86
87 - it attaches the first socket in the pair to a local socket
88 which is itself attached to the transport's remote socket:
89
90
91 - it sends the file descriptor of the second socket directly
92 to the JDWP process with the help of sendmsg()
93
94
Josh Gao9eaf33c2016-05-13 15:28:34 -070095 JDWP thread @jdwp-control
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096 | |
97 | <----------------------|
98 | OK, try this file descriptor |
99 | |
100 | |
101
102 then, the JDWP thread uses this new socket descriptor as its
103 pass-through connection to the debugger (and receives the
104 JDWP-Handshake message, answers to it, etc...)
105
106 this gives the following graphics:
107 ____________________________________
108 | |
109 | ADB Server (host) |
110 | |
111 Debugger <---> LocalSocket <----> RemoteSocket |
112 | ^^ |
113 |___________________________||_______|
114 ||
115 Transport ||
116 (TCP for emulator - USB for device) ||
117 ||
118 ___________________________||_______
119 | || |
120 | ADBD (device) || |
121 | VV |
122 JDWP <======> LocalSocket <----> RemoteSocket |
123 | |
124 |____________________________________|
125
126 due to the way adb works, this doesn't need a special socket
127 type or fancy handling of socket termination if either the debugger
128 or the JDWP process closes the connection.
129
130 THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN
131 TO HAVE A BETTER IDEA, LET ME KNOW - Digit
132
133**********************************************************************/
134
135/** JDWP PID List Support Code
136 ** for each JDWP process, we record its pid and its connected socket
137 **/
138
Shukang Zhou420ad552020-02-13 17:01:39 -0800139enum class TrackerKind {
140 kJdwp,
141 kApp,
142};
143
Josh Gao9eaf33c2016-05-13 15:28:34 -0700144static void jdwp_process_event(int socket, unsigned events, void* _proc);
145static void jdwp_process_list_updated(void);
Shukang Zhou420ad552020-02-13 17:01:39 -0800146static void app_process_list_updated(void);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800147
Josh Gao9eaf33c2016-05-13 15:28:34 -0700148struct JdwpProcess;
Josh Gao361148b2018-01-02 12:01:43 -0800149static auto& _jdwp_list = *new std::list<std::unique_ptr<JdwpProcess>>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800151struct JdwpProcess {
Shukang Zhou420ad552020-02-13 17:01:39 -0800152 JdwpProcess(unique_fd socket, ProcessInfo process) {
153 CHECK(process.pid != 0);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700154
Josh Gao9eaf33c2016-05-13 15:28:34 -0700155 this->socket = socket;
Shukang Zhou420ad552020-02-13 17:01:39 -0800156 this->process = process;
Josh Gaoeb5e6052019-06-19 14:14:57 -0700157 this->fde = fdevent_create(socket.release(), jdwp_process_event, this);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800158
Josh Gao9eaf33c2016-05-13 15:28:34 -0700159 if (!this->fde) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700160 LOG(FATAL) << "could not create fdevent for new JDWP process";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700161 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700162 }
163
164 ~JdwpProcess() {
165 if (this->socket >= 0) {
166 adb_shutdown(this->socket);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700167 this->socket = -1;
168 }
169
170 if (this->fde) {
171 fdevent_destroy(this->fde);
172 this->fde = nullptr;
173 }
174
175 out_fds.clear();
176 }
177
178 void RemoveFromList() {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700179 auto pred = [this](const auto& proc) { return proc.get() == this; };
180 _jdwp_list.remove_if(pred);
181 }
182
Josh Gaoeb5e6052019-06-19 14:14:57 -0700183 borrowed_fd socket = -1;
Shukang Zhou420ad552020-02-13 17:01:39 -0800184 ProcessInfo process;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700185 fdevent* fde = nullptr;
186
187 std::vector<unique_fd> out_fds;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800188};
189
Shukang Zhou420ad552020-02-13 17:01:39 -0800190// Populate the list of processes for "track-jdwp" service.
Josh Gao9eaf33c2016-05-13 15:28:34 -0700191static size_t jdwp_process_list(char* buffer, size_t bufferlen) {
192 std::string temp;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800193
Josh Gao9eaf33c2016-05-13 15:28:34 -0700194 for (auto& proc : _jdwp_list) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800195 if (!proc->process.debuggable) continue;
196 std::string next = std::to_string(proc->process.pid) + "\n";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700197 if (temp.length() + next.length() > bufferlen) {
198 D("truncating JDWP process list (max len = %zu)", bufferlen);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800200 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700201 temp.append(next);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700203
204 memcpy(buffer, temp.data(), temp.length());
205 return temp.length();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206}
207
Shukang Zhou420ad552020-02-13 17:01:39 -0800208// Populate the list of processes for "track-app" service.
209// The list is a protobuf message in the binary format for efficiency.
210static size_t app_process_list(char* buffer, size_t bufferlen) {
211 adb::proto::AppProcesses output; // result that's guaranteed to fit in the given buffer
212 adb::proto::AppProcesses temp; // temporary result that may be longer than the given buffer
213 std::string serialized_message;
214
215 for (auto& proc : _jdwp_list) {
216 if (!proc->process.debuggable && !proc->process.profileable) continue;
217 auto* entry = temp.add_process();
218 entry->set_pid(proc->process.pid);
219 entry->set_debuggable(proc->process.debuggable);
220 entry->set_profileable(proc->process.profileable);
221 entry->set_architecture(proc->process.arch_name, proc->process.arch_name_length);
222 temp.SerializeToString(&serialized_message);
223 if (serialized_message.size() > bufferlen) {
224 D("truncating app process list (max len = %zu)", bufferlen);
225 break;
226 }
227 output = temp;
228 }
229 output.SerializeToString(&serialized_message);
230 memcpy(buffer, serialized_message.data(), serialized_message.length());
231 return serialized_message.length();
232}
233
234// Populate the list of processes for either "track-jdwp" or "track-app" services,
235// depending on the given kind.
236static size_t process_list(TrackerKind kind, char* buffer, size_t bufferlen) {
237 switch (kind) {
238 case TrackerKind::kJdwp:
239 return jdwp_process_list(buffer, bufferlen);
240 case TrackerKind::kApp:
241 return app_process_list(buffer, bufferlen);
242 }
243}
244
245static size_t process_list_msg(TrackerKind kind, char* buffer, size_t bufferlen) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700246 // Message is length-prefixed with 4 hex digits in ASCII.
247 static constexpr size_t header_len = 4;
248 if (bufferlen < header_len) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700249 LOG(FATAL) << "invalid JDWP process list buffer size: " << bufferlen;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800250 }
251
Josh Gao9eaf33c2016-05-13 15:28:34 -0700252 char head[header_len + 1];
Shukang Zhou420ad552020-02-13 17:01:39 -0800253 size_t len = process_list(kind, buffer + header_len, bufferlen - header_len);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700254 snprintf(head, sizeof head, "%04zx", len);
255 memcpy(buffer, head, header_len);
256 return len + header_len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800257}
258
Josh Gao9eaf33c2016-05-13 15:28:34 -0700259static void jdwp_process_event(int socket, unsigned events, void* _proc) {
260 JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700261 CHECK_EQ(socket, proc->socket.get());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800262
263 if (events & FDE_READ) {
Josh Gaoeb5e6052019-06-19 14:14:57 -0700264 // We already have the PID, if we can read from the socket, we've probably hit EOF.
Shukang Zhou420ad552020-02-13 17:01:39 -0800265 D("terminating JDWP connection %" PRId64, proc->process.pid);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700266 goto CloseProcess;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800267 }
268
269 if (events & FDE_WRITE) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700270 D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size());
Josh Gaocc1dcc82018-10-05 17:09:41 -0700271 CHECK(!proc->out_fds.empty());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800272
Josh Gaocc1dcc82018-10-05 17:09:41 -0700273 int fd = proc->out_fds.back().get();
Josh Gao2d83b542019-01-10 14:29:29 -0800274 if (android::base::SendFileDescriptors(socket, "", 1, fd) != 1) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800275 D("sending new file descriptor to JDWP %" PRId64 " failed: %s", proc->process.pid,
276 strerror(errno));
Josh Gaocc1dcc82018-10-05 17:09:41 -0700277 goto CloseProcess;
278 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800279
Shukang Zhou420ad552020-02-13 17:01:39 -0800280 D("sent file descriptor %d to JDWP process %" PRId64, fd, proc->process.pid);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700281
Josh Gaocc1dcc82018-10-05 17:09:41 -0700282 proc->out_fds.pop_back();
283 if (proc->out_fds.empty()) {
284 fdevent_del(proc->fde, FDE_WRITE);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800285 }
286 }
Josh Gao9eaf33c2016-05-13 15:28:34 -0700287
288 return;
289
290CloseProcess:
Shukang Zhou420ad552020-02-13 17:01:39 -0800291 bool debuggable = proc->process.debuggable;
292 bool profileable = proc->process.profileable;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700293 proc->RemoveFromList();
Shukang Zhou420ad552020-02-13 17:01:39 -0800294 if (debuggable) jdwp_process_list_updated();
295 if (debuggable || profileable) app_process_list_updated();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296}
297
Josh Gao5607e922018-07-25 18:15:52 -0700298unique_fd create_jdwp_connection_fd(int pid) {
Yabin Cui815ad882015-09-02 17:44:28 -0700299 D("looking for pid %d in JDWP process list", pid);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700300
301 for (auto& proc : _jdwp_list) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800302 // Don't allow JDWP connection to a non-debuggable process.
303 if (!proc->process.debuggable) continue;
304 if (proc->process.pid == static_cast<uint64_t>(pid)) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700305 int fds[2];
306
307 if (adb_socketpair(fds) < 0) {
308 D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno));
Josh Gao5607e922018-07-25 18:15:52 -0700309 return unique_fd{};
Josh Gao9eaf33c2016-05-13 15:28:34 -0700310 }
311 D("socketpair: (%d,%d)", fds[0], fds[1]);
312
313 proc->out_fds.emplace_back(fds[1]);
314 if (proc->out_fds.size() == 1) {
315 fdevent_add(proc->fde, FDE_WRITE);
316 }
317
Josh Gao5607e922018-07-25 18:15:52 -0700318 return unique_fd{fds[0]};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800319 }
320 }
Yabin Cui815ad882015-09-02 17:44:28 -0700321 D("search failed !!");
Josh Gao5607e922018-07-25 18:15:52 -0700322 return unique_fd{};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800323}
324
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800325/** "jdwp" local service implementation
326 ** this simply returns the list of known JDWP process pids
327 **/
328
Josh Gao9eaf33c2016-05-13 15:28:34 -0700329struct JdwpSocket : public asocket {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800330 bool pass = false;
Elliott Hughesfe7ff812015-04-17 09:47:42 -0700331};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800332
Josh Gao9eaf33c2016-05-13 15:28:34 -0700333static void jdwp_socket_close(asocket* s) {
334 D("LS(%d): closing jdwp socket", s->id);
335
336 if (s->peer) {
337 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
338 s->peer->peer = nullptr;
339 s->peer->close(s->peer);
340 s->peer = nullptr;
341 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800342
343 remove_socket(s);
Josh Gao5cb76ce2018-02-12 17:24:00 -0800344 delete s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800345}
346
Josh Gaocd2a5292018-03-07 16:52:28 -0800347static int jdwp_socket_enqueue(asocket* s, apacket::payload_type) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800348 /* you can't write to this asocket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700349 D("LS(%d): JDWP socket received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800350 s->peer->close(s->peer);
351 return -1;
352}
353
Josh Gao9eaf33c2016-05-13 15:28:34 -0700354static void jdwp_socket_ready(asocket* s) {
355 JdwpSocket* jdwp = (JdwpSocket*)s;
356 asocket* peer = jdwp->peer;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800357
Josh Gao9eaf33c2016-05-13 15:28:34 -0700358 /* on the first call, send the list of pids,
359 * on the second one, close the connection
360 */
361 if (!jdwp->pass) {
Josh Gaocd2a5292018-03-07 16:52:28 -0800362 apacket::payload_type data;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800363 data.resize(s->get_max_payload());
364 size_t len = jdwp_process_list(&data[0], data.size());
365 data.resize(len);
366 peer->enqueue(peer, std::move(data));
Josh Gao9eaf33c2016-05-13 15:28:34 -0700367 jdwp->pass = true;
368 } else {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800369 peer->close(peer);
370 }
371}
372
Josh Gao9eaf33c2016-05-13 15:28:34 -0700373asocket* create_jdwp_service_socket(void) {
Josh Gao5cb76ce2018-02-12 17:24:00 -0800374 JdwpSocket* s = new JdwpSocket();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800375
Josh Gao9eaf33c2016-05-13 15:28:34 -0700376 if (!s) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700377 LOG(FATAL) << "failed to allocate JdwpSocket";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700378 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379
Josh Gao9eaf33c2016-05-13 15:28:34 -0700380 install_local_socket(s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800381
Josh Gao9eaf33c2016-05-13 15:28:34 -0700382 s->ready = jdwp_socket_ready;
383 s->enqueue = jdwp_socket_enqueue;
384 s->close = jdwp_socket_close;
385 s->pass = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800386
Josh Gao9eaf33c2016-05-13 15:28:34 -0700387 return s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388}
389
390/** "track-jdwp" local service implementation
391 ** this periodically sends the list of known JDWP process pids
392 ** to the client...
393 **/
394
Josh Gao9eaf33c2016-05-13 15:28:34 -0700395struct JdwpTracker : public asocket {
Shukang Zhou420ad552020-02-13 17:01:39 -0800396 TrackerKind kind;
Josh Gao9eaf33c2016-05-13 15:28:34 -0700397 bool need_initial;
Shukang Zhou420ad552020-02-13 17:01:39 -0800398
399 explicit JdwpTracker(TrackerKind k, bool initial) : kind(k), need_initial(initial) {}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800400};
401
Josh Gao361148b2018-01-02 12:01:43 -0800402static auto& _jdwp_trackers = *new std::vector<std::unique_ptr<JdwpTracker>>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800403
Shukang Zhou420ad552020-02-13 17:01:39 -0800404static void process_list_updated(TrackerKind kind) {
Josh Gaoa7d9d712018-02-01 13:17:50 -0800405 std::string data;
Shukang Zhou420ad552020-02-13 17:01:39 -0800406 const int kMaxLength = kind == TrackerKind::kJdwp ? 1024 : 2048;
407 data.resize(kMaxLength);
408 data.resize(process_list_msg(kind, &data[0], data.size()));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800409
Josh Gao9eaf33c2016-05-13 15:28:34 -0700410 for (auto& t : _jdwp_trackers) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800411 if (t->kind == kind && t->peer) {
Josh Gao9eaf33c2016-05-13 15:28:34 -0700412 // The tracker might not have been connected yet.
Josh Gaocd2a5292018-03-07 16:52:28 -0800413 apacket::payload_type payload(data.begin(), data.end());
414 t->peer->enqueue(t->peer, std::move(payload));
Josh Gao9eaf33c2016-05-13 15:28:34 -0700415 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800416 }
417}
418
Shukang Zhou420ad552020-02-13 17:01:39 -0800419static void jdwp_process_list_updated(void) {
420 process_list_updated(TrackerKind::kJdwp);
421}
422
423static void app_process_list_updated(void) {
424 process_list_updated(TrackerKind::kApp);
425}
426
Josh Gao9eaf33c2016-05-13 15:28:34 -0700427static void jdwp_tracker_close(asocket* s) {
428 D("LS(%d): destroying jdwp tracker service", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800429
Josh Gao9eaf33c2016-05-13 15:28:34 -0700430 if (s->peer) {
431 D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
432 s->peer->peer = nullptr;
433 s->peer->close(s->peer);
434 s->peer = nullptr;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800435 }
436
437 remove_socket(s);
438
Josh Gao9eaf33c2016-05-13 15:28:34 -0700439 auto pred = [s](const auto& tracker) { return tracker.get() == s; };
Josh Gaoeea5fea2017-03-10 11:19:48 -0800440 _jdwp_trackers.erase(std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred),
441 _jdwp_trackers.end());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800442}
443
Josh Gao9eaf33c2016-05-13 15:28:34 -0700444static void jdwp_tracker_ready(asocket* s) {
445 JdwpTracker* t = (JdwpTracker*)s;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800446
Josh Gao9eaf33c2016-05-13 15:28:34 -0700447 if (t->need_initial) {
Josh Gaocd2a5292018-03-07 16:52:28 -0800448 apacket::payload_type data;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800449 data.resize(s->get_max_payload());
Shukang Zhou420ad552020-02-13 17:01:39 -0800450 data.resize(process_list_msg(t->kind, &data[0], data.size()));
Josh Gao9eaf33c2016-05-13 15:28:34 -0700451 t->need_initial = false;
Josh Gaoa7d9d712018-02-01 13:17:50 -0800452 s->peer->enqueue(s->peer, std::move(data));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453 }
454}
455
Josh Gaocd2a5292018-03-07 16:52:28 -0800456static int jdwp_tracker_enqueue(asocket* s, apacket::payload_type) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800457 /* you can't write to this socket */
Josh Gao9eaf33c2016-05-13 15:28:34 -0700458 D("LS(%d): JDWP tracker received data?", s->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800459 s->peer->close(s->peer);
460 return -1;
461}
462
Josh Gaofa3605a2020-03-16 11:30:09 -0700463static asocket* create_process_tracker_service_socket(TrackerKind kind) {
Shukang Zhou420ad552020-02-13 17:01:39 -0800464 auto t = std::make_unique<JdwpTracker>(kind, true);
Josh Gao9eaf33c2016-05-13 15:28:34 -0700465 if (!t) {
Elliott Hughese64126b2018-10-19 13:59:44 -0700466 LOG(FATAL) << "failed to allocate JdwpTracker";
Josh Gao9eaf33c2016-05-13 15:28:34 -0700467 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800468
Josh Gao9eaf33c2016-05-13 15:28:34 -0700469 memset(t.get(), 0, sizeof(asocket));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800470
Josh Gao9eaf33c2016-05-13 15:28:34 -0700471 install_local_socket(t.get());
472 D("LS(%d): created new jdwp tracker service", t->id);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800473
Josh Gao9eaf33c2016-05-13 15:28:34 -0700474 t->ready = jdwp_tracker_ready;
475 t->enqueue = jdwp_tracker_enqueue;
476 t->close = jdwp_tracker_close;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800477
Josh Gao9eaf33c2016-05-13 15:28:34 -0700478 asocket* result = t.get();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800479
Josh Gao9eaf33c2016-05-13 15:28:34 -0700480 _jdwp_trackers.emplace_back(std::move(t));
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800481
Josh Gao9eaf33c2016-05-13 15:28:34 -0700482 return result;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800483}
484
Shukang Zhou420ad552020-02-13 17:01:39 -0800485asocket* create_jdwp_tracker_service_socket() {
486 return create_process_tracker_service_socket(TrackerKind::kJdwp);
487}
488
489asocket* create_app_tracker_service_socket() {
490 return create_process_tracker_service_socket(TrackerKind::kApp);
491}
492
Josh Gao9eaf33c2016-05-13 15:28:34 -0700493int init_jdwp(void) {
Josh Gaoeb5e6052019-06-19 14:14:57 -0700494 std::thread([]() {
495 adb_thread_setname("jdwp control");
Shukang Zhou420ad552020-02-13 17:01:39 -0800496 adbconnection_listen([](int fd, ProcessInfo process) {
497 LOG(INFO) << "jdwp connection from " << process.pid;
498 fdevent_run_on_main_thread([fd, process] {
Josh Gaoeb5e6052019-06-19 14:14:57 -0700499 unique_fd ufd(fd);
Shukang Zhou420ad552020-02-13 17:01:39 -0800500 auto proc = std::make_unique<JdwpProcess>(std::move(ufd), process);
Josh Gaoeb5e6052019-06-19 14:14:57 -0700501 if (!proc) {
502 LOG(FATAL) << "failed to allocate JdwpProcess";
503 }
504 _jdwp_list.emplace_back(std::move(proc));
Shukang Zhou420ad552020-02-13 17:01:39 -0800505 if (process.debuggable) jdwp_process_list_updated();
506 if (process.debuggable || process.profileable) app_process_list_updated();
Josh Gaoeb5e6052019-06-19 14:14:57 -0700507 });
508 });
509 }).detach();
510 return 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800511}
512
Josh Gaofa3605a2020-03-16 11:30:09 -0700513#else // !defined(__ANDROID_RECOVERY)
514#include "adb.h"
515
516asocket* create_jdwp_service_socket(void) {
517 return nullptr;
518}
519
520unique_fd create_jdwp_connection_fd(int pid) {
521 return {};
522}
523
524asocket* create_app_tracker_service_socket() {
525 return nullptr;
526}
527
528asocket* create_jdwp_tracker_service_socket() {
529 return nullptr;
530}
531
532int init_jdwp() {
533 return 0;
534}
535
536#endif /* defined(__ANDROID_RECOVERY__) */
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800537#endif /* !ADB_HOST */