1 /* 2 * Copyright (c) 1998, 2014, 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 * jlong conversion macros 282 */ 283 #define jlong_zero ((jlong) 0) 284 #define jlong_one ((jlong) 1) 285 286 #define jlong_to_ptr(a) ((void*)(intptr_t)(a)) 287 #define ptr_to_jlong(a) ((jlong)(intptr_t)(a)) 288 #define jint_to_jlong(a) ((jlong)(a)) 289 #define jlong_to_jint(a) ((jint)(a)) 290 291 292 /* 293 * util funcs 294 */ 295 void util_initialize(JNIEnv *env); 296 void util_reset(void); 297 298 struct PacketInputStream; 299 struct PacketOutputStream; 300 301 jint uniqueID(void); 302 jbyte referenceTypeTag(jclass clazz); 303 jbyte specificTypeKey(JNIEnv *env, jobject object); 304 jboolean isObjectTag(jbyte tag); 305 jvmtiError spawnNewThread(jvmtiStartFunction func, void *arg, char *name); 306 void convertSignatureToClassname(char *convert); 307 void writeCodeLocation(struct PacketOutputStream *out, jclass clazz, 308 jmethodID method, jlocation location); 309 310 jvmtiError classInstances(jclass klass, ObjectBatch *instances, int maxInstances); 311 jvmtiError classInstanceCounts(jint classCount, jclass *classes, jlong *counts); 312 jvmtiError objectReferrers(jobject obj, ObjectBatch *referrers, int maxObjects); 313 314 /* 315 * Command handling helpers shared among multiple command sets 316 */ 317 int filterDebugThreads(jthread *threads, int count); 318 319 320 void sharedGetFieldValues(struct PacketInputStream *in, 321 struct PacketOutputStream *out, 322 jboolean isStatic); 323 jboolean sharedInvoke(struct PacketInputStream *in, 324 struct PacketOutputStream *out); 325 326 jvmtiError fieldSignature(jclass, jfieldID, char **, char **, char **); 327 jvmtiError fieldModifiers(jclass, jfieldID, jint *); 328 jvmtiError methodSignature(jmethodID, char **, char **, char **); 329 jvmtiError methodReturnType(jmethodID, char *); 330 jvmtiError methodModifiers(jmethodID, jint *); 331 jvmtiError methodClass(jmethodID, jclass *); 332 jvmtiError methodLocation(jmethodID, jlocation*, jlocation*); 333 jvmtiError classLoader(jclass, jobject *); 334 335 /* 336 * Thin wrappers on top of JNI 337 */ 338 JNIEnv *getEnv(void); 339 jboolean isClass(jobject object); 340 jboolean isThread(jobject object); 341 jboolean isThreadGroup(jobject object); 342 jboolean isString(jobject object); 343 jboolean isClassLoader(jobject object); 344 jboolean isArray(jobject object); 345 346 /* 347 * Thin wrappers on top of JVMTI 348 */ 349 jvmtiError jvmtiGetCapabilities(jvmtiCapabilities *caps); 350 jint jvmtiMajorVersion(void); 351 jint jvmtiMinorVersion(void); 352 jint jvmtiMicroVersion(void); 353 jvmtiError getSourceDebugExtension(jclass clazz, char **extensionPtr); 354 jboolean canSuspendResumeThreadLists(void); 355 356 jrawMonitorID debugMonitorCreate(char *name); 357 void debugMonitorEnter(jrawMonitorID theLock); 358 void debugMonitorExit(jrawMonitorID theLock); 359 void debugMonitorWait(jrawMonitorID theLock); 360 void debugMonitorTimedWait(jrawMonitorID theLock, jlong millis); 361 void debugMonitorNotify(jrawMonitorID theLock); 362 void debugMonitorNotifyAll(jrawMonitorID theLock); 363 void debugMonitorDestroy(jrawMonitorID theLock); 364 365 jthread *allThreads(jint *count); 366 367 void threadGroupInfo(jthreadGroup, jvmtiThreadGroupInfo *info); 368 369 char *getClassname(jclass); 370 jvmtiError classSignature(jclass, char**, char**); 371 jint classStatus(jclass); 372 void writeGenericSignature(struct PacketOutputStream *, char *); 373 jboolean isMethodNative(jmethodID); 374 jboolean isMethodObsolete(jmethodID); 375 jvmtiError isMethodSynthetic(jmethodID, jboolean*); 376 jvmtiError isFieldSynthetic(jclass, jfieldID, jboolean*); 377 378 jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2); 379 380 jint objectHashCode(jobject); 381 382 jvmtiError allInterfaces(jclass clazz, jclass **ppinterfaces, jint *count); 383 jvmtiError allLoadedClasses(jclass **ppclasses, jint *count); 384 jvmtiError allClassLoaderClasses(jobject loader, jclass **ppclasses, jint *count); 385 jvmtiError allNestedClasses(jclass clazz, jclass **ppnested, jint *pcount); 386 387 void setAgentPropertyValue(JNIEnv *env, char *propertyName, char* propertyValue); 388 389 void *jvmtiAllocate(jint numBytes); 390 void jvmtiDeallocate(void *buffer); 391 392 void eventIndexInit(void); 393 jdwpEvent eventIndex2jdwp(EventIndex i); 394 jvmtiEvent eventIndex2jvmti(EventIndex i); 395 EventIndex jdwp2EventIndex(jdwpEvent eventType); 396 EventIndex jvmti2EventIndex(jvmtiEvent kind); 397 398 jvmtiError map2jvmtiError(jdwpError); 399 jdwpError map2jdwpError(jvmtiError); 400 jdwpThreadStatus map2jdwpThreadStatus(jint state); 401 jint map2jdwpSuspendStatus(jint state); 402 jint map2jdwpClassStatus(jint); 403 404 void log_debugee_location(const char *func, 405 jthread thread, jmethodID method, jlocation location); 406 407 /* 408 * Local Reference management. The two macros below are used 409 * throughout the back end whenever space for JNI local references 410 * is needed in the current frame. 411 */ 412 413 void createLocalRefSpace(JNIEnv *env, jint capacity); 414 415 #define WITH_LOCAL_REFS(env, number) \ 416 createLocalRefSpace(env, number); \ 417 { /* BEGINNING OF WITH SCOPE */ 418 419 #define END_WITH_LOCAL_REFS(env) \ 420 JNI_FUNC_PTR(env,PopLocalFrame)(env, NULL); \ 421 } /* END OF WITH SCOPE */ 422 423 void saveGlobalRef(JNIEnv *env, jobject obj, jobject *pobj); 424 void tossGlobalRef(JNIEnv *env, jobject *pobj); 425 426 #endif