xref: /aosp_15_r20/art/adbconnection/adbconnection.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_ADBCONNECTION_ADBCONNECTION_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_ADBCONNECTION_ADBCONNECTION_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <jni.h>
21*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
22*795d594fSAndroid Build Coastguard Worker #include <sys/socket.h>
23*795d594fSAndroid Build Coastguard Worker #include <sys/un.h>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include <limits>
26*795d594fSAndroid Build Coastguard Worker #include <memory>
27*795d594fSAndroid Build Coastguard Worker #include <string>
28*795d594fSAndroid Build Coastguard Worker #include <vector>
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker #include "adbconnection/client.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/array_ref.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
33*795d594fSAndroid Build Coastguard Worker #include "runtime_callbacks.h"
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker namespace adbconnection {
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker static constexpr char kAdbConnectionThreadName[] = "ADB-JDWP Connection Control Thread";
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker // The default jdwp agent name.
40*795d594fSAndroid Build Coastguard Worker static constexpr char kDefaultJdwpAgentName[] = "libjdwp.so";
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker class AdbConnectionState;
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker struct AdbConnectionDebuggerController : public art::DebuggerControlCallback {
AdbConnectionDebuggerControllerAdbConnectionDebuggerController45*795d594fSAndroid Build Coastguard Worker   explicit AdbConnectionDebuggerController(AdbConnectionState* connection)
46*795d594fSAndroid Build Coastguard Worker       : connection_(connection) {}
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker   // Begin running the debugger.
49*795d594fSAndroid Build Coastguard Worker   void StartDebugger() override;
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker   // The debugger should begin shutting down since the runtime is ending.
52*795d594fSAndroid Build Coastguard Worker   void StopDebugger() override;
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   bool IsDebuggerConfigured() override;
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker  private:
57*795d594fSAndroid Build Coastguard Worker   AdbConnectionState* connection_;
58*795d594fSAndroid Build Coastguard Worker };
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker enum class DdmPacketType : uint8_t { kReply = 0x80, kCmd = 0x00, };
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker struct AdbConnectionDdmCallback : public art::DdmCallback {
AdbConnectionDdmCallbackAdbConnectionDdmCallback63*795d594fSAndroid Build Coastguard Worker   explicit AdbConnectionDdmCallback(AdbConnectionState* connection) : connection_(connection) {}
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   void DdmPublishChunk(uint32_t type,
66*795d594fSAndroid Build Coastguard Worker                        const art::ArrayRef<const uint8_t>& data)
67*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_);
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker  private:
70*795d594fSAndroid Build Coastguard Worker   AdbConnectionState* connection_;
71*795d594fSAndroid Build Coastguard Worker };
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker struct AdbConnectionAppInfoCallback : public art::AppInfoCallback {
AdbConnectionAppInfoCallbackAdbConnectionAppInfoCallback74*795d594fSAndroid Build Coastguard Worker   explicit AdbConnectionAppInfoCallback(AdbConnectionState* connection) : connection_(connection) {}
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   void AddApplication(const std::string& package_name) REQUIRES_SHARED(art::Locks::mutator_lock_);
77*795d594fSAndroid Build Coastguard Worker   void RemoveApplication(const std::string& package_name)
78*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_);
79*795d594fSAndroid Build Coastguard Worker   void SetCurrentProcessName(const std::string& process_name)
80*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_);
81*795d594fSAndroid Build Coastguard Worker   void SetWaitingForDebugger(bool waiting) REQUIRES_SHARED(art::Locks::mutator_lock_);
82*795d594fSAndroid Build Coastguard Worker   void SetUserId(int uid) REQUIRES_SHARED(art::Locks::mutator_lock_);
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker  private:
85*795d594fSAndroid Build Coastguard Worker   AdbConnectionState* connection_;
86*795d594fSAndroid Build Coastguard Worker };
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker class AdbConnectionState {
89*795d594fSAndroid Build Coastguard Worker  public:
90*795d594fSAndroid Build Coastguard Worker   explicit AdbConnectionState(const std::string& name);
91*795d594fSAndroid Build Coastguard Worker   ~AdbConnectionState();
92*795d594fSAndroid Build Coastguard Worker 
93*795d594fSAndroid Build Coastguard Worker   // Called on the listening thread to start dealing with new input. thr is used to attach the new
94*795d594fSAndroid Build Coastguard Worker   // thread to the runtime.
95*795d594fSAndroid Build Coastguard Worker   void RunPollLoop(art::Thread* self);
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker   // Sends ddms data over the socket, if there is one. This data is sent even if we haven't finished
98*795d594fSAndroid Build Coastguard Worker   // hand-shaking yet.
99*795d594fSAndroid Build Coastguard Worker   void PublishDdmData(uint32_t type, const art::ArrayRef<const uint8_t>& data);
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker   // Wake up the poll. Call this if the set of interesting event has changed. They will be
102*795d594fSAndroid Build Coastguard Worker   // recalculated and poll will be called again via fdevent write. This wakeup  relies on fdevent
103*795d594fSAndroid Build Coastguard Worker   // and should be ACKed via AcknowledgeWakeup.
104*795d594fSAndroid Build Coastguard Worker   void WakeupPollLoop();
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker   // After a call to WakeupPollLoop, the fdevent internal counter should be decreased via
107*795d594fSAndroid Build Coastguard Worker   // this method. This should be called after WakeupPollLoop was called and poll triggered.
108*795d594fSAndroid Build Coastguard Worker   void AcknowledgeWakeup();
109*795d594fSAndroid Build Coastguard Worker 
110*795d594fSAndroid Build Coastguard Worker   // Stops debugger threads during shutdown.
111*795d594fSAndroid Build Coastguard Worker   void StopDebuggerThreads();
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker   // If StartDebuggerThreads was called successfully.
DebuggerThreadsStarted()114*795d594fSAndroid Build Coastguard Worker   bool DebuggerThreadsStarted() {
115*795d594fSAndroid Build Coastguard Worker     return started_debugger_threads_;
116*795d594fSAndroid Build Coastguard Worker   }
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   // Should be called by Framework when it changes its process name.
119*795d594fSAndroid Build Coastguard Worker   void SetCurrentProcessName(const std::string& process_name);
120*795d594fSAndroid Build Coastguard Worker 
121*795d594fSAndroid Build Coastguard Worker   // Should be called by Framework when it adds an app to a process.
122*795d594fSAndroid Build Coastguard Worker   // This can be called several times (see android:process)
123*795d594fSAndroid Build Coastguard Worker   void AddApplication(const std::string& package_name);
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker   // Should be called by Framework when it removes an app from a process.
126*795d594fSAndroid Build Coastguard Worker   void RemoveApplication(const std::string& package_name);
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker   // Should be called by Framework when its debugging state changes.
129*795d594fSAndroid Build Coastguard Worker   void SetWaitingForDebugger(bool waiting);
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker   // Should be called by Framework when the UserID is known.
132*795d594fSAndroid Build Coastguard Worker   void SetUserId(int uid);
133*795d594fSAndroid Build Coastguard Worker 
134*795d594fSAndroid Build Coastguard Worker  private:
135*795d594fSAndroid Build Coastguard Worker   uint32_t NextDdmId();
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker   void StartDebuggerThreads();
138*795d594fSAndroid Build Coastguard Worker 
139*795d594fSAndroid Build Coastguard Worker   // Tell adbd about the new runtime.
140*795d594fSAndroid Build Coastguard Worker   bool SetupAdbConnection();
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker   std::string MakeAgentArg();
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker   void SendAgentFds(bool require_handshake);
145*795d594fSAndroid Build Coastguard Worker 
146*795d594fSAndroid Build Coastguard Worker   void CloseFds();
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   void HandleDataWithoutAgent(art::Thread* self);
149*795d594fSAndroid Build Coastguard Worker 
150*795d594fSAndroid Build Coastguard Worker   void PerformHandshake();
151*795d594fSAndroid Build Coastguard Worker 
152*795d594fSAndroid Build Coastguard Worker   void AttachJdwpAgent(art::Thread* self);
153*795d594fSAndroid Build Coastguard Worker 
154*795d594fSAndroid Build Coastguard Worker   void NotifyDdms(bool active);
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker   void SendDdmPacket(uint32_t id,
157*795d594fSAndroid Build Coastguard Worker                      DdmPacketType type,
158*795d594fSAndroid Build Coastguard Worker                      uint32_t ddm_type,
159*795d594fSAndroid Build Coastguard Worker                      art::ArrayRef<const uint8_t> data);
160*795d594fSAndroid Build Coastguard Worker 
161*795d594fSAndroid Build Coastguard Worker   std::string agent_name_;
162*795d594fSAndroid Build Coastguard Worker 
163*795d594fSAndroid Build Coastguard Worker   AdbConnectionDebuggerController controller_;
164*795d594fSAndroid Build Coastguard Worker   AdbConnectionDdmCallback ddm_callback_;
165*795d594fSAndroid Build Coastguard Worker   AdbConnectionAppInfoCallback appinfo_callback_;
166*795d594fSAndroid Build Coastguard Worker 
167*795d594fSAndroid Build Coastguard Worker   // Eventfd used to allow the StopDebuggerThreads function to wake up sleeping threads
168*795d594fSAndroid Build Coastguard Worker   android::base::unique_fd sleep_event_fd_;
169*795d594fSAndroid Build Coastguard Worker 
170*795d594fSAndroid Build Coastguard Worker   // Context which wraps the socket which we use to talk to adbd.
171*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<AdbConnectionClientContext, void(*)(AdbConnectionClientContext*)> control_ctx_;
172*795d594fSAndroid Build Coastguard Worker 
173*795d594fSAndroid Build Coastguard Worker   // Socket that we use to talk to the agent (if it's loaded).
174*795d594fSAndroid Build Coastguard Worker   android::base::unique_fd local_agent_control_sock_;
175*795d594fSAndroid Build Coastguard Worker 
176*795d594fSAndroid Build Coastguard Worker   // The fd of the socket the agent uses to talk to us. We need to keep it around in order to clean
177*795d594fSAndroid Build Coastguard Worker   // it up when the runtime goes away.
178*795d594fSAndroid Build Coastguard Worker   android::base::unique_fd remote_agent_control_sock_;
179*795d594fSAndroid Build Coastguard Worker 
180*795d594fSAndroid Build Coastguard Worker   // The fd that is forwarded through adb to the client. This is guarded by the
181*795d594fSAndroid Build Coastguard Worker   // adb_write_event_fd_.
182*795d594fSAndroid Build Coastguard Worker   android::base::unique_fd adb_connection_socket_;
183*795d594fSAndroid Build Coastguard Worker 
184*795d594fSAndroid Build Coastguard Worker   // The fd we send to the agent to let us synchronize access to the shared adb_connection_socket_.
185*795d594fSAndroid Build Coastguard Worker   // This is also used as a general lock for the adb_connection_socket_ on any threads other than
186*795d594fSAndroid Build Coastguard Worker   // the poll thread.
187*795d594fSAndroid Build Coastguard Worker   android::base::unique_fd adb_write_event_fd_;
188*795d594fSAndroid Build Coastguard Worker 
189*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> shutting_down_;
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker   // True if we have loaded the agent library.
192*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> agent_loaded_;
193*795d594fSAndroid Build Coastguard Worker 
194*795d594fSAndroid Build Coastguard Worker   // True if the dt_fd_forward transport is listening for a new communication channel.
195*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> agent_listening_;
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker   // True if the dt_fd_forward transport has the socket. If so we don't do anything to the agent or
198*795d594fSAndroid Build Coastguard Worker   // the adb connection socket until connection goes away.
199*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> agent_has_socket_;
200*795d594fSAndroid Build Coastguard Worker 
201*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> sent_agent_fds_;
202*795d594fSAndroid Build Coastguard Worker 
203*795d594fSAndroid Build Coastguard Worker   std::atomic<bool> performed_handshake_;
204*795d594fSAndroid Build Coastguard Worker 
205*795d594fSAndroid Build Coastguard Worker   bool notified_ddm_active_;
206*795d594fSAndroid Build Coastguard Worker 
207*795d594fSAndroid Build Coastguard Worker   std::atomic<uint32_t> next_ddm_id_;
208*795d594fSAndroid Build Coastguard Worker 
209*795d594fSAndroid Build Coastguard Worker   bool started_debugger_threads_;
210*795d594fSAndroid Build Coastguard Worker 
211*795d594fSAndroid Build Coastguard Worker   friend struct AdbConnectionDebuggerController;
212*795d594fSAndroid Build Coastguard Worker };
213*795d594fSAndroid Build Coastguard Worker 
214*795d594fSAndroid Build Coastguard Worker }  // namespace adbconnection
215*795d594fSAndroid Build Coastguard Worker 
216*795d594fSAndroid Build Coastguard Worker #endif  // ART_ADBCONNECTION_ADBCONNECTION_H_
217