aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/launch/NdkGdbLaunchShortcut.java
blob: 22eb118a275b4917a289c3133907784ecf308dd2 (plain)
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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.ndk.internal.launch;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.launch.AndroidLaunchController;
import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
import com.android.ide.eclipse.ndk.internal.NdkHelper;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.ILaunchShortcut;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;

@SuppressWarnings("restriction") // for adt.internal classes
public class NdkGdbLaunchShortcut implements ILaunchShortcut {
    @Override
    public void launch(ISelection selection, String mode) {
        if (!(selection instanceof IStructuredSelection)) {
            return;
        }

        Object s = ((IStructuredSelection) selection).getFirstElement();
        if (!(s instanceof IAdaptable)) {
            return;
        }

        IResource r = (IResource) ((IAdaptable) s).getAdapter(IResource.class);
        if (r == null) {
            return;
        }

        IProject project = r.getProject();
        if (project == null) {
            return;
        }

        // verify that this is a non library Android project
        ProjectState state = Sdk.getProjectState(project);
        if (state == null || state.isLibrary()) {
            return;
        }

        // verify that this project has C/C++ nature
        if (!CoreModel.hasCCNature(project) && !CoreModel.hasCNature(project)) {
            AdtPlugin.printErrorToConsole(project,
                    String.format("Selected project (%s) does not have C/C++ nature. "
                            + "To add native support, right click on the project, "
                            + "Android Tools -> Add Native Support",
                            project.getName()));
            return;
        }

        debugProject(project, mode);
    }

    @Override
    public void launch(IEditorPart editor, String mode) {
    }

    private void debugProject(IProject project, String mode) {
        // obtain existing native debug config for project
        ILaunchConfiguration config = AndroidLaunchController.getLaunchConfig(project,
                NdkGdbLaunchDelegate.LAUNCH_TYPE_ID);
        if (config == null) {
            return;
        }

        // Set the ndk gdb specific launch attributes in the config (if necessary)
        if (!hasNdkAttributes(config)) {
            try {
                config = setNdkDefaults(config, project);
            } catch (CoreException e) {
                AdtPlugin.printErrorToConsole(project,
                        "Unable to create launch configuration for project.");
                return;
            }
        }

        // launch
        DebugUITools.launch(config, mode);
    }

    private boolean hasNdkAttributes(ILaunchConfiguration config) {
        try {
            // All NDK launch configurations have ATTR_REMOTE_TCP set to true
            boolean isRemote = config.getAttribute(IGDBLaunchConfigurationConstants.ATTR_REMOTE_TCP,
                    false);
            return isRemote;
        } catch (CoreException e) {
            return false;
        }
    }

    private ILaunchConfiguration setNdkDefaults(ILaunchConfiguration config, IProject project)
            throws CoreException {
        ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy();
        NdkHelper.setLaunchConfigDefaults(wc);
        wc.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, project.getName());
        return wc.doSave();
    }
}