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