Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/jvmti/compiledMethodLoad/compiledMethodLoad.c
38829 views
/*1* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940#include <stdio.h>41#include <stdlib.h>42#include <string.h>4344#include "jni.h"45#include "jvmti.h"46#include "jvmticmlr.h"4748#include "agent_util.h"4950/* Global static data */51static char OUTPUT_FILE[] = "compiledMethodLoad.txt";52static FILE *fp;53static jvmtiEnv *jvmti;54static jrawMonitorID lock;5556/* print a jvmtiCompiledMethodLoadDummyRecord */57void58print_dummy_record(jvmtiCompiledMethodLoadDummyRecord* record,59jvmtiEnv* jvmti, FILE* fp) {6061if (record != NULL) {62fprintf(fp, "Dummy record detected containing message: %s\n",63(char *)record->message);64}65}6667/* print the specified stack frames */68void69print_stack_frames(PCStackInfo* record, jvmtiEnv *jvmti, FILE* fp) {70if (record != NULL && record->methods != NULL) {71int i;7273for (i = 0; i < record->numstackframes; i++) {74jvmtiError err;75char* method_name = NULL;76char* class_name = NULL;77char* method_signature = NULL;78char* class_signature = NULL;79char* generic_ptr_method = NULL;80char* generic_ptr_class = NULL;81jmethodID id;82jclass declaringclassptr;83id = record->methods[i];8485err = (*jvmti)->GetMethodDeclaringClass(jvmti, id,86&declaringclassptr);87check_jvmti_error(jvmti, err, "get method declaring class");8889err = (*jvmti)->GetClassSignature(jvmti, declaringclassptr,90&class_signature, &generic_ptr_class);91check_jvmti_error(jvmti, err, "get class signature");9293err = (*jvmti)->GetMethodName(jvmti, id, &method_name,94&method_signature, &generic_ptr_method);95check_jvmti_error(jvmti, err, "get method name");9697fprintf(fp, "%s::%s %s %s @%d\n", class_signature, method_name,98method_signature,99generic_ptr_method == NULL ? "" : generic_ptr_method,100record->bcis[i]);101102if (method_name != NULL) {103err = (*jvmti)->Deallocate(jvmti, (unsigned char*)method_name);104check_jvmti_error(jvmti, err, "deallocate method_name");105}106if (method_signature != NULL) {107err = (*jvmti)->Deallocate(jvmti,108(unsigned char*)method_signature);109check_jvmti_error(jvmti, err, "deallocate method_signature");110}111if (generic_ptr_method != NULL) {112err = (*jvmti)->Deallocate(jvmti,113(unsigned char*)generic_ptr_method);114check_jvmti_error(jvmti, err, "deallocate generic_ptr_method");115}116if (class_name != NULL) {117err = (*jvmti)->Deallocate(jvmti, (unsigned char*)class_name);118check_jvmti_error(jvmti, err, "deallocate class_name");119}120if (class_signature != NULL) {121err = (*jvmti)->Deallocate(jvmti,122(unsigned char*)class_signature);123check_jvmti_error(jvmti, err, "deallocate class_signature");124}125if (generic_ptr_class != NULL) {126err = (*jvmti)->Deallocate(jvmti,127(unsigned char*)generic_ptr_class);128check_jvmti_error(jvmti, err, "deallocate generic_ptr_class");129}130}131}132}133134/* print a jvmtiCompiledMethodLoadInlineRecord */135void136print_inline_info_record(jvmtiCompiledMethodLoadInlineRecord* record,137jvmtiEnv *jvmti, FILE* fp) {138139if (record != NULL && record->pcinfo != NULL) {140int numpcs = record->numpcs;141int i;142143for (i = 0; i < numpcs; i++) {144PCStackInfo pcrecord = (record->pcinfo[i]);145fprintf(fp, "PcDescriptor(pc=0x%lx):\n", (jint)(pcrecord.pc));146print_stack_frames(&pcrecord, jvmti, fp);147}148}149}150151/* decode kind of CompiledMethodLoadRecord and print */152void153print_records(jvmtiCompiledMethodLoadRecordHeader* list, jvmtiEnv *jvmti,154FILE* fp)155{156jvmtiCompiledMethodLoadRecordHeader* curr = list;157fprintf(fp, "\nPrinting PC Descriptors\n\n");158while (curr != NULL) {159switch (curr->kind) {160case JVMTI_CMLR_DUMMY:161print_dummy_record((jvmtiCompiledMethodLoadDummyRecord *)curr,162jvmti, fp);163break;164165case JVMTI_CMLR_INLINE_INFO:166print_inline_info_record(167(jvmtiCompiledMethodLoadInlineRecord *)curr, jvmti, fp);168break;169170default:171fprintf(fp, "Warning: unrecognized record: kind=%d\n", curr->kind);172break;173}174175curr = (jvmtiCompiledMethodLoadRecordHeader *)curr->next;176}177}178179/* Callback for JVMTI_EVENT_COMPILED_METHOD_LOAD */180void JNICALL181compiled_method_load(jvmtiEnv *jvmti, jmethodID method, jint code_size,182const void* code_addr, jint map_length, const jvmtiAddrLocationMap* map,183const void* compile_info)184{185jvmtiError err;186char* name = NULL;187char* signature = NULL;188char* generic_ptr = NULL;189jvmtiCompiledMethodLoadRecordHeader* pcs;190191err = (*jvmti)->RawMonitorEnter(jvmti, lock);192check_jvmti_error(jvmti, err, "raw monitor enter");193194err = (*jvmti)->GetMethodName(jvmti, method, &name, &signature,195&generic_ptr);196check_jvmti_error(jvmti, err, "get method name");197198fprintf(fp, "\nCompiled method load event\n");199fprintf(fp, "Method name %s %s %s\n\n", name, signature,200generic_ptr == NULL ? "" : generic_ptr);201pcs = (jvmtiCompiledMethodLoadRecordHeader *)compile_info;202if (pcs != NULL) {203print_records(pcs, jvmti, fp);204}205206if (name != NULL) {207err = (*jvmti)->Deallocate(jvmti, (unsigned char*)name);208check_jvmti_error(jvmti, err, "deallocate name");209}210if (signature != NULL) {211err = (*jvmti)->Deallocate(jvmti, (unsigned char*)signature);212check_jvmti_error(jvmti, err, "deallocate signature");213}214if (generic_ptr != NULL) {215err = (*jvmti)->Deallocate(jvmti, (unsigned char*)generic_ptr);216check_jvmti_error(jvmti, err, "deallocate generic_ptr");217}218219err = (*jvmti)->RawMonitorExit(jvmti, lock);220check_jvmti_error(jvmti, err, "raw monitor exit");221}222223/* Agent_OnLoad() is called first, we prepare for a COMPILED_METHOD_LOAD224* event here.225*/226JNIEXPORT jint JNICALL227Agent_OnLoad(JavaVM *vm, char *options, void *reserved)228{229jint rc;230jvmtiError err;231jvmtiCapabilities capabilities;232jvmtiEventCallbacks callbacks;233234fp = fopen(OUTPUT_FILE, "w");235if (fp == NULL) {236fatal_error("ERROR: %s: Unable to create output file\n", OUTPUT_FILE);237return -1;238}239240/* Get JVMTI environment */241rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);242if (rc != JNI_OK) {243fatal_error(244"ERROR: Unable to create jvmtiEnv, GetEnv failed, error=%d\n", rc);245return -1;246}247248/* add JVMTI capabilities */249memset(&capabilities,0, sizeof(capabilities));250capabilities.can_generate_compiled_method_load_events = 1;251err = (*jvmti)->AddCapabilities(jvmti, &capabilities);252check_jvmti_error(jvmti, err, "add capabilities");253254/* set JVMTI callbacks for events */255memset(&callbacks, 0, sizeof(callbacks));256callbacks.CompiledMethodLoad = &compiled_method_load;257err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));258check_jvmti_error(jvmti, err, "set event callbacks");259260/* enable JVMTI events */261err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,262JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);263check_jvmti_error(jvmti, err, "set event notify");264265/* create coordination monitor */266err = (*jvmti)->CreateRawMonitor(jvmti, "agent lock", &lock);267check_jvmti_error(jvmti, err, "create raw monitor");268269return 0;270}271272/* Agent_OnUnload() is called last */273JNIEXPORT void JNICALL274Agent_OnUnload(JavaVM *vm)275{276}277278279