12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "jvmti.h"
28 #include "jni_tools.h"
29 #include "agent_common.h"
30 #include "JVMTITools.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #ifndef JNI_ENV_ARG
37
38 #ifdef __cplusplus
39 #define JNI_ENV_ARG(x, y) y
40 #define JNI_ENV_PTR(x) x
41 #else
42 #define JNI_ENV_ARG(x,y) x, y
43 #define JNI_ENV_PTR(x) (*x)
44 #endif
45
46 #endif
47
48 #define PASSED 0
49 #define STATUS_FAILED 2
50
51 static jvmtiEnv *jvmti = NULL;
52 static jint result = PASSED;
53 static jboolean printdump = JNI_FALSE;
54 static jvmtiCapabilities jvmti_caps;
55 static jint dummy_user_data = 0;
56 static jboolean user_data_error_flag = JNI_FALSE;
57
58 #define HEAP_ROOT_REF_KIND_BASE 100
59 #define MISSED_REF_KIND_BASE 300
60
61 typedef enum {
62 rthread,
63 rclass,
64 rother,
65 rmark
66 } refKind;
256 #ifdef STATIC_BUILD
257 JNIEXPORT jint JNICALL Agent_OnLoad_refignore(JavaVM *jvm, char *options, void *reserved) {
258 return Agent_Initialize(jvm, options, reserved);
259 }
260 JNIEXPORT jint JNICALL Agent_OnAttach_refignore(JavaVM *jvm, char *options, void *reserved) {
261 return Agent_Initialize(jvm, options, reserved);
262 }
263 JNIEXPORT jint JNI_OnLoad_refignore(JavaVM *jvm, char *options, void *reserved) {
264 return JNI_VERSION_1_8;
265 }
266 #endif
267
268 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
269 jint res;
270 jvmtiError err;
271
272 if (options != NULL && strcmp(options, "printdump") == 0) {
273 printdump = JNI_TRUE;
274 }
275
276 res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
277 JVMTI_VERSION_1_1);
278 if (res != JNI_OK || jvmti == NULL) {
279 printf("Wrong result of a valid call to GetEnv!\n");
280 return JNI_ERR;
281 }
282
283 memset((void*)&jvmti_caps, 0, sizeof(jvmtiCapabilities));
284 jvmti_caps.can_tag_objects = 1;
285 err = jvmti->AddCapabilities(&jvmti_caps);
286 if (err != JVMTI_ERROR_NONE) {
287 printf("Error (AddCapabilities): %s (%d)\n", TranslateError(err), err);
288 return JNI_ERR;
289 }
290
291 return JNI_OK;
292 }
293
294 jvmtiIterationControl JNICALL
295 heapMarkCallback(jlong class_tag, jlong size, jlong* tag_ptr, void* user_data) {
296 const MyTag* const tag = newTag(rmark, (const MyTag*)(intptr_t)class_tag, size, NULL);
297 *tag_ptr = (intptr_t)tag;
468 heapRootCallback,
469 stackReferenceCallback,
470 objectReferenceCallback,
471 &dummy_user_data);
472 if (err != JVMTI_ERROR_NONE) {
473 printf("Error (IterateOverReachableObjects): %s (%d)\n", TranslateError(err), err);
474 result = STATUS_FAILED;
475 }
476
477 if (printdump == JNI_TRUE) {
478 printf("<html><head><title>Heap Dump</title></head><body><pre>\n");
479 walk(fakeRoot, 0, "roots");
480 printf("\n------------------- MISSED ------------------\n\n");
481 walk(missed, 0, "missed");
482 printf("</pre></body></html>\n");
483 }
484
485 return result;
486 }
487
488 #ifdef __cplusplus
489 }
490 #endif
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "jvmti.h"
28 #include "jni_tools.h"
29 #include "agent_common.h"
30 #include "JVMTITools.h"
31
32 extern "C" {
33
34
35 #define PASSED 0
36 #define STATUS_FAILED 2
37
38 static jvmtiEnv *jvmti = NULL;
39 static jint result = PASSED;
40 static jboolean printdump = JNI_FALSE;
41 static jvmtiCapabilities jvmti_caps;
42 static jint dummy_user_data = 0;
43 static jboolean user_data_error_flag = JNI_FALSE;
44
45 #define HEAP_ROOT_REF_KIND_BASE 100
46 #define MISSED_REF_KIND_BASE 300
47
48 typedef enum {
49 rthread,
50 rclass,
51 rother,
52 rmark
53 } refKind;
243 #ifdef STATIC_BUILD
244 JNIEXPORT jint JNICALL Agent_OnLoad_refignore(JavaVM *jvm, char *options, void *reserved) {
245 return Agent_Initialize(jvm, options, reserved);
246 }
247 JNIEXPORT jint JNICALL Agent_OnAttach_refignore(JavaVM *jvm, char *options, void *reserved) {
248 return Agent_Initialize(jvm, options, reserved);
249 }
250 JNIEXPORT jint JNI_OnLoad_refignore(JavaVM *jvm, char *options, void *reserved) {
251 return JNI_VERSION_1_8;
252 }
253 #endif
254
255 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
256 jint res;
257 jvmtiError err;
258
259 if (options != NULL && strcmp(options, "printdump") == 0) {
260 printdump = JNI_TRUE;
261 }
262
263 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
264 if (res != JNI_OK || jvmti == NULL) {
265 printf("Wrong result of a valid call to GetEnv!\n");
266 return JNI_ERR;
267 }
268
269 memset((void*)&jvmti_caps, 0, sizeof(jvmtiCapabilities));
270 jvmti_caps.can_tag_objects = 1;
271 err = jvmti->AddCapabilities(&jvmti_caps);
272 if (err != JVMTI_ERROR_NONE) {
273 printf("Error (AddCapabilities): %s (%d)\n", TranslateError(err), err);
274 return JNI_ERR;
275 }
276
277 return JNI_OK;
278 }
279
280 jvmtiIterationControl JNICALL
281 heapMarkCallback(jlong class_tag, jlong size, jlong* tag_ptr, void* user_data) {
282 const MyTag* const tag = newTag(rmark, (const MyTag*)(intptr_t)class_tag, size, NULL);
283 *tag_ptr = (intptr_t)tag;
454 heapRootCallback,
455 stackReferenceCallback,
456 objectReferenceCallback,
457 &dummy_user_data);
458 if (err != JVMTI_ERROR_NONE) {
459 printf("Error (IterateOverReachableObjects): %s (%d)\n", TranslateError(err), err);
460 result = STATUS_FAILED;
461 }
462
463 if (printdump == JNI_TRUE) {
464 printf("<html><head><title>Heap Dump</title></head><body><pre>\n");
465 walk(fakeRoot, 0, "roots");
466 printf("\n------------------- MISSED ------------------\n\n");
467 walk(missed, 0, "missed");
468 printf("</pre></body></html>\n");
469 }
470
471 return result;
472 }
473
474 }
|