blob: 6500b791645bfa0ff9786f49921c547de298006e [file] [log] [blame]
Alex Lightfbf96702017-12-14 13:27:13 -08001/*
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>
Josh Gaobd396c02020-01-22 18:02:19 -080021#include <memory>
Alex Lightfbf96702017-12-14 13:27:13 -080022#include <vector>
23#include <limits>
24
25#include "android-base/unique_fd.h"
Josh Gaobd396c02020-01-22 18:02:19 -080026#include "adbconnection/client.h"
Alex Lightfbf96702017-12-14 13:27:13 -080027
28#include "base/mutex.h"
Alex Light15b81132018-01-24 13:29:07 -080029#include "base/array_ref.h"
Alex Lightfbf96702017-12-14 13:27:13 -080030#include "runtime_callbacks.h"
31
32#include <sys/socket.h>
33#include <sys/un.h>
34#include <jni.h>
35
36namespace adbconnection {
37
38static constexpr char kJdwpControlName[] = "\0jdwp-control";
39static constexpr char kAdbConnectionThreadName[] = "ADB-JDWP Connection Control Thread";
40
41// The default jdwp agent name.
42static constexpr char kDefaultJdwpAgentName[] = "libjdwp.so";
43
44class AdbConnectionState;
45
46struct AdbConnectionDebuggerController : public art::DebuggerControlCallback {
47 explicit AdbConnectionDebuggerController(AdbConnectionState* connection)
48 : connection_(connection) {}
49
50 // Begin running the debugger.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010051 void StartDebugger() override;
Alex Lightfbf96702017-12-14 13:27:13 -080052
53 // The debugger should begin shutting down since the runtime is ending.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010054 void StopDebugger() override;
Alex Lightfbf96702017-12-14 13:27:13 -080055
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010056 bool IsDebuggerConfigured() override;
Alex Lightfbf96702017-12-14 13:27:13 -080057
58 private:
59 AdbConnectionState* connection_;
60};
61
Alex Light15b81132018-01-24 13:29:07 -080062enum class DdmPacketType : uint8_t { kReply = 0x80, kCmd = 0x00, };
63
Alex Lightfbf96702017-12-14 13:27:13 -080064struct AdbConnectionDdmCallback : public art::DdmCallback {
65 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
75class AdbConnectionState {
76 public:
Alex Lightd6f9d852018-01-25 11:26:28 -080077 explicit AdbConnectionState(const std::string& name);
Flash Liu013e2082019-10-31 11:18:55 +080078 ~AdbConnectionState();
Alex Lightfbf96702017-12-14 13:27:13 -080079
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
Alex Lightd6f9d852018-01-25 11:26:28 -080091 // If StartDebuggerThreads was called successfully.
92 bool DebuggerThreadsStarted() {
93 return started_debugger_threads_;
94 }
95
Alex Lightfbf96702017-12-14 13:27:13 -080096 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
Alex Light15b81132018-01-24 13:29:07 -0800108 void SendAgentFds(bool require_handshake);
Alex Lightfbf96702017-12-14 13:27:13 -0800109
110 void CloseFds();
111
Alex Light15b81132018-01-24 13:29:07 -0800112 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
Alex Lightfbf96702017-12-14 13:27:13 -0800125 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
Josh Gaobd396c02020-01-22 18:02:19 -0800133 // Context which wraps the socket which we use to talk to adbd.
134 std::unique_ptr<AdbConnectionClientContext, void(*)(AdbConnectionClientContext*)> control_ctx_;
Alex Lightfbf96702017-12-14 13:27:13 -0800135
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
Alex Lightf51d1822021-02-01 19:25:35 -0800166 std::atomic<bool> performed_handshake_;
Alex Light15b81132018-01-24 13:29:07 -0800167
168 bool notified_ddm_active_;
169
Alex Lightfbf96702017-12-14 13:27:13 -0800170 std::atomic<uint32_t> next_ddm_id_;
171
Alex Lightd6f9d852018-01-25 11:26:28 -0800172 bool started_debugger_threads_;
173
Alex Lightfbf96702017-12-14 13:27:13 -0800174 friend struct AdbConnectionDebuggerController;
175};
176
177} // namespace adbconnection
178
179#endif // ART_ADBCONNECTION_ADBCONNECTION_H_