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