Skip to content

Commit c54154c

Browse files
author
caipeichao
committed
抛异常追踪
1 parent 8c8ac6e commit c54154c

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/main/java/gr/gousiosg/javacg/dyn/Instrumenter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import javassist.CtBehavior;
4444
import javassist.CtClass;
4545
import javassist.NotFoundException;
46+
import javassist.expr.ExprEditor;
47+
import javassist.expr.MethodCall;
4648

4749
public class Instrumenter implements ClassFileTransformer {
4850

@@ -169,9 +171,8 @@ private void enhanceMethod(CtBehavior method, String className)
169171

170172
if (method.getName().equals(name))
171173
methodName = "<init>";
172-
173174
method.insertBefore(String.format("gr.gousiosg.javacg.dyn.CallTrace.push(\"%s\",\"%s\");", className, methodName));
174-
method.insertAfter("gr.gousiosg.javacg.dyn.CallTrace.pop();");
175+
method.insertAfter("gr.gousiosg.javacg.dyn.CallTrace.pop();", true);
175176
}
176177

177178
private static void err(String msg) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package gr.gousiosg.javacg.test;
2+
3+
/**
4+
* Created by caipeichao on 16/12/10.
5+
*/
6+
public class TestException {
7+
8+
public static void main(String[] argv) {
9+
new TestException().run();
10+
}
11+
12+
public void run() {
13+
System.out.println("Hello world");
14+
foo();
15+
bar();
16+
}
17+
18+
public void foo() {
19+
System.out.println("Foo");
20+
throw new RuntimeException("Bad times");
21+
}
22+
23+
public void bar() {
24+
System.out.println("Bar");
25+
}
26+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
# check args
12
cd "`dirname "$0"`"
3+
c="$1"
4+
if [ "$c" == "" ] ; then
5+
echo try:
6+
ls ../src/main/java/gr/gousiosg/javacg/test | sed 's/\.java$//g' | xargs -n 1 echo 'bash' "$0"
7+
exit 1
8+
fi
9+
10+
# run test
211
cd ..
312
mvn package -DskipTests
413
cd target
514
export CLASSPATH=javacg-0.1-SNAPSHOT-test.jar
6-
java -javaagent:javacg-0.1-SNAPSHOT-dycg-agent.jar="incl=gr.gousiosg.javacg.test.*;" gr.gousiosg.javacg.test.TestOneThread
15+
java -javaagent:javacg-0.1-SNAPSHOT-dycg-agent.jar="incl=gr.gousiosg.javacg.test.*;" gr.gousiosg.javacg.test.$1
716
echo ------------------------
817
echo Call trace is:
918
cat calltrace.json

0 commit comments

Comments
 (0)