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