| RE: [aspectj-users] LTW Agent Dynamic Attachement with Attach API |
|
A step more but unfortunnaly
not the last I have a little
application that is correctly woven when it is launched with the agent
myaspectjweaver.jar (myaspectjweaver.jar aspectjweaver.jar + aspects package+
aop.xml+ aspects configuration) as shown as below : E:\opt\perfStats\scripts>"C:\Program
Files\Java\jdk1.6.0_11\bin\java" -javaagent:E:\\opt\\perfStats\\myaspectjweaver.jar
-classpath "C:\Program Files\Java\jdk1.6.0_11\lib\tools.jar";..\exemple1.jar;;.
jlp.exemple1.jvm14.Main [AppClassLoader@df6ccd]
info AspectJ Weaver Version DEVELOPMENT built on Thursday Dec 18, 2008 at
18:33:02 GMT [AppClassLoader@df6ccd]
info register classloader sun.misc.Launcher$AppClassLoader@df6ccd [AppClassLoader@df6ccd]
info using configuration file:/E:/opt/perfStats/myaspectjweaver.jar!/META-INF/aop.xml [AppClassLoader@df6ccd]
info define aspect iep.perf.aspects.abstractAspects.AbstractDurationMethodImpl [AppClassLoader@df6ccd]
weaveinfo Join point 'method-call(void jlp.exemple1.jvm14.Main.addNumberCircle(int))'
in Type 'jlp.exemple1.jvm14.Main' (Main.java:71) advised by around advice from
'iep.perf.aspects.abstractAspects.AbstractDurationMethodImpl'
(AbstractDurationMethod.aj:53) [AppClassLoader@df6ccd]
weaveinfo Join point 'method-call(double jlp.exemple1.jvm14.Circle.perimeter())'
in Type 'jlp.exemple1.jvm14.Main' (Main.java:75) advised by around advice from
'iep.perf.aspects.abstractAspects.AbstractDurationMethodImpl'
(AbstractDurationMethod.aj:53) [AppClassLoader@df6ccd]
weaveinfo Join point 'method-call(void jlp.exemple1.jvm14.Circle.externalCircle())'
in Type 'jlp.exemple1.jvm14.Main' (Main.java:78) advised by around advice from
'iep.perf.aspects.abstractAspects.AbstractDurationMethodImpl'
(AbstractDurationMethod.aj:53) The 3 last lines show
that aspects are woven, when the agent is launched with the start of
application. For test with the Attach
API, I launch the application, the first that do the main method is to sleep 1
mn. I can get the PID of the java VM with jps and afterwards I can launch the tool
to attach the javaagent myaspectjweaver.jar. The logs below show that the agent
is correctly loaded, and the aop.xml correctly read, but threr is no weaving
after ;-( E:\opt\perfStats\scripts>"C:\Program
Files\Java\jdk1.6.0_11\bin\java" -classpath "C:\Program
Files\Java\jdk1.6.0_11\lib\tools.jar";..\exemple1.jar;;. jlp.exemple1.jvm14.Main
100 60000 [AppClassLoader@11b86e7]
info AspectJ Weaver Version DEVELOPMENT built on Thursd ay Dec 18, 2008
at 18:33:02 GMT [AppClassLoader@11b86e7]
info register classloader sun.misc.Launcher$AppClassLoader@11b86e7 [AppClassLoader@11b86e7]
info using configuration file:/E:/opt/perfStats/lib/myaspectjweaver.jar!/META-INF/aop.xml [AppClassLoader@11b86e7]
info define aspect iep.perf.aspects.abstractAspects.AbstractDurationMethodImpl External public
Call from Circle Internal
Private Call from Circle External public
Call from Circle Internal
Private Call from Circle External public
Call from Circle Internal
Private Call from Circle External public
Call from Circle External public
Call from Circle Internal
Private Call from Circle External public
Call from Circle Internal
Private Call from Circle External public
Call from Circle So it is not a bug of
AspectJ1.6.3, because it is not a feature of this release. I will add this request
for the next version …. Cordialement / Best
regards Jean-Louis Pasturel De :
aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-bounces@xxxxxxxxxxx]
De la part de jeanlouis.pasturel@xxxxxxxxxxxxxxxxxx Hi Andy, I have progressed, the agent
is correctly loaded, I have just add the method agentmain to org.aspectj.weaver.loadtime.Agent
. : public static void agentmain(String options,
Instrumentation instrumentation) { if(s_instrumentation != null) {
return; } else {
s_instrumentation = instrumentation;
s_instrumentation.addTransformer(s_transformer);
return; }
} The code is the
same as the premain existing
method. Below the code for loading
the agent : package attachapi.launcher; import java.io.File; import java.io.IOException; import
com.sun.tools.attach.AgentInitializationException; import
com.sun.tools.attach.AgentLoadException; import
com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.VirtualMachine; public class PerfStatsLaunch { public VirtualMachine vm =null; public int pidVm=-1; String agent =""; public PerfStatsLaunch(int pidVm, String path) {
super();
this.pidVm = pidVm;
this.agent=path;
try {
vm = VirtualMachine.attach(Integer.toString(pidVm));
} catch (AttachNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void attachPerfStats() {
try {
System.out.println("loading agent : "+agent);
vm.loadAgent(agent,null);
} catch (AgentLoadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AgentInitializationException e) {
// TODO Auto-generated catch block
System.out.println("Echec Attach ");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void detachPerfStats() {
try {
vm.detach();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /** * @param args */ public static void main(String[] args) {
if(args.length!=2)
{
System.out.println("usage :\n"+
"java
-classpath <path to myaspectjweaver.jar>:<path to tools.jar>
attachapi.launcher.PerfStats pidJVM fullpathToAgent\n");
System.exit(1);
}
PerfStatsLaunch psl=new PerfStatsLaunch(Integer.parseInt(args[0]),args[1]);
psl.attachPerfStats();
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
psl.detachPerfStats();
} } Next step : =>
obtaining weaving that a more bit difficult ( ClassLoaders must works …) Cordialement / Best regards Jean-Louis Pasturel De :
aspectj-users-bounces@xxxxxxxxxxx [mailto:aspectj-users-bounces@xxxxxxxxxxx] De la part de Andy Clement Hi Jean-Louis, 2009/1/6 <jeanlouis.pasturel@xxxxxxxxxxxxxxxxxx> I have yet asked the question, and I made a try. JDK 1.6 brings Sun Api to dynamically load java
Agent http://java.sun.com/javase/6/docs/technotes/guides/attach/index.html I test it with aspectjweaver.jar ( in fact a
custom myaspectjweaver.jar that contains aspectjweaver.jar ) but I can't
success a run. Does-it possible to attach LTW agent, when an
application is already running? Or the LTW agent *must* be loaded at starting? I see that there is some thing similar with
the Equinox/Aspects; project in status incubator… I load the Agent but the initialisation failed as
shown with this traces: E:\opt\perfStats\scripts>"C:\Program
Files\Java\jdk1.6.0_11\bin\java" -classpath
..\lib\myaspectjweaver.jar;"C:\Program
Files\Java\jdk1.6.0_11\lib\tools.jar"; ;. attachapi.launcher.PerfStatsLaunch 4668
E:\opt\perfStats\lib\myaspectjweaver.jar loading agent :
E:\opt\perfStats\lib\myaspectjweaver.jar Echec Attach com.sun.tools.attach.AgentInitializationException:
Agent JAR loaded but agent failed to initialize at
sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:103) at
attachapi.launcher.PerfStatsLaunch.attachPerfStats(PerfStatsLaunch.java:33) at
attachapi.launcher.PerfStatsLaunch.main(PerfStatsLaunch.java:69) My MANIFEST.MF Manifest-Version: 1.0 Name: org/aspectj/weaver/ Specification-Title: AspectJ Weaver Classes Specification-Version: DEVELOPMENT Specification-Vendor: aspectj.org Implementation-Title: org.aspectj.weaver Implementation-Version: DEVELOPMENT Implementation-Vendor: aspectj.org Premain-Class: org.aspectj.weaver.loadtime.Agent Can-Redefine-Classes: true Agent-Class: org.aspectj.weaver.loadtime.Agent I added the Agent-Class line. Cordialement / Best regards Jean-Louis Pasturel
|