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