Java Forum / General / December 2005
java.lang.instrument
Francesco Devittori - 08 Dec 2005 09:28 GMT Hi everybody, this is my first post in this newsgroup, I hope the question is appropriate.
I am working with java.lang.instrument and I'd like to discuss some aspects, is there anybody that works with this api?
Francesco
Chris Smith - 08 Dec 2005 15:27 GMT > this is my first post in this newsgroup, I hope the question is appropriate. > > I am working with java.lang.instrument and I'd like to discuss some > aspects, is there anybody that works with this api? This is a fine place to ask. What aspects of the package do you want to discuss?
Don't worry about finding experienced people in advance. If your question sparks someone's interest, they may even go learn the package to answer your questions. I don't know much about it, for example... but I've always thought the concept was intriguing.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
Francesco Devittori - 08 Dec 2005 17:17 GMT >>this is my first post in this newsgroup, I hope the question is appropriate. >> [quoted text clipped - 8 lines] > to answer your questions. I don't know much about it, for example... > but I've always thought the concept was intriguing. Ok, thanks. Sorry for the long message, but maybe someone is interested. I'm working on a project that uses this api and here are the first things I noticed:
1) exceptions thrown in the agent (or in objects created from the agent) simply make the agent die silently. No output, nothing. This makes debugging a nightmare. I can't understand why things are that way (maybe this happens because of something I do?).
2) exactly which classes go through the instrumentation? I.e. classes called by the agent are not rewritten (I guess). Then what happens if a "normal" class calls a class that was already loaded by the agent? It gets instrumented or not?
3) a very strange problem: if you run your class with a classpath that includes the .class of your agent, then the application gets stuck at some random point (you have to Ctrl-Z/pkill -9 java).
For example: java -cp someDir -javaagent:myAgent.jar HelloWorld if myAgent.class is also inside someDir/ then you have this problem.
In my case the application gets stuck inside a Bcel method (JavaClass.parse()).
This same problem occurs if the agent tries to load a class that is not inside a jar file. I.e. in the agent you say "MyClass obj = new MyClass();" then you cannot have MyClass.class in your classpath, but you have to put it inside a jar (and put the jar in the classpath of course).
Maybe these things are due to something I do wrong, I'm curious if someone has some experience (I cannot find a lot of documentation online)
Francesco
Thomas Hawtin - 08 Dec 2005 19:44 GMT > 1) exceptions thrown in the agent (or in objects created from the agent) > simply make the agent die silently. No output, nothing. > This makes debugging a nightmare. I can't understand why things are that > way (maybe this happens because of something I do?). Looking at the source j2se/src/share/classes/sun/instrument/TransformationManager.java, I spy:
catch (Throwable t) { // don't let any one transformer mess it up for the others. // This is where we need to put some logging. What should go here? FIXME }
You can grab the source from http://tiger.dev.java.net/ (although not the latest version, yet). You can then just edit that file to dump the exception and run with -Xbootclass/p:. Perhaps even supply a fix to Mustang. Alternatively, compile with debugging information and run it through your debugger (if it supports that sort of thing).
> 3) a very strange problem: if you run your class with a classpath that > includes the .class of your agent, then the application gets stuck at > some random point (you have to Ctrl-Z/pkill -9 java). Can you find the stack with Ctrl-\ (or ctrl-break(?) for Windows)?
It may be worth replacing/prefixing the package name of third party libraries. For instance, BCEL is include with the JRE, but has been renamed.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Francesco Devittori - 09 Dec 2005 07:27 GMT >> 1) exceptions thrown in the agent (or in objects created from the >> agent) simply make the agent die silently. No output, nothing. [quoted text clipped - 15 lines] > Mustang. Alternatively, compile with debugging information and run it > through your debugger (if it supports that sort of thing). Thanks, the idea is fine. I put some t.printStackTrace() there and load it with -Xbootclass/p: How can I provide a fix to Mustang? Is there a standard way of doing this? (bugs.sun.com?)
>> 3) a very strange problem: if you run your class with a classpath that >> includes the .class of your agent, then the application gets stuck at [quoted text clipped - 5 lines] > libraries. For instance, BCEL is include with the JRE, but has been > renamed. Now I try to reproduce the problem and get the stack.
> Tom Hawtin Thanks for these interesting suggestions!
Thomas Fritsch - 09 Dec 2005 09:12 GMT > How can I provide a fix to Mustang? Is there a standard way of doing > this? (bugs.sun.com?) Exactly. On that page click "Report a bug or request a feature". In the report page you select "Request for enhancement (RFE)".
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Thomas Hawtin - 10 Dec 2005 01:00 GMT >> How can I provide a fix to Mustang? Is there a standard way of doing >> this? (bugs.sun.com?) > > Exactly. On that page click "Report a bug or request a feature". In the > report page you select "Request for enhancement (RFE)". The submitting fixes part, rather than submitting a bug or RFE report, is in the paragraph below, labeled "Submitting bug fixes".
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Francesco Devittori - 09 Dec 2005 07:48 GMT > Francesco Devittori wrote: >> 3) a very strange problem: if you run your class with a classpath that >> includes the .class of your agent, then the application gets stuck at >> some random point (you have to Ctrl-Z/pkill -9 java). > > Can you find the stack with Ctrl-\ (or ctrl-break(?) for Windows)? I have the stack. My little test includes these files:
MyAgent.class MyAgent.jar MyAgent.java bcel-5.1.jar hello/HelloWorld.java hello/HelloWorld.class
When I run
java -cp hello:bcel-5.1.jar -javaagent:MyAgent.jar HelloWorld
no problems. But if I run
java -cp hello:bcel-5.1.jar:. -javaagent:MyAgent.jar HelloWorld
then it deadlocks. (The difference is that there is "." in the cp, which includes MyAgent.class - It happens the same if I put MyAgent.class in another dir and append that dir to the cp)
So here the stack trace: ---------------------------------------------------------------
java -cp hello:bcel-5.1.jar:. -javaagent:MyAgent.jar HelloWorld parsing HelloWorld parsing java/lang/Enum Full thread dump Java HotSpot(TM) Client VM (1.5.0_05-b05 mixed mode):
"Low Memory Detector" daemon prio=1 tid=0x080eedb8 nid=0xb77 runnable [0x00000000..0x00000000]
"CompilerThread0" daemon prio=1 tid=0x080eb128 nid=0xb76 waiting on condition [0x00000000..0xaa1ab438]
"Signal Dispatcher" daemon prio=1 tid=0x080ea0f0 nid=0xb75 runnable [0x00000000..0x00000000]
"Finalizer" daemon prio=1 tid=0x080be428 nid=0xb74 waiting for monitor entry [0xaa43d000..0xaa43eb50] at org.apache.bcel.classfile.ConstantPool.<init>(ConstantPool.java:97) at org.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:254) at org.apache.bcel.classfile.ClassParser.parse(ClassParser.java:162) at MyAgent.transform(MyAgent.java:28) at sun.instrument.TransformerManager.transform(TransformerManager.java:122) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:155) at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:82) at java.lang.ref.Finalizer.access$100(Finalizer.java:14) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)
"Reference Handler" daemon prio=1 tid=0x080bcca0 nid=0xb73 in Object.wait() [0xaa4be000..0xaa4bebd0] at java.lang.Object.wait(Native Method) - waiting on <0xaa6502a8> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:474) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116) - locked <0xaa6502a8> (a java.lang.ref.Reference$Lock)
"main" prio=1 tid=0x0805cb58 nid=0xb70 waiting for monitor entry [0xbfd14000..0xbfd175c8] at java.lang.ClassLoader.findBootstrapClass(Native Method) at java.lang.ClassLoader.findBootstrapClass0(ClassLoader.java:891) at java.lang.ClassLoader.loadClass(ClassLoader.java:301) - locked <0xaa653038> (a sun.misc.Launcher$ExtClassLoader) at java.lang.ClassLoader.loadClass(ClassLoader.java:299) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at org.apache.bcel.classfile.ConstantPool.<init>(ConstantPool.java:103) at org.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:254) at org.apache.bcel.classfile.ClassParser.parse(ClassParser.java:162) at MyAgent.transform(MyAgent.java:28) at sun.instrument.TransformerManager.transform(TransformerManager.java:122) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:155) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:620) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at java.net.URLClassLoader.defineClass(URLClassLoader.java:260) at java.net.URLClassLoader.access$100(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader)
"VM Thread" prio=1 tid=0x080b6b48 nid=0xb72 runnable
"VM Periodic Task Thread" prio=1 tid=0x080f0e08 nid=0xb78 waiting on condition
Found one Java-level deadlock: ============================= "Finalizer": waiting to lock monitor 0x080bfddc (object 0xaa6502b0, a sun.misc.Launcher$AppClassLoader), which is held by "main" "main": waiting to lock monitor 0x080bfe1c (object 0xae5c4860, a [[I), which is held by "Finalizer"
Java stack information for the threads listed above: =================================================== "Finalizer": at org.apache.bcel.classfile.ConstantPool.<init>(ConstantPool.java:97) at org.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:254) at org.apache.bcel.classfile.ClassParser.parse(ClassParser.java:162) at MyAgent.transform(MyAgent.java:28) at sun.instrument.TransformerManager.transform(TransformerManager.java:122) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:155) at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:82) at java.lang.ref.Finalizer.access$100(Finalizer.java:14) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160) "main": at java.lang.ClassLoader.findBootstrapClass(Native Method) at java.lang.ClassLoader.findBootstrapClass0(ClassLoader.java:891) at java.lang.ClassLoader.loadClass(ClassLoader.java:301) - locked <0xaa653038> (a sun.misc.Launcher$ExtClassLoader) at java.lang.ClassLoader.loadClass(ClassLoader.java:299) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at org.apache.bcel.classfile.ConstantPool.<init>(ConstantPool.java:103) at org.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:254) at org.apache.bcel.classfile.ClassParser.parse(ClassParser.java:162) at MyAgent.transform(MyAgent.java:28) at sun.instrument.TransformerManager.transform(TransformerManager.java:122) at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:155) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:620) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at java.net.URLClassLoader.defineClass(URLClassLoader.java:260) at java.net.URLClassLoader.access$100(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319) - locked <0xaa6502b0> (a sun.misc.Launcher$AppClassLoader)
Found 1 deadlock.
Roedy Green - 09 Dec 2005 17:22 GMT On Fri, 09 Dec 2005 08:48:34 +0100, Francesco Devittori <frenkatfrenkdtcm> wrote, quoted or indirectly quoted someone who said
> java -cp hello:bcel-5.1.jar -javaagent:MyAgent.jar HelloWorld -javaagent:MyAgent.jar what kind of syntax is that? Do you mean that to be appended to the classpath?
don't you mean?
java -cp hello:bcel-5.1.jar:javaagent:MyAgent.jar HelloWorld
you can use the where utility or -verbose to see where Java is finding its classes. Keep in mind that . is the root of packages. It will find classes all over the tree starting there.
see http://mindprod.com/jgloss/where.html
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Thomas Fritsch - 09 Dec 2005 17:46 GMT > On Fri, 09 Dec 2005 08:48:34 +0100, Francesco Devittori > <frenkatfrenkdtcm> wrote, quoted or indirectly quoted someone who said [quoted text clipped - 4 lines] > what kind of syntax is that? Do you mean that to be appended to the > classpath? It is a new command-line syntax introduced with Java 1.5, for adding so-called agent-libraries to the JVM. For some reason Sun separated it from the classpath mechanism (maybe to make the agent classes un-callable by the user's application). The description is well-hidden in http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/package-summary.html in paragraph "Command-Line Interface".
 Signature "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Roedy Green - 09 Dec 2005 23:37 GMT On Fri, 09 Dec 2005 17:46:56 GMT, Thomas Fritsch <i.dont.like.spam@invalid.com> wrote, quoted or indirectly quoted someone who said :
>It is a new command-line syntax introduced with Java 1.5, it does not show up when you type plain java. Note the quirky extra colon.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 10 Dec 2005 00:54 GMT > On Fri, 09 Dec 2005 17:46:56 GMT, Thomas Fritsch > <i.dont.like.spam@invalid.com> wrote, quoted or indirectly quoted [quoted text clipped - 3 lines] > > it does not show up when you type plain java. It does for me (1.5.0_06). Right at the bottom.
> Note the quirky extra colon. It's the same syntax for everything except -classpath/-cp and -D for java (javac has a mix).
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Francesco Devittori - 11 Dec 2005 09:46 GMT > On Fri, 09 Dec 2005 17:46:56 GMT, Thomas Fritsch > <i.dont.like.spam@invalid.com> wrote, quoted or indirectly quoted [quoted text clipped - 4 lines] > it does not show up when you type plain java. > Note the quirky extra colon. It should, I'm using sun's jdk 1.5.0_05 and it's the last option it shows.
Thomas Hawtin - 10 Dec 2005 01:07 GMT >>> 3) a very strange problem: if you run your class with a classpath >>> that includes the .class of your agent, then the application gets >>> stuck at some random point (you have to Ctrl-Z/pkill -9 java). >> >> Can you find the stack with Ctrl-\ (or ctrl-break(?) for Windows)?
> Found one Java-level deadlock: > ============================= [quoted text clipped - 5 lines] > waiting to lock monitor 0x080bfe1c (object 0xae5c4860, a [[I), > which is held by "Finalizer" I'm not sure what is locking the array of array of ints, it appears to be in native code. My guess is that it represents a class-loader type object for Java Agents. That or something else to do with class loading.
I believe Java Agent classes should be the last resort after all applicable class-loaders, including the bootstrap class-loader, have not found the item. sun.misc.Launcher$AppClassLoader is the system class-loader for the application.
As I understand it: What can happen, is that a code with its class-loader locked, load a class from a child class-loader. The usual order of nested synchronisation is from parent class loader to child. So we have a chance of deadlock.
I think much the same thing is happening here. So, make sure your Java Agent only appear in the agent path. For libraries like BCEL, you may need to rename packages.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Francesco Devittori - 11 Dec 2005 09:48 GMT >>>> 3) a very strange problem: if you run your class with a classpath >>>> that includes the .class of your agent, then the application gets [quoted text clipped - 31 lines] > > Tom Hawtin This makes sense. The only thing I don't understand, is why to rename BCEL?
Thomas Hawtin - 11 Dec 2005 19:16 GMT > This makes sense. The only thing I don't understand, is why to rename BCEL? If the application uses BCEL for something, then you are going to have the classes in two places at once, and the possibility of the same deadlock.
In any case the application may use a different version of BCEL from the instrumentation. In that case the instrumentation may fail mysteriously. Particularly with the way the current instrument API swallows exceptions.
Tom Hawtin
 Signature Unemployed English Java programmer http://jroller.com/page/tackline/
Andrew Thompson - 09 Dec 2005 09:08 GMT ...
> Sorry for the long message, What long* message? Is my server dropping posts?
* My take is that a message has to be 100+ lines before I'd consider it to be long (others have different views, of course, but I'd be interested to hear the 'limit' expressed by anybody that disagrees with '100' lines.
OTOH - an interesting or detailed message might breach 200 lines and still not be -long-. (Of course, fewer people will read longer messages.)
> ...but maybe someone is interested. I got interested the moment I realised you were talking about a core Java package I'd never heard of.
In any case, I've not dealt with it, but I will stay tuned in the hope that the technical question is resolved.
Good luck with it.
 Signature Andrew Thompson physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Francesco Devittori - 11 Dec 2005 09:49 GMT > ... > >> Sorry for the long message, > > What long* message? Is my server dropping posts? Sorry, I wrote a long message, edited it and forgot to remove the "sorry for the long message" :-)
> * My take is that a message has to be 100+ lines before I'd > consider it to be long (others have different views, of course, [quoted text clipped - 14 lines] > > Good luck with it. Thanks!
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|