Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0d3c7cd00cdf603f018eb29a0ac0adc48aaf0719 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*******************************************************************************
 * Copyright (c) 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.dsf.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.core.IAddress;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.dd.dsf.concurrent.DataRequestMonitor;
import org.eclipse.dd.dsf.concurrent.RequestMonitor;
import org.eclipse.dd.dsf.datamodel.AbstractDMContext;
import org.eclipse.dd.dsf.datamodel.IDMContext;
import org.eclipse.dd.dsf.service.AbstractDsfService;
import org.eclipse.dd.dsf.service.DsfSession;
import org.eclipse.dd.dsf.service.IDsfService;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.tm.internal.tcf.debug.model.ITCFBreakpointListener;
import org.eclipse.tm.internal.tcf.debug.model.ITCFConstants;
import org.eclipse.tm.internal.tcf.debug.model.TCFBreakpointsModel;
import org.eclipse.tm.internal.tcf.debug.model.TCFLaunch;
import org.eclipse.tm.internal.tcf.dsf.Activator;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IBreakpoints;
import org.eclipse.tm.tcf.util.TCFDataCache;
import org.osgi.framework.BundleContext;

public class TCFDSFBreakpoints extends AbstractDsfService implements org.eclipse.dd.dsf.debug.service.IBreakpoints {
    
    private class BreakpointDMC extends AbstractDMContext implements IBreakpointDMContext {
        
        final String id;
        final IBreakpoint bp;
        final TCFDataCache<Map<String,Object>> status;
        final Set<IBreakpointsTargetDMContext> targets;
        
        boolean disposed;

        public BreakpointDMC(IDsfService service, final String id, IBreakpoint bp) {
            super(service, new IDMContext[0]);
            this.id = id;
            this.bp = bp;
            cache.put(id, this);
            status = new TCFDataCache<Map<String,Object>>(channel) {
                @Override
                public boolean startDataRetrieval() {
                    assert command == null;
                    assert !disposed;
                    if (tcf_bpt_service == null) {
                        reset(null);
                        return true;
                    }
                    command = tcf_bpt_service.getStatus(id, new org.eclipse.tm.tcf.services.IBreakpoints.DoneGetStatus() {
                        public void doneGetStatus(IToken token, Exception err, Map<String,Object> status) {
                            set(token, err, status);
                        }
                    });
                    return false;
                }
            };
            targets = new HashSet<IBreakpointsTargetDMContext>();
        }

        @Override
        public boolean equals(Object other) {
            return super.baseEquals(other) && ((BreakpointDMC)other).id.equals(id);
        }

        @Override
        public int hashCode() {
            return id.hashCode();
        }
        
        void dispose() {
            assert !disposed;
            cache.remove(id);
            for (IBreakpointsTargetDMContext t : targets.toArray(
                    new IBreakpointsTargetDMContext[targets.size()])) onRemoved(t);
            assert targets.isEmpty();
            disposed = true;
        }
        
        void onAdded(final IBreakpointsTargetDMContext t) {
            targets.add(t);
            IBreakpointsAddedEvent e = new IBreakpointsAddedEvent() {
                public IBreakpointsTargetDMContext getDMContext() {
                    return t;
                }
                public IBreakpointDMContext[] getBreakpoints() {
                    return new IBreakpointDMContext[]{ BreakpointDMC.this };
                }
            };
            getSession().dispatchEvent(e, getProperties());
        }
        
        void onUpdated(final IBreakpointsTargetDMContext t) {
            assert targets.contains(t);
            IBreakpointsUpdatedEvent e = new IBreakpointsUpdatedEvent() {
                public IBreakpointsTargetDMContext getDMContext() {
                    return t;
                }
                public IBreakpointDMContext[] getBreakpoints() {
                    return new IBreakpointDMContext[]{ BreakpointDMC.this };
                }
            };
            getSession().dispatchEvent(e, getProperties());
        }
        
        void onRemoved(final IBreakpointsTargetDMContext t) {
            targets.remove(t);
            IBreakpointsRemovedEvent e = new IBreakpointsRemovedEvent() {
                public IBreakpointsTargetDMContext getDMContext() {
                    return t;
                }
                public IBreakpointDMContext[] getBreakpoints() {
                    return new IBreakpointDMContext[]{ BreakpointDMC.this };
                }
            };
            getSession().dispatchEvent(e, getProperties());
        }
    }
    
    private class BreakpointData implements IBreakpointDMData {
        
