001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.bytecode;
005:
006: import com.tc.asm.ClassAdapter;
007: import com.tc.asm.MethodAdapter;
008: import com.tc.asm.ClassVisitor;
009: import com.tc.asm.Label;
010: import com.tc.asm.MethodVisitor;
011: import com.tc.asm.Opcodes;
012:
013: public class JavaUtilConcurrentCyclicBarrierDebugClassAdapter extends
014: ClassAdapter implements Opcodes {
015:
016: public JavaUtilConcurrentCyclicBarrierDebugClassAdapter(
017: ClassVisitor cv) {
018: super (cv);
019: }
020:
021: public MethodVisitor visitMethod(int access, String name,
022: String desc, String signature, String[] exceptions) {
023: MethodVisitor mv = super .visitMethod(access, name, desc,
024: signature, exceptions);
025: if ("dowait".equals(name) && "(ZJ)I".equals(desc)) {
026: mv = new DoWaitMethodVisitor(mv);
027: }
028:
029: return mv;
030: }
031:
032: public void visitEnd() {
033: addDumpStateMethod();
034: super .visitEnd();
035: }
036:
037: private void addDumpStateMethod() {
038: MethodVisitor mv = super .visitMethod(ACC_PRIVATE
039: + ACC_SYNTHETIC, "dumpState", "()V", null, null);
040: mv.visitCode();
041: Label l0 = new Label();
042: mv.visitLabel(l0);
043: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
044: "Ljava/io/PrintStream;");
045: mv.visitLdcInsn("Current Status CyclicBarrier:");
046: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
047: "println", "(Ljava/lang/String;)V");
048: Label l1 = new Label();
049: mv.visitLabel(l1);
050: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
051: "Ljava/io/PrintStream;");
052: mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
053: mv.visitInsn(DUP);
054: mv.visitLdcInsn("-- parties: ");
055: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer",
056: "<init>", "(Ljava/lang/String;)V");
057: mv.visitVarInsn(ALOAD, 0);
058: mv.visitFieldInsn(GETFIELD,
059: "java/util/concurrent/CyclicBarrier", "parties", "I");
060: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
061: "append", "(I)Ljava/lang/StringBuffer;");
062: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
063: "toString", "()Ljava/lang/String;");
064: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
065: "println", "(Ljava/lang/String;)V");
066: Label l2 = new Label();
067: mv.visitLabel(l2);
068: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
069: "Ljava/io/PrintStream;");
070: mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
071: mv.visitInsn(DUP);
072: mv.visitLdcInsn("-- barrierCommand: ");
073: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer",
074: "<init>", "(Ljava/lang/String;)V");
075: mv.visitVarInsn(ALOAD, 0);
076: mv.visitFieldInsn(GETFIELD,
077: "java/util/concurrent/CyclicBarrier", "barrierCommand",
078: "Ljava/lang/Runnable;");
079: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
080: "append",
081: "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
082: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
083: "toString", "()Ljava/lang/String;");
084: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
085: "println", "(Ljava/lang/String;)V");
086: Label l3 = new Label();
087: mv.visitLabel(l3);
088: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
089: "Ljava/io/PrintStream;");
090: mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
091: mv.visitInsn(DUP);
092: mv.visitLdcInsn("-- generation: ");
093: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer",
094: "<init>", "(Ljava/lang/String;)V");
095: mv.visitVarInsn(ALOAD, 0);
096: mv.visitFieldInsn(GETFIELD,
097: "java/util/concurrent/CyclicBarrier", "generation",
098: "Ljava/util/concurrent/CyclicBarrier$Generation;");
099: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
100: "append",
101: "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
102: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
103: "toString", "()Ljava/lang/String;");
104: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
105: "println", "(Ljava/lang/String;)V");
106: Label l4 = new Label();
107: mv.visitLabel(l4);
108: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
109: "Ljava/io/PrintStream;");
110: mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
111: mv.visitInsn(DUP);
112: mv.visitLdcInsn("-- trip: ");
113: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer",
114: "<init>", "(Ljava/lang/String;)V");
115: mv.visitVarInsn(ALOAD, 0);
116: mv.visitFieldInsn(GETFIELD,
117: "java/util/concurrent/CyclicBarrier", "trip",
118: "Ljava/util/concurrent/locks/Condition;");
119: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
120: "append",
121: "(Ljava/lang/Object;)Ljava/lang/StringBuffer;");
122: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
123: "toString", "()Ljava/lang/String;");
124: mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
125: "println", "(Ljava/lang/String;)V");
126: Label l5 = new Label();
127: mv.visitLabel(l5);
128: mv.visitInsn(RETURN);
129: Label l6 = new Label();
130: mv.visitLabel(l6);
131: mv.visitMaxs(0, 0);
132: mv.visitEnd();
133: }
134:
135: private static class DoWaitMethodVisitor extends MethodAdapter
136: implements Opcodes {
137: private Label target;
138:
139: public DoWaitMethodVisitor(MethodVisitor mv) {
140: super (mv);
141: }
142:
143: public void visitJumpInsn(int opcode, Label label) {
144: super .visitJumpInsn(opcode, label);
145: if (IF_ACMPEQ == opcode) {
146: target = label;
147: }
148: }
149:
150: public void visitLabel(Label label) {
151: super .visitLabel(label);
152: if (label.equals(target)) {
153: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
154: "Ljava/io/PrintStream;");
155: mv
156: .visitLdcInsn("Wake up, but local generation the same as new generation.");
157: mv.visitMethodInsn(INVOKEVIRTUAL,
158: "java/io/PrintStream", "println",
159: "(Ljava/lang/String;)V");
160: Label l42 = new Label();
161: mv.visitLabel(l42);
162: mv.visitLineNumber(124, l42);
163: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
164: "Ljava/io/PrintStream;");
165: mv.visitLdcInsn("Entering debug block.");
166: mv.visitMethodInsn(INVOKEVIRTUAL,
167: "java/io/PrintStream", "println",
168: "(Ljava/lang/String;)V");
169: Label l43 = new Label();
170: mv.visitLabel(l43);
171: mv.visitLineNumber(125, l43);
172: mv.visitVarInsn(ALOAD, 0);
173: mv.visitMethodInsn(INVOKESPECIAL,
174: "java/util/concurrent/CyclicBarrier",
175: "dumpState", "()V");
176: Label l44 = new Label();
177: mv.visitLabel(l44);
178: mv.visitLineNumber(126, l44);
179: mv.visitLdcInsn(new Long(5000L));
180: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Thread",
181: "sleep", "(J)V");
182: Label l45 = new Label();
183: mv.visitLabel(l45);
184: mv.visitLineNumber(127, l45);
185: mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err",
186: "Ljava/io/PrintStream;");
187: mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
188: mv.visitInsn(DUP);
189: mv
190: .visitLdcInsn("Is local generation equal to new generation: ");
191: mv.visitMethodInsn(INVOKESPECIAL,
192: "java/lang/StringBuffer", "<init>",
193: "(Ljava/lang/String;)V");
194: mv.visitVarInsn(ALOAD, 5);
195: mv.visitVarInsn(ALOAD, 0);
196: mv
197: .visitFieldInsn(GETFIELD,
198: "java/util/concurrent/CyclicBarrier",
199: "generation",
200: "Ljava/util/concurrent/CyclicBarrier$Generation;");
201: Label l46 = new Label();
202: mv.visitJumpInsn(IF_ACMPNE, l46);
203: mv.visitInsn(ICONST_1);
204: Label l47 = new Label();
205: mv.visitJumpInsn(GOTO, l47);
206: mv.visitLabel(l46);
207: mv.visitInsn(ICONST_0);
208: mv.visitLabel(l47);
209: mv.visitMethodInsn(INVOKEVIRTUAL,
210: "java/lang/StringBuffer", "append",
211: "(Z)Ljava/lang/StringBuffer;");
212: mv.visitMethodInsn(INVOKEVIRTUAL,
213: "java/lang/StringBuffer", "toString",
214: "()Ljava/lang/String;");
215: mv.visitMethodInsn(INVOKEVIRTUAL,
216: "java/io/PrintStream", "println",
217: "(Ljava/lang/String;)V");
218: Label l48 = new Label();
219: mv.visitLabel(l48);
220: mv.visitLineNumber(128, l48);
221: mv.visitVarInsn(ALOAD, 0);
222: mv.visitMethodInsn(INVOKESPECIAL,
223: "java/util/concurrent/CyclicBarrier",
224: "dumpState", "()V");
225: Label l49 = new Label();
226: mv.visitLabel(l49);
227: }
228: }
229: }
230: }
|