Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 58c06bab085c2e254fe0abc444016c10f2ddeaa4 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*******************************************************************************
 * 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.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.cdt.core.IAddress;
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.debug.service.IRunControl;
import org.eclipse.dd.dsf.debug.service.IStack;
import org.eclipse.dd.dsf.debug.service.IRunControl.StateChangeReason;
import org.eclipse.dd.dsf.service.AbstractDsfService;
import org.eclipse.dd.dsf.service.DsfServiceEventHandler;
import org.eclipse.dd.dsf.service.DsfSession;
import org.eclipse.tm.internal.tcf.debug.model.TCFSourceRef;
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.ILineNumbers;
import org.eclipse.tm.tcf.services.IStackTrace;
import org.eclipse.tm.tcf.services.ILineNumbers.CodeArea;
import org.eclipse.tm.tcf.util.TCFDataCache;
import org.osgi.framework.BundleContext;


public class TCFDSFStack extends AbstractDsfService implements IStack {
    
    public class TCFFrameDMC extends AbstractDMContext implements IFrameDMContext, Comparable<TCFFrameDMC> {
        
        public final String id;
        public final TCFDSFExecutionDMC exe_dmc;
        public final TCFDataCache<IStackTrace.StackTraceContext> context_cache;
        public final TCFDataCache<TCFSourceRef> source_cache;
        
        int level;
        TCFSourceRef prev_data;
        
        public TCFFrameDMC(final TCFDSFExecutionDMC exe_dmc, final String id) {
            super(TCFDSFStack.this.getSession().getId(), new IDMContext[] { exe_dmc });
            this.id = id;
            this.exe_dmc = exe_dmc;
            context_cache = new TCFDataCache<IStackTrace.StackTraceContext>(channel) {
                
                @Override
                public boolean startDataRetrieval() {
                    assert command == null;
                    if (id == null || tcf_stk_service == null) {
                        reset(null);
                        return true;
                    }
                    command = tcf_stk_service.getContext(new String[]{ id }, new IStackTrace.DoneGetContext() {
                        public void doneGetContext(IToken token, Exception err, IStackTrace.StackTraceContext[] context) {
                            if (command != token) return;
                            IStackTrace.StackTraceContext ctx = null;
                            if (context != null && context.length > 0) ctx = context[0];
                            set(token, err, ctx);
                        }
                    });
                    return false;
                }
            };
            
            source_cache = new TCFDataCache<TCFSourceRef>(channel) {

                @Override
                protected boolean startDataRetrieval() {
                    if (!context_cache.validate()) {
                        context_cache.wait(this);
                        return false;
                    }
                    IStackTrace.StackTraceContext ctx = context_cache.getData();
                    Number n = ctx.getInstructionAddress();
                    BigInteger a = null;
                    if (n != null) a = new BigInteger(n.toString());
                    // Optimization: skip source position lookup if same address
                    TCFSourceRef data = null;
                    if (prev_data != null && prev_data.address != null && prev_data.address.equals(a)) {
                        data = prev_data;
                    }
                    else {
                        data = new TCFSourceRef();
                        data.address = a;
                        if (!getSourcePos(data)) return false;
                    }
                    set(null, null, prev_data = data);
                    return true;
                }
                
                private boolean getSourcePos(final TCFSourceRef data) {
                    if (tcf_lns_service == null) return true;
                    if (data.address == null) return true;
                    BigInteger a1 = data.address;
                    BigInteger a2 = data.address.add(BigInteger.valueOf(1));
                    command = tcf_lns_service.mapToSource(exe_dmc.getTcfContextId(), a1, a2, new ILineNumbers.DoneMapToSource() {
                        public void doneMapToSource(IToken token, Exception err, CodeArea[] areas) {
                            if (command != token) return;
                            if (areas != null && areas.length > 0) {
                                for (ILineNumbers.CodeArea area : areas) {
                                    if (data.area == null || area.start_line < data.area.start_line) {
                                        data.area = area;
                                    }
                                }
                            }
                            set(token, err, prev_data = data);
                        }
                    });
                    return false;
                }
            };
        }
        
        public int getLevel() {
            return level;
        }
        
        @Override
        public boolean equals(Object other) {
            return super.baseEquals(other) && ((TCFFrameDMC)other).id.equals(id);
        }
        
        @Override
        public int hashCode() {
            return id.hashCode();
        }
        
        @Override
        public String toString() { 
            return baseToString() + ".frame[" + id + "]";  //$NON-NLS-1$ //$NON-NLS-2$
        }

        public int compareTo(TCFFrameDMC f) {
            if (level < f.level) return -1;
            if (level > f.level) return +1;
            return 0;
        }    
    }
    
    public static class TCFFrameData implements IFrameDMData {
        
        public final IStackTrace.StackTraceContext context;
        public final IAddress address;
        public final int level;
        public final String function;
        public final ILineNumbers.CodeArea code_area;
        
