1 /* |
|
2 * Copyright (c) 2003, 2012, 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 /* Use THIS_FILE when it is available. */ |
|
45 #ifndef THIS_FILE |
|
46 #define THIS_FILE __FILE__ |
|
47 #endif |
|
48 |
|
49 /* Macros that protect code from accidently using a local ref improperly */ |
|
50 #define WITH_LOCAL_REFS(env, number) \ |
|
51 { \ |
|
52 JNIEnv *_env = (env); \ |
|
53 pushLocalFrame(_env, number); \ |
|
54 { /* BEGINNING OF WITH SCOPE */ |
|
55 |
|
56 #define END_WITH_LOCAL_REFS \ |
|
57 } /* END OF WITH SCOPE */ \ |
|
58 popLocalFrame(_env, NULL); \ |
|
59 } |
|
60 |
|
61 /* Macro to check for exceptions after JNI calls. */ |
|
62 #define CHECK_EXCEPTIONS(env) \ |
|
63 { \ |
|
64 JNIEnv *_env = (env); \ |
|
65 jobject _exception; \ |
|
66 _exception = exceptionOccurred(_env); \ |
|
67 if ( _exception != NULL ) { \ |
|
68 exceptionDescribe(_env); \ |
|
69 HPROF_ERROR(JNI_TRUE, "Unexpected Exception found beforehand");\ |
|
70 } \ |
|
71 { |
|
72 |
|
73 #define END_CHECK_EXCEPTIONS \ |
|
74 } \ |
|
75 _exception = exceptionOccurred(_env); \ |
|
76 if ( _exception != NULL ) { \ |
|
77 exceptionDescribe(_env); \ |
|
78 HPROF_ERROR(JNI_TRUE, "Unexpected Exception found afterward");\ |
|
79 } \ |
|
80 } |
|
81 |
|
82 JNIEnv * getEnv(void); |
|
83 |
|
84 /* JNI support functions */ |
|
85 jobject newGlobalReference(JNIEnv *env, jobject object); |
|
86 jobject newWeakGlobalReference(JNIEnv *env, jobject object); |
|
87 void deleteGlobalReference(JNIEnv *env, jobject object); |
|
88 jobject newLocalReference(JNIEnv *env, jobject object); |
|
89 void deleteLocalReference(JNIEnv *env, jobject object); |
|
90 void deleteWeakGlobalReference(JNIEnv *env, jobject object); |
|
91 jclass getObjectClass(JNIEnv *env, jobject object); |
|
92 jmethodID getMethodID(JNIEnv *env, jclass clazz, const char* name, |
|
93 const char *sig); |
|
94 jclass getSuperclass(JNIEnv *env, jclass klass); |
|
95 jmethodID getStaticMethodID(JNIEnv *env, jclass clazz, const char* name, |
|
96 const char *sig); |
|
97 jfieldID getStaticFieldID(JNIEnv *env, jclass clazz, const char* name, |
|
98 const char *sig); |
|
99 jclass findClass(JNIEnv *env, const char *name); |
|
100 void setStaticIntField(JNIEnv *env, jclass clazz, jfieldID field, |
|
101 jint value); |
|
102 jboolean isSameObject(JNIEnv *env, jobject o1, jobject o2); |
|
103 void pushLocalFrame(JNIEnv *env, jint capacity); |
|
104 void popLocalFrame(JNIEnv *env, jobject ret); |
|
105 jobject exceptionOccurred(JNIEnv *env); |
|
106 void exceptionDescribe(JNIEnv *env); |
|
107 void exceptionClear(JNIEnv *env); |
|
108 void registerNatives(JNIEnv *env, jclass clazz, |
|
109 JNINativeMethod *methods, jint count); |
|
110 |
|
111 /* More JVMTI support functions */ |
|
112 char * getErrorName(jvmtiError error_number); |
|
113 jvmtiPhase getPhase(void); |
|
114 char * phaseString(jvmtiPhase phase); |
|
115 void disposeEnvironment(void); |
|
116 jlong getObjectSize(jobject object); |
|
117 jobject getClassLoader(jclass klass); |
|
118 jint getClassStatus(jclass klass); |
|
119 jlong getTag(jobject object); |
|
120 void setTag(jobject object, jlong tag); |
|
121 void getObjectMonitorUsage(jobject object, jvmtiMonitorUsage *uinfo); |
|
122 void getOwnedMonitorInfo(jthread thread, jobject **ppobjects, |
|
123 jint *pcount); |
|
124 void getSystemProperty(const char *name, char **value); |
|
125 void getClassSignature(jclass klass, char**psignature, |
|
126 char **pgeneric_signature); |
|
127 void getSourceFileName(jclass klass, char** src_name_ptr); |
|
128 |
|
129 jvmtiPrimitiveType sigToPrimType(char *sig); |
|
130 int sigToPrimSize(char *sig); |
|
131 char primTypeToSigChar(jvmtiPrimitiveType primType); |
|
132 |
|
133 void getAllClassFieldInfo(JNIEnv *env, jclass klass, |
|
134 jint* field_count_ptr, FieldInfo** fields_ptr); |
|
135 void getMethodName(jmethodID method, char** name_ptr, |
|
136 char** signature_ptr); |
|
137 void getMethodClass(jmethodID method, jclass *pclazz); |
|
138 jboolean isMethodNative(jmethodID method); |
|
139 void getPotentialCapabilities(jvmtiCapabilities *capabilities); |
|
140 void addCapabilities(jvmtiCapabilities *capabilities); |
|
141 void setEventCallbacks(jvmtiEventCallbacks *pcallbacks); |
|
142 void setEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event, |
|
143 jthread thread); |
|
144 void * getThreadLocalStorage(jthread thread); |
|
145 void setThreadLocalStorage(jthread thread, void *ptr); |
|
146 void getThreadState(jthread thread, jint *threadState); |
|
147 void getThreadInfo(jthread thread, jvmtiThreadInfo *info); |
|
148 void getThreadGroupInfo(jthreadGroup thread_group, jvmtiThreadGroupInfo *info); |
|
149 void getLoadedClasses(jclass **ppclasses, jint *pcount); |
|
150 jint getLineNumber(jmethodID method, jlocation location); |
|
151 jlong getMaxMemory(JNIEnv *env); |
|
152 void createAgentThread(JNIEnv *env, const char *name, |
|
153 jvmtiStartFunction func); |
|
154 jlong getThreadCpuTime(jthread thread); |
|
155 void getStackTrace(jthread thread, jvmtiFrameInfo *pframes, jint depth, |
|
156 jint *pcount); |
|
157 void getThreadListStackTraces(jint count, jthread *threads, |
|
158 jint depth, jvmtiStackInfo **stack_info); |
|
159 void getFrameCount(jthread thread, jint *pcount); |
|
160 void followReferences(jvmtiHeapCallbacks *pHeapCallbacks, void *user_data); |
|
161 |
|
162 /* GC control */ |
|
163 void runGC(void); |
|
164 |
|
165 /* Get initial JVMTI environment */ |
|
166 void getJvmti(void); |
|
167 |
|
168 /* Get current runtime JVMTI version */ |
|
169 jint jvmtiVersion(void); |
|
170 |
|
171 /* Raw monitor functions */ |
|
172 jrawMonitorID createRawMonitor(const char *str); |
|
173 void rawMonitorEnter(jrawMonitorID m); |
|
174 void rawMonitorWait(jrawMonitorID m, jlong pause_time); |
|
175 void rawMonitorNotifyAll(jrawMonitorID m); |
|
176 void rawMonitorExit(jrawMonitorID m); |
|
177 void destroyRawMonitor(jrawMonitorID m); |
|
178 |
|
179 /* JVMTI alloc/dealloc */ |
|
180 void * jvmtiAllocate(int size); |
|
181 void jvmtiDeallocate(void *ptr); |
|
182 |
|
183 /* System malloc/free */ |
|
184 void * hprof_malloc(int size); |
|
185 void hprof_free(void *ptr); |
|
186 |
|
187 #include "debug_malloc.h" |
|
188 |
|
189 #ifdef DEBUG |
|
190 void * hprof_debug_malloc(int size, char *file, int line); |
|
191 void hprof_debug_free(void *ptr, char *file, int line); |
|
192 #define HPROF_MALLOC(size) hprof_debug_malloc(size, THIS_FILE, __LINE__) |
|
193 #define HPROF_FREE(ptr) hprof_debug_free(ptr, THIS_FILE, __LINE__) |
|
194 #else |
|
195 #define HPROF_MALLOC(size) hprof_malloc(size) |
|
196 #define HPROF_FREE(ptr) hprof_free(ptr) |
|
197 #endif |
|
198 |
|
199 #endif |
|