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