Permalink
Browse files

Add line number to traces

  • Loading branch information...
1 parent 1f9e354 commit faea1c5c392766792bb34a0b4366022d58c9d4a9 @krestenkrab krestenkrab committed Mar 22, 2013
View
5 src/main/java/erjang/ErlangError.java
@@ -68,13 +68,14 @@ public ESeq getTrace() {
if (args != null && !trace.isNil()) {
- ETuple3 first = ETuple3.cast(trace.head().testTuple());
+ ETuple4 first = ETuple4.cast(trace.head().testTuple());
ESeq res = trace.tail().testSeq();
- ETuple3 fa = new ETuple3();
+ ETuple4 fa = new ETuple4();
fa.elem1 = first.elem1;
fa.elem2 = first.elem2;
fa.elem3 = args;
+ fa.elem4 = first.elem4;
trace = res.cons(fa);
}
View
18 src/main/java/erjang/ErlangException.java
@@ -35,6 +35,8 @@
static final EAtom am_error = EAtom.intern("error");
static final EAtom am_throw = EAtom.intern("throw");
static final EAtom am_exit = EAtom.intern("exit");
+ static final EAtom am_file = EAtom.intern("file");
+ static final EAtom am_line = EAtom.intern("line");
private EObject reason;
public abstract EAtom getExClass();
@@ -141,8 +143,8 @@ public ETuple testTuple() {
// the rest of this file is a big hack to reconstruct erlang traces...
//
- static Map<StackTraceElement, ETuple3> cache = Collections
- .synchronizedMap(new WeakHashMap<StackTraceElement, ETuple3>());
+ static Map<StackTraceElement, ETuple4> cache = Collections
+ .synchronizedMap(new WeakHashMap<StackTraceElement, ETuple4>());
public ESeq getTrace() {
return decodeTrace(getStackTrace());
@@ -165,7 +167,7 @@ public static ESeq decodeTrace(StackTraceElement[] st) {
StackTraceElement st2 = st[i];
- ETuple3 elem;
+ ETuple4 elem;
if ((elem = cache.get(st2)) != null) {
trace = trace.cons(elem);
@@ -187,7 +189,7 @@ public static ESeq decodeTrace(StackTraceElement[] st) {
* @param st
* @return
*/
- private static ETuple3 decodeTraceElem(StackTraceElement st) {
+ private static ETuple4 decodeTraceElem(StackTraceElement st) {
String cname = st.getClassName();
String mname = st.getMethodName();
@@ -221,10 +223,11 @@ private static ETuple3 decodeTraceElem(StackTraceElement st) {
}
if (module != null && function != null && arity != -1) {
- ETuple3 res = new ETuple3();
+ ETuple4 res = new ETuple4();
res.elem1 = module;
res.elem2 = function;
res.elem3 = new ESmall(arity);
+ res.elem4 = EList.make(new ETuple2(am_file, new EString(st.getFileName())), new ETuple2(am_line, ERT.box(st.getLineNumber())));
return res;
}
@@ -257,7 +260,7 @@ private static ETuple3 decodeTraceElem(StackTraceElement st) {
* @param ann
* @return
*/
- private static ETuple3 resolve(Class<?> c, Method m, BIF ann1) {
+ private static ETuple4 resolve(Class<?> c, Method m, BIF ann1) {
if (ann1 == null)
return null;
@@ -281,10 +284,11 @@ private static ETuple3 resolve(Class<?> c, Method m, BIF ann1) {
}
if (module != null && fun != null) {
- ETuple3 res = new ETuple3();
+ ETuple4 res = new ETuple4();
res.elem1 = EAtom.intern(module);
res.elem2 = EAtom.intern(fun);
res.elem3 = ESmall.make(arity);
+ res.elem4 = ERT.NIL;
return res;
} else {
return null;
View
2 src/main/java/erjang/ErlangUndefined.java
@@ -65,6 +65,6 @@ public ErlangUndefined(EAtom mod, EAtom fun, int length) {
*/
@Override
public ESeq getTrace() {
- return super.getTrace().cons(ETuple.make(module, function, arity));
+ return super.getTrace().cons(ETuple.make(module, function, arity, ERT.NIL));
}
}
View
2 src/main/java/erjang/beam/BlockVisitor2.java
@@ -212,4 +212,6 @@ void visitTest(BeamOpcode test, int failLabel, Arg arg, Arg arity,
void visitDecrement(Arg src, Arg out);
void visitInitWritable(Arg size, Arg dest);
+
+ void visitLine(int line);
}
View
34 src/main/java/erjang/beam/CompilerVisitor.java
@@ -101,8 +101,11 @@
// atom values is just encoded as an if-then-else-etc.
public static final int ATOM_SELECT_IF_ELSE_LIMIT = 4;
+ EAtom am_source = EAtom.intern("source");
+
ECons atts = ERT.NIL;
ECons compile_info = ERT.NIL;
+ String source = null;
private Set<String> exported = new HashSet<String>();
private final ClassVisitor cv;
@@ -214,7 +217,6 @@ public void visitModule(EAtom name) {
add_module_annotation(cv);
- cv.visitSource(name.getName()+".S", null);
}
private void add_module_annotation(ClassVisitor cv) {
@@ -254,9 +256,26 @@ public void visitAttribute(EAtom att, EObject value) {
@Override
public void visitCompile(EAtom att, EObject value) {
- compile_info = compile_info.cons(ETuple2.make(att, value));
+ EString string;
+ if (att == am_source && (string = value.testString()) != null) {
+ source = string.stringValue();
+ }
+ compile_info = compile_info.cons(ETuple2.make(att, value));
}
+ String source() {
+ if (source == null) {
+ return module_name.getName() + ".erl";
+ } else {
+ int idx = source.lastIndexOf('/');
+ if (idx == -1) {
+ return source;
+ } else {
+ return source.substring(idx+1);
+ }
+ }
+ }
+
/*
* (non-Javadoc)
*
@@ -265,6 +284,8 @@ public void visitCompile(EAtom att, EObject value) {
@Override
public void visitEnd() {
+ cv.visitSource(source(), null);
+
// wow, this is where we generate <clinit>
for (Map.Entry<String, ExtFun> ent : imported.entrySet()) {
@@ -885,7 +906,7 @@ public BlockVisitor visitLabeledBlock(int label) {
mv.visitLabel(blockLabel);
label_inserted.add(label);
- mv.visitLineNumber(label & 0x7fff, blockLabel);
+// mv.visitLineNumber(label & 0x7fff, blockLabel);
return new ASMBlockVisitor(label);
}
@@ -1200,6 +1221,13 @@ public void visitBitStringTest(BeamOpcode test, int failLabel, Arg in, Arg bits,
}
@Override
+ public void visitLine(int line) {
+ Label here = new Label();
+ mv.visitLabel(here);
+ mv.visitLineNumber(line, here);
+ }
+
+ @Override
public void visitBitStringTest(BeamOpcode test, int failLabel, Arg in, Arg bits, int unit, int flags, Arg dst) {
switch (test) {
case bs_get_binary2: {
View
2 src/main/java/erjang/beam/analysis/BeamTypeAnalysis.java
@@ -830,6 +830,8 @@ private void accept_2(BlockVisitor2 vis, BeamExceptionHandler exh) {
}
case line: // TODO!
+ vis.visitLine(((Insn.I)insn_).i1);
+
case on_load: // ignore
case trim:
break;

0 comments on commit faea1c5

Please sign in to comment.