????????debug??????????????????′??????????????????????????????????java?????
????JDK?????????????
????1.premain?????Java SE5????????????????????????????????????jar????????????????main????????????????????????????????????????????????????????
????2.agentmain?????JavaSE6?????????????????ó????VM??????????????????
????agentmain??ó?????
???????????????????????£?????????????????????????????????????????????????????????Щ?????????????????????????????????????????
??????Permain?????agent??????????????agent jar?????????jar???????[?????????jar???]??
????1.??manifest?????Agent-Class??????????????·??
????2.???????????public static void agentmain(String args?? Instrumentation inst)??public static void agentmain(String args)????????????????????????????????args??inst??premain?е?????
??????????????????????????????????
?????????????£?
????Java Code ???????????
public class JavaAgent {
public static final Logger logger = LoggerFactory.getLogger(JavaAgent.class);
private static String classesPath;
private static String jarPath;
private static VirtualMachine vm;
private static String pid;
static {
classesPath = JavaAgent.class.getClassLoader().getResource("").getPath();
logger.error("java agent:classpath:{}"?? classesPath);
jarPath = getJarPath();
logger.error("java agent:jarPath:{}"?? jarPath);
// ???????pid
String name = ManagementFactory.getRuntimeMXBean().getName();
pid = name.split("@")[0];
logger.error("???????pid??{}"?? pid);
}
/**
* ???jar??·??
* @return
*/
public static String getJarPath() {
// StringUtils??jar???????
URL url = StringUtils.class.getProtectionDomain().getCodeSource().getLocation();
String filePath = null;
try {
filePath = URLDecoder.decode(url.getPath()?? "utf-8");// ????utf-8????
} catch (Exception e) {
e.printStackTrace();
}
if (filePath.endsWith(".jar")) {// ?????jar?????е????????".jar"
// ???·???е?jar????
filePath = filePath.substring(0?? filePath.lastIndexOf("/") + 1);
}
File file = new File(filePath);
filePath = file.getAbsolutePath();//???windows?μ????·??
return filePath;
}
private static void init() throws IOException?? AttachNotSupportedException?? AgentLoadException?? AgentInitializationException {
// ?????????
vm = VirtualMachine.attach(pid);
vm.loadAgent(jarPath + "/javaagent.jar");
Instrumentation instrumentation = JavaDynAgent.getInstrumentation();
Preconditions.checkNotNull(instrumentation?? "initInstrumentation must not be null");
}
private static void destroy() throws IOException {
if (vm != null) {
vm.detach();
}
}
/**
* ?????????
*
* @param classArr
* @throws Exception
*/
public static void javaAgent(String root?? String[] classArr) throws ClassNotFoundException?? IOException?? UnmodifiableClassException?? AttachNotSupportedException?? AgentLoadException?? AgentInitializationException {
init();
try {
// 1.???????????????
List<ClassDefinition> classDefList = new ArrayList<ClassDefinition>();
for (String className : classArr) {
Class<?> c = Class.forName(className);
String classPath = (StringUtils.isNotBlank(root) ? root : classesPath) + className.replace("."?? "/") + ".class";
logger.error("class redefined:" + classPath);
byte[] bytesFromFile = Files.toByteArray(new File(classPath));
ClassDefinition classDefinition = new ClassDefinition(c?? bytesFromFile);
classDefList.add(classDefinition);
}
// 2.redefine
JavaDynAgent.getInstrumentation().redefineClasses(classDefList.toArray(new ClassDefinition[classDefList.size()]));
} finally {
destroy();
}
}
public static void main(String[] args) throws Exception {
PortUtil.test();
javaAgent(null?? new String[] {"com.agileeagle.webgame.framework.util.PortUtil"});
PortUtil.test();
}
}