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> |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 24 | #include <inttypes.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
Elliott Hughes | 43df109 | 2015-07-23 17:12:58 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <string.h> |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 28 | #include <sys/socket.h> |
| 29 | #include <sys/un.h> |
Teddie Stenvi | f56e7f5 | 2010-02-15 12:20:44 +0100 | [diff] [blame] | 30 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 32 | #include <list> |
| 33 | #include <memory> |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 34 | #include <thread> |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 37 | #include <adbconnection/process_info.h> |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 38 | #include <adbconnection/server.h> |
Josh Gao | 2d83b54 | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 39 | #include <android-base/cmsg.h> |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
Josh Gao | 2d83b54 | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 41 | |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 42 | #include "adb.h" |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 43 | #include "adb_io.h" |
Josh Gao | ea7457b | 2016-08-30 15:39:25 -0700 | [diff] [blame] | 44 | #include "adb_unique_fd.h" |
Yabin Cui | 5fc2231 | 2015-10-06 15:10:05 -0700 | [diff] [blame] | 45 | #include "adb_utils.h" |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 46 | #include "app_processes.pb.h" |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 47 | |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 48 | using android::base::borrowed_fd; |
| 49 | using android::base::unique_fd; |
| 50 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | /* here's how these things work. |
| 52 | |
| 53 | when adbd starts, it creates a unix server socket |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 54 | 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] | 55 | to use the private namespace instead of the file system) |
| 56 | |
| 57 | when a new JDWP daemon thread starts in a new VM process, it creates |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 58 | a connection to @jdwp-control to announce its availability. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 59 | |
| 60 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 61 | JDWP thread @jdwp-control |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | | | |
| 63 | |-------------------------------> | |
| 64 | | hello I'm in process <pid> | |
| 65 | | | |
| 66 | | | |
| 67 | |
| 68 | the connection is kept alive. it will be closed automatically if |
| 69 | the JDWP process terminates (this allows adbd to detect dead |
| 70 | processes). |
| 71 | |
| 72 | adbd thus maintains a list of "active" JDWP processes. it can send |
| 73 | its content to clients through the "device:debug-ports" service, |
| 74 | or even updates through the "device:track-debug-ports" service. |
| 75 | |
| 76 | when a debugger wants to connect, it simply runs the command |
| 77 | equivalent to "adb forward tcp:<hostport> jdwp:<pid>" |
| 78 | |
| 79 | "jdwp:<pid>" is a new forward destination format used to target |
| 80 | a given JDWP process on the device. when sutch a request arrives, |
| 81 | adbd does the following: |
| 82 | |
| 83 | - first, it calls socketpair() to create a pair of equivalent |
| 84 | sockets. |
| 85 | |
| 86 | - it attaches the first socket in the pair to a local socket |
| 87 | which is itself attached to the transport's remote socket: |
| 88 | |
| 89 | |
| 90 | - it sends the file descriptor of the second socket directly |
| 91 | to the JDWP process with the help of sendmsg() |
| 92 | |
| 93 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 94 | JDWP thread @jdwp-control |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | | | |
| 96 | | <----------------------| |
| 97 | | OK, try this file descriptor | |
| 98 | | | |
| 99 | | | |
| 100 | |
| 101 | then, the JDWP thread uses this new socket descriptor as its |
| 102 | pass-through connection to the debugger (and receives the |
| 103 | JDWP-Handshake message, answers to it, etc...) |
| 104 | |
| 105 | this gives the following graphics: |
| 106 | ____________________________________ |
| 107 | | | |
| 108 | | ADB Server (host) | |
| 109 | | | |
| 110 | Debugger <---> LocalSocket <----> RemoteSocket | |
| 111 | | ^^ | |
| 112 | |___________________________||_______| |
| 113 | || |
| 114 | Transport || |
| 115 | (TCP for emulator - USB for device) || |
| 116 | || |
| 117 | ___________________________||_______ |
| 118 | | || | |
| 119 | | ADBD (device) || | |
| 120 | | VV | |
| 121 | JDWP <======> LocalSocket <----> RemoteSocket | |
| 122 | | | |
| 123 | |____________________________________| |
| 124 | |
| 125 | due to the way adb works, this doesn't need a special socket |
| 126 | type or fancy handling of socket termination if either the debugger |
| 127 | or the JDWP process closes the connection. |
| 128 | |
| 129 | THIS IS THE SIMPLEST IMPLEMENTATION I COULD FIND, IF YOU HAPPEN |
| 130 | TO HAVE A BETTER IDEA, LET ME KNOW - Digit |
| 131 | |
| 132 | **********************************************************************/ |
| 133 | |
| 134 | /** JDWP PID List Support Code |
| 135 | ** for each JDWP process, we record its pid and its connected socket |
| 136 | **/ |
| 137 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 138 | enum class TrackerKind { |
| 139 | kJdwp, |
| 140 | kApp, |
| 141 | }; |
| 142 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 143 | static void jdwp_process_event(int socket, unsigned events, void* _proc); |
| 144 | static void jdwp_process_list_updated(void); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 145 | static void app_process_list_updated(void); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 147 | struct JdwpProcess; |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 148 | 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] | 149 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | struct JdwpProcess { |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 151 | JdwpProcess(unique_fd socket, ProcessInfo process) { |
| 152 | CHECK(process.pid != 0); |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 153 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 154 | this->socket = socket; |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 155 | this->process = process; |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 156 | this->fde = fdevent_create(socket.release(), jdwp_process_event, this); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 157 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 158 | if (!this->fde) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 159 | LOG(FATAL) << "could not create fdevent for new JDWP process"; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 160 | } |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ~JdwpProcess() { |
| 164 | if (this->socket >= 0) { |
| 165 | adb_shutdown(this->socket); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 166 | this->socket = -1; |
| 167 | } |
| 168 | |
| 169 | if (this->fde) { |
| 170 | fdevent_destroy(this->fde); |
| 171 | this->fde = nullptr; |
| 172 | } |
| 173 | |
| 174 | out_fds.clear(); |
| 175 | } |
| 176 | |
| 177 | void RemoveFromList() { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 178 | auto pred = [this](const auto& proc) { return proc.get() == this; }; |
| 179 | _jdwp_list.remove_if(pred); |
| 180 | } |
| 181 | |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 182 | borrowed_fd socket = -1; |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 183 | ProcessInfo process; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 184 | fdevent* fde = nullptr; |
| 185 | |
| 186 | std::vector<unique_fd> out_fds; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | }; |
| 188 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 189 | // Populate the list of processes for "track-jdwp" service. |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 190 | static size_t jdwp_process_list(char* buffer, size_t bufferlen) { |
| 191 | std::string temp; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 192 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 193 | for (auto& proc : _jdwp_list) { |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 194 | if (!proc->process.debuggable) continue; |
| 195 | std::string next = std::to_string(proc->process.pid) + "\n"; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 196 | if (temp.length() + next.length() > bufferlen) { |
| 197 | D("truncating JDWP process list (max len = %zu)", bufferlen); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 199 | } |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 200 | temp.append(next); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | } |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 202 | |
| 203 | memcpy(buffer, temp.data(), temp.length()); |
| 204 | return temp.length(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 207 | // Populate the list of processes for "track-app" service. |
| 208 | // The list is a protobuf message in the binary format for efficiency. |
| 209 | static size_t app_process_list(char* buffer, size_t bufferlen) { |
| 210 | adb::proto::AppProcesses output; // result that's guaranteed to fit in the given buffer |
| 211 | adb::proto::AppProcesses temp; // temporary result that may be longer than the given buffer |
| 212 | std::string serialized_message; |
| 213 | |
| 214 | for (auto& proc : _jdwp_list) { |
| 215 | if (!proc->process.debuggable && !proc->process.profileable) continue; |
| 216 | auto* entry = temp.add_process(); |
| 217 | entry->set_pid(proc->process.pid); |
| 218 | entry->set_debuggable(proc->process.debuggable); |
| 219 | entry->set_profileable(proc->process.profileable); |
| 220 | entry->set_architecture(proc->process.arch_name, proc->process.arch_name_length); |
| 221 | temp.SerializeToString(&serialized_message); |
| 222 | if (serialized_message.size() > bufferlen) { |
| 223 | D("truncating app process list (max len = %zu)", bufferlen); |
| 224 | break; |
| 225 | } |
| 226 | output = temp; |
| 227 | } |
| 228 | output.SerializeToString(&serialized_message); |
| 229 | memcpy(buffer, serialized_message.data(), serialized_message.length()); |
| 230 | return serialized_message.length(); |
| 231 | } |
| 232 | |
| 233 | // Populate the list of processes for either "track-jdwp" or "track-app" services, |
| 234 | // depending on the given kind. |
| 235 | static size_t process_list(TrackerKind kind, char* buffer, size_t bufferlen) { |
| 236 | switch (kind) { |
| 237 | case TrackerKind::kJdwp: |
| 238 | return jdwp_process_list(buffer, bufferlen); |
| 239 | case TrackerKind::kApp: |
| 240 | return app_process_list(buffer, bufferlen); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static size_t process_list_msg(TrackerKind kind, char* buffer, size_t bufferlen) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 245 | // Message is length-prefixed with 4 hex digits in ASCII. |
| 246 | static constexpr size_t header_len = 4; |
| 247 | if (bufferlen < header_len) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 248 | LOG(FATAL) << "invalid JDWP process list buffer size: " << bufferlen; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 251 | char head[header_len + 1]; |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 252 | size_t len = process_list(kind, buffer + header_len, bufferlen - header_len); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 253 | snprintf(head, sizeof head, "%04zx", len); |
| 254 | memcpy(buffer, head, header_len); |
| 255 | return len + header_len; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 258 | static void jdwp_process_event(int socket, unsigned events, void* _proc) { |
| 259 | JdwpProcess* proc = reinterpret_cast<JdwpProcess*>(_proc); |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 260 | CHECK_EQ(socket, proc->socket.get()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | |
| 262 | if (events & FDE_READ) { |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 263 | // We already have the PID, if we can read from the socket, we've probably hit EOF. |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 264 | D("terminating JDWP connection %" PRId64, proc->process.pid); |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 265 | goto CloseProcess; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (events & FDE_WRITE) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 269 | D("trying to send fd to JDWP process (count = %zu)", proc->out_fds.size()); |
Josh Gao | cc1dcc8 | 2018-10-05 17:09:41 -0700 | [diff] [blame] | 270 | CHECK(!proc->out_fds.empty()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | |
Josh Gao | cc1dcc8 | 2018-10-05 17:09:41 -0700 | [diff] [blame] | 272 | int fd = proc->out_fds.back().get(); |
Josh Gao | 2d83b54 | 2019-01-10 14:29:29 -0800 | [diff] [blame] | 273 | if (android::base::SendFileDescriptors(socket, "", 1, fd) != 1) { |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 274 | D("sending new file descriptor to JDWP %" PRId64 " failed: %s", proc->process.pid, |
| 275 | strerror(errno)); |
Josh Gao | cc1dcc8 | 2018-10-05 17:09:41 -0700 | [diff] [blame] | 276 | goto CloseProcess; |
| 277 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 279 | D("sent file descriptor %d to JDWP process %" PRId64, fd, proc->process.pid); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 280 | |
Josh Gao | cc1dcc8 | 2018-10-05 17:09:41 -0700 | [diff] [blame] | 281 | proc->out_fds.pop_back(); |
| 282 | if (proc->out_fds.empty()) { |
| 283 | fdevent_del(proc->fde, FDE_WRITE); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 286 | |
| 287 | return; |
| 288 | |
| 289 | CloseProcess: |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 290 | bool debuggable = proc->process.debuggable; |
| 291 | bool profileable = proc->process.profileable; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 292 | proc->RemoveFromList(); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 293 | if (debuggable) jdwp_process_list_updated(); |
| 294 | if (debuggable || profileable) app_process_list_updated(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 297 | unique_fd create_jdwp_connection_fd(int pid) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 298 | D("looking for pid %d in JDWP process list", pid); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 299 | |
| 300 | for (auto& proc : _jdwp_list) { |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 301 | // Don't allow JDWP connection to a non-debuggable process. |
| 302 | if (!proc->process.debuggable) continue; |
| 303 | if (proc->process.pid == static_cast<uint64_t>(pid)) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 304 | int fds[2]; |
| 305 | |
| 306 | if (adb_socketpair(fds) < 0) { |
| 307 | D("%s: socket pair creation failed: %s", __FUNCTION__, strerror(errno)); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 308 | return unique_fd{}; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 309 | } |
| 310 | D("socketpair: (%d,%d)", fds[0], fds[1]); |
| 311 | |
| 312 | proc->out_fds.emplace_back(fds[1]); |
| 313 | if (proc->out_fds.size() == 1) { |
| 314 | fdevent_add(proc->fde, FDE_WRITE); |
| 315 | } |
| 316 | |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 317 | return unique_fd{fds[0]}; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 320 | D("search failed !!"); |
Josh Gao | 5607e92 | 2018-07-25 18:15:52 -0700 | [diff] [blame] | 321 | return unique_fd{}; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | } |
| 323 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | /** "jdwp" local service implementation |
| 325 | ** this simply returns the list of known JDWP process pids |
| 326 | **/ |
| 327 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 328 | struct JdwpSocket : public asocket { |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 329 | bool pass = false; |
Elliott Hughes | fe7ff81 | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 330 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 332 | static void jdwp_socket_close(asocket* s) { |
| 333 | D("LS(%d): closing jdwp socket", s->id); |
| 334 | |
| 335 | if (s->peer) { |
| 336 | D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
| 337 | s->peer->peer = nullptr; |
| 338 | s->peer->close(s->peer); |
| 339 | s->peer = nullptr; |
| 340 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | |
| 342 | remove_socket(s); |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 343 | delete s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 346 | static int jdwp_socket_enqueue(asocket* s, apacket::payload_type) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 347 | /* you can't write to this asocket */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 348 | D("LS(%d): JDWP socket received data?", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 349 | s->peer->close(s->peer); |
| 350 | return -1; |
| 351 | } |
| 352 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 353 | static void jdwp_socket_ready(asocket* s) { |
| 354 | JdwpSocket* jdwp = (JdwpSocket*)s; |
| 355 | asocket* peer = jdwp->peer; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 357 | /* on the first call, send the list of pids, |
| 358 | * on the second one, close the connection |
| 359 | */ |
| 360 | if (!jdwp->pass) { |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 361 | apacket::payload_type data; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 362 | data.resize(s->get_max_payload()); |
| 363 | size_t len = jdwp_process_list(&data[0], data.size()); |
| 364 | data.resize(len); |
| 365 | peer->enqueue(peer, std::move(data)); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 366 | jdwp->pass = true; |
| 367 | } else { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | peer->close(peer); |
| 369 | } |
| 370 | } |
| 371 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 372 | asocket* create_jdwp_service_socket(void) { |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 373 | JdwpSocket* s = new JdwpSocket(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 375 | if (!s) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 376 | LOG(FATAL) << "failed to allocate JdwpSocket"; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 377 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 379 | install_local_socket(s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 380 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 381 | s->ready = jdwp_socket_ready; |
| 382 | s->enqueue = jdwp_socket_enqueue; |
| 383 | s->close = jdwp_socket_close; |
| 384 | s->pass = false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 386 | return s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** "track-jdwp" local service implementation |
| 390 | ** this periodically sends the list of known JDWP process pids |
| 391 | ** to the client... |
| 392 | **/ |
| 393 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 394 | struct JdwpTracker : public asocket { |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 395 | TrackerKind kind; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 396 | bool need_initial; |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 397 | |
| 398 | explicit JdwpTracker(TrackerKind k, bool initial) : kind(k), need_initial(initial) {} |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 399 | }; |
| 400 | |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 401 | 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] | 402 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 403 | static void process_list_updated(TrackerKind kind) { |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 404 | std::string data; |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 405 | const int kMaxLength = kind == TrackerKind::kJdwp ? 1024 : 2048; |
| 406 | data.resize(kMaxLength); |
| 407 | data.resize(process_list_msg(kind, &data[0], data.size())); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 409 | for (auto& t : _jdwp_trackers) { |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 410 | if (t->kind == kind && t->peer) { |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 411 | // The tracker might not have been connected yet. |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 412 | apacket::payload_type payload(data.begin(), data.end()); |
| 413 | t->peer->enqueue(t->peer, std::move(payload)); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 414 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 418 | static void jdwp_process_list_updated(void) { |
| 419 | process_list_updated(TrackerKind::kJdwp); |
| 420 | } |
| 421 | |
| 422 | static void app_process_list_updated(void) { |
| 423 | process_list_updated(TrackerKind::kApp); |
| 424 | } |
| 425 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 426 | static void jdwp_tracker_close(asocket* s) { |
| 427 | D("LS(%d): destroying jdwp tracker service", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 428 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 429 | if (s->peer) { |
| 430 | D("LS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
| 431 | s->peer->peer = nullptr; |
| 432 | s->peer->close(s->peer); |
| 433 | s->peer = nullptr; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | remove_socket(s); |
| 437 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 438 | auto pred = [s](const auto& tracker) { return tracker.get() == s; }; |
Josh Gao | eea5fea | 2017-03-10 11:19:48 -0800 | [diff] [blame] | 439 | _jdwp_trackers.erase(std::remove_if(_jdwp_trackers.begin(), _jdwp_trackers.end(), pred), |
| 440 | _jdwp_trackers.end()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 443 | static void jdwp_tracker_ready(asocket* s) { |
| 444 | JdwpTracker* t = (JdwpTracker*)s; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 446 | if (t->need_initial) { |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 447 | apacket::payload_type data; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 448 | data.resize(s->get_max_payload()); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 449 | data.resize(process_list_msg(t->kind, &data[0], data.size())); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 450 | t->need_initial = false; |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 451 | s->peer->enqueue(s->peer, std::move(data)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 455 | static int jdwp_tracker_enqueue(asocket* s, apacket::payload_type) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 456 | /* you can't write to this socket */ |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 457 | D("LS(%d): JDWP tracker received data?", s->id); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 458 | s->peer->close(s->peer); |
| 459 | return -1; |
| 460 | } |
| 461 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 462 | asocket* create_process_tracker_service_socket(TrackerKind kind) { |
| 463 | auto t = std::make_unique<JdwpTracker>(kind, true); |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 464 | if (!t) { |
Elliott Hughes | e64126b | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 465 | LOG(FATAL) << "failed to allocate JdwpTracker"; |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 466 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 467 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 468 | memset(t.get(), 0, sizeof(asocket)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 469 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 470 | install_local_socket(t.get()); |
| 471 | 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] | 472 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 473 | t->ready = jdwp_tracker_ready; |
| 474 | t->enqueue = jdwp_tracker_enqueue; |
| 475 | t->close = jdwp_tracker_close; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 476 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 477 | asocket* result = t.get(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 478 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 479 | _jdwp_trackers.emplace_back(std::move(t)); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 480 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 481 | return result; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 482 | } |
| 483 | |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 484 | asocket* create_jdwp_tracker_service_socket() { |
| 485 | return create_process_tracker_service_socket(TrackerKind::kJdwp); |
| 486 | } |
| 487 | |
| 488 | asocket* create_app_tracker_service_socket() { |
| 489 | return create_process_tracker_service_socket(TrackerKind::kApp); |
| 490 | } |
| 491 | |
Josh Gao | 9eaf33c | 2016-05-13 15:28:34 -0700 | [diff] [blame] | 492 | int init_jdwp(void) { |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 493 | std::thread([]() { |
| 494 | adb_thread_setname("jdwp control"); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 495 | adbconnection_listen([](int fd, ProcessInfo process) { |
| 496 | LOG(INFO) << "jdwp connection from " << process.pid; |
| 497 | fdevent_run_on_main_thread([fd, process] { |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 498 | unique_fd ufd(fd); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 499 | auto proc = std::make_unique<JdwpProcess>(std::move(ufd), process); |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 500 | if (!proc) { |
| 501 | LOG(FATAL) << "failed to allocate JdwpProcess"; |
| 502 | } |
| 503 | _jdwp_list.emplace_back(std::move(proc)); |
Shukang Zhou | 420ad55 | 2020-02-13 17:01:39 -0800 | [diff] [blame^] | 504 | if (process.debuggable) jdwp_process_list_updated(); |
| 505 | if (process.debuggable || process.profileable) app_process_list_updated(); |
Josh Gao | eb5e605 | 2019-06-19 14:14:57 -0700 | [diff] [blame] | 506 | }); |
| 507 | }); |
| 508 | }).detach(); |
| 509 | return 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | #endif /* !ADB_HOST */ |