Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d26bfa0554d1c413264100d4aeaf95cee457850a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*******************************************************************************
 * Copyright (c) 2008, 2017 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.ui.contexts;

import java.util.concurrent.RejectedExecutionException;

import org.eclipse.cdt.dsf.concurrent.ConfinedToDsfExecutor;
import org.eclipse.cdt.dsf.concurrent.CountingRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.ImmediateExecutor;
import org.eclipse.cdt.dsf.concurrent.ThreadSafe;
import org.eclipse.cdt.dsf.datamodel.DataModelInitializedEvent;
import org.eclipse.cdt.dsf.debug.service.IRunControl;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IContainerDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.service.DsfServiceEventHandler;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.ui.contexts.ISuspendTrigger;
import org.eclipse.debug.ui.contexts.ISuspendTriggerListener;

/**
 * DSF implementation of the ISuspendTrigger interface.  The suspend trigger
 * is used by the IDE to trigger activation of the debug perspective when
 * the debugger suspends.
 *
 * @see ISuspendTrigger
 *
 * @since 1.0
 */
@ConfinedToDsfExecutor("fSession.getExecutor()")
public class DsfSuspendTrigger implements ISuspendTrigger {

	private final DsfSession fSession;
	private final ILaunch fLaunch;
	private volatile boolean fDisposed = false;
	private boolean fEventListenerRegisterd = false;
	private final DsfServicesTracker fServicesTracker;

	@ThreadSafe
	private final ListenerList<ISuspendTriggerListener> fListeners = new ListenerList<>();

