|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2020 Microsoft Corporation and others. |
| 3 | +* All rights reserved. This program and the accompanying materials |
| 4 | +* are made available under the terms of the Eclipse Public License v1.0 |
| 5 | +* which accompanies this distribution, and is available at |
| 6 | +* http://www.eclipse.org/legal/epl-v10.html |
| 7 | +* |
| 8 | +* Contributors: |
| 9 | +* Microsoft Corporation - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | + |
| 12 | +package com.microsoft.java.debug.core.adapter; |
| 13 | + |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import com.microsoft.java.debug.core.DebugUtility; |
| 19 | +import com.microsoft.java.debug.core.JdiMethodResult; |
| 20 | +import com.microsoft.java.debug.core.protocol.Requests; |
| 21 | +import com.sun.jdi.IncompatibleThreadStateException; |
| 22 | +import com.sun.jdi.Location; |
| 23 | +import com.sun.jdi.ThreadReference; |
| 24 | +import com.sun.jdi.request.EventRequestManager; |
| 25 | +import com.sun.jdi.request.MethodExitRequest; |
| 26 | +import com.sun.jdi.request.StepRequest; |
| 27 | +import io.reactivex.disposables.Disposable; |
| 28 | + |
| 29 | +public class StepRequestManager { |
| 30 | + private final Map<Long, ThreadState> threadStates = Collections.synchronizedMap(new HashMap<>()); |
| 31 | + |
| 32 | + public ThreadState setThreadState(Requests.Command stepType, ThreadReference thread) throws IncompatibleThreadStateException { |
| 33 | + long threadId = thread.uniqueID(); |
| 34 | + int stackDepth = thread.frameCount(); |
| 35 | + Location location = thread.frame(0).location(); |
| 36 | + ThreadState threadState = new ThreadState(threadId, stepType, stackDepth, location); |
| 37 | + threadStates.put(threadId, threadState); |
| 38 | + return threadState; |
| 39 | + } |
| 40 | + |
| 41 | + public ThreadState getThreadState(long threadId) { |
| 42 | + return threadStates.get(threadId); |
| 43 | + } |
| 44 | + |
| 45 | + public void setMethodResult(long threadId, JdiMethodResult methodResult) { |
| 46 | + ThreadState threadState = getThreadState(threadId); |
| 47 | + threadState.methodResult = methodResult; |
| 48 | + } |
| 49 | + |
| 50 | + public JdiMethodResult getMethodResult(long threadId) { |
| 51 | + ThreadState threadState = getThreadState(threadId); |
| 52 | + if (threadState == null) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + return threadState.methodResult; |
| 56 | + } |
| 57 | + |
| 58 | + public void deletePendingStep(long threadId, EventRequestManager manager) { |
| 59 | + ThreadState threadState = getThreadState(threadId); |
| 60 | + if (threadState != null) { |
| 61 | + threadState.deletePendingStep(manager); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void deleteAllPendingSteps(EventRequestManager manager) { |
| 66 | + this.threadStates.forEach((threadId, threadState) -> threadState.deletePendingStep(manager)); |
| 67 | + } |
| 68 | + |
| 69 | + public void removeMethodResult(long threadId) { |
| 70 | + ThreadState threadState = getThreadState(threadId); |
| 71 | + if (threadState == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + threadState.methodResult = null; |
| 75 | + } |
| 76 | + |
| 77 | + public void removeAllMethodResults() { |
| 78 | + this.threadStates.forEach((threadId, threadState) -> threadState.methodResult = null); |
| 79 | + } |
| 80 | + |
| 81 | + public static class ThreadState { |
| 82 | + long threadId; |
| 83 | + Requests.Command stepType; |
| 84 | + StepRequest pendingStepRequest = null; |
| 85 | + MethodExitRequest pendingMethodExitRequest = null; |
| 86 | + int stackDepth; |
| 87 | + Location stepLocation; |
| 88 | + Disposable eventSubscription = null; |
| 89 | + JdiMethodResult methodResult = null; |
| 90 | + |
| 91 | + public ThreadState(long threadId, Requests.Command stepType, int stackDepth, Location stepLocation) { |
| 92 | + this.threadId = threadId; |
| 93 | + this.stepType = stepType; |
| 94 | + this.stackDepth = stackDepth; |
| 95 | + this.stepLocation = stepLocation; |
| 96 | + } |
| 97 | + |
| 98 | + public long getThreadId() { |
| 99 | + return threadId; |
| 100 | + } |
| 101 | + |
| 102 | + public Requests.Command getStepType() { |
| 103 | + return stepType; |
| 104 | + } |
| 105 | + |
| 106 | + public void setPendingMethodExitRequest(MethodExitRequest pendingMethodExitRequest) { |
| 107 | + this.pendingMethodExitRequest = pendingMethodExitRequest; |
| 108 | + } |
| 109 | + |
| 110 | + public MethodExitRequest getPendingMethodExitRequest() { |
| 111 | + return pendingMethodExitRequest; |
| 112 | + } |
| 113 | + |
| 114 | + public void setPendingStepRequest(StepRequest pendingStepRequest) { |
| 115 | + this.pendingStepRequest = pendingStepRequest; |
| 116 | + } |
| 117 | + |
| 118 | + public int getStackDepth() { |
| 119 | + return stackDepth; |
| 120 | + } |
| 121 | + |
| 122 | + public StepRequest getPendingStepRequest() { |
| 123 | + return pendingStepRequest; |
| 124 | + } |
| 125 | + |
| 126 | + public Location getStepLocation() { |
| 127 | + return stepLocation; |
| 128 | + } |
| 129 | + |
| 130 | + public void setEventSubscription(Disposable eventSubscription) { |
| 131 | + this.eventSubscription = eventSubscription; |
| 132 | + } |
| 133 | + |
| 134 | + public void deletePendingStep(EventRequestManager manager) { |
| 135 | + deleteStepRequest(manager); |
| 136 | + deleteMethodExitRequest(manager); |
| 137 | + eventSubscription.dispose(); |
| 138 | + } |
| 139 | + |
| 140 | + public void deleteStepRequest(EventRequestManager manager) { |
| 141 | + if (this.pendingStepRequest == null) { |
| 142 | + return; |
| 143 | + } |
| 144 | + DebugUtility.deleteEventRequestSafely(manager, this.pendingStepRequest); |
| 145 | + this.pendingStepRequest = null; |
| 146 | + } |
| 147 | + |
| 148 | + private void deleteMethodExitRequest(EventRequestManager manager) { |
| 149 | + if (this.pendingMethodExitRequest == null) { |
| 150 | + return; |
| 151 | + } |
| 152 | + DebugUtility.deleteEventRequestSafely(manager, this.pendingMethodExitRequest); |
| 153 | + this.pendingMethodExitRequest = null; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments