1 /*
2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Java Debug Wire Protocol Transport Service Provider Interface.
28  */
29 
30 #ifndef JDWPTRANSPORT_H
31 #define JDWPTRANSPORT_H
32 
33 #include "jni.h"
34 
35 enum {
36     JDWPTRANSPORT_VERSION_1_0 = 0x00010000,
37     JDWPTRANSPORT_VERSION_1_1 = 0x00010001
38 };
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 struct jdwpTransportNativeInterface_;
45 
46 struct _jdwpTransportEnv;
47 
48 #ifdef __cplusplus
49 typedef _jdwpTransportEnv jdwpTransportEnv;
50 #else
51 typedef const struct jdwpTransportNativeInterface_ *jdwpTransportEnv;
52 #endif /* __cplusplus */
53 
54 /*
55  * Errors. Universal errors with JVMTI/JVMDI equivalents keep the
56  * values the same.
57  */
58 typedef enum {
59     JDWPTRANSPORT_ERROR_NONE = 0,
60     JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT = 103,
61     JDWPTRANSPORT_ERROR_OUT_OF_MEMORY = 110,
62     JDWPTRANSPORT_ERROR_INTERNAL = 113,
63     JDWPTRANSPORT_ERROR_ILLEGAL_STATE = 201,
64     JDWPTRANSPORT_ERROR_IO_ERROR = 202,
65     JDWPTRANSPORT_ERROR_TIMEOUT = 203,
66     JDWPTRANSPORT_ERROR_MSG_NOT_AVAILABLE = 204
67 } jdwpTransportError;
68 
69 
70 /*
71  * Structure to define capabilities
72  */
73 typedef struct {
74     unsigned int can_timeout_attach     :1;
75     unsigned int can_timeout_accept     :1;
76     unsigned int can_timeout_handshake  :1;
77     unsigned int reserved3              :1;
78     unsigned int reserved4              :1;
79     unsigned int reserved5              :1;
80     unsigned int reserved6              :1;
81     unsigned int reserved7              :1;
82     unsigned int reserved8              :1;
83     unsigned int reserved9              :1;
84     unsigned int reserved10             :1;
85     unsigned int reserved11             :1;
86     unsigned int reserved12             :1;
87     unsigned int reserved13             :1;
88     unsigned int reserved14             :1;
89     unsigned int reserved15             :1;
90 } JDWPTransportCapabilities;
91 
92 
93 /*
94  * Structures to define packet layout.
95  *
96  * See: http://java.sun.com/j2se/1.5/docs/guide/jpda/jdwp-spec.html
97  */
98 
99 #define JDWP_HEADER_SIZE 11
100 
101 enum {
102     /*
103      * If additional flags are added that apply to jdwpCmdPacket,
104      * then debugLoop.c: reader() will need to be updated to
105      * accept more than JDWPTRANSPORT_FLAGS_NONE.
106      */
107     JDWPTRANSPORT_FLAGS_NONE     = 0x0,
108     JDWPTRANSPORT_FLAGS_REPLY    = 0x80
109 };
110 
111 typedef struct {
112     jint len;
113     jint id;
114     jbyte flags;
115     jbyte cmdSet;
116     jbyte cmd;
117     jbyte *data;
118 } jdwpCmdPacket;
119 
120 typedef struct {
121     jint len;
122     jint id;
123     jbyte flags;
124     jshort errorCode;
125     jbyte *data;
126 } jdwpReplyPacket;
127 
128 typedef struct {
129     union {
130         jdwpCmdPacket cmd;
131         jdwpReplyPacket reply;
132     } type;
133 } jdwpPacket;
134 
135 /*
136  * JDWP functions called by the transport.
137  */
138 typedef struct jdwpTransportCallback {
139     void *(*alloc)(jint numBytes);   /* Call this for all allocations */
140     void (*free)(void *buffer);      /* Call this for all deallocations */
141 } jdwpTransportCallback;
142 
143 typedef jint (JNICALL *jdwpTransport_OnLoad_t)(JavaVM *jvm,
144                                                jdwpTransportCallback *callback,
145                                                jint version,
146                                                jdwpTransportEnv** env);
147 
148 /*
149  * JDWP transport configuration from the agent.
150  */
151 typedef struct jdwpTransportConfiguration {
152     /* Field added in JDWPTRANSPORT_VERSION_1_1: */
153     const char* allowed_peers;       /* Peers allowed for connection */
154 } jdwpTransportConfiguration;
155 
156 
157 /* Function Interface */
158 
159 struct jdwpTransportNativeInterface_ {
160     /*  1 :  RESERVED */
161     void *reserved1;
162 
163     /*  2 : Get Capabilities */
164     jdwpTransportError (JNICALL *GetCapabilities)(jdwpTransportEnv* env,
165          JDWPTransportCapabilities *capabilities_ptr);
166 
167     /*  3 : Attach */
168     jdwpTransportError (JNICALL *Attach)(jdwpTransportEnv* env,
169         const char* address,
170         jlong attach_timeout,
171         jlong handshake_timeout);
172 
173     /*  4: StartListening */
174     jdwpTransportError (JNICALL *StartListening)(jdwpTransportEnv* env,
175         const char* address,
176         char** actual_address);
177 
178     /*  5: StopListening */
179     jdwpTransportError (JNICALL *StopListening)(jdwpTransportEnv* env);
180 
181     /*  6: Accept */
182     jdwpTransportError (JNICALL *Accept)(jdwpTransportEnv* env,
183         jlong accept_timeout,
184         jlong handshake_timeout);
185 
186     /*  7: IsOpen */
187     jboolean (JNICALL *IsOpen)(jdwpTransportEnv* env);
188 
189     /*  8: Close */
190     jdwpTransportError (JNICALL *Close)(jdwpTransportEnv* env);
191 
192     /*  9: ReadPacket */
193     jdwpTransportError (JNICALL *ReadPacket)(jdwpTransportEnv* env,
194         jdwpPacket *pkt);
195 
196     /*  10: Write Packet */
197     jdwpTransportError (JNICALL *WritePacket)(jdwpTransportEnv* env,
198         const jdwpPacket* pkt);
199 
200     /*  11:  GetLastError */
201     jdwpTransportError (JNICALL *GetLastError)(jdwpTransportEnv* env,
202         char** error);
203 
204     /*  12: SetTransportConfiguration added in JDWPTRANSPORT_VERSION_1_1 */
205     jdwpTransportError (JNICALL *SetTransportConfiguration)(jdwpTransportEnv* env,
206         jdwpTransportConfiguration *config);
207 };
208 
209 
210 /*
211  * Use inlined functions so that C++ code can use syntax such as
212  *      env->Attach("mymachine:5000", 10*1000, 0);
213  *
214  * rather than using C's :-
215  *
216  *      (*env)->Attach(env, "mymachine:5000", 10*1000, 0);
217  */
218 struct _jdwpTransportEnv {
219     const struct jdwpTransportNativeInterface_ *functions;
220 #ifdef __cplusplus
221 
GetCapabilities_jdwpTransportEnv222     jdwpTransportError GetCapabilities(JDWPTransportCapabilities *capabilities_ptr) {
223         return functions->GetCapabilities(this, capabilities_ptr);
224     }
225 
Attach_jdwpTransportEnv226     jdwpTransportError Attach(const char* address, jlong attach_timeout,
227                 jlong handshake_timeout) {
228         return functions->Attach(this, address, attach_timeout, handshake_timeout);
229     }
230 
StartListening_jdwpTransportEnv231     jdwpTransportError StartListening(const char* address,
232                 char** actual_address) {
233         return functions->StartListening(this, address, actual_address);
234     }
235 
StopListening_jdwpTransportEnv236     jdwpTransportError StopListening(void) {
237         return functions->StopListening(this);
238     }
239 
Accept_jdwpTransportEnv240     jdwpTransportError Accept(jlong accept_timeout, jlong handshake_timeout) {
241         return functions->Accept(this, accept_timeout, handshake_timeout);
242     }
243 
IsOpen_jdwpTransportEnv244     jboolean IsOpen(void) {
245         return functions->IsOpen(this);
246     }
247 
Close_jdwpTransportEnv248     jdwpTransportError Close(void) {
249         return functions->Close(this);
250     }
251 
ReadPacket_jdwpTransportEnv252     jdwpTransportError ReadPacket(jdwpPacket *pkt) {
253         return functions->ReadPacket(this, pkt);
254     }
255 
WritePacket_jdwpTransportEnv256     jdwpTransportError WritePacket(const jdwpPacket* pkt) {
257         return functions->WritePacket(this, pkt);
258     }
259 
GetLastError_jdwpTransportEnv260     jdwpTransportError GetLastError(char** error) {
261         return functions->GetLastError(this, error);
262     }
263 
264     /*  SetTransportConfiguration added in JDWPTRANSPORT_VERSION_1_1 */
265     jdwpTransportError SetTransportConfiguration(jdwpTransportEnv* env,
266         return functions->SetTransportConfiguration(this, config);
267     }
268 
269 #endif /* __cplusplus */
270 };
271 
272 #ifdef __cplusplus
273 } /* extern "C" */
274 #endif /* __cplusplus */
275 
276 #endif /* JDWPTRANSPORT_H */
277