Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #ifndef ART_ADBCONNECTION_ADBCONNECTION_H_ |
| 18 | #define ART_ADBCONNECTION_ADBCONNECTION_H_ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <vector> |
| 22 | #include <limits> |
| 23 | |
| 24 | #include "android-base/unique_fd.h" |
| 25 | |
| 26 | #include "base/mutex.h" |
Alex Light | 15b8113 | 2018-01-24 13:29:07 -0800 | [diff] [blame] | 27 | #include "base/array_ref.h" |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 28 | #include "runtime_callbacks.h" |
| 29 | |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/un.h> |
| 32 | #include <jni.h> |
| 33 | |
| 34 | namespace adbconnection { |
| 35 | |
| 36 | static constexpr char kJdwpControlName[] = "\0jdwp-control"; |
| 37 | static constexpr char kAdbConnectionThreadName[] = "ADB-JDWP Connection Control Thread"; |
| 38 | |
| 39 | // The default jdwp agent name. |
| 40 | static constexpr char kDefaultJdwpAgentName[] = "libjdwp.so"; |
| 41 | |
| 42 | class AdbConnectionState; |
| 43 | |
| 44 | struct AdbConnectionDebuggerController : public art::DebuggerControlCallback { |
| 45 | explicit AdbConnectionDebuggerController(AdbConnectionState* connection) |
| 46 | : connection_(connection) {} |
| 47 | |
| 48 | // Begin running the debugger. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 49 | void StartDebugger() override; |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 50 | |
| 51 | // The debugger should begin shutting down since the runtime is ending. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 52 | void StopDebugger() override; |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 53 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 54 | bool IsDebuggerConfigured() override; |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | AdbConnectionState* connection_; |
| 58 | }; |
| 59 | |
Alex Light | 15b8113 | 2018-01-24 13:29:07 -0800 | [diff] [blame] | 60 | enum class DdmPacketType : uint8_t { kReply = 0x80, kCmd = 0x00, }; |
| 61 | |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 62 | struct AdbConnectionDdmCallback : public art::DdmCallback { |
| 63 | explicit AdbConnectionDdmCallback(AdbConnectionState* connection) : connection_(connection) {} |
| 64 | |
| 65 | void DdmPublishChunk(uint32_t type, |
| 66 | const art::ArrayRef<const uint8_t>& data) |
| 67 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 68 | |
| 69 | private: |
| 70 | AdbConnectionState* connection_; |
| 71 | }; |
| 72 | |
| 73 | class AdbConnectionState { |
| 74 | public: |
Alex Light | d6f9d85 | 2018-01-25 11:26:28 -0800 | [diff] [blame] | 75 | explicit AdbConnectionState(const std::string& name); |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 76 | |
| 77 | // Called on the listening thread to start dealing with new input. thr is used to attach the new |
| 78 | // thread to the runtime. |
| 79 | void RunPollLoop(art::Thread* self); |
| 80 | |
| 81 | // Sends ddms data over the socket, if there is one. This data is sent even if we haven't finished |
| 82 | // hand-shaking yet. |
| 83 | void PublishDdmData(uint32_t type, const art::ArrayRef<const uint8_t>& data); |
| 84 | |
| 85 | // Stops debugger threads during shutdown. |
| 86 | void StopDebuggerThreads(); |
| 87 | |
Alex Light | d6f9d85 | 2018-01-25 11:26:28 -0800 | [diff] [blame] | 88 | // If StartDebuggerThreads was called successfully. |
| 89 | bool DebuggerThreadsStarted() { |
| 90 | return started_debugger_threads_; |
| 91 | } |
| 92 | |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 93 | private: |
| 94 | uint32_t NextDdmId(); |
| 95 | |
| 96 | void StartDebuggerThreads(); |
| 97 | |
| 98 | // Tell adbd about the new runtime. |
| 99 | bool SetupAdbConnection(); |
| 100 | |
| 101 | std::string MakeAgentArg(); |
| 102 | |
| 103 | android::base::unique_fd ReadFdFromAdb(); |
| 104 | |
Alex Light | 15b8113 | 2018-01-24 13:29:07 -0800 | [diff] [blame] | 105 | void SendAgentFds(bool require_handshake); |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 106 | |
| 107 | void CloseFds(); |
| 108 | |
Alex Light | 15b8113 | 2018-01-24 13:29:07 -0800 | [diff] [blame] | 109 | void HandleDataWithoutAgent(art::Thread* self); |
| 110 | |
| 111 | void PerformHandshake(); |
| 112 | |
| 113 | void AttachJdwpAgent(art::Thread* self); |
| 114 | |
| 115 | void NotifyDdms(bool active); |
| 116 | |
| 117 | void SendDdmPacket(uint32_t id, |
| 118 | DdmPacketType type, |
| 119 | uint32_t ddm_type, |
| 120 | art::ArrayRef<const uint8_t> data); |
| 121 | |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 122 | std::string agent_name_; |
| 123 | |
| 124 | AdbConnectionDebuggerController controller_; |
| 125 | AdbConnectionDdmCallback ddm_callback_; |
| 126 | |
| 127 | // Eventfd used to allow the StopDebuggerThreads function to wake up sleeping threads |
| 128 | android::base::unique_fd sleep_event_fd_; |
| 129 | |
| 130 | // Socket that we use to talk to adbd. |
| 131 | android::base::unique_fd control_sock_; |
| 132 | |
| 133 | // Socket that we use to talk to the agent (if it's loaded). |
| 134 | android::base::unique_fd local_agent_control_sock_; |
| 135 | |
| 136 | // The fd of the socket the agent uses to talk to us. We need to keep it around in order to clean |
| 137 | // it up when the runtime goes away. |
| 138 | android::base::unique_fd remote_agent_control_sock_; |
| 139 | |
| 140 | // The fd that is forwarded through adb to the client. This is guarded by the |
| 141 | // adb_write_event_fd_. |
| 142 | android::base::unique_fd adb_connection_socket_; |
| 143 | |
| 144 | // The fd we send to the agent to let us synchronize access to the shared adb_connection_socket_. |
| 145 | // This is also used as a general lock for the adb_connection_socket_ on any threads other than |
| 146 | // the poll thread. |
| 147 | android::base::unique_fd adb_write_event_fd_; |
| 148 | |
| 149 | std::atomic<bool> shutting_down_; |
| 150 | |
| 151 | // True if we have loaded the agent library. |
| 152 | std::atomic<bool> agent_loaded_; |
| 153 | |
| 154 | // True if the dt_fd_forward transport is listening for a new communication channel. |
| 155 | std::atomic<bool> agent_listening_; |
| 156 | |
| 157 | // True if the dt_fd_forward transport has the socket. If so we don't do anything to the agent or |
| 158 | // the adb connection socket until connection goes away. |
| 159 | std::atomic<bool> agent_has_socket_; |
| 160 | |
| 161 | std::atomic<bool> sent_agent_fds_; |
| 162 | |
Alex Light | 15b8113 | 2018-01-24 13:29:07 -0800 | [diff] [blame] | 163 | bool performed_handshake_; |
| 164 | |
| 165 | bool notified_ddm_active_; |
| 166 | |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 167 | std::atomic<uint32_t> next_ddm_id_; |
| 168 | |
Alex Light | d6f9d85 | 2018-01-25 11:26:28 -0800 | [diff] [blame] | 169 | bool started_debugger_threads_; |
| 170 | |
Alex Light | fbf9670 | 2017-12-14 13:27:13 -0800 | [diff] [blame] | 171 | socklen_t control_addr_len_; |
| 172 | union { |
| 173 | sockaddr_un controlAddrUn; |
| 174 | sockaddr controlAddrPlain; |
| 175 | } control_addr_; |
| 176 | |
| 177 | friend struct AdbConnectionDebuggerController; |
| 178 | }; |
| 179 | |
| 180 | } // namespace adbconnection |
| 181 | |
| 182 | #endif // ART_ADBCONNECTION_ADBCONNECTION_H_ |