Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 31 additions & 51 deletions async/src/main/java/com/ea/async/instrumentation/Agent.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,56 @@
/*
Copyright (C) 2015 Electronic Arts Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.async.instrumentation;

import java.lang.instrument.Instrumentation;

/**
* Class called when a java agent is attached to the jvm in runtime.
* Class called when a java agent is attached to the JVM in runtime.
*/
public class Agent
{
public class Agent {

/*
* From https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/instrument/Instrumentation.html
*
*
* Agent-Class
*
* If an implementation supports a mechanism to start agents sometime
* after the VM has started then this attribute specifies the agent class.
* That is, the class containing the agentmain method. This attribute is
* required, if it is not present the agent will not be started. Note: this
* is a class name, not a file name or path.
*
* If an implementation supports a mechanism to start agents sometime after the VM has started,
* then this attribute specifies the agent class. That is, the class containing the agentmain method.
* This attribute is required; if it is not present, the agent will not be started.
* Note: this is a class name, not a file name or path.
*/
public static void agentmain(String agentArgs, Instrumentation inst)
{
public static void agentmain(String agentArgs, Instrumentation inst) {
Transformer transformer = new Transformer();
inst.addTransformer(transformer, true);

// Loop through all loaded classes and retransform the ones that need instrumentation
f1:
for (Class<?> clazz : inst.getAllLoadedClasses())
{
for (Class<?> clazz : inst.getAllLoadedClasses()) {
if (inst.isModifiableClass(clazz)
&& !clazz.getName().startsWith("java.")
&& !clazz.getName().startsWith("javax.")
&& !clazz.getName().startsWith("sun."))
{
try
{
if (transformer.needsInstrumentation(clazz))
{
&& !clazz.getName().startsWith("sun.")) {
try {
// Avoid retransformation if the class is already instrumented
if (transformer.needsInstrumentation(clazz) && !alreadyInstrumented(clazz)) {
inst.retransformClasses(clazz);
markAsInstrumented(clazz);
}
}
catch (Exception | Error e)
{
} catch (Exception | Error e) {
e.printStackTrace();
}
}
}

// Set a system property to indicate that EA async is running
System.setProperty(Transformer.EA_ASYNC_RUNNING, "true");
}

// Helper method to check if a class has already been instrumented by this agent
private static boolean alreadyInstrumented(Class<?> clazz) {
return System.getProperty("instrumented." + clazz.getName()) != null;
}

// Helper method to mark a class as instrumented
private static void markAsInstrumented(Class<?> clazz) {
System.setProperty("instrumented." + clazz.getName(), "true");
}
}
Loading