|
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.RequestMonitor; |
| 22 |
import org.eclipse.dd.dsf.datamodel.DMContexts; |
| 23 |
import org.eclipse.dd.dsf.datamodel.IDMContext; |
| 24 |
import org.eclipse.dd.dsf.datamodel.IDMEvent; |
| 25 |
import org.eclipse.dd.dsf.debug.service.IStack.IFrameDMContext; |
| 26 |
import org.eclipse.dd.dsf.debug.service.command.CommandCache; |
| 27 |
import org.eclipse.dd.dsf.service.AbstractDsfService; |
| 28 |
import org.eclipse.dd.dsf.service.DsfServiceEventHandler; |
| 29 |
import org.eclipse.dd.dsf.service.DsfSession; |
| 30 |
import org.eclipse.dd.mi.internal.MIPlugin; |
| 31 |
import org.eclipse.dd.mi.service.command.AbstractMIControl; |
| 32 |
import org.eclipse.dd.mi.service.command.commands.MIExecContinue; |
| 33 |
import org.eclipse.dd.mi.service.command.commands.MIExecFinish; |
| 34 |
import org.eclipse.dd.mi.service.command.commands.MIExecInterrupt; |
| 35 |
import org.eclipse.dd.mi.service.command.commands.MIExecNext; |
| 36 |
import org.eclipse.dd.mi.service.command.commands.MIExecNextInstruction; |
| 37 |
import org.eclipse.dd.mi.service.command.commands.MIExecStep; |
| 38 |
import org.eclipse.dd.mi.service.command.commands.MIExecStepInstruction; |
| 39 |
import org.eclipse.dd.mi.service.command.commands.MIExecUntil; |
| 40 |
import org.eclipse.dd.mi.service.command.commands.MIThreadListIds; |
| 41 |
import org.eclipse.dd.mi.service.command.events.MIGDBExitEvent; |
| 42 |
import org.eclipse.dd.mi.service.command.events.MIRunningEvent; |
| 43 |
import org.eclipse.dd.mi.service.command.events.MIStoppedEvent; |
| 44 |
import org.eclipse.dd.mi.service.command.events.MIThreadCreatedEvent; |
| 45 |
import org.eclipse.dd.mi.service.command.events.MIThreadExitEvent; |
| 46 |
import org.eclipse.dd.mi.service.command.output.MIInfo; |
| 47 |
import org.eclipse.dd.mi.service.command.output.MIThreadListIdsInfo; |
| 48 |
import org.osgi.framework.BundleContext; |
| 49 |
|
| 50 |
/** |
| 51 |
* Implementation note: This class implements event handlers for the events that |
| 52 |
* are generated by this service itself. When the event is dispatched, these |
| 53 |
* handlers will be called first, before any of the clients. These handlers |
| 54 |
* update the service's internal state information to make them consistent with |
| 55 |
* the events being issued. Doing this in the handlers as opposed to when the |
| 56 |
* events are generated, guarantees that the state of the service will always be |
| 57 |
* consistent with the events. The purpose of this pattern is to allow clients |
| 58 |
* that listen to service events and track service state, to be perfectly in |
| 59 |
* sync with the service state. |
| 60 |
*/ |
| 61 |
public class MIRunControlNS extends AbstractDsfService implements |
| 62 |
IMIRunControl { |
| 63 |
|
| 64 |
protected class MIThreadRunState { |
| 65 |
// State flags |
| 66 |
boolean fSuspended = false; |
| 67 |
boolean fResumePending = false; |
| 68 |
boolean fStepping = false; |
| 69 |
StateChangeReason fStateChangeReason; |
| 70 |
} |
| 71 |
|
| 72 |
/////////////////////////////////////////////////////////////////////////// |
| 73 |
// MIRunControlNS |
| 74 |
/////////////////////////////////////////////////////////////////////////// |
| 75 |
|
| 76 |
private AbstractMIControl fConnection; |
| 77 |
|
| 78 |
private boolean fTerminated = false; |
| 79 |
|
| 80 |
// ThreadStates indexed by the execution context |
| 81 |
protected Map<IMIExecutionDMContext, MIThreadRunState> fThreadRunStates = new HashMap<IMIExecutionDMContext, MIThreadRunState>(); |
| 82 |
|
| 83 |
/////////////////////////////////////////////////////////////////////////// |
| 84 |
// Initialization and shutdown |
| 85 |
/////////////////////////////////////////////////////////////////////////// |
| 86 |
|
| 87 |
public MIRunControlNS(DsfSession session) { |
| 88 |
super(session); |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void initialize(final RequestMonitor rm) { |
| 93 |
super.initialize(new RequestMonitor(getExecutor(), rm) { |
| 94 |
@Override |
| 95 |
protected void handleSuccess() { |
| 96 |
doInitialize(rm); |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
private void doInitialize(final RequestMonitor rm) { |
| 102 |
fConnection = getServicesTracker().getService(AbstractMIControl.class); |
| 103 |
getSession().addServiceEventListener(this, null); |
| 104 |
rm.done(); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void shutdown(final RequestMonitor rm) { |
| 109 |
getSession().removeServiceEventListener(this); |
| 110 |
super.shutdown(rm); |
| 111 |
} |
| 112 |
|
| 113 |
/////////////////////////////////////////////////////////////////////////// |
| 114 |
// AbstractDsfService |
| 115 |
/////////////////////////////////////////////////////////////////////////// |
| 116 |
|
| 117 |
@Override |
| 118 |
protected BundleContext getBundleContext() { |
| 119 |
return MIPlugin.getBundleContext(); |
| 120 |
} |
| 121 |
|
| 122 |
/////////////////////////////////////////////////////////////////////////// |
| 123 |
// IDMService |
| 124 |
/////////////////////////////////////////////////////////////////////////// |
| 125 |
|
| 126 |
@SuppressWarnings("unchecked") |
| 127 |
public void getModelData(IDMContext dmc, DataRequestMonitor<?> rm) { |
| 128 |
if (dmc instanceof IExecutionDMContext) { |
| 129 |
getExecutionData((IExecutionDMContext) dmc, (DataRequestMonitor<IExecutionDMData>) rm); |
| 130 |
} else { |
| 131 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$ |
| 132 |
rm.done(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/////////////////////////////////////////////////////////////////////////// |
| 137 |
// IRunControl |
| 138 |
/////////////////////////////////////////////////////////////////////////// |
| 139 |
|
| 140 |
// ------------------------------------------------------------------------ |
| 141 |
// Suspend |
| 142 |
// ------------------------------------------------------------------------ |
| 143 |
|
| 144 |
public boolean isSuspended(IExecutionDMContext context) { |
| 145 |
|
| 146 |
// Thread case |
| 147 |
if (context instanceof MIExecutionDMC) { |
| 148 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 149 |
return (threadState == null) ? false : !fTerminated && threadState.fSuspended; |
| 150 |
} |
| 151 |
|
| 152 |
// Container case |
| 153 |
if (context instanceof IContainerDMContext) { |
| 154 |
boolean isSuspended = false; |
| 155 |
for (IMIExecutionDMContext threadContext : fThreadRunStates.keySet()) { |
| 156 |
if (DMContexts.isAncestorOf(threadContext, context)) { |
| 157 |
isSuspended |= isSuspended(threadContext); |
| 158 |
} |
| 159 |
} |
| 160 |
return isSuspended; |
| 161 |
} |
| 162 |
|
| 163 |
// Default case |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
public void canSuspend(IExecutionDMContext context, DataRequestMonitor<Boolean> rm) { |
| 168 |
|
| 169 |
// Thread case |
| 170 |
if (context instanceof MIExecutionDMC) { |
| 171 |
rm.setData(doCanSuspend(context)); |
| 172 |
rm.done(); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// Container case |
| 177 |
if (context instanceof IContainerDMContext) { |
| 178 |
boolean canSuspend = false; |
| 179 |
for (IMIExecutionDMContext threadContext : fThreadRunStates.keySet()) { |
| 180 |
if (DMContexts.isAncestorOf(threadContext, context)) { |
| 181 |
canSuspend |= doCanSuspend(threadContext); |
| 182 |
} |
| 183 |
} |
| 184 |
rm.setData(canSuspend); |
| 185 |
rm.done(); |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
// Default case |
| 190 |
rm.setData(false); |
| 191 |
rm.done(); |
| 192 |
} |
| 193 |
|
| 194 |
private boolean doCanSuspend(IExecutionDMContext context) { |
| 195 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 196 |
return (threadState == null) ? false : !fTerminated && !threadState.fSuspended; |
| 197 |
} |
| 198 |
|
| 199 |
public void suspend(IExecutionDMContext context, final RequestMonitor rm) { |
| 200 |
|
| 201 |
assert context != null; |
| 202 |
|
| 203 |
// Thread case |
| 204 |
IMIExecutionDMContext thread = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 205 |
if (thread != null) { |
| 206 |
doSuspendThread(thread, rm); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
// Container case |
| 211 |
IContainerDMContext container = DMContexts.getAncestorOfType(context, IContainerDMContext.class); |
| 212 |
if (container != null) { |
| 213 |
doSuspendContainer(container, rm); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
// Default case |
| 218 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, "Invalid context type.", null)); //$NON-NLS-1$ |
| 219 |
rm.done(); |
| 220 |
} |
| 221 |
|
| 222 |
private void doSuspendThread(IMIExecutionDMContext context, final RequestMonitor rm) { |
| 223 |
|
| 224 |
if (!doCanSuspend(context)) { |
| 225 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 226 |
"Given context: " + context + ", is already suspended.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 227 |
rm.done(); |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
MIExecInterrupt cmd = new MIExecInterrupt(context, true); |
| 232 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 233 |
} |
| 234 |
|
| 235 |
private void doSuspendContainer(IExecutionDMContext context, final RequestMonitor rm) { |
| 236 |
MIExecInterrupt cmd = new MIExecInterrupt(context, true); |
| 237 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 238 |
} |
| 239 |
|
| 240 |
// ------------------------------------------------------------------------ |
| 241 |
// Resume |
| 242 |
// ------------------------------------------------------------------------ |
| 243 |
|
| 244 |
public void canResume(IExecutionDMContext context, DataRequestMonitor<Boolean> rm) { |
| 245 |
|
| 246 |
// Thread case |
| 247 |
if (context instanceof MIExecutionDMC) { |
| 248 |
rm.setData(doCanResume(context)); |
| 249 |
rm.done(); |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
// Container case |
| 254 |
if (context instanceof IContainerDMContext) { |
| 255 |
boolean canSuspend = false; |
| 256 |
for (IMIExecutionDMContext threadContext : fThreadRunStates.keySet()) { |
| 257 |
if (DMContexts.isAncestorOf(threadContext, context)) { |
| 258 |
canSuspend |= doCanResume(threadContext); |
| 259 |
} |
| 260 |
} |
| 261 |
rm.setData(canSuspend); |
| 262 |
rm.done(); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
// Default case |
| 267 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, "Invalid context type.", null)); //$NON-NLS-1$ |
| 268 |
rm.done(); |
| 269 |
} |
| 270 |
|
| 271 |
private boolean doCanResume(IExecutionDMContext context) { |
| 272 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 273 |
return (threadState == null) ? false : !fTerminated && threadState.fSuspended && !threadState.fResumePending; |
| 274 |
} |
| 275 |
|
| 276 |
public void resume(IExecutionDMContext context, final RequestMonitor rm) { |
| 277 |
|
| 278 |
assert context != null; |
| 279 |
|
| 280 |
// Thread case |
| 281 |
IMIExecutionDMContext thread = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 282 |
if (thread != null) { |
| 283 |
doResumeThread(thread, rm); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
// Container case |
| 288 |
IContainerDMContext container = DMContexts.getAncestorOfType(context, IContainerDMContext.class); |
| 289 |
if (container != null) { |
| 290 |
doResumeContainer(container, rm); |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
// Default case |
| 295 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, "Invalid context type.", null)); //$NON-NLS-1$ |
| 296 |
rm.done(); |
| 297 |
} |
| 298 |
|
| 299 |
private void doResumeThread(IMIExecutionDMContext context, final RequestMonitor rm) { |
| 300 |
|
| 301 |
if (!doCanResume(context)) { |
| 302 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 303 |
"Given context: " + context + ", is already running.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 304 |
rm.done(); |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 309 |
if (threadState == null) { |
| 310 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 311 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 312 |
rm.done(); |
| 313 |
return; |
| 314 |
} |
| 315 |
threadState.fResumePending = true; |
| 316 |
|
| 317 |
MIExecContinue cmd = new MIExecContinue(context, true); |
| 318 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 319 |
} |
| 320 |
|
| 321 |
private void doResumeContainer(IExecutionDMContext context, final RequestMonitor rm) { |
| 322 |
MIExecContinue cmd = new MIExecContinue(context, true); |
| 323 |
fConnection.queueCommand(cmd, new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 324 |
} |
| 325 |
|
| 326 |
// ------------------------------------------------------------------------ |
| 327 |
// Step |
| 328 |
// ------------------------------------------------------------------------ |
| 329 |
|
| 330 |
public boolean isStepping(IExecutionDMContext context) { |
| 331 |
|
| 332 |
// If it's a thread, just look it up |
| 333 |
if (context instanceof MIExecutionDMC) { |
| 334 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 335 |
return (threadState == null) ? false : !fTerminated && threadState.fStepping; |
| 336 |
} |
| 337 |
|
| 338 |
// Default case |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
public void canStep(IExecutionDMContext context, StepType stepType, DataRequestMonitor<Boolean> rm) { |
| 343 |
|
| 344 |
// If it's a thread, just look it up |
| 345 |
if (context instanceof MIExecutionDMC) { |
| 346 |
canResume(context, rm); |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
// If it's a container, then we don't want to step it |
| 351 |
rm.setData(false); |
| 352 |
rm.done(); |
| 353 |
} |
| 354 |
|
| 355 |
public void step(IExecutionDMContext context, StepType stepType, final RequestMonitor rm) { |
| 356 |
|
| 357 |
assert context != null; |
| 358 |
|
| 359 |
IMIExecutionDMContext dmc = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 360 |
if (dmc == null) { |
| 361 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 362 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 363 |
rm.done(); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
if (!doCanResume(context)) { |
| 368 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 369 |
"Cannot resume context", null)); //$NON-NLS-1$ |
| 370 |
rm.done(); |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 375 |
if (threadState == null) { |
| 376 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 377 |
"Given context: " + context + " can't be found.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 378 |
rm.done(); |
| 379 |
return; |
| 380 |
} |
| 381 |
|
| 382 |
threadState.fResumePending = true; |
| 383 |
threadState.fStepping = true; |
| 384 |
|
| 385 |
switch (stepType) { |
| 386 |
case STEP_INTO: |
| 387 |
fConnection.queueCommand(new MIExecStep(dmc, true), |
| 388 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 389 |
break; |
| 390 |
case STEP_OVER: |
| 391 |
fConnection.queueCommand(new MIExecNext(dmc, true), |
| 392 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 393 |
break; |
| 394 |
case STEP_RETURN: |
| 395 |
// The -exec-finish command operates on the selected stack frame, but here we always |
| 396 |
// want it to operate on the stop stack frame. So we manually create a top-frame |
| 397 |
// context to use with the MI command. |
| 398 |
// We get a local instance of the stack service because the stack service can be shut |
| 399 |
// down before the run control service is shut down. So it is possible for the |
| 400 |
// getService() request below to return null. |
| 401 |
MIStack stackService = getServicesTracker().getService(MIStack.class); |
| 402 |
if (stackService != null) { |
| 403 |
IFrameDMContext topFrameDmc = stackService.createFrameDMContext(dmc, 0); |
| 404 |
fConnection.queueCommand(new MIExecFinish(topFrameDmc), |
| 405 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 406 |
} else { |
| 407 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 408 |
"Cannot create context for command, stack service not available.", null)); //$NON-NLS-1$ |
| 409 |
rm.done(); |
| 410 |
} |
| 411 |
break; |
| 412 |
case INSTRUCTION_STEP_INTO: |
| 413 |
fConnection.queueCommand(new MIExecStepInstruction(dmc, true), |
| 414 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 415 |
break; |
| 416 |
case INSTRUCTION_STEP_OVER: |
| 417 |
fConnection.queueCommand(new MIExecNextInstruction(dmc, true), |
| 418 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 419 |
break; |
| 420 |
default: |
| 421 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, |
| 422 |
INTERNAL_ERROR, "Given step type not supported", null)); //$NON-NLS-1$ |
| 423 |
rm.done(); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
// ------------------------------------------------------------------------ |
| 428 |
// Run to line |
| 429 |
// ------------------------------------------------------------------------ |
| 430 |
|
| 431 |
// Later add support for Address and function. |
| 432 |
// skipBreakpoints is not used at the moment. Implement later |
| 433 |
public void runToLine(IExecutionDMContext context, String fileName, String lineNo, boolean skipBreakpoints, final DataRequestMonitor<MIInfo> rm) { |
| 434 |
|
| 435 |
assert context != null; |
| 436 |
|
| 437 |
IMIExecutionDMContext dmc = DMContexts.getAncestorOfType(context, IMIExecutionDMContext.class); |
| 438 |
if (dmc == null) { |
| 439 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, NOT_SUPPORTED, |
| 440 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 441 |
rm.done(); |
| 442 |
return; |
| 443 |
} |
| 444 |
|
| 445 |
if (!doCanResume(context)) { |
| 446 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 447 |
"Cannot resume context", null)); //$NON-NLS-1$ |
| 448 |
rm.done(); |
| 449 |
return; |
| 450 |
} |
| 451 |
|
| 452 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 453 |
if (threadState == null) { |
| 454 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_STATE, |
| 455 |
"Given context: " + context + " is not an MI execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 456 |
rm.done(); |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
threadState.fResumePending = true; |
| 461 |
fConnection.queueCommand(new MIExecUntil(dmc, fileName + ":" + lineNo), //$NON-NLS-1$ |
| 462 |
new DataRequestMonitor<MIInfo>(getExecutor(), rm)); |
| 463 |
} |
| 464 |
|
| 465 |
// ------------------------------------------------------------------------ |
| 466 |
// Support functions |
| 467 |
// ------------------------------------------------------------------------ |
| 468 |
|
| 469 |
public void getExecutionContexts(final IContainerDMContext containerDmc, final DataRequestMonitor<IExecutionDMContext[]> rm) { |
| 470 |
fConnection.queueCommand(new MIThreadListIds(containerDmc), |
| 471 |
new DataRequestMonitor<MIThreadListIdsInfo>(getExecutor(), rm) { |
| 472 |
@Override |
| 473 |
protected void handleSuccess() { |
| 474 |
rm.setData(makeExecutionDMCs(containerDmc, getData())); |
| 475 |
rm.done(); |
| 476 |
} |
| 477 |
}); |
| 478 |
} |
| 479 |
|
| 480 |
private IExecutionDMContext[] makeExecutionDMCs(IContainerDMContext containerCtx, MIThreadListIdsInfo info) { |
| 481 |
IExecutionDMContext[] executionDmcs = new IMIExecutionDMContext[info.getThreadIds().length]; |
| 482 |
for (int i = 0; i < info.getThreadIds().length; i++) { |
| 483 |
executionDmcs[i] = createMIExecutionContext(containerCtx, info.getThreadIds()[i]); |
| 484 |
} |
| 485 |
return executionDmcs; |
| 486 |
} |
| 487 |
|
| 488 |
public void getExecutionData(IExecutionDMContext dmc, DataRequestMonitor<IExecutionDMData> rm) { |
| 489 |
MIThreadRunState threadState = fThreadRunStates.get(dmc); |
| 490 |
if (threadState == null) { |
| 491 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID,INVALID_HANDLE, |
| 492 |
"Given context: " + dmc + " is not a recognized execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 493 |
rm.done(); |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
if (dmc instanceof IMIExecutionDMContext) { |
| 498 |
rm.setData(new ExecutionData(threadState.fSuspended ? threadState.fStateChangeReason : null)); |
| 499 |
} else { |
| 500 |
rm.setStatus(new Status(IStatus.ERROR, MIPlugin.PLUGIN_ID, INVALID_HANDLE, |
| 501 |
"Given context: " + dmc + " is not a recognized execution context.", null)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 502 |
} |
| 503 |
rm.done(); |
| 504 |
} |
| 505 |
|
| 506 |
/////////////////////////////////////////////////////////////////////////// |
| 507 |
// IMIRunControl |
| 508 |
/////////////////////////////////////////////////////////////////////////// |
| 509 |
|
| 510 |
public IMIExecutionDMContext createMIExecutionContext(IContainerDMContext container, int threadId) { |
| 511 |
return new MIExecutionDMC(getSession().getId(), container, threadId); |
| 512 |
} |
| 513 |
|
| 514 |
/////////////////////////////////////////////////////////////////////////// |
| 515 |
// IMIRunControl |
| 516 |
/////////////////////////////////////////////////////////////////////////// |
| 517 |
|
| 518 |
public CommandCache getCache() { |
| 519 |
return null; |
| 520 |
} |
| 521 |
|
| 522 |
public AbstractMIControl getConnection() { |
| 523 |
return fConnection; |
| 524 |
} |
| 525 |
|
| 526 |
/////////////////////////////////////////////////////////////////////////// |
| 527 |
// Event handlers |
| 528 |
/////////////////////////////////////////////////////////////////////////// |
| 529 |
|
| 530 |
@DsfServiceEventHandler |
| 531 |
public void eventDispatched(final MIRunningEvent e) { |
| 532 |
|
| 533 |
IDMEvent<?> event = null; |
| 534 |
|
| 535 |
// If it's not an execution context (what else could it be?!?), just propagate it |
| 536 |
IMIExecutionDMContext executionDmc = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 537 |
if (executionDmc == null) { |
| 538 |
event = new ResumedEvent(e.getDMContext(), e); |
| 539 |
getSession().dispatchEvent(event, getProperties()); |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
// It's a thread execution context (since we are in non-stop mode) |
| 544 |
event = new ThreadResumedEvent(e.getDMContext(), e); |
| 545 |
updateThreadState(executionDmc, (ThreadResumedEvent) event); |
| 546 |
getSession().dispatchEvent(event, getProperties()); |
| 547 |
|
| 548 |
// Find the container context, which is used in multi-threaded debugging. |
| 549 |
IContainerDMContext containerDmc = DMContexts.getAncestorOfType(e.getDMContext(), IContainerDMContext.class); |
| 550 |
if (containerDmc != null) { |
| 551 |
// If there is a single stopped thread in that container, bail out. |
| 552 |
for (IExecutionDMContext ctx : fThreadRunStates.keySet()) { |
| 553 |
if (containerDmc.equals(ctx.getParents()[0])) { |
| 554 |
if (fThreadRunStates.get(ctx).fSuspended) |
| 555 |
return; |
| 556 |
} |
| 557 |
} |
| 558 |
// Else, all threads are running so the container can't accept any commands |
| 559 |
IExecutionDMContext triggeringCtx = !e.getDMContext().equals(containerDmc) ? e.getDMContext() : null; |
| 560 |
event = new ContainerResumedEvent(containerDmc, e, triggeringCtx); |
| 561 |
getSession().dispatchEvent(event, getProperties()); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
private void updateThreadState(IMIExecutionDMContext context, ThreadResumedEvent event) { |
| 566 |
StateChangeReason reason = event.getReason(); |
| 567 |
boolean isStepping = reason.equals(StateChangeReason.STEP); |
| 568 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 569 |
if (threadState == null) { |
| 570 |
threadState = new MIThreadRunState(); |
| 571 |
fThreadRunStates.put(context, threadState); |
| 572 |
} |
| 573 |
threadState.fSuspended = false; |
| 574 |
threadState.fResumePending = false; |
| 575 |
threadState.fStateChangeReason = reason; |
| 576 |
threadState.fStepping = isStepping; |
| 577 |
} |
| 578 |
|
| 579 |
@DsfServiceEventHandler |
| 580 |
public void eventDispatched(final MIStoppedEvent e) { |
| 581 |
|
| 582 |
IDMEvent<?> event = null; |
| 583 |
|
| 584 |
// If it's not an execution context (what else could it be?!?), just propagate it |
| 585 |
IMIExecutionDMContext executionDmc = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 586 |
if (executionDmc == null) { |
| 587 |
event = new SuspendedEvent(e.getDMContext(), e); |
| 588 |
getSession().dispatchEvent(event, getProperties()); |
| 589 |
return; |
| 590 |
} |
| 591 |
|
| 592 |
// It's a thread execution context (since we are in non-stop mode) |
| 593 |
event = new ThreadSuspendedEvent(e.getDMContext(), e); |
| 594 |
updateThreadState(executionDmc, (ThreadSuspendedEvent) event); |
| 595 |
getSession().dispatchEvent(event, getProperties()); |
| 596 |
|
| 597 |
// Find the container context, which is used in multi-threaded debugging. |
| 598 |
IContainerDMContext containerDmc = DMContexts.getAncestorOfType(e.getDMContext(), IContainerDMContext.class); |
| 599 |
if (containerDmc != null) { |
| 600 |
IExecutionDMContext triggeringCtx = !e.getDMContext().equals(containerDmc) ? e.getDMContext() : null; |
| 601 |
event = new ContainerSuspendedEvent(containerDmc, e, triggeringCtx); |
| 602 |
getSession().dispatchEvent(event, getProperties()); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
private void updateThreadState(IMIExecutionDMContext context, ThreadSuspendedEvent event) { |
| 607 |
StateChangeReason reason = event.getReason(); |
| 608 |
MIThreadRunState threadState = fThreadRunStates.get(context); |
| 609 |
if (threadState == null) { |
| 610 |
threadState = new MIThreadRunState(); |
| 611 |
fThreadRunStates.put(context, threadState); |
| 612 |
} |
| 613 |
threadState.fSuspended = true; |
| 614 |
threadState.fResumePending = false; |
| 615 |
threadState.fStepping = false; |
| 616 |
threadState.fStateChangeReason = reason; |
| 617 |
} |
| 618 |
|
| 619 |
@DsfServiceEventHandler |
| 620 |
public void eventDispatched(final MIThreadCreatedEvent e) { |
| 621 |
IContainerDMContext containerDmc = e.getDMContext(); |
| 622 |
IMIExecutionDMContext executionCtx = null; |
| 623 |
if (e.getId() != -1) { |
| 624 |
executionCtx = createMIExecutionContext(containerDmc, e.getId()); |
| 625 |
if (fThreadRunStates.get(executionCtx) == null) { |
| 626 |
fThreadRunStates.put(executionCtx, new MIThreadRunState()); |
| 627 |
} |
| 628 |
} |
| 629 |
getSession().dispatchEvent(new StartedDMEvent(executionCtx, e), getProperties()); |
| 630 |
} |
| 631 |
|
| 632 |
@DsfServiceEventHandler |
| 633 |
public void eventDispatched(final MIThreadExitEvent e) { |
| 634 |
IContainerDMContext containerDmc = e.getDMContext(); |
| 635 |
IMIExecutionDMContext executionCtx = null; |
| 636 |
if (e.getId() != -1) { |
| 637 |
executionCtx = createMIExecutionContext(containerDmc, e.getId()); |
| 638 |
fThreadRunStates.remove(executionCtx); |
| 639 |
} |
| 640 |
getSession().dispatchEvent(new ExitedDMEvent(executionCtx, e), getProperties()); |
| 641 |
} |
| 642 |
|
| 643 |
@DsfServiceEventHandler |
| 644 |
public void eventDispatched(ThreadResumedEvent e) { |
| 645 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 646 |
if (context == null) { |
| 647 |
return; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
@DsfServiceEventHandler |
| 652 |
public void eventDispatched(ThreadSuspendedEvent e) { |
| 653 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 654 |
if (context == null) { |
| 655 |
return; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
@DsfServiceEventHandler |
| 660 |
public void eventDispatched(ContainerResumedEvent e) { |
| 661 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 662 |
if (context == null) { |
| 663 |
return; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
@DsfServiceEventHandler |
| 668 |
public void eventDispatched(ContainerSuspendedEvent e) { |
| 669 |
IMIExecutionDMContext context = DMContexts.getAncestorOfType(e.getDMContext(), IMIExecutionDMContext.class); |
| 670 |
if (context == null) { |
| 671 |
return; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
@DsfServiceEventHandler |
| 676 |
public void eventDispatched(MIGDBExitEvent e) { |
| 677 |
fTerminated = true; |
| 678 |
} |
| 679 |
|
| 680 |
} |