Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdfc3deaee80da1158f5007613cf5ccf2d06d6d8 (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
/*******************************************************************************
 * Copyright (c) 2007, 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.debug.launch;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
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.Path;
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.model.LaunchConfigurationDelegate;
import org.eclipse.tcf.internal.debug.Activator;
import org.eclipse.tcf.internal.debug.model.ITCFConstants;
import org.eclipse.tcf.internal.debug.model.TCFLaunch;
import org.eclipse.tcf.internal.debug.model.TCFMemoryRegion;
import org.eclipse.tcf.protocol.JSON;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IMemoryMap;
import org.eclipse.tcf.services.IPathMap;
import org.eclipse.tcf.util.TCFPathMapRule;
import org.eclipse.tcf.util.TCFTask;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class TCFLaunchDelegate extends LaunchConfigurationDelegate {

    public static final String
        ATTR_PEER_ID = ITCFConstants.ID_TCF_DEBUG_MODEL + ".PeerID",
        ATTR_PROJECT_NAME = ITCFConstants.ID_TCF_DEBUG_MODEL + ".ProjectName",
        ATTR_LOCAL_PROGRAM_FILE = ITCFConstants.ID_TCF_DEBUG_MODEL + ".LocalProgramFile",
        ATTR_REMOTE_PROGRAM_FILE = ITCFConstants.ID_TCF_DEBUG_MODEL + ".ProgramFile",
        ATTR_COPY_TO_REMOTE_FILE = ITCFConstants.ID_TCF_DEBUG_MODEL + ".CopyToRemote",
        ATTR_PROGRAM_ARGUMENTS = ITCFConstants.ID_TCF_DEBUG_MODEL + ".ProgramArguments",
        ATTR_WORKING_DIRECTORY = ITCFConstants.ID_TCF_DEBUG_MODEL + ".WorkingDirectory",
        ATTR_ATTACH_CHILDREN = ITCFConstants.ID_TCF_DEBUG_MODEL + ".AttachChildren",
        ATTR_STOP_AT_ENTRY = ITCFConstants.ID_TCF_DEBUG_MODEL + ".StopAtEntry",
        ATTR_STOP_AT_MAIN = ITCFConstants.ID_TCF_DEBUG_MODEL + ".StopAtMain",
        ATTR_DISCONNECT_ON_CTX_EXIT = ITCFConstants.ID_TCF_DEBUG_MODEL + ".DisconnectOnCtxExit",
        ATTR_USE_TERMINAL = ITCFConstants.ID_TCF_DEBUG_MODEL + ".UseTerminal",
        ATTR_RUN_LOCAL_AGENT = ITCFConstants.ID_TCF_DEBUG_MODEL + ".RunLocalAgent",
        ATTR_USE_LOCAL_AGENT = ITCFConstants.ID_TCF_DEBUG_MODEL + ".UseLocalAgent",
        ATTR_SIGNALS_DONT_STOP = ITCFConstants.ID_TCF_DEBUG_MODEL + ".SignalsDontStop",
        ATTR_SIGNALS_DONT_PASS = ITCFConstants.ID_TCF_DEBUG_MODEL + ".SignalsDontPass",
        ATTR_FILES = ITCFConstants.ID_TCF_DEBUG_MODEL + ".Files",
        ATTR_PATH_MAP = ITCFConstants.ID_TCF_DEBUG_MODEL + ".PathMap",
        ATTR_MEMORY_MAP = ITCFConstants.ID_TCF_DEBUG_MODEL + ".MemoryMap",
        ATTR_ATTACH_PATH = ITCFConstants.ID_TCF_DEBUG_MODEL + ".Attach";

    public static final String
        FILES_CONTEXT_FULL_NAME = "Context",
        FILES_CONTEXT_ID = "ContextID",
        FILES_FILE_NAME = "File",
        FILES_LOAD_SYMBOLS = "LoadSymbols",
        FILES_RELOCATE = "Relocate",
        FILES_ADDRESS = IMemoryMap.PROP_ADDRESS,
        FILES_OFFSET = IMemoryMap.PROP_OFFSET,
        FILES_SIZE = IMemoryMap.PROP_SIZE,
        FILES_DOWNLOAD = "Download",
        FILES_SET_PC = "SetPC",
        FILES_ENABLE_OSA = "EnableOSA";

    public static class PathMapRule extends TCFPathMapRule {

        public PathMapRule(Map<String,Object> props) {
            super(props);
        }

        /* (non-Javadoc)
         * @see java.lang.Object#equals(java.lang.Object)
         */
        @Override
        public boolean equals(Object obj) {
            boolean equal = super.equals(obj);
            if (!equal && obj instanceof PathMapRule) {
                return this.toString().equals(((PathMapRule)obj).toString());
            }
            return equal;
        }

        @Override
        public String toString() {
            StringBuffer bf = new StringBuffer();
            Map<String,Object> props = getProperties();
            String[] keySet = props.keySet().toArray(new String[props.size()]);
            Arrays.sort(keySet);
            for (String nm : keySet) {
                Object o = props.get(nm);
                if (o != null) {
                    bf.append(nm);
                    bf.append('=');
                    String s = o.toString();
                    for (int i = 0; i < s.length(); i++) {
                        char ch = s.charAt(i);
                        if (ch >= ' ' && ch != '|' && ch != '\\') {
                            bf.append(ch);
                        }
                        else {
                            bf.append('\\');
                            bf.append((int)ch);
                            bf.append(';');
                        }
                    }
                    bf.append('|');
                }
            }
            bf.append('|');
            return bf.toString();
        }
    }

    /**
     * Given value of ATTR_PATH_MAP, return array of PathMapRule objects.
     * @param s - value of ATTR_PATH_MAP.
     * @return array of PathMapRule objects.
     */
    public static ArrayList<PathMapRule> parsePathMapAttribute(String s) {
        ArrayList<PathMapRule> map = new ArrayList<PathMapRule>();
        StringBuffer bf = new StringBuffer();
        int i = 0;
        while (i < s.length()) {
            // To guarantee a predictable path map properties iteration order,
            // we have to use a LinkedHashMap.
            PathMapRule e = new PathMapRule(new LinkedHashMap<String,Object>());
            while (i < s.length()) {
                char ch = s.charAt(i++);
                if (ch == '|') {
                    map.add(e);
                    break;
                }
                bf.setLength(0);
                bf.append(ch);
                while (i < s.length()) {
                    ch = s.charAt(i++);
                    if (ch == '=') break;
                    bf.append(ch);
                }
                String nm = bf.toString();
                bf.setLength(0);
                while (i < s.length()) {
                    ch = s.charAt(i++);
                    if (ch == '|') {
                        if (bf.length() > 0) e.getProperties().put(nm, bf.toString());
                        break;
                    }
                    else if (ch == '\\') {
                        int n = 0;
                        while (i < s.length()) {
                            char d = s.charAt(i++);
                            if (d == ';') break;
                            n = n * 10 + (d - '0');
                        }
                        bf.append((char)n);
                    }
                    else {
                        bf.append(ch);
                    }
                }
            }
        }
        return map;
    }

    /**
     * Given value of ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO,
     * return array of PathMapRule objects.
     * @param s - value of ATTR_PATH_MAP.
     * @return array of PathMapRule objects.
     */
    public static ArrayList<PathMapRule> parseSourceLocatorMemento(String s) throws CoreException {
        ArrayList<PathMapRule> map = new ArrayList<PathMapRule>();
        if (s == null || s.length() == 0) return map;
        Element root = DebugPlugin.parseDocument(s);
        NodeList list = root.getChildNodes();
        int length = list.getLength();
        for (int i = 0; i < length; i++) {
            Node node = list.item(i);
            short type = node.getNodeType();
            if (type == Node.ELEMENT_NODE) {
                Element entry = (Element)node;
                if (entry.getNodeName().equalsIgnoreCase("sourceContainers")) {
                    parseSourceContainers(map, entry);
                }
            }
        }
        return map;
    }

    private static void parseSourceContainers(ArrayList<PathMapRule> map, Element element) throws CoreException {
        NodeList list = element.getChildNodes();
        int length = list.getLength();
        for (int i = 0; i < length; i++) {
            Node node = list.item(i);
            short type = node.getNodeType();
            if (type == Node.ELEMENT_NODE) {
                Element entry = (Element)node;
                String memento = entry.getAttribute("memento");
                if (memento != null && memento.length() > 0) readSourceContainer(map, memento);
            }
        }
    }

    private static void readSourceContainer(ArrayList<PathMapRule> map, String s) throws CoreException {
        // The user may add source container which stores their memento not
        // as an XML string. For those containers, DebugPlugin.parseDocument(s)
        // will throw an CoreException and the debug launch fails. Such source
        // container should be ignored and the launch should continue.
        Element root = null;
        try {
            root = DebugPlugin.parseDocument(s);
        } catch (CoreException e) {
            // The memento does not represent a XML string
        }
        if (root == null) return;

        if ("mapping".equals(root.getNodeName())) {
            NodeList list = root.getChildNodes();
            int length = list.getLength();
            for (int i = 0; i < length; i++) {
                Node node = list.item(i);
                short type = node.getNodeType();
                if (type == Node.ELEMENT_NODE) {
                    Element entry = (Element)node;
                    if (entry.getNodeName().equalsIgnoreCase("mapEntry")) {
                        String memento = entry.getAttribute("memento");
                        if (memento != null && memento.length() > 0) {
                            Element map_entry = DebugPlugin.parseDocument(memento);
                            String src = map_entry.getAttribute("backendPath");
                            String dst = map_entry.getAttribute("localPath");
                            if (src != null) src = src.replace('\\', '/');
                            // To guarantee a predictable path map properties iteration order,
                            // we have to use a LinkedHashMap.
                            Map<String,Object> props = new LinkedHashMap<String,Object>();
                            props.put(IPathMap.PROP_SOURCE, src);
                            props.put(IPathMap.PROP_DESTINATION, dst);
                            map.add(new PathMapRule(props));
                        }
                    }
                }
            }
        }
    }

    /**
     * Given value of ATTR_MEMORY_MAP, add lists of TCFMemoryRegion objects into 'maps'.
     * @param maps - Map object to fill with memory maps.
     * @param s - value of ATTR_MEMORY_MAP.
     */
    @SuppressWarnings("unchecked")
    public static void parseMemMapsAttribute(Map<String,ArrayList<IMemoryMap.MemoryRegion>> maps, String s) throws Exception {
        if (s == null || s.length() == 0) return;
        Collection<Map<String,Object>> list = (Collection<Map<String,Object>>)JSON.parseOne(s.getBytes("UTF-8"));
        if (list == null) return;
        for (Map<String,Object> map : list) {
            String id = (String)map.get(IMemoryMap.PROP_ID);
            if (id != null) {
                ArrayList<IMemoryMap.MemoryRegion> l = maps.get(id);
                if (l == null) {
                    l = new ArrayList<IMemoryMap.MemoryRegion>();
                    maps.put(id, l);
                }
                l.add(new TCFMemoryRegion(map));
            }
        }
    }

    /**
     * Read ATTR_MEMORY_MAP attribute of a launch configuration.
     * @param maps - Map object to fill with memory maps.
     * @param cfg - the launch configuration.
     * @throws Exception
     */
    public static void getMemMapsAttribute(Map<String,ArrayList<IMemoryMap.MemoryRegion>> maps,
            ILaunchConfiguration cfg) throws Exception {
        String maps_cfg = cfg.getAttribute(ATTR_MEMORY_MAP, (String)null);
        parseMemMapsAttribute(maps, maps_cfg);
    }

    /**
     * Given project name and program name returns absolute path of the program.
     * @param project_name - workspace project name.
     * @param program_name - launch program name.
     * @return program path or null if both project name and program name are null.
     */
    public static String getProgramPath(String project_name, String program_name) {
        if (program_name == null || program_name.length() == 0) return null;
        if (project_name == null || project_name.length() == 0) {
            File file = new File(program_name);
            if (!file.isAbsolute()) {
                File ws = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
                file = new File(ws, program_name);
            }
            return file.getAbsolutePath();
        }
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(project_name);
        IPath program_path = new Path(program_name);
        if (!program_path.isAbsolute()) {
            if (project == null || !project.getFile(program_name).exists()) return null;
            program_path = project.getFile(program_name).getLocation();
        }
        return program_path.toOSString();
    }

    /**
     * Create new TCF launch object.
     * @return new TCFLaunch object
     */
    @Override
    public ILaunch getLaunch(final ILaunchConfiguration configuration, final String mode) throws CoreException {
        return new TCFTask<ILaunch>() {
            int cnt;
            public void run() {
                // Need to delay at least one dispatch cycle to work around
                // a possible racing between thread that calls getLaunch() and
                // the process of activation of other TCF plug-ins.
                if (cnt++ < 2) Protocol.invokeLater(this);
                else done(new TCFLaunch(configuration, mode));
            }
        }.getE();
    }

    /**
     * Launch TCF session.
     */
    public void launch(final ILaunchConfiguration configuration, final String mode,
            final ILaunch launch, final IProgressMonitor monitor) throws CoreException {
        String local_id = null;
        if (configuration.getAttribute(ATTR_RUN_LOCAL_AGENT, false)) {
            if (monitor != null) monitor.subTask("Starting TCF Agent"); //$NON-NLS-1$
            local_id = TCFLocalAgent.runLocalAgent();
        }
        else if (configuration.getAttribute(ATTR_USE_LOCAL_AGENT, true)) {
            if (monitor != null) monitor.subTask("Searching TCF Agent"); //$NON-NLS-1$
            local_id = TCFLocalAgent.getLocalAgentID();
            if (local_id == null) throw new CoreException(new Status(IStatus.ERROR,
                    Activator.PLUGIN_ID, 0,
                    "Cannot find TCF agent on the local host",
                    null));
        }

        final String id = configuration.getAttribute(ATTR_USE_LOCAL_AGENT, true) ?
                local_id : configuration.getAttribute(ATTR_PEER_ID, "");

        new TCFTask<Boolean>() {
            public void run() {
                ((TCFLaunch)launch).launchTCF(mode, id, this, monitor);
            }
        }.getE();
    }
}

Back to the top