        final IBreakpoint bp;
        final Map<String,Object> attrs;
        final Map<String,Object> status;
        final String file;
        
        @SuppressWarnings("unchecked")
        BreakpointData(IBreakpoint bp, Map<String,Object> status) throws CoreException, IOException {
            this.bp = bp;
            this.status = status;
            attrs = bp.getMarker().getAttributes();
            IResource resource = bp.getMarker().getResource();
            if (resource == ResourcesPlugin.getWorkspace().getRoot()) {
                file = null;
            }
            else {
                IPath p = resource.getRawLocation();
                if (p == null) file = null;
                else file = p.toFile().getCanonicalPath();
            }
        }

        public IBreakpoint getPlatformBreakpoint() {
            return bp;
        }

        public Map<String,Object> getStatus() {
            return status;
        }

        @SuppressWarnings("unchecked")
        public IAddress[] getAddresses() {
            if (status == null) return null;
            Map<String,Collection<Number>> arr = (Map<String,Collection<Number>>)status.get(IBreakpoints.STATUS_PLANTED);
            if (arr == null) return null;
            int cnt = 0;
            for (Collection<Number> c : arr.values()) cnt += c.size(); 
            IAddress[] res = new IAddress[cnt];
            int pos = 0;
            for (Collection<Number> c : arr.values()) {
                for (Number addr : c) res[pos++] = new TCFAddress(addr);
            }
            return res;
        }

        public String getBreakpointType() {
            // TODO Auto-generated method stub
            return null;
        }

        public String getCondition() {
            return (String)attrs.get(ITCFConstants.ID_TCF_DEBUG_MODEL + '.' + IBreakpoints.PROP_CONDITION);
        }

        public String getExpression() {
            // TODO Auto-generated method stub
            return null;
        }

        public String getFileName() {
            return file;
        }

        public String getFunctionName() {
            // TODO Auto-generated method stub
            return null;
        }

        public int getIgnoreCount() {
            Integer count = (Integer)attrs.get(ITCFConstants.ID_TCF_DEBUG_MODEL + '.' + IBreakpoints.PROP_SKIP_COUNT);
            if (count != null) return count.intValue();
            return 0;
        }

        public int getLineNumber() {
            Integer line = (Integer)attrs.get(IMarker.LINE_NUMBER);
            if (line != null) return line.intValue();
            return 0;
        }

        public boolean isEnabled() {
            Boolean enabled = (Boolean)attrs.get(IBreakpoint.ENABLED);
            return enabled != null && enabled.booleanValue() && bp_manager.isEnabled();
        }
    }
    
    private final ITCFBreakpointListener bp_listener = new ITCFBreakpointListener() {

        @SuppressWarnings("unchecked")
        public void breakpointStatusChanged(String id) {
            final BreakpointDMC dmc = cache.get(id);
            if (dmc != null) {
                TCFDSFRunControl rc = getServicesTracker().getService(TCFDSFRunControl.class);
                Map<String,Object> map = launch.getBreakpointsStatus().getStatus(dmc.id);
                dmc.status.reset(map);
                Set<IBreakpointsTargetDMContext> add_targets = new HashSet<IBreakpointsTargetDMContext>();
                Set<IBreakpointsTargetDMContext> rem_targets = new HashSet<IBreakpointsTargetDMContext>();
                if (map != null) {
                    Map<String,Collection<Number>> arr = (Map<String,Collection<Number>>)map.get(IBreakpoints.STATUS_PLANTED);
                    if (arr != null) {
                        for (String ctx_id : arr.keySet()) add_targets.add(rc.getContext(ctx_id));
                    }
                }
                for (IBreakpointsTargetDMContext t : dmc.targets) {
                    if (add_targets.contains(t)) {
                        dmc.onUpdated(t);
                        add_targets.remove(t);
                    }
                    else {
                        rem_targets.add(t);
                    }
                }
                for (IBreakpointsTargetDMContext t : rem_targets) dmc.onRemoved(t);
                for (IBreakpointsTargetDMContext t : add_targets) dmc.onAdded(t);
            }
        }

        public void breakpointRemoved(String id) {
            final BreakpointDMC dmc = cache.get(id);
            if (dmc != null) dmc.dispose();
        }
    };
    
