Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1b73dc20ff1055204ecd0be435db07ef55df4b6f (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
package org.eclipse.tm.internal.tcf.debug.actions;

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

import org.eclipse.tm.internal.tcf.debug.model.TCFContextState;
import org.eclipse.tm.internal.tcf.debug.model.TCFLaunch;
import org.eclipse.tm.internal.tcf.debug.model.TCFSourceRef;
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.IRunControl;
import org.eclipse.tm.tcf.services.IStackTrace;
import org.eclipse.tm.tcf.services.IRunControl.RunControlContext;
import org.eclipse.tm.tcf.util.TCFDataCache;

public abstract class TCFActionStepInto extends TCFAction implements IRunControl.RunControlListener {

    private final boolean src_step;
    private final IRunControl rc = launch.getService(IRunControl.class);

    private IRunControl.RunControlContext ctx;
    private TCFDataCache<TCFContextState> state;
    private TCFDataCache<TCFSourceRef> line_info;
    private TCFSourceRef source_ref;
    private BigInteger pc0;
    private BigInteger pc1;
    private int step_cnt;

    protected boolean exited;

    public TCFActionStepInto(TCFLaunch launch, IRunControl.RunControlContext ctx, boolean src_step) {
        super(launch, ctx.getID());
        this.ctx = ctx;
        this.src_step = src_step;
    }

    protected abstract TCFDataCache<TCFContextState> getContextState();
    protected abstract TCFDataCache<TCFSourceRef> getLineInfo();
    protected abstract TCFDataCache<?> getStackTrace();
    protected abstract TCFDataCache<IStackTrace.StackTraceContext> getStackFrame();
    protected abstract int getStackFrameIndex();

    public void run() {
        if (exited) return;
        try {
            runAction();
        }
        catch (Throwable x) {
            exit(x);
        }
    }

    private void runAction() {
        if (state == null) {
            rc.addListener(this);
            state = getContextState();
            if (state == null) {
                exit(new Exception("Invalid context ID"));
                return;
            }
        }
        if (!state.validate(this)) return;
        if (!state.getData().is_suspended) {
            exit(new Exception("Context is not suspended"));
            return;
        }
        if (ctx.canResume(src_step ? IRunControl.RM_STEP_INTO_LINE : IRunControl.RM_STEP_INTO)) {
            if (step_cnt > 0) return;
            ctx.resume(src_step ? IRunControl.RM_STEP_INTO_LINE : IRunControl.RM_STEP_INTO, 1, new IRunControl.DoneCommand() {
                public void doneCommand(IToken token, Exception error) {
                    exit(error);
                }
            });
            step_cnt++;
            return;
        }
        if (!src_step) {
            exit(new Exception("Step into is not supported"));
            return;
        }
        TCFDataCache<?> stack_trace = getStackTrace();
        if (!stack_trace.validate(this)) return;
        if (stack_trace.getData() == null) {
            exit(stack_trace.getError());
            return;
        }
        if (source_ref == null) {
            line_info = getLineInfo();
            if (!line_info.validate(this)) return;
            source_ref = line_info.getData();
            if (source_ref == null) {
                exit(new Exception("Line info not available"));
                return;
            }
            if (source_ref.error != null) {
                exit(source_ref.error);
                return;
            }
            ILineNumbers.CodeArea area = source_ref.area;
            if (area != null) {
                if (area.start_address instanceof BigInteger) pc0 = (BigInteger)area.start_address;
                else if (area.start_address != null) pc0 = new BigInteger(area.start_address.toString());
                if (area.end_address instanceof BigInteger) pc1 = (BigInteger)area.end_address;
                else if (area.end_address != null) pc1 = new BigInteger(area.end_address.toString());
            }
        }
        if (getStackFrameIndex() != 0) {
            // Stepped out of selected function
            exit(null);
            return;
        }
        if (step_cnt > 0) {
            TCFDataCache<IStackTrace.StackTraceContext> frame = getStackFrame();
            if (!frame.validate(this)) return;
            Number addr = frame.getData().getInstructionAddress();
            BigInteger pc = addr instanceof BigInteger ? (BigInteger)addr : new BigInteger(addr.toString());
            if (pc == null || pc0 == null || pc1 == null) {
                exit(null);
                return;
            }
            if (pc.compareTo(pc0) < 0 || pc.compareTo(pc1) >= 0) {
                if (!line_info.validate(this)) return;
                TCFSourceRef ref = line_info.getData();
                if (ref != null && ref.area != null) {
                    if (isSameLine(source_ref.area, ref.area)) {
                        source_ref = ref;
                        ILineNumbers.CodeArea area = source_ref.area;
                        if (area.start_address instanceof BigInteger) pc0 = (BigInteger)area.start_address;
                        else if (area.start_address != null) pc0 = new BigInteger(area.start_address.toString());
                        if (area.end_address instanceof BigInteger) pc1 = (BigInteger)area.end_address;
                        else if (area.end_address != null) pc1 = new BigInteger(area.end_address.toString());
                    }
                    else {
                        exit(null);
                        return;
                    }
                }
            }
        }
        step_cnt++;
        if (ctx.canResume(IRunControl.RM_STEP_INTO)) {
            ctx.resume(IRunControl.RM_STEP_INTO, 1, new IRunControl.DoneCommand() {
                public void doneCommand(IToken token, Exception error) {
                    if (error != null) exit(error);
                }
            });
        }
        else {
            exit(new Exception("Step into is not supported"));
        }
    }

    private boolean isSameLine(ILineNumbers.CodeArea x, ILineNumbers.CodeArea y) {
        if (x == null || y == null) return false;
        if (x.start_line != y.start_line) return false;
        if (x.directory != y.directory && (x.directory == null || !x.directory.equals(y.directory))) return false;
        if (x.file != y.file && (x.file == null || !x.file.equals(y.file))) return false;
        return true;
    }

    protected void exit(Throwable error) {
        if (exited) return;
        rc.removeListener(this);
        exited = true;
        done("Step");
    }

    public void containerResumed(String[] context_ids) {
    }

    public void containerSuspended(String context, String pc,
            String reason, Map<String, Object> params,
            String[] suspended_ids) {
        for (String id : suspended_ids) {
            if (!id.equals(context)) contextSuspended(id, null, null, null);
        }
        contextSuspended(context, pc, reason, params);
    }

    public void contextAdded(RunControlContext[] contexts) {
    }

    public void contextChanged(RunControlContext[] contexts) {
        for (RunControlContext c : contexts) {
            if (c.getID().equals(ctx.getID())) ctx = c;
        }
    }

    public void contextException(String context, String msg) {
        if (context.equals(ctx.getID())) exit(new Exception(msg));
    }

    public void contextRemoved(String[] context_ids) {
        for (String context : context_ids) {
            if (context.equals(ctx.getID())) exit(null);
        }
    }

    public void contextResumed(String context) {
    }

    public void contextSuspended(String context, String pc, String reason,
            Map<String, Object> params) {
        if (!context.equals(ctx.getID())) return;
        if (IRunControl.REASON_STEP.equals(reason)) {
            Protocol.invokeLater(this);
        }
        else {
            exit(null);
        }
    }
}

Back to the top