|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright (c) 2022 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 | +* Gayan Perera - initial API and implementation |
| 10 | +*******************************************************************************/ |
| 11 | +package com.microsoft.java.debug.core; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | + |
| 24 | +import com.sun.jdi.ReferenceType; |
| 25 | +import com.sun.jdi.ThreadReference; |
| 26 | +import com.sun.jdi.VMDisconnectedException; |
| 27 | +import com.sun.jdi.VirtualMachine; |
| 28 | +import com.sun.jdi.event.ClassPrepareEvent; |
| 29 | +import com.sun.jdi.event.ThreadDeathEvent; |
| 30 | +import com.sun.jdi.request.ClassPrepareRequest; |
| 31 | +import com.sun.jdi.request.EventRequest; |
| 32 | +import com.sun.jdi.request.MethodEntryRequest; |
| 33 | + |
| 34 | +import io.reactivex.Observable; |
| 35 | +import io.reactivex.disposables.Disposable; |
| 36 | + |
| 37 | +public class MethodBreakpoint implements IMethodBreakpoint, IEvaluatableBreakpoint { |
| 38 | + |
| 39 | + private VirtualMachine vm; |
| 40 | + private IEventHub eventHub; |
| 41 | + private String className; |
| 42 | + private String functionName; |
| 43 | + private String condition; |
| 44 | + private int hitCount; |
| 45 | + |
| 46 | + private HashMap<Object, Object> propertyMap = new HashMap<>(); |
| 47 | + private Object compiledConditionalExpression = null; |
| 48 | + private Map<Long, Object> compiledExpressions = new ConcurrentHashMap<>(); |
| 49 | + |
| 50 | + private List<EventRequest> requests = new ArrayList<>(); |
| 51 | + private List<Disposable> subscriptions = new ArrayList<>(); |
| 52 | + |
| 53 | + public MethodBreakpoint(VirtualMachine vm, IEventHub eventHub, String className, String functionName, |
| 54 | + String condition, int hitCount) { |
| 55 | + Objects.requireNonNull(vm); |
| 56 | + Objects.requireNonNull(eventHub); |
| 57 | + Objects.requireNonNull(className); |
| 58 | + Objects.requireNonNull(functionName); |
| 59 | + this.vm = vm; |
| 60 | + this.eventHub = eventHub; |
| 61 | + this.className = className; |
| 62 | + this.functionName = functionName; |
| 63 | + this.condition = condition; |
| 64 | + this.hitCount = hitCount; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public List<EventRequest> requests() { |
| 69 | + return requests; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public List<Disposable> subscriptions() { |
| 74 | + return subscriptions; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void close() throws Exception { |
| 79 | + try { |
| 80 | + vm.eventRequestManager().deleteEventRequests(requests()); |
| 81 | + } catch (VMDisconnectedException ex) { |
| 82 | + // ignore since removing breakpoints is meaningless when JVM is terminated. |
| 83 | + } |
| 84 | + subscriptions().forEach(Disposable::dispose); |
| 85 | + requests.clear(); |
| 86 | + subscriptions.clear(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public boolean containsEvaluatableExpression() { |
| 91 | + return containsConditionalExpression() || containsLogpointExpression(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean containsConditionalExpression() { |
| 96 | + return StringUtils.isNotBlank(getCondition()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean containsLogpointExpression() { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String getCondition() { |
| 106 | + return condition; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void setCondition(String condition) { |
| 111 | + this.condition = condition; |
| 112 | + setCompiledConditionalExpression(null); |
| 113 | + compiledExpressions.clear(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getLogMessage() { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void setLogMessage(String logMessage) { |
| 123 | + // for future implementation |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void setCompiledConditionalExpression(Object compiledExpression) { |
| 128 | + this.compiledConditionalExpression = compiledExpression; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Object getCompiledConditionalExpression() { |
| 133 | + return compiledConditionalExpression; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void setCompiledLogpointExpression(Object compiledExpression) { |
| 138 | + // for future implementation |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Object getCompiledLogpointExpression() { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void setCompiledExpression(long threadId, Object compiledExpression) { |
| 148 | + compiledExpressions.put(threadId, compiledExpression); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public Object getCompiledExpression(long threadId) { |
| 153 | + return compiledExpressions.get(threadId); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public int getHitCount() { |
| 158 | + return hitCount; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void setHitCount(int hitCount) { |
| 163 | + this.hitCount = hitCount; |
| 164 | + Observable.fromIterable(this.requests()) |
| 165 | + .filter(request -> request instanceof MethodEntryRequest) |
| 166 | + .subscribe(request -> { |
| 167 | + request.addCountFilter(hitCount); |
| 168 | + request.enable(); |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public CompletableFuture<IMethodBreakpoint> install() { |
| 174 | + Disposable subscription = eventHub.events() |
| 175 | + .filter(debugEvent -> debugEvent.event instanceof ThreadDeathEvent) |
| 176 | + .subscribe(debugEvent -> { |
| 177 | + ThreadReference deathThread = ((ThreadDeathEvent) debugEvent.event).thread(); |
| 178 | + compiledExpressions.remove(deathThread.uniqueID()); |
| 179 | + }); |
| 180 | + |
| 181 | + subscriptions.add(subscription); |
| 182 | + |
| 183 | + // It's possible that different class loaders create new class with the same |
| 184 | + // name. |
| 185 | + // Here to listen to future class prepare events to handle such case. |
| 186 | + ClassPrepareRequest classPrepareRequest = vm.eventRequestManager().createClassPrepareRequest(); |
| 187 | + classPrepareRequest.addClassFilter(className); |
| 188 | + classPrepareRequest.enable(); |
| 189 | + requests.add(classPrepareRequest); |
| 190 | + |
| 191 | + CompletableFuture<IMethodBreakpoint> future = new CompletableFuture<>(); |
| 192 | + subscription = eventHub.events() |
| 193 | + .filter(debugEvent -> debugEvent.event instanceof ClassPrepareEvent |
| 194 | + && (classPrepareRequest.equals(debugEvent.event.request()))) |
| 195 | + .subscribe(debugEvent -> { |
| 196 | + ClassPrepareEvent event = (ClassPrepareEvent) debugEvent.event; |
| 197 | + Optional<MethodEntryRequest> createdRequest = createMethodEntryRequest(event.referenceType()); |
| 198 | + if (createdRequest.isPresent()) { |
| 199 | + MethodEntryRequest methodEntryRequest = createdRequest.get(); |
| 200 | + requests.add(methodEntryRequest); |
| 201 | + if (!future.isDone()) { |
| 202 | + this.putProperty("verified", true); |
| 203 | + future.complete(this); |
| 204 | + } |
| 205 | + } |
| 206 | + }); |
| 207 | + subscriptions.add(subscription); |
| 208 | + |
| 209 | + List<ReferenceType> types = vm.classesByName(className); |
| 210 | + for (ReferenceType type : types) { |
| 211 | + Optional<MethodEntryRequest> createdRequest = createMethodEntryRequest(type); |
| 212 | + if (createdRequest.isPresent()) { |
| 213 | + MethodEntryRequest methodEntryRequest = createdRequest.get(); |
| 214 | + requests.add(methodEntryRequest); |
| 215 | + if (!future.isDone()) { |
| 216 | + this.putProperty("verified", true); |
| 217 | + future.complete(this); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + return future; |
| 222 | + } |
| 223 | + |
| 224 | + private Optional<MethodEntryRequest> createMethodEntryRequest(ReferenceType type) { |
| 225 | + return type.methodsByName(functionName).stream().findFirst().map(method -> { |
| 226 | + MethodEntryRequest request = vm.eventRequestManager().createMethodEntryRequest(); |
| 227 | + |
| 228 | + request.addClassFilter(type); |
| 229 | + request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD); |
| 230 | + if (hitCount > 0) { |
| 231 | + request.addCountFilter(hitCount); |
| 232 | + } |
| 233 | + request.enable(); |
| 234 | + return request; |
| 235 | + }); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public Object getProperty(Object key) { |
| 240 | + return propertyMap.get(key); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public void putProperty(Object key, Object value) { |
| 245 | + propertyMap.put(key, value); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public String methodName() { |
| 250 | + return functionName; |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public String className() { |
| 255 | + return className; |
| 256 | + } |
| 257 | + |
| 258 | +} |
0 commit comments