Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b4efec1794038296600938e9211ce2fbf6b75702 (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
/*******************************************************************************
 * Copyright (c) 2011 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.commands;

import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.commands.IDebugCommandRequest;
import org.eclipse.debug.core.commands.IDropToFrameHandler;
import org.eclipse.debug.core.commands.IEnabledStateRequest;
import org.eclipse.tm.internal.tcf.debug.actions.TCFActionStepOut;
import org.eclipse.tm.internal.tcf.debug.model.TCFContextState;
import org.eclipse.tm.internal.tcf.debug.ui.Activator;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFModel;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNode;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNodeExecContext;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFNodeStackFrame;
import org.eclipse.tm.internal.tcf.debug.ui.model.TCFRunnable;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.services.IBreakpoints;
import org.eclipse.tm.tcf.services.IRunControl;
import org.eclipse.tm.tcf.services.IRunControl.RunControlContext;
import org.eclipse.tm.tcf.services.IStackTrace.StackTraceContext;
import org.eclipse.tm.tcf.util.TCFDataCache;

/**
 * Drop-to-frame command handler for TCF.
 */
public class DropToFrameCommand implements IDropToFrameHandler {

    private static class StepStateMachine extends TCFActionStepOut {

        private final IDebugCommandRequest monitor;
        private final Runnable done;
        private final TCFNodeExecContext node;
        private final TCFNodeStackFrame frame;

        StepStateMachine(TCFModel model, IDebugCommandRequest monitor,
                IRunControl.RunControlContext ctx, TCFNodeStackFrame frame, Runnable done) {
            super(model.getLaunch(), ctx, false);
            this.monitor = monitor;
            this.done = done;
            this.frame = frame;
            this.node = (TCFNodeExecContext)frame.getParent();
        }

        @Override
        protected TCFDataCache<TCFContextState> getContextState() {
            return node.getState();
        }

        @Override
        protected TCFDataCache<StackTraceContext> getStackFrame() {
            return frame.getStackTraceContext();
        }

        @Override
        protected int getStackFrameIndex() {
            return frame.getFrameNo();
        }

        @Override
        protected TCFDataCache<?> getStackTrace() {
            return node.getStackTrace();
        }

        protected void exit(Throwable error) {
            exit(error, "Drop To Frame");
        }

        @Override
        protected void exit(Throwable error, String reason) {
            if (exited) return;
            super.exit(error, reason);
            if (error != null && node.getChannel().getState() == IChannel.STATE_OPEN) {
                monitor.setStatus(new Status(IStatus.ERROR,
                        Activator.PLUGIN_ID, 0, "Cannot drop to frame: " + error.getLocalizedMessage(), error));
            }
            if (aborted) {
                monitor.setStatus(Status.CANCEL_STATUS);
            }
            done.run();
        }
    }

    private final TCFModel model;

    public DropToFrameCommand(TCFModel tcfModel) {
        model = tcfModel;
    }

    public void canExecute(final IEnabledStateRequest request) {
        final Object[] elements = request.getElements();
        if (elements.length != 1 || !(elements[0] instanceof TCFNodeStackFrame)) {
            request.setEnabled(false);
            request.done();
            return;
        }
        final TCFNodeStackFrame frameNode = (TCFNodeStackFrame) elements[0];
        new TCFRunnable(request) {
            public void run() {
                if (frameNode.getFrameNo() < 1) {
                    request.setEnabled(false);
                    done();
                    return;
                }
                TCFNodeExecContext exeNode = (TCFNodeExecContext) frameNode.getParent();
                TCFDataCache<IRunControl.RunControlContext> ctx_cache = exeNode.getRunContext();
                if (!ctx_cache.validate(this)) {
                    return;
                }
                IRunControl.RunControlContext ctx = ctx_cache.getData();
                if (!canStepOut(ctx)) {
                    request.setEnabled(false);
                    done();
                    return;
                }
                int action_cnt = model.getLaunch().getContextActionsCount(ctx.getID());
                if (action_cnt > 0 || !canStepOut(ctx)) {
                    request.setEnabled(false);
                    done();
                    return;
                }
                TCFDataCache<TCFContextState> state_cache = exeNode.getState();
                if (!state_cache.validate(this)) {
                    return;
                }
                TCFContextState state_data = state_cache.getData();
                request.setEnabled(state_data != null && state_data.is_suspended);
                done();
            }

            private boolean canStepOut(RunControlContext ctx) {
                if (ctx == null) return false;
                if (ctx.canResume(IRunControl.RM_STEP_OUT)) return true;
                if (!ctx.hasState()) return false;
                if (ctx.canResume(IRunControl.RM_RESUME) && model.getLaunch().getService(IBreakpoints.class) != null) return true;
                return false;
            }
        };
    }

    public boolean execute(final IDebugCommandRequest request) {
        final Object[] elements = request.getElements();
        if (elements.length != 1 || !(elements[0] instanceof TCFNodeStackFrame)) {
            request.setStatus(Status.CANCEL_STATUS);
            request.done();
            return false;
        }
        final TCFNodeStackFrame frameNode = (TCFNodeStackFrame) elements[0];
        new TCFRunnable(request) {
            public void run() {
                int frameNo = frameNode.getFrameNo();
                if (frameNo < 1) {
                    request.setStatus(Status.CANCEL_STATUS);
                    done();
                    return;
                }
                TCFNodeExecContext exeNode = (TCFNodeExecContext) frameNode.getParent();
                TCFDataCache<IRunControl.RunControlContext> ctx_cache = exeNode.getRunContext();
                if (!ctx_cache.validate(this)) return;
                TCFDataCache<TCFContextState> state_cache = exeNode.getState();
                if (!state_cache.validate(this)) return;
                TCFContextState state_data = state_cache.getData();
                if (state_data == null || !state_data.is_suspended) {
                    request.setStatus(Status.CANCEL_STATUS);
                    done();
                }
                if (!exeNode.getStackTrace().validate(this)) return;
                Map<String, TCFNode> stack = exeNode.getStackTrace().getData();
                for (TCFNode node : stack.values()) {
                    TCFNodeStackFrame frame_to_step_out = (TCFNodeStackFrame) node;
                    if (frame_to_step_out.getFrameNo() == frameNo - 1) {
                        new StepStateMachine(model, request, ctx_cache.getData(), frame_to_step_out, new Runnable() {
                            public void run() {
                                request.done();
                            }
                        });
                        return;
                    }
                }
                request.setStatus(Status.CANCEL_STATUS);
                done();
            }
        };
        return false;
    }
}

Back to the top