~vil/pydev/upstream

« back to all changes in this revision

Viewing changes to org.python.pydev.debug/src/org/python/pydev/debug/model/PyThread.java

  • Committer: Vladimír Lapáček
  • Date: 2006-08-30 18:38:44 UTC
  • Revision ID: vladimir.lapacek@gmail.com-20060830183844-f4d82c1239a7770a
Initial import of upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Author: atotic
 
3
 * Created on Apr 21, 2004
 
4
 * License: Common Public License v1.0
 
5
 */
 
6
package org.python.pydev.debug.model;
 
7
 
 
8
import org.eclipse.core.resources.IResource;
 
9
import org.eclipse.core.runtime.Platform;
 
10
import org.eclipse.core.runtime.PlatformObject;
 
11
import org.eclipse.debug.core.DebugException;
 
12
import org.eclipse.debug.core.ILaunch;
 
13
import org.eclipse.debug.core.model.IBreakpoint;
 
14
import org.eclipse.debug.core.model.IDebugTarget;
 
15
import org.eclipse.debug.core.model.IStackFrame;
 
16
import org.eclipse.debug.core.model.IThread;
 
17
import org.eclipse.ui.views.properties.IPropertySource;
 
18
import org.eclipse.ui.views.tasklist.ITaskListResourceAdapter;
 
19
import org.python.pydev.debug.model.remote.AbstractDebuggerCommand;
 
20
import org.python.pydev.debug.model.remote.AbstractRemoteDebugger;
 
21
import org.python.pydev.debug.model.remote.StepCommand;
 
22
import org.python.pydev.debug.model.remote.ThreadRunCommand;
 
23
import org.python.pydev.debug.model.remote.ThreadSuspendCommand;
 
24
import org.python.pydev.plugin.PydevPlugin;
 
25
 
 
26
/**
 
27
 * Represents python threads.
 
28
 * Stack global variables are associated with threads.
 
29
 */
 
