Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 17 | #if !ADB_HOST |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 19 | #define TRACE_TAG JDWP |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include "sysdeps.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
Elliott Hughes | 381cfa9 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <string.h> |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 27 | #include <sys/socket.h> |
| 28 | #include <sys/un.h> |
Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 31 | #include <list> |
| 32 | #include <memory> |
| 33 | #include <vector> |
| 34 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 35 | #include "adb.h" |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 36 | #include "adb_io.h" |
Josh Gao | 924d35a | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 37 | #include "adb_unique_fd.h" |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 38 | #include "adb_utils.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | /* here's how these things work. |
| 41 | |
| 42 | when adbd starts, it creates a unix server socket |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 43 | named @jdwp-control (@ is a shortcut for "first byte is zero" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | to use the private namespace instead of the file system) |
| 45 | |
| 46 | when a new JDWP daemon thread starts in a new VM process, it creates |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 47 | a connection to @jdwp-control to announce its availability. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
| 49 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 50 | JDWP thread @jdwp-control |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | | | |
| 52 | |-------------------------------> | |
| 53 | | hello I'm in process <pid> | |
| 54 | | | |
| 55 | | | |
| 56 | |
| 57 | the connection is kept alive. it will be closed automatically if |
| 58 | the JDWP process terminates (this allows adbd to detect dead |
| 59 | processes). |
| 60 | |
| 61 | adbd thus maintains a list of "active" JDWP processes. it can send |
| 62 | its content to clients through the "device:debug-ports" service, |
| 63 | or even updates through the "device:track-debug-ports" service. |
| 64 | |
| 65 | when a debugger wants to connect, it simply runs the command |
| 66 | equivalent to "adb forward tcp:<hostport> jdwp:<pid>" |
| 67 | |
| 68 | "jdwp:<pid>" is a new forward destination format used to target |
| 69 | a given JDWP process on the device. when sutch a request arrives, |
| 70 | adbd does the following: |
| 71 | |
| 72 | - first, it calls socketpair() to create a pair of equivalent |
| 73 | sockets. |
| 74 | |
| 75 | - it attaches the first socket in the pair to a local socket |
| 76 | which is itself attached to the transport's remote socket: |
| 77 | |
| 78 | |
| 79 | - it sends the file descriptor of the second socket directly |
| 80 | to the JDWP process with the help of sendmsg() |
| 81 | |
| 82 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 83 | JDWP thread @jdwp-control |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | | | |
| 85 | | <----------------------| |
| 86 | | OK, try this file descriptor | |
| 87 | | | |
| 88 | | | |
| 89 | |
| 90 | then, the JDWP thread uses this new socket descriptor as its |
| 91 | pass-through connection to the debugger (and receives the |
| 92 | JDWP-Handshake message, answers to it, etc...) |
| 93 | |
| 94 | this gives the following graphics: |
| 95 | ____________________________________ |
| 96 | | | |
| 97 | | ADB Server (host) | |
| 98 | | | |
| 99 | Debugger <---> LocalSocket <----> RemoteSocket | |
| 100 | | ^^ | |
| 101 | |___________________________||_______| |
| 102 | || |
| 103 | Transport || |
| 104 | (TCP for emulator - USB for device) || |
| 105 | || |
| 106 | ___________________________||_______ |
| 107 | | || | |
| 108 | | ADBD (device) || | |
| 109 | | VV | |
| 110 | JDWP <======> LocalSocket <----> RemoteSocket | |
| 111 | | | |
| 112 | |____________________________________| |
| 113 | |
| 114 | due to the way adb works, this doesn't need a special socket |
| 115 | type or fancy handling of socket termination if either the debugger |
| 116 | or the JDWP process closes the connection. |
| 117 | |
| 118 | THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN |
| 119 | TO HAVE A BETTER IDEA, LET ME KNOW - Digit |
| 120 | |
| 121 | **********************************************************************/ |
| 122 | |
| 123 | /** JDWP PID List Support Code |
| 124 | ** for each JDWP process, we record its pid and its connected socket |
| 125 | **/ |
| 126 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 127 | static void jdwp_process_event(int socket, unsigned events, void* _proc); |
| 128 | static void jdwp_process_list_updated(void); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 130 | struct JdwpProcess; |
| 131 | static std::list<std::unique_ptr<JdwpProcess>> _jdwp_list; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | struct JdwpProcess { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 134 | explicit JdwpProcess(int socket) { |
| 135 | this->socket = socket; |
| 136 | this->fde = fdevent_create(socket, jdwp_process_event, this); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 138 | if (!this->fde) { |
| 139 | fatal("could not create fdevent for new JDWP process"); |
| 140 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 142 | this->fde->state |= FDE_DONT_CLOSE; |
| 143 | |
| 144 | /* start by waiting for the PID */ |
| 145 | fdevent_add(this->fde, FDE_READ); |
| 146 | } |
| 147 | |
| 148 | ~JdwpProcess() { |
| 149 | if (this->socket >= 0) { |
| 150 | adb_shutdown(this->socket); |
| 151 | adb_close(this->socket); |
| 152 | this->socket = -1; |
| 153 | } |
| 154 | |
| 155 | if (this->fde) { |
| 156 | fdevent_destroy(this->fde); |
| 157 | this->fde = nullptr; |
| 158 | } |
| 159 | |
| 160 | out_fds.clear(); |
| 161 | } |
| 162 | |
| 163 | void RemoveFromList() { |
| 164 | if (this->pid >= 0) { |
| 165 | D("removing pid %d from jdwp process list", this->pid); |
| 166 | } else { |
| 167 | D("removing transient JdwpProcess from list"); |
| 168 | } |
| 169 | |
| 170 | auto pred = [this](const auto& proc) { return proc.get() == this; }; |
| 171 | _jdwp_list.remove_if(pred); |
| 172 | } |
| 173 | |
Josh Gao | 86f39b4 | 2018-02-12 15:05:05 -0800 | [diff] [blame] | 174 | int32_t pid = -1; |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 175 | int socket = -1; |
| 176 | fdevent* fde = nullptr; |
| 177 | |
| 178 | std::vector<unique_fd> out_fds; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 181 | static size_t jdwp_process_list(char* buffer, size_t bufferlen) { |
| 182 | std::string temp; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 184 | for (auto& proc : _jdwp_list) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | /* skip transient connections */ |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 186 | if (proc->pid < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | continue; |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 188 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 190 | std::string next = std::to_string(proc->pid) + "\n"; |
| 191 | if (temp.length() + next.length() > bufferlen) { |
| 192 | D("truncating JDWP process list (max len = %zu)", bufferlen); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | } |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 195 | temp.append(next); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | } |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 197 | |
| 198 | memcpy(buffer, temp.data(), temp.length()); |
| 199 | return temp.length(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 202 | static size_t jdwp_process_list_msg(char* buffer, size_t bufferlen) { |
| 203 | // Message is length-prefixed with 4 hex digits in ASCII. |
| 204 | static constexpr size_t header_len = 4; |
| 205 | if (bufferlen < header_len) { |
| 206 | fatal("invalid JDWP process list buffer size: %zu", bufferlen); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 209 | char head[header_len + 1]; |
| 210 | size_t len = jdwp_process_list(buffer + header_len, bufferlen - header_len); |
| 211 | snprintf(head, sizeof head, "%04zx", len); |
| 212 | memcpy(buffer, head, header_len); |
| 213 | return len + header_len; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 216 | static void jdwp_process_event(int socket, unsigned events, void* _proc) { |
| 217 | JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 218 | |
| 219 | if (events & FDE_READ) { |
| 220 | if (proc->pid < 0) { |
Josh Gao | 86f39b4 | 2018-02-12 15:05:05 -0800 | [diff] [blame] | 221 | ssize_t rc = TEMP_FAILURE_RETRY(recv(socket, &proc->pid, sizeof(proc->pid), 0)); |
| 222 | if (rc != sizeof(proc->pid)) { |
| 223 | D("failed to read jdwp pid: rc = %zd, errno = %s", rc, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | goto CloseProcess; |
| 225 | } |
| 226 | |
| 227 | /* all is well, keep reading to detect connection closure */ |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 228 | D("Adding pid %d to jdwp process list", proc->pid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | jdwp_process_list_updated(); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 230 | } else { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | /* the pid was read, if we get there it's probably because the connection |
| 232 | * was closed (e.g. the JDWP process exited or crashed) */ |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 233 | char buf[32]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 235 | while (true) { |
| 236 | int len = TEMP_FAILURE_RETRY(recv(socket, buf, sizeof(buf), 0)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 238 | if (len == 0) { |
| 239 | D("terminating JDWP %d connection: EOF", proc->pid); |
| 240 | break; |
| 241 | } else if (len < 0) { |
| 242 | if (len < 0 && errno == EAGAIN) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 244 | } |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 245 | |
| 246 | D("terminating JDWP %d connection: EOF", proc->pid); |
| 247 | break; |
| 248 | } else { |
| 249 | D("ignoring unexpected JDWP %d control socket activity (%d bytes)", proc->pid, |
| 250 | len); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 254 | goto CloseProcess; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | if (events & FDE_WRITE) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 259 | D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size()); |
| 260 | if (!proc->out_fds.empty()) { |
| 261 | int fd = proc->out_fds.back().get(); |
| 262 | struct cmsghdr* cmsg; |
| 263 | struct msghdr msg; |
| 264 | struct iovec iov; |
| 265 | char dummy = '!'; |
| 266 | char buffer[sizeof(struct cmsghdr) + sizeof(int)]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 268 | iov.iov_base = &dummy; |
| 269 | iov.iov_len = 1; |
| 270 | msg.msg_name = NULL; |
| 271 | msg.msg_namelen = 0; |
| 272 | msg.msg_iov = &iov; |
| 273 | msg.msg_iovlen = 1; |
| 274 | msg.msg_flags = 0; |
| 275 | msg.msg_control = buffer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | msg.msg_controllen = sizeof(buffer); |
| 277 | |
| 278 | cmsg = CMSG_FIRSTHDR(&msg); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 279 | cmsg->cmsg_len = msg.msg_controllen; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | cmsg->cmsg_level = SOL_SOCKET; |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 281 | cmsg->cmsg_type = SCM_RIGHTS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 | ((int*)CMSG_DATA(cmsg))[0] = fd; |
| 283 | |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 284 | if (!set_file_block_mode(proc->socket, true)) { |
| 285 | VLOG(JDWP) << "failed to set blocking mode for fd " << proc->socket; |
Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 286 | goto CloseProcess; |
| 287 | } |
| 288 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 289 | int ret = TEMP_FAILURE_RETRY(sendmsg(proc->socket, &msg, 0)); |
| 290 | if (ret < 0) { |
| 291 | D("sending new file descriptor to JDWP %d failed: %s", proc->pid, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 292 | goto CloseProcess; |
| 293 | } |
| 294 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 295 | D("sent file descriptor %d to JDWP process %d", fd, proc->pid); |
| 296 | |
| 297 | proc->out_fds.pop_back(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 | |
Yabin Cui | 6dfef25 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 299 | if (!set_file_block_mode(proc->socket, false)) { |
| 300 | VLOG(JDWP) << "failed to set non-blocking mode for fd " << proc->socket; |
Teddie Stenvi | 8f5daad | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 301 | goto CloseProcess; |
| 302 | } |
| 303 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 304 | if (proc->out_fds.empty()) { |
| 305 | fdevent_del(proc->fde, FDE_WRITE); |
| 306 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 309 | |
| 310 | return; |
| 311 | |
| 312 | CloseProcess: |
| 313 | proc->RemoveFromList(); |
| 314 | jdwp_process_list_updated(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 317 | int create_jdwp_connection_fd(int pid) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 318 | D("looking for pid %d in JDWP process list", pid); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 319 | |
| 320 | for (auto& proc : _jdwp_list) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | if (proc->pid == pid) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 322 | int fds[2]; |
| 323 | |
| 324 | if (adb_socketpair(fds) < 0) { |
| 325 | D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno)); |
| 326 | return -1; |
| 327 | } |
| 328 | D("socketpair: (%d,%d)", fds[0], fds[1]); |
| 329 | |
| 330 | proc->out_fds.emplace_back(fds[1]); |
| 331 | if (proc->out_fds.size() == 1) { |
| 332 | fdevent_add(proc->fde, FDE_WRITE); |
| 333 | } |
| 334 | |
| 335 | return fds[0]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 338 | D("search failed !!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 339 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** VM DEBUG CONTROL SOCKET |
| 343 | ** |
| 344 | ** we do implement a custom asocket to receive the data |
| 345 | **/ |
| 346 | |
| 347 | /* name of the debug control Unix socket */ |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 348 | #define JDWP_CONTROL_NAME "\0jdwp-control" |
| 349 | #define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME) - 1) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 350 | |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 351 | struct JdwpControl { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 352 | int listen_socket; |
| 353 | fdevent* fde; |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 354 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 356 | static JdwpControl _jdwp_control; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 358 | static void jdwp_control_event(int s, unsigned events, void* user); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 360 | static int jdwp_control_init(JdwpControl* control, const char* sockname, int socknamelen) { |
| 361 | sockaddr_un addr; |
| 362 | socklen_t addrlen; |
| 363 | int s; |
| 364 | int maxpath = sizeof(addr.sun_path); |
| 365 | int pathlen = socknamelen; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | |
| 367 | if (pathlen >= maxpath) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 368 | D("vm debug control socket name too long (%d extra chars)", pathlen + 1 - maxpath); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 369 | return -1; |
| 370 | } |
| 371 | |
| 372 | memset(&addr, 0, sizeof(addr)); |
| 373 | addr.sun_family = AF_UNIX; |
| 374 | memcpy(addr.sun_path, sockname, socknamelen); |
| 375 | |
Josh Gao | b2a7251 | 2017-03-20 13:37:13 -0700 | [diff] [blame] | 376 | s = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 377 | if (s < 0) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 378 | D("could not create vm debug control socket. %d: %s", errno, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 382 | addrlen = pathlen + sizeof(addr.sun_family); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 383 | |
Erik Kline | 7e16cc1 | 2015-12-01 17:27:59 +0900 | [diff] [blame] | 384 | if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) < 0) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 385 | D("could not bind vm debug control socket: %d: %s", errno, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | adb_close(s); |
| 387 | return -1; |
| 388 | } |
| 389 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 390 | if (listen(s, 4) < 0) { |
| 391 | D("listen failed in jdwp control socket: %d: %s", errno, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | adb_close(s); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | control->listen_socket = s; |
| 397 | |
| 398 | control->fde = fdevent_create(s, jdwp_control_event, control); |
| 399 | if (control->fde == NULL) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 400 | D("could not create fdevent for jdwp control socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 401 | adb_close(s); |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | /* only wait for incoming connections */ |
| 406 | fdevent_add(control->fde, FDE_READ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 408 | D("jdwp control socket started (%d)", control->listen_socket); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 412 | static void jdwp_control_event(int s, unsigned events, void* _control) { |
| 413 | JdwpControl* control = (JdwpControl*)_control; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 414 | |
| 415 | if (events & FDE_READ) { |
Josh Gao | 78e1eb1 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 416 | int s = adb_socket_accept(control->listen_socket, nullptr, nullptr); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 417 | if (s < 0) { |
| 418 | if (errno == ECONNABORTED) { |
| 419 | /* oops, the JDWP process died really quick */ |
| 420 | D("oops, the JDWP process died really quick"); |
| 421 | return; |
| 422 | } else { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | /* the socket is probably closed ? */ |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 424 | D("weird accept() failed on jdwp control socket: %s", strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 428 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 429 | auto proc = std::make_unique<JdwpProcess>(s); |
| 430 | if (!proc) { |
| 431 | fatal("failed to allocate JdwpProcess"); |
| 432 | } |
| 433 | |
| 434 | _jdwp_list.emplace_back(std::move(proc)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 438 | /** "jdwp" local service implementation |
| 439 | ** this simply returns the list of known JDWP process pids |
| 440 | **/ |
| 441 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 442 | struct JdwpSocket : public asocket { |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 443 | bool pass = false; |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 444 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 446 | static void jdwp_socket_close(asocket* s) { |
| 447 | D("LS(%d): closing jdwp socket", s->id); |
| 448 | |
| 449 | if (s->peer) { |
| 450 | D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
| 451 | s->peer->peer = nullptr; |
| 452 | s->peer->close(s->peer); |
| 453 | s->peer = nullptr; |
| 454 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | |
| 456 | remove_socket(s); |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 457 | delete s; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 460 | static int jdwp_socket_enqueue(asocket* s, std::string) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 461 | /* you can't write to this asocket */ |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 462 | D("LS(%d): JDWP socket received data?", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | s->peer->close(s->peer); |
| 464 | return -1; |
| 465 | } |
| 466 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 467 | static void jdwp_socket_ready(asocket* s) { |
| 468 | JdwpSocket* jdwp = (JdwpSocket*)s; |
| 469 | asocket* peer = jdwp->peer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 470 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 471 | /* on the first call, send the list of pids, |
| 472 | * on the second one, close the connection |
| 473 | */ |
| 474 | if (!jdwp->pass) { |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 475 | std::string data; |
| 476 | data.resize(s->get_max_payload()); |
| 477 | size_t len = jdwp_process_list(&data[0], data.size()); |
| 478 | data.resize(len); |
| 479 | peer->enqueue(peer, std::move(data)); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 480 | jdwp->pass = true; |
| 481 | } else { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | peer->close(peer); |
| 483 | } |
| 484 | } |
| 485 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 486 | asocket* create_jdwp_service_socket(void) { |
Josh Gao | e0361d1 | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 487 | JdwpSocket* s = new JdwpSocket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 489 | if (!s) { |
| 490 | fatal("failed to allocate JdwpSocket"); |
| 491 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 493 | install_local_socket(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 495 | s->ready = jdwp_socket_ready; |
| 496 | s->enqueue = jdwp_socket_enqueue; |
| 497 | s->close = jdwp_socket_close; |
| 498 | s->pass = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 500 | return s; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** "track-jdwp" local service implementation |
| 504 | ** this periodically sends the list of known JDWP process pids |
| 505 | ** to the client... |
| 506 | **/ |
| 507 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 508 | struct JdwpTracker : public asocket { |
| 509 | bool need_initial; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 | }; |
| 511 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 512 | static std::vector<std::unique_ptr<JdwpTracker>> _jdwp_trackers; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 513 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 514 | static void jdwp_process_list_updated(void) { |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 515 | std::string data; |
| 516 | data.resize(1024); |
| 517 | data.resize(jdwp_process_list_msg(&data[0], data.size())); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 518 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 519 | for (auto& t : _jdwp_trackers) { |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 520 | if (t->peer) { |
| 521 | // The tracker might not have been connected yet. |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 522 | t->peer->enqueue(t->peer, data); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 523 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 527 | static void jdwp_tracker_close(asocket* s) { |
| 528 | D("LS(%d): destroying jdwp tracker service", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 529 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 530 | if (s->peer) { |
| 531 | D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
| 532 | s->peer->peer = nullptr; |
| 533 | s->peer->close(s->peer); |
| 534 | s->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | remove_socket(s); |
| 538 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 539 | auto pred = [s](const auto& tracker) { return tracker.get() == s; }; |
Josh Gao | 6def9bd | 2017-03-10 11:19:48 -0800 | [diff] [blame] | 540 | _jdwp_trackers.erase(std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred), |
| 541 | _jdwp_trackers.end()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 544 | static void jdwp_tracker_ready(asocket* s) { |
| 545 | JdwpTracker* t = (JdwpTracker*)s; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 546 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 547 | if (t->need_initial) { |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 548 | std::string data; |
| 549 | data.resize(s->get_max_payload()); |
| 550 | data.resize(jdwp_process_list_msg(&data[0], data.size())); |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 551 | t->need_initial = false; |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 552 | s->peer->enqueue(s->peer, std::move(data)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Josh Gao | 27cb7dc | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 556 | static int jdwp_tracker_enqueue(asocket* s, std::string) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 557 | /* you can't write to this socket */ |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 558 | D("LS(%d): JDWP tracker received data?", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | s->peer->close(s->peer); |
| 560 | return -1; |
| 561 | } |
| 562 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 563 | asocket* create_jdwp_tracker_service_socket(void) { |
| 564 | auto t = std::make_unique<JdwpTracker>(); |
| 565 | if (!t) { |
| 566 | fatal("failed to allocate JdwpTracker"); |
| 567 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 568 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 569 | memset(t.get(), 0, sizeof(asocket)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 571 | install_local_socket(t.get()); |
| 572 | D("LS(%d): created new jdwp tracker service", t->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 574 | t->ready = jdwp_tracker_ready; |
| 575 | t->enqueue = jdwp_tracker_enqueue; |
| 576 | t->close = jdwp_tracker_close; |
| 577 | t->need_initial = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 578 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 579 | asocket* result = t.get(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 581 | _jdwp_trackers.emplace_back(std::move(t)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 582 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 583 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Josh Gao | 52338a3 | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 586 | int init_jdwp(void) { |
| 587 | return jdwp_control_init(&_jdwp_control, JDWP_CONTROL_NAME, JDWP_CONTROL_NAME_LEN); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | #endif /* !ADB_HOST */ |