	@ThreadSafe
	public DsfSuspendTrigger(DsfSession session, ILaunch launch) {
		fSession = session;
		fLaunch = launch;
		fServicesTracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(), fSession.getId());
		try {
			fSession.getExecutor().execute(new DsfRunnable() {
				@Override
				public void run() {
					if (!fDisposed) {
						fSession.addServiceEventListener(DsfSuspendTrigger.this, null);
						fEventListenerRegisterd = true;
					}
				}
			});
		} catch (RejectedExecutionException e) {
		}
	}

	@ThreadSafe
	@Override
	public void addSuspendTriggerListener(final ISuspendTriggerListener listener) {
		fListeners.add(listener);

		// Check if an execution context in the model is already suspended.
		// If so notify the listener.
		getIsLaunchSuspended(new DataRequestMonitor<Boolean>(ImmediateExecutor.getInstance(), null) {
			@Override
			protected void handleSuccess() {
				if (!fDisposed && getData().booleanValue()) {
					listener.suspended(fLaunch, null);
				}
			}

			@Override
			protected void handleErrorOrWarning() {
				// Ignore expected race condition and not supported error.
				// Log other errors.
				if (getStatus().getCode() > IDsfStatusConstants.NOT_SUPPORTED) {
					super.handleErrorOrWarning();
				}
			}
		});
	}

	@ThreadSafe
	@Override
	public void removeSuspendTriggerListener(ISuspendTriggerListener listener) {
		fListeners.remove(listener);
	}

	@ThreadSafe
	public void dispose() {
		try {
			fSession.getExecutor().execute(new DsfRunnable() {
				@Override
				public void run() {
					if (fEventListenerRegisterd) {
						fSession.removeServiceEventListener(DsfSuspendTrigger.this);
					}
				}
			});
		} catch (RejectedExecutionException e) {
			// Session already gone.
		}

		fServicesTracker.dispose();
		fDisposed = true;
	}

	@DsfServiceEventHandler
	public void eventDispatched(IRunControl.ISuspendedDMEvent e) {
		fireSuspended(null);
	}

	/**
	 * @noreference This method is not intended to be referenced by clients.
	 */
	@DsfServiceEventHandler
	public void eventDispatched(DataModelInitializedEvent e) {
		getIsLaunchSuspended(new DataRequestMonitor<Boolean>(ImmediateExecutor.getInstance(), null) {
			@Override
			protected void handleSuccess() {
				if (!fDisposed && getData().booleanValue()) {
					fireSuspended(null);
				}
			}

			@Override
			protected void handleErrorOrWarning() {
				// Ignore expected race condition and not supported error.
				// Log other errors.
				if (getStatus().getCode() > IDsfStatusConstants.NOT_SUPPORTED) {
					super.handleErrorOrWarning();
				}
			}
		});
	}

	/**
	 * Returns the services tracker used by the suspend trigger.
	 * @since 2.1
	 */
	protected DsfServicesTracker getServicesTracker() {
		return fServicesTracker;
	}

	/**
	 * Returns the launch for this suspend trigger.
	 * @since 2.1
	 */
	@ThreadSafe
	protected ILaunch getLaunch() {
		return fLaunch;
	}

	/**
	 * Returns the DSF session for this suspend trigger.
	 * @since 2.1
	 */
	@ThreadSafe
	protected DsfSession getSession() {
		return fSession;
	}

	/**
	 * Notifies the listeners that a suspend event was received.
	 *
	 * @param context
	 *
	 * @since 2.1
	 */
	@ThreadSafe
	protected void fireSuspended(final Object context) {
		if (!fListeners.isEmpty()) {
			new Job("DSF Suspend Trigger Notify") { //$NON-NLS-1$
				{
					setSystem(true);
				}

				@Override
				protected IStatus run(IProgressMonitor monitor) {
					final MultiStatus status = new MultiStatus(DsfUIPlugin.PLUGIN_ID, 0,
							"DSF Suspend Trigger Notify Job Status", null); //$NON-NLS-1$
					for (final ISuspendTriggerListener listener : fListeners) {
						SafeRunner.run(new ISafeRunnable() {
							@Override
							public void run() throws Exception {
								listener.suspended(fLaunch, context);
							}

							@Override
							public void handleException(Throwable exception) {
								status.add(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID,
										"Exception while calling suspend trigger listeners", exception)); //$NON-NLS-1$
							}

						});
					}
					return status;
				}
			}.schedule();
		}
	}

	/**
	 * Retrieves the top-level containers for this launch.  This method should
	 * be overriden by specific debugger integrations.
	 * @param rm
	 *
	 * @since 2.1
	 */
	@ThreadSafe
	protected void getLaunchTopContainers(DataRequestMonitor<IContainerDMContext[]> rm) {
		rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, IDsfStatusConstants.NOT_SUPPORTED,
				"Not implemented.", null)); //$NON-NLS-1$
		rm.done();
	}

	/**
	 * Checks if the given launch is currently suspended.
	 *
	 * @param rm Request monitor.
	 *
	 * @since 2.1
	 */
	@ThreadSafe
	private void getIsLaunchSuspended(final DataRequestMonitor<Boolean> rm) {
		getLaunchTopContainers(new DataRequestMonitor<IContainerDMContext[]>(fSession.getExecutor(), rm) {
			@Override
			protected void handleSuccess() {
				final CountingRequestMonitor crm = new CountingRequestMonitor(fSession.getExecutor(), rm) {
					@Override
					protected void handleSuccess() {
						if (rm.getData() == null) {
							rm.setData(Boolean.FALSE);
						}
						rm.done();
					};
				};
				int count = 0;
				for (final IContainerDMContext containerCtx : getData()) {
					getIsContainerSuspended(containerCtx,
							new DataRequestMonitor<Boolean>(ImmediateExecutor.getInstance(), crm) {
								@Override
								protected void handleSuccess() {
									if (getData().booleanValue()) {
										rm.setData(Boolean.TRUE);
									}
									crm.done();
								};
							});
					count++;
				}
				crm.setDoneCount(count);
			}
		});
	}

	/**
	 * Recursively checks if the given container or any of its execution
	 * contexts are suspended.
	 *
	 * @param container Container to check.
	 * @param rm Request monitor.
	 *
	 * @since 2.1
	 */
	@ConfinedToDsfExecutor("fSession.getExecutor()")
	private void getIsContainerSuspended(final IContainerDMContext container, final DataRequestMonitor<Boolean> rm) {
		// Check if run control service is still available.
		IRunControl rc = fServicesTracker.getService(IRunControl.class);
		if (rc == null) {
			rm.setData(Boolean.FALSE);
			rm.done();
			return;
		}

		// Check if container is suspended.  If so, stop searching.
		if (rc.isSuspended(container)) {
			rm.setData(Boolean.TRUE);
			rm.done();
			return;
		}

		// Retrieve the execution contexts and check if any of them are suspended.
		rc.getExecutionContexts(container, new DataRequestMonitor<IExecutionDMContext[]>(fSession.getExecutor(), rm) {
			@Override
			protected void handleSuccess() {
				// Check if run control service is still available.
				IRunControl rc = fServicesTracker.getService(IRunControl.class);
				if (rc == null) {
					rm.setData(Boolean.FALSE);
					rm.done();
					return;
				}

				// If any of the execution contexts are suspended, stop searching
				boolean hasContainers = false;
				for (IExecutionDMContext execCtx : getData()) {
					if (rc.isSuspended(execCtx)) {
						rm.setData(Boolean.TRUE);
						rm.done();
						return;
					}
					hasContainers = hasContainers || execCtx instanceof IContainerDMContext;
				}

				// If any of the returned contexts were containers, check them recursively.
				if (hasContainers) {
					final CountingRequestMonitor crm = new CountingRequestMonitor(fSession.getExecutor(), rm) {
						@Override
						protected void handleSuccess() {
							if (rm.getData() == null) {
								rm.setData(Boolean.FALSE);
							}
							rm.done();
						};
					};
					int count = 0;
					for (IExecutionDMContext execCtx : getData()) {
						if (execCtx instanceof IContainerDMContext) {
							getIsContainerSuspended((IContainerDMContext) execCtx,
									new DataRequestMonitor<Boolean>(ImmediateExecutor.getInstance(), crm) {
										@Override
										protected void handleSuccess() {
											if (getData().booleanValue()) {
												rm.setData(Boolean.TRUE);
											}
											crm.done();
										};
									});
							count++;
						}
					}
					crm.setDoneCount(count);
				} else {
					rm.setData(Boolean.FALSE);
					rm.done();
				}
			}
		});
	}

}

Back to the top