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