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 kJDWPObjectReferenceCmdSet = 9U;
49 static constexpr uint8_t kJDWPObjectReferenceInvokeCmd = 6U;
50 
51 /* Event command */
52 static constexpr uint8_t kJDWPEventCmdSet = 64U;
53 static constexpr uint8_t kJDWPEventCompositeCmd = 100U;
54 
55 /* DDM support */
56 static constexpr uint8_t kJDWPDdmCmdSet = 199U;  // 0xc7, or 'G'+128
57 static constexpr uint8_t kJDWPDdmCmd = 1U;
58 
59 namespace art {
60 
61 namespace JDWP {
62 
63 struct JdwpState;
64 
65 bool InitSocketTransport(JdwpState*, const JdwpOptions*);
66 bool InitAdbTransport(JdwpState*, const JdwpOptions*);
67 
68 /*
69  * Base class for the adb and socket JdwpNetState implementations.
70  */
71 class JdwpNetStateBase {
72  public:
73   explicit JdwpNetStateBase(JdwpState*);
74   virtual ~JdwpNetStateBase();
75 
76   virtual bool Accept() = 0;
77   virtual bool Establish(const JdwpOptions*) = 0;
78   virtual void Shutdown() = 0;
79   virtual bool ProcessIncoming() = 0;
80 
81   void ConsumeBytes(size_t byte_count);
82 
83   bool IsConnected();
84 
85   bool IsAwaitingHandshake();
86 
87   void Close();
88 
89   ssize_t WritePacket(ExpandBuf* pReply, size_t length) LOCKS_EXCLUDED(socket_lock_);
90   ssize_t WriteBufferedPacket(const std::vector<iovec>& iov) LOCKS_EXCLUDED(socket_lock_);
GetSocketLock()91   Mutex* GetSocketLock() {
92     return &socket_lock_;
93   }
94   ssize_t WriteBufferedPacketLocked(const std::vector<iovec>& iov);
95 
96   int clientSock;  // Active connection to debugger.
97 
98   int wake_pipe_[2];  // Used to break out of select.
99 
100   uint8_t input_buffer_[8192];
101   size_t input_count_;
102 
103  protected:
104   bool HaveFullPacket();
105 
106   bool MakePipe();
107   void WakePipe();
108 
109   void SetAwaitingHandshake(bool new_state);
110 
111   JdwpState* state_;
112 
113  private:
114   // Used to serialize writes to the socket.
115   Mutex socket_lock_;
116 
117   // Are we waiting for the JDWP handshake?
118   bool awaiting_handshake_;
119 };
120 
121 }  // namespace JDWP
122 
123 }  // namespace art
124 
125 #endif  // ART_RUNTIME_JDWP_JDWP_PRIV_H_
126