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