37
|
1 |
/*******************************************************************************
|
|
2 |
* Copyright (c) 2008 QNX Software 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 |
* QNX Software Systems - Initial API and implementation
|
|
10 |
* Windriver and Ericsson - Updated for DSF
|
|
11 |
*******************************************************************************/
|
|
12 |
package org.eclipse.cdt.dsf.gdb.launching;
|
|
13 |
|
|
14 |
import java.util.ArrayList;
|
|
15 |
import java.util.HashSet;
|
|
16 |
import java.util.concurrent.ExecutionException;
|
|
17 |
|
|
18 |
import org.eclipse.cdt.core.model.ICModelMarker;
|
|
19 |
import org.eclipse.cdt.core.model.ICProject;
|
|
20 |
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
|
|
21 |
import org.eclipse.cdt.dsf.concurrent.DsfExecutor;
|
|
22 |
import org.eclipse.cdt.dsf.concurrent.Query;
|
|
23 |
import org.eclipse.cdt.dsf.concurrent.Sequence;
|
|
24 |
import org.eclipse.cdt.dsf.concurrent.ThreadSafe;
|
|
25 |
import org.eclipse.cdt.dsf.debug.service.IDsfDebugServicesFactory;
|
|
26 |
import org.eclipse.cdt.dsf.debug.sourcelookup.DsfSourceLookupDirector;
|
|
27 |
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
|
|
28 |
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
|
|
29 |
import org.eclipse.cdt.dsf.gdb.service.GdbDebugServicesFactory;
|
|
30 |
import org.eclipse.cdt.dsf.gdb.service.GdbDebugServicesFactoryNS;
|
|
31 |
import org.eclipse.cdt.dsf.gdb.service.SessionType;
|
|
32 |
import org.eclipse.cdt.dsf.service.DsfSession;
|
|
33 |
import org.eclipse.core.resources.IMarker;
|
|
34 |
import org.eclipse.core.resources.IProject;
|
|
35 |
import org.eclipse.core.resources.IResource;
|
|
36 |
import org.eclipse.core.resources.ResourcesPlugin;
|
|
37 |
import org.eclipse.core.runtime.CoreException;
|
|
38 |
import org.eclipse.core.runtime.IPath;
|
|
39 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
40 |
import org.eclipse.core.runtime.IStatus;
|
|
41 |
import org.eclipse.core.runtime.NullProgressMonitor;
|
|
42 |
import org.eclipse.core.runtime.Path;
|
|
43 |
import org.eclipse.core.runtime.Status;
|
|
44 |
import org.eclipse.core.runtime.SubProgressMonitor;
|
|
45 |
import org.eclipse.debug.core.DebugException;
|
|
46 |
import org.eclipse.debug.core.ILaunch;
|
|
47 |
import org.eclipse.debug.core.ILaunchConfiguration;
|
|
48 |
import org.eclipse.debug.core.ILaunchManager;
|
|
49 |
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2;
|
|
50 |
import org.eclipse.debug.core.model.ISourceLocator;
|
|
51 |
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* The shared launch configuration delegate for the DSF/GDB debugger.
|
|
55 |
* This delegate supports all configuration types (local, remote, attach, etc)
|
|
56 |
*/
|
|
57 |
@ThreadSafe
|
|
58 |
public class GdbLaunchDelegate extends LaunchConfigurationDelegate
|
|
59 |
implements ILaunchConfigurationDelegate2
|
|
60 |
{
|
|
61 |
public final static String GDB_DEBUG_MODEL_ID = "org.eclipse.cdt.dsf.gdb"; //$NON-NLS-1$
|
|
62 |
|
|
63 |
private final static String NON_STOP_FIRST_VERSION = "6.8.50"; //$NON-NLS-1$
|
|
64 |
private boolean isNonStopSession = false;
|
|
65 |
|
|
66 |
public void launch( ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor ) throws CoreException {
|
|
67 |
if ( monitor == null ) {
|
|
68 |
monitor = new NullProgressMonitor();
|
|
69 |
}
|
|
70 |
if ( mode.equals( ILaunchManager.DEBUG_MODE ) ) {
|
|
71 |
launchDebugger( config, launch, monitor );
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
private void launchDebugger( ILaunchConfiguration config, ILaunch launch, IProgressMonitor monitor ) throws CoreException {
|
|
76 |
monitor.beginTask("Launching debugger session", 10);
|
|
77 |
if ( monitor.isCanceled() ) {
|
|
78 |
return;
|
|
79 |
}
|
|
80 |
|
|
81 |
try {
|
|
82 |
launchDebugSession( config, launch, monitor );
|
|
83 |
}
|
|
84 |
finally {
|
|
85 |
monitor.done();
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
private void launchDebugSession( final ILaunchConfiguration config, ILaunch l, IProgressMonitor monitor ) throws CoreException {
|
|
90 |
if ( monitor.isCanceled() ) {
|
|
91 |
return;
|
|
92 |
}
|
|
93 |
|
|
94 |
SessionType sessionType = LaunchUtils.getSessionType(config);
|
|
95 |
boolean attach = LaunchUtils.getIsAttach(config);
|
|
96 |
|
|
97 |
final GdbLaunch launch = (GdbLaunch)l;
|
|
98 |
|
|
99 |
if (sessionType == SessionType.REMOTE) {
|
|
100 |
monitor.subTask( "Debugging remote C/C++ application" );
|
|
101 |
} else if (sessionType == SessionType.CORE) {
|
|
102 |
monitor.subTask( "Post Mortem Debugging of C/C++ application" );
|
|
103 |
} else {
|
|
104 |
monitor.subTask( "Debugging local C/C++ application" );
|
|
105 |
}
|
|
106 |
|
|
107 |
IPath exePath = new Path(""); //$NON-NLS-1$
|
|
108 |
// An attach session does not need to necessarily have an
|
|
109 |
// executable specified. This is because:
|
|
110 |
// - In remote multi-process attach, there will be more than one executable
|
|
111 |
// In this case executables need to be specified differently.
|
|
112 |
// The current solution is to use the solib-search-path to specify
|
|
113 |
// the path of any executable we can attach to.
|
|
114 |
// - In local single process, GDB has the ability to find the executable
|
|
115 |
// automatically.
|
|
116 |
//
|
|
117 |
// An attach session also does not need to necessarily have a project
|
|
118 |
// specified. This is because we can perform source lookup towards
|
|
119 |
// code that is outside the workspace.
|
|
120 |
// See bug 244567
|
|
121 |
if (!attach) {
|
|
122 |
// First verify we are dealing with a proper project.
|
|
123 |
ICProject project = LaunchUtils.verifyCProject(config);
|
|
124 |
// Now verify we know the program to debug.
|
|
125 |
exePath = LaunchUtils.verifyProgramPath(config, project);
|
|
126 |
// Finally, make sure the program is a proper binary.
|
|
127 |
LaunchUtils.verifyBinary(config, exePath);
|
|
128 |
}
|
|
129 |
|
|
130 |
monitor.worked( 1 );
|
|
131 |
|
|
132 |
String gdbVersion = getGDBVersion(config);
|
|
133 |
|
|
134 |
// First make sure non-stop is supported, if the user want to use this mode
|
|
135 |
if (isNonStopSession && !isNonStopSupported(gdbVersion)) {
|
|
136 |
throw new DebugException(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED, "Non-stop mode is only supported starting with GDB " + NON_STOP_FIRST_VERSION, null)); //$NON-NLS-1$
|
|
137 |
}
|
|
138 |
|
|
139 |
launch.setServiceFactory(newServiceFactory(gdbVersion));
|
|
140 |
|
|
141 |
// Create and invoke the launch sequence to create the debug control and services
|
|
142 |
IProgressMonitor subMon1 = new SubProgressMonitor(monitor, 4, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
|
|
143 |
final ServicesLaunchSequence servicesLaunchSequence =
|
|
144 |
new ServicesLaunchSequence(launch.getSession(), launch, subMon1);
|
|
145 |
|
|
146 |
launch.getSession().getExecutor().execute(servicesLaunchSequence);
|
|
147 |
try {
|
|
148 |
servicesLaunchSequence.get();
|
|
149 |
} catch (InterruptedException e1) {
|
|
150 |
throw new DebugException(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.INTERNAL_ERROR, "Interrupted Exception in dispatch thread", e1)); //$NON-NLS-1$
|
|
151 |
} catch (ExecutionException e1) {
|
|
152 |
throw new DebugException(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED, "Error in services launch sequence", e1.getCause())); //$NON-NLS-1$
|
|
153 |
}
|
|
154 |
|
|
155 |
if (monitor.isCanceled())
|
|
156 |
return;
|
|
157 |
|
|
158 |
// The initializeControl method should be called after the ICommandControlService
|
|
159 |
// be initialized in the ServicesLaunchSequence above. This is because it is that
|
|
160 |
// service that will trigger the launch cleanup (if we need it during this launch)
|
|
161 |
// through an ICommandControlShutdownDMEvent
|
|
162 |
launch.initializeControl();
|
|
163 |
|
|
164 |
// Add the CLI and "inferior" process objects to the launch.
|
|
165 |
launch.addCLIProcess("gdb"); //$NON-NLS-1$
|
|
166 |
if (!attach && sessionType != SessionType.CORE) {
|
|
167 |
launch.addInferiorProcess(exePath.lastSegment());
|
|
168 |
}
|
|
169 |
|
|
170 |
monitor.worked(1);
|
|
171 |
|
|
172 |
// Create and invoke the final launch sequence to setup GDB
|
|
173 |
IProgressMonitor subMon2 = new SubProgressMonitor(monitor, 4, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
|
|
174 |
final Sequence finalLaunchSequence =
|
|
175 |
getFinalLaunchSequence(launch.getSession().getExecutor(), launch, sessionType, attach, subMon2);
|
|
176 |
|
|
177 |
launch.getSession().getExecutor().execute(finalLaunchSequence);
|
|
178 |
boolean succeed = false;
|
|
179 |
try {
|
|
180 |
finalLaunchSequence.get();
|
|
181 |
succeed = true;
|
|
182 |
} catch (InterruptedException e1) {
|
|
183 |
throw new DebugException(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.INTERNAL_ERROR, "Interrupted Exception in dispatch thread", e1)); //$NON-NLS-1$
|
|
184 |
} catch (ExecutionException e1) {
|
|
185 |
throw new DebugException(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED, "Error in final launch sequence", e1.getCause())); //$NON-NLS-1$
|
|
186 |
} finally {
|
|
187 |
if (!succeed) {
|
|
188 |
// finalLaunchSequence failed. Shutdown the session so that all started
|
|
189 |
// services including any GDB process are shutdown. (bug 251486)
|
|
190 |
//
|
|
191 |
Query<Object> launchShutdownQuery = new Query<Object>() {
|
|
192 |
@Override
|
|
193 |
protected void execute(DataRequestMonitor<Object> rm) {
|
|
194 |
launch.shutdownSession(rm);
|
|
195 |
}
|
|
196 |
};
|
|
197 |
|
|
198 |
launch.getSession().getExecutor().execute(launchShutdownQuery);
|
|
199 |
|
|
200 |
// wait for the shutdown to finish.
|
|
201 |
// The Query.get() method is a synchronous call which blocks until the
|
|
202 |
// query completes.
|
|
203 |
try {
|
|
204 |
launchShutdownQuery.get();
|
|
205 |
} catch (InterruptedException e) {
|
|
206 |
throw new DebugException( new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.INTERNAL_ERROR, "InterruptedException while shutting down debugger launch " + launch, e)); //$NON-NLS-1$
|
|
207 |
} catch (ExecutionException e) {
|
|
208 |
throw new DebugException(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED, "Error in shutting down debugger launch " + launch, e)); //$NON-NLS-1$
|
|
209 |
}
|
|
210 |
}
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Returns the GDB version.
|
|
216 |
* Subclass can override for special need.
|
|
217 |
*
|
|
218 |
* @since 2.0
|
|
219 |
*/
|
|
220 |
protected String getGDBVersion(ILaunchConfiguration config) throws CoreException {
|
|
221 |
return LaunchUtils.getGDBVersion(config);
|
|
222 |
}
|
|
223 |
|
|
224 |
/*
|
|
225 |
* This method can be overridden by subclasses to allow to change the final launch sequence without
|
|
226 |
* having to change the entire GdbLaunchDelegate
|
|
227 |
*/
|
|
228 |
protected Sequence getFinalLaunchSequence(DsfExecutor executor, GdbLaunch launch, SessionType type, boolean attach, IProgressMonitor pm) {
|
|
229 |
return new FinalLaunchSequence(executor, launch, type, attach, pm);
|
|
230 |
}
|
|
231 |
|
|
232 |
|
|
233 |
private boolean isNonStopSession(ILaunchConfiguration config) {
|
|
234 |
try {
|
|
235 |
boolean nonStopMode = config.getAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUGGER_NON_STOP,
|
|
236 |
IGDBLaunchConfigurationConstants.DEBUGGER_NON_STOP_DEFAULT);
|
|
237 |
return nonStopMode;
|
|
238 |
} catch (CoreException e) {
|
|
239 |
}
|
|
240 |
return false;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
@Override
|
|
245 |
public boolean preLaunchCheck(ILaunchConfiguration config, String mode, IProgressMonitor monitor) throws CoreException {
|
|
246 |
// no pre launch check for core file
|
|
247 |
if (mode.equals(ILaunchManager.DEBUG_MODE) && LaunchUtils.getSessionType(config) == SessionType.CORE) return true;
|
|
248 |
|
|
249 |
return super.preLaunchCheck(config, mode, monitor);
|
|
250 |
}
|
|
251 |
|
|
252 |
@Override
|
|
253 |
public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException {
|
|
254 |
// Need to configure the source locator before creating the launch
|
|
255 |
// because once the launch is created and added to launch manager,
|
|
256 |
// the adapters will be created for the whole session, including
|
|
257 |
// the source lookup adapter.
|
|
258 |
|
|
259 |
isNonStopSession = isNonStopSession(configuration);
|
|
260 |
|
|
261 |
GdbLaunch launch = new GdbLaunch(configuration, mode, null);
|
|
262 |
launch.initialize();
|
|
263 |
launch.setSourceLocator(getSourceLocator(configuration, launch.getSession()));
|
|
264 |
return launch;
|
|
265 |
}
|
|
266 |
|
|
267 |
private ISourceLocator getSourceLocator(ILaunchConfiguration configuration, DsfSession session) throws CoreException {
|
|
268 |
DsfSourceLookupDirector locator = new DsfSourceLookupDirector(session);
|
|
269 |
String memento = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String)null);
|
|
270 |
if (memento == null) {
|
|
271 |
locator.initializeDefaults(configuration);
|
|
272 |
} else {
|
|
273 |
locator.initializeFromMemento(memento, configuration);
|
|
274 |
}
|
|
275 |
return locator;
|
|
276 |
}
|
|
277 |
|
|
278 |
/**
|
|
279 |
* Recursively creates a set of projects referenced by the current project
|
|
280 |
*
|
|
281 |
* @param proj
|
|
282 |
* The current project
|
|
283 |
* @param referencedProjSet
|
|
284 |
* A set of referenced projects
|
|
285 |
* @throws CoreException
|
|
286 |
* if an error occurs while getting referenced projects from the
|
|
287 |
* current project
|
|
288 |
*/
|
|
289 |
private HashSet<IProject> getReferencedProjectSet(IProject proj, HashSet<IProject> referencedProjSet) throws CoreException {
|
|
290 |
// The top project is a reference too and it must be added at the top to avoid cycles
|
|
291 |
referencedProjSet.add(proj);
|
|
292 |
|
|
293 |
IProject[] projects = proj.getReferencedProjects();
|
|
294 |
for (IProject refProject : projects) {
|
|
295 |
if (refProject.exists() && !referencedProjSet.contains(refProject)) {
|
|
296 |
getReferencedProjectSet(refProject, referencedProjSet);
|
|
297 |
}
|
|
298 |
}
|
|
299 |
return referencedProjSet;
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* Returns the order list of projects to build before launching.
|
|
304 |
* Used in buildForLaunch()
|
|
305 |
*/
|
|
306 |
@Override
|
|
307 |
protected IProject[] getBuildOrder(ILaunchConfiguration configuration, String mode) throws CoreException {
|
|
308 |
IProject[] orderedProjects = null;
|
|
309 |
ArrayList<IProject> orderedProjList = null;
|
|
310 |
|
|
311 |
ICProject cProject = LaunchUtils.verifyCProject(configuration);
|
|
312 |
if (cProject != null) {
|
|
313 |
HashSet<IProject> projectSet = getReferencedProjectSet(cProject.getProject(), new HashSet<IProject>());
|
|
314 |
|
|
315 |
String[] orderedNames = ResourcesPlugin.getWorkspace().getDescription().getBuildOrder();
|
|
316 |
if (orderedNames != null) {
|
|
317 |
//Projects may not be in the build order but should still be built if selected
|
|
318 |
ArrayList<IProject> unorderedProjects = new ArrayList<IProject>(projectSet.size());
|
|
319 |
unorderedProjects.addAll(projectSet);
|
|
320 |
orderedProjList = new ArrayList<IProject>(projectSet.size());
|
|
321 |
|
|
322 |
for (String projectName : orderedNames) {
|
|
323 |
for (IProject proj : unorderedProjects) {
|
|
324 |
if (proj.getName().equals(projectName)) {
|
|
325 |
orderedProjList.add(proj);
|
|
326 |
unorderedProjects.remove(proj);
|
|
327 |
break;
|
|
328 |
}
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
// Add any remaining projects to the end of the list
|
|
333 |
orderedProjList.addAll(unorderedProjects);
|
|
334 |
|
|
335 |
orderedProjects = orderedProjList.toArray(new IProject[orderedProjList.size()]);
|
|
336 |
} else {
|
|
337 |
// Try the project prerequisite order then
|
|
338 |
IProject[] projects = projectSet.toArray(new IProject[projectSet.size()]);
|
|
339 |
orderedProjects = ResourcesPlugin.getWorkspace().computeProjectOrder(projects).projects;
|
|
340 |
}
|
|
341 |
}
|
|
342 |
return orderedProjects;
|
|
343 |
}
|
|
344 |
|
|
345 |
/* Used in finalLaunchCheck() */
|
|
346 |
@Override
|
|
347 |
protected IProject[] getProjectsForProblemSearch(ILaunchConfiguration configuration, String mode) throws CoreException {
|
|
348 |
return getBuildOrder(configuration, mode);
|
|
349 |
}
|
|
350 |
|
|
351 |
/**
|
|
352 |
* Searches for compile errors in the specified project
|
|
353 |
* Used in finalLaunchCheck()
|
|
354 |
* @param proj
|
|
355 |
* The project to search
|
|
356 |
* @return true if compile errors exist, otherwise false
|
|
357 |
*/
|
|
358 |
@Override
|
|
359 |
protected boolean existsProblems(IProject proj) throws CoreException {
|
|
360 |
IMarker[] markers = proj.findMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);
|
|
361 |
if (markers.length > 0) {
|
|
362 |
for (IMarker marker : markers) {
|
|
363 |
Integer severity = (Integer)marker.getAttribute(IMarker.SEVERITY);
|
|
364 |
if (severity != null) {
|
|
365 |
return severity.intValue() >= IMarker.SEVERITY_ERROR;
|
|
366 |
}
|
|
367 |
}
|
|
368 |
}
|
|
369 |
return false;
|
|
370 |
}
|
|
371 |
|
|
372 |
private boolean isNonStopSupported(String version) {
|
|
373 |
if (NON_STOP_FIRST_VERSION.compareTo(version) <= 0) {
|
|
374 |
return true;
|
|
375 |
}
|
|
376 |
return false;
|
|
377 |
}
|
|
378 |
|
|
379 |
// A subclass can override this method and provide its own ServiceFactory.
|
|
380 |
protected IDsfDebugServicesFactory newServiceFactory(String version) {
|
|
381 |
|
|
382 |
if (isNonStopSession && isNonStopSupported(version)) {
|
|
383 |
return new GdbDebugServicesFactoryNS(version);
|
|
384 |
}
|
|
385 |
|
|
386 |
if (version.startsWith("6.6") || //$NON-NLS-1$
|
|
387 |
version.startsWith("6.7") || //$NON-NLS-1$
|
|
388 |
version.startsWith("6.8")) { //$NON-NLS-1$
|
|
389 |
return new GdbDebugServicesFactory(version);
|
|
390 |
}
|
|
391 |
|
|
392 |
return new GdbDebugServicesFactory(version);
|
|
393 |
}
|
|
394 |
}
|