1 /*
2  * Copyright (C) 2008 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  * JDWP internal interfaces.
18  */
19 #ifndef ART_RUNTIME_JDWP_JDWP_PRIV_H_
20 #define ART_RUNTIME_JDWP_JDWP_PRIV_H_
21 
22 #include "debugger.h"
23 #include "jdwp/jdwp.h"
24 #include "jdwp/jdwp_event.h"
25 
26 #include <pthread.h>
27 #include <sys/uio.h>
28 
29 /*
30  * JDWP constants.
31  */
32 static constexpr size_t kJDWPHeaderSizeOffset = 0U;
33 static constexpr size_t kJDWPHeaderIdOffset = 4U;
34 static constexpr size_t kJDWPHeaderFlagsOffset = 8U;
35 static constexpr size_t kJDWPHeaderErrorCodeOffset = 9U;
36 static constexpr size_t kJDWPHeaderCmdSetOffset = 9U;
37 static constexpr size_t kJDWPHeaderCmdOffset = 10U;
38 static constexpr size_t kJDWPHeaderLen = 11U;
39 static constexpr uint8_t kJDWPFlagReply = 0x80;
40 
41 static constexpr const char kMagicHandshake[] = "JDWP-Handshake";
42 static constexpr size_t kMagicHandshakeLen = sizeof(kMagicHandshake) - 1;
43 
44 /* Invoke commands */
45 static constexpr uint8_t kJDWPClassTypeCmdSet = 3U;
46 static constexpr uint8_t kJDWPClassTypeInvokeMethodCmd = 3U;
47 static constexpr uint8_t kJDWPClassTypeNewInstanceCmd = 4U;
48 static constexpr uint8_t kJDWPInterfaceTypeCmdSet = 5U;
49 static constexpr uint8_t kJDWPInterfaceTypeInvokeMethodCmd = 1U;
50 static constexpr uint8_t kJDWPObjectReferenceCmdSet = 9U;
51 static constexpr uint8_t kJDWPObjectReferenceInvokeCmd = 6U;
52 
53 /* Event command */
54 static constexpr uint8_t kJDWPEventCmdSet = 64U;
55 static constexpr uint8_t kJDWPEventCompositeCmd = 100U;
56 
57 /* DDM support */
58 static constexpr uint8_t kJDWPDdmCmdSet = 199U;  // 0xc7, or 'G'+128
59 static constexpr uint8_t kJDWPDdmCmd = 1U;
60 
61 namespace art {
62 
63 namespace JDWP {
64 
65 struct JdwpState;
66 
67 bool InitSocketTransport(JdwpState*, const JdwpOptions*);
68 bool InitAdbTransport(JdwpState*, const JdwpOptions*);
69 
70 /*
71  * Base class for the adb and socket JdwpNetState implementations.
72  */
73 class JdwpNetStateBase {
74  public:
75   explicit JdwpNetStateBase(JdwpState*);
76   virtual ~JdwpNetStateBase();
77 
78   virtual bool Accept() = 0;
79   virtual bool Establish(const JdwpOptions*) = 0;
80   virtual void Shutdown() = 0;
81   virtual bool ProcessIncoming() = 0;
82 
83   void ConsumeBytes(size_t byte_count);
84 
85   bool IsConnected();
86 
87   bool IsAwaitingHandshake();
88 
89   void Close();
90 
91   ssize_t WritePacket(ExpandBuf* pReply, size_t length) REQUIRES(!socket_lock_);
92   ssize_t WriteBufferedPacket(const std::vector<iovec>& iov) REQUIRES(!socket_lock_);
GetSocketLock()93   Mutex* GetSocketLock() {
94     return &socket_lock_;
95   }
96   ssize_t WriteBufferedPacketLocked(const std::vector<iovec>& iov);
97 
98   int clientSock;  // Active connection to debugger.
99 
100   int wake_pipe_[2];  // Used to break out of select.
101 
102   uint8_t input_buffer_[8192];
103   size_t input_count_;
104 
105  protected:
106   bool HaveFullPacket();
107 
108   bool MakePipe();
109   void WakePipe();
110 
111   void SetAwaitingHandshake(bool new_state);
112 
113   JdwpState* state_;
114 
115  private:
116   // Used to serialize writes to the socket.
117   Mutex socket_lock_;
118 
119   // Are we waiting for the JDWP handshake?
120   bool awaiting_handshake_;
121 };
122 
123 }  // namespace JDWP
124 
125 }  // namespace art
126 
127 #endif  // ART_RUNTIME_JDWP_JDWP_PRIV_H_
128