|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2006, 2008 Wind River Systems 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 |
* Wind River Systems - initial API and implementation |
| 10 |
* Ericsson AB - Modified for handling of multiple threads |
| 11 |
*******************************************************************************/ |
| 12 |
|
| 13 |
package org.eclipse.dd.mi.service; |
| 14 |
|
| 15 |
import java.util.HashMap; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
import org.eclipse.core.runtime.IStatus; |
| 19 |
import org.eclipse.core.runtime.Status; |
| 20 |
import org.eclipse.dd.dsf.concurrent.DataRequestMonitor; |
| 21 |
import org.eclipse.dd.dsf.concurrent.Immutable; |
| 22 |
import org.eclipse.dd.dsf.concurrent.RequestMonitor; |
| 23 |
import org.eclipse.dd.dsf.datamodel.AbstractDMContext; |
| 24 |
import org.eclipse.dd.dsf.datamodel.AbstractDMEvent; |
| 25 |
import org.eclipse.dd.dsf.datamodel.DMContexts; |
| 26 |
import org.eclipse.dd.dsf.datamodel.IDMContext; |
| 27 |
import org.eclipse.dd.dsf.datamodel.IDMEvent; |
| 28 |
import org.eclipse.dd.dsf.debug.service.IStack.IFrameDMContext; |
| 29 |
import org.eclipse.dd.dsf.debug.service.command.CommandCache; |
| 30 |
import org.eclipse.dd.dsf.service.AbstractDsfService; |
| 31 |
import org.eclipse.dd.dsf.service.DsfServiceEventHandler; |
| 32 |
import org.eclipse.dd.dsf.service.DsfSession; |
| 33 |
import org.eclipse.dd.mi.internal.MIPlugin; |
| 34 |
import org.eclipse.dd.mi.service.command.AbstractMIControl; |
| 35 |
import org.eclipse.dd.mi.service.command.commands.MIExecContinue; |
| 36 |
import org.eclipse.dd.mi.service.command.commands.MIExecFinish; |
| 37 |
import org.eclipse.dd.mi.service.command.commands.MIExecInterrupt; |
| 38 |
import org.eclipse.dd.mi.service.command.commands.MIExecNext; |
| 39 |
import org.eclipse.dd.mi.service.command.commands.MIExecNextInstruction; |
| 40 |
import org.eclipse.dd.mi.service.command.commands.MIExecStep; |
| 41 |
import org.eclipse.dd.mi.service.command.commands.MIExecStepInstruction; |
| 42 |
import org.eclipse.dd.mi.service.command.commands.MIExecUntil; |
| 43 |
import org.eclipse.dd.mi.service.command.commands.MIThreadListIds; |
| 44 |
import org.eclipse.dd.mi.service.command.events.IMIDMEvent; |
| 45 |
import org.eclipse.dd.mi.service.command.events.MIBreakpointHitEvent; |
| 46 |
import org.eclipse.dd.mi.service.command.events.MIErrorEvent; |
| 47 |
import org.eclipse.dd.mi.service.command.events.MIEvent; |
| 48 |
import org.eclipse.dd.mi.service.command.events.MIGDBExitEvent; |
| 49 |
import org.eclipse.dd.mi.service.command.events.MIRunningEvent; |
| 50 |
import org.eclipse.dd.mi.service.command.events.MISharedLibEvent; |
| 51 |
import org.eclipse.dd.mi.service.command.events.MISignalEvent; |
| 52 |
import org.eclipse.dd.mi.service.command.events.MISteppingRangeEvent; |
| 53 |
import org.eclipse.dd.mi.service.command.events.MIStoppedEvent; |
| 54 |
import org.eclipse.dd.mi.service.command.events.MIThreadCreatedEvent; |
| 55 |
import org.eclipse.dd.mi.service.command.events.MIThreadExitEvent; |
| 56 |
import org.eclipse.dd.mi.service.command.events.MIWatchpointTriggerEvent; |
| 57 |
import org.eclipse.dd.mi.service.command.output.MIInfo; |
| 58 |
import org.eclipse.dd.mi.service.command.output.MIThreadListIdsInfo; |
| 59 |
import org.osgi.framework.BundleContext; |
| 60 |
|
| 61 |
/** |
| 62 |
* Implementation note: This class implements event handlers for the events that |
| 63 |
* are generated by this service itself. When the event is dispatched, these |
| 64 |
* handlers will be called first, before any of the clients. These handlers |
| 65 |
* update the service's internal state information to make them consistent with |
| 66 |
* the events being issued. Doing this in the handlers as opposed to when the |
| 67 |
* events are generated, guarantees that the state of the service will always be |
| 68 |
* consistent with the events. The purpose of this pattern is to allow clients |
| 69 |
* that listen to service events and track service state, to be perfectly in |
| 70 |
* sync with the service state. |
| 71 |
*/ |
| 72 |
public class MIRunControlNS extends AbstractDsfService implements IMIRunControl |
| 73 |
{ |
| 74 |
// This is an exact copy of the structures in MIRunControl. In an ideal world, |
| 75 |
// it would be declared only once in IMIRunControl but this is real life and |
| 76 |
// it has to be duplicated for the sake of backward compatibility. |
| 77 |
// It sucks and leads to bloated, error-prone code but that's the way it is. |
| 78 |
class MIExecutionDMCNS extends AbstractDMContext implements IMIExecutionDMContext |
| 79 |
{ |
| 80 |
/** |
| 81 |
* Integer ID that is used to identify the thread in the GDB/MI protocol. |
| 82 |
*/ |
| 83 |
private final int fThreadId; |
| 84 |
|
| 85 |
/** |
| 86 |
* Constructor for the context. It should not be called directly by clients. |
| 87 |
* Instead clients should call {@link MIRunControl#createMIExecutionContext(IContainerDMContext, int)} |
| 88 |
* to create instances of this context based on the thread ID. |
| 89 |
* <p/> |
| 90 |
* Classes extending {@link MIRunControl} may also extend this class to include |
| 91 |
* additional information in the context. |
| 92 |
* |
| 93 |
* @param sessionId Session that this context belongs to. |
| 94 |
* @param containerDmc The container that this context belongs to. |
| 95 |
* @param threadId GDB/MI thread identifier. |
| 96 |
*/ |
| 97 |
protected MIExecutionDMCNS(String sessionId, IContainerDMContext containerDmc, int threadId) { |
| 98 |
super(sessionId, containerDmc != null ? new IDMContext[] { containerDmc } : new IDMContext[0]); |
| 99 |
fThreadId = threadId; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns the GDB/MI thread identifier of this context. |
| 104 |
* @return |
| 105 |
*/ |
| 106 |
public int getThreadId(){ |
| 107 |
return fThreadId; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public String toString() { return baseToString() + ".thread[" + fThreadId + "]"; } //$NON-NLS-1$ //$NON-NLS-2$ |
| 112 |
|
| 113 |
@Override |
| 114 |
public boolean equals(Object obj) { |
| 115 |
return super.baseEquals(obj) && ((MIExecutionDMCNS)obj).fThreadId == fThreadId; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public int hashCode() { return super.baseHashCode() ^ fThreadId; } |
| 120 |
} |
| 121 |
|
| 122 |
@Immutable |
| 123 |
static class ExecutionData implements IExecutionDMData { |
| 124 |
private final StateChangeReason fReason; |
| 125 |
ExecutionData(StateChangeReason reason) { |
| 126 |
fReason = reason; |
| 127 |
} |
| 128 |
public StateChangeReason getStateChangeReason() { return fReason; } |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Base class for events generated by the MI Run Control service. Most events |
| 133 |
* generated by the MI Run Control service are directly caused by some MI event. |
| 134 |
* Other services may need access to the extended MI data carried in the event. |
| 135 |
* |
| 136 |
* @param <V> DMC that this event refers to |
| 137 |
* @param <T> MIInfo object that is the direct cause of this event |
| 138 |
* @see MIRunControl |
| 139 |
*/ |
| 140 |
@Immutable |
| 141 |
static class RunControlEvent<V extends IDMContext, T extends MIEvent<? extends IDMContext>> extends AbstractDMEvent<V> |
| 142 |
implements IDMEvent<V>, IMIDMEvent |
| 143 |
{ |
| 144 |
final private T fMIInfo; |
| 145 |
public RunControlEvent(V dmc, T miInfo) { |
| 146 |
super(dmc); |
| 147 |
fMIInfo = miInfo; |
| 148 |
} |
| 149 |
|
| 150 |
public T getMIEvent() { return fMIInfo; } |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Indicates that the given thread has been suspended. |
| 155 |
*/ |
| 156 |
@Immutable |
| 157 |
static class SuspendedEvent extends RunControlEvent<IExecutionDMContext, MIStoppedEvent> |
| 158 |
implements ISuspendedDMEvent |
| 159 |
{ |
| 160 |
SuspendedEvent(IExecutionDMContext ctx, MIStoppedEvent miInfo) { |
| 161 |
super(ctx, miInfo); |
| 162 |
} |
| 163 |
|
| 164 |
public StateChangeReason getReason() { |
| 165 |
if (getMIEvent() instanceof MIBreakpointHitEvent) { |
| 166 |
return StateChangeReason.BREAKPOINT; |
| 167 |
} else if (getMIEvent() instanceof MISteppingRangeEvent) { |
| 168 |
return StateChangeReason.STEP; |
| 169 |
} else if (getMIEvent() instanceof MISharedLibEvent) { |
| 170 |
return StateChangeReason.SHAREDLIB; |
| 171 |
}else if (getMIEvent() instanceof MISignalEvent) { |
| 172 |
return StateChangeReason.SIGNAL; |
| 173 |
}else if (getMIEvent() instanceof MIWatchpointTriggerEvent) { |
| 174 |
return StateChangeReason.WATCHPOINT; |
| 175 |
}else if (getMIEvent() instanceof MIErrorEvent) { |
| 176 |
return StateChangeReason.ERROR; |
| 177 |
}else { |
| 178 |
return StateChangeReason.USER_REQUEST; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Immutable |
| 184 |
static class ContainerSuspendedEvent extends SuspendedEvent |
| 185 |
implements IContainerSuspendedDMEvent |
| 186 |
{ |
| 187 |
final IExecutionDMContext[] triggeringDmcs; |
| 188 |
ContainerSuspendedEvent(IContainerDMContext containerDmc, MIStoppedEvent miInfo, IExecutionDMContext triggeringDmc) { |
| 189 |
super(containerDmc, miInfo); |
| 190 |
this.triggeringDmcs = triggeringDmc != null |
| 191 |
? new IExecutionDMContext[] { triggeringDmc } : new IExecutionDMContext[0]; |
| 192 |
} |
| 193 |
|
| 194 |
public IExecutionDMContext[] getTriggeringContexts() { |
| 195 |
return triggeringDmcs; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
@Immutable |
| 200 |
static class ThreadSuspendedEvent extends SuspendedEvent |
| 201 |
{ |
| 202 |
ThreadSuspendedEvent(IExecutionDMContext executionDmc, MIStoppedEvent miInfo) { |
| 203 |
super(executionDmc, miInfo); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
@Immutable |
| 208 |
static class ResumedEvent extends RunControlEvent<IExecutionDMContext, MIRunningEvent> |
| 209 |
implements IResumedDMEvent |
| 210 |
{ |
| 211 |
ResumedEvent(IExecutionDMContext ctx, MIRunningEvent miInfo) { |
| 212 |
super(ctx, miInfo); |
| 213 |
} |
| 214 |
|
| 215 |
public StateChangeReason getReason() { |
| 216 |
switch(getMIEvent().getType()) { |
| 217 |
case MIRunningEvent.CONTINUE: |
| 218 |
return StateChangeReason.USER_REQUEST; |
| 219 |
case MIRunningEvent.NEXT: |
| 220 |
case MIRunningEvent.NEXTI: |
| 221 |
return StateChangeReason.STEP; |
| 222 |
case MIRunningEvent.STEP: |
| 223 |
case MIRunningEvent.STEPI: |
| 224 |
return StateChangeReason.STEP; |
| 225 |
case MIRunningEvent.FINISH: |
| 226 |
return StateChangeReason.STEP; |
| 227 |
case MIRunningEvent.UNTIL: |
| 228 |
case MIRunningEvent.RETURN: |
| 229 |
break; |
| 230 |
} |
| 231 |
return StateChangeReason.UNKNOWN; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
@Immutable |
| 236 |
static class ContainerResumedEvent extends ResumedEvent |
| 237 |
implements IContainerResumedDMEvent |
| 238 |
{ |
| 239 |
final IExecutionDMContext[] triggeringDmcs; |
| 240 |
|
| 241 |
ContainerResumedEvent(IContainerDMContext containerDmc, MIRunningEvent miInfo, IExecutionDMContext triggeringDmc) { |
| 242 |
super(containerDmc, miInfo); |
| 243 |
this.triggeringDmcs = triggeringDmc != null |
| 244 |
? new IExecutionDMContext[] { triggeringDmc } : new IExecutionDMContext[0]; |
| 245 |
} |
| 246 |
|
| 247 |
public IExecutionDMContext[] getTriggeringContexts() { |
| 248 |
return triggeringDmcs; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
@Immutable |
| 253 |
static class ThreadResumedEvent extends ResumedEvent |
| 254 |
{ |
| 255 |
ThreadResumedEvent(IExecutionDMContext executionDmc, MIRunningEvent miInfo) { |
| 256 |
super(executionDmc, miInfo); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
@Immutable |
| 261 |
static class StartedDMEvent extends RunControlEvent<IExecutionDMContext,MIThreadCreatedEvent> |
| 262 |
implements IStartedDMEvent |
| 263 |
{ |
| 264 |
StartedDMEvent(IMIExecutionDMContext executionDmc, MIThreadCreatedEvent miInfo) { |
| 265 |
super(executionDmc, miInfo); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
@Immutable |
| 270 |
static class ExitedDMEvent extends RunControlEvent<IExecutionDMContext,MIThreadExitEvent> |
| 271 |
implements IExitedDMEvent |
| 272 |
{ |
| 273 |
ExitedDMEvent(IMIExecutionDMContext executionDmc, MIThreadExitEvent miInfo) { |
| 274 |
super(executionDmc, miInfo); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
protected class MIThreadRunState { |
| 279 |
// State flags |
| 280 |
boolean fSuspended = false; |
| 281 |
boolean fResumePending = false; |
| 282 |
boolean fStepping = false; |
| 283 |
StateChangeReason fStateChangeReason; |
| 284 |
} |
| 285 |
|
| 286 |
/////////////////////////////////////////////////////////////////////////// |
| 287 |
// MIRunControlNS |
| 288 |
/////////////////////////////////////////////////////////////////////////// |
| 289 |
|
| 290 |
private AbstractMIControl fConnection; |
| 291 |
|
| 292 |
// The command cache applies only for the thread-info command at the |
| 293 |
// container (process) level and is *always* available in non-stop mode. |
| 294 |
// The only thing to do is to reset it every time a thread is created/ |
| 295 |
// terminated. |
| 296 |
private CommandCache fMICommandCache; |
| 297 |
|
| 298 |
private boolean fTerminated = false; |
| 299 |
|
| 300 |
// ThreadStates indexed by the execution context |
| 301 |
protected Map<IMIExecutionDMContext, MIThreadRunState> fThreadRunStates = new HashMap<IMIExecutionDMContext, MIThreadRunState>(); |
| 302 |
|
| 303 |
/////////////////////////////////////////////////////////////////////////// |
| 304 |
// Initialization and shutdown |
| 305 |
/////////////////////////////////////////////////////////////////////////// |
| 306 |
|
| 307 |
public MIRunControlNS(DsfSession session) { |
| 308 |
super(session); |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public void initialize(final RequestMonitor rm) { |
| 313 |
super.initialize(new RequestMonitor(getExecutor(), rm) { |
| 314 |
@Override |
| 315 |
protected void handleSuccess() { |
| 316 |
doInitialize(rm); |
| 317 |
} |
| 318 |
}); |
| 319 |
} |
| 320 |
|
| 321 |
private void doInitialize(final RequestMonitor rm) { |
| 322 |
fConnection = getServicesTracker().getService(AbstractMIControl.class); |
| 323 |
fMICommandCache = new CommandCache(getSession(), fConnection); |
| 324 |
fMICommandCache.setContextAvailable(fConnection.getControlDMContext(), true); |
| 325 |
getSession().addServiceEventListener(this, null); |
| 326 |
rm.done(); |
| 327 |
} |
| 328 |
|
| 329 |
@Override |
| 330 |
public void shutdown(final RequestMonitor rm) { |
| 331 |
getSession().removeServiceEventListener(this); |
| 332 |
fMICommandCache.reset(); |
| 333 |
super.shutdown(rm); |
| 334 |
} |
| 335 |
|
| 336 |
/////////////////////////////////////////////////////////////////////////// |
| 337 |
// AbstractDsfService |
| 338 |
/////////////////////////////////////////////////////////////////////////// |
| 339 |
|
| 340 |
@Override |
| 341 |
protected BundleContext getBundleContext() { |
| 342 |
return MIPlugin.getBundleContext(); |
| 343 |
} |
| 344 |
|
| 345 |
/////////////////////////////////////////////////////////////////////////// |
| 346 |
// IDMService |
| 347 |
/////////////////////////////////////////////////////////////////////////// |
| 348 |
|
| 349 |
@SuppressWarnings("unchecked") |
| 350 |
public void getModelData(IDMContext dmc, DataRequestMonitor<?> rm) { |
| 351 |
if (dmc instanceof IExecutionDMContext) { |
| 352 |
getExecutionData((IExecutionDMContext) dmc, (DataRequestMonitor<IExecutionDMData>) rm); |
| 353 |
} else { |
| 354 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$ |
| 355 |
rm.done(); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
/////////////////////////////////////////////////////////////////////////// |
| 360 |
// IRunControl |
| 361 |
/////////////////////////////////////////////////////////////////////////// |
| 362 |
|
| 363 |
// ------------------------------------------------------------------------ |
| 364 |
// Suspend |
| 365 |
// ------------------------------------------------------------------------ |
| 366 |
|
| 367 |
public boolean isSuspended(IExecutionDMContext context) { |
| 368 |
|
| 369 |
// Thread case |
| 370 |
if (context instanceof MIExecutionDMCNS) { |
| 371 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 372 |
return (threadState == null) ? false : !fTerminated && threadState.fSuspended; |
| 373 |
} |
| 374 |
|
| 375 |
// Container case |
| 376 |
if (context instanceof IContainerDMContext) { |
| 377 |
boolean isSuspended = false; |
| 378 |
for (IMIExecutionDMContext threadContext : fThreadRunStates.keySet()) { |
| 379 |
if (DMContexts.isAncestorOf(threadContext, context)) { |
| 380 |
isSuspended |= isSuspended(threadContext); |
| 381 |
} |
| 382 |
} |
| 383 |
return isSuspended; |
| 384 |
} |
| 385 |
|
| 386 |
// Default case |
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
public void canSuspend(IExecutionDMContext context, DataRequestMonitor<Boolean> rm) { |
| 391 |
|
| 392 |
// Thread case |
| 393 |
if (context instanceof MIExecutionDMCNS) { |
| 394 |
rm.setData(doCanSuspend(context)); |
| 395 |
rm.done(); |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
// Container case |
| 400 |
if (context instanceof IContainerDMContext) { |
| 401 |
boolean canSuspend = false; |
| 402 |
for (IMIExecutionDMContext threadContext : fThreadRunStates.keySet()) { |
| 403 |
if (DMContexts.isAncestorOf(threadContext, context)) { |
| 404 |
canSuspend |= doCanSuspend(threadContext); |
| 405 |
} |
| 406 |
} |
| 407 |
rm.setData(canSuspend); |
| 408 |
rm.done(); |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
// Default case |
| 413 |
rm.setData(false); |
| 414 |
rm.done(); |
| 415 |
} |
| 416 |
|
| 417 |
private boolean doCanSuspend(IExecutionDMContext context) { |
| 418 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 419 |
return (threadState == null) ? false : !fTerminated && !threadState.fSuspended; |
| 420 |
} |
| 421 |
|
| 422 |
public void suspend(IExecutionDMContext context, final RequestMonitor rm) { |
| 423 |
|
| 424 |
assert context != null; |
| 425 |
|
| 426 |
// Thread case |
| 427 |
IMIExecutionDMContext thread = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 428 |
if (thread != null) { |
| 429 |
doSuspendThread(thread, rm); |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
// Container case |
| 434 |
IContainerDMContext container = DMContexts.getAncestorOfType(context, IContainerDMContext.class); |
| 435 |
if (container != null) { |
| 436 |
doSuspendContainer(container, rm); |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
// Default case |
| 441 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, "Invalid context type.", null)); //$NON-NLS-1$ |
| 442 |
rm.done(); |
| 443 |
} |
| 444 |
|
| 445 |
private void doSuspendThread(IMIExecutionDMContext context, final RequestMonitor rm) { |
| 446 |
|
| 447 |
if (!doCanSuspend(context)) { |
| 448 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 449 |
"Given context: " + context + ", is already suspended.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 450 |
rm.done(); |
| 451 |
return; |
| 452 |
} |
| 453 |
|
| 454 |
MIExecInterrupt cmd = new MIExecInterrupt(context, true); |
| 455 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 456 |
} |
| 457 |
|
| 458 |
private void doSuspendContainer(IExecutionDMContext context, final RequestMonitor rm) { |
| 459 |
MIExecInterrupt cmd = new MIExecInterrupt(context, true); |
| 460 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 461 |
} |
| 462 |
|
| 463 |
// ------------------------------------------------------------------------ |
| 464 |
// Resume |
| 465 |
// ------------------------------------------------------------------------ |
| 466 |
|
| 467 |
public void canResume(IExecutionDMContext context, DataRequestMonitor<Boolean> rm) { |
| 468 |
|
| 469 |
// Thread case |
| 470 |
if (context instanceof MIExecutionDMCNS) { |
| 471 |
rm.setData(doCanResume(context)); |
| 472 |
rm.done(); |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
// Container case |
| 477 |
if (context instanceof IContainerDMContext) { |
| 478 |
boolean canSuspend = false; |
| 479 |
for (IMIExecutionDMContext threadContext : fThreadRunStates.keySet()) { |
| 480 |
if (DMContexts.isAncestorOf(threadContext, context)) { |
| 481 |
canSuspend |= doCanResume(threadContext); |
| 482 |
} |
| 483 |
} |
| 484 |
rm.setData(canSuspend); |
| 485 |
rm.done(); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
// Default case |
| 490 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, "Invalid context type.", null)); //$NON-NLS-1$ |
| 491 |
rm.done(); |
| 492 |
} |
| 493 |
|
| 494 |
private boolean doCanResume(IExecutionDMContext context) { |
| 495 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 496 |
return (threadState == null) ? false : !fTerminated && threadState.fSuspended && !threadState.fResumePending; |
| 497 |
} |
| 498 |
|
| 499 |
public void resume(IExecutionDMContext context, final RequestMonitor rm) { |
| 500 |
|
| 501 |
assert context != null; |
| 502 |
|
| 503 |
// Thread case |
| 504 |
IMIExecutionDMContext thread = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 505 |
if (thread != null) { |
| 506 |
doResumeThread(thread, rm); |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
// Container case |
| 511 |
IContainerDMContext container = DMContexts.getAncestorOfType(context, IContainerDMContext.class); |
| 512 |
if (container != null) { |
| 513 |
doResumeContainer(container, rm); |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
// Default case |
| 518 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, "Invalid context type.", null)); //$NON-NLS-1$ |
| 519 |
rm.done(); |
| 520 |
} |
| 521 |
|
| 522 |
private void doResumeThread(IMIExecutionDMContext context, final RequestMonitor rm) { |
| 523 |
|
| 524 |
if (!doCanResume(context)) { |
| 525 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 526 |
"Given context: " + context + ", is already running.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 527 |
rm.done(); |
| 528 |
return; |
| 529 |
} |
| 530 |
|
| 531 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 532 |
if (threadState == null) { |
| 533 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 534 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 535 |
rm.done(); |
| 536 |
return; |
| 537 |
} |
| 538 |
threadState.fResumePending = true; |
| 539 |
|
| 540 |
MIExecContinue cmd = new MIExecContinue(context, true); |
| 541 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 542 |
} |
| 543 |
|
| 544 |
private void doResumeContainer(IExecutionDMContext context, final RequestMonitor rm) { |
| 545 |
MIExecContinue cmd = new MIExecContinue(context, true); |
| 546 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 547 |
} |
| 548 |
|
| 549 |
// ------------------------------------------------------------------------ |
| 550 |
// Step |
| 551 |
// ------------------------------------------------------------------------ |
| 552 |
|
| 553 |
public boolean isStepping(IExecutionDMContext context) { |
| 554 |
|
| 555 |
// If it's a thread, just look it up |
| 556 |
if (context instanceof MIExecutionDMCNS) { |
| 557 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 558 |
return (threadState == null) ? false : !fTerminated && threadState.fStepping; |
| 559 |
} |
| 560 |
|
| 561 |
// Default case |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
public void canStep(IExecutionDMContext context, StepType stepType, DataRequestMonitor<Boolean> rm) { |
| 566 |
|
| 567 |
// If it's a thread, just look it up |
| 568 |
if (context instanceof MIExecutionDMCNS) { |
| 569 |
canResume(context, rm); |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
// If it's a container, then we don't want to step it |
| 574 |
rm.setData(false); |
| 575 |
rm.done(); |
| 576 |
} |
| 577 |
|
| 578 |
public void step(IExecutionDMContext context, StepType stepType, final RequestMonitor rm) { |
| 579 |
|
| 580 |
assert context != null; |
| 581 |
|
| 582 |
IMIExecutionDMContext dmc = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 583 |
if (dmc == null) { |
| 584 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 585 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 586 |
rm.done(); |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
if (!doCanResume(context)) { |
| 591 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 592 |
"Cannot resume context", null)); //$NON-NLS-1$ |
| 593 |
rm.done(); |
| 594 |
return; |
| 595 |
} |
| 596 |
|
| 597 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 598 |
if (threadState == null) { |
| 599 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 600 |
"Given context: " + context + " can't be found.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 601 |
rm.done(); |
| 602 |
return; |
| 603 |
} |
| 604 |
|
| 605 |
threadState.fResumePending = true; |
| 606 |
threadState.fStepping = true; |
| 607 |
|
| 608 |
switch (stepType) { |
| 609 |
case STEP_INTO: |
| 610 |
fConnection.queueCommand(new MIExecStep(dmc, true), |
| 611 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 612 |
break; |
| 613 |
case STEP_OVER: |
| 614 |
fConnection.queueCommand(new MIExecNext(dmc, true), |
| 615 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 616 |
break; |
| 617 |
case STEP_RETURN: |
| 618 |
// The -exec-finish command operates on the selected stack frame, but here we always |
| 619 |
// want it to operate on the stop stack frame. So we manually create a top-frame |
| 620 |
// context to use with the MI command. |
| 621 |
// We get a local instance of the stack service because the stack service can be shut |
| 622 |
// down before the run control service is shut down. So it is possible for the |
| 623 |
// getService() request below to return null. |
| 624 |
MIStack stackService = getServicesTracker().getService(MIStack.class); |
| 625 |
if (stackService != null) { |
| 626 |
IFrameDMContext topFrameDmc = stackService.createFrameDMContext(dmc, 0); |
| 627 |
fConnection.queueCommand(new MIExecFinish(topFrameDmc), |
| 628 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 629 |
} else { |
| 630 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 631 |
"Cannot create context for command, stack service not available.", null)); //$NON-NLS-1$ |
| 632 |
rm.done(); |
| 633 |
} |
| 634 |
break; |
| 635 |
case INSTRUCTION_STEP_INTO: |
| 636 |
fConnection.queueCommand(new MIExecStepInstruction(dmc, true), |
| 637 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 638 |
break; |
| 639 |
case INSTRUCTION_STEP_OVER: |
| 640 |
fConnection.queueCommand(new MIExecNextInstruction(dmc, true), |
| 641 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 642 |
break; |
| 643 |
default: |
| 644 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, |
| 645 |
INTERNAL_ERROR, "Given step type not supported", null)); //$NON-NLS-1$ |
| 646 |
rm.done(); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
// ------------------------------------------------------------------------ |
| 651 |
// Run to line |
| 652 |
// ------------------------------------------------------------------------ |
| 653 |
|
| 654 |
// Later add support for Address and function. |
| 655 |
// skipBreakpoints is not used at the moment. Implement later |
| 656 |
public void runToLine(IExecutionDMContext context, String fileName, String lineNo, boolean skipBreakpoints, final DataRequestMonitor<MIInfo> rm) { |
| 657 |
|
| 658 |
assert context != null; |
| 659 |
|
| 660 |
IMIExecutionDMContext dmc = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 661 |
if (dmc == null) { |
| 662 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 663 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 664 |
rm.done(); |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
if (!doCanResume(context)) { |
| 669 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 670 |
"Cannot resume context", null)); //$NON-NLS-1$ |
| 671 |
rm.done(); |
| 672 |
return; |
| 673 |
} |
| 674 |
|
| 675 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 676 |
if (threadState == null) { |
| 677 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 678 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 679 |
rm.done(); |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
threadState.fResumePending = true; |
| 684 |
fConnection.queueCommand(new MIExecUntil(dmc, fileName + ":" + lineNo), //$NON-NLS-1$ |
| 685 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 686 |
} |
| 687 |
|
| 688 |
// ------------------------------------------------------------------------ |
| 689 |
// Support functions |
| 690 |
// ------------------------------------------------------------------------ |
| 691 |
|
| 692 |
public void getExecutionContexts(final IContainerDMContext containerDmc, final DataRequestMonitor<IExecutionDMContext[]> rm) { |
| 693 |
fConnection.queueCommand(new MIThreadListIds(containerDmc), |
| 694 |
new DataRequestMonitor<MIThreadListIdsInfo>(getExecutor(), rm) { |
| 695 |
@Override |
| 696 |
protected void handleSuccess() { |
| 697 |
rm.setData(makeExecutionDMCs(containerDmc, getData())); |
| 698 |
rm.done(); |
| 699 |
} |
| 700 |
}); |
| 701 |
} |
| 702 |
|
| 703 |
private IExecutionDMContext[] makeExecutionDMCs(IContainerDMContext containerCtx, MIThreadListIdsInfo info) { |
| 704 |
IExecutionDMContext[] executionDmcs = new IMIExecutionDMContext[info.getThreadIds().length]; |
| 705 |
for (int i = 0; i < info.getThreadIds().length; i++) { |
| 706 |
executionDmcs[i] = createMIExecutionContext(containerCtx, info.getThreadIds()[i]); |
| 707 |
} |
| 708 |
return executionDmcs; |
| 709 |
} |
| 710 |
|
| 711 |
public void getExecutionData(IExecutionDMContext dmc, DataRequestMonitor<IExecutionDMData> rm) { |
| 712 |
MIThreadRunState threadState = fThreadRunStates.get(dmc); |
| 713 |
if (threadState == null) { |
| 714 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID,INVALID_HANDLE, |
| 715 |
"Given context: " + dmc + " is not a recognized execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 716 |
rm.done(); |
| 717 |
return; |
| 718 |
} |
| 719 |
|
| 720 |
if (dmc instanceof IMIExecutionDMContext) { |
| 721 |
rm.setData(new ExecutionData(threadState.fSuspended ? threadState.fStateChangeReason : null)); |
| 722 |
} else { |
| 723 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_HANDLE, |
| 724 |
"Given context: " + dmc + " is not a recognized execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 725 |
} |
| 726 |
rm.done(); |
| 727 |
} |
| 728 |
|
| 729 |
/////////////////////////////////////////////////////////////////////////// |
| 730 |
// IMIRunControl |
| 731 |
/////////////////////////////////////////////////////////////////////////// |
| 732 |
|
| 733 |
public IMIExecutionDMContext createMIExecutionContext(IContainerDMContext container, int threadId) { |
| 734 |
return new MIExecutionDMCNS(getSession().getId(), container, threadId); |
| 735 |
} |
| 736 |
|
| 737 |
/////////////////////////////////////////////////////////////////////////// |
| 738 |
// IMIRunControl |
| 739 |
/////////////////////////////////////////////////////////////////////////// |
| 740 |
|
| 741 |
public CommandCache getCache() { |
| 742 |
return fMICommandCache; |
| 743 |
} |
| 744 |
|
| 745 |
public AbstractMIControl getConnection() { |
| 746 |
return fConnection; |
| 747 |
} |
| 748 |
|
| 749 |
/////////////////////////////////////////////////////////////////////////// |
| 750 |
// Event handlers |
| 751 |
/////////////////////////////////////////////////////////////////////////// |
| 752 |
|
| 753 |
@DsfServiceEventHandler |
| 754 |
public void eventDispatched(final MIRunningEvent e) { |
| 755 |
|
| 756 |
IDMEvent<?> event = null; |
| 757 |
|
| 758 |
// If it's not an execution context (what else could it be?!?), just propagate it |
| 759 |
IMIExecutionDMContext executionDmc = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 760 |
if (executionDmc == null) { |
| 761 |
event = new ResumedEvent(e.getDMContext(), e); |
| 762 |
getSession().dispatchEvent(event, getProperties()); |
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
// It's a thread execution context (since we are in non-stop mode) |
| 767 |
event = new ThreadResumedEvent(e.getDMContext(), e); |
| 768 |
updateThreadState(executionDmc, (ThreadResumedEvent) event); |
| 769 |
getSession().dispatchEvent(event, getProperties()); |
| 770 |
|
| 771 |
// Find the container context, which is used in multi-threaded debugging. |
| 772 |
IContainerDMContext containerDmc = DMContexts.getAncestorOfType(e.getDMContext(), IContainerDMContext.class); |
| 773 |
if (containerDmc != null) { |
| 774 |
// If there is a single stopped thread in that container, bail out. |
| 775 |
for (IExecutionDMContext ctx : fThreadRunStates.keySet()) { |
| 776 |
if (containerDmc.equals(ctx.getParents()[0])) { |
| 777 |
if (fThreadRunStates.get(ctx).fSuspended) |
| 778 |
return; |
| 779 |
} |
| 780 |
} |
| 781 |
// Else, all threads are running so the container can't accept any commands |
| 782 |
IExecutionDMContext triggeringCtx = !e.getDMContext().equals(containerDmc) ? e.getDMContext() : null; |
| 783 |
event = new ContainerResumedEvent(containerDmc, e, triggeringCtx); |
| 784 |
getSession().dispatchEvent(event, getProperties()); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
private void updateThreadState(IMIExecutionDMContext context, ThreadResumedEvent event) { |
| 789 |
StateChangeReason reason = event.getReason(); |
| 790 |
boolean isStepping = reason.equals(StateChangeReason.STEP); |
| 791 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 792 |
if (threadState == null) { |
| 793 |
threadState = new MIThreadRunState(); |
| 794 |
fThreadRunStates.put(context, threadState); |
| 795 |
} |
| 796 |
threadState.fSuspended = false; |
| 797 |
threadState.fResumePending = false; |
| 798 |
threadState.fStateChangeReason = reason; |
| 799 |
threadState.fStepping = isStepping; |
| 800 |
} |
| 801 |
|
| 802 |
@DsfServiceEventHandler |
| 803 |
public void eventDispatched(final MIStoppedEvent e) { |
| 804 |
|
| 805 |
IDMEvent<?> event = null; |
| 806 |
|
| 807 |
// If it's not an execution context (what else could it be?!?), just propagate it |
| 808 |
IMIExecutionDMContext executionDmc = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 809 |
if (executionDmc == null) { |
| 810 |
event = new SuspendedEvent(e.getDMContext(), e); |
| 811 |
getSession().dispatchEvent(event, getProperties()); |
| 812 |
return; |
| 813 |
} |
| 814 |
|
| 815 |
// It's a thread execution context (since we are in non-stop mode) |
| 816 |
event = new ThreadSuspendedEvent(e.getDMContext(), e); |
| 817 |
updateThreadState(executionDmc, (ThreadSuspendedEvent) event); |
| 818 |
getSession().dispatchEvent(event, getProperties()); |
| 819 |
|
| 820 |
// Find the container context, which is used in multi-threaded debugging. |
| 821 |
IContainerDMContext containerDmc = DMContexts.getAncestorOfType(e.getDMContext(), IContainerDMContext.class); |
| 822 |
if (containerDmc != null) { |
| 823 |
IExecutionDMContext triggeringCtx = !e.getDMContext().equals(containerDmc) ? e.getDMContext() : null; |
| 824 |
event = new ContainerSuspendedEvent(containerDmc, e, triggeringCtx); |
| 825 |
getSession().dispatchEvent(event, getProperties()); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
private void updateThreadState(IMIExecutionDMContext context, ThreadSuspendedEvent event) { |
| 830 |
StateChangeReason reason = event.getReason(); |
| 831 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 832 |
if (threadState == null) { |
| 833 |
threadState = new MIThreadRunState(); |
| 834 |
fThreadRunStates.put(context, threadState); |
| 835 |
} |
| 836 |
threadState.fSuspended = true; |
| 837 |
threadState.fResumePending = false; |
| 838 |
threadState.fStepping = false; |
| 839 |
threadState.fStateChangeReason = reason; |
| 840 |
} |
| 841 |
|
| 842 |
@DsfServiceEventHandler |
| 843 |
public void eventDispatched(final MIThreadCreatedEvent e) { |
| 844 |
IContainerDMContext containerDmc = e.getDMContext(); |
| 845 |
IMIExecutionDMContext executionCtx = null; |
| 846 |
if (e.getId() != -1) { |
| 847 |
executionCtx = createMIExecutionContext(containerDmc, e.getId()); |
| 848 |
if (fThreadRunStates.get(executionCtx) == null) { |
| 849 |
fThreadRunStates.put(executionCtx, new MIThreadRunState()); |
| 850 |
} |
| 851 |
} |
| 852 |
getSession().dispatchEvent(new StartedDMEvent(executionCtx, e), getProperties()); |
| 853 |
fMICommandCache.reset(); |
| 854 |
} |
| 855 |
|
| 856 |
@DsfServiceEventHandler |
| 857 |
public void eventDispatched(final MIThreadExitEvent e) { |
| 858 |
IContainerDMContext containerDmc = e.getDMContext(); |
| 859 |
IMIExecutionDMContext executionCtx = null; |
| 860 |
if (e.getId() != -1) { |
| 861 |
executionCtx = createMIExecutionContext(containerDmc, e.getId()); |
| 862 |
fThreadRunStates.remove(executionCtx); |
| 863 |
} |
| 864 |
getSession().dispatchEvent(new ExitedDMEvent(executionCtx, e), getProperties()); |
| 865 |
fMICommandCache.reset(); |
| 866 |
} |
| 867 |
|
| 868 |
@DsfServiceEventHandler |
| 869 |
public void eventDispatched(ThreadResumedEvent e) { |
| 870 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 871 |
if (context == null) { |
| 872 |
return; |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
@DsfServiceEventHandler |
| 877 |
public void eventDispatched(ThreadSuspendedEvent e) { |
| 878 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 879 |
if (context == null) { |
| 880 |
return; |
| 881 |
} |
| 882 |
} |
| 883 |
|
| 884 |
@DsfServiceEventHandler |
| 885 |
public void eventDispatched(ContainerResumedEvent e) { |
| 886 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 887 |
if (context == null) { |
| 888 |
return; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
@DsfServiceEventHandler |
| 893 |
public void eventDispatched(ContainerSuspendedEvent e) { |
| 894 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 895 |
if (context == null) { |
| 896 |
return; |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
@DsfServiceEventHandler |
| 901 |
public void eventDispatched(MIGDBExitEvent e) { |
| 902 |
fTerminated = true; |
| 903 |
} |
| 904 |
|
| 905 |
} |