Home | History | Annotate | Download | only in back
      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 /* Definition of a CommonRef tracked by the backend for the frontend */
     60 typedef 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 
     75 typedef jint FrameNumber;
     76 
     77 // ANDROID-CHANGED: support for DDMS extension apis.
     78 typedef 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 
     86 typedef 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 
    148      /* ANDROID-CHANGED: com.android.art.internal.ddm.process_chunk extension function */
    149      DdmProcessChunk ddm_process_chunk;
    150 
    151      /* ANDROID-CHANGED: Need to keep track of if ddm is initially active. */
    152      jboolean ddmInitiallyActive;
    153 
    154 } BackendGlobalData;
    155 
    156 extern BackendGlobalData * gdata;
    157 
    158 /*
    159  * Event Index for handlers
    160  */
    161 
    162 typedef enum {
    163         EI_min                  =  1,
    164 
    165         EI_SINGLE_STEP          =  1,
    166         EI_BREAKPOINT           =  2,
    167         EI_FRAME_POP            =  3,
    168         EI_EXCEPTION            =  4,
    169         EI_THREAD_START         =  5,
    170         EI_THREAD_END           =  6,
    171         EI_CLASS_PREPARE        =  7,
    172         EI_GC_FINISH            =  8,
    173         EI_CLASS_LOAD           =  9,
    174         EI_FIELD_ACCESS         = 10,
    175         EI_FIELD_MODIFICATION   = 11,
    176         EI_EXCEPTION_CATCH      = 12,
    177         EI_METHOD_ENTRY         = 13,
    178         EI_METHOD_EXIT          = 14,
    179         EI_MONITOR_CONTENDED_ENTER = 15,
    180         EI_MONITOR_CONTENDED_ENTERED = 16,
    181         EI_MONITOR_WAIT         = 17,
    182         EI_MONITOR_WAITED       = 18,
    183         EI_VM_INIT              = 19,
    184         EI_VM_DEATH             = 20,
    185         EI_max                  = 20
    186 } EventIndex;
    187 
    188 /* Agent errors that might be in a jvmtiError for JDWP or internal.
    189  *    (Done this way so that compiler allows it's use as a jvmtiError)
    190  */
    191 #define _AGENT_ERROR(x)                 ((jvmtiError)(JVMTI_ERROR_MAX+64+x))
    192 #define AGENT_ERROR_INTERNAL                    _AGENT_ERROR(1)
    193 #define AGENT_ERROR_VM_DEAD                     _AGENT_ERROR(2)
    194 #define AGENT_ERROR_NO_JNI_ENV                  _AGENT_ERROR(3)
    195 #define AGENT_ERROR_JNI_EXCEPTION               _AGENT_ERROR(4)
    196 #define AGENT_ERROR_JVMTI_INTERNAL              _AGENT_ERROR(5)
    197 #define AGENT_ERROR_JDWP_INTERNAL               _AGENT_ERROR(6)
    198 #define AGENT_ERROR_NOT_CURRENT_FRAME           _AGENT_ERROR(7)
    199 #define AGENT_ERROR_OUT_OF_MEMORY               _AGENT_ERROR(8)
    200 #define AGENT_ERROR_INVALID_TAG                 _AGENT_ERROR(9)
    201 #define AGENT_ERROR_ALREADY_INVOKING            _AGENT_ERROR(10)
    202 #define AGENT_ERROR_INVALID_INDEX               _AGENT_ERROR(11)
    203 #define AGENT_ERROR_INVALID_LENGTH              _AGENT_ERROR(12)
    204 #define AGENT_ERROR_INVALID_STRING              _AGENT_ERROR(13)
    205 #define AGENT_ERROR_INVALID_CLASS_LOADER        _AGENT_ERROR(14)
    206 #define AGENT_ERROR_INVALID_ARRAY               _AGENT_ERROR(15)
    207 #define AGENT_ERROR_TRANSPORT_LOAD              _AGENT_ERROR(16)
    208 #define AGENT_ERROR_TRANSPORT_INIT              _AGENT_ERROR(17)
    209 #define AGENT_ERROR_NATIVE_METHOD               _AGENT_ERROR(18)
    210 #define AGENT_ERROR_INVALID_COUNT               _AGENT_ERROR(19)
    211 #define AGENT_ERROR_INVALID_FRAMEID             _AGENT_ERROR(20)
    212 #define AGENT_ERROR_NULL_POINTER                _AGENT_ERROR(21)
    213 #define AGENT_ERROR_ILLEGAL_ARGUMENT            _AGENT_ERROR(22)
    214 #define AGENT_ERROR_INVALID_THREAD              _AGENT_ERROR(23)
    215 #define AGENT_ERROR_INVALID_EVENT_TYPE          _AGENT_ERROR(24)
    216 #define AGENT_ERROR_INVALID_OBJECT              _AGENT_ERROR(25)
    217 #define AGENT_ERROR_NO_MORE_FRAMES              _AGENT_ERROR(26)
    218 
    219 /* Combined event information */
    220 
    221 typedef struct {
    222 
    223     EventIndex  ei;
    224     jthread     thread;
    225     jclass      clazz;
    226     jmethodID   method;
    227     jlocation   location;
    228     jobject     object; /* possibly an exception or user object */
    229 
    230     union {
    231 
    232         /* ei = EI_FIELD_ACCESS */
    233         struct {
    234             jclass      field_clazz;
    235             jfieldID    field;
    236         } field_access;
    237 
    238         /* ei = EI_FIELD_MODIFICATION */
    239         struct {
    240             jclass      field_clazz;
    241             jfieldID    field;
    242             char        signature_type;
    243             jvalue      new_value;
    244         } field_modification;
    245 
    246         /* ei = EI_EXCEPTION */
    247         struct {
    248             jclass      catch_clazz;
    249             jmethodID   catch_method;
    250             jlocation   catch_location;
    251         } exception;
    252 
    253         /* ei = EI_METHOD_EXIT */
    254         struct {
    255             jvalue      return_value;
    256         } method_exit;
    257 
    258         /* For monitor wait events */
    259         union {
    260             /* ei = EI_MONITOR_WAIT */
    261             jlong timeout;
    262             /* ei = EI_MONITOR_WAITED */
    263             jboolean timed_out;
    264         } monitor;
    265     } u;
    266 
    267 } EventInfo;
    268 
    269 /* Structure to hold dynamic array of objects */
    270 typedef struct ObjectBatch {
    271     jobject *objects;
    272     jint     count;
    273 } ObjectBatch;
    274 
    275 /*
    276  * JNI signature constants, beyond those defined in JDWP_TAG(*)
    277  */
    278 #define SIGNATURE_BEGIN_ARGS    '('
    279 #define SIGNATURE_END_ARGS      ')'
    280 #define SIGNATURE_END_CLASS     ';'
    281 
    282 /*
    283  * Modifier flags for classes, fields, methods
    284  */
    285 #define MOD_PUBLIC       0x0001     /* visible to everyone */
    286 #define MOD_PRIVATE      0x0002     /* visible only to the defining class */
    287 #define MOD_PROTECTED    0x0004     /* visible to subclasses */
    288 #define MOD_STATIC       0x0008     /* instance variable is static */
    289 #define MOD_FINAL        0x0010     /* no further subclassing, overriding */
    290 #define MOD_SYNCHRONIZED 0x0020     /* wrap method call in monitor lock */
    291 #define MOD_VOLATILE     0x0040     /* can cache in registers */
    292 #define MOD_TRANSIENT    0x0080     /* not persistant */
    293 #define MOD_NATIVE       0x0100     /* implemented in C */
    294 #define MOD_INTERFACE    0x0200     /* class is an interface */
    295 #define MOD_ABSTRACT     0x0400     /* no definition provided */
    296 /*
    297  * Additional modifiers not defined as such in the JVM spec
    298  */
    299 #define MOD_SYNTHETIC    0xf0000000  /* not in source code */
    300 
    301 /*
    302  * jlong conversion macros
    303  */
    304 #define jlong_zero       ((jlong) 0)
    305 #define jlong_one        ((jlong) 1)
    306 
    307 #define jlong_to_ptr(a)  ((void*)(intptr_t)(a))
    308 #define ptr_to_jlong(a)  ((jlong)(intptr_t)(a))
    309 #define jint_to_jlong(a) ((jlong)(a))
    310 #define jlong_to_jint(a) ((jint)(a))
    311 
    312 
    313 /*
    314  * util funcs
    315  */
    316 void util_initialize(JNIEnv *env);
    317 void util_reset(void);
    318 
    319 struct PacketInputStream;
    320 struct PacketOutputStream;
    321 
    322 jint uniqueID(void);
    323 jbyte referenceTypeTag(jclass clazz);
    324 jbyte specificTypeKey(JNIEnv *env, jobject object);
    325 jboolean isObjectTag(jbyte tag);
    326 jvmtiError spawnNewThread(jvmtiStartFunction func, void *arg, char *name);
    327 void convertSignatureToClassname(char *convert);
    328 void writeCodeLocation(struct PacketOutputStream *out, jclass clazz,
    329                        jmethodID method, jlocation location);
    330 
    331 jvmtiError classInstances(jclass klass, ObjectBatch *instances, int maxInstances);
    332 jvmtiError classInstanceCounts(jint classCount, jclass *classes, jlong *counts);
    333 jvmtiError objectReferrers(jobject obj, ObjectBatch *referrers, int maxObjects);
    334 
    335 // ANDROID-CHANGED: Helper function to get current time in milliseconds on CLOCK_MONOTONIC
    336 jlong milliTime(void);
    337 
    338 /*
    339  * Command handling helpers shared among multiple command sets
    340  */
    341 int filterDebugThreads(jthread *threads, int count);
    342 
    343 
    344 void sharedGetFieldValues(struct PacketInputStream *in,
    345                           struct PacketOutputStream *out,
    346                           jboolean isStatic);
    347 jboolean sharedInvoke(struct PacketInputStream *in,
    348                       struct PacketOutputStream *out);
    349 
    350 jvmtiError fieldSignature(jclass, jfieldID, char **, char **, char **);
    351 jvmtiError fieldModifiers(jclass, jfieldID, jint *);
    352 jvmtiError methodSignature(jmethodID, char **, char **, char **);
    353 jvmtiError methodReturnType(jmethodID, char *);
    354 jvmtiError methodModifiers(jmethodID, jint *);
    355 jvmtiError methodClass(jmethodID, jclass *);
    356 jvmtiError methodLocation(jmethodID, jlocation*, jlocation*);
    357 jvmtiError classLoader(jclass, jobject *);
    358 
    359 /*
    360  * Thin wrappers on top of JNI
    361  */
    362 JNIEnv *getEnv(void);
    363 jboolean isClass(jobject object);
    364 jboolean isThread(jobject object);
    365 jboolean isThreadGroup(jobject object);
    366 jboolean isString(jobject object);
    367 jboolean isClassLoader(jobject object);
    368 jboolean isArray(jobject object);
    369 
    370 /*
    371  * Thin wrappers on top of JVMTI
    372  */
    373 jvmtiError jvmtiGetCapabilities(jvmtiCapabilities *caps);
    374 jint jvmtiMajorVersion(void);
    375 jint jvmtiMinorVersion(void);
    376 jint jvmtiMicroVersion(void);
    377 jvmtiError getSourceDebugExtension(jclass clazz, char **extensionPtr);
    378 jboolean canSuspendResumeThreadLists(void);
    379 
    380 jrawMonitorID debugMonitorCreate(char *name);
    381 void debugMonitorEnter(jrawMonitorID theLock);
    382 void debugMonitorExit(jrawMonitorID theLock);
    383 void debugMonitorWait(jrawMonitorID theLock);
    384 void debugMonitorTimedWait(jrawMonitorID theLock, jlong millis);
    385 void debugMonitorNotify(jrawMonitorID theLock);
    386 void debugMonitorNotifyAll(jrawMonitorID theLock);
    387 void debugMonitorDestroy(jrawMonitorID theLock);
    388 
    389 jthread *allThreads(jint *count);
    390 
    391 void threadGroupInfo(jthreadGroup, jvmtiThreadGroupInfo *info);
    392 
    393 char *getClassname(jclass);
    394 jvmtiError classSignature(jclass, char**, char**);
    395 jint classStatus(jclass);
    396 void writeGenericSignature(struct PacketOutputStream *, char *);
    397 jboolean isMethodNative(jmethodID);
    398 jboolean isMethodObsolete(jmethodID);
    399 jvmtiError isMethodSynthetic(jmethodID, jboolean*);
    400 jvmtiError isFieldSynthetic(jclass, jfieldID, jboolean*);
    401 
    402 jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2);
    403 
    404 jint objectHashCode(jobject);
    405 
    406 jvmtiError allInterfaces(jclass clazz, jclass **ppinterfaces, jint *count);
    407 jvmtiError allLoadedClasses(jclass **ppclasses, jint *count);
    408 jvmtiError allClassLoaderClasses(jobject loader, jclass **ppclasses, jint *count);
    409 jvmtiError allNestedClasses(jclass clazz, jclass **ppnested, jint *pcount);
    410 
    411 void setAgentPropertyValue(JNIEnv *env, char *propertyName, char* propertyValue);
    412 
    413 void *jvmtiAllocate(jint numBytes);
    414 void jvmtiDeallocate(void *buffer);
    415 
    416 void             eventIndexInit(void);
    417 jdwpEvent        eventIndex2jdwp(EventIndex i);
    418 jvmtiEvent       eventIndex2jvmti(EventIndex i);
    419 EventIndex       jdwp2EventIndex(jdwpEvent eventType);
    420 EventIndex       jvmti2EventIndex(jvmtiEvent kind);
    421 
    422 jvmtiError       map2jvmtiError(jdwpError);
    423 jdwpError        map2jdwpError(jvmtiError);
    424 jdwpThreadStatus map2jdwpThreadStatus(jint state);
    425 jint             map2jdwpSuspendStatus(jint state);
    426 jint             map2jdwpClassStatus(jint);
    427 
    428 void log_debugee_location(const char *func,
    429                 jthread thread, jmethodID method, jlocation location);
    430 
    431 /*
    432  * Local Reference management. The two macros below are used
    433  * throughout the back end whenever space for JNI local references
    434  * is needed in the current frame.
    435  */
    436 
    437 void createLocalRefSpace(JNIEnv *env, jint capacity);
    438 
    439 #define WITH_LOCAL_REFS(env, number) \
    440     createLocalRefSpace(env, number); \
    441     { /* BEGINNING OF WITH SCOPE */
    442 
    443 #define END_WITH_LOCAL_REFS(env) \
    444         JNI_FUNC_PTR(env,PopLocalFrame)(env, NULL); \
    445     } /* END OF WITH SCOPE */
    446 
    447 void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj);
    448 void tossGlobalRef(JNIEnv *env, jobject *pobj);
    449 
    450 /* ANDROID_CHANGED: Expose this method publicly.
    451  * This returns a newly allocated jvmtiEnv* with the can_tag_objects capability.
    452  */
    453 jvmtiEnv *getSpecialJvmti(void);
    454 
    455 #endif
    456