Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 98baa6423956d4dbb9094e382fce2edf4beae0cb (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
/*******************************************************************************
 * 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.ui.model;

import java.math.BigInteger;
import java.util.Map;

import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.tm.internal.tcf.debug.model.TCFSourceRef;
import org.eclipse.tm.internal.tcf.debug.ui.ImageCache;
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.ILineNumbers;
import org.eclipse.tm.tcf.services.IMemory;
import org.eclipse.tm.tcf.services.IStackTrace;
import org.eclipse.tm.tcf.services.ILineNumbers.CodeArea;
import org.eclipse.tm.tcf.util.TCFDataCache;

public class TCFNodeStackFrame extends TCFNode {

    private int frame_no;
    private final TCFChildrenRegisters children_regs;
    private final TCFChildrenLocalVariables children_vars;
    private final TCFChildrenExpressions children_exps;
    private final TCFDataCache<IStackTrace.StackTraceContext> stack_trace_context;
    private final TCFDataCache<TCFSourceRef> line_info;
    
    TCFNodeStackFrame(final TCFNodeExecContext parent, final String id) {
        super(parent, id);
        children_regs = new TCFChildrenRegisters(this);
        children_vars = new TCFChildrenLocalVariables(this);
        children_exps = new TCFChildrenExpressions(this);
        IChannel channel = model.getLaunch().getChannel();
        stack_trace_context = new TCFDataCache<IStackTrace.StackTraceContext>(channel) {
            @Override
            protected boolean startDataRetrieval() {
                assert command == null;
                if (!parent.isSuspended()) {
                    set(null, null, null);
                    return true;
                }
                IStackTrace st = model.getLaunch().getService(IStackTrace.class);
                if (st == null) {
                    assert frame_no == 0;
                    set(null, null, null);
                    return true;
                }
                command = st.getContext(new String[]{ id }, new IStackTrace.DoneGetContext() {
                    public void doneGetContext(IToken token, Exception error, IStackTrace.StackTraceContext[] context) {
                        set(token, error, context == null || context.length == 0 ? null : context[0]);
                    }
                });
                return false;
            }
        };
        final Map<BigInteger,TCFSourceRef> line_info_cache = parent.getLineInfoCache();
        line_info = new TCFDataCache<TCFSourceRef>(channel) {
            @Override
            protected boolean startDataRetrieval() {
                if (!stack_trace_context.validate()) {
                    stack_trace_context.wait(this);
                    return false;
                }
                BigInteger n = getAddress();
                if (n == null) {
                    set(null, null, null);
                    return true;
                }
                TCFSourceRef l = line_info_cache.get(n);
                if (l != null) {
                    set(null, null, l);
                    return true;
                }
                ILineNumbers ln = model.getLaunch().getService(ILineNumbers.class);
                if (ln == null) {
                    l = new TCFSourceRef();
                    l.address = n;
                    set(null, null, l);
                    return true;
                }
                final BigInteger n0 = n;
                final BigInteger n1 = n0.add(BigInteger.valueOf(1));
                command = ln.mapToSource(parent.id, n0, n1, new ILineNumbers.DoneMapToSource() {
                    public void doneMapToSource(IToken token, Exception error, CodeArea[] areas) {
                        TCFSourceRef l = new TCFSourceRef();
                        l.address = n0;
                        if (error == null && areas != null && areas.length > 0) {
                            for (ILineNumbers.CodeArea area : areas) {
                                if (l.area == null || area.start_line < l.area.start_line) {
                                    l.area = area;
                                }
                            }
                        }
                        l.error = error;
                        set(token, null, l);
                        if (error == null) line_info_cache.put(l.address, l);
                    }
                });
                return false;
            }
        };
    }

    public int getFrameNo() {
        assert Protocol.isDispatchThread();
        return frame_no;
    }
    
    void setFrameNo(int frame_no) {
        this.frame_no = frame_no;
    }
    
    public TCFDataCache<TCFSourceRef> getLineInfo() {
        return line_info;
    }

    @Override
    void dispose() {
        stack_trace_context.reset(null);
        line_info.reset(null);
        children_regs.dispose();
        children_vars.dispose();
        children_exps.dispose();
        super.dispose();
    }

    @Override
    void dispose(String id) {
        children_regs.dispose(id);
        children_vars.dispose(id);
        children_exps.dispose(id);
    }
    
    public TCFDataCache<IStackTrace.StackTraceContext> getStackTraceContext() {
        return stack_trace_context;
    }

    @Override
    public BigInteger getAddress() {
        assert Protocol.isDispatchThread();
        if (!stack_trace_context.isValid()) return null;
        IStackTrace.StackTraceContext ctx = stack_trace_context.getData();
        if (ctx != null) {
            Number n = ctx.getInstructionAddress();
            if (n instanceof BigInteger) return (BigInteger)n;
            if (n != null) return new BigInteger(n.toString());
        }
        if (frame_no == 0) return parent.getAddress();
        return null;
    }
    
    public BigInteger getReturnAddress() {
        assert Protocol.isDispatchThread();
        if (!stack_trace_context.isValid()) return null;
        IStackTrace.StackTraceContext ctx = stack_trace_context.getData();
        if (ctx != null) {
            Number n = ctx.getReturnAddress();
            if (n instanceof BigInteger) return (BigInteger)n;
            if (n != null) return new BigInteger(n.toString());
        }
        return null;
    }
    
    @Override
    public int getNodeIndex(IPresentationContext p, TCFNode n) {
        if (IDebugUIConstants.ID_REGISTER_VIEW.equals(p.getId())) {
            if (!children_regs.isValid()) return -1;
            return children_regs.getIndexOf(n);
        }
        if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(p.getId())) {
            if (!children_vars.isValid()) return -1;
            return children_vars.getIndexOf(n);
        }
        if (IDebugUIConstants.ID_EXPRESSION_VIEW.equals(p.getId())) {
            if (!children_exps.isValid()) return -1;
            return children_exps.getIndexOf(n);
        }
        return 0;
    }
    
    @Override
    public int getChildrenCount(IPresentationContext p) {
        if (IDebugUIConstants.ID_REGISTER_VIEW.equals(p.getId())) {
            if (!children_regs.isValid()) return -1;
            return children_regs.size();
        }
        if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(p.getId())) {
            if (!children_vars.isValid()) return -1;
            return children_vars.size();
        }
        if (IDebugUIConstants.ID_EXPRESSION_VIEW.equals(p.getId())) {
            if (!children_exps.isValid()) return -1;
            return children_exps.size();
        }
        return 0;
    }

    @Override
    protected void getData(IChildrenCountUpdate result) {
        if (IDebugUIConstants.ID_REGISTER_VIEW.equals(result.getPresentationContext().getId())) {
            result.setChildCount(children_regs.size());
        }
        else if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(result.getPresentationContext().getId())) {
            result.setChildCount(children_vars.size());
        }
        else if (IDebugUIConstants.ID_EXPRESSION_VIEW.equals(result.getPresentationContext().getId())) {
            result.setChildCount(children_exps.size());
        }
        else {
            result.setChildCount(0);
        }
    }

    @Override
    protected void getData(IChildrenUpdate result) {
        TCFNode[] arr = null;
        if (IDebugUIConstants.ID_REGISTER_VIEW.equals(result.getPresentationContext().getId())) {
            arr = children_regs.toArray();
        }
        else if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(result.getPresentationContext().getId())) {
            arr = children_vars.toArray();
        }
        else if (IDebugUIConstants.ID_EXPRESSION_VIEW.equals(result.getPresentationContext().getId())) {
            arr = children_exps.toArray();
        }
        else {
            arr = new TCFNode[0];
        }
        int offset = 0;
        int r_offset = result.getOffset(); 
        int r_length = result.getLength(); 
        for (TCFNode n : arr) {
            if (offset >= r_offset && offset < r_offset + r_length) {
                result.setChild(n, offset);
            }
            offset++;
        }
    }

    @Override
    protected void getData(IHasChildrenUpdate result) {
        if (IDebugUIConstants.ID_REGISTER_VIEW.equals(result.getPresentationContext().getId())) {
            result.setHasChilren(children_regs.size() > 0);
        }
        else if (IDebugUIConstants.ID_VARIABLE_VIEW.equals(result.getPresentationContext().getId())) {
            result.setHasChilren(children_vars.size() > 0);
        }
        else if (IDebugUIConstants.ID_EXPRESSION_VIEW.equals(result.getPresentationContext().getId())) {
            result.setHasChilren(children_exps.size() > 0);
        }
        else {
            result.setHasChilren(false);
        }
    }

    @Override
    protected void getData(ILabelUpdate result) {
        result.setImageDescriptor(ImageCache.getImageDescriptor(getImageName()), 0);
        TCFChildrenStackTrace st = ((TCFNodeExecContext)parent).getStackTrace();
        if (st.getData().get(id) == null) {
            result.setLabel("", 0);
        }
        else {
            Throwable error = stack_trace_context.getError();
            if (error == null) error = line_info.getError();
            if (error != null && ((TCFNodeExecContext)parent).isSuspended()) {
                System.out.println(error.toString());            
                result.setForeground(new RGB(255, 0, 0), 0);
                result.setLabel(error.getClass().getName() + ": " + error.getMessage(), 0);
            }
            else {
                TCFSourceRef l = line_info.getData();
                if (l == null) {
                    result.setLabel("...", 0);
                }
                else {
                    String label = makeHexAddrString(l.address);
                    if (l.area != null && l.area.file != null) {
                        label += ": " + l.area.file + ", line " + l.area.start_line;
                    }
                    result.setLabel(label, 0);
                }
            }
        }
    }

    private String makeHexAddrString(Number n) {
        BigInteger i = null;
        if (n instanceof BigInteger) i = (BigInteger)n;
        else i = new BigInteger(n.toString());
        String s = i.toString(16);
        IMemory.MemoryContext m = ((TCFNodeExecContext)parent).getMemoryContext();
        int sz = (m != null ? m.getAddressSize() : 4) * 2;
        int l = sz - s.length();
        if (l < 0) l = 0;
        if (l > 16) l = 16;
        return "0x0000000000000000".substring(0, 2 + l) + s;
    }

    void onSourceMappingChange() {
        line_info.reset();
        addModelDelta(IModelDelta.STATE);
    }

    void onSuspended() {
        stack_trace_context.reset();
        line_info.reset();
        children_regs.onSuspended();
        children_vars.onSuspended();
        children_exps.onSuspended();
        addModelDelta(IModelDelta.STATE | IModelDelta.CONTENT);
    }
    
    void onRegistersChanged() {
        children_regs.onRegistersChanged();
        addModelDelta(IModelDelta.STATE | IModelDelta.CONTENT);
    }

    @Override
    public boolean validateNode(Runnable done) {
        TCFDataCache<?> pending = null;
        TCFChildrenStackTrace stack_trace = ((TCFNodeExecContext)parent).getStackTrace();
        if (!stack_trace.validate()) pending = stack_trace;
        if (!stack_trace_context.validate()) pending = stack_trace_context;
        if (!children_regs.validate()) pending = children_regs;
        if (!children_vars.validate()) pending = children_vars;
        if (!children_exps.validate()) pending = children_exps;
        if (!line_info.validate()) pending = line_info;
        if (pending == null) return true;
        pending.wait(done);
        return false;
    }

    @Override
    protected String getImageName() {
        if (((TCFNodeExecContext)parent).isRunning()) return ImageCache.IMG_STACK_FRAME_RUNNING;
        return ImageCache.IMG_STACK_FRAME_SUSPENDED;
    }

    @Override
    public int compareTo(TCFNode n) {
        if (n instanceof TCFNodeStackFrame) {
            TCFNodeStackFrame f = (TCFNodeStackFrame)n;
            if (frame_no < f.frame_no) return -1;
            if (frame_no > f.frame_no) return +1;
        }
        return id.compareTo(n.id);
    }
}

Back to the top