1 /* 2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 #ifndef HPROF_UTIL_H 42 #define HPROF_UTIL_H 43 44 /* Macros that protect code from accidently using a local ref improperly */ 45 #define WITH_LOCAL_REFS(env, number) \ 46 { \ 47 JNIEnv *_env = (env); \ 48 pushLocalFrame(_env, number); \ 49 { /* BEGINNING OF WITH SCOPE */ 50 51 #define END_WITH_LOCAL_REFS \ 52 } /* END OF WITH SCOPE */ \ 53 popLocalFrame(_env, NULL); \ 54 } 55 56 /* Macro to check for exceptions after JNI calls. */ 57 #define CHECK_EXCEPTIONS(env) \ 58 { \ 59 JNIEnv *_env = (env); \ 60 jobject _exception; \ 61 _exception = exceptionOccurred(_env); \ 62 if ( _exception != NULL ) { \ 63 exceptionDescribe(_env); \ 64 HPROF_ERROR(JNI_TRUE, "Unexpected Exception found beforehand");\ 65 } \ 66 { 67 68 #define END_CHECK_EXCEPTIONS \ 69 } \ 70 _exception = exceptionOccurred(_env); \ 71 if ( _exception != NULL ) { \ 72 exceptionDescribe(_env); \ 73 HPROF_ERROR(JNI_TRUE, "Unexpected Exception found afterward");\ 74 } \ 75 } 76 77 JNIEnv * getEnv(void); 78 79 /* JNI support functions */ 80 jobject newGlobalReference(JNIEnv *env, jobject object); 81 jobject newWeakGlobalReference(JNIEnv *env, jobject object); 82 void deleteGlobalReference(JNIEnv *env, jobject object); 83 jobject newLocalReference(JNIEnv *env, jobject object); 84 void deleteLocalReference(JNIEnv *env, jobject object); 85 void deleteWeakGlobalReference(JNIEnv *env, jobject object); 86 jclass getObjectClass(JNIEnv *env, jobject object); 87 jmethodID getMethodID(JNIEnv *env, jclass clazz, const char* name, 88 const char *sig); 89 jclass getSuperclass(JNIEnv *env, jclass klass); 90 jmethodID getStaticMethodID(JNIEnv *env, jclass clazz, const char* name, 91 const char *sig); 92 jfieldID getStaticFieldID(JNIEnv *env, jclass clazz, const char* name, 93 const char *sig); 94 jclass findClass(JNIEnv *env, const char *name); 95 void setStaticIntField(JNIEnv *env, jclass clazz, jfieldID field, 96 jint value); 97 jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2); 98 void pushLocalFrame(JNIEnv *env, jint capacity); 99 void popLocalFrame(JNIEnv *env, jobject ret); 100 jobject exceptionOccurred(JNIEnv *env); 101 void exceptionDescribe(JNIEnv *env); 102 void exceptionClear(JNIEnv *env); 103 void registerNatives(JNIEnv *env, jclass clazz, 104 JNINativeMethod *methods, jint count); 105 106 /* More JVMTI support functions */ 107 char * getErrorName(jvmtiError error_number); 108 jvmtiPhase getPhase(void); 109 char * phaseString(jvmtiPhase phase); 110 void disposeEnvironment(void); 111 jlong getObjectSize(jobject object); 112 jobject getClassLoader(jclass klass); 113 jint getClassStatus(jclass klass); 114 jlong getTag(jobject object); 115 void setTag(jobject object, jlong tag); 116 void getObjectMonitorUsage(jobject object, jvmtiMonitorUsage *uinfo); 117 void getOwnedMonitorInfo(jthread thread, jobject **ppobjects, 118 jint *pcount); 119 void getSystemProperty(const char *name, char **value); 120 void getClassSignature(jclass klass, char**psignature, 121 char **pgeneric_signature); 122 void getSourceFileName(jclass klass, char** src_name_ptr); 123 124 jvmtiPrimitiveType sigToPrimType(char *sig); 125 int sigToPrimSize(char *sig); 126 char primTypeToSigChar(jvmtiPrimitiveType primType); 127 128 void getAllClassFieldInfo(JNIEnv *env, jclass klass, 129 jint* field_count_ptr, FieldInfo** fields_ptr); 130 void getMethodName(jmethodID method, char** name_ptr, 131 char** signature_ptr); 132 void getMethodClass(jmethodID method, jclass *pclazz); 133 jboolean isMethodNative(jmethodID method); 134 void getPotentialCapabilities(jvmtiCapabilities *capabilities); 135 void addCapabilities(jvmtiCapabilities *capabilities); 136 void setEventCallbacks(jvmtiEventCallbacks *pcallbacks); 137 void setEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event, 138 jthread thread); 139 void * getThreadLocalStorage(jthread thread); 140 void setThreadLocalStorage(jthread thread, void *ptr); 141 void getThreadState(jthread thread, jint *threadState); 142 void getThreadInfo(jthread thread, jvmtiThreadInfo *info); 143 void getThreadGroupInfo(jthreadGroup thread_group, jvmtiThreadGroupInfo *info); 144 void getLoadedClasses(jclass **ppclasses, jint *pcount); 145 jint getLineNumber(jmethodID method, jlocation location); 146 jlong getMaxMemory(JNIEnv *env); 147 void createAgentThread(JNIEnv *env, const char *name, 148 jvmtiStartFunction func); 149 jlong getThreadCpuTime(jthread thread); 150 void getStackTrace(jthread thread, jvmtiFrameInfo *pframes, jint depth, 151 jint *pcount); 152 void getThreadListStackTraces(jint count, jthread *threads, 153 jint depth, jvmtiStackInfo **stack_info); 154 void getFrameCount(jthread thread, jint *pcount); 155 void followReferences(jvmtiHeapCallbacks *pHeapCallbacks, void *user_data); 156 157 /* GC control */ 158 void runGC(void); 159 160 /* Get initial JVMTI environment */ 161 void getJvmti(void); 162 163 /* Get current runtime JVMTI version */ 164 jint jvmtiVersion(void); 165 166 /* Raw monitor functions */ 167 jrawMonitorID createRawMonitor(const char *str); 168 void rawMonitorEnter(jrawMonitorID m); 169 void rawMonitorWait(jrawMonitorID m, jlong pause_time); 170 void rawMonitorNotifyAll(jrawMonitorID m); 171 void rawMonitorExit(jrawMonitorID m); 172 void destroyRawMonitor(jrawMonitorID m); 173 174 /* JVMTI alloc/dealloc */ 175 void * jvmtiAllocate(int size); 176 void jvmtiDeallocate(void *ptr); 177 178 /* System malloc/free */ 179 void * hprof_malloc(int size); 180 void hprof_free(void *ptr); 181 182 #include "debug_malloc.h" 183 184 #ifdef DEBUG 185 void * hprof_debug_malloc(int size, char *file, int line); 186 void hprof_debug_free(void *ptr, char *file, int line); 187 #define HPROF_MALLOC(size) hprof_debug_malloc(size, __FILE__, __LINE__) 188 #define HPROF_FREE(ptr) hprof_debug_free(ptr, __FILE__, __LINE__) 189 #else 190 #define HPROF_MALLOC(size) hprof_malloc(size) 191 #define HPROF_FREE(ptr) hprof_free(ptr) 192 #endif 193 194 #endif