Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4532112d119d883cfead1cc408b2d69db1c5b9f9 (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
/*******************************************************************************
 * Copyright (c) 2008, 2014 Ericsson 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:
 *     Ericsson - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.actions;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfExecutor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.service.IMultiDetach;
import org.eclipse.cdt.dsf.debug.service.IProcesses;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IContainerDMContext;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService;
import org.eclipse.cdt.dsf.gdb.internal.ui.GdbUIPlugin;
import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.dsf.service.DsfSession.SessionEndedListener;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.commands.IDebugCommandRequest;
import org.eclipse.debug.core.commands.IDisconnectHandler;
import org.eclipse.debug.core.commands.IEnabledStateRequest;

public class GdbDisconnectCommand implements IDisconnectHandler {
	private final DsfSession fSession;
	private final DsfExecutor fExecutor;
	private final DsfServicesTracker fTracker;

	public GdbDisconnectCommand(DsfSession session) {
		super();
		fSession = session;
		fExecutor = session.getExecutor();
		fTracker = new DsfServicesTracker(GdbUIPlugin.getBundleContext(), session.getId());
	}

	public void dispose() {
		fTracker.dispose();
	}

	@Override
	public void canExecute(final IEnabledStateRequest request) {
		if (request.getElements().length == 0) {
			request.setEnabled(false);
			request.done();
			return;
		}

		getContainerDMContexts(request.getElements(), new DataRequestMonitor<IContainerDMContext[]>(fExecutor, null) {
			@Override
			protected void handleCompleted() {
				if (!isSuccess()) {
					request.setEnabled(false);
					request.done();
				} else {
					canDisconnect(getData(), new ImmediateDataRequestMonitor<Boolean>() {
						@Override
						protected void handleCompleted() {
							if (!isSuccess()) {
								request.setEnabled(false);
							} else {
								request.setEnabled(getData());

							}
							request.done();
						}
					});
				}
			}
		});
	}

	@Override
	public boolean execute(final IDebugCommandRequest request) {
		if (request.getElements().length == 0) {
			request.done();
			return false;
		}

		getContainerDMContexts(request.getElements(), new DataRequestMonitor<IContainerDMContext[]>(fExecutor, null) {
			@Override
			protected void handleCompleted() {
				if (!isSuccess()) {
					request.setStatus(getStatus());
					request.done();
				} else {
					disconnect(getData(), new ImmediateRequestMonitor() {
						@Override
						protected void handleCompleted() {
							if (!isSuccess()) {
								request.setStatus(getStatus());
								request.done();
							} else {
								waitForTermination(request);
							}
						}
					});
				}
			}
		});

		return false;
	}

	/**
	 * Wait for the debug session to be fully shutdown before reporting
	 * that the terminate was completed.  This is important for the
	 * 'Terminate and remove' operation.
	 * The wait time is limited with a timeout so as to eventually complete the
	 * request in the case of termination error, or when terminating
	 * a single process in a multi-process session.
	 * See bug 377447
	 */
	private void waitForTermination(final IDebugCommandRequest request) {
		// It is possible that the session already had time to terminate
		if (!DsfSession.isSessionActive(fSession.getId())) {
			request.done();
			return;
		}

		// Listener that will indicate when the shutdown is complete
		final SessionEndedListener endedListener = new SessionEndedListener() {
			@Override
			public void sessionEnded(DsfSession session) {
				if (fSession.equals(session)) {
					DsfSession.removeSessionEndedListener(this);
					request.done();
				}
			}
		};

		DsfSession.addSessionEndedListener(endedListener);

		// Create the timeout
		// For a multi-process session, if a single process is
		// terminated, this timeout will always hit (unless the
		// session is also terminated before the timeout).
		// We haven't found a problem with delaying the completion
		// of the request that way.
		// Note that this timeout is not removed even if we don't
		// need it anymore, once the session has terminated;
		// instead, we let it timeout and ignore it if the session
		// is already terminated.
		fExecutor.schedule(new Runnable() {
			@Override
			public void run() {
				// Check that the session is still active when the timeout hits.
				// If it is not, then everything has been cleaned up already.
				if (DsfSession.isSessionActive(fSession.getId())) {
					DsfSession.removeSessionEndedListener(endedListener);

					// Marking the request as cancelled will prevent the removal of
					// the launch from the Debug view in case of "Terminate and Remove".
					// This is important for multi-process sessions when "Terminate and Remove"
					// is applied to one of the running processes. In this case the selected
					// process will be terminated but the associated launch will not be removed
					// from the Debug view.
					request.setStatus(Status.CANCEL_STATUS);
					request.done();
				}
			}
		}, 1, TimeUnit.MINUTES);
	}

	private void getContainerDMContexts(Object[] elements, final DataRequestMonitor<IContainerDMContext[]> rm) {
		GdbLaunch launch = null;
		final Set<IContainerDMContext> contDmcs = new HashSet<>();
		for (Object obj : elements) {
			if (obj instanceof GdbLaunch) {
				launch = (GdbLaunch) obj;
				break;
			}
			if (obj instanceof IDMVMContext) {
				IContainerDMContext contDmc = DMContexts.getAncestorOfType(((IDMVMContext) obj).getDMContext(),
						IContainerDMContext.class);
				if (contDmc != null) {
					contDmcs.add(contDmc);
				}
			}
		}
		if (launch == null) {
			rm.setData(contDmcs.toArray(new IContainerDMContext[contDmcs.size()]));
			rm.done();
		} else {
			try {
				fExecutor.execute(new DsfRunnable() {
					@Override
					public void run() {
						ICommandControlService commandControl = fTracker.getService(ICommandControlService.class);
						final IProcesses procService = fTracker.getService(IProcesses.class);
						if (commandControl != null && procService != null) {
							procService.getProcessesBeingDebugged(commandControl.getContext(),
									new ImmediateDataRequestMonitor<IDMContext[]>() {
										@Override
										protected void handleCompleted() {
											if (!isSuccess()) {
												rm.setStatus(getStatus());
											} else {
												for (IDMContext ctx : getData()) {
													IContainerDMContext contDmc = DMContexts.getAncestorOfType(ctx,
															IContainerDMContext.class);
													if (contDmc != null) {
														contDmcs.add(contDmc);
													}
												}
												rm.setData(contDmcs.toArray(new IContainerDMContext[contDmcs.size()]));
											}
											rm.done();
										}
									});
						} else {
							rm.setStatus(new Status(IStatus.ERROR, GdbUIPlugin.PLUGIN_ID, "Service is not available.")); //$NON-NLS-1$
							rm.done();
						}
					}
				});
			} catch (RejectedExecutionException e) {
				rm.setStatus(new Status(IStatus.ERROR, GdbUIPlugin.PLUGIN_ID, e.getLocalizedMessage()));
				rm.done();
			}
		}
	}

	private void canDisconnect(IContainerDMContext[] contDmcs, DataRequestMonitor<Boolean> rm) {
		if (contDmcs.length == 0) {
			rm.setData(false);
			rm.done();
			return;
		}

		IMultiDetach multiDetach = fTracker.getService(IMultiDetach.class);
		if (multiDetach != null) {
			multiDetach.canDetachDebuggerFromSomeProcesses(contDmcs, rm);
		} else {
			IProcesses procService = fTracker.getService(IProcesses.class);
			if (procService != null && contDmcs.length == 1) {
				procService.canDetachDebuggerFromProcess(contDmcs[0], rm);
			} else {
				rm.setData(false);
				rm.done();
			}
		}
	}

	private void disconnect(IContainerDMContext[] contDmcs, RequestMonitor rm) {
		if (contDmcs.length == 0) {
			rm.done();
			return;
		}

		IMultiDetach multiDetach = fTracker.getService(IMultiDetach.class);
		if (multiDetach != null) {
			multiDetach.detachDebuggerFromProcesses(contDmcs, rm);
		} else {
			IProcesses procService = fTracker.getService(IProcesses.class);
			if (procService != null && contDmcs.length == 1) {
				procService.detachDebuggerFromProcess(contDmcs[0], rm);
			} else {
				rm.done();
			}
		}
	}
}

Back to the top