From c947f2cf0ddcd38b6775fa48dade8158ded6210f Mon Sep 17 00:00:00 2001 From: Fabien Sanglard Date: Wed, 16 Nov 2022 17:20:22 -0800 Subject: Remove 1.3s "Waiting for Debugger" delay Remove arbitrary loop when a debugged app starts. Instead for waiting for 1.3 seconds, we instruct oj-jdwplib to suspendfor all the threads and sent VM_START. We continue as soon as the debugger sends ResumeAll. Design doc: go/waitingfordebugger Test: Manually tested against Android Studio and jdb Bug: 261096302 Change-Id: I76c02d6acd0d7fb5babed0018432db74ae29487e --- core/java/android/os/Debug.java | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/core/java/android/os/Debug.java b/core/java/android/os/Debug.java index 399f11b0f2a7..c731a808263a 100644 --- a/core/java/android/os/Debug.java +++ b/core/java/android/os/Debug.java @@ -981,6 +981,43 @@ public final class Debug } + /** + * Wait until a debugger attaches. As soon as a debugger attaches, + * suspend all Java threads and send VM_START (a.k.a VM_INIT) + * packet. + * + * @hide + */ + public static void suspendAllAndSendVmStart() { + if (!VMDebug.isDebuggingEnabled()) { + return; + } + + // if DDMS is listening, inform them of our plight + System.out.println("Sending WAIT chunk"); + byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger" + Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1); + DdmServer.sendChunk(waitChunk); + + // We must wait until a debugger is connected (debug socket is + // open and at least one non-DDM JDWP packedt has been received. + // This guarantees that oj-libjdwp has been attached and that + // ART's default implementation of suspendAllAndSendVmStart has + // been replaced with an implementation that will suspendAll and + // send VM_START. + System.out.println("Waiting for debugger first packet"); + while (!isDebuggerConnected()) { + try { + Thread.sleep(100); + } catch (InterruptedException ie) { + } + } + + System.out.println("Debug.suspendAllAndSentVmStart"); + VMDebug.suspendAllAndSendVmStart(); + System.out.println("Debug.suspendAllAndSendVmStart, resumed"); + } + /** * Wait until a debugger attaches. As soon as the debugger attaches, * this returns, so you will need to place a breakpoint after the -- cgit v1.2.3-59-g8ed1b