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 _DALVIK_JDWP_JDWPPRIV
20 #define _DALVIK_JDWP_JDWPPRIV
21
22 #define LOG_TAG "jdwp"
23
24 #include "jdwp/Jdwp.h"
25 #include "jdwp/JdwpEvent.h"
26 #include "Debugger.h"
27
28 #include <pthread.h>
29 #include <sys/uio.h>
30
31 /*
32 * JDWP constants.
33 */
34 #define kJDWPHeaderLen 11
35 #define kJDWPFlagReply 0x80
36
37 /* DDM support */
38 #define kJDWPDdmCmdSet 199 /* 0xc7, or 'G'+128 */
39 #define kJDWPDdmCmd 1
40
41
42 /*
43 * Transport-specific network status.
44 */
45 struct JdwpNetState;
46 typedef struct JdwpNetState JdwpNetState;
47
48 struct JdwpState;
49
50 /*
51 * Transport functions.
52 */
53 typedef struct JdwpTransport {
54 bool (*startup)(struct JdwpState* state, const JdwpStartupParams* pParams);
55 bool (*accept)(struct JdwpState* state);
56 bool (*establish)(struct JdwpState* state);
57 void (*close)(struct JdwpState* state);
58 void (*shutdown)(struct JdwpState* state);
59 void (*free)(struct JdwpState* state);
60 bool (*isConnected)(struct JdwpState* state);
61 bool (*awaitingHandshake)(struct JdwpState* state);
62 bool (*processIncoming)(struct JdwpState* state);
63 bool (*sendRequest)(struct JdwpState* state, ExpandBuf* pReq);
64 bool (*sendBufferedRequest)(struct JdwpState* state,
65 const struct iovec* iov, int iovcnt);
66 } JdwpTransport;
67
68 const JdwpTransport* dvmJdwpSocketTransport();
69 const JdwpTransport* dvmJdwpAndroidAdbTransport();
70
71
72 /*
73 * State for JDWP functions.
74 */
75 struct JdwpState {
76 JdwpStartupParams params;
77
78 /* wait for creation of the JDWP thread */
79 pthread_mutex_t threadStartLock;
80 pthread_cond_t threadStartCond;
81
82 int debugThreadStarted;
83 pthread_t debugThreadHandle;
84 ObjectId debugThreadId;
85 bool run;
86
87 const JdwpTransport* transport;
88 JdwpNetState* netState;
89
90 /* for wait-for-debugger */
91 pthread_mutex_t attachLock;
92 pthread_cond_t attachCond;
93
94 /* time of last debugger activity, in milliseconds */
95 s8 lastActivityWhen;
96
97 /* global counters and a mutex to protect them */
98 u4 requestSerial;
99 u4 eventSerial;
100 pthread_mutex_t serialLock;
101
102 /*
103 * Events requested by the debugger (breakpoints, class prep, etc).
104 */
105 int numEvents; /* #of elements in eventList */
106 JdwpEvent* eventList; /* linked list of events */
107 pthread_mutex_t eventLock; /* guards numEvents/eventList */
108
109 /*
110 * Synchronize suspension of event thread (to avoid receiving "resume"
111 * events before the thread has finished suspending itself).
112 */
113 pthread_mutex_t eventThreadLock;
114 pthread_cond_t eventThreadCond;
115 ObjectId eventThreadId;
116
117 /*
118 * DDM support.
119 */
120 bool ddmActive;
121 };
122
123
124 /* reset all session-specific data */
125 void dvmJdwpResetState(JdwpState* state);
126
127 /* atomic ops to get next serial number */
128 u4 dvmJdwpNextRequestSerial(JdwpState* state);
129 u4 dvmJdwpNextEventSerial(JdwpState* state);
130
131 /* get current time, in msec */
132 s8 dvmJdwpGetNowMsec(void);
133
134
135 /*
136 * Transport functions.
137 */
dvmJdwpNetStartup(JdwpState * state,const JdwpStartupParams * pParams)138 INLINE bool dvmJdwpNetStartup(JdwpState* state,
139 const JdwpStartupParams* pParams)
140 {
141 return (*state->transport->startup)(state, pParams);
142 }
dvmJdwpAcceptConnection(JdwpState * state)143 INLINE bool dvmJdwpAcceptConnection(JdwpState* state) {
144 return (*state->transport->accept)(state);
145 }
dvmJdwpEstablishConnection(JdwpState * state)146 INLINE bool dvmJdwpEstablishConnection(JdwpState* state) {
147 return (*state->transport->establish)(state);
148 }
dvmJdwpCloseConnection(JdwpState * state)149 INLINE void dvmJdwpCloseConnection(JdwpState* state) {
150 (*state->transport->close)(state);
151 }
dvmJdwpNetShutdown(JdwpState * state)152 INLINE void dvmJdwpNetShutdown(JdwpState* state) {
153 (*state->transport->shutdown)(state);
154 }
dvmJdwpNetFree(JdwpState * state)155 INLINE void dvmJdwpNetFree(JdwpState* state) {
156 (*state->transport->free)(state);
157 }
dvmJdwpIsTransportDefined(JdwpState * state)158 INLINE bool dvmJdwpIsTransportDefined(JdwpState* state) {
159 return state != NULL && state->transport != NULL;
160 }
dvmJdwpIsConnected(JdwpState * state)161 INLINE bool dvmJdwpIsConnected(JdwpState* state) {
162 return state != NULL && (*state->transport->isConnected)(state);
163 }
dvmJdwpAwaitingHandshake(JdwpState * state)164 INLINE bool dvmJdwpAwaitingHandshake(JdwpState* state) {
165 return (*state->transport->awaitingHandshake)(state);
166 }
dvmJdwpProcessIncoming(JdwpState * state)167 INLINE bool dvmJdwpProcessIncoming(JdwpState* state) {
168 return (*state->transport->processIncoming)(state);
169 }
dvmJdwpSendRequest(JdwpState * state,ExpandBuf * pReq)170 INLINE bool dvmJdwpSendRequest(JdwpState* state, ExpandBuf* pReq) {
171 return (*state->transport->sendRequest)(state, pReq);
172 }
dvmJdwpSendBufferedRequest(JdwpState * state,const struct iovec * iov,int iovcnt)173 INLINE bool dvmJdwpSendBufferedRequest(JdwpState* state,
174 const struct iovec* iov, int iovcnt)
175 {
176 return (*state->transport->sendBufferedRequest)(state, iov, iovcnt);
177 }
178
179 #endif /*_DALVIK_JDWP_JDWPPRIV*/
180