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 #define kJDWPHeaderLen  11
33 #define kJDWPFlagReply  0x80
34 
35 #define kMagicHandshake     "JDWP-Handshake"
36 #define kMagicHandshakeLen  (sizeof(kMagicHandshake)-1)
37 
38 /* DDM support */
39 #define kJDWPDdmCmdSet  199     /* 0xc7, or 'G'+128 */
40 #define kJDWPDdmCmd     1
41 
42 namespace art {
43 
44 namespace JDWP {
45 
46 struct JdwpState;
47 
48 bool InitSocketTransport(JdwpState*, const JdwpOptions*);
49 bool InitAdbTransport(JdwpState*, const JdwpOptions*);
50 
51 /*
52  * Base class for the adb and socket JdwpNetState implementations.
53  */
54 class JdwpNetStateBase {
55  public:
56   explicit JdwpNetStateBase(JdwpState*);
57   virtual ~JdwpNetStateBase();
58 
59   virtual bool Accept() = 0;
60   virtual bool Establish(const JdwpOptions*) = 0;
61   virtual void Shutdown() = 0;
62   virtual bool ProcessIncoming() = 0;
63 
64   void ConsumeBytes(size_t byte_count);
65 
66   bool IsConnected();
67 
68   bool IsAwaitingHandshake();
69 
70   void Close();
71 
72   ssize_t WritePacket(ExpandBuf* pReply, size_t length) LOCKS_EXCLUDED(socket_lock_);
73   ssize_t WriteBufferedPacket(const std::vector<iovec>& iov) LOCKS_EXCLUDED(socket_lock_);
74 
75   int clientSock;  // Active connection to debugger.
76 
77   int wake_pipe_[2];  // Used to break out of select.
78 
79   uint8_t input_buffer_[8192];
80   size_t input_count_;
81 
82  protected:
83   bool HaveFullPacket();
84 
85   bool MakePipe();
86   void WakePipe();
87 
88   void SetAwaitingHandshake(bool new_state);
89 
90   JdwpState* state_;
91 
92  private:
93   // Used to serialize writes to the socket.
94   Mutex socket_lock_;
95 
96   // Are we waiting for the JDWP handshake?
97   bool awaiting_handshake_;
98 };
99 
100 }  // namespace JDWP
101 
102 }  // namespace art
103 
104 #endif  // ART_RUNTIME_JDWP_JDWP_PRIV_H_
105