        TCFFrameData(TCFFrameDMC dmc) {
            context = dmc.context_cache.getData();
            TCFSourceRef ref = dmc.source_cache.getData();
            address = ref.address != null ? new TCFAddress(ref.address) : null;
            level = dmc.getLevel();
            function = null;
            code_area = ref.area;
        }

        public IAddress getAddress() {
            return address;
        }
        
        public String getFunction() {
            return function;
        }

        public int getLevel() {
            return level;
        }

        public String getFile() {
            if (code_area == null) return null;
            return code_area.file;
        }

        public int getLine() {
            if (code_area == null) return -1;
            return code_area.start_line;
        }

        public int getColumn() {
            if (code_area == null) return -1;
            return code_area.start_column;
        }
    }
    
    private class FramesCache extends TCFDataCache<Map<String,TCFFrameDMC>> {
        
        private final TCFDSFExecutionDMC dmc;
        private final Map<String,TCFFrameDMC> frame_pool;
        
        private String top_frame_id;
        
        FramesCache(IChannel channel, TCFDSFExecutionDMC dmc) {
            super(channel);
            this.dmc = dmc;
            frame_pool = new HashMap<String,TCFFrameDMC>();
        }
        
        @Override
        public boolean startDataRetrieval() {
            assert command == null;
            if (!dmc.run_control_context_cache.validate()) {
                dmc.run_control_context_cache.wait(this);
                return false;
            }
            if (dmc.run_control_context_cache.getError() != null) {
                set(null, dmc.run_control_context_cache.getError(), null);
                return true;
            }
            org.eclipse.tm.tcf.services.IRunControl.RunControlContext ctx = dmc.run_control_context_cache.getData();
            if (ctx == null || !ctx.hasState()) {
                set(null, new Exception("DMC does not have a stack"), null); //$NON-NLS-1$
                return true;
            }
            if (tcf_stk_service == null) {
                HashMap<String,TCFFrameDMC> data = new HashMap<String,TCFFrameDMC>();
                top_frame_id = "TopFrame:" + dmc.getTcfContextId();
                data.put(top_frame_id, createFrameDMC(top_frame_id, 0));
                set(null, null, data);
                return true;
            }
            assert !dmc.isDisposed();
            command = tcf_stk_service.getChildren(dmc.getTcfContextId(), new IStackTrace.DoneGetChildren() {
                public void doneGetChildren(IToken token, Exception err, String[] contexts) {
                    if (command != token) return;
                    HashMap<String,TCFFrameDMC> data = new HashMap<String,TCFFrameDMC>();
                    if (contexts != null) {
                        for (int i = 0; i < contexts.length; i++) {
                            String id = contexts[i];
                            data.put(id, createFrameDMC(id, contexts.length - i - 1));
                        }
                    }
                    set(token, err, data);
                }
            });
            return false;
        }
        
        TCFFrameDMC createFrameDMC(String id, int level) {
            TCFFrameDMC n = frame_pool.get(id);
            if (n == null) frame_pool.put(id, n = new TCFFrameDMC(dmc, id));
            n.level = level;
            if (n.level == 0) top_frame_id = id;
            return n;
        }

        void invalidateFrames() {
            reset();
            for (TCFFrameDMC dmc : frame_pool.values()) {
                dmc.context_cache.reset();
                dmc.source_cache.reset();
            }
        }
        
        void dispose() {
        }
    }

    private final IChannel channel;
    private final IStackTrace tcf_stk_service;
    private final ILineNumbers tcf_lns_service;

    public TCFDSFStack(DsfSession session, IChannel channel, final RequestMonitor monitor) {
        super(session);
        this.channel = channel;
        tcf_stk_service = channel.getRemoteService(IStackTrace.class);
        tcf_lns_service = channel.getRemoteService(ILineNumbers.class);
        initialize(new RequestMonitor(getExecutor(), monitor) { 
            @Override
            protected void handleSuccess() {
                String[] class_names = {
                        IStack.class.getName(),
                        TCFDSFStack.class.getName()
                };
                register(class_names, new Hashtable<String,String>());
                getSession().addServiceEventListener(TCFDSFStack.this, null);
                monitor.done();
            }
        });
    }

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

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