30
public class PyThread extends PlatformObject implements IThread {
 
31
 
 
32
        private AbstractDebugTarget target;
 
33
        private String name;
 
34
        private String id;
 
35
    
 
36
    /**
 
37
     * true if this is a debugger thread, that can't be killed/suspended
 
38
     */
 
39
        private boolean isPydevThread;
 
40
 
 
41
        private boolean isSuspended = false;
 
42
        private boolean isStepping = false;
 
43
        private IStackFrame[] stack;
 
44
        
 
45
        public PyThread(AbstractDebugTarget target, String name, String id) {
 
46
                this.target = target;
 
47
                this.name = name;
 
48
                this.id = id;
 
49
                isPydevThread = id.equals("-1");        // use a special id for pydev threads
 
50
        }
 
51
 
 
52
        /**
 
53
         * If a thread is entering a suspended state, pass in the stack
 
54
         */
 
55
        public void setSuspended(boolean state, IStackFrame[] stack) {
 
56
                isSuspended = state;
 
57
                this.stack = stack;
 
58
        }
 
59
 
 
60
        public String getName() throws DebugException {
 
61
                return name;
 
62
        }
 
63
        
 
64
        public String getId() {
 
65
                return id;
 
66
        }
 
67
        
 
68
        public boolean isPydevThread() {
 
69
                return isPydevThread;
 
70
        }
 
71
 
 
72
        public int getPriority() throws DebugException {
 
73
                return 0;
 
74
        }
 
75
 
 
76
        public String getModelIdentifier() {
 
77
                return target.getModelIdentifier();
 
78
        }
 
79
 
 
80
        public IDebugTarget getDebugTarget() {
 
81
                return target;
 
82
        }
 
83
 
 
84
        public ILaunch getLaunch() {
 
85
                return target.getLaunch();
 
86
        }
 
87
 
 
88
        public boolean canTerminate() {
 
89
                return !isPydevThread;
 
90
        }
 
91
 
 
92
        public boolean isTerminated() {
 
93
                return target.isTerminated();
 
94
        }
 
95
 
 
96
        public void terminate() throws DebugException {
 
97
                target.terminate();
 
98
        }
 
99
 
 
100
        public boolean canResume() {
 
101
                return !isPydevThread && isSuspended;
 
102
        }
 
103
 
 
104
        public boolean canSuspend() {
 
105
                return !isPydevThread && !isSuspended;
 
106
        }
 
107
 
 
108
        public boolean isSuspended() {
 
109
                return isSuspended;
 
110
        }
 
111
 
 
112
        public void resume() throws DebugException {
 
113
                if (!isPydevThread) {
 
114
                        stack = null;
 
115
                        isStepping = false;
 
116
                        //RemoteDebugger d = target.getDebugger();
 
117
                        AbstractRemoteDebugger d = target.getDebugger();
 
118
                        if(d != null){
 
119
                                d.postCommand(new ThreadRunCommand(d, id));
 
120
                        }else{//no debugger?
 
121
                                PydevPlugin.log("Terminating: No debugger in target when resuming.");
 
122
                                terminate();
 
123
                        }
 
124
                }
 
125
        }
 
126
 
 
127
        public void suspend() throws DebugException {
 
128
                if (!isPydevThread) {
 
129
                        stack = null;
 
130
                        AbstractRemoteDebugger d = target.getDebugger();
 
131
                        d.postCommand(new ThreadSuspendCommand(d, id));
 
132
                }
 
133
        }
 
134
 
 
135
        public boolean canStepInto() {
 
136
                return !isPydevThread && isSuspended;
 
137
        }
 
138
 
 
139
        public boolean canStepOver() {
 
140
                return !isPydevThread && isSuspended;
 
141
        }
 
142
 
 
143
        public boolean canStepReturn() {
 
144
                return !isPydevThread && isSuspended;
 
145
        }
 
146
 
 
147
        public boolean isStepping() {
 
148
                return isStepping;
 
149
        }
 
150
 
 
151
        public void stepInto() throws DebugException {
 
152
                if (!isPydevThread) {
 
153
                        isStepping = true;
 
154
                        AbstractRemoteDebugger d = target.getDebugger();
 
155
                        d.postCommand(new StepCommand(d, AbstractDebuggerCommand.CMD_STEP_INTO, id));
 
156
                }               
 
157
        }
 
158
 
 
159
        public void stepOver() throws DebugException {
 
160
                if (!isPydevThread) {
 
161
                        isStepping = true;
 
162
                        AbstractRemoteDebugger d = target.getDebugger();
 
163
                        d.postCommand(new StepCommand(d, AbstractDebuggerCommand.CMD_STEP_OVER, id));
 
164
                }               
 
165
        }
 
166
 
 
167
        public void stepReturn() throws DebugException {
 
168
                if (!isPydevThread) {
 
169
                        isStepping = true;
 
170
                        AbstractRemoteDebugger d = target.getDebugger();
 
171
                        d.postCommand(new StepCommand(d, AbstractDebuggerCommand.CMD_STEP_RETURN, id));
 
172
                }               
 
173
        }
 
174
 
 
175
        public IStackFrame[] getStackFrames() throws DebugException {
 
176
        if(isSuspended && stack != null){
 
177
            return stack;
 
178
        }
 
179
        return new IStackFrame[0];
 
180
        }
 
181
 
 
182
        public boolean hasStackFrames() throws DebugException {
 
183
                return (stack != null && stack.length > 0);
 
184
        }
 
185
 
 
186
        public IStackFrame getTopStackFrame() throws DebugException {
 
187
                return stack == null ? null : stack[0];
 
188
        }
 
189
 
 
190
        public PyStackFrame findStackFrameByID(String id) {
 
191
                if (stack != null) {
 
192
            
 
193
                        for (int i=0; i<stack.length; i++){
 
194
                
 
195
                                if (id.equals(((PyStackFrame)stack[i]).getId())){
 
196
                    
 
197
                                        return (PyStackFrame)stack[i];
 
198
                }
 
199
            }
 
200
        }
 
201
                return null;
 
202
        }
 
203
 
 
204
        public IBreakpoint[] getBreakpoints() {
 
205
                // should return breakpoint that caused this thread to suspend
 
206
                // not implementing this seems to cause no harm
 
207
                PyBreakpoint[] breaks = new PyBreakpoint[0];
 
208
                return breaks;
 
209
        }
 
210
 
 
211
        public Object getAdapter(Class adapter) {
 
212
                if (adapter.equals(ILaunch.class) ||
 
213
                        adapter.equals(IResource.class))
 
214
                        return target.getAdapter(adapter);
 
215
                else if (adapter.equals(ITaskListResourceAdapter.class))
 
216
                        return null;
 
217
                else if (adapter.equals(IPropertySource.class) 
 
218
                                || adapter.equals(ITaskListResourceAdapter.class)
 
219
                                || adapter.equals(org.eclipse.debug.ui.actions.IToggleBreakpointsTarget.class)
 
220
                                || adapter.equals(org.eclipse.debug.ui.actions.IRunToLineTarget.class)
 
221
                                || adapter.equals(org.eclipse.ui.IContributorResourceAdapter.class)
 
222
                                || adapter.equals(org.eclipse.ui.model.IWorkbenchAdapter.class)
 
223
                                || adapter.equals(org.eclipse.ui.IActionFilter.class)
 
224
                                ) 
 
225
                        return  super.getAdapter(adapter);
 
226
                else {
 
227
//                      System.err.println("PythonThread Need adapter " + adapter.toString());
 
228
                        Platform.getAdapterManager().getAdapter(this, adapter);
 
229
                }
 
230
                // ongoing, I do not fully understand all the interfaces they'd like me to support
 
231
                return super.getAdapter(adapter);
 
232
        }
 
233
 
 
234
}