Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] LTW Agent Dynamic Attachement with Attach API

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
Envoyé : mardi 6 janvier 2009 18:02
À : aspectj-users@xxxxxxxxxxx
Objet : Re: [aspectj-users] LTW Agent Dynamic Attachement with Attach API

 

Hi Jean-Louis,

I'd suspect we need the weaver to perhaps implement a new interface or two, so that it can be plugged in via that route - but I haven't looked into it.  I don't think we have an open enhancement request to support this, so you could create one.  And if you discover the magic we need to get it working, please add it to the enhancement - that would be the easiest way to get it done in the 1.6.4 timeframe.  I don't think it will be too complex as the weaver doesn't have any logic in to stop you trying to do this.

The disadvantage of attaching any time later than right up front is simply that things will not have been woven that have already been loaded (unless they are reloaded after the weaver is attached). 

Andy.

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

 

*********************************
This message and any attachments (the "message") are confidential and intended solely for the addressees.
Any unauthorised use or dissemination is prohibited.
Messages are susceptible to alteration.
France Telecom Group shall not be liable for the message if altered, changed or falsified.
If you are not the intended addressee of this message, please cancel it immediately and inform the sender.
********************************


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users

 

*********************************
This message and any attachments (the "message") are confidential and intended solely for the addressees.
Any unauthorised use or dissemination is prohibited.
Messages are susceptible to alteration.
France Telecom Group shall not be liable for the message if altered, changed or falsified.
If you are not the intended addressee of this message, please cancel it immediately and inform the sender.
********************************

Back to the top