14package org.dartlang.vm.service;
16import com.google.gson.JsonObject;
17import org.dartlang.vm.service.consumer.*;
18import org.dartlang.vm.service.element.*;
19import org.dartlang.vm.service.logging.Logger;
20import org.dartlang.vm.service.logging.Logging;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.HashSet;
30 private static File dartVm;
31 private static File sampleDart;
32 private static File sampleDartWithException;
33 private static int vmPort = 7575;
34 private static Process process;
35 private static VmService vmService;
38 private static int actualVmServiceVersionMajor;
47 runSampleWithException();
48 System.out.println(
"Test Complete");
55 private static void echoDartVmVersion() {
57 List<String> processArgs =
new ArrayList<>();
58 processArgs.add(dartVm.getAbsolutePath());
59 processArgs.add(
"--version");
60 ProcessBuilder processBuilder =
new ProcessBuilder(processArgs);
62 process = processBuilder.start();
63 }
catch (IOException
e) {
64 throw new RuntimeException(
"Failed to launch Dart VM",
e);
66 new SampleOutPrinter(
"version output", process.getInputStream());
67 new SampleOutPrinter(
"version output", process.getErrorStream());
72 vmResume(isolates.
get(0),
null);
73 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.Resume);
76 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.PauseExit);
77 vmResume(isolates.
get(0),
null);
78 vmListener.waitFor(VmService.ISOLATE_STREAM_ID, EventKind.IsolateExit);
88 private static boolean isWindows() {
89 return System.getProperty(
"os.name").startsWith(
"Win");
92 private static void parseArgs(String[]
args) {
93 if (
args.length != 1) {
94 showErrorAndExit(
"Expected absolute path to Dart SDK");
96 File sdkDir =
new File(
args[0]);
97 if (!sdkDir.isDirectory()) {
98 showErrorAndExit(
"Specified directory does not exist: " + sdkDir);
100 File binDir =
new File(sdkDir,
"bin");
101 dartVm =
new File(binDir, isWindows() ?
"dart.exe" :
"dart");
102 if (!dartVm.isFile()) {
103 showErrorAndExit(
"Cannot find Dart VM in SDK: " + dartVm);
105 File currentDir =
new File(
".").getAbsoluteFile();
106 File projDir = currentDir;
108 while (!projDir.getName().equals(
projName)) {
109 projDir = projDir.getParentFile();
110 if (projDir ==
null) {
111 showErrorAndExit(
"Cannot find project " +
projName +
" from " + currentDir);
115 sampleDart =
new File(projDir,
"java/example/sample_main.dart".replace(
"/", File.separator));
116 if (!sampleDart.isFile()) {
117 showErrorAndExit(
"Cannot find sample: " + sampleDart);
119 sampleDartWithException =
new File(projDir,
120 "java/example/sample_exception.dart".replace(
"/", File.separator));
121 if (!sampleDartWithException.isFile()) {
122 showErrorAndExit(
"Cannot find sample: " + sampleDartWithException);
124 System.out.println(
"Using Dart SDK: " + sdkDir);
130 private static void runSample() {
131 SampleVmServiceListener vmListener = startSampleAndConnect(sampleDart);
134 Isolate sampleIsolate = vmGetIsolate(isolates.
get(0));
135 Library rootLib = vmGetLibrary(sampleIsolate, sampleIsolate.getRootLib());
136 vmGetScript(sampleIsolate, rootLib.getScripts().get(0));
137 vmCallServiceExtension(sampleIsolate);
140 vmAddBreakpoint(sampleIsolate, rootLib.getScripts().get(0), 25);
141 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.BreakpointAdded);
142 vmResume(isolates.
get(0),
null);
143 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.Resume);
144 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.PauseBreakpoint);
148 vmGetStack(sampleIsolate);
151 vmEvaluateInFrame(sampleIsolate, 0,
"deepList[0]");
154 vmGetSourceReport(sampleIsolate);
157 vmResume(isolates.
get(0), StepOption.Over);
158 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.Resume);
159 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.PauseBreakpoint);
162 finishExecution(vmListener, isolates);
168 private static void runSampleWithException() {
169 SampleVmServiceListener vmListener = startSampleAndConnect(sampleDartWithException);
171 Isolate sampleIsolate = vmGetIsolate(isolates.
get(0));
174 vmPauseOnException(isolates.
get(0), ExceptionPauseMode.All);
175 vmResume(isolates.
get(0),
null);
176 vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.Resume);
177 Event event = vmListener.waitFor(VmService.DEBUG_STREAM_ID, EventKind.PauseException);
178 InstanceRefToString
convert =
new InstanceRefToString(sampleIsolate, vmService,
new OpLatch());
179 System.out.println(
"Received PauseException event");
180 System.out.println(
" Exception: " +
convert.toString(
event.getException()));
181 System.out.println(
" Top Frame:");
185 finishExecution(vmListener, isolates);
188 private static void setupLogging() {
189 Logging.setLogger(
new Logger() {
191 public void logError(String
message) {
192 System.out.println(
"Log error: " +
message);
196 public void logError(String
message, Throwable exception) {
197 System.out.println(
"Log error: " +
message);
198 if (exception !=
null) {
199 System.out.println(
"Log error exception: " + exception);
200 exception.printStackTrace();
205 public void logInformation(String
message) {
206 System.out.println(
"Log info: " +
message);
210 public void logInformation(String
message, Throwable exception) {
211 System.out.println(
"Log info: " +
message);
212 if (exception !=
null) {
213 System.out.println(
"Log info exception: " + exception);
214 exception.printStackTrace();
220 private static void showErrorAndExit(String errMsg) {
221 System.out.println(errMsg);
224 System.out.println(
"Usage: VmServiceTest /path/to/Dart/SDK");
228 private static void showFrame(InstanceRefToString
convert, Frame
frame) {
229 System.out.println(
" #" +
frame.getIndex() +
" " +
frame.getFunction().getName() +
" ("
230 +
frame.getLocation().getScript().getUri() +
")");
231 for (BoundVariable var :
frame.getVars()) {
232 InstanceRef instanceRef = (InstanceRef)var.getValue();
233 System.out.println(
" " + var.getName() +
" = " +
convert.toString(instanceRef));
238 System.out.println(
">>> Received error response");
239 System.out.println(
" Code: " +
error.getCode());
240 System.out.println(
" Message: " +
error.getMessage());
241 System.out.println(
" Details: " +
error.getDetails());
242 System.out.println(
" Request: " +
error.getRequest());
245 private static void showSentinel(Sentinel sentinel) {
246 System.out.println(
">>> Received sentinel response");
247 System.out.println(
" Sentinel kind: " + sentinel.getKind());
248 System.out.println(
" Sentinel value: " + sentinel.getValueAsString());
251 private static void sleep(
int milliseconds) {
253 Thread.sleep(milliseconds);
254 }
catch (InterruptedException
e) {
259 private static void startSample(File dartFile) {
261 ProcessBuilder processBuilder;
268 processArgs =
new ArrayList<>();
269 processArgs.
add(dartVm.getAbsolutePath());
270 processArgs.
add(
"--pause_isolates_on_start");
271 processArgs.
add(
"--observe");
272 processArgs.
add(
"--enable-vm-service=" + vmPort);
273 processArgs.
add(
"--disable-service-auth-codes");
274 processArgs.
add(dartFile.getAbsolutePath());
275 processBuilder =
new ProcessBuilder(processArgs);
276 System.out.println(
"=================================================");
277 System.out.println(
"Launching sample: " + dartFile);
279 process = processBuilder.start();
280 }
catch (IOException
e) {
281 throw new RuntimeException(
"Failed to launch Dart sample",
e);
284 sampleOut =
new SampleOutPrinter(
"stdout",
process.getInputStream());
285 sampleErr =
new SampleOutPrinter(
"stderr",
process.getErrorStream());
286 System.out.println(
"Dart process started - port " + vmPort);
289 private static SampleVmServiceListener startSampleAndConnect(File dartFile) {
290 startSample(dartFile);
293 SampleVmServiceListener vmListener =
new SampleVmServiceListener(
294 new HashSet<>(Collections.singletonList(EventKind.BreakpointResolved)));
295 vmService.addVmServiceListener(vmListener);
296 vmStreamListen(VmService.DEBUG_STREAM_ID);
297 vmStreamListen(VmService.ISOLATE_STREAM_ID);
301 private static void stopSample() {
302 if (process ==
null) {
305 final Process processToStop =
process;
307 long endTime = System.currentTimeMillis() + 5000;
308 while (System.currentTimeMillis() < endTime) {
310 int exit = processToStop.exitValue();
312 System.out.println(
"Sample exit code: " +
exit);
315 }
catch (IllegalThreadStateException
e) {
320 }
catch (InterruptedException
e) {
324 processToStop.destroy();
325 System.out.println(
"Terminated sample process");
328 @SuppressWarnings(
"SameParameterValue")
329 private static
void vmAddBreakpoint(Isolate isolate, ScriptRef
script,
int lineNum) {
330 final OpLatch latch =
new OpLatch();
331 vmService.addBreakpoint(isolate.getId(),
script.getId(), lineNum,
new AddBreakpointConsumer() {
338 public void received(Breakpoint response) {
339 System.out.println(
"Received Breakpoint response");
340 System.out.println(
" BreakpointNumber:" + response.getBreakpointNumber());
345 public void received(Sentinel response) {
346 showSentinel(response);
349 latch.waitAndAssertOpComplete();
352 private static void vmConnect() {
354 vmService = VmService.localConnect(vmPort);
355 }
catch (IOException
e) {
356 throw new RuntimeException(
"Failed to connect to the VM vmService service",
e);
360 private static void vmDisconnect() {
361 if (vmService !=
null) {
362 vmService.disconnect();
366 @SuppressWarnings(
"SameParameterValue")
367 private static
void vmEvaluateInFrame(Isolate isolate,
int frameIndex, String expression) {
368 System.out.println(
"Evaluating: " + expression);
369 final ResultLatch<InstanceRef> latch =
new ResultLatch<>();
370 vmService.evaluateInFrame(isolate.getId(), frameIndex, expression,
new EvaluateInFrameConsumer() {
377 public void received(ErrorRef response) {
378 showErrorAndExit(response.getMessage());
381 public void received(Sentinel response) {
382 System.out.println(response.getValueAsString());
386 public void received(InstanceRef response) {
387 System.out.println(
"Received InstanceRef response");
388 System.out.println(
" Id: " + response.getId());
389 System.out.println(
" Kind: " + response.getKind());
390 System.out.println(
" Json: " + response.getJson());
391 latch.setValue(response);
394 InstanceRef instanceRef = latch.getValue();
395 InstanceRefToString
convert =
new InstanceRefToString(isolate, vmService, latch);
396 System.out.println(
"Result: " +
convert.toString(instanceRef));
399 private static SourceReport vmGetSourceReport(Isolate isolate) {
400 System.out.println(
"Getting coverage information for " + isolate.getId());
401 final long startTime = System.currentTimeMillis();
402 final ResultLatch<SourceReport> latch =
new ResultLatch<>();
403 vmService.getSourceReport(isolate.getId(), Collections.singletonList(SourceReportKind.Coverage),
new GetSourceReportConsumer() {
410 public void received(SourceReport response) {
411 System.out.println(
"Received SourceReport response (" + (System.currentTimeMillis() - startTime) +
"ms)");
412 System.out.println(
" Script count: " + response.getScripts().size());
413 System.out.println(
" Range count: " + response.getRanges().size());
414 latch.setValue(response);
418 public void received(Sentinel response) {
419 showSentinel(response);
422 return latch.getValue();
425 private static Isolate vmGetIsolate(IsolateRef isolate) {
426 final ResultLatch<Isolate> latch =
new ResultLatch<>();
427 vmService.getIsolate(isolate.getId(),
new GetIsolateConsumer() {
434 public void received(Isolate response) {
435 System.out.println(
"Received Isolate response");
436 System.out.println(
" Id: " + response.getId());
437 System.out.println(
" Name: " + response.getName());
438 System.out.println(
" Number: " + response.getNumber());
439 System.out.println(
" Start Time: " + response.getStartTime());
440 System.out.println(
" RootLib Id: " + response.getRootLib().getId());
441 System.out.println(
" RootLib Uri: " + response.getRootLib().getUri());
442 System.out.println(
" RootLib Name: " + response.getRootLib().getName());
443 System.out.println(
" RootLib Json: " + response.getRootLib().getJson());
444 System.out.println(
" Isolate: " + response);
445 latch.setValue(response);
449 public void received(Sentinel response) {
450 showSentinel(response);
453 return latch.getValue();
456 private static Library vmGetLibrary(Isolate isolateId, LibraryRef library) {
457 final ResultLatch<Library> latch =
new ResultLatch<>();
465 public void received(Library response) {
466 System.out.println(
"Received GetLibrary library");
467 System.out.println(
" uri: " + response.getUri());
468 latch.setValue(response);
471 return latch.getValue();
474 private static void vmGetScript(Isolate isolate, ScriptRef scriptRef) {
475 final ResultLatch<Script> latch =
new ResultLatch<>();
476 vmService.getObject(isolate.getId(), scriptRef.getId(),
new GetObjectConsumer() {
483 public void received(Obj response) {
484 if (response instanceof Script) {
485 latch.setValue((Script) response);
492 public void received(Sentinel response) {
496 Script
script = latch.getValue();
497 System.out.println(
"Received Script");
498 System.out.println(
" Id: " +
script.getId());
499 System.out.println(
" Uri: " +
script.getUri());
500 System.out.println(
" Source: " +
script.getSource());
501 System.out.println(
" TokenPosTable: " +
script.getTokenPosTable());
502 if (
script.getTokenPosTable() ==
null) {
503 showErrorAndExit(
"Expected TokenPosTable to be non-null");
507 private static void vmGetStack(Isolate isolate) {
508 final ResultLatch<Stack> latch =
new ResultLatch<>();
509 vmService.getStack(isolate.getId(),
new GetStackConsumer() {
516 public void received(Stack stack) {
517 latch.setValue(stack);
521 public void received(Sentinel response) {
522 showSentinel(response);
525 Stack stack = latch.getValue();
526 System.out.println(
"Received Stack response");
527 System.out.println(
" Messages:");
528 for (Message
message : stack.getMessages()) {
529 System.out.println(
" " +
message.getName());
531 System.out.println(
" Frames:");
532 InstanceRefToString
convert =
new InstanceRefToString(isolate, vmService, latch);
533 for (Frame
frame : stack.getFrames()) {
538 private static void vmGetVersion() {
539 final OpLatch latch =
new OpLatch();
540 vmService.getVersion(
new VersionConsumer() {
547 public void received(
Version response) {
548 System.out.println(
"Received Version response");
549 actualVmServiceVersionMajor = response.getMajor();
550 System.out.println(
" Major: " + actualVmServiceVersionMajor);
551 System.out.println(
" Minor: " + response.getMinor());
552 System.out.println(response.getJson());
556 latch.waitAndAssertOpComplete();
559 private static void vmCallServiceExtension(Isolate isolateId) {
560 final OpLatch latch =
new OpLatch();
568 public void received(JsonObject
result) {
569 System.out.println(
"Received response: " +
result);
573 latch.waitAndAssertOpComplete();
577 final ResultLatch<ElementList<IsolateRef>> latch =
new ResultLatch<>();
578 vmService.getVM(
new VMConsumer() {
585 public void received(VM response) {
586 System.out.println(
"Received VM response");
587 System.out.println(
" ArchitectureBits: " + response.getArchitectureBits());
588 System.out.println(
" HostCPU: " + response.getHostCPU());
589 System.out.println(
" TargetCPU: " + response.getTargetCPU());
590 System.out.println(
" Pid: " + response.getPid());
591 System.out.println(
" StartTime: " + response.getStartTime());
592 for (IsolateRef isolate : response.getIsolates()) {
593 System.out.println(
" Isolate " + isolate.getNumber() +
", " + isolate.getId() +
", "
594 + isolate.getName());
596 latch.setValue(response.getIsolates());
599 return latch.getValue();
602 private static void vmPauseOnException(IsolateRef isolate, ExceptionPauseMode
mode) {
603 System.out.println(
"Request pause on exception: " +
mode);
604 final OpLatch latch =
new OpLatch();
605 vmService.setIsolatePauseMode(isolate.getId(),
mode,
true,
new SetIsolatePauseModeConsumer() {
612 public void received(Success response) {
613 System.out.println(
"Successfully set pause on exception");
618 public void received(Sentinel response) {
619 showSentinel(response);
622 latch.waitAndAssertOpComplete();
625 private static void vmResume(IsolateRef isolateRef,
final StepOption
step) {
626 final String
id = isolateRef.getId();
627 vmService.resume(
id,
step,
null,
new ResumeConsumer() {
634 public void received(Success response) {
636 System.out.println(
"Resumed isolate " +
id);
638 System.out.println(
"Step " +
step +
" isolate " +
id);
643 public void received(Sentinel response) {
644 showSentinel(response);
650 private static void vmStreamListen(String streamId) {
651 final OpLatch latch =
new OpLatch();
652 vmService.streamListen(streamId,
new SuccessConsumer() {
659 public void received(Success response) {
660 System.out.println(
"Subscribed to debug event stream");
664 latch.waitAndAssertOpComplete();
667 private static void waitForProcessExit() {
668 if (actualVmServiceVersionMajor == 2) {
672 long end = System.currentTimeMillis() + 5000;
675 System.out.println(
"Exit code: " +
process.exitValue());
677 }
catch (IllegalThreadStateException
e) {
680 if (System.currentTimeMillis() >= end) {
681 throw new RuntimeException(
"Expected child process to finish");
static int step(int x, SkScalar min, SkScalar max)
GrAATriangulator::Event Event
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)
void assertLastLine(String text)
static void main(String[] args)
static RPCError unexpected(String expectedType, Response response)
static Editor::Movement convert(skui::Key key)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint8_t uint32_t uint32_t GError ** error
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode