Dan Albert | db6fe64 | 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 | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 17 | #if !ADB_HOST |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 19 | #define TRACE_TAG JDWP |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include "sysdeps.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <string.h> |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 27 | #include <sys/socket.h> |
| 28 | #include <sys/un.h> |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 31 | #include <list> |
| 32 | #include <memory> |
| 33 | #include <vector> |
| 34 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 35 | #include "adb.h" |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 36 | #include "adb_io.h" |
Josh Gao | ea7457b | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 37 | #include "adb_unique_fd.h" |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 38 | #include "adb_utils.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | 9ca14dc | 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 | 9eaf33c | 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 | 9ca14dc | 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 | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 47 | a connection to @jdwp-control to announce its availability. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
| 49 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 50 | JDWP thread @jdwp-control |
The Android Open Source Project | 9ca14dc | 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 | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 83 | JDWP thread @jdwp-control |
The Android Open Source Project | 9ca14dc | 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 | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 130 | struct JdwpProcess; |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame^] | 131 | static auto& _jdwp_list = *new std::list<std::unique_ptr<JdwpProcess>>(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | struct JdwpProcess { |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | |
Josh Gao | 9eaf33c | 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 | 03e4ab2 | 2018-02-12 15:05:05 -0800 | [diff] [blame] | 174 | int32_t pid = -1; |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | }; |
| 180 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 183 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 184 | for (auto& proc : _jdwp_list) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | /* skip transient connections */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 186 | if (proc->pid < 0) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | continue; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 188 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 189 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | } |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 195 | temp.append(next); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 196 | } |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 218 | |
| 219 | if (events & FDE_READ) { |
| 220 | if (proc->pid < 0) { |
Josh Gao | 03e4ab2 | 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 | 9ca14dc | 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 | 815ad88 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 229 | jdwp_process_list_updated(); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 230 | } else { |
The Android Open Source Project | 9ca14dc | 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 | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 233 | char buf[32]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 234 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | return; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 244 | } |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 254 | goto CloseProcess; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | if (events & FDE_WRITE) { |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 276 | msg.msg_controllen = sizeof(buffer); |
| 277 | |
| 278 | cmsg = CMSG_FIRSTHDR(&msg); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 279 | cmsg->cmsg_len = msg.msg_controllen; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | cmsg->cmsg_level = SOL_SOCKET; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 281 | cmsg->cmsg_type = SCM_RIGHTS; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 | ((int*)CMSG_DATA(cmsg))[0] = fd; |
| 283 | |
Yabin Cui | 5fc2231 | 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 | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 286 | goto CloseProcess; |
| 287 | } |
| 288 | |
Josh Gao | 9eaf33c | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 292 | goto CloseProcess; |
| 293 | } |
| 294 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 295 | adb_close(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 296 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 297 | D("sent file descriptor %d to JDWP process %d", fd, proc->pid); |
| 298 | |
| 299 | proc->out_fds.pop_back(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 | |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 301 | if (!set_file_block_mode(proc->socket, false)) { |
| 302 | VLOG(JDWP) << "failed to set non-blocking mode for fd " << proc->socket; |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 303 | goto CloseProcess; |
| 304 | } |
| 305 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 306 | if (proc->out_fds.empty()) { |
| 307 | fdevent_del(proc->fde, FDE_WRITE); |
| 308 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 311 | |
| 312 | return; |
| 313 | |
| 314 | CloseProcess: |
| 315 | proc->RemoveFromList(); |
| 316 | jdwp_process_list_updated(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 319 | int create_jdwp_connection_fd(int pid) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 320 | D("looking for pid %d in JDWP process list", pid); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 321 | |
| 322 | for (auto& proc : _jdwp_list) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 323 | if (proc->pid == pid) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 324 | int fds[2]; |
| 325 | |
| 326 | if (adb_socketpair(fds) < 0) { |
| 327 | D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno)); |
| 328 | return -1; |
| 329 | } |
| 330 | D("socketpair: (%d,%d)", fds[0], fds[1]); |
| 331 | |
| 332 | proc->out_fds.emplace_back(fds[1]); |
| 333 | if (proc->out_fds.size() == 1) { |
| 334 | fdevent_add(proc->fde, FDE_WRITE); |
| 335 | } |
| 336 | |
| 337 | return fds[0]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 338 | } |
| 339 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 340 | D("search failed !!"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** VM DEBUG CONTROL SOCKET |
| 345 | ** |
| 346 | ** we do implement a custom asocket to receive the data |
| 347 | **/ |
| 348 | |
| 349 | /* name of the debug control Unix socket */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 350 | #define JDWP_CONTROL_NAME "\0jdwp-control" |
| 351 | #define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME) - 1) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 353 | struct JdwpControl { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 354 | int listen_socket; |
| 355 | fdevent* fde; |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 356 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 358 | static JdwpControl _jdwp_control; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 360 | static void jdwp_control_event(int s, unsigned events, void* user); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 361 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 362 | static int jdwp_control_init(JdwpControl* control, const char* sockname, int socknamelen) { |
| 363 | sockaddr_un addr; |
| 364 | socklen_t addrlen; |
| 365 | int s; |
| 366 | int maxpath = sizeof(addr.sun_path); |
| 367 | int pathlen = socknamelen; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | |
| 369 | if (pathlen >= maxpath) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 370 | D("vm debug control socket name too long (%d extra chars)", pathlen + 1 - maxpath); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 371 | return -1; |
| 372 | } |
| 373 | |
| 374 | memset(&addr, 0, sizeof(addr)); |
| 375 | addr.sun_family = AF_UNIX; |
| 376 | memcpy(addr.sun_path, sockname, socknamelen); |
| 377 | |
Josh Gao | 6f3fea2 | 2017-03-20 13:37:13 -0700 | [diff] [blame] | 378 | s = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | if (s < 0) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 380 | D("could not create vm debug control socket. %d: %s", errno, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 384 | addrlen = pathlen + sizeof(addr.sun_family); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | |
Erik Kline | 3c8d44d | 2015-12-01 17:27:59 +0900 | [diff] [blame] | 386 | if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) < 0) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 387 | D("could not bind vm debug control socket: %d: %s", errno, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 388 | adb_close(s); |
| 389 | return -1; |
| 390 | } |
| 391 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 392 | if (listen(s, 4) < 0) { |
| 393 | D("listen failed in jdwp control socket: %d: %s", errno, strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 394 | adb_close(s); |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | control->listen_socket = s; |
| 399 | |
| 400 | control->fde = fdevent_create(s, jdwp_control_event, control); |
| 401 | if (control->fde == NULL) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 402 | D("could not create fdevent for jdwp control socket"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | adb_close(s); |
| 404 | return -1; |
| 405 | } |
| 406 | |
| 407 | /* only wait for incoming connections */ |
| 408 | fdevent_add(control->fde, FDE_READ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 409 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 410 | D("jdwp control socket started (%d)", control->listen_socket); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | return 0; |
| 412 | } |
| 413 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 414 | static void jdwp_control_event(int s, unsigned events, void* _control) { |
| 415 | JdwpControl* control = (JdwpControl*)_control; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 416 | |
| 417 | if (events & FDE_READ) { |
Josh Gao | 782d8d4 | 2016-08-23 15:41:56 -0700 | [diff] [blame] | 418 | int s = adb_socket_accept(control->listen_socket, nullptr, nullptr); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 419 | if (s < 0) { |
| 420 | if (errno == ECONNABORTED) { |
| 421 | /* oops, the JDWP process died really quick */ |
| 422 | D("oops, the JDWP process died really quick"); |
| 423 | return; |
| 424 | } else { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | /* the socket is probably closed ? */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 426 | D("weird accept() failed on jdwp control socket: %s", strerror(errno)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 430 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 431 | auto proc = std::make_unique<JdwpProcess>(s); |
| 432 | if (!proc) { |
| 433 | fatal("failed to allocate JdwpProcess"); |
| 434 | } |
| 435 | |
| 436 | _jdwp_list.emplace_back(std::move(proc)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 440 | /** "jdwp" local service implementation |
| 441 | ** this simply returns the list of known JDWP process pids |
| 442 | **/ |
| 443 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 444 | struct JdwpSocket : public asocket { |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 445 | bool pass = false; |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 446 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 447 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 448 | static void jdwp_socket_close(asocket* s) { |
| 449 | D("LS(%d): closing jdwp socket", s->id); |
| 450 | |
| 451 | if (s->peer) { |
| 452 | D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
| 453 | s->peer->peer = nullptr; |
| 454 | s->peer->close(s->peer); |
| 455 | s->peer = nullptr; |
| 456 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | |
| 458 | remove_socket(s); |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 459 | delete s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 462 | static int jdwp_socket_enqueue(asocket* s, std::string) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | /* you can't write to this asocket */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 464 | D("LS(%d): JDWP socket received data?", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 | s->peer->close(s->peer); |
| 466 | return -1; |
| 467 | } |
| 468 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 469 | static void jdwp_socket_ready(asocket* s) { |
| 470 | JdwpSocket* jdwp = (JdwpSocket*)s; |
| 471 | asocket* peer = jdwp->peer; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 473 | /* on the first call, send the list of pids, |
| 474 | * on the second one, close the connection |
| 475 | */ |
| 476 | if (!jdwp->pass) { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 477 | std::string data; |
| 478 | data.resize(s->get_max_payload()); |
| 479 | size_t len = jdwp_process_list(&data[0], data.size()); |
| 480 | data.resize(len); |
| 481 | peer->enqueue(peer, std::move(data)); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 482 | jdwp->pass = true; |
| 483 | } else { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | peer->close(peer); |
| 485 | } |
| 486 | } |
| 487 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 488 | asocket* create_jdwp_service_socket(void) { |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 489 | JdwpSocket* s = new JdwpSocket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 491 | if (!s) { |
| 492 | fatal("failed to allocate JdwpSocket"); |
| 493 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 495 | install_local_socket(s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 497 | s->ready = jdwp_socket_ready; |
| 498 | s->enqueue = jdwp_socket_enqueue; |
| 499 | s->close = jdwp_socket_close; |
| 500 | s->pass = false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 501 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 502 | return s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /** "track-jdwp" local service implementation |
| 506 | ** this periodically sends the list of known JDWP process pids |
| 507 | ** to the client... |
| 508 | **/ |
| 509 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 510 | struct JdwpTracker : public asocket { |
| 511 | bool need_initial; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 512 | }; |
| 513 | |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame^] | 514 | static auto& _jdwp_trackers = *new std::vector<std::unique_ptr<JdwpTracker>>(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 515 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 516 | static void jdwp_process_list_updated(void) { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 517 | std::string data; |
| 518 | data.resize(1024); |
| 519 | data.resize(jdwp_process_list_msg(&data[0], data.size())); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 520 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 521 | for (auto& t : _jdwp_trackers) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 522 | if (t->peer) { |
| 523 | // The tracker might not have been connected yet. |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 524 | t->peer->enqueue(t->peer, data); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 525 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 529 | static void jdwp_tracker_close(asocket* s) { |
| 530 | D("LS(%d): destroying jdwp tracker service", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 532 | if (s->peer) { |
| 533 | D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
| 534 | s->peer->peer = nullptr; |
| 535 | s->peer->close(s->peer); |
| 536 | s->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | remove_socket(s); |
| 540 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 541 | auto pred = [s](const auto& tracker) { return tracker.get() == s; }; |
Josh Gao | eea5fea | 2017-03-10 11:19:48 -0800 | [diff] [blame] | 542 | _jdwp_trackers.erase(std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred), |
| 543 | _jdwp_trackers.end()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 546 | static void jdwp_tracker_ready(asocket* s) { |
| 547 | JdwpTracker* t = (JdwpTracker*)s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 549 | if (t->need_initial) { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 550 | std::string data; |
| 551 | data.resize(s->get_max_payload()); |
| 552 | data.resize(jdwp_process_list_msg(&data[0], data.size())); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 553 | t->need_initial = false; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 554 | s->peer->enqueue(s->peer, std::move(data)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 558 | static int jdwp_tracker_enqueue(asocket* s, std::string) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | /* you can't write to this socket */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 560 | D("LS(%d): JDWP tracker received data?", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 | s->peer->close(s->peer); |
| 562 | return -1; |
| 563 | } |
| 564 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 565 | asocket* create_jdwp_tracker_service_socket(void) { |
| 566 | auto t = std::make_unique<JdwpTracker>(); |
| 567 | if (!t) { |
| 568 | fatal("failed to allocate JdwpTracker"); |
| 569 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 571 | memset(t.get(), 0, sizeof(asocket)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 572 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 573 | install_local_socket(t.get()); |
| 574 | D("LS(%d): created new jdwp tracker service", t->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 575 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 576 | t->ready = jdwp_tracker_ready; |
| 577 | t->enqueue = jdwp_tracker_enqueue; |
| 578 | t->close = jdwp_tracker_close; |
| 579 | t->need_initial = true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 581 | asocket* result = t.get(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 582 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 583 | _jdwp_trackers.emplace_back(std::move(t)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 584 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 585 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 588 | int init_jdwp(void) { |
| 589 | return jdwp_control_init(&_jdwp_control, JDWP_CONTROL_NAME, JDWP_CONTROL_NAME_LEN); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | #endif /* !ADB_HOST */ |