1 /* 2 * Copyright (c) 1998, 2015, 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 /* 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_class_path; /* UTF8 sun.boot.class.path */ 117 char* property_sun_boot_library_path; /* UTF8 sun.boot.library.path */ 118 char* property_path_separator; /* UTF8 path.separator */ 119 char* property_user_dir; /* UTF8 user.dir */ 120 121 unsigned log_flags; 122 123 /* Common References static data */ 124 jrawMonitorID refLock; 125 jlong nextSeqNum; 126 RefNode **objectsByID; 127 int objectsByIDsize; 128 int objectsByIDcount; 129 130 /* Indication that the agent has been loaded */ 131 jboolean isLoaded; 132 133 } BackendGlobalData; 134 135 extern BackendGlobalData * gdata; 136 137 /* 138 * Event Index for handlers 139 */ 140 141 typedef enum { 142 EI_min = 1, 143 144 EI_SINGLE_STEP = 1, 145 EI_BREAKPOINT = 2, 146 EI_FRAME_POP = 3, 147 EI_EXCEPTION = 4, 148 EI_THREAD_START = 5, 149 EI_THREAD_END = 6, 150 EI_CLASS_PREPARE = 7, 151 EI_GC_FINISH = 8, 152 EI_CLASS_LOAD = 9, 153 EI_FIELD_ACCESS = 10, 154 EI_FIELD_MODIFICATION = 11, 155 EI_EXCEPTION_CATCH = 12, 156 EI_METHOD_ENTRY = 13, 157 EI_METHOD_EXIT = 14, 158 EI_MONITOR_CONTENDED_ENTER = 15, 159 EI_MONITOR_CONTENDED_ENTERED = 16, 160 EI_MONITOR_WAIT = 17, 161 EI_MONITOR_WAITED = 18, 162 EI_VM_INIT = 19, 163 EI_VM_DEATH = 20, 164 EI_max = 20 165 } EventIndex; 166 167 /* Agent errors that might be in a jvmtiError for JDWP or internal. 168 * (Done this way so that compiler allows it's use as a jvmtiError) 169 */ 170 #define _AGENT_ERROR(x) ((jvmtiError)(JVMTI_ERROR_MAX+64+x)) 171 #define AGENT_ERROR_INTERNAL _AGENT_ERROR(1) 172 #define AGENT_ERROR_VM_DEAD _AGENT_ERROR(2) 173 #define AGENT_ERROR_NO_JNI_ENV _AGENT_ERROR(3) 174 #define AGENT_ERROR_JNI_EXCEPTION _AGENT_ERROR(4) 175 #define AGENT_ERROR_JVMTI_INTERNAL _AGENT_ERROR(5) 176 #define AGENT_ERROR_JDWP_INTERNAL _AGENT_ERROR(6) 177 #define AGENT_ERROR_NOT_CURRENT_FRAME _AGENT_ERROR(7) 178 #define AGENT_ERROR_OUT_OF_MEMORY _AGENT_ERROR(8) 179 #define AGENT_ERROR_INVALID_TAG _AGENT_ERROR(9) 180 #define AGENT_ERROR_ALREADY_INVOKING _AGENT_ERROR(10) 181 #define AGENT_ERROR_INVALID_INDEX _AGENT_ERROR(11) 182 #define AGENT_ERROR_INVALID_LENGTH _AGENT_ERROR(12) 183 #define AGENT_ERROR_INVALID_STRING _AGENT_ERROR(13) 184 #define AGENT_ERROR_INVALID_CLASS_LOADER _AGENT_ERROR(14) 185 #define AGENT_ERROR_INVALID_ARRAY _AGENT_ERROR(15) 186 #define AGENT_ERROR_TRANSPORT_LOAD _AGENT_ERROR(16) 187 #define AGENT_ERROR_TRANSPORT_INIT _AGENT_ERROR(17) 188 #define AGENT_ERROR_NATIVE_METHOD _AGENT_ERROR(18) 189 #define AGENT_ERROR_INVALID_COUNT _AGENT_ERROR(19) 190 #define AGENT_ERROR_INVALID_FRAMEID _AGENT_ERROR(20) 191 #define AGENT_ERROR_NULL_POINTER _AGENT_ERROR(21) 192 #define AGENT_ERROR_ILLEGAL_ARGUMENT _AGENT_ERROR(22) 193 #define AGENT_ERROR_INVALID_THREAD _AGENT_ERROR(23) 194 #define AGENT_ERROR_INVALID_EVENT_TYPE _AGENT_ERROR(24) 195 #define AGENT_ERROR_INVALID_OBJECT _AGENT_ERROR(25) 196 #define AGENT_ERROR_NO_MORE_FRAMES _AGENT_ERROR(26) 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 char *getClassname(jclass); 358 jvmtiError classSignature(jclass, char**, char**); 359 jint classStatus(jclass); 360 void writeGenericSignature(struct PacketOutputStream *, char *); 361 jboolean isMethodNative(jmethodID); 362 jboolean isMethodObsolete(jmethodID); 363 jvmtiError isMethodSynthetic(jmethodID, jboolean*); 364 jvmtiError isFieldSynthetic(jclass, jfieldID, jboolean*); 365 366 jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2); 367 368 jint objectHashCode(jobject); 369 370 jvmtiError allInterfaces(jclass clazz, jclass **ppinterfaces, jint *count); 371 jvmtiError allLoadedClasses(jclass **ppclasses, jint *count); 372 jvmtiError allClassLoaderClasses(jobject loader, jclass **ppclasses, jint *count); 373 jvmtiError allNestedClasses(jclass clazz, jclass **ppnested, jint *pcount); 374 375 void setAgentPropertyValue(JNIEnv *env, char *propertyName, char* propertyValue); 376 377 void *jvmtiAllocate(jint numBytes); 378 void jvmtiDeallocate(void *buffer); 379 380 void eventIndexInit(void); 381 jdwpEvent eventIndex2jdwp(EventIndex i); 382 jvmtiEvent eventIndex2jvmti(EventIndex i); 383 EventIndex jdwp2EventIndex(jdwpEvent eventType); 384 EventIndex jvmti2EventIndex(jvmtiEvent kind); 385 386 jvmtiError map2jvmtiError(jdwpError); 387 jdwpError map2jdwpError(jvmtiError); 388 jdwpThreadStatus map2jdwpThreadStatus(jint state); 389 jint map2jdwpSuspendStatus(jint state); 390 jint map2jdwpClassStatus(jint); 391 392 void log_debugee_location(const char *func, 393 jthread thread, jmethodID method, jlocation location); 394 395 /* 396 * Local Reference management. The two macros below are used 397 * throughout the back end whenever space for JNI local references 398 * is needed in the current frame. 399 */ 400 401 void createLocalRefSpace(JNIEnv *env, jint capacity); 402 403 #define WITH_LOCAL_REFS(env, number) \ 404 createLocalRefSpace(env, number); \ 405 { /* BEGINNING OF WITH SCOPE */ 406 407 #define END_WITH_LOCAL_REFS(env) \ 408 JNI_FUNC_PTR(env,PopLocalFrame)(env, NULL); \ 409 } /* END OF WITH SCOPE */ 410 411 void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj); 412 void tossGlobalRef(JNIEnv *env, jobject *pobj); 413 414 #endif