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

添加look命令,用来观测方法内的本地变量表,可以通过行号或者特殊Code(jad -L 来输出)来指定位置 #2435

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.taobao.arthas.core.advisor;

public enum AccessPoint {
ACCESS_BEFORE(1, "AtEnter"), ACCESS_AFTER_RETUNING(1 << 1, "AtExit"), ACCESS_AFTER_THROWING(1 << 2, "AtExceptionExit");
ACCESS_BEFORE(1, "AtEnter"), ACCESS_AFTER_RETUNING(1 << 1, "AtExit"), ACCESS_AFTER_THROWING(1 << 2, "AtExceptionExit"), ACCESS_LOOK_LOCATION(1 << 3, "atLookLocation");

private int value;

Expand Down
28 changes: 28 additions & 0 deletions core/src/main/java/com/taobao/arthas/core/advisor/Advice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.taobao.arthas.core.advisor;

import java.util.Map;

/**
* 通知点 Created by vlinux on 15/5/20.
*/
Expand All @@ -12,6 +14,7 @@ public class Advice {
private final Object[] params;
private final Object returnObj;
private final Throwable throwExp;
private final Map<String,Object> varMap;
private final boolean isBefore;
private final boolean isThrow;
private final boolean isReturn;
Expand Down Expand Up @@ -76,6 +79,7 @@ private Advice(
Object[] params,
Object returnObj,
Throwable throwExp,
Map<String, Object> varMap,
int access) {
this.loader = loader;
this.clazz = clazz;
Expand All @@ -84,6 +88,7 @@ private Advice(
this.params = params;
this.returnObj = returnObj;
this.throwExp = throwExp;
this.varMap = varMap;
isBefore = (access & AccessPoint.ACCESS_BEFORE.getValue()) == AccessPoint.ACCESS_BEFORE.getValue();
isThrow = (access & AccessPoint.ACCESS_AFTER_THROWING.getValue()) == AccessPoint.ACCESS_AFTER_THROWING.getValue();
isReturn = (access & AccessPoint.ACCESS_AFTER_RETUNING.getValue()) == AccessPoint.ACCESS_AFTER_RETUNING.getValue();
Expand All @@ -102,6 +107,7 @@ public static Advice newForBefore(ClassLoader loader,
params,
null, //returnObj
null, //throwExp
null,//varMap
AccessPoint.ACCESS_BEFORE.getValue()
);
}
Expand All @@ -120,6 +126,7 @@ public static Advice newForAfterReturning(ClassLoader loader,
params,
returnObj,
null, //throwExp
null,//varMap
AccessPoint.ACCESS_AFTER_RETUNING.getValue()
);
}
Expand All @@ -138,9 +145,30 @@ public static Advice newForAfterThrowing(ClassLoader loader,
params,
null, //returnObj
throwExp,
null,//varMap
AccessPoint.ACCESS_AFTER_THROWING.getValue()
);

}

public static Advice newForLooking(ClassLoader loader,
Class<?> clazz,
ArthasMethod method,
Object target,
Object[] params,
Map<String, Object> varMap) {
return new Advice(
loader,
clazz,
method,
target,
params,
null, //returnObj
null, //throwExp
varMap,
AccessPoint.ACCESS_LOOK_LOCATION.getValue()
);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,18 @@ void afterThrowing(
Object target, Object[] args,
Throwable throwable) throws Throwable;

/**
* look命令中使用,查看本地变量
* @param clazz 类
* @param methodName 方法名
* @param methodDesc 方法描述
* @param target 目标类实例,若目标为静态方法,则为null
* @param args 参数列表
* @param lookLocation LineNumber 或 LocationCode
* @param vars 本地变量数组
* @param varNames 本地变量名数组
* @throws Throwable 通知过程出错
*/
void atLookLocation(Class<?> clazz, String methodName, String methodDesc, Object target, Object[] args, String lookLocation, Object[] vars, String[] varNames) throws Throwable;

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ final public void afterThrowing(Class<?> clazz, String methodName, String method
throwable);
}

@Override
public void atLookLocation(Class<?> clazz, String methodName, String methodDesc, Object target, Object[] args, String lookLocation, Object[] vars, String[] varNames) throws Throwable {
atLookLocation(clazz.getClassLoader(), clazz, new ArthasMethod(clazz, methodName, methodDesc), target, args, lookLocation, vars, varNames);
}

/**
* 前置通知
*
Expand Down Expand Up @@ -106,6 +111,24 @@ public abstract void afterReturning(ClassLoader loader, Class<?> clazz, ArthasMe
public abstract void afterThrowing(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target,
Object[] args, Throwable throwable) throws Throwable;


/**
* look命令中使用,查看本地变量
*
* @param loader 类加载器
* @param clazz 类
* @param method 方法
* @param target 目标类实例,若目标为静态方法,则为null
* @param args 参数列表
* @param location look命令使用的location
* @param vars 本地变量数组
* @param varNames 本地变量名数组
* @throws Throwable 通知过程出错
*/
public void atLookLocation(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args, String location, Object[] vars, String[] varNames) throws Throwable{
//doing nothing by default
}

/**
* 判断条件是否满足,满足的情况下需要输出结果
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ private String keyForTrace(String className, String owner, String methodName, St
return className + owner + methodName + methodDesc;
}

private String keyForLook(String className, String lookLocation, String methodName, String methodDesc) {
return className + lookLocation + methodName + methodDesc;
}

public void registerAdviceListener(String className, String methodName, String methodDesc,
AdviceListener listener) {
synchronized (this) {
Expand Down Expand Up @@ -162,6 +166,33 @@ public List<AdviceListener> queryTraceAdviceListeners(String className, String o

return listeners;
}

public void registerLookAdviceListener(String className, String lookLocation, String methodName, String methodDesc,
AdviceListener listener) {

className = className.replace('/', '.');
String key = keyForLook(className, lookLocation, methodName, methodDesc);

List<AdviceListener> listeners = map.get(key);
if (listeners == null) {
listeners = new ArrayList<AdviceListener>();
map.put(key, listeners);
}
if (!listeners.contains(listener)) {
listeners.add(listener);
}
}

public List<AdviceListener> queryLookAdviceListeners(String className, String lookLocation, String methodName,
String methodDesc) {
className = className.replace('/', '.');
String key = keyForLook(className, lookLocation, methodName, methodDesc);

List<AdviceListener> listeners = map.get(key);

return listeners;
}

}

public static void registerAdviceListener(ClassLoader classLoader, String className, String methodName,
Expand Down Expand Up @@ -222,6 +253,33 @@ public static List<AdviceListener> queryTraceAdviceListeners(ClassLoader classLo
return null;
}

public static void registerLookAdviceListener(ClassLoader classLoader, String className, String lookLocation,
String methodName, String methodDesc, AdviceListener listener) {
classLoader = wrap(classLoader);
className = className.replace('/', '.');

ClassLoaderAdviceListenerManager manager = adviceListenerMap.get(classLoader);

if (manager == null) {
manager = new ClassLoaderAdviceListenerManager();
adviceListenerMap.put(classLoader, manager);
}
manager.registerLookAdviceListener(className, lookLocation, methodName, methodDesc, listener);
}

public static List<AdviceListener> queryLookAdviceListeners(ClassLoader classLoader, String className,
String loolLoc, String methodName, String methodDesc) {
classLoader = wrap(classLoader);
className = className.replace('/', '.');
ClassLoaderAdviceListenerManager manager = adviceListenerMap.get(classLoader);

if (manager != null) {
return manager.queryLookAdviceListeners(className, loolLoc, methodName, methodDesc);
}

return null;
}

private static ClassLoader wrap(ClassLoader classLoader) {
if (classLoader != null) {
return classLoader;
Expand Down
Loading