1
/*******************************************************************************
2
* Copyright (c) 2004, 2010 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
9
* QNX Software Systems - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.launch.internal;
13
import java.io.FileNotFoundException;
15
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
16
import org.eclipse.cdt.core.model.ICProject;
17
import org.eclipse.cdt.debug.core.CDIDebugModel;
18
import org.eclipse.cdt.debug.core.CDebugUtils;
19
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
20
import org.eclipse.cdt.debug.core.ICDebugConfiguration;
21
import org.eclipse.cdt.debug.core.cdi.CDIException;
22
import org.eclipse.cdt.debug.core.cdi.ICDISession;
23
import org.eclipse.cdt.debug.core.cdi.model.ICDITarget;
24
import org.eclipse.cdt.launch.AbstractCLaunchDelegate;
25
import org.eclipse.cdt.launch.internal.ui.LaunchMessages;
26
import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin;
27
import org.eclipse.core.resources.IFile;
28
import org.eclipse.core.runtime.CoreException;
29
import org.eclipse.core.runtime.IPath;
30
import org.eclipse.core.runtime.IProgressMonitor;
31
import org.eclipse.core.runtime.IStatus;
32
import org.eclipse.core.runtime.NullProgressMonitor;
33
import org.eclipse.core.runtime.Status;
34
import org.eclipse.core.runtime.SubProgressMonitor;
35
import org.eclipse.debug.core.DebugPlugin;
36
import org.eclipse.debug.core.ILaunch;
37
import org.eclipse.debug.core.ILaunchConfiguration;
38
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
39
import org.eclipse.debug.core.ILaunchManager;
40
import org.eclipse.debug.core.IStatusHandler;
41
import org.eclipse.osgi.util.NLS;
43
public class LocalAttachLaunchDelegate extends AbstractCLaunchDelegate {
48
* @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration,
49
* java.lang.String, org.eclipse.debug.core.ILaunch,
50
* org.eclipse.core.runtime.IProgressMonitor)
52
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
53
IBinaryObject exeFile = null;
55
if (monitor == null) {
56
monitor = new NullProgressMonitor();
59
monitor.beginTask(LaunchMessages.LocalAttachLaunchDelegate_Attaching_to_Local_C_Application, 10);
60
// check for cancellation
61
if (monitor.isCanceled()) {
66
ICProject cproject = CDebugUtils.verifyCProject(config);
67
IPath exePath = CDebugUtils.getProgramPath(config);
68
if (exePath != null && !exePath.isEmpty()) {
69
if (!exePath.isAbsolute()) {
70
IFile wsProgramPath = cproject.getProject().getFile(exePath);
71
exePath = wsProgramPath.getLocation();
73
if (!exePath.toFile().exists()) {
74
abort(LaunchMessages.AbstractCLaunchDelegate_Program_file_does_not_exist,
75
new FileNotFoundException(
76
NLS.bind(LaunchMessages.AbstractCLaunchDelegate_PROGRAM_PATH_not_found,
77
exePath.toOSString())),
78
ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
80
exeFile = verifyBinary(cproject, exePath);
83
if (mode.equals(ILaunchManager.DEBUG_MODE)) {
84
ICDebugConfiguration debugConfig = getDebugConfig(config);
85
ICDISession dsession = null;
86
String debugMode = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE,
87
ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN);
88
if (debugMode.equals(ICDTLaunchConfigurationConstants.DEBUGGER_MODE_ATTACH)) {
89
// It may be that we have already been provided with a
91
if (config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_ATTACH_PROCESS_ID, -1) == -1) {
92
int pid = promptForProcessID(config);
94
cancel(LaunchMessages.LocalAttachLaunchDelegate_No_Process_ID_selected,
95
ICDTLaunchConfigurationConstants.ERR_NO_PROCESSID);
97
ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
98
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_ATTACH_PROCESS_ID, pid);
99
wc.launch(mode, new SubProgressMonitor(monitor, 9));
100
wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_ATTACH_PROCESS_ID, (String)null);
101
cancel("", -1); //$NON-NLS-1$\
103
dsession = debugConfig.createDebugger().createDebuggerSession(launch, exeFile,
104
new SubProgressMonitor(monitor, 8));
106
// set the default source locator if required
107
setDefaultSourceLocator(launch, config);
108
ICDITarget[] targets = dsession.getTargets();
109
for (int i = 0; i < targets.length; i++) {
110
CDIDebugModel.newDebugTarget(launch, cproject.getProject(), targets[i],
111
renderTargetLabel(debugConfig), null, exeFile, true, true, false);
113
} catch (CoreException e) {
115
dsession.terminate();
116
} catch (CDIException ex) {
129
protected int promptForProcessID(ILaunchConfiguration config) throws CoreException {
130
IStatus fPromptStatus = new Status(IStatus.INFO, "org.eclipse.debug.ui", 200, "", null); //$NON-NLS-1$//$NON-NLS-2$
131
IStatus processPrompt = new Status(IStatus.INFO, "org.eclipse.cdt.launch", 100, "", null); //$NON-NLS-1$//$NON-NLS-2$
132
// consult a status handler
133
IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler(fPromptStatus);
134
if (prompter != null) {
135
Object result = prompter.handleStatus(processPrompt, config);
136
if (result instanceof Integer) {
137
return ((Integer)result).intValue();
146
* @see org.eclipse.cdt.launch.AbstractCLaunchConfigurationDelegate#getPluginID()
148
protected String getPluginID() {
149
return LaunchUIPlugin.getUniqueIdentifier();