trace line number support (#668)

pull/666/head^2
徐志毅 6 years ago committed by hengyunabc
parent c3805d8dab
commit e188370099

@ -86,9 +86,9 @@ public class AgentBootstrap {
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);
}

@ -20,7 +20,6 @@
<groupId>com.alibaba.middleware</groupId>
<artifactId>cli</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

@ -171,11 +171,11 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
* @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);
}
@ -190,11 +190,11 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
* @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);
}
@ -209,11 +209,11 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
* @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);
}
@ -442,6 +442,7 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
// 代码锁
private final CodeLock codeLockForTracing = new TracingAsmCodeLock(this);
private int lineNumber;
private void _debug(final StringBuilder append, final String msg) {
@ -740,6 +741,12 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
super.visitMaxs(maxStack, maxLocals);
}
@Override
public void visitLineNumber(int line, Label start) {
super.visitLineNumber(line, start);
lineNumber = line;
}
/**
*
* @return true: / false:
@ -819,8 +826,8 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
/**
*
*/
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();
@ -843,6 +850,12 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
push(3);
push(desc);
arrayStore(ASM_TYPE_STRING);
dup();
push(4);
push(lineNumber);
box(ASM_TYPE_INT);
arrayStore(ASM_TYPE_INTEGER);
}
@ -871,7 +884,7 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
/*
*
*/
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) {
@ -903,7 +916,7 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
_debug(append, "loadAdviceMethod()");
pushNull();
loadArrayForInvokeTracing(owner, name, desc);
loadArrayForInvokeTracing(owner, name, desc, lineNumber);
_debug(append, "loadArrayForInvokeTracing()");
invokeVirtual(ASM_TYPE_METHOD, ASM_METHOD_METHOD_INVOKE);
@ -934,7 +947,7 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
}
// 方法调用前通知
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();
@ -948,7 +961,7 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
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);
// }
@ -956,7 +969,7 @@ public class AdviceWeaver extends ClassVisitor implements Opcodes {
// {
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();

@ -13,12 +13,14 @@ 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;
/**
*
@ -26,12 +28,14 @@ public interface InvokeTraceable {
* @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;
/**
@ -40,12 +44,14 @@ public interface InvokeTraceable {
* @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;
}

@ -57,7 +57,8 @@ public class AbstractTraceAdviceListener extends ReflectAdviceListenerAdapter {
@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);
}

@ -20,20 +20,20 @@ public class TraceAdviceListener extends AbstractTraceAdviceListener implements
* 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");
}

Loading…
Cancel
Save