Skip to content

Commit

Permalink
trace line number support (alibaba#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhiyiYoh authored and hengyunabc committed May 9, 2019
1 parent bb750e9 commit 30fea92
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ private static void initSpy(ClassLoader classLoader) throws ClassNotFoundExcepti
String.class, String.class, Object.class, Object[].class);
Method onReturn = adviceWeaverClass.getMethod(ON_RETURN, Object.class);
Method onThrows = adviceWeaverClass.getMethod(ON_THROWS, Throwable.class);
Method beforeInvoke = adviceWeaverClass.getMethod(BEFORE_INVOKE, int.class, String.class, String.class, String.class);
Method afterInvoke = adviceWeaverClass.getMethod(AFTER_INVOKE, int.class, String.class, String.class, String.class);
Method throwInvoke = adviceWeaverClass.getMethod(THROW_INVOKE, int.class, String.class, String.class, String.class);
Method beforeInvoke = adviceWeaverClass.getMethod(BEFORE_INVOKE, int.class, String.class, String.class, String.class, int.class);
Method afterInvoke = adviceWeaverClass.getMethod(AFTER_INVOKE, int.class, String.class, String.class, String.class, int.class);
Method throwInvoke = adviceWeaverClass.getMethod(THROW_INVOKE, int.class, String.class, String.class, String.class, int.class);
Method reset = AgentBootstrap.class.getMethod(RESET);
Spy.initForAgentLauncher(classLoader, onBefore, onReturn, onThrows, beforeInvoke, afterInvoke, throwInvoke, reset);
}
Expand Down
1 change: 0 additions & 1 deletion boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<groupId>com.alibaba.middleware</groupId>
<artifactId>cli</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
39 changes: 26 additions & 13 deletions core/src/main/java/com/taobao/arthas/core/advisor/AdviceWeaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ private static void methodOnEnd(boolean isThrowing, Object returnOrThrowable) {
* @param name 调用方法名
* @param desc 调用方法描述
*/
public static void methodOnInvokeBeforeTracing(int adviceId, String owner, String name, String desc) {
public static void methodOnInvokeBeforeTracing(int adviceId, String owner, String name, String desc, int lineNumber) {
final InvokeTraceable listener = (InvokeTraceable) getListener(adviceId);
if (null != listener) {
try {
listener.invokeBeforeTracing(owner, name, desc);
listener.invokeBeforeTracing(owner, name, desc, lineNumber);
} catch (Throwable t) {
logger.warn("advice before tracing failed.", t);
}
Expand All @@ -190,11 +190,11 @@ public static void methodOnInvokeBeforeTracing(int adviceId, String owner, Strin
* @param name 调用方法名
* @param desc 调用方法描述
*/
public static void methodOnInvokeAfterTracing(int adviceId, String owner, String name, String desc) {
public static void methodOnInvokeAfterTracing(int adviceId, String owner, String name, String desc, int lineNumber) {
final InvokeTraceable listener = (InvokeTraceable) getListener(adviceId);
if (null != listener) {
try {
listener.invokeAfterTracing(owner, name, desc);
listener.invokeAfterTracing(owner, name, desc, lineNumber);
} catch (Throwable t) {
logger.warn("advice after tracing failed.", t);
}
Expand All @@ -209,11 +209,11 @@ public static void methodOnInvokeAfterTracing(int adviceId, String owner, String
* @param name 调用方法名
* @param desc 调用方法描述
*/
public static void methodOnInvokeThrowTracing(int adviceId, String owner, String name, String desc) {
public static void methodOnInvokeThrowTracing(int adviceId, String owner, String name, String desc, int lineNumber) {
final InvokeTraceable listener = (InvokeTraceable) getListener(adviceId);
if (null != listener) {
try {
listener.invokeThrowTracing(owner, name, desc);
listener.invokeThrowTracing(owner, name, desc, lineNumber);
} catch (Throwable t) {
logger.warn("advice throw tracing failed.", t);
}
Expand Down Expand Up @@ -442,6 +442,7 @@ public MethodVisitor visitMethod(
// 代码锁
private final CodeLock codeLockForTracing = new TracingAsmCodeLock(this);

private int lineNumber;

private void _debug(final StringBuilder append, final String msg) {

Expand Down Expand Up @@ -740,6 +741,12 @@ public void code() {
super.visitMaxs(maxStack, maxLocals);
}

@Override
public void visitLineNumber(int line, Label start) {
super.visitLineNumber(line, start);
lineNumber = line;
}

/**
* 是否静态方法
* @return true:静态方法 / false:非静态方法
Expand Down Expand Up @@ -819,8 +826,8 @@ private void loadThrow() {
/**
* 加载方法调用跟踪通知所需参数数组
*/
private void loadArrayForInvokeTracing(String owner, String name, String desc) {
push(4);
private void loadArrayForInvokeTracing(String owner, String name, String desc, int lineNumber) {
push(5);
newArray(ASM_TYPE_OBJECT);

dup();
Expand All @@ -843,6 +850,12 @@ private void loadArrayForInvokeTracing(String owner, String name, String desc) {
push(3);
push(desc);
arrayStore(ASM_TYPE_STRING);

dup();
push(4);
push(lineNumber);
box(ASM_TYPE_INT);
arrayStore(ASM_TYPE_INTEGER);
}


Expand Down Expand Up @@ -871,7 +884,7 @@ public void visitEnd() {
/*
* 跟踪代码
*/
private void tracing(final int tracingType, final String owner, final String name, final String desc) {
private void tracing(final int tracingType, final String owner, final String name, final String desc, final int lineNumber) {

final String label;
switch (tracingType) {
Expand Down Expand Up @@ -903,7 +916,7 @@ public void code() {
_debug(append, "loadAdviceMethod()");

pushNull();
loadArrayForInvokeTracing(owner, name, desc);
loadArrayForInvokeTracing(owner, name, desc, lineNumber);
_debug(append, "loadArrayForInvokeTracing()");

invokeVirtual(ASM_TYPE_METHOD, ASM_METHOD_METHOD_INVOKE);
Expand Down Expand Up @@ -934,7 +947,7 @@ public void visitMethodInsn(int opcode, final String owner, final String name, f
}

// 方法调用前通知
tracing(KEY_ARTHAS_ADVICE_BEFORE_INVOKING_METHOD, owner, name, desc);
tracing(KEY_ARTHAS_ADVICE_BEFORE_INVOKING_METHOD, owner, name, desc, lineNumber);

final Label beginLabel = new Label();
final Label endLabel = new Label();
Expand All @@ -948,15 +961,15 @@ public void visitMethodInsn(int opcode, final String owner, final String name, f
mark(endLabel);

// 方法调用后通知
tracing(KEY_ARTHAS_ADVICE_AFTER_INVOKING_METHOD, owner, name, desc);
tracing(KEY_ARTHAS_ADVICE_AFTER_INVOKING_METHOD, owner, name, desc, lineNumber);
goTo(finallyLabel);

// }
// catch
// {

catchException(beginLabel, endLabel, ASM_TYPE_THROWABLE);
tracing(KEY_ARTHAS_ADVICE_THROW_INVOKING_METHOD, owner, name, desc);
tracing(KEY_ARTHAS_ADVICE_THROW_INVOKING_METHOD, owner, name, desc, lineNumber);

throwException();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ public interface InvokeTraceable {
* @param tracingClassName 调用类名
* @param tracingMethodName 调用方法名
* @param tracingMethodDesc 调用方法描述
* @param tracingLineNumber 执行调用行数
* @throws Throwable 通知过程出错
*/
void invokeBeforeTracing(
String tracingClassName,
String tracingMethodName,
String tracingMethodDesc) throws Throwable;
String tracingMethodDesc,
int tracingLineNumber) throws Throwable;

/**
* 抛异常后跟踪
*
* @param tracingClassName 调用类名
* @param tracingMethodName 调用方法名
* @param tracingMethodDesc 调用方法描述
* @param tracingLineNumber 执行调用行数
* @throws Throwable 通知过程出错
*/
void invokeThrowTracing(
String tracingClassName,
String tracingMethodName,
String tracingMethodDesc) throws Throwable;
String tracingMethodDesc,
int tracingLineNumber) throws Throwable;


/**
Expand All @@ -40,12 +44,14 @@ void invokeThrowTracing(
* @param tracingClassName 调用类名
* @param tracingMethodName 调用方法名
* @param tracingMethodDesc 调用方法描述
* @param tracingLineNumber 执行调用行数
* @throws Throwable 通知过程出错
*/
void invokeAfterTracing(
String tracingClassName,
String tracingMethodName,
String tracingMethodDesc) throws Throwable;
String tracingMethodDesc,
int tracingLineNumber) throws Throwable;


}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void afterReturning(ClassLoader loader, Class<?> clazz, ArthasMethod meth
@Override
public void afterThrowing(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args,
Throwable throwable) throws Throwable {
threadBoundEntity.get().view.begin("throw:" + throwable.getClass().getName() + "()").end().end();
int lineNumber = throwable.getStackTrace()[0].getLineNumber();
threadBoundEntity.get().view.begin("throw:" + throwable.getClass().getName() + "()" + " #" + lineNumber).end().end();
final Advice advice = Advice.newForAfterThrowing(loader, clazz, method, target, args, throwable);
finishing(advice);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ public TraceAdviceListener(TraceCommand command, CommandProcess process) {
* trace 会在被观测的方法体中,在每个方法调用前后插入字节码,所以方法调用开始,结束,抛异常的时候,都会回调下面的接口
*/
@Override
public void invokeBeforeTracing(String tracingClassName, String tracingMethodName, String tracingMethodDesc)
public void invokeBeforeTracing(String tracingClassName, String tracingMethodName, String tracingMethodDesc, int tracingLineNumber)
throws Throwable {
threadBoundEntity.get().view.begin(
StringUtils.normalizeClassName(tracingClassName) + ":" + tracingMethodName + "()");
StringUtils.normalizeClassName(tracingClassName) + ":" + tracingMethodName + "()" + " #" + tracingLineNumber);
}

@Override
public void invokeAfterTracing(String tracingClassName, String tracingMethodName, String tracingMethodDesc)
public void invokeAfterTracing(String tracingClassName, String tracingMethodName, String tracingMethodDesc, int tracingLineNumber)
throws Throwable {
threadBoundEntity.get().view.end();
}

@Override
public void invokeThrowTracing(String tracingClassName, String tracingMethodName, String tracingMethodDesc)
public void invokeThrowTracing(String tracingClassName, String tracingMethodName, String tracingMethodDesc, int tracingLineNumber)
throws Throwable {
threadBoundEntity.get().view.end("throws Exception");
}
Expand Down

0 comments on commit 30fea92

Please sign in to comment.