Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 370e4b500fb8c1e073cb7970a60f60bc2343c4d5 (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
372
373
374
375
376
377
378
379
380
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.tm.internal.tcf.debug.model;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IMarkerDelta;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointListener;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.IBreakpointManagerListener;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.tm.internal.tcf.debug.Activator;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.IBreakpoints;


/**
 * TCFBreakpointsModel class handles breakpoints for all active TCF launches.
 * It downloads initial set of breakpoint data when launch is activated,
 * listens for Eclipse breakpoint manager events and propagates breakpoint changes to TCF targets.
 */
public class TCFBreakpointsModel implements IBreakpointListener, IBreakpointManagerListener {

    private final IBreakpointManager bp_manager = DebugPlugin.getDefault().getBreakpointManager();
    private final HashSet<IChannel> channels = new HashSet<IChannel>();

    public static TCFBreakpointsModel getBreakpointsModel() {
        return Activator.getBreakpointsModel();
    }

    public void dispose() {
        bp_manager.removeBreakpointListener(this);
        bp_manager.removeBreakpointManagerListener(this);
    }

    public boolean isSupported(IChannel channel, IBreakpoint bp) {
        // TODO: implement per-channel breakpoint filtering
        return true;
    }

    public String getBreakpointID(IBreakpoint bp) throws CoreException {
        IMarker marker = bp.getMarker();
        String id = (String)marker.getAttributes().get(ITCFConstants.ID_TCF_DEBUG_MODEL + '.' + IBreakpoints.PROP_ID);
        if (id != null) return id;
        id = marker.getResource().getLocationURI().toString();
        if (id == null) return null;
        return id + ':' + marker.getId();
    }

    @SuppressWarnings("unchecked")
    public void downloadBreakpoints(final IChannel channel, final Runnable done) throws IOException, CoreException {
        assert Protocol.isDispatchThread();
        IBreakpoints service = channel.getRemoteService(IBreakpoints.class);
        if (service != null) {
            if (channels.isEmpty()) {
                bp_manager.addBreakpointListener(this);
                bp_manager.addBreakpointManagerListener(this);
            }
            channels.add(channel);
            channel.addChannelListener(new IChannel.IChannelListener() {
                public void congestionLevel(int level) {
                }
                public void onChannelClosed(Throwable error) {
                    channels.remove(channel);
                    if (channels.isEmpty()) {
                        bp_manager.removeBreakpointListener(TCFBreakpointsModel.this);
                        bp_manager.removeBreakpointManagerListener(TCFBreakpointsModel.this);
                    }
                }
                public void onChannelOpened() {
                }
            });
            IBreakpoint[] arr = bp_manager.getBreakpoints();
            if (arr != null && arr.length > 0) {
                Map<String,Object>[] bps = new Map[arr.length];
                for (int i = 0; i < arr.length; i++) {
                    if (!isSupported(channel, arr[i])) continue;
                    String id = getBreakpointID(arr[i]);
                    if (id == null) continue;
                    IMarker marker = arr[i].getMarker();
                    String file = getFilePath(marker.getResource());
                    bps[i] = toBreakpointAttributes(id, file, marker.getAttributes());
                }
                service.set(bps, new IBreakpoints.DoneCommand() {
                    public void doneCommand(IToken token, Exception error) {
                        if (error == null) done.run();
                        else channel.terminate(error);
                    }
                });
                return;
            }
        }
        Protocol.invokeLater(done);
    }

    public void breakpointManagerEnablementChanged(final boolean enabled) {
        try {
            IBreakpoint[] arr = bp_manager.getBreakpoints();
            if (arr == null || arr.length == 0) return;
            final Map<String,IBreakpoint> map = new HashMap<String,IBreakpoint>();
            for (int i = 0; i < arr.length; i++) {
                IMarker marker = arr[i].getMarker();
                Boolean b = marker.getAttribute(IBreakpoint.ENABLED, Boolean.FALSE);
                if (!b.booleanValue()) continue;
                String id = getBreakpointID(arr[i]);
                if (id == null) continue;
                map.put(id, arr[i]);
            }
            if (map.isEmpty()) return;
            Runnable r = new Runnable() {
                public void run() {
                    for (final IChannel channel : channels) {
                        IBreakpoints service = channel.getRemoteService(IBreakpoints.class);
                        Set<String> ids = new HashSet<String>();
                        for (String id : map.keySet()) {
                            IBreakpoint bp = map.get(id);
                            if (isSupported(channel, bp)) ids.add(id);
                        }
                        IBreakpoints.DoneCommand done = new IBreakpoints.DoneCommand() {
                            public void doneCommand(IToken token, Exception error) {
                                if (error != null) channel.terminate(error);
                            }
                        };
                        if (enabled) {
                            service.enable(ids.toArray(new String[ids.size()]), done);
                        }
                        else {
                            service.disable(ids.toArray(new String[ids.size()]), done);
                        }
                    }
                    Protocol.sync(new Runnable() {
                        public void run() {
                            synchronized (map) {
                                map.notify();
                            }
                        }
                    });
                }
            };
            synchronized (map) {
                assert !Protocol.isDispatchThread();
                Protocol.invokeLater(r);
                map.wait();
            }
        }
        catch (Throwable x) {
            Activator.log("Unhandled exception in breakpoint listener", x);
        }
    }

    private abstract class BreakpointUpdate implements Runnable {

        private final IBreakpoint breakpoint;
        private final Map<String,Object> marker_attrs;
        private final String marker_file;
        private final String marker_id;

        IBreakpoints service;
        IBreakpoints.DoneCommand done;
        Map<String,Object> tcf_attrs;

        @SuppressWarnings("unchecked")
        BreakpointUpdate(IBreakpoint breakpoint) throws CoreException, IOException {
            this.breakpoint = breakpoint;
            marker_attrs = new HashMap<String,Object>(breakpoint.getMarker().getAttributes());
            marker_file = getFilePath(breakpoint.getMarker().getResource());
            marker_id = getBreakpointID(breakpoint);
        }

        synchronized void exec() throws InterruptedException {
            assert !Protocol.isDispatchThread();
            if (marker_id != null) {
                Protocol.invokeLater(this);
                wait();
            }
        }

        public void run() {
            tcf_attrs = toBreakpointAttributes(marker_id, marker_file, marker_attrs);
            for (final IChannel channel : channels) {
                service = channel.getRemoteService(IBreakpoints.class);
                if (!isSupported(channel, breakpoint)) continue;
                done = new IBreakpoints.DoneCommand() {
                    public void doneCommand(IToken token, Exception error) {
                        if (error != null) channel.terminate(error);
                    }
                };
                update();
            }
            Protocol.sync(new Runnable() {
                public void run() {
                    synchronized (BreakpointUpdate.this) {
                        BreakpointUpdate.this.notify();
                    }
                }
            });
        };

        abstract void update();
    }

    private String getFilePath(IResource resource) throws IOException {
        if (resource == ResourcesPlugin.getWorkspace().getRoot()) return null;
        IPath p = resource.getRawLocation();
        if (p == null) return null;
        return p.toFile().getCanonicalPath();
    }

    public void breakpointAdded(IBreakpoint breakpoint) {
        try {
            new BreakpointUpdate(breakpoint) {
                @Override
                void update() {
                    service.add(tcf_attrs, done);
                }
            }.exec();
        }
        catch (Throwable x) {
            Activator.log("Unhandled exception in breakpoint listener", x);
        }
    }

    @SuppressWarnings("unchecked")
    private Set<String> calcMarkerDeltaKeys(IMarker marker, IMarkerDelta delta) throws CoreException {
        Set<String> keys = new HashSet<String>();
        if (delta == null) return keys;
        assert delta.getKind() == IResourceDelta.CHANGED;
        Map<String,Object> m0 = delta.getAttributes();
        Map<String,Object> m1 = marker.getAttributes();
        if (m0 != null) keys.addAll(m0.keySet());
        if (m1 != null) keys.addAll(m1.keySet());
        for (Iterator<String> i = keys.iterator(); i.hasNext();) {
            String key = i.next();
            Object v0 = m0 != null ? m0.get(key) : null;
            Object v1 = m1 != null ? m1.get(key) : null;
            if (v0 instanceof String && ((String)v0).length() == 0) v0 = null;
            if (v1 instanceof String && ((String)v1).length() == 0) v1 = null;
            if (v0 instanceof Boolean && !((Boolean)v0).booleanValue()) v0 = null;
            if (v1 instanceof Boolean && !((Boolean)v1).booleanValue()) v1 = null;
            if ((v0 == null) != (v1 == null)) continue;
            if (v0 != null && !v0.equals(v1)) continue;
            i.remove();
        }
        return keys;
    }

    public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
        try {
            final Set<String> s = calcMarkerDeltaKeys(breakpoint.getMarker(), delta);
            if (s.isEmpty()) return;
            new BreakpointUpdate(breakpoint) {
                @Override
                void update() {
                    if (s.size() == 1 && s.contains(IBreakpoint.ENABLED)) {
                        Boolean enabled = (Boolean)tcf_attrs.get(IBreakpoints.PROP_ENABLED);
                        if (enabled == null || !enabled.booleanValue()) {
                            service.disable(new String[]{ (String)tcf_attrs.get(IBreakpoints.PROP_ID) }, done);
                        }
                        else {
                            service.enable(new String[]{ (String)tcf_attrs.get(IBreakpoints.PROP_ID) }, done);
                        }
                    }
                    else {
                        service.change(tcf_attrs, done);
                    }
                }
            }.exec();
        }
        catch (Throwable x) {
            Activator.log("Unhandled exception in breakpoint listener", x);
        }
    }

    public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
        try {
            new BreakpointUpdate(breakpoint) {
                @Override
                void update() {
                    service.remove(new String[]{ (String)tcf_attrs.get(IBreakpoints.PROP_ID) }, done);
                }
            }.exec();
        }
        catch (Throwable x) {
            Activator.log("Unhandled exception in breakpoint listener", x);
        }
    }

    public Map<String,Object> toMarkerAttributes(Map<String,Object> p) {
        assert Protocol.isDispatchThread();
        Map<String,Object> m = new HashMap<String,Object>();
        for (Iterator<Map.Entry<String,Object>> i = p.entrySet().iterator(); i.hasNext();) {
            Map.Entry<String,Object> e = i.next();
            String key = e.getKey();
            Object val = e.getValue();
            if (key.equals(IBreakpoints.PROP_ENABLED)) continue;
            if (key.equals(IBreakpoints.PROP_FILE)) continue;
            if (key.equals(IBreakpoints.PROP_LINE)) continue;
            if (key.equals(IBreakpoints.PROP_COLUMN)) continue;
            m.put(ITCFConstants.ID_TCF_DEBUG_MODEL + '.' + key, val);
        }
        Boolean enabled = (Boolean)p.get(IBreakpoints.PROP_ENABLED);
        if (enabled == null) m.put(IBreakpoint.ENABLED, Boolean.FALSE);
        else m.put(IBreakpoint.ENABLED, enabled);
        m.put(IBreakpoint.REGISTERED, Boolean.TRUE);
        m.put(IBreakpoint.PERSISTED, Boolean.TRUE);
        m.put(IBreakpoint.ID, ITCFConstants.ID_TCF_DEBUG_MODEL);
        String msg = "";
        if (p.get(IBreakpoints.PROP_LOCATION) != null) msg += p.get(IBreakpoints.PROP_LOCATION);
        m.put(IMarker.MESSAGE, "Breakpoint: " + msg);
        Number line = (Number)p.get(IBreakpoints.PROP_LINE);
        if (line != null) {
            m.put(IMarker.LINE_NUMBER, new Integer(line.intValue()));
            Number column = (Number)p.get(IBreakpoints.PROP_COLUMN);
            if (column != null) {
                m.put(IMarker.CHAR_START, new Integer(column.intValue()));
                m.put(IMarker.CHAR_END, new Integer(column.intValue() + 1));
            }
        }
        return m;
    }

    public Map<String,Object> toBreakpointAttributes(String id, String file, Map<String,Object> p) {
        assert Protocol.isDispatchThread();
        Map<String,Object> m = new HashMap<String,Object>();
        m.put(IBreakpoints.PROP_ID, id);
        for (Iterator<Map.Entry<String,Object>> i = p.entrySet().iterator(); i.hasNext();) {
            Map.Entry<String,Object> e = i.next();
            String key = e.getKey();
            Object val = e.getValue();
            if (!key.startsWith(ITCFConstants.ID_TCF_DEBUG_MODEL)) continue;
            m.put(key.substring(ITCFConstants.ID_TCF_DEBUG_MODEL.length() + 1), val);
        }
        Boolean enabled = (Boolean)p.get(IBreakpoint.ENABLED);
        if (enabled != null && enabled.booleanValue() && bp_manager.isEnabled()) {
            m.put(IBreakpoints.PROP_ENABLED, enabled);
        }
        if (file != null) {
            // Map file path to remote file system
            int i = file.lastIndexOf('/');
            int j = file.lastIndexOf('\\');
            String name = file;
            if (i > j) name = file.substring(i + 1);
            else if (i < j) name = file.substring(j + 1);
            m.put(IBreakpoints.PROP_FILE, name);
            Integer line = (Integer)p.get(IMarker.LINE_NUMBER);
            if (line != null) {
                m.put(IBreakpoints.PROP_LINE, new Integer(line.intValue()));
                Integer column = (Integer)p.get(IMarker.CHAR_START);
                if (column != null) m.put(IBreakpoints.PROP_COLUMN, column);
            }
        }
        String condition  = (String)p.get("org.eclipse.cdt.debug.core.condition");
        if (condition != null && condition.length() > 0) m.put(IBreakpoints.PROP_CONDITION, condition);
        Integer skip_count = (Integer)p.get("org.eclipse.cdt.debug.core.ignoreCount");
        if (skip_count != null && skip_count.intValue() > 0) m.put(IBreakpoints.PROP_IGNORECOUNT, skip_count);
        return m;
    }
}

Back to the top