    private final TCFLaunch launch;
    private final IChannel channel;
    private final org.eclipse.tm.tcf.services.IBreakpoints tcf_bpt_service;
    private final Map<String,BreakpointDMC> cache = new HashMap<String,BreakpointDMC>();
    private final IBreakpointManager bp_manager = DebugPlugin.getDefault().getBreakpointManager();
    
    public TCFDSFBreakpoints(DsfSession session, TCFLaunch launch, final RequestMonitor monitor) {
        super(session);
        this.launch = launch; 
        channel = launch.getChannel();
        launch.getBreakpointsStatus().addListener(bp_listener);
        tcf_bpt_service = channel.getRemoteService(org.eclipse.tm.tcf.services.IBreakpoints.class);
        initialize(new RequestMonitor(getExecutor(), monitor) { 
            @Override
            protected void handleSuccess() {
                String[] class_names = {
                        org.eclipse.dd.dsf.debug.service.IBreakpoints.class.getName(),
                        TCFDSFBreakpoints.class.getName()
                };
                register(class_names, new Hashtable<String,String>());
                monitor.done();
            }
        });
    }

    @Override 
    public void shutdown(RequestMonitor monitor) {
        unregister();
        super.shutdown(monitor);
    }

    @Override
    protected BundleContext getBundleContext() {
        return Activator.getBundleContext();
    }

    public void getBreakpoints(IBreakpointsTargetDMContext ctx, DataRequestMonitor<IBreakpointDMContext[]> rm) {
        TCFBreakpointsModel m = TCFBreakpointsModel.getBreakpointsModel();
        IBreakpoint[] arr = bp_manager.getBreakpoints(ITCFConstants.ID_TCF_DEBUG_MODEL);
        ArrayList<IBreakpointDMContext> l = new ArrayList<IBreakpointDMContext>();
        if (arr != null && arr.length == 0) {
            for (IBreakpoint bp : arr) {
                if (m.isSupported(channel, bp)) {
                    IMarker marker = bp.getMarker();
                    String id = marker.getAttribute(ITCFConstants.ID_TCF_DEBUG_MODEL +
                            '.' + org.eclipse.tm.tcf.services.IBreakpoints.PROP_ID, (String)null);
                    if (id != null) {
                        BreakpointDMC c = cache.get(id);
                        if (c == null) c = new BreakpointDMC(this, id, bp);
                        l.add(c);
                    }
                }
            }
        }
        rm.setData(l.toArray(new IBreakpointDMContext[l.size()]));
        rm.done();
    }

    public void getBreakpointDMData(final IBreakpointDMContext dmc, final DataRequestMonitor<IBreakpointDMData> rm) {
        if (dmc instanceof BreakpointDMC) {
            BreakpointDMC bp = (BreakpointDMC)dmc;
            if (!bp.status.validate()) {
                bp.status.wait(new Runnable() {
                    public void run() {
                        getBreakpointDMData(dmc, rm);
                    }
                });
                return;
            }
            if (bp.status.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", bp.status.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            try {
                rm.setData(new BreakpointData(bp.bp, bp.status.getData()));
            }
            catch (Exception x) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", x)); //$NON-NLS-1$
                
            }
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null));  //$NON-NLS-1$
        }
        rm.done();
    }

    @SuppressWarnings("unchecked")
    public void getModelData(IDMContext dmc, DataRequestMonitor<?> rm) {
        if (dmc instanceof BreakpointDMC) {
            getBreakpointDMData((BreakpointDMC)dmc, (DataRequestMonitor<IBreakpointDMData>)rm);
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null));  //$NON-NLS-1$
            rm.done();
        }
    }

    public void insertBreakpoint(IBreakpointsTargetDMContext context, Map<String,Object> attributes,
            DataRequestMonitor<IBreakpointDMContext> rm) {
        // Clients are not allowed to call this method.
        // Use IBreakpointManager instead.
        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                REQUEST_FAILED, "Not allowed", new Error())); //$NON-NLS-1$
        rm.done();
    }

    public void removeBreakpoint(IBreakpointDMContext dmc, RequestMonitor rm) {
        // Clients are not allowed to call this method.
        // Use IBreakpointManager instead.
        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                REQUEST_FAILED, "Not allowed", new Error())); //$NON-NLS-1$
        rm.done();
    }

    public void updateBreakpoint(IBreakpointDMContext dmc, Map<String,Object> delta, RequestMonitor rm) {
        // Clients are not allowed to call this method.
        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                REQUEST_FAILED, "Not allowed", new Error())); //$NON-NLS-1$
        rm.done();
    }
}

Back to the top