Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 85a72f1f7d3597a6f1eb9f5faa145891acc4d888 (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
/*******************************************************************************
 * 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 com.windriver.tcf.dsf.core.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.datamodel.ServiceDMContext;
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.osgi.framework.BundleContext;

import com.windriver.tcf.api.protocol.IChannel;
import com.windriver.tcf.api.protocol.IToken;
import com.windriver.tcf.api.services.ILineNumbers;
import com.windriver.tcf.api.services.IStackTrace;
import com.windriver.tcf.api.services.ILineNumbers.CodeArea;
import com.windriver.tcf.dsf.core.Activator;

public class TCFDSFStack extends AbstractDsfService implements IStack {
    
    private static final String TOP_FRAME = "TopFrame:";

    private class TCFFrameDMC extends AbstractDMContext implements IFrameDMContext, Comparable<TCFFrameDMC> {
        
        final String id;
        final TCFDSFExecutionDMC exe_dmc;
        final TCFDataCache<TCFFrameData> frame_data;
        
        int level;
        TCFFrameData 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;
            frame_data = new TCFDataCache<TCFFrameData>(channel) {
                
                @Override
                public boolean startDataRetrieval() {
                    assert command == null;
                    if (id == null || tcf_stk_service == null) {
                        data = null;
                        valid = true;
                        return true;
                    }
                    if (level == 0) {
                        assert id.startsWith(TOP_FRAME);
                        // Top frame is special case: most of its data is stored in CPU registers.
                        // Other frames are stored in memory - in thread stack area.
                        return getTopFrame();
                    }
                    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;
                            command = null;
                            if (err != null) {
                                error = err;
                                data = null;
                            }
                            else {
                                TCFAddress a = null;
                                Number n = context[0].getReturnAddress();
                                if (n != null) a = new TCFAddress(n);
                                // Optimization: skip source position lookup if same address
                                if (prev_data != null && prev_data.address.equals(a)) {
                                    data = prev_data;
                                    data.context = context[0];
                                    data.level = level;
                                }
                                else {
                                    data = new TCFFrameData();
                                    data.context = context[0];
                                    data.address = a;
                                    data.level = level;
                                    if (!getSourcePos()) return;
                                }
                            }
                            valid = true;
                            validate();
                        }
                    });
                    return false;
                }
                
                private boolean getTopFrame() {
                    assert level == 0;
                    if (!exe_dmc.validateState()) {
                        exe_dmc.addStateWaitingRequest(new IDataRequest() {
                            public void cancel() {
                                reset();
                            }
                            public void done() {
                                validate();
                            }
                        });
                        return false;
                    }
                    prev_data = data = new TCFFrameData();
                    data.address = exe_dmc.getPC();
                    data.level = level;
                    if (!getSourcePos()) return false;
                    valid = true;
                    return true;
                }
                
                private boolean getSourcePos() {
                    if (tcf_lns_service == null) return true;
                    if (data.address == null) return true;
                    BigInteger a1 = data.address.getValue();
                    BigInteger a2 = data.address.add(1).getValue();
                    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;
                            command = null;
                            if (err != null) {
                                data.src_pos_error = err;
                            }
                            else if (areas != null && areas.length > 0) {
                                for (ILineNumbers.CodeArea area : areas) {
                                    if (data.code_area == null || area.start_line < data.code_area.start_line) {
                                        data.code_area = area;
                                    }
                                }
                                prev_data = data;
                            }
                            valid = true;
                            validate();
                        }
                    });
                    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;
        }    
    }
    
    private static class TCFFrameData implements IFrameDMData {
        
        IStackTrace.StackTraceContext context;
        IAddress address;
        int level;
        String function;
        Throwable src_pos_error;
        ILineNumbers.CodeArea code_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 + 1;
        }

        public int getColumn() {
            if (code_area == null) return -1;
            return code_area.start_column + 1;
        }
    }
    
    private class FramesCache extends TCFDataCache<Map<String,TCFFrameDMC>> implements TCFDSFExecutionDMC.DataCache {
        
        private final TCFDSFExecutionDMC dmc;
        private final Map<String,TCFFrameDMC> frame_pool;
        
        FramesCache(IChannel channel, TCFDSFExecutionDMC dmc) {
            super(channel);
            this.dmc = dmc;
            frame_pool = new HashMap<String,TCFFrameDMC>();
        }
        
        @Override
        public boolean startDataRetrieval() {
            assert command == null;
            if (tcf_stk_service == null) {
                data = null;
                valid = true;
                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;
                    command = null;
                    if (err != null) {
                        data = null;
                        error = err;
                    }
                    else {
                        data = new HashMap<String,TCFFrameDMC>();
                        for (int i = 0; i < contexts.length; i++) {
                            String id = contexts[i];
                            TCFFrameDMC n = frame_pool.get(id);
                            if (n == null) frame_pool.put(id, n = new TCFFrameDMC(dmc, id));
                            n.level = contexts.length - i;
                            data.put(id, n);
                        }
                        String id = TOP_FRAME + dmc.getTcfContextId();
                        TCFFrameDMC n = frame_pool.get(id);
                        if (n == null) frame_pool.put(id, n = new TCFFrameDMC(dmc, id));
                        n.level = 0;
                        data.put(id, n);
                    }
                    valid = true;
                    validate();
                }
            });
            return false;
        }

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

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

    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);
        service_dmc = new ServiceDMContext(this, "#stack_trace");
        initialize(new RequestMonitor(getExecutor(), monitor) { 
            @Override
            protected void handleOK() {
                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 IDMContext getServiceContext() {
        return service_dmc;
    }

    public void getArguments(IDMContext 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;
            TCFDataCache<TCFFrameData> cache = frame_dmc.frame_data;
            if (!cache.validate()) {
                cache.addWaitingRequest(new IDataRequest() {
                    public void cancel() {
                        rm.setStatus(new Status(IStatus.CANCEL, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Canceled", null)); //$NON-NLS-1$
                        rm.setCanceled(true);
                        rm.done();
                    }
                    public void done() {
                        getFrameData(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.getData());
            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 getFrames(final IDMContext dmc, final DataRequestMonitor<IFrameDMContext[]> rm) {
        if (tcf_stk_service == null) {
            rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                    INVALID_HANDLE, "Stack trace service is not available", null)); //$NON-NLS-1$
            rm.done();
        }
        else if (dmc instanceof TCFDSFExecutionDMC) {
            TCFDSFExecutionDMC exe = (TCFDSFExecutionDMC)dmc;
            if (exe.isDisposed()) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Disposed DMC", null)); //$NON-NLS-1$
                rm.done();
                return;
            }
            if (exe.stack_frames_cache == null) exe.stack_frames_cache = new FramesCache(channel, exe);
            FramesCache cache = (FramesCache)exe.stack_frames_cache;
            if (!cache.validate()) {
                cache.addWaitingRequest(new IDataRequest() {
                    public void cancel() {
                        rm.setStatus(new Status(IStatus.CANCEL, Activator.PLUGIN_ID,
                                REQUEST_FAILED, "Canceled", null)); //$NON-NLS-1$
                        rm.setCanceled(true);
                        rm.done();
                    }
                    public void done() {
                        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> c = cache.getData();
            TCFFrameDMC[] arr = c.values().toArray(new TCFFrameDMC[c.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(IDMContext 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(DataRequestMonitor<Integer> rm) {
        // TODO don't know what getStackDepth() is supposed to return
        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
        rm.done();
    }

    public void getStackDepth(int maxDepth, DataRequestMonitor<Integer> rm) {
        // TODO don't know what getStackDepth() is supposed to return
        rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                INVALID_HANDLE, "Unknown DMC type", null)); //$NON-NLS-1$
        rm.done();
    }

    public void getTopFrame(IDMContext dmc, DataRequestMonitor<IFrameDMContext> rm) {
        if (dmc instanceof TCFDSFExecutionDMC) {
            TCFDSFExecutionDMC exe = (TCFDSFExecutionDMC)dmc;
            if (exe.isDisposed()) {
                rm.setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                        INVALID_HANDLE, "Disposed DMC", null)); //$NON-NLS-1$
                rm.done();
                return;
            }
            if (exe.stack_frames_cache == null) exe.stack_frames_cache = new FramesCache(channel, exe);
            FramesCache cache = (FramesCache)exe.stack_frames_cache;
            String id = TOP_FRAME + exe.getTcfContextId();
            TCFFrameDMC n = cache.frame_pool.get(id);
            if (n == null) cache.frame_pool.put(id, n = new TCFFrameDMC(exe, id));
            n.level = 0;
            rm.setData(n);
            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 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 if (dmc == service_dmc) {
            ((DataRequestMonitor<TCFDSFStack>)rm).setData(this);
            rm.done();
        }
        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.getExecutionContext()).stack_frames_cache;
        if (cache != null) cache.dispose();
    }
}

Back to the top