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