instrumentation

How do I replace a class with new one with Java instrumentation?

倖福魔咒の 提交于 2019-12-05 15:46:35
I need to create a java agent that when is enabled it gets the path to a jar file as argument and then it replaces any loaded class the the one inside the jar file if their names are matched. For example, we have an application with a class called com.something.ClassTest. Now if the mentioned jar (is not in the class path) has a class exactly the same name as com.something.ClassTest, I want to replace it with the one in the jar. I have this class transformer but not sure if that's correct or not. I get IOException with message Class not found. @Override public byte[] transform(ClassLoader

How to fix INJECT_EVENT permission exception when sending touches to an ActivityInstrumentationTestCase2 test

荒凉一梦 提交于 2019-12-05 13:24:00
Although there are many examples showing that something like this should work, the following code fails. This code lives in a test project that is associated with the real project. public class MyTest extends ActivityInstrumentationTestCase2<MyActivity> { public MyTest(String name) { super("com.mypackage.activities", MyActivity.class); setName(name); } public void testTap() throws Throwable { //Required by MotionEvent.obtain according to JavaDocs long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis(); Instrumentation i = getInstrumentation(); //Setup the info

android instrumentation test case - getinstrumentation() returning null

北慕城南 提交于 2019-12-05 09:42:50
I've been trying to make a test case extending intstrumentationtestcase, and whenever I call getinstrumentation() it returns a null instance of Instrumentation instead of an Instrumentation, rendering any of the automation I'm wanting to do useless. I have the permission set in the manifest as well even though I'm only testing the automation on the same app this case is going to run on...any ideas? You need inject the instrumentation programmatically by calling injectInstrumentation(InstrumentationRegistry.getInstrumentation()); using InstrumentationRegistry from the official Android testing

bytecode instrumentation using ClassFileTransformer.transform

会有一股神秘感。 提交于 2019-12-05 09:24:37
I have written a Class with premain method and inside that method I have added a ClassFileTransformer to Instrumentation ( Instrumentation.addTransformer() ). I have invoked a application using java -javaagent:<path_to_agnet.jar> <application> However ClassFileTransformer.transform() method is not being invoked. I have observed that premain is being invoked. I have also observed that if I call Instrumentation.retransformClasses() , then ClassFileTransformer.transform() is being invoked. On first definition ( Classloader.defineClass() ), transform() method is not being invoked. Any clue what

renaming a field using javassist at runtime in the pre-main method (java instrumentation)

谁说胖子不能爱 提交于 2019-12-05 07:28:34
I want to rename a field inside a java class at runtime. In addition, Any method that access that field ;wether it's read or write; I need it to be modified to use the new name instead of the old name.... All this will be done inside the pre-main method... As an Exmaple, given the following code: public class Class1 { String strCompany; public String Test() { strCompany = "TestCompany"; return strCompany; } } In the above class, I need to change the field "strCompany" to be "strCompany2", in addition I need the method Test to use the new name instead of the old name.... changing the field name

How to retransform an executing method with JVMTI agent which has no further invocations?

Deadly 提交于 2019-12-05 02:21:58
I am instrumenting a class file during runtime for various purposes. I'm using a JVMTI agent to that end. My strategy to instrument a method is to call RetransformClasses function to invoke ClassFileLoadHook . This strategy works fine for all the methods which have any further invocation after the time of instrumentation because actual instrumentation happens at subsequent function call, but it doesn't work for any method which does not have further invocations like main function in a program. I want to instrument a method on the fly during its execution. I want some procedure like On-Stack

Detecting System.setProperty method invocations

霸气de小男生 提交于 2019-12-05 02:08:02
问题 I'm facing a conundrum here. One of the applications that I've developed is loading an incorrect implementation of the DocumentBuilderFactory class of JAXP. This behavior was later deduced to be resulting from another class in different application built by a different team/company. The said class had changed the preferred DocumentBuilderFactory class upon loading, by inclusion of a static block similar to the one below: static { System.setProperty("javax.xml.parsers.DocumentBuilderFactory",

“Failed to load Premain-Class manifest attribute” while trying to get the size of an object using java agent

人走茶凉 提交于 2019-12-05 01:03:32
When i try to run a java program ( java -javaagent:size.jar ObjectSizeTest ) i get the following error: Failed to load Premain-Class manifest attribute from D:\workspace\ObjectSizeTest\size.jar Error occurred during initialization of VM agent library failed to init: instrument Here is ObjectSizeTest's code: public class ObjectSizeTest { public static void main(String[] args) { String s = new String("sai"); System.out.println(ObjectSizeFetcher.getObjectSize(s)); } } MANIFEST.MF (for size.jar): Manifest-Version: 1.0 Created-By: 1.5.0_18 (Sun Microsystems Inc.) Premain-Class: ObjectSizeFetcher

How to instrument a statement just before another statement using clang

戏子无情 提交于 2019-12-04 13:19:37
问题 I have to instrument certain statements in clang by adding a statement just before it. I have a pointer to an Expr object using which I need to insert another statement just before the statement containing it. Right now I am using a hacky approach which just moves back the SourceLocation pointer till I see a ; or } or {. But this does not work for all cases. eg when I try to instrument a for statement, it fails. Is there any class in clang which provides a method to do this in a more cleaner

Instrumentation (diagnostic) library for C++

帅比萌擦擦* 提交于 2019-12-04 12:26:03
I'm thinking about adding code to my application that would gather diagnostic information for later examination. Is there any C++ library created for such purpose? What I'm trying to do is similar to profiling, but it's not the same, because gathered data will be used more for debugging than profiling. EDIT: Platform: Linux Diagnostic information to gather: information resulting from application logic, various asserts and statistics. You might also want to check out libcwd : Libcwd is a thread-safe, full-featured debugging support library for C++ developers. It includes ostream-based debug