44import datadog .trace .api .gateway .Flow ;
55import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
66import datadog .trace .instrumentation .jetty .JettyBlockingHelper ;
7+ import java .io .BufferedWriter ;
8+ import java .io .FileOutputStream ;
9+ import java .io .IOException ;
10+ import java .io .OutputStreamWriter ;
711import java .util .ArrayList ;
812import java .util .List ;
913import java .util .function .Function ;
@@ -86,9 +90,28 @@ public class HandleVisitor extends MethodVisitor {
8690 private boolean success ;
8791 private final String methodName ;
8892
93+ private BufferedWriter debugWriter ;
94+
95+ private void debug (String msg ) {
96+ if (debugWriter == null ) {
97+ return ;
98+ }
99+ try {
100+ debugWriter .write (msg );
101+ debugWriter .newLine ();
102+ } catch (IOException ignored ) {
103+ }
104+ }
105+
89106 public HandleVisitor (int api , DelayCertainInsMethodVisitor methodVisitor , String methodName ) {
90107 super (api , methodVisitor );
91108 this .methodName = methodName ;
109+ try {
110+ String path = "/Users/bruce.bujon/go/src/github.com/DataDog/dd-trace-java/bbujon/debug/HandleVisitor-" + System .nanoTime () + ".txt" ;
111+ this .debugWriter = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (path )));
112+ debug ("Initializing" );
113+ } catch (IOException ignored ) {
114+ }
92115 }
93116
94117 DelayCertainInsMethodVisitor delayVisitorDelegate () {
@@ -98,18 +121,25 @@ DelayCertainInsMethodVisitor delayVisitorDelegate() {
98121 @ Override
99122 public void visitMethodInsn (
100123 int opcode , String owner , String name , String descriptor , boolean isInterface ) {
124+ debug ("visitMethodInsn" );
125+ debug (">> contextVar: " + contextVar );
126+ debug (">> success: " + success );
127+ debug (">> opcode: " + opcode + ", owner: " + owner + ", name: " + name + ", descriptor: " + descriptor );
101128 if (contextVar == -1 ) {
102129 lookForStore =
103130 !lookForStore
104131 && opcode == Opcodes .INVOKEVIRTUAL
105132 && name .equals ("startSpan" )
106- && descriptor .endsWith ("Ldatadog.context.Context;" );
107133 && descriptor .endsWith ("Ldatadog/context/Context;" );
134+ if (lookForStore ) {
135+ debug ("Found store" );
136+ }
108137 } else if (!success
109138 && opcode == Opcodes .INVOKEVIRTUAL
110139 && owner .equals ("org/eclipse/jetty/server/Server" )
111140 && name .equals ("handle" )
112141 && descriptor .equals ("(Lorg/eclipse/jetty/server/HttpChannel;)V" )) {
142+ debug ("handle bytecode found" );
113143 DelayCertainInsMethodVisitor mv = delayVisitorDelegate ();
114144 List <Function > savedVisitations = mv .transferVisitations ();
115145 /*
@@ -119,6 +149,7 @@ public void visitMethodInsn(
119149 * invokevirtual #78 // Method getServer:()Lorg/eclipse/jetty/server/Server;
120150 * aload_0
121151 */
152+ debug ("Saved visitation size: " +savedVisitations .size ());
122153 if (savedVisitations .size () != 3 ) {
123154 mv .commitVisitations (savedVisitations );
124155 super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
@@ -156,6 +187,7 @@ public void visitMethodInsn(
156187 super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
157188 super .visitLabel (afterHandle );
158189 super .visitFrame (Opcodes .F_SAME , 0 , null , 0 , null );
190+ debug ("handle bytecode injected" );
159191 this .success = true ;
160192 return ;
161193 } else if (!success
@@ -312,6 +344,7 @@ private boolean checkDispatchMethodState(final List<Function> savedVisitations)
312344 @ Override
313345 public void visitVarInsn (int opcode , int varIndex ) {
314346 if (lookForStore && opcode == Opcodes .ASTORE ) {
347+ debug ("Found context" );
315348 contextVar = varIndex ;
316349 lookForStore = false ;
317350 }
@@ -325,6 +358,12 @@ public void visitEnd() {
325358 log .warn (
326359 "Transformation of Jetty's connection class was not successful. Blocking will likely not work" );
327360 }
361+ if (this .debugWriter != null ) {
362+ try {
363+ this .debugWriter .close ();
364+ } catch (IOException ignored ) {
365+ }
366+ }
328367 super .visitEnd ();
329368 }
330369}
0 commit comments