Skip to content
This repository
Browse code

Add line number to traces

  • Loading branch information...
commit faea1c5c392766792bb34a0b4366022d58c9d4a9 1 parent 1f9e354
Kresten Krab Thorup krestenkrab authored
5 src/main/java/erjang/ErlangError.java
@@ -68,13 +68,14 @@ public ESeq getTrace() {
68 68
69 69 if (args != null && !trace.isNil()) {
70 70
71   - ETuple3 first = ETuple3.cast(trace.head().testTuple());
  71 + ETuple4 first = ETuple4.cast(trace.head().testTuple());
72 72 ESeq res = trace.tail().testSeq();
73 73
74   - ETuple3 fa = new ETuple3();
  74 + ETuple4 fa = new ETuple4();
75 75 fa.elem1 = first.elem1;
76 76 fa.elem2 = first.elem2;
77 77 fa.elem3 = args;
  78 + fa.elem4 = first.elem4;
78 79
79 80 trace = res.cons(fa);
80 81 }
18 src/main/java/erjang/ErlangException.java
@@ -35,6 +35,8 @@
35 35 static final EAtom am_error = EAtom.intern("error");
36 36 static final EAtom am_throw = EAtom.intern("throw");
37 37 static final EAtom am_exit = EAtom.intern("exit");
  38 + static final EAtom am_file = EAtom.intern("file");
  39 + static final EAtom am_line = EAtom.intern("line");
38 40 private EObject reason;
39 41
40 42 public abstract EAtom getExClass();
@@ -141,8 +143,8 @@ public ETuple testTuple() {
141 143 // the rest of this file is a big hack to reconstruct erlang traces...
142 144 //
143 145
144   - static Map<StackTraceElement, ETuple3> cache = Collections
145   - .synchronizedMap(new WeakHashMap<StackTraceElement, ETuple3>());
  146 + static Map<StackTraceElement, ETuple4> cache = Collections
  147 + .synchronizedMap(new WeakHashMap<StackTraceElement, ETuple4>());
146 148
147 149 public ESeq getTrace() {
148 150 return decodeTrace(getStackTrace());
@@ -165,7 +167,7 @@ public static ESeq decodeTrace(StackTraceElement[] st) {
165 167
166 168 StackTraceElement st2 = st[i];
167 169
168   - ETuple3 elem;
  170 + ETuple4 elem;
169 171
170 172 if ((elem = cache.get(st2)) != null) {
171 173 trace = trace.cons(elem);
@@ -187,7 +189,7 @@ public static ESeq decodeTrace(StackTraceElement[] st) {
187 189 * @param st
188 190 * @return
189 191 */
190   - private static ETuple3 decodeTraceElem(StackTraceElement st) {
  192 + private static ETuple4 decodeTraceElem(StackTraceElement st) {
191 193
192 194 String cname = st.getClassName();
193 195 String mname = st.getMethodName();
@@ -221,10 +223,11 @@ private static ETuple3 decodeTraceElem(StackTraceElement st) {
221 223 }
222 224
223 225 if (module != null && function != null && arity != -1) {
224   - ETuple3 res = new ETuple3();
  226 + ETuple4 res = new ETuple4();
225 227 res.elem1 = module;
226 228 res.elem2 = function;
227 229 res.elem3 = new ESmall(arity);
  230 + res.elem4 = EList.make(new ETuple2(am_file, new EString(st.getFileName())), new ETuple2(am_line, ERT.box(st.getLineNumber())));
228 231 return res;
229 232 }
230 233
@@ -257,7 +260,7 @@ private static ETuple3 decodeTraceElem(StackTraceElement st) {
257 260 * @param ann
258 261 * @return
259 262 */
260   - private static ETuple3 resolve(Class<?> c, Method m, BIF ann1) {
  263 + private static ETuple4 resolve(Class<?> c, Method m, BIF ann1) {
261 264
262 265 if (ann1 == null)
263 266 return null;
@@ -281,10 +284,11 @@ private static ETuple3 resolve(Class<?> c, Method m, BIF ann1) {
281 284 }
282 285
283 286 if (module != null && fun != null) {
284   - ETuple3 res = new ETuple3();
  287 + ETuple4 res = new ETuple4();
285 288 res.elem1 = EAtom.intern(module);
286 289 res.elem2 = EAtom.intern(fun);
287 290 res.elem3 = ESmall.make(arity);
  291 + res.elem4 = ERT.NIL;
288 292 return res;
289 293 } else {
290 294 return null;
2  src/main/java/erjang/ErlangUndefined.java
@@ -65,6 +65,6 @@ public ErlangUndefined(EAtom mod, EAtom fun, int length) {
65 65 */
66 66 @Override
67 67 public ESeq getTrace() {
68   - return super.getTrace().cons(ETuple.make(module, function, arity));
  68 + return super.getTrace().cons(ETuple.make(module, function, arity, ERT.NIL));
69 69 }
70 70 }
2  src/main/java/erjang/beam/BlockVisitor2.java
@@ -212,4 +212,6 @@ void visitTest(BeamOpcode test, int failLabel, Arg arg, Arg arity,
212 212 void visitDecrement(Arg src, Arg out);
213 213
214 214 void visitInitWritable(Arg size, Arg dest);
  215 +
  216 + void visitLine(int line);
215 217 }
34 src/main/java/erjang/beam/CompilerVisitor.java
@@ -101,8 +101,11 @@
101 101 // atom values is just encoded as an if-then-else-etc.
102 102 public static final int ATOM_SELECT_IF_ELSE_LIMIT = 4;
103 103
  104 + EAtom am_source = EAtom.intern("source");
  105 +
104 106 ECons atts = ERT.NIL;
105 107 ECons compile_info = ERT.NIL;
  108 + String source = null;
106 109 private Set<String> exported = new HashSet<String>();
107 110
108 111 private final ClassVisitor cv;
@@ -214,7 +217,6 @@ public void visitModule(EAtom name) {
214 217
215 218 add_module_annotation(cv);
216 219
217   - cv.visitSource(name.getName()+".S", null);
218 220 }
219 221
220 222 private void add_module_annotation(ClassVisitor cv) {
@@ -254,9 +256,26 @@ public void visitAttribute(EAtom att, EObject value) {
254 256
255 257 @Override
256 258 public void visitCompile(EAtom att, EObject value) {
257   - compile_info = compile_info.cons(ETuple2.make(att, value));
  259 + EString string;
  260 + if (att == am_source && (string = value.testString()) != null) {
  261 + source = string.stringValue();
  262 + }
  263 + compile_info = compile_info.cons(ETuple2.make(att, value));
258 264 }
259 265
  266 + String source() {
  267 + if (source == null) {
  268 + return module_name.getName() + ".erl";
  269 + } else {
  270 + int idx = source.lastIndexOf('/');
  271 + if (idx == -1) {
  272 + return source;
  273 + } else {
  274 + return source.substring(idx+1);
  275 + }
  276 + }
  277 + }
  278 +
260 279 /*
261 280 * (non-Javadoc)
262 281 *
@@ -265,6 +284,8 @@ public void visitCompile(EAtom att, EObject value) {
265 284 @Override
266 285 public void visitEnd() {
267 286
  287 + cv.visitSource(source(), null);
  288 +
268 289 // wow, this is where we generate <clinit>
269 290
270 291 for (Map.Entry<String, ExtFun> ent : imported.entrySet()) {
@@ -885,7 +906,7 @@ public BlockVisitor visitLabeledBlock(int label) {
885 906
886 907 mv.visitLabel(blockLabel);
887 908 label_inserted.add(label);
888   - mv.visitLineNumber(label & 0x7fff, blockLabel);
  909 +// mv.visitLineNumber(label & 0x7fff, blockLabel);
889 910 return new ASMBlockVisitor(label);
890 911 }
891 912
@@ -1200,6 +1221,13 @@ public void visitBitStringTest(BeamOpcode test, int failLabel, Arg in, Arg bits,
1200 1221 }
1201 1222
1202 1223 @Override
  1224 + public void visitLine(int line) {
  1225 + Label here = new Label();
  1226 + mv.visitLabel(here);
  1227 + mv.visitLineNumber(line, here);
  1228 + }
  1229 +
  1230 + @Override
1203 1231 public void visitBitStringTest(BeamOpcode test, int failLabel, Arg in, Arg bits, int unit, int flags, Arg dst) {
1204 1232 switch (test) {
1205 1233 case bs_get_binary2: {
2  src/main/java/erjang/beam/analysis/BeamTypeAnalysis.java
@@ -830,6 +830,8 @@ private void accept_2(BlockVisitor2 vis, BeamExceptionHandler exh) {
830 830 }
831 831
832 832 case line: // TODO!
  833 + vis.visitLine(((Insn.I)insn_).i1);
  834 +
833 835 case on_load: // ignore
834 836 case trim:
835 837 break;

0 comments on commit faea1c5

Please sign in to comment.
Something went wrong with that request. Please try again.