Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e7c541b72ea3227fa663feda22584997c6c7ec7d (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
/*******************************************************************************
 * Copyright (c) 2010, 2012 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.cdt.ui.hover;

import java.util.Map;

import org.eclipse.cdt.debug.ui.editors.AbstractDebugTextHover;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHoverExtension2;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.internal.debug.ui.model.TCFChildren;
import org.eclipse.tcf.internal.debug.ui.model.TCFChildrenStackTrace;
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.TCFNodeStackFrame;
import org.eclipse.tcf.internal.debug.ui.model.TCFNumberFormat;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IExpressions;
import org.eclipse.tcf.services.IExpressions.DoneCreate;
import org.eclipse.tcf.services.IExpressions.DoneDispose;
import org.eclipse.tcf.services.IExpressions.DoneEvaluate;
import org.eclipse.tcf.services.IExpressions.Expression;
import org.eclipse.tcf.services.IExpressions.Value;
import org.eclipse.tcf.util.TCFDataCache;
import org.eclipse.tcf.util.TCFTask;

/**
 * TCF implementation of debug expression hover for the C/C++ Editor.
 */
public class TCFDebugTextHover extends AbstractDebugTextHover implements ITextHoverExtension2 {

    @Override
    public IInformationControlCreator getHoverControlCreator() {
        if (useExpressionExplorer()) {
            return createExpressionInformationControlCreator();
        }
        else {
            return new IInformationControlCreator() {
                public IInformationControl createInformationControl(Shell parent) {
                    return new DefaultInformationControl(parent, false);
                }
            };
        }
    }

    private IInformationControlCreator createExpressionInformationControlCreator() {
        return new ExpressionInformationControlCreator();
    }

    protected boolean useExpressionExplorer() {
        return true;
    }

    public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
        if (!useExpressionExplorer()) return getHoverInfo(textViewer, hoverRegion);
        final TCFNodeStackFrame activeFrame = getActiveFrame();
        if (activeFrame == null) return null;
        final String text = getExpressionText(textViewer, hoverRegion);
        if (text == null || text.length() == 0) return null;
        try {
            return new TCFTask<TCFNode>(activeFrame.getChannel()) {
                public void run() {
                    TCFNode evalContext = activeFrame.isEmulated() ? activeFrame.getParent() : activeFrame;
                    TCFChildren cache = evalContext.getModel().getHoverExpressionCache(evalContext, text);
                    if (!cache.validate(this)) return;
                    Map<String,TCFNode> nodes = cache.getData();
                    if (nodes != null) {
                        for (TCFNode node : nodes.values()) {
                            TCFDataCache<IExpressions.Value> value = ((TCFNodeExpression)node).getValue();
                            if (!value.validate(this)) return;
                            if (value.getData() != null) {
                                done(node.getParent());
                                return;
                            }
                        }
                    }
                    done(null);
                }
            }.get();
        }
        catch (Exception x) {
            // Problem in Eclipse 3.7:
            // TextViewerHoverManager calls Thread.interrupt(),
            // but it fails to handle InterruptedException.
            // We have to catch and ignore the exception.
            return null;
        }
    }

    @Override
    protected boolean canEvaluate() {
        return getActiveFrame() != null;
    }

    private TCFNodeStackFrame getActiveFrame() {
        IAdaptable context = getSelectionAdaptable();
        if (context instanceof TCFNodeStackFrame) return (TCFNodeStackFrame) context;
        if (context instanceof TCFNodeExecContext) {
            try {
                final TCFNodeExecContext exe = (TCFNodeExecContext) context;
                return new TCFTask<TCFNodeStackFrame>(exe.getChannel()) {
                    public void run() {
                        TCFChildrenStackTrace stack = exe.getStackTrace();
                        if (!stack.validate(this)) return;
                        done(stack.getTopFrame());
                    }
                }.get();
            }
            catch (Exception x) {
                // Problem in Eclipse 3.7:
                // TextViewerHoverManager calls Thread.interrupt(),
                // but it fails to handle InterruptedException.
                // We have to catch and ignore the exception.
                return null;
            }
        }
        return null;
    }

    @Override
    protected String evaluateExpression(final String expression) {
        final TCFNodeStackFrame activeFrame = getActiveFrame();
        if (activeFrame == null) return null;
        final IChannel channel = activeFrame.getChannel();
        return new TCFTask<String>(channel) {
            public void run() {
                final IExpressions exprSvc = channel.getRemoteService(IExpressions.class);
                if (exprSvc != null) {
                    TCFNode evalContext = activeFrame.isEmulated() ? activeFrame.getParent() : activeFrame;
                    exprSvc.create(evalContext.getID(), null, expression, new DoneCreate() {
                        public void doneCreate(IToken token, Exception error, final Expression context) {
                            if (error == null) {
                                exprSvc.evaluate(context.getID(), new DoneEvaluate() {
                                    public void doneEvaluate(IToken token, Exception error, Value value) {
                                        done(error == null && value != null ? getValueText(value) : null);
                                        exprSvc.dispose(context.getID(), new DoneDispose() {
                                            public void doneDispose(IToken token, Exception error) {
                                                // no-op
                                            }
                                        });
                                    }
                                });
                            }
                            else {
                                done(null);
                            }
                        }
                    });
                }
                else {
                    done(null);
                }
            }
        }.getE();
    }

    private static String getValueText(Value value) {
        byte[] data = value.getValue();
        if (data == null) return "N/A";
        switch(value.getTypeClass()) {
        case integer:
            return TCFNumberFormat.toBigInteger(data, value.isBigEndian(), true).toString();
        case real:
            return TCFNumberFormat.toFPString(data, value.isBigEndian());
        case complex:
            return TCFNumberFormat.toComplexFPString(data, value.isBigEndian());
        default:
            return "0x" + TCFNumberFormat.toBigInteger(data, value.isBigEndian(), false).toString(16);
        }
    }
}

Back to the top