Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4875747440aab2c698335e453d26cc47e5a55c37 (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
/*******************************************************************************
 * Copyright (c) 2014 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.internal.cdt.launch;

import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.IStatusHandler;
import org.eclipse.tcf.internal.debug.launch.TCFLaunchDelegate;
import org.eclipse.tcf.internal.debug.model.TCFLaunch;
import org.eclipse.tcf.internal.debug.ui.launch.ContextSelection;
import org.eclipse.tcf.util.TCFTask;

/**
 * TCF launch delegate for CDT based launch configuration types.
 * The launch configuration is converted on the fly to be compatible
 * with the TCFLaunch.
 */
public class TCFCDTLaunchDelegate extends TCFLaunchDelegate {

    @Override
    public ILaunch getLaunch(ILaunchConfiguration configuration, final String mode) throws CoreException {
        final ILaunchConfiguration tcfLaunchConfig = convertToTcfConfig(configuration);
        return super.getLaunch(tcfLaunchConfig, mode);
    }

    public void launch(ILaunchConfiguration configuration, final String mode,
            final ILaunch launch, final IProgressMonitor monitor) throws CoreException {

        ILaunchConfiguration tcfLaunchConfig = convertToTcfConfig(configuration);
        final String peerId;
        if (isAttachLaunch(tcfLaunchConfig)) {
            ContextSelection selection = promptForContext(tcfLaunchConfig);
            if (selection == null) {
                // canceled
                throw new CoreException(Status.OK_STATUS);
            }
            launch.setAttribute("attach_to_context", selection.fContextId);
            if (!selection.fIsAttached) {
                launch.setAttribute("attach_to_process", selection.fContextId);
            }
            peerId = selection.fPeerId;
        }
        else if (isRemoteLaunch(tcfLaunchConfig)) {
            peerId = tcfLaunchConfig.getAttribute(TCFLaunchDelegate.ATTR_PEER_ID, (String) null);
        }
        else {
            peerId = null;
        }
        if (peerId == null) {
            super.launch(tcfLaunchConfig, mode, launch, monitor);
        }
        else {
            new TCFTask<Boolean>() {
                public void run() {
                    ((TCFLaunch)launch).launchTCF(mode, peerId, this, monitor);
                }
            }.getE();
        }
    }

    private boolean isAttachLaunch(ILaunchConfiguration config) throws CoreException {
        String configTypeName = config.getType().getIdentifier();
        return ICDTLaunchConfigurationConstants.ID_LAUNCH_C_ATTACH.equals(configTypeName);
    }

    private boolean isRemoteLaunch(ILaunchConfiguration config) throws CoreException {
        String configTypeName = config.getType().getIdentifier();
        return "org.eclipse.tcf.cdt.launch.remoteApplicationLaunchType".equals(configTypeName);
    }

    private ContextSelection promptForContext(ILaunchConfiguration config) throws CoreException {
        IStatus promptStatus = new Status(IStatus.INFO, "org.eclipse.debug.ui", 200, "", null); //$NON-NLS-1$//$NON-NLS-2$
        IStatus contextPrompt = new Status(IStatus.INFO, "org.eclipse.tcf.cdt.core", 100, "", null); //$NON-NLS-1$//$NON-NLS-2$
        // consult a status handler
        IStatusHandler prompter = DebugPlugin.getDefault().getStatusHandler(promptStatus);
        if (prompter != null) {
            Object result = prompter.handleStatus(contextPrompt, config);
            if (result instanceof ContextSelection) {
                return (ContextSelection)result;
            }
        }
        return null;
    }

    protected ILaunchConfiguration convertToTcfConfig(ILaunchConfiguration orig) throws CoreException {
        ILaunchConfigurationWorkingCopy copy = orig.copy(orig.getName());
        boolean changed = copyStringAttribute(orig, copy, ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME,
                TCFLaunchDelegate.ATTR_PROJECT_NAME);
        if (isAttachLaunch(orig)) {
            changed = setStringAttribute(copy, TCFLaunchDelegate.ATTR_LOCAL_PROGRAM_FILE, null) || changed;
            changed = setStringAttribute(copy, TCFLaunchDelegate.ATTR_REMOTE_PROGRAM_FILE, null) || changed;
        }
        else {
            changed = copyStringAttribute(orig, copy, ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME,
                    TCFLaunchDelegate.ATTR_LOCAL_PROGRAM_FILE) || changed;
            changed = copyStringAttribute(orig, copy, ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
                    TCFLaunchDelegate.ATTR_PROGRAM_ARGUMENTS) || changed;
            changed = copyStringAttribute(orig, copy, ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
                    TCFLaunchDelegate.ATTR_WORKING_DIRECTORY) || changed;
            changed = copyBooleanAttribute(orig, copy, ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL,
                    TCFLaunchDelegate.ATTR_USE_TERMINAL) || changed;
        }
        return changed ? copy.doSave() : orig;
    }

    private boolean copyStringAttribute(ILaunchConfiguration orig, ILaunchConfigurationWorkingCopy copy, String origAttr,
            String newAttr) throws CoreException {
        String newValue = orig.getAttribute(newAttr, (String) null);
        return setStringAttribute(copy, newAttr, newValue);
    }

    private boolean setStringAttribute(ILaunchConfigurationWorkingCopy copy, String attr, String newValue) throws CoreException {
        String origValue = copy.getAttribute(attr, (String) null);
        if (origValue == newValue || origValue != null && origValue.equals(newValue)) {
            return false;
        }
        copy.setAttribute(attr, newValue);
        return true;
    }

    private boolean copyBooleanAttribute(ILaunchConfiguration orig, ILaunchConfigurationWorkingCopy copy, String origAttr,
            String newAttr) throws CoreException {
        boolean origValue = orig.getAttribute(origAttr, false);
        boolean newValue = orig.getAttribute(newAttr, false);
        if (origValue == newValue) {
            return false;
        }
        copy.setAttribute(newAttr, origValue);
        return true;
    }

}

Back to the top