blob: 3b67bfad6ea514707c203cccad8fd911d989c907 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
duke6e45e102007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
duke6e45e102007-12-01 00:00:00 +000010 *
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 *
ohair2283b9d2010-05-25 15:58:33 -070021 * 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.
duke6e45e102007-12-01 00:00:00 +000024 */
25
26#ifndef JDWP_UTIL_H
27#define JDWP_UTIL_H
28
29#include <stddef.h>
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdarg.h>
34
35#ifdef DEBUG
36 /* Just to make sure these interfaces are not used here. */
37 #undef free
38 #define free(p) Do not use this interface.
39 #undef malloc
40 #define malloc(p) Do not use this interface.
41 #undef calloc
42 #define calloc(p) Do not use this interface.
43 #undef realloc
44 #define realloc(p) Do not use this interface.
45 #undef strdup
46 #define strdup(p) Do not use this interface.
47#endif
48
49#include "log_messages.h"
50#include "vm_interface.h"
51#include "JDWP.h"
52#include "util_md.h"
53#include "error_messages.h"
54#include "debugInit.h"
55
56/* Get access to Native Platform Toolkit functions */
57#include "npt.h"
58
59/* Definition of a CommonRef tracked by the backend for the frontend */
60typedef struct RefNode {
61 jlong seqNum; /* ID of reference, also key for hash table */
62 jobject ref; /* could be strong or weak */
63 struct RefNode *next; /* next RefNode* in bucket chain */
64 jint count; /* count of references */
65 unsigned isStrong : 1; /* 1 means this is a string reference */
66} RefNode;
67
68/* Value of a NULL ID */
69#define NULL_OBJECT_ID ((jlong)0)
70
71/*
72 * Globals used throughout the back end
73 */
74
75typedef jint FrameNumber;
76
Alex Light6af14c52017-11-13 11:01:06 -080077// ANDROID-CHANGED: support for DDMS extension apis.
78typedef jvmtiError (*DdmProcessChunk)(jvmtiEnv* jvmti,
79 jint type_in,
80 jint length_in,
81 const jbyte* data_in,
82 jint* type_out,
83 jint* length_out,
84 jbyte** data_out);
85
duke6e45e102007-12-01 00:00:00 +000086typedef struct {
87 jvmtiEnv *jvmti;
88 JavaVM *jvm;
89 volatile jboolean vmDead; /* Once VM is dead it stays that way - don't put in init */
90 jboolean assertOn;
91 jboolean assertFatal;
92 jboolean doerrorexit;
93 jboolean modifiedUtf8;
94 jboolean quiet;
95
96 /* Debug flags (bit mask) */
97 int debugflags;
98
99 /* Possible debug flags */
100 #define USE_ITERATE_THROUGH_HEAP 0X001
101
102 char * options;
103
104 jclass classClass;
105 jclass threadClass;
106 jclass threadGroupClass;
107 jclass classLoaderClass;
108 jclass stringClass;
109 jclass systemClass;
110 jmethodID threadConstructor;
111 jmethodID threadSetDaemon;
112 jmethodID threadResume;
113 jmethodID systemGetProperty;
114 jmethodID setProperty;
115 jthreadGroup systemThreadGroup;
116 jobject agent_properties;
117
118 jint cachedJvmtiVersion;
119 jvmtiCapabilities cachedJvmtiCapabilities;
120 jboolean haveCachedJvmtiCapabilities;
121 jvmtiEventCallbacks callbacks;
122
123 /* Various property values we should grab on initialization */
124 char* property_java_version; /* UTF8 java.version */
125 char* property_java_vm_name; /* UTF8 java.vm.name */
126 char* property_java_vm_info; /* UTF8 java.vm.info */
127 char* property_java_class_path; /* UTF8 java.class.path */
128 char* property_sun_boot_class_path; /* UTF8 sun.boot.class.path */
129 char* property_sun_boot_library_path; /* UTF8 sun.boot.library.path */
130 char* property_path_separator; /* UTF8 path.separator */
131 char* property_user_dir; /* UTF8 user.dir */
132
133 unsigned log_flags;
134
135 /* The Native Platform Toolkit access */
136 NptEnv *npt;
137
138 /* Common References static data */
139 jrawMonitorID refLock;
140 jlong nextSeqNum;
141 RefNode **objectsByID;
142 int objectsByIDsize;
143 int objectsByIDcount;
144
145 /* Indication that the agent has been loaded */
146 jboolean isLoaded;
147
Alex Light6af14c52017-11-13 11:01:06 -0800148 /* ANDROID-CHANGED: com.android.art.internal.ddm.process_chunk extension function */
149 DdmProcessChunk ddm_process_chunk;
150
duke6e45e102007-12-01 00:00:00 +0000151} BackendGlobalData;
152
153extern BackendGlobalData * gdata;
154
155/*
156 * Event Index for handlers
157 */
158
159typedef enum {
160 EI_min = 1,
161
162 EI_SINGLE_STEP = 1,
163 EI_BREAKPOINT = 2,
164 EI_FRAME_POP = 3,
165 EI_EXCEPTION = 4,
166 EI_THREAD_START = 5,
167 EI_THREAD_END = 6,
168 EI_CLASS_PREPARE = 7,
169 EI_GC_FINISH = 8,
170 EI_CLASS_LOAD = 9,
171 EI_FIELD_ACCESS = 10,
172 EI_FIELD_MODIFICATION = 11,
173 EI_EXCEPTION_CATCH = 12,
174 EI_METHOD_ENTRY = 13,
175 EI_METHOD_EXIT = 14,
176 EI_MONITOR_CONTENDED_ENTER = 15,
177 EI_MONITOR_CONTENDED_ENTERED = 16,
178 EI_MONITOR_WAIT = 17,
179 EI_MONITOR_WAITED = 18,
180 EI_VM_INIT = 19,
181 EI_VM_DEATH = 20,
182 EI_max = 20
183} EventIndex;
184
185/* Agent errors that might be in a jvmtiError for JDWP or internal.
186 * (Done this way so that compiler allows it's use as a jvmtiError)
187 */
188#define _AGENT_ERROR(x) ((jvmtiError)(JVMTI_ERROR_MAX+64+x))
189#define AGENT_ERROR_INTERNAL _AGENT_ERROR(1)
190#define AGENT_ERROR_VM_DEAD _AGENT_ERROR(2)
191#define AGENT_ERROR_NO_JNI_ENV _AGENT_ERROR(3)
192#define AGENT_ERROR_JNI_EXCEPTION _AGENT_ERROR(4)
193#define AGENT_ERROR_JVMTI_INTERNAL _AGENT_ERROR(5)
194#define AGENT_ERROR_JDWP_INTERNAL _AGENT_ERROR(6)
195#define AGENT_ERROR_NOT_CURRENT_FRAME _AGENT_ERROR(7)
196#define AGENT_ERROR_OUT_OF_MEMORY _AGENT_ERROR(8)
197#define AGENT_ERROR_INVALID_TAG _AGENT_ERROR(9)
198#define AGENT_ERROR_ALREADY_INVOKING _AGENT_ERROR(10)
199#define AGENT_ERROR_INVALID_INDEX _AGENT_ERROR(11)
200#define AGENT_ERROR_INVALID_LENGTH _AGENT_ERROR(12)
201#define AGENT_ERROR_INVALID_STRING _AGENT_ERROR(13)
202#define AGENT_ERROR_INVALID_CLASS_LOADER _AGENT_ERROR(14)
203#define AGENT_ERROR_INVALID_ARRAY _AGENT_ERROR(15)
204#define AGENT_ERROR_TRANSPORT_LOAD _AGENT_ERROR(16)
205#define AGENT_ERROR_TRANSPORT_INIT _AGENT_ERROR(17)
206#define AGENT_ERROR_NATIVE_METHOD _AGENT_ERROR(18)
207#define AGENT_ERROR_INVALID_COUNT _AGENT_ERROR(19)
208#define AGENT_ERROR_INVALID_FRAMEID _AGENT_ERROR(20)
209#define AGENT_ERROR_NULL_POINTER _AGENT_ERROR(21)
210#define AGENT_ERROR_ILLEGAL_ARGUMENT _AGENT_ERROR(22)
211#define AGENT_ERROR_INVALID_THREAD _AGENT_ERROR(23)
212#define AGENT_ERROR_INVALID_EVENT_TYPE _AGENT_ERROR(24)
213#define AGENT_ERROR_INVALID_OBJECT _AGENT_ERROR(25)
214#define AGENT_ERROR_NO_MORE_FRAMES _AGENT_ERROR(26)
215
216/* Combined event information */
217
218typedef struct {
219
220 EventIndex ei;
221 jthread thread;
222 jclass clazz;
223 jmethodID method;
224 jlocation location;
225 jobject object; /* possibly an exception or user object */
226
227 union {
228
229 /* ei = EI_FIELD_ACCESS */
230 struct {
231 jclass field_clazz;
232 jfieldID field;
233 } field_access;
234
235 /* ei = EI_FIELD_MODIFICATION */
236 struct {
237 jclass field_clazz;
238 jfieldID field;
239 char signature_type;
240 jvalue new_value;
241 } field_modification;
242
243 /* ei = EI_EXCEPTION */
244 struct {
245 jclass catch_clazz;
246 jmethodID catch_method;
247 jlocation catch_location;
248 } exception;
249
250 /* ei = EI_METHOD_EXIT */
251 struct {
252 jvalue return_value;
253 } method_exit;
254
255 /* For monitor wait events */
256 union {
257 /* ei = EI_MONITOR_WAIT */
258 jlong timeout;
259 /* ei = EI_MONITOR_WAITED */
260 jboolean timed_out;
261 } monitor;
262 } u;
263
264} EventInfo;
265
266/* Structure to hold dynamic array of objects */
267typedef struct ObjectBatch {
268 jobject *objects;
269 jint count;
270} ObjectBatch;
271
272/*
273 * JNI signature constants, beyond those defined in JDWP_TAG(*)
274 */
275#define SIGNATURE_BEGIN_ARGS '('
276#define SIGNATURE_END_ARGS ')'
277#define SIGNATURE_END_CLASS ';'
278
279/*
280 * Modifier flags for classes, fields, methods
281 */
282#define MOD_PUBLIC 0x0001 /* visible to everyone */
283#define MOD_PRIVATE 0x0002 /* visible only to the defining class */
284#define MOD_PROTECTED 0x0004 /* visible to subclasses */
285#define MOD_STATIC 0x0008 /* instance variable is static */
286#define MOD_FINAL 0x0010 /* no further subclassing, overriding */
287#define MOD_SYNCHRONIZED 0x0020 /* wrap method call in monitor lock */
288#define MOD_VOLATILE 0x0040 /* can cache in registers */
289#define MOD_TRANSIENT 0x0080 /* not persistant */
290#define MOD_NATIVE 0x0100 /* implemented in C */
291#define MOD_INTERFACE 0x0200 /* class is an interface */
292#define MOD_ABSTRACT 0x0400 /* no definition provided */
293/*
294 * Additional modifiers not defined as such in the JVM spec
295 */
296#define MOD_SYNTHETIC 0xf0000000 /* not in source code */
297
298/*
299 * jlong conversion macros
300 */
301#define jlong_zero ((jlong) 0)
302#define jlong_one ((jlong) 1)
303
304#define jlong_to_ptr(a) ((void*)(intptr_t)(a))
305#define ptr_to_jlong(a) ((jlong)(intptr_t)(a))
306#define jint_to_jlong(a) ((jlong)(a))
307#define jlong_to_jint(a) ((jint)(a))
308
309
310/*
311 * util funcs
312 */
313void util_initialize(JNIEnv *env);
314void util_reset(void);
315
316struct PacketInputStream;
317struct PacketOutputStream;
318
319jint uniqueID(void);
320jbyte referenceTypeTag(jclass clazz);
321jbyte specificTypeKey(JNIEnv *env, jobject object);
322jboolean isObjectTag(jbyte tag);
323jvmtiError spawnNewThread(jvmtiStartFunction func, void *arg, char *name);
324void convertSignatureToClassname(char *convert);
325void writeCodeLocation(struct PacketOutputStream *out, jclass clazz,
326 jmethodID method, jlocation location);
327
328jvmtiError classInstances(jclass klass, ObjectBatch *instances, int maxInstances);
329jvmtiError classInstanceCounts(jint classCount, jclass *classes, jlong *counts);
330jvmtiError objectReferrers(jobject obj, ObjectBatch *referrers, int maxObjects);
331
332/*
333 * Command handling helpers shared among multiple command sets
334 */
335int filterDebugThreads(jthread *threads, int count);
336
337
338void sharedGetFieldValues(struct PacketInputStream *in,
339 struct PacketOutputStream *out,
340 jboolean isStatic);
341jboolean sharedInvoke(struct PacketInputStream *in,
342 struct PacketOutputStream *out);
343
344jvmtiError fieldSignature(jclass, jfieldID, char **, char **, char **);
345jvmtiError fieldModifiers(jclass, jfieldID, jint *);
346jvmtiError methodSignature(jmethodID, char **, char **, char **);
347jvmtiError methodReturnType(jmethodID, char *);
348jvmtiError methodModifiers(jmethodID, jint *);
349jvmtiError methodClass(jmethodID, jclass *);
350jvmtiError methodLocation(jmethodID, jlocation*, jlocation*);
351jvmtiError classLoader(jclass, jobject *);
352
353/*
354 * Thin wrappers on top of JNI
355 */
356JNIEnv *getEnv(void);
357jboolean isClass(jobject object);
358jboolean isThread(jobject object);
359jboolean isThreadGroup(jobject object);
360jboolean isString(jobject object);
361jboolean isClassLoader(jobject object);
362jboolean isArray(jobject object);
363
364/*
365 * Thin wrappers on top of JVMTI
366 */
367jvmtiError jvmtiGetCapabilities(jvmtiCapabilities *caps);
368jint jvmtiMajorVersion(void);
369jint jvmtiMinorVersion(void);
370jint jvmtiMicroVersion(void);
371jvmtiError getSourceDebugExtension(jclass clazz, char **extensionPtr);
372jboolean canSuspendResumeThreadLists(void);
373
374jrawMonitorID debugMonitorCreate(char *name);
375void debugMonitorEnter(jrawMonitorID theLock);
376void debugMonitorExit(jrawMonitorID theLock);
377void debugMonitorWait(jrawMonitorID theLock);
378void debugMonitorTimedWait(jrawMonitorID theLock, jlong millis);
379void debugMonitorNotify(jrawMonitorID theLock);
380void debugMonitorNotifyAll(jrawMonitorID theLock);
381void debugMonitorDestroy(jrawMonitorID theLock);
382
383jthread *allThreads(jint *count);
384
385void threadGroupInfo(jthreadGroup, jvmtiThreadGroupInfo *info);
386
387char *getClassname(jclass);
388jvmtiError classSignature(jclass, char**, char**);
389jint classStatus(jclass);
390void writeGenericSignature(struct PacketOutputStream *, char *);
391jboolean isMethodNative(jmethodID);
392jboolean isMethodObsolete(jmethodID);
393jvmtiError isMethodSynthetic(jmethodID, jboolean*);
394jvmtiError isFieldSynthetic(jclass, jfieldID, jboolean*);
395
396jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2);
397
398jint objectHashCode(jobject);
399
400jvmtiError allInterfaces(jclass clazz, jclass **ppinterfaces, jint *count);
401jvmtiError allLoadedClasses(jclass **ppclasses, jint *count);
402jvmtiError allClassLoaderClasses(jobject loader, jclass **ppclasses, jint *count);
403jvmtiError allNestedClasses(jclass clazz, jclass **ppnested, jint *pcount);
404
405void setAgentPropertyValue(JNIEnv *env, char *propertyName, char* propertyValue);
406
407void *jvmtiAllocate(jint numBytes);
408void jvmtiDeallocate(void *buffer);
409
410void eventIndexInit(void);
411jdwpEvent eventIndex2jdwp(EventIndex i);
412jvmtiEvent eventIndex2jvmti(EventIndex i);
413EventIndex jdwp2EventIndex(jdwpEvent eventType);
414EventIndex jvmti2EventIndex(jvmtiEvent kind);
415
416jvmtiError map2jvmtiError(jdwpError);
417jdwpError map2jdwpError(jvmtiError);
418jdwpThreadStatus map2jdwpThreadStatus(jint state);
419jint map2jdwpSuspendStatus(jint state);
420jint map2jdwpClassStatus(jint);
421
422void log_debugee_location(const char *func,
423 jthread thread, jmethodID method, jlocation location);
424
425/*
426 * Local Reference management. The two macros below are used
427 * throughout the back end whenever space for JNI local references
428 * is needed in the current frame.
429 */
430
431void createLocalRefSpace(JNIEnv *env, jint capacity);
432
433#define WITH_LOCAL_REFS(env, number) \
434 createLocalRefSpace(env, number); \
435 { /* BEGINNING OF WITH SCOPE */
436
437#define END_WITH_LOCAL_REFS(env) \
438 JNI_FUNC_PTR(env,PopLocalFrame)(env, NULL); \
439 } /* END OF WITH SCOPE */
440
441void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj);
442void tossGlobalRef(JNIEnv *env, jobject *pobj);
443
444#endif