FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonVSCode/src/client/debugger/PythonProcess.ts at master · rmagee/pythonVSCode · GitHub
rmagee
/
pythonVSCode
Public
forked from
DonJayamanne/pythonVSCode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonVSCode
/
src
/
client
/
debugger
/
PythonProcess.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
388 lines (355 loc) · 14.7 KB
Breadcrumbs
pythonVSCode
/
src
/
client
/
debugger
/
PythonProcess.ts
Copy path
File metadata and controls
388 lines (355 loc) · 14.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"use strict"
;
import
*
as
net
from
"net"
;
import
{
EventEmitter
}
from
"events"
;
import
{
FrameKind
,
IPythonProcess
,
IPythonThread
,
IPythonModule
,
IPythonEvaluationResult
,
IPythonStackFrame
,
IStepCommand
}
from
"./Common/Contracts"
;
import
{
IPythonBreakpoint
,
PythonBreakpointConditionKind
,
PythonBreakpointPassCountKind
,
IBreakpointCommand
,
IChildEnumCommand
}
from
"./Common/Contracts"
;
import
{
PythonEvaluationResultReprKind
,
IExecutionCommand
,
enum_EXCEPTION_STATE
}
from
"./Common/Contracts"
;
import
{
Commands
}
from
"./ProxyCommands"
;
import
{
IdDispenser
}
from
"../common/idDispenser"
;
import
{
PythonProcessCallbackHandler
}
from
"./PythonProcessCallbackHandler"
;
import
{
SocketStream
}
from
"../common/comms/SocketStream"
;
export
class
PythonProcess
extends
EventEmitter
implements
IPythonProcess
{
private
id
:
number
;
public
get
Id
(
)
:
number
{
return
this
.
id
;
}
private
guid
:
string
;
public
get
Guid
(
)
:
string
{
return
this
.
guid
;
}
private
hasExited
:
boolean
;
public
get
HasExited
(
)
:
boolean
{
return
this
.
hasExited
;
}
private
_mainThread
:
IPythonThread
;
public
get
MainThread
(
)
:
IPythonThread
{
return
this
.
_mainThread
;
}
private
_lastExecutedThread
:
IPythonThread
;
public
get
LastExecutedThread
(
)
:
IPythonThread
{
return
this
.
_lastExecutedThread
;
}
private
_idDispenser
:
IdDispenser
;
private
_threads
:
Map
<
number
,
IPythonThread
>
;
public
get
Threads
(
)
:
Map
<
number
,
IPythonThread
>
{
return
this
.
_threads
;
}
public
PendingChildEnumCommands
:
Map
<
number
,
IChildEnumCommand
>
;
public
PendingExecuteCommands
:
Map
<
number
,
IExecutionCommand
>
;
private
executeCommandsQueue
:
IExecutionCommand
[
]
;
private
callbackHandler
:
PythonProcessCallbackHandler
;
private
stream
:
SocketStream
=
null
;
private
programDirectory
:
string
;
public
get
ProgramDirectory
(
)
:
string
{
return
this
.
programDirectory
;
}
constructor
(
id
:
number
,
guid
:
string
,
programDirectory
:
string
)
{
super
(
)
;
this
.
id
=
id
;
this
.
guid
=
guid
;
this
.
_threads
=
new
Map
<
number
,
IPythonThread
>
(
)
;
this
.
_idDispenser
=
new
IdDispenser
(
)
;
this
.
PendingChildEnumCommands
=
new
Map
<
number
,
IChildEnumCommand
>
(
)
;
this
.
PendingExecuteCommands
=
new
Map
<
number
,
IExecutionCommand
>
(
)
;
this
.
programDirectory
=
programDirectory
;
this
.
executeCommandsQueue
=
[
]
;
}
public
Kill
(
)
{
if
(
!
this
.
isRemoteProcess
&&
this
.
pid
&&
typeof
this
.
pid
===
"number"
)
{
try
{
let
kill
=
require
(
"tree-kill"
)
;
kill
(
this
.
pid
)
;
this
.
pid
=
null
;
}
catch
(
ex
)
{
}
}
}
public
Terminate
(
)
{
this
.
stream
.
Write
(
Commands
.
ExitCommandBytes
)
;
}
public
Detach
(
)
{
this
.
stream
.
Write
(
Commands
.
DetachCommandBytes
)
;
}
private
guidRead
:
boolean
;
private
statusRead
:
boolean
;
private
pidRead
:
boolean
;
private
pid
:
number
;
private
isRemoteProcess
:
Boolean
;
public
Connect
(
buffer
:
Buffer
,
socket
:
net
.
Socket
,
isRemoteProcess
:
boolean
=
false
)
:
boolean
{
this
.
isRemoteProcess
=
isRemoteProcess
;
if
(
this
.
stream
===
null
)
{
this
.
stream
=
new
SocketStream
(
socket
,
buffer
)
;
}
else
{
this
.
stream
.
Append
(
buffer
)
;
}
if
(
!
isRemoteProcess
)
{
if
(
!
this
.
guidRead
)
{
this
.
stream
.
BeginTransaction
(
)
;
let
guid
=
this
.
stream
.
ReadString
(
)
;
if
(
this
.
stream
.
HasInsufficientDataForReading
)
{
this
.
stream
.
RollBackTransaction
(
)
;
return
false
;
}
this
.
guidRead
=
true
;
this
.
stream
.
EndTransaction
(
)
;
}
if
(
!
this
.
statusRead
)
{
this
.
stream
.
BeginTransaction
(
)
;
let
result
=
this
.
stream
.
ReadInt32
(
)
;
if
(
this
.
stream
.
HasInsufficientDataForReading
)
{
this
.
stream
.
RollBackTransaction
(
)
;
return
false
;
}
this
.
statusRead
=
true
;
this
.
stream
.
EndTransaction
(
)
;
}
if
(
!
this
.
pidRead
)
{
this
.
stream
.
BeginTransaction
(
)
;
this
.
pid
=
this
.
stream
.
ReadInt32
(
)
;
if
(
this
.
stream
.
HasInsufficientDataForReading
)
{
this
.
stream
.
RollBackTransaction
(
)
;
return
false
;
}
this
.
pidRead
=
true
;
this
.
stream
.
EndTransaction
(
)
;
}
}
this
.
callbackHandler
=
new
PythonProcessCallbackHandler
(
this
,
this
.
stream
,
this
.
_idDispenser
)
;
this
.
callbackHandler
.
on
(
"detach"
,
(
)
=>
this
.
emit
(
"detach"
)
)
;
this
.
callbackHandler
.
on
(
"last"
,
(
)
=>
this
.
emit
(
"last"
)
)
;
this
.
callbackHandler
.
on
(
"moduleLoaded"
,
arg
=>
this
.
emit
(
"moduleLoaded"
,
arg
)
)
;
this
.
callbackHandler
.
on
(
"asyncBreakCompleted"
,
arg
=>
this
.
emit
(
"asyncBreakCompleted"
,
arg
)
)
;
this
.
callbackHandler
.
on
(
"threadCreated"
,
arg
=>
this
.
emit
(
"threadCreated"
,
arg
)
)
;
this
.
callbackHandler
.
on
(
"threadExited"
,
arg
=>
this
.
emit
(
"threadExited"
,
arg
)
)
;
this
.
callbackHandler
.
on
(
"stepCompleted"
,
arg
=>
this
.
onPythonStepCompleted
(
arg
)
)
;
this
.
callbackHandler
.
on
(
"breakpointSet"
,
arg
=>
this
.
onBreakpointSet
(
arg
,
true
)
)
;
this
.
callbackHandler
.
on
(
"breakpointNotSet"
,
arg
=>
this
.
onBreakpointSet
(
arg
,
false
)
)
;
this
.
callbackHandler
.
on
(
"output"
,
(
pyThread
,
output
)
=>
this
.
emit
(
"output"
,
pyThread
,
output
)
)
;
this
.
callbackHandler
.
on
(
"exceptionRaised"
,
(
pyThread
,
ex
,
brkType
)
=>
{
this
.
_lastExecutedThread
=
pyThread
;
this
.
emit
(
"exceptionRaised"
,
pyThread
,
ex
,
brkType
)
;
}
)
;
this
.
callbackHandler
.
on
(
"breakpointHit"
,
(
pyThread
,
breakpointId
)
=>
this
.
onBreakpointHit
(
pyThread
,
breakpointId
)
)
;
this
.
callbackHandler
.
on
(
"processLoaded"
,
arg
=>
{
this
.
_mainThread
=
<
IPythonThread
>
arg
;
this
.
_lastExecutedThread
=
this
.
_mainThread
;
this
.
emit
(
"processLoaded"
,
arg
)
;
}
)
;
this
.
callbackHandler
.
HandleIncomingData
(
)
;
return
true
;
}
public
HandleIncomingData
(
buffer
:
Buffer
)
{
this
.
stream
.
Append
(
buffer
)
;
if
(
!
this
.
isRemoteProcess
)
{
if
(
!
this
.
guidRead
)
{
this
.
stream
.
RollBackTransaction
(
)
;
let
guid
=
this
.
stream
.
ReadString
(
)
;
if
(
this
.
stream
.
HasInsufficientDataForReading
)
{
return
;
}
this
.
guidRead
=
true
;
this
.
stream
.
EndTransaction
(
)
;
}
if
(
!
this
.
statusRead
)
{
this
.
stream
.
BeginTransaction
(
)
;
let
result
=
this
.
stream
.
ReadInt32
(
)
;
if
(
this
.
stream
.
HasInsufficientDataForReading
)
{
this
.
stream
.
RollBackTransaction
(
)
;
return
;
}
this
.
statusRead
=
true
;
this
.
stream
.
EndTransaction
(
)
;
}
if
(
!
this
.
pidRead
)
{
this
.
stream
.
BeginTransaction
(
)
;
this
.
pid
=
this
.
stream
.
ReadInt32
(
)
;
if
(
this
.
stream
.
HasInsufficientDataForReading
)
{
this
.
stream
.
RollBackTransaction
(
)
;
return
;
}
this
.
pidRead
=
true
;
this
.
stream
.
EndTransaction
(
)
;
}
}
this
.
callbackHandler
.
HandleIncomingData
(
)
;
}
// #region Step Commands
private
onPythonStepCompleted
(
pyThread
:
IPythonThread
)
{
this
.
_lastExecutedThread
=
pyThread
;
this
.
emit
(
"stepCompleted"
,
pyThread
)
;
}
private
sendStepCommand
(
threadId
:
number
,
command
:
Buffer
)
{
this
.
stream
.
Write
(
command
)
;
this
.
stream
.
WriteInt64
(
threadId
)
;
}
public
SendExceptionInfo
(
defaultBreakOnMode
:
enum_EXCEPTION_STATE
,
breakOn
:
Map
<
string
,
enum_EXCEPTION_STATE
>
)
{
this
.
stream
.
Write
(
Commands
.
SetExceptionInfoCommandBytes
)
;
this
.
stream
.
WriteInt32
(
defaultBreakOnMode
)
;
if
(
breakOn
===
null
||
breakOn
===
undefined
)
{
this
.
stream
.
WriteInt32
(
0
)
;
}
else
{
this
.
stream
.
WriteInt32
(
breakOn
.
size
)
;
breakOn
.
forEach
(
(
value
,
key
)
=>
{
this
.
stream
.
WriteInt32
(
value
)
;
this
.
stream
.
WriteString
(
key
)
;
}
)
;
}
}
public
SendStepOver
(
threadId
:
number
)
{
return
this
.
sendStepCommand
(
threadId
,
Commands
.
StepOverCommandBytes
)
;
}
public
SendStepOut
(
threadId
:
number
)
{
return
this
.
sendStepCommand
(
threadId
,
Commands
.
StepOutCommandBytes
)
;
}
public
SendStepInto
(
threadId
:
number
)
{
return
this
.
sendStepCommand
(
threadId
,
Commands
.
StepIntoCommandBytes
)
;
}
// #endregion
private
onBreakpointHit
(
pyThread
:
IPythonThread
,
breakpointId
:
number
)
{
this
.
_lastExecutedThread
=
pyThread
;
this
.
emit
(
"breakpointHit"
,
pyThread
,
breakpointId
)
;
}
private
onBreakpointSet
(
breakpointId
:
number
,
success
:
boolean
)
{
// Find the last breakpoint command associated with this breakpoint
let
index
=
this
.
breakpointCommands
.
findIndex
(
cmd
=>
cmd
.
Id
===
breakpointId
)
;
if
(
index
===
-
1
)
{
// Hmm this is not possible, log this exception and carry on
this
.
emit
(
"error"
,
"command.breakpoint.hit"
,
`Uknown Breakpoit Id
${
breakpointId
}
`
)
;
return
;
}
let
cmd
=
this
.
breakpointCommands
.
splice
(
index
,
1
)
[
0
]
;
if
(
success
)
{
cmd
.
PromiseResolve
(
)
;
}
else
{
cmd
.
PromiseReject
(
)
;
}
}
private
breakpointCommands
:
IBreakpointCommand
[
]
=
[
]
;
public
DisableBreakPoint
(
breakpoint
:
IPythonBreakpoint
)
{
if
(
breakpoint
.
IsDjangoBreakpoint
)
{
this
.
stream
.
Write
(
Commands
.
RemoveDjangoBreakPointCommandBytes
)
;
}
else
{
this
.
stream
.
Write
(
Commands
.
RemoveBreakPointCommandBytes
)
;
}
this
.
stream
.
WriteInt32
(
breakpoint
.
LineNo
)
;
this
.
stream
.
WriteInt32
(
breakpoint
.
Id
)
;
if
(
breakpoint
.
IsDjangoBreakpoint
)
{
this
.
stream
.
WriteString
(
breakpoint
.
Filename
)
;
}
}
public
BindBreakpoint
(
brkpoint
:
IPythonBreakpoint
)
:
Promise
<
any
>
{
return
new
Promise
<
IPythonThread
>
(
(
resolve
,
reject
)
=>
{
let
bkCmd
:
IBreakpointCommand
=
{
Id
:
brkpoint
.
Id
,
PromiseResolve
:
resolve
,
PromiseReject
:
reject
}
;
this
.
breakpointCommands
.
push
(
bkCmd
)
;
if
(
brkpoint
.
IsDjangoBreakpoint
)
{
this
.
stream
.
Write
(
Commands
.
AddDjangoBreakPointCommandBytes
)
;
}
else
{
this
.
stream
.
Write
(
Commands
.
SetBreakPointCommandBytes
)
;
}
this
.
stream
.
WriteInt32
(
brkpoint
.
Id
)
;
this
.
stream
.
WriteInt32
(
brkpoint
.
LineNo
)
;
this
.
stream
.
WriteString
(
brkpoint
.
Filename
)
;
if
(
brkpoint
.
IsDjangoBreakpoint
)
{
// Bining django breakpoints don't return any responses
// Assume it worked
resolve
(
)
;
}
else
{
this
.
SendCondition
(
brkpoint
)
;
this
.
SendPassCount
(
brkpoint
)
;
}
}
)
;
}
private
SendCondition
(
breakpoint
:
IPythonBreakpoint
)
{
this
.
stream
.
WriteInt32
(
<
number
>
breakpoint
.
ConditionKind
)
;
this
.
stream
.
WriteString
(
breakpoint
.
Condition
||
""
)
;
}
private
SendPassCount
(
breakpoint
:
IPythonBreakpoint
)
{
// DebugWriteCommand("Send BP pass count");
this
.
stream
.
WriteInt32
(
<
number
>
breakpoint
.
PassCountKind
)
;
this
.
stream
.
WriteInt32
(
breakpoint
.
PassCount
)
;
}
public
SendResumeThread
(
threadId
:
number
)
{
return
this
.
sendStepCommand
(
threadId
,
Commands
.
ResumeThreadCommandBytes
)
;
}
public
SendContinue
(
)
:
Promise
<
IPythonThread
>
{
return
new
Promise
<
IPythonThread
>
(
resolve
=>
{
this
.
stream
.
Write
(
Commands
.
ResumeAllCommandBytes
)
;
resolve
(
)
;
}
)
;
}
public
AutoResumeThread
(
threadId
:
number
)
{
}
public
SendClearStepping
(
threadId
:
number
)
{
}
public
Break
(
)
{
this
.
stream
.
Write
(
Commands
.
BreakAllCommandBytes
)
;
}
public
ExecuteText
(
text
:
string
,
reprKind
:
PythonEvaluationResultReprKind
,
stackFrame
:
IPythonStackFrame
)
:
Promise
<
IPythonEvaluationResult
>
{
return
new
Promise
<
IPythonEvaluationResult
>
(
(
resolve
,
reject
)
=>
{
let
executeId
=
this
.
_idDispenser
.
Allocate
(
)
;
let
cmd
:
IExecutionCommand
=
{
Id
:
executeId
,
Text
:
text
,
Frame
:
stackFrame
,
PromiseResolve
:
resolve
,
PromiseReject
:
reject
,
ReprKind
:
reprKind
}
;
this
.
executeCommandsQueue
.
push
(
cmd
)
;
this
.
ProcessPendingExecuteCommands
(
)
;
}
)
;
}
public
ProcessPendingExecuteCommands
(
)
{
if
(
this
.
executeCommandsQueue
.
length
===
0
||
this
.
PendingExecuteCommands
.
size
>
0
)
{
return
;
}
const
cmd
=
this
.
executeCommandsQueue
.
shift
(
)
;
this
.
PendingExecuteCommands
.
set
(
cmd
.
Id
,
cmd
)
;
this
.
stream
.
Write
(
Commands
.
ExecuteTextCommandBytes
)
;
this
.
stream
.
WriteString
(
cmd
.
Text
)
;
this
.
stream
.
WriteInt64
(
cmd
.
Frame
.
Thread
.
Id
)
;
this
.
stream
.
WriteInt32
(
cmd
.
Frame
.
FrameId
)
;
this
.
stream
.
WriteInt32
(
cmd
.
Id
)
;
this
.
stream
.
WriteInt32
(
<
number
>
cmd
.
Frame
.
Kind
)
;
this
.
stream
.
WriteInt32
(
<
number
>
cmd
.
ReprKind
)
;
}
public
EnumChildren
(
text
:
string
,
stackFrame
:
IPythonStackFrame
,
timeout
:
number
)
:
Promise
<
IPythonEvaluationResult
[
]
>
{
return
new
Promise
<
IPythonEvaluationResult
[
]
>
(
(
resolve
,
reject
)
=>
{
let
executeId
=
this
.
_idDispenser
.
Allocate
(
)
;
if
(
typeof
(
executeId
)
!==
"number"
)
{
let
y
=
""
;
}
let
cmd
:
IChildEnumCommand
=
{
Id
:
executeId
,
Frame
:
stackFrame
,
PromiseResolve
:
resolve
,
PromiseReject
:
reject
}
;
this
.
PendingChildEnumCommands
.
set
(
executeId
,
cmd
)
;
setTimeout
(
(
)
=>
{
if
(
this
.
PendingChildEnumCommands
.
has
(
executeId
)
)
{
this
.
PendingChildEnumCommands
.
delete
(
executeId
)
;
}
let
seconds
=
timeout
/
1000
;
reject
(
`Enumerating children for
${
text
}
timed out after
${
seconds
}
seconds.`
)
;
}
,
timeout
)
;
this
.
stream
.
Write
(
Commands
.
GetChildrenCommandBytes
)
;
this
.
stream
.
WriteString
(
text
)
;
this
.
stream
.
WriteInt64
(
stackFrame
.
Thread
.
Id
)
;
this
.
stream
.
WriteInt32
(
stackFrame
.
FrameId
)
;
this
.
stream
.
WriteInt32
(
executeId
)
;
this
.
stream
.
WriteInt32
(
stackFrame
.
Kind
)
;
}
)
;
}
public
SetLineNumber
(
pythonStackFrame
:
IPythonStackFrame
,
lineNo
:
number
)
{
}
}
Back
|
FazBrowse Home
|
New Git URL