JdwpPriv.h revision 128689844fda7f9287cc7494ec357397b6d59576
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_H_
20#define DALVIK_JDWP_JDWPPRIV_H_
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 */
45struct JdwpNetState;
46struct JdwpState;
47
48/*
49 * Transport functions.
50 */
51struct 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    bool (*sendBufferedRequest)(struct JdwpState* state,
63        const struct iovec* iov, int iovcnt);
64};
65
66const JdwpTransport* dvmJdwpSocketTransport();
67const JdwpTransport* dvmJdwpAndroidAdbTransport();
68
69
70/*
71 * State for JDWP functions.
72 */
73struct JdwpState {
74    JdwpStartupParams   params;
75
76    /* wait for creation of the JDWP thread */
77    pthread_mutex_t threadStartLock;
78    pthread_cond_t  threadStartCond;
79
80    int             debugThreadStarted;
81    pthread_t       debugThreadHandle;
82    ObjectId        debugThreadId;
83    bool            run;
84
85    const JdwpTransport*    transport;
86    JdwpNetState*   netState;
87
88    /* for wait-for-debugger */
89    pthread_mutex_t attachLock;
90    pthread_cond_t  attachCond;
91
92    /* time of last debugger activity, in milliseconds */
93    s8              lastActivityWhen;
94
95    /* global counters and a mutex to protect them */
96    u4              requestSerial;
97    u4              eventSerial;
98    pthread_mutex_t serialLock;
99
100    /*
101     * Events requested by the debugger (breakpoints, class prep, etc).
102     */
103    int             numEvents;      /* #of elements in eventList */
104    JdwpEvent*      eventList;      /* linked list of events */
105    pthread_mutex_t eventLock;      /* guards numEvents/eventList */
106
107    /*
108     * Synchronize suspension of event thread (to avoid receiving "resume"
109     * events before the thread has finished suspending itself).
110     */
111    pthread_mutex_t eventThreadLock;
112    pthread_cond_t  eventThreadCond;
113    ObjectId        eventThreadId;
114
115    /*
116     * DDM support.
117     */
118    bool            ddmActive;
119};
120
121/*
122 * Base class for JdwpNetState
123 */
124class JdwpNetStateBase {
125public:
126    int             clientSock;     /* active connection to debugger */
127
128    JdwpNetStateBase();
129    ssize_t writePacket(ExpandBuf* pReply);
130    ssize_t writeBufferedPacket(const struct iovec* iov, int iovcnt);
131
132private:
133    pthread_mutex_t socketLock;     /* socket synchronization */
134};
135
136
137/* reset all session-specific data */
138void dvmJdwpResetState(JdwpState* state);
139
140/* atomic ops to get next serial number */
141u4 dvmJdwpNextRequestSerial(JdwpState* state);
142u4 dvmJdwpNextEventSerial(JdwpState* state);
143
144/* get current time, in msec */
145s8 dvmJdwpGetNowMsec(void);
146
147
148/*
149 * Transport functions.
150 */
151INLINE bool dvmJdwpNetStartup(JdwpState* state,
152    const JdwpStartupParams* pParams)
153{
154    return (*state->transport->startup)(state, pParams);
155}
156INLINE bool dvmJdwpAcceptConnection(JdwpState* state) {
157    return (*state->transport->accept)(state);
158}
159INLINE bool dvmJdwpEstablishConnection(JdwpState* state) {
160    return (*state->transport->establish)(state);
161}
162INLINE void dvmJdwpCloseConnection(JdwpState* state) {
163    (*state->transport->close)(state);
164}
165INLINE void dvmJdwpNetShutdown(JdwpState* state) {
166    (*state->transport->shutdown)(state);
167}
168INLINE void dvmJdwpNetFree(JdwpState* state) {
169    (*state->transport->free)(state);
170}
171INLINE bool dvmJdwpIsTransportDefined(JdwpState* state) {
172    return state != NULL && state->transport != NULL;
173}
174INLINE bool dvmJdwpIsConnected(JdwpState* state) {
175    return state != NULL && (*state->transport->isConnected)(state);
176}
177INLINE bool dvmJdwpAwaitingHandshake(JdwpState* state) {
178    return (*state->transport->awaitingHandshake)(state);
179}
180INLINE bool dvmJdwpProcessIncoming(JdwpState* state) {
181    return (*state->transport->processIncoming)(state);
182}
183INLINE bool dvmJdwpSendRequest(JdwpState* state, ExpandBuf* pReq) {
184    return (*state->transport->sendRequest)(state, pReq);
185}
186INLINE bool dvmJdwpSendBufferedRequest(JdwpState* state,
187    const struct iovec* iov, int iovcnt)
188{
189    return (*state->transport->sendBufferedRequest)(state, iov, iovcnt);
190}
191
192#endif  // DALVIK_JDWP_JDWPPRIV_H_
193