-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathJavaDebugServerTest.java
More file actions
288 lines (260 loc) · 10.3 KB
/
JavaDebugServerTest.java
File metadata and controls
288 lines (260 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package org.javacs;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Logger;
import org.javacs.debug.*;
import org.javacs.debug.proto.*;
import org.junit.Test;
public class JavaDebugServerTest {
Path workingDirectory = Paths.get("src/test/examples/debug");
DebugClient client = new MockClient();
JavaDebugServer server = new JavaDebugServer(client);
Process process;
ArrayBlockingQueue<StoppedEventBody> stoppedEvents = new ArrayBlockingQueue<>(10);
class MockClient implements DebugClient {
@Override
public void initialized() {}
@Override
public void stopped(StoppedEventBody evt) {
stoppedEvents.add(evt);
}
@Override
public void terminated(TerminatedEventBody evt) {}
@Override
public void exited(ExitedEventBody evt) {}
@Override
public void output(OutputEventBody evt) {
LOG.info(evt.output);
}
@Override
public void breakpoint(BreakpointEventBody evt) {
if (evt.breakpoint.verified) {
LOG.info(
String.format(
"Breakpoint at %s:%d is verified", evt.breakpoint.source.path, evt.breakpoint.line));
} else {
LOG.info(
String.format(
"Breakpoint at %s:%d cannot be verified because %s",
evt.breakpoint.source.path, evt.breakpoint.line, evt.breakpoint.message));
}
}
@Override
public RunInTerminalResponseBody runInTerminal(RunInTerminalRequest req) {
throw new UnsupportedOperationException();
}
}
public void launchProcess(String mainClass) throws IOException, InterruptedException {
var command =
List.of("java", "-Xdebug", "-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y", mainClass);
LOG.info("Launch " + String.join(", ", command));
process = new ProcessBuilder().command(command).directory(workingDirectory.toFile()).inheritIO().start();
java.lang.Thread.sleep(1000);
}
private void attach(int port) {
var attach = new AttachRequestArguments();
attach.port = port;
attach.sourceRoots = new String[] {};
server.attach(attach);
}
private void setBreakpoint(String className, int line) {
var set = new SetBreakpointsArguments();
var point = new SourceBreakpoint();
point.line = line;
set.source.path = workingDirectory.resolve(className + ".java").toString();
set.breakpoints = new SourceBreakpoint[] {point};
server.setBreakpoints(set);
}
@Test
public void attachToProcess() throws IOException, InterruptedException {
launchProcess("Hello");
attach(5005);
server.configurationDone();
process.waitFor();
}
@Test
public void setBreakpoint() throws IOException, InterruptedException {
launchProcess("Hello");
// Attach to the process
attach(5005);
// Set a breakpoint at Hello.java:4
setBreakpoint("Hello", 4);
server.configurationDone();
// Wait for stop
stoppedEvents.take();
// Find the main thread
var threads = server.threads().threads;
for (var t : threads) {
if (t.name.equals("main")) {
// Get the stack trace
var requestTrace = new StackTraceArguments();
requestTrace.threadId = t.id;
var stack = server.stackTrace(requestTrace);
System.out.println("Thread main:");
for (var frame : stack.stackFrames) {
System.out.println(String.format("\t%s:%d (%s)", frame.name, frame.line, frame.source.path));
}
// Get variables
var requestScopes = new ScopesArguments();
requestScopes.frameId = stack.stackFrames[0].id;
var scopes = server.scopes(requestScopes).scopes;
// Get locals
var requestLocals = new VariablesArguments();
requestLocals.variablesReference = scopes[0].variablesReference;
var locals = server.variables(requestLocals).variables;
System.out.println("Locals:");
for (var v : locals) {
System.out.println(String.format("\t%s %s = %s", v.type, v.name, v.value));
}
// Get arguments
var requestArgs = new VariablesArguments();
requestArgs.variablesReference = scopes[1].variablesReference;
var arguments = server.variables(requestArgs).variables;
System.out.println("Arguments:");
for (var v : arguments) {
System.out.println(String.format("\t%s %s = %s", v.type, v.name, v.value));
}
}
}
// Wait for process to exit
server.continue_(new ContinueArguments());
process.waitFor();
}
@Test
public void step() throws IOException, InterruptedException {
launchProcess("Hello");
// Attach to the process
attach(5005);
// Set a breakpoint at HelloWorld.java:4
setBreakpoint("Hello", 4);
server.configurationDone();
// Wait for stop
stoppedEvents.take();
// Find the main thread
var threads = server.threads().threads;
for (var t : threads) {
if (t.name.equals("main")) {
var next = new NextArguments();
next.threadId = t.id;
server.next(next);
// Wait for stop
stoppedEvents.take();
}
}
// Wait for process to exit
server.continue_(new ContinueArguments());
process.waitFor();
}
@Test
public void addBreakpoint() throws IOException, InterruptedException {
launchProcess("Hello");
// Attach to the process
attach(5005);
// Stop at 4
setBreakpoint("Hello", 4);
server.configurationDone();
stoppedEvents.take();
// Stop at 6
setBreakpoint("Hello", 6);
server.continue_(new ContinueArguments());
stoppedEvents.take();
// Wait for process to exit
server.continue_(new ContinueArguments());
process.waitFor();
}
@Test
public void printCollections() throws IOException, InterruptedException {
launchProcess("Collections");
attach(5005);
setBreakpoint("Collections", 8);
server.configurationDone();
stoppedEvents.take();
// Find the main thread
var threads = server.threads().threads;
for (var t : threads) {
if (t.name.equals("main")) {
// Get the stack trace
var requestTrace = new StackTraceArguments();
requestTrace.threadId = t.id;
var stack = server.stackTrace(requestTrace);
System.out.println("Thread main:");
for (var frame : stack.stackFrames) {
System.out.println(String.format("\t%s:%d (%s)", frame.name, frame.line, frame.source.path));
}
// Get variables
var requestScopes = new ScopesArguments();
requestScopes.frameId = stack.stackFrames[0].id;
var scopes = server.scopes(requestScopes).scopes;
// Get locals
var requestLocals = new VariablesArguments();
requestLocals.variablesReference = scopes[1].variablesReference;
var locals = server.variables(requestLocals).variables;
System.out.println("Locals:");
for (var v : locals) {
System.out.println(String.format("\t%s %s = %s", v.type, v.name, v.value));
}
}
}
// Wait for process to exit
server.continue_(new ContinueArguments());
process.waitFor();
}
@Test
public void deepVariables() throws IOException, InterruptedException {
launchProcess("DeepVariables");
attach(5005);
setBreakpoint("DeepVariables", 8);
server.configurationDone();
stoppedEvents.take();
// Find the main thread
org.javacs.debug.proto.Thread mainThread = null;
for (var t : server.threads().threads) {
if (t.name.equals("main")) {
mainThread = t;
}
}
assertThat(mainThread, notNullValue());
// Get the stack trace
var requestTrace = new StackTraceArguments();
requestTrace.threadId = mainThread.id;
var stack = server.stackTrace(requestTrace);
// Get variables
var requestScopes = new ScopesArguments();
requestScopes.frameId = stack.stackFrames[0].id;
var scopes = server.scopes(requestScopes).scopes;
// Get locals
var requestLocals = new VariablesArguments();
requestLocals.variablesReference = scopes[1].variablesReference;
var locals = server.variables(requestLocals).variables;
// Find an object value
Variable objectVariable = null;
for (var v : locals) {
if (v.name.equals("object")) {
objectVariable = v;
}
}
assertThat(objectVariable, notNullValue());
// Get an object field
var requestObject = new VariablesArguments();
requestObject.variablesReference = objectVariable.variablesReference;
var fields = server.variables(requestObject).variables;
// Inspect an object field
Variable fieldVariable = null;
for (var v : fields) {
if (v.name.equals("value")) {
fieldVariable = v;
}
}
assertThat(fieldVariable, notNullValue());
assertThat(fieldVariable.value, equalTo("42"));
// Wait for process to exit
server.continue_(new ContinueArguments());
process.waitFor();
}
private static final Logger LOG = Logger.getLogger("main");
}