comparison src/java.instrument/share/native/libinstrument/JPLISAgent.c @ 13901:b2a69d66dc65

8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, chris.hegarty@oracle.com, alexandr.scherbatiy@oracle.com, amy.lu@oracle.com, calvin.cheung@oracle.com, daniel.fuchs@oracle.com, erik.joelsson@oracle.com, harold.seigel@oracle.com, jaroslav.bachorik@oracle.com, jean-francois.denise@oracle.com, jan.lahoda@oracle.com, james.laskey@oracle.com, lois.foltan@oracle.com, miroslav.kos@oracle.com, huaming.li@oracle.com, sean.mullan@oracle.com, naoto.sato@oracle.com, masayoshi.okutsu@oracle.com, peter.levart@gmail.com, philip.race@oracle.com, claes.redestad@oracle.com, sergey.bylokhov@oracle.com, alexandre.iline@oracle.com, volker.simonis@gmail.com, staffan.larsen@oracle.com, stuart.marks@oracle.com, semyon.sadetsky@oracle.com, serguei.spitsyn@oracle.com, sundararajan.athijegannathan@oracle.com, valerie.peng@oracle.com, vincent.x.ryan@oracle.com, weijun.wang@oracle.com, yuri.nesterenko@oracle.com, yekaterina.kantserova@oracle.com, alexander.kulyakhtin@oracle.com, felix.yang@oracle.com, andrei.eremeev@oracle.com, frank.yuan@oracle.com, sergei.pikalev@oracle.com, sibabrata.sahoo@oracle.com, tiantian.du@oracle.com, sha.jiang@oracle.com
author alanb
date Thu, 17 Mar 2016 19:04:16 +0000
parents f08705540498
children 497a8134cda1
comparison
equal deleted inserted replaced
0:bf40a798a728 1:499aaf7dc222
1 /* 1 /*
2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
26 /* 26 /*
27 * Copyright 2003 Wily Technology, Inc. 27 * Copyright 2003 Wily Technology, Inc.
28 */ 28 */
29 29
30 #include <jni.h> 30 #include <jni.h>
31 #include <jvm.h>
31 #include <jvmti.h> 32 #include <jvmti.h>
32 #include <stdlib.h> 33 #include <stdlib.h>
33 #include <string.h> 34 #include <string.h>
34 #include "JPLISAgent.h" 35 #include "JPLISAgent.h"
35 #include "JPLISAssert.h" 36 #include "JPLISAssert.h"
767 agent->mRedefineAdded = JNI_TRUE; 768 agent->mRedefineAdded = JNI_TRUE;
768 } 769 }
769 } 770 }
770 } 771 }
771 772
773 static jobject
774 getModuleObject(JNIEnv * jnienv,
775 jobject loaderObject,
776 const char* cname) {
777 jboolean errorOutstanding = JNI_FALSE;
778 jobject moduleObject = NULL;
779 jstring package = NULL;
780
781 /* find last slash in the class name */
782 char* last_slash = (cname == NULL) ? NULL : strrchr(cname, '/');
783 int len = (last_slash == NULL) ? 0 : (int)(last_slash - cname);
784 char* pkg_name_buf = (char*)malloc(len + 1);
785
786 jplis_assert_msg(pkg_name_buf != NULL, "OOM error in native tmp buffer allocation");
787 if (last_slash != NULL) {
788 strncpy(pkg_name_buf, cname, len);
789 }
790 pkg_name_buf[len] = '\0';
791
792 package = (*jnienv)->NewStringUTF(jnienv, pkg_name_buf);
793 jplis_assert_msg(package != NULL, "OOM error in NewStringUTF");
794
795 moduleObject = JVM_GetModuleByPackageName(jnienv, loaderObject, package);
796
797 errorOutstanding = checkForAndClearThrowable(jnienv);
798 jplis_assert_msg(!errorOutstanding,
799 "error in lookup of a module of the class being instrumented");
800 free((void*)pkg_name_buf);
801 return moduleObject;
802 }
772 803
773 /* 804 /*
774 * Support for the JVMTI callbacks 805 * Support for the JVMTI callbacks
775 */ 806 */
776 807
808 839
809 if ( !errorOutstanding ) { 840 if ( !errorOutstanding ) {
810 classFileBufferObject = (*jnienv)->NewByteArray(jnienv, 841 classFileBufferObject = (*jnienv)->NewByteArray(jnienv,
811 class_data_len); 842 class_data_len);
812 errorOutstanding = checkForAndClearThrowable(jnienv); 843 errorOutstanding = checkForAndClearThrowable(jnienv);
813 jplis_assert_msg(!errorOutstanding, "can't create byte arrau"); 844 jplis_assert_msg(!errorOutstanding, "can't create byte array");
814 } 845 }
815 846
816 if ( !errorOutstanding ) { 847 if ( !errorOutstanding ) {
817 jbyte * typedBuffer = (jbyte *) class_data; /* nasty cast, dumb JNI interface, const missing */ 848 jbyte * typedBuffer = (jbyte *) class_data; /* nasty cast, dumb JNI interface, const missing */
818 /* The sign cast is safe. The const cast is dumb. */ 849 /* The sign cast is safe. The const cast is dumb. */
826 } 857 }
827 858
828 /* now call the JPL agents to do the transforming */ 859 /* now call the JPL agents to do the transforming */
829 /* potential future optimization: may want to skip this if there are none */ 860 /* potential future optimization: may want to skip this if there are none */
830 if ( !errorOutstanding ) { 861 if ( !errorOutstanding ) {
862 jobject moduleObject = NULL;
863
864 if (classBeingRedefined == NULL) {
865 moduleObject = getModuleObject(jnienv, loaderObject, name);
866 } else {
867 // Redefine or retransform, InstrumentationImpl.transform() will use
868 // classBeingRedefined.getModule() to get the module.
869 }
831 jplis_assert(agent->mInstrumentationImpl != NULL); 870 jplis_assert(agent->mInstrumentationImpl != NULL);
832 jplis_assert(agent->mTransform != NULL); 871 jplis_assert(agent->mTransform != NULL);
833 transformedBufferObject = (*jnienv)->CallObjectMethod( 872 transformedBufferObject = (*jnienv)->CallObjectMethod(
834 jnienv, 873 jnienv,
835 agent->mInstrumentationImpl, 874 agent->mInstrumentationImpl,
836 agent->mTransform, 875 agent->mTransform,
837 loaderObject, 876 loaderObject,
877 moduleObject,
838 classNameStringObject, 878 classNameStringObject,
839 classBeingRedefined, 879 classBeingRedefined,
840 protectionDomain, 880 protectionDomain,
841 classFileBufferObject, 881 classFileBufferObject,
842 is_retransformer); 882 is_retransformer);