Search in sources :

Example 31 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class ThriftPlugin method addTAsyncMethodCallEditor.

private void addTAsyncMethodCallEditor() {
    final String targetClassName = "org.apache.thrift.async.TAsyncMethodCall";
    transformTemplate.transform(targetClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            final InstrumentClass target = instrumentor.getInstrumentClass(loader, className, classfileBuffer);
            target.addField(AsyncTraceIdAccessor.class.getName());
            target.addField(ThriftConstants.FIELD_ACCESSOR_SOCKET_ADDRESS);
            target.addGetter(ThriftConstants.FIELD_GETTER_T_NON_BLOCKING_TRANSPORT, ThriftConstants.T_ASYNC_METHOD_CALL_FIELD_TRANSPORT);
            // TAsyncMethodCall(TAsyncClient, TProtocolFactory, TNonblockingTransport, AsyncMethodCallback<T>, boolean)
            final InstrumentMethod constructor = target.getConstructor("org.apache.thrift.async.TAsyncClient", "org.apache.thrift.protocol.TProtocolFactory", "org.apache.thrift.transport.TNonblockingTransport", "org.apache.thrift.async.AsyncMethodCallback", "boolean");
            if (constructor != null) {
                String interceptor = "com.navercorp.pinpoint.plugin.thrift.interceptor.client.async.TAsyncMethodCallConstructInterceptor";
                constructor.addInterceptor(interceptor);
            }
            // TAsyncMethodCall.cleanUpAndFireCallback(SelectionKey)
            final InstrumentMethod cleanUpAndFireCallback = target.getDeclaredMethod("cleanUpAndFireCallback", "java.nio.channels.SelectionKey");
            if (cleanUpAndFireCallback != null) {
                String interceptor = "com.navercorp.pinpoint.plugin.thrift.interceptor.client.async.TAsyncMethodCallCleanUpAndFireCallbackInterceptor";
                cleanUpAndFireCallback.addInterceptor(interceptor);
            }
            // TAsyncMethodCall.onError(Exception)
            final InstrumentMethod onError = target.getDeclaredMethod("onError", "java.lang.Exception");
            if (onError != null) {
                String interceptor = "com.navercorp.pinpoint.plugin.thrift.interceptor.client.async.TAsyncMethodCallOnErrorInterceptor";
                onError.addInterceptor(interceptor);
            }
            return target.toBytecode();
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)

Example 32 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class AccessorInjectionTest method testAddSetter.

@Test
public void testAddSetter() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String targetClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject4";
    loader.addTransformer(targetClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", classLoader);
                InstrumentClass testClass = instrumentor.getInstrumentClass(classLoader, className, classfileBuffer);
                testClass.addSetter(IntSetter.class.getName(), "intValue");
                testClass.addSetter(IntArraySetter.class.getName(), "intValues");
                testClass.addSetter(IntegerArraySetter.class.getName(), "integerValues");
                return testClass.toBytecode();
            } catch (InstrumentException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Object testObject = loader.loadClass(targetClassName).newInstance();
    Class<?> intSetter = loader.loadClass(IntSetter.class.getName());
    Class<?> intsSetter = loader.loadClass(IntArraySetter.class.getName());
    Class<?> integersSetter = loader.loadClass(IntegerArraySetter.class.getName());
    Assert.assertTrue(intSetter.isInstance(testObject));
    Assert.assertTrue(intsSetter.isInstance(testObject));
    Assert.assertTrue(integersSetter.isInstance(testObject));
    int intValue = 99;
    int[] intValues = { 99, 100 };
    Integer[] integerValues = { 99, 100 };
    Method setInt = intSetter.getMethod("_$PINPOINT$_setInt", int.class);
    setInt.invoke(testObject, intValue);
    Method getInt = testObject.getClass().getMethod("getIntValue");
    Assert.assertEquals(intValue, getInt.invoke(testObject));
    Method setInts = intsSetter.getMethod("_$PINPOINT$_setIntArray", int[].class);
    setInts.invoke(testObject, intValues);
    Method getInts = testObject.getClass().getMethod("getIntValues");
    Assert.assertEquals(intValues, getInts.invoke(testObject));
    Method setIntegers = integersSetter.getMethod("_$PINPOINT$_setIntegerArray", Integer[].class);
    // wrap due to vararg expansion
    Object[] wrappedIntegerValues = new Object[] { integerValues };
    setIntegers.invoke(testObject, wrappedIntegerValues);
    Method getIntegers = testObject.getClass().getMethod("getIntegerValues");
    Assert.assertEquals(integerValues, getIntegers.invoke(testObject));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test) JavassistClassTest(com.navercorp.pinpoint.test.javasssit.JavassistClassTest)

Example 33 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class ASMClass method addField.

@Override
public void addField(final String accessorTypeName) throws InstrumentException {
    try {
        final Class<?> accessorType = this.pluginContext.injectClass(this.classLoader, accessorTypeName);
        final AccessorAnalyzer accessorAnalyzer = new AccessorAnalyzer();
        final AccessorAnalyzer.AccessorDetails accessorDetails = accessorAnalyzer.analyze(accessorType);
        final ASMFieldNodeAdapter fieldNode = this.classNode.addField(FIELD_PREFIX + JavaAssistUtils.javaClassNameToVariableName(accessorTypeName), accessorDetails.getFieldType());
        this.classNode.addInterface(accessorTypeName);
        this.classNode.addGetterMethod(accessorDetails.getGetter().getName(), fieldNode);
        this.classNode.addSetterMethod(accessorDetails.getSetter().getName(), fieldNode);
        setModified(true);
    } catch (Exception e) {
        throw new InstrumentException("Failed to add field with accessor [" + accessorTypeName + "]. Cause:" + e.getMessage(), e);
    }
}
Also used : InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundInstrumentException(com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)

Example 34 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class AccessorInjectionTest method testAddGetter.

@Test
public void testAddGetter() throws Exception {
    final TestClassLoader loader = getTestClassLoader();
    final String targetClassName = "com.navercorp.pinpoint.test.javasssit.mock.TestObject3";
    loader.addTransformer(targetClassName, new TransformCallback() {

        @Override
        public byte[] doInTransform(Instrumentor instrumentor, ClassLoader classLoader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws InstrumentException {
            try {
                logger.info("modify cl:{}", classLoader);
                InstrumentClass aClass = instrumentor.getInstrumentClass(classLoader, className, classfileBuffer);
                aClass.addGetter(StringGetter.class.getName(), "value");
                aClass.addGetter(IntGetter.class.getName(), "intValue");
                aClass.addGetter(IntArrayGetter.class.getName(), "intValues");
                aClass.addGetter(IntegerArrayGetter.class.getName(), "integerValues");
                return aClass.toBytecode();
            } catch (InstrumentException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    });
    Object testObject = loader.loadClass(targetClassName).newInstance();
    Class<?> stringGetter = loader.loadClass(StringGetter.class.getName());
    Class<?> intGetter = loader.loadClass(IntGetter.class.getName());
    Class<?> intsGetter = loader.loadClass(IntArrayGetter.class.getName());
    Class<?> integersGetter = loader.loadClass(IntegerArrayGetter.class.getName());
    Assert.assertTrue(stringGetter.isInstance(testObject));
    Assert.assertTrue(intGetter.isInstance(testObject));
    Assert.assertTrue(intsGetter.isInstance(testObject));
    Assert.assertTrue(integersGetter.isInstance(testObject));
    String value = "hehe";
    int intValue = 99;
    int[] intValues = { 99, 100 };
    Integer[] integerValues = { 99, 100 };
    Method method = testObject.getClass().getMethod("setValue", String.class);
    method.invoke(testObject, value);
    Method getString = stringGetter.getMethod("_$PINPOINT$_getString");
    Assert.assertEquals(value, getString.invoke(testObject));
    Method setIntValue = testObject.getClass().getMethod("setIntValue", int.class);
    setIntValue.invoke(testObject, intValue);
    Method getInt = intGetter.getMethod("_$PINPOINT$_getInt");
    Assert.assertEquals(intValue, getInt.invoke(testObject));
    Method setIntValues = testObject.getClass().getMethod("setIntValues", int[].class);
    setIntValues.invoke(testObject, intValues);
    Method getIntValues = intsGetter.getMethod("_$PINPOINT$_getIntArray");
    Assert.assertEquals(intValues, getIntValues.invoke(testObject));
    Method setIntegerValues = testObject.getClass().getMethod("setIntegerValues", Integer[].class);
    // wrap due to vararg expansion
    Object[] wrappedIntegerValues = new Object[] { integerValues };
    setIntegerValues.invoke(testObject, wrappedIntegerValues);
    Method getIntegerValues = integersGetter.getMethod("_$PINPOINT$_getIntegerArray");
    Assert.assertEquals(integerValues, getIntegerValues.invoke(testObject));
}
Also used : ProtectionDomain(java.security.ProtectionDomain) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) Instrumentor(com.navercorp.pinpoint.bootstrap.instrument.Instrumentor) Method(java.lang.reflect.Method) TransformCallback(com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback) InstrumentClass(com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) TestClassLoader(com.navercorp.pinpoint.test.classloader.TestClassLoader) Test(org.junit.Test) JavassistClassTest(com.navercorp.pinpoint.test.javasssit.JavassistClassTest)

Example 35 with InstrumentException

use of com.navercorp.pinpoint.bootstrap.instrument.InstrumentException in project pinpoint by naver.

the class ConstructorTransformer method edit.

@Override
public void edit(ClassLoader classLoader, InstrumentClass target) throws Throwable {
    InstrumentMethod targetConstructor = target.getConstructor(targetParameterTypes);
    if (targetConstructor == null) {
        if (ignoreIfNotExist) {
            return;
        } else {
            Exception e = new NoSuchMethodException("No such constructor: " + "(" + Arrays.deepToString(targetParameterTypes) + ")");
            if (exceptionHandler != null) {
                exceptionHandler.handle(target.getName(), "init", targetParameterTypes, e);
                logger.info("Cannot find target constructor with parameter types (" + Arrays.deepToString(targetParameterTypes) + ") but MethodTransformerExceptionHandler handled it.");
                return;
            } else {
                throw new InstrumentException("Fail to edit constructor", e);
            }
        }
    }
    for (MethodRecipe recipe : recipes) {
        try {
            recipe.edit(classLoader, target, targetConstructor);
        } catch (Throwable t) {
            logger.info("Exception thrown while editing " + targetConstructor.getDescriptor().getApiDescriptor(), t);
            if (exceptionHandler != null) {
                exceptionHandler.handle(target.getName(), "init", targetParameterTypes, t);
                logger.info("Exception thrown while editing" + targetConstructor.getDescriptor().getApiDescriptor() + " but MethodTransformerExceptionHandler handled it.", t);
            } else {
                throw new InstrumentException("Fail to edit constructor " + targetConstructor.getDescriptor().getApiDescriptor(), t);
            }
        }
    }
}
Also used : InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException) InstrumentMethod(com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod) InstrumentException(com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)

Aggregations

InstrumentException (com.navercorp.pinpoint.bootstrap.instrument.InstrumentException)35 InstrumentClass (com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass)29 Instrumentor (com.navercorp.pinpoint.bootstrap.instrument.Instrumentor)28 TransformCallback (com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback)28 ProtectionDomain (java.security.ProtectionDomain)28 InstrumentMethod (com.navercorp.pinpoint.bootstrap.instrument.InstrumentMethod)22 Method (java.lang.reflect.Method)6 Test (org.junit.Test)6 TestClassLoader (com.navercorp.pinpoint.test.classloader.TestClassLoader)5 NotFoundInstrumentException (com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 JavassistClassTest (com.navercorp.pinpoint.test.javasssit.JavassistClassTest)3 List (java.util.List)3 ProfilerConfig (com.navercorp.pinpoint.bootstrap.config.ProfilerConfig)2 MethodFilter (com.navercorp.pinpoint.bootstrap.instrument.MethodFilter)2 Interceptor (com.navercorp.pinpoint.bootstrap.interceptor.Interceptor)2 ObjectFactory (com.navercorp.pinpoint.bootstrap.plugin.ObjectFactory)2 AsyncTraceIdAccessor (com.navercorp.pinpoint.bootstrap.async.AsyncTraceIdAccessor)1 DatabaseInfo (com.navercorp.pinpoint.bootstrap.context.DatabaseInfo)1 PreparedStatementBindingMethodFilter (com.navercorp.pinpoint.bootstrap.plugin.jdbc.PreparedStatementBindingMethodFilter)1