4
4
import datadog .trace .api .gateway .Flow ;
5
5
import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
6
6
import 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 ;
7
11
import java .util .ArrayList ;
8
12
import java .util .List ;
9
13
import java .util .function .Function ;
@@ -86,9 +90,28 @@ public class HandleVisitor extends MethodVisitor {
86
90
private boolean success ;
87
91
private final String methodName ;
88
92
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
+
89
106
public HandleVisitor (int api , DelayCertainInsMethodVisitor methodVisitor , String methodName ) {
90
107
super (api , methodVisitor );
91
108
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
+ }
92
115
}
93
116
94
117
DelayCertainInsMethodVisitor delayVisitorDelegate () {
@@ -98,18 +121,25 @@ DelayCertainInsMethodVisitor delayVisitorDelegate() {
98
121
@ Override
99
122
public void visitMethodInsn (
100
123
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 );
101
128
if (contextVar == -1 ) {
102
129
lookForStore =
103
130
!lookForStore
104
131
&& opcode == Opcodes .INVOKEVIRTUAL
105
132
&& name .equals ("startSpan" )
106
- && descriptor .endsWith ("Ldatadog.context.Context;" );
107
133
&& descriptor .endsWith ("Ldatadog/context/Context;" );
134
+ if (lookForStore ) {
135
+ debug ("Found store" );
136
+ }
108
137
} else if (!success
109
138
&& opcode == Opcodes .INVOKEVIRTUAL
110
139
&& owner .equals ("org/eclipse/jetty/server/Server" )
111
140
&& name .equals ("handle" )
112
141
&& descriptor .equals ("(Lorg/eclipse/jetty/server/HttpChannel;)V" )) {
142
+ debug ("handle bytecode found" );
113
143
DelayCertainInsMethodVisitor mv = delayVisitorDelegate ();
114
144
List <Function > savedVisitations = mv .transferVisitations ();
115
145
/*
@@ -119,6 +149,7 @@ public void visitMethodInsn(
119
149
* invokevirtual #78 // Method getServer:()Lorg/eclipse/jetty/server/Server;
120
150
* aload_0
121
151
*/
152
+ debug ("Saved visitation size: " +savedVisitations .size ());
122
153
if (savedVisitations .size () != 3 ) {
123
154
mv .commitVisitations (savedVisitations );
124
155
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
@@ -156,6 +187,7 @@ public void visitMethodInsn(
156
187
super .visitMethodInsn (opcode , owner , name , descriptor , isInterface );
157
188
super .visitLabel (afterHandle );
158
189
super .visitFrame (Opcodes .F_SAME , 0 , null , 0 , null );
190
+ debug ("handle bytecode injected" );
159
191
this .success = true ;
160
192
return ;
161
193
} else if (!success
@@ -312,6 +344,7 @@ private boolean checkDispatchMethodState(final List<Function> savedVisitations)
312
344
@ Override
313
345
public void visitVarInsn (int opcode , int varIndex ) {
314
346
if (lookForStore && opcode == Opcodes .ASTORE ) {
347
+ debug ("Found context" );
315
348
contextVar = varIndex ;
316
349
lookForStore = false ;
317
350
}
@@ -325,6 +358,12 @@ public void visitEnd() {
325
358
log .warn (
326
359
"Transformation of Jetty's connection class was not successful. Blocking will likely not work" );
327
360
}
361
+ if (this .debugWriter != null ) {
362
+ try {
363
+ this .debugWriter .close ();
364
+ } catch (IOException ignored ) {
365
+ }
366
+ }
328
367
super .visitEnd ();
329
368
}
330
369
}
0 commit comments