Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f78187319c04bdb99695c3670c146df5c2291d1 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*******************************************************************************
 * Copyright (c) 2015 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.launch.cdt.launching;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
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.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch;
import org.eclipse.cdt.dsf.gdb.launching.GdbLaunchDelegate;
import org.eclipse.cdt.dsf.gdb.service.command.IGDBControl;
import org.eclipse.cdt.dsf.mi.service.IMICommandControl;
import org.eclipse.cdt.dsf.mi.service.command.CommandFactory;
import org.eclipse.cdt.dsf.mi.service.command.output.MIInfo;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.services.ServiceUtils;
import org.eclipse.tcf.te.tcf.core.streams.StreamsDataReceiver;
import org.eclipse.tcf.te.tcf.launch.cdt.activator.Activator;
import org.eclipse.tcf.te.tcf.launch.cdt.interfaces.IGdbserverLaunchHandlerDelegate;
import org.eclipse.tcf.te.tcf.launch.cdt.interfaces.IRemoteTEConfigurationConstants;
import org.eclipse.tcf.te.tcf.launch.cdt.nls.Messages;
import org.eclipse.tcf.te.tcf.launch.cdt.utils.TEHelper;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.processes.core.launcher.ProcessLauncher;

/**
 * Abstract launch delegate implementation handling launching the gdbserver via TCF/TE.
 */
public abstract class TEGdbAbstractLaunchDelegate extends GdbLaunchDelegate {

	/**
	 * Constructor
	 */
	public TEGdbAbstractLaunchDelegate() {
		super();
	}

	/**
	 * Constructor
	 *
	 * @param requireCProject <code>True</code> if a C project is required for launching,
	 *            <code>false</code> otherwise.
	 */
	public TEGdbAbstractLaunchDelegate(boolean requireCProject) {
		super(requireCProject);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.gdb.launching.GdbLaunchDelegate#createGdbLaunch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.model.ISourceLocator)
	 */
	@Override
	protected GdbLaunch createGdbLaunch(ILaunchConfiguration configuration, String mode, ISourceLocator locator) throws CoreException {
	    return new TEGdbLaunch(configuration, mode, locator);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.gdb.launching.GdbLaunchDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
		// If not of the expected type --> return immediately
		if (!(launch instanceof GdbLaunch)) return;

		// Initialize TE
		Activator.getDefault().initializeTE();
		// Get the peer from the launch configuration
		IPeer peer = TEHelper.getCurrentConnection(config).getPeer();

		// Determine if the launch is an attach launch
		final boolean isAttachLaunch = ICDTLaunchConfigurationConstants.ID_LAUNCH_C_ATTACH.equals(config.getType().getIdentifier());

		// Get the executable path (run/debug application) or the PID (attach to application)
		IPath exePath = checkBinaryDetails(config);
		String remoteExePath = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_REMOTE_PATH, (String)null);
		String remotePID = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_REMOTE_PID, (String)null);

		// Not an attach launch and the executable is not specified --> abort
		if (!isAttachLaunch && exePath == null) {
			abort(Messages.TEGdbAbstractLaunchDelegate_no_program, null, ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
		}
		// Not an attach launch and the remote executable is not specified --> abort
		if (!isAttachLaunch && remoteExePath == null) {
			abort(Messages.TEGdbAbstractLaunchDelegate_no_remote_path, null, ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
		}
		// Attach launch and the remote PID is not specified --> abort
		if (isAttachLaunch && remotePID == null) {
			abort(Messages.TEGdbAbstractLaunchDelegate_no_pid, null, ICDTLaunchConfigurationConstants.ERR_NO_PROCESSID);
		}

		// If an executable path is specified, download the binary if needed
		if (!isAttachLaunch && exePath != null && remoteExePath != null) {
			monitor.setTaskName(Messages.TEGdbAbstractLaunchDelegate_downloading);
			boolean skipDownload = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_SKIP_DOWNLOAD_TO_TARGET, false);

			if (!skipDownload) {
				try {
					TEHelper.remoteFileTransfer(peer, exePath.toString(), remoteExePath, new SubProgressMonitor(monitor, 80));
				}
				catch (IOException e) {
					abort(NLS.bind(Messages.TEGdbAbstractLaunchDelegate_filetransferFailed, e.getLocalizedMessage()), e, ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
				}
			}
		}

		// Launch gdbserver on target
		final AtomicReference<String> gdbserverPortNumber = new AtomicReference<String>(config.getAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT, IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT_DEFAULT));
		final AtomicReference<String> gdbserverPortNumberMappedTo = new AtomicReference<String>(config.getAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT_MAPPED_TO, (String) null));
		final String gdbserverCommand = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_COMMAND, IRemoteTEConfigurationConstants.ATTR_GDBSERVER_COMMAND_DEFAULT);
		final List<String> gdbserverPortNumberAlternatives = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT_ALTERNATIVES, (List<String>) null);
		final List<String> gdbserverPortNumberMappedToAlternatives = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT_MAPPED_TO_ALTERNATIVES, (List<String>) null);