    public void getArguments(IFrameDMContext dmc, DataRequestMonitor<IVariableDMContext[]> rm) {
        if (dmc instanceof TCFFrameDMC) {
            // TODO function arguments
            rm.setData(new IVariableDMContext[0]);
            rm.done();
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }

    public void getFrameData(final IFrameDMContext dmc, final DataRequestMonitor<IFrameDMData> rm) {
        if (dmc instanceof TCFFrameDMC) {
            final TCFFrameDMC frame_dmc = (TCFFrameDMC)dmc;
            if (!frame_dmc.context_cache.validate()) {
                frame_dmc.context_cache.wait(new Runnable() {
                    public void run() {
                        getFrameData(dmc, rm);
                    }
                });
                return;
            }
            if (frame_dmc.context_cache.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", frame_dmc.context_cache.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            if (!frame_dmc.source_cache.validate()) {
                frame_dmc.source_cache.wait(new Runnable() {
                    public void run() {
                        getFrameData(dmc, rm);
                    }
                });
                return;
            }
            if (frame_dmc.source_cache.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", frame_dmc.source_cache.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            rm.setData(new TCFFrameData(frame_dmc));
            rm.done();
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }
    
    public TCFDataCache<?> getFramesCache(TCFDSFExecutionDMC exe, DataRequestMonitor<?> rm) {
        if (tcf_stk_service == null) {
            if (rm != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Stack trace service is not available", null)); //$NON-NLS-1$
                rm.done();
            }
            return null;
        }
        if (exe.isDisposed()) {
            if (rm != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Disposed DMC", null)); //$NON-NLS-1$
                rm.done();
            }
            return null;
        }
        if (exe.stack_frames_cache == null) exe.stack_frames_cache = new FramesCache(channel, exe);
        return (FramesCache)exe.stack_frames_cache;
    }
    
    public void getFrames(final IDMContext dmc, final DataRequestMonitor<IFrameDMContext[]> rm) {
        if (dmc instanceof TCFDSFExecutionDMC) {
            TCFDSFExecutionDMC exe = (TCFDSFExecutionDMC)dmc;
            FramesCache cache = (FramesCache)getFramesCache(exe, rm);
            if (cache == null) return;
            if (!cache.validate()) {
                cache.wait(new Runnable() {
                    public void run() {
                        getFrames(dmc, rm);
                    }
                });
                return;
            }
            if (cache.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", cache.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            Map<String,TCFFrameDMC> map = cache.getData();
            TCFFrameDMC[] arr = map.values().toArray(new TCFFrameDMC[map.size()]);
            Arrays.sort(arr);
            rm.setData(arr);
            rm.done();
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }

    public void getLocals(IFrameDMContext dmc, DataRequestMonitor<IVariableDMContext[]> rm) {
        if (dmc instanceof TCFFrameDMC) {
            // TODO function local variables
            rm.setData(new IVariableDMContext[0]);
            rm.done();
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }

    public void getStackDepth(final IDMContext dmc, final int maxDepth, final DataRequestMonitor<Integer> rm) {
        if (dmc instanceof TCFDSFExecutionDMC) {
            TCFDSFExecutionDMC exe = (TCFDSFExecutionDMC)dmc;
            FramesCache cache = (FramesCache)getFramesCache(exe, rm);
            if (cache == null) return;
            if (!cache.validate()) {
                cache.wait(new Runnable() {
                    public void run() {
                        getStackDepth(dmc, maxDepth, rm);
                    }
                });
                return;
            }
            if (cache.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", cache.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            rm.setData(cache.getData().size());
            rm.done();
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }

    public void getTopFrame(final IDMContext dmc, final DataRequestMonitor<IFrameDMContext> rm) {
        if (dmc instanceof TCFDSFExecutionDMC) {
            TCFDSFExecutionDMC exe = (TCFDSFExecutionDMC)dmc;
            FramesCache cache = (FramesCache)getFramesCache(exe, rm);
            if (cache == null) return;
            if (!cache.validate()) {
                cache.wait(new Runnable() {
                    public void run() {
                        getTopFrame(dmc, rm);
                    }
                });
                return;
            }
            if (cache.getError() != null) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        REQUEST_FAILED, "Data error", cache.getError())); //$NON-NLS-1$
                rm.done();
                return;
            }
            rm.setData(cache.createFrameDMC(cache.top_frame_id, 0));
            rm.done();
        }
        else {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
            rm.done();
        }
    }
    
    public TCFFrameDMC getTopFrame(TCFDSFExecutionDMC exe) {
        FramesCache cache = (FramesCache)getFramesCache(exe, null);
        assert cache != null;
        assert cache.isValid();
        assert cache.getError() == null;
        return cache.createFrameDMC(cache.top_frame_id, 0);
    }

    public void getVariableData(IVariableDMContext variableDmc, DataRequestMonitor<IVariableDMData> rm) {
        // TODO model data for local variables
        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
        rm.done();
    }

    public boolean isStackAvailable(IDMContext dmc) {
        return tcf_stk_service != null && dmc instanceof TCFDSFExecutionDMC;
    }

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

    @DsfServiceEventHandler
    public void eventDispatched(IRunControl.IResumedDMEvent e) {
        if (e.getReason() != StateChangeReason.STEP) {
            FramesCache cache = (FramesCache)((TCFDSFExecutionDMC)e.getDMContext()).stack_frames_cache;
            if (cache != null) cache.invalidateFrames();
        }
    }
    
    @DsfServiceEventHandler
    public void eventDispatched(IRunControl.ISuspendedDMEvent e) {
        FramesCache cache = (FramesCache)((TCFDSFExecutionDMC)e.getDMContext()).stack_frames_cache;
        if (cache != null) cache.invalidateFrames();
    }

    @DsfServiceEventHandler
    public void eventDispatched(IRunControl.IExitedDMEvent e) {
        FramesCache cache = (FramesCache)((TCFDSFExecutionDMC)e.getDMContext()).stack_frames_cache;
        if (cache != null) cache.dispose();
    }
}

Back to the top