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