// For quick testing.
//		final List<String> gdbserverPortNumberAlternatives = new ArrayList<String>(Arrays.asList(new String[] { "49152", "49153", "49154", "49155", "49156" })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
//		final List<String> gdbserverPortNumberMappedToAlternatives = new ArrayList<String>(Arrays.asList(new String[] { "49652", "49653", "49654", "49655", "49656" })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

		// Remember the originally configured port number and mapped to port number
		final String origGdbserverPortNumber = gdbserverPortNumber.get();
		final String origGdbserverPortNumberMappedTo = gdbserverPortNumberMappedTo.get();
		final AtomicReference<String> origGdbserverOutput = new AtomicReference<String>(null);

		ProcessLauncher launcher = null;

		final AtomicBoolean gdbserverLaunchRetry = new AtomicBoolean(false);
//		final AtomicBoolean portRangeExceeded = new AtomicBoolean(false);
		final AtomicInteger indexAlternatives = new AtomicInteger(0);

		do {
			gdbserverLaunchRetry.set(false);

			final AtomicBoolean gdbServerStarted = new AtomicBoolean(false);
			final AtomicBoolean gdbServerReady = new AtomicBoolean(false);
			final AtomicBoolean gdbServerExited = new AtomicBoolean(false);
			final StringBuilder gdbServerOutput = new StringBuilder();
			final Object lock = new Object();

			String commandArguments = ""; //$NON-NLS-1$
			if (isAttachLaunch) {
				commandArguments = "--attach :" + gdbserverPortNumber.get() + " " + remotePID; //$NON-NLS-1$ //$NON-NLS-2$
				monitor.setTaskName(Messages.TEGdbAbstractLaunchDelegate_attaching_program);
			} else {
				commandArguments = ":" + gdbserverPortNumber.get() + " " + TEHelper.spaceEscapify(remoteExePath); //$NON-NLS-1$ //$NON-NLS-2$

				String arguments = getProgramArguments(config);
				String prelaunchCmd = config.getAttribute(IRemoteTEConfigurationConstants.ATTR_PRERUN_COMMANDS, ""); //$NON-NLS-1$

				TEHelper.launchCmd(peer, prelaunchCmd, null, new SubProgressMonitor(monitor, 2), new Callback());

				if (arguments != null && !arguments.equals("")) { //$NON-NLS-1$
					commandArguments += " " + arguments; //$NON-NLS-1$
				}
				monitor.setTaskName(Messages.TEGdbAbstractLaunchDelegate_starting_program);
			}

			final GdbLaunch l = (GdbLaunch) launch;
			final Callback callback = new Callback() {
				@Override
				protected void internalDone(Object caller, IStatus status) {
					if (!status.isOK()) {
						gdbServerOutput.append(status.getMessage());
						gdbServerExited.set(true);
						synchronized (lock) {
							lock.notifyAll();
						}
					}
					else {
						gdbServerStarted.set(true);
					}
					super.internalDone(caller, status);
				}
			};

			StreamsDataReceiver.Listener listener = new StreamsDataReceiver.Listener() {

				@Override
				public void dataReceived(String data) {
					gdbServerOutput.append(data);
					if (data.contains("Listening on port")) { //$NON-NLS-1$
						gdbServerReady.set(true);
						synchronized (lock) {
							lock.notifyAll();
						}
					}
					else if (data.contains("GDBserver exiting") || data.contains("Exiting")) { //$NON-NLS-1$ //$NON-NLS-2$
						// Check if the gdbserver exited because the port is already in use
						if (gdbServerOutput.toString().contains("Address already in use.")) { //$NON-NLS-1$
							// If we have still alternatives, then retry the gdbserver launch
							// with an alternative port
							if (!gdbserverPortNumberAlternatives.isEmpty()) {
								String newPort = null;
								String newPortMappedTo = null;

								do {
									newPort = gdbserverPortNumberAlternatives.get(indexAlternatives.get());
									if (gdbserverPortNumber.get().equals(newPort)) {
										newPort = null;
									} else {
										newPortMappedTo = gdbserverPortNumberMappedToAlternatives != null && !gdbserverPortNumberMappedToAlternatives.isEmpty() ? gdbserverPortNumberMappedToAlternatives.get(indexAlternatives.get()) : null;
									}
									indexAlternatives.getAndIncrement();
								} while (newPort == null && indexAlternatives.get() < gdbserverPortNumberAlternatives.size());

								if (newPort != null) {
									// Remember the original error
									if (origGdbserverOutput.get() == null) origGdbserverOutput.set(gdbServerOutput.toString());
									// Set the flag to retry the gdbserver launch
									gdbserverLaunchRetry.set(true);
									// Update the ports
									gdbserverPortNumber.set(newPort);
									gdbserverPortNumberMappedTo.set(newPortMappedTo);
								}
							}
						}
						gdbServerExited.set(true);
						synchronized (lock) {
							lock.notifyAll();
						}
					}

				}
			};

			launcher = TEHelper.launchCmd(peer, gdbserverCommand, commandArguments, listener, new SubProgressMonitor(monitor, 3), callback);

			// Now wait until gdbserver is up and running on the remote host
			while (!gdbServerReady.get() && !gdbServerExited.get()) {
				if (monitor.isCanceled()) {
					// gdbserver launch failed
					// Need to shutdown the DSF launch session because it is
					// partially started already.
					shutdownSession(l, Messages.TEGdbAbstractLaunchDelegate_canceledMsg, launcher);
				}
				if (gdbServerStarted.get() && launcher.getChannel() == null) {
					// gdbserver died or exited. Wait a little bit to process
					// possible gdbserver output before shutting down the session.
					synchronized (lock) {
						try {
							lock.wait(500);
						}
						catch (InterruptedException e) { /* ignored on purpose */ }
					}
					if (!gdbserverLaunchRetry.get()) {
						shutdownSession(l, origGdbserverOutput.get() != null ? origGdbserverOutput.get() : gdbServerOutput.toString(), launcher);
					}
				}
				synchronized (lock) {
					try {
						lock.wait(300);
					}
					catch (InterruptedException e) { /* ignored on purpose */ }
				}
			}

			// If the gdbserver exited, also shutdown the DSF launch session
			if (!gdbserverLaunchRetry.get() && gdbServerExited.get()) {
				shutdownSession(l, origGdbserverOutput.get() != null ? origGdbserverOutput.get() : gdbServerOutput.toString(), launcher);
			}
		} while (gdbserverLaunchRetry.get());

		// Set the launcher to the launch
		if (launch instanceof TEGdbLaunch) {
			((TEGdbLaunch)launch).setLauncher(launcher);
		}

		// Let debugger know how gdbserver was started on the remote
		ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
		if (!origGdbserverPortNumber.equals(gdbserverPortNumber.get())) {
			wc.setAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT, gdbserverPortNumber.get());
		}
		if (origGdbserverPortNumberMappedTo != null && !origGdbserverPortNumberMappedTo.equals(gdbserverPortNumberMappedTo.get())) {
			wc.setAttribute(IRemoteTEConfigurationConstants.ATTR_GDBSERVER_PORT_MAPPED_TO, gdbserverPortNumberMappedTo.get());
		}
		wc.setAttribute(IGDBLaunchConfigurationConstants.ATTR_REMOTE_TCP, true);
		wc.setAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, TEHelper.getCurrentConnection(config).getPeer().getAttributes().get(IPeer.ATTR_IP_HOST));
		wc.setAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, gdbserverPortNumberMappedTo.get() == null || "".equals(gdbserverPortNumberMappedTo.get()) ? gdbserverPortNumber.get() : gdbserverPortNumberMappedTo.get()); //$NON-NLS-1$
		wc.doSave();
		try {
			super.launch(config, mode, launch, monitor);
		}
		catch (CoreException ex) {
			// Launch failed, need to kill gdbserver
			if (launcher != null) launcher.terminate();
			// report failure further
			throw ex;
		}
		finally {
			monitor.done();
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.gdb.launching.GdbLaunchDelegate#launchDebugSession(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
	 */
    @Override
	protected void launchDebugSession(ILaunchConfiguration config, ILaunch l, IProgressMonitor monitor) throws CoreException {
	    super.launchDebugSession(config, l, monitor);

		// Determine if the launch is an attach launch
		final boolean isAttachLaunch = ICDTLaunchConfigurationConstants.ID_LAUNCH_C_ATTACH.equals(config.getType().getIdentifier());
		if (!isAttachLaunch) return;

		final IPath exePath = checkBinaryDetails(config);

	    if (l instanceof GdbLaunch && exePath != null) {
	    	final GdbLaunch launch = (GdbLaunch) l;
	    	final DsfExecutor executor = launch.getDsfExecutor();

	    	executor.execute(new DsfRunnable() {
	    		@Override
	    		public void run() {
	    	        DsfServicesTracker tracker = new DsfServicesTracker(Activator.getDefault().getBundle().getBundleContext(), launch.getSession().getId());

	    	        try {
	    	        	IGDBControl commandControl = tracker.getService(IGDBControl.class);
	    	        	CommandFactory commandFactory = tracker.getService(IMICommandControl.class).getCommandFactory();

	    	        	commandControl.queueCommand(
	    	        					commandFactory.createMIFileSymbolFile(commandControl.getContext(), exePath.toString()),
	    	        					new ImmediateDataRequestMonitor<MIInfo>());
	    	        } finally {
	    	        	tracker.dispose();
	    	        }
	    		}
	    	});

	    }
	}

	/**
	 * Shutdown the GDB debug session.
	 *
	 * @param launch The GDB launch. Must not be <code>null</code>.
	 * @param details Error message, may be <code>null</code>
	 * @throws CoreException If the GDB debug session shutdown failed.
	 */
	protected void shutdownSession(final GdbLaunch launch, final String details, final ProcessLauncher launcher) throws CoreException {
		Assert.isNotNull(launch);
		try {
			launch.getSession().getExecutor().submit(new DsfRunnable() {
				@Override
				public void run() {
					// Avoid an NPE while running the shutdown
					if (launch.getDsfExecutor() != null) {
						launch.shutdownSession(new ImmediateRequestMonitor());
					}
					// Make sure that the gdbserver is killed and the launcher resources gets disposed
					if (launcher != null) launcher.terminate();
				}
			}).get(1000, TimeUnit.MILLISECONDS);
		}
		catch (RejectedExecutionException e) {
			// Session disposed.
		}
		catch (Exception e) {
			// Ignore exceptions during shutdown.
		}

		// Normalize the gdbserver start failure details
		String details2 = normalizeDetails(launch, details);

		String msg = Messages.TEGdbAbstractLaunchDelegate_gdbserverFailedToStartErrorMessage;
		if (details2 != null && details2.length() > 0) msg = NLS.bind(Messages.TEGdbAbstractLaunchDelegate_gdbserverFailedToStartErrorWithDetails, details2);
		abort(msg, null, ICDTLaunchConfigurationConstants.ERR_DEBUGGER_NOT_INSTALLED);
	}

	/**
	 * Normalize the gdbserver launch failure details message.
	 *
	 * @param launch The launch. Must not be <code>null</code>
	 * @param details The details message or <code>null</code>.
	 *
	 * @return The normalized details message or <code>null</code>.
	 *
	 * @throws CoreException In case of an failure accessing any launch configuration attribute or similar.
	 */
	protected String normalizeDetails(final GdbLaunch launch, final String details) throws CoreException {
		Assert.isNotNull(launch);

		// Get the launch configuration from the launch
		final ILaunchConfiguration lc = launch.getLaunchConfiguration();

		String d = details;
		if (d != null && !"".equals(d)) { //$NON-NLS-1$
			// Try the delegate first if available
			IPeerNode peerNode = TEHelper.getCurrentConnection(lc);
			Assert.isNotNull(peerNode);
			IGdbserverLaunchHandlerDelegate delegate = ServiceUtils.getDelegateServiceDelegate(peerNode, peerNode, IGdbserverLaunchHandlerDelegate.class);
			if (delegate != null) {
				d = delegate.normalizeGdbserverLaunchFailureDetailsMessage(launch, details);
			} else {
				// Rewrite "Address in use" error.
				if (d.contains("Address already in use.")) { //$NON-NLS-1$
					// Get host and port
					String host = lc.getAttribute(IGDBLaunchConfigurationConstants.ATTR_HOST, (String)null);
					String port = lc.getAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT, (String)null);

					String address = host + (port != null ? ":" + port : ""); //$NON-NLS-1$ //$NON-NLS-2$
					d = NLS.bind(Messages.TEGdbAbstractLaunchDelegate_error_addressInUse, address);
				}
			}
		}

		return d;
	}

	protected String getProgramArguments(ILaunchConfiguration config) throws CoreException {
		String args = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, (String) null);
		if (args != null) {
			args = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(args);
		}
		return args;
	}

	@Override
	protected String getPluginID() {
		return Activator.PLUGIN_ID;
	}

}

Back to the top