Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 72160c1a06f768c63e90e71b0a2983725dc42d61 (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
/*******************************************************************************
 * Copyright (c) 2011, 2015 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.tcf.internal.debug.ui.adapters;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.debug.ui.IDebugView;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.widgets.Display;
import org.eclipse.tcf.internal.debug.model.TCFContextState;
import org.eclipse.tcf.internal.debug.model.TCFLaunch;
import org.eclipse.tcf.internal.debug.model.TCFSourceRef;
import org.eclipse.tcf.internal.debug.ui.Activator;
import org.eclipse.tcf.internal.debug.ui.model.TCFModel;
import org.eclipse.tcf.internal.debug.ui.model.TCFNode;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeExecContext;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeExpression;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeLaunch;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeModule;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeRegister;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeExecContext.MemoryRegion;
import org.eclipse.tcf.internal.debug.ui.model.TCFNodeStackFrame;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.JSON;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IExpressions;
import org.eclipse.tcf.services.IMemory;
import org.eclipse.tcf.services.IRegisters;
import org.eclipse.tcf.services.IRunControl;
import org.eclipse.tcf.services.IStackTrace;
import org.eclipse.tcf.services.ISymbols;
import org.eclipse.tcf.util.TCFDataCache;
import org.eclipse.tcf.util.TCFTask;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.PropertySheet;
import org.eclipse.ui.views.properties.PropertySheetPage;

/**
 * Adapts TCFNode to IPropertySource.
 */
public class TCFNodePropertySource implements IPropertySource {

    private final TCFNode node;
    private final Map<String,Object> properties = new HashMap<String,Object>();

    private IPropertyDescriptor[] descriptors;

    public TCFNodePropertySource(TCFNode node) {
        this.node = node;
    }

    public Object getEditableValue() {
        return null;
    }

    public IPropertyDescriptor[] getPropertyDescriptors() {
        if (descriptors == null) {
            if (node == null) {
                // A disconnected TCF launch was selected
                return descriptors = new IPropertyDescriptor[0];
            }
            try {
                final List<IPropertyDescriptor> list = new ArrayList<IPropertyDescriptor>();
                descriptors = new TCFTask<IPropertyDescriptor[]>(node.getChannel()) {
                    public void run() {
                        list.clear();
                        properties.clear();
                        if (node instanceof TCFNodeLaunch) {
                            getLaunchDescriptors((TCFNodeLaunch)node);
                        }
                        else if (node instanceof TCFNodeExecContext) {
                            getExecContextDescriptors((TCFNodeExecContext)node);
                        }
                        else if (node instanceof TCFNodeStackFrame) {
                            getFrameDescriptors((TCFNodeStackFrame)node);
                        }
                        else if (node instanceof TCFNodeRegister) {
                            getRegisterDescriptors((TCFNodeRegister)node);
                        }
                        else if (node instanceof TCFNodeModule) {
                            getModuleDescriptors((TCFNodeModule)node);
                        }
                        else if (node instanceof TCFNodeExpression) {
                            getExpressionDescriptors((TCFNodeExpression)node);
                        }
                        else {
                            done(list.toArray(new IPropertyDescriptor[list.size()]));
                        }
                    }

                    private void getExpressionDescriptors(TCFNodeExpression exp_node) {
                        TCFDataCache<IExpressions.Expression> exp_cache = exp_node.getExpression();
                        if (!exp_cache.validate(this)) return;
                        TCFDataCache<ISymbols.Symbol> type_cache = exp_node.getType();
                        if (!type_cache.validate(this)) return;
                        TCFDataCache<IExpressions.Value> value_cache = exp_node.getValue();
                        if (!value_cache.validate(this)) return;

                        Throwable exp_error = exp_cache.getError();
                        if (exp_error != null) addDescriptor("Expression", "Error", TCFModel.getErrorMessage(exp_error, false));
                        IExpressions.Expression exp_data = exp_cache.getData();
                        if (exp_data != null) {
                            Map<String,Object> props = exp_data.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Expression", key, value);
                            }
                        }

                        Throwable type_error = type_cache.getError();
                        if (type_error != null) addDescriptor("Expression Type", "Error", TCFModel.getErrorMessage(type_error, false));
                        ISymbols.Symbol type_data = type_cache.getData();
                        if (type_data != null) {
                            Map<String,Object> props = type_data.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Expression Type", key, value);
                            }
                        }

                        Throwable value_error = value_cache.getError();
                        if (value_error != null) addDescriptor("Expression Value", "Error", TCFModel.getErrorMessage(value_error, false));
                        IExpressions.Value value_data = value_cache.getData();
                        if (value_data != null) {
                            Map<String,Object> props = value_data.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Expression Value", key, value);
                            }
                            String sym_id = value_data.getSymbolID();
                            if (sym_id != null) {
                                TCFDataCache<ISymbols.Symbol> sym_cache = exp_node.getModel().getSymbolInfoCache(sym_id);
                                if (!sym_cache.validate(this)) return;
                                Throwable sym_error = sym_cache.getError();
                                if (sym_error != null) addDescriptor("Expression Value Symbol", "Error", TCFModel.getErrorMessage(sym_error, false));
                                ISymbols.Symbol sym_data = sym_cache.getData();
                                if (sym_data != null) {
                                    props = sym_data.getProperties();
                                    for (String key : props.keySet()) {
                                        Object value = props.get(key);
                                        if (value instanceof Number) {
                                            value = toHexAddrString((Number)value);
                                        }
                                        addDescriptor("Expression Value Symbol", key, value);
                                    }
                                }
                            }
                        }
                        done(list.toArray(new IPropertyDescriptor[list.size()]));
                    }

                    private void getModuleDescriptors(TCFNodeModule mod_node) {
                        TCFDataCache<MemoryRegion> mod_cache = mod_node.getRegion();
                        if (!mod_cache.validate(this)) return;
                        Throwable error = mod_cache.getError();
                        if (error != null) addDescriptor("Module Properties", "Error", TCFModel.getErrorMessage(error, false));
                        MemoryRegion ctx = mod_cache.getData();
                        if (ctx != null && ctx.region != null) {
                            Map<String,Object> props = ctx.region.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Module Properties", key, value);
                            }
                        }
                        done(list.toArray(new IPropertyDescriptor[list.size()]));
                    }

                    private void getRegisterDescriptors(TCFNodeRegister reg_node) {
                        TCFDataCache<IRegisters.RegistersContext> ctx_cache = reg_node.getContext();
                        if (!ctx_cache.validate(this)) return;
                        Throwable error = ctx_cache.getError();
                        if (error != null) addDescriptor("Register Properties", "Error", TCFModel.getErrorMessage(error, false));
                        IRegisters.RegistersContext ctx = ctx_cache.getData();
                        if (ctx != null) {
                            Map<String,Object> props = ctx.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Register Properties", key, value);
                            }
                        }
                        done(list.toArray(new IPropertyDescriptor[list.size()]));
                    }

                    private void getFrameDescriptors(TCFNodeStackFrame frameNode) {
                        TCFDataCache<IStackTrace.StackTraceContext> ctx_cache = frameNode.getStackTraceContext();
                        TCFDataCache<TCFSourceRef> line_info_cache = frameNode.getLineInfo();
                        if (!validateAll(ctx_cache, line_info_cache)) return;
                        IStackTrace.StackTraceContext ctx = ctx_cache.getData();
                        if (ctx != null) {
                            Map<String,Object> props = ctx.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Stack Frame Properties", key, value);
                            }
                        }
                        TCFSourceRef ref = line_info_cache.getData();
                        if (ref != null) {
                            if (ref.area != null) {
                                if (ref.area.directory != null) addDescriptor("Source", "Directory", ref.area.directory);
                                if (ref.area.file != null) addDescriptor("Source", "File", ref.area.file);
                                if (ref.area.start_line > 0) addDescriptor("Source", "Line", ref.area.start_line);
                                if (ref.area.start_column > 0) addDescriptor("Source", "Column", ref.area.start_column);
                            }
                            if (ref.error != null) {
                                addDescriptor("Source", "Error", TCFModel.getErrorMessage(ref.error, false));
                            }
                        }
                        done(list.toArray(new IPropertyDescriptor[list.size()]));
                    }

                    private void getExecContextDescriptors(TCFNodeExecContext exe_node) {
                        TCFDataCache<IMemory.MemoryContext> mem_cache = exe_node.getMemoryContext();
                        TCFDataCache<IRunControl.RunControlContext> ctx_cache = exe_node.getRunContext();
                        TCFDataCache<TCFContextState> state_cache = exe_node.getState();
                        TCFDataCache<MemoryRegion[]> mem_map_cache = exe_node.getMemoryMap();
                        if (!validateAll(mem_cache, ctx_cache, state_cache, mem_map_cache)) return;
                        IMemory.MemoryContext mem = mem_cache.getData();
                        if (mem != null) {
                            Map<String,Object> props = mem.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Memory", key, value);
                            }
                        }
                        IRunControl.RunControlContext ctx = ctx_cache.getData();
                        if (ctx != null) {
                            Map<String,Object> props = ctx.getProperties();
                            for (String key : props.keySet()) {
                                Object value = props.get(key);
                                if (value instanceof Number) {
                                    value = toHexAddrString((Number)value);
                                }
                                addDescriptor("Context", key, value);
                            }
                        }
                        TCFContextState state = state_cache.getData();
                        if (state != null) {
                            addDescriptor("State", "Suspended", state.is_suspended);
                            if (state.suspend_reason != null) addDescriptor("State", "Suspend reason", state.suspend_reason);
                            if (state.suspend_pc != null) addDescriptor("State", "PC", toHexAddrString(new BigInteger(state.suspend_pc)));
                            addDescriptor("State", "Active", !exe_node.isNotActive());
                            if (state.suspend_params != null) {
                                for (String key : state.suspend_params.keySet()) {
                                    Object value = state.suspend_params.get(key);
                                    if (value instanceof Number) {
                                        value = toHexAddrString((Number)value);
                                    }
                                    addDescriptor("State Properties", key, value);
                                }
                            }
                        }
                        done(list.toArray(new IPropertyDescriptor[list.size()]));
                    }

                    private void getLaunchDescriptors(TCFNodeLaunch launch_node) {
                        IChannel channel = launch_node.getChannel();
                        for (String s : channel.getRemoteServices()) {
                            addDescriptor("Target Services", s, "");
                        }
                        TCFLaunch launch = launch_node.getModel().getLaunch();
                        Set<String> filter = launch.getContextFilter();
                        if (filter != null) addDescriptor("Context Filter", "Context IDs", filter.toString());
                        done(list.toArray(new IPropertyDescriptor[list.size()]));
                    }

                    private void addDescriptor(String category, String key, Object value) {
                        String id = category + '.' + key;
                        PropertyDescriptor desc = new PropertyDescriptor(id, key);
                        desc.setCategory(category);
                        list.add(desc);
                        properties.put(id, value);
                    }

                    private boolean validateAll(TCFDataCache<?>... caches) {
                        TCFDataCache<?> pending = null;
                        for (TCFDataCache<?> cache : caches) {
                            if (!cache.validate()) pending = cache;
                        }
                        if (pending != null) {
                            pending.wait(this);
                            return false;
                        }
                        return true;
                    }
                    @Override
                    public void done(IPropertyDescriptor[] r) {
                        need_refresh = true;
                        super.done(r);
                    }
                }.get();
            }
            catch (Exception e) {
                if (node.getChannel().getState() != IChannel.STATE_CLOSED) {
                    Activator.log("Error retrieving property data", e);
                }
                descriptors = new IPropertyDescriptor[0];
            }
        }
        return descriptors;
    }

    public Object getPropertyValue(final Object id) {
        return properties.get(id);
    }

    public boolean isPropertySet(Object id) {
        return false;
    }

    public void resetPropertyValue(Object id) {
    }

    public void setPropertyValue(Object id, Object value) {
    }

    private static String toHexAddrString(Number num) {
        BigInteger n = JSON.toBigInteger(num);
        String s = n.toString(16);
        int sz = s.length() > 8 ? 16 : 8;
        int l = sz - s.length();
        if (l < 0) l = 0;
        if (l > 16) l = 16;
        return "0x0000000000000000".substring(0, 2 + l) + s;
    }

    private static final long REFRESH_DELAY = 250;
    private static boolean refresh_posted = false;
    private static long refresh_time = 0;
    private static boolean need_refresh = false;

    public static void refresh(TCFNode node) {
        assert Protocol.isDispatchThread();
        if (!need_refresh) return;
        refresh_time = System.currentTimeMillis();
        if (refresh_posted) return;
        refresh_posted = true;
        Protocol.invokeLater(REFRESH_DELAY, new Runnable() {
            public void run() {
                long time = System.currentTimeMillis();
                if (time - refresh_time < REFRESH_DELAY * 3 / 4) {
                    Protocol.invokeLater(refresh_time + REFRESH_DELAY - time, this);
                    return;
                }
                refresh_posted = false;
                need_refresh = false;
                synchronized (Device.class) {
                    Display display = Display.getDefault();
                    if (!display.isDisposed()) {
                        display.asyncExec(new Runnable() {
                            public void run() {
                                for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
                                    IWorkbenchPart active_part = window.getActivePage().getActivePart();
                                    if (active_part instanceof IDebugView) {
                                        IViewPart part = window.getActivePage().findView("org.eclipse.ui.views.PropertySheet");
                                        if (part instanceof PropertySheet) {
                                            PropertySheet props = (PropertySheet)part;
                                            PropertySheetPage page = (PropertySheetPage)props.getCurrentPage();
                                            // TODO: need to check Properties view selection to skip unnecessary refreshes
                                            if (page != null) page.refresh();
                                        }
                                    }
                                }
                            }
                        });
                    }
                }
            }
        });
    }
}

Back to the top