FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
vscode-java-debug/src/processTree.ts at main · l-git/vscode-java-debug · GitHub
l-git
/
vscode-java-debug
Public
forked from
microsoft/vscode-java-debug
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
vscode-java-debug
/
src
/
processTree.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
201 lines (169 loc) · 6.24 KB
Breadcrumbs
vscode-java-debug
/
src
/
processTree.ts
Copy path
File metadata and controls
201 lines (169 loc) · 6.24 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*
* Copied from https://github.com/microsoft/vscode-node-debug/blob/master/src/node/extension/processTree.ts
*--------------------------------------------------------------------------------------------*/
/* tslint:disable */
'use strict'
;
import
{
spawn
,
ChildProcessWithoutNullStreams
}
from
'child_process'
;
import
{
join
}
from
'path'
;
export
class
ProcessTreeNode
{
children
?:
ProcessTreeNode
[
]
;
constructor
(
public
pid
:
number
,
public
ppid
:
number
,
public
command
:
string
,
public
args
:
string
)
{
}
}
export
async
function
getProcessTree
(
rootPid
:
number
)
:
Promise
<
ProcessTreeNode
|
undefined
>
{
const
map
=
new
Map
<
number
,
ProcessTreeNode
>
(
)
;
map
.
set
(
0
,
new
ProcessTreeNode
(
0
,
0
,
'???'
,
''
)
)
;
try
{
await
getProcesses
(
(
pid
:
number
,
ppid
:
number
,
command
:
string
,
args
:
string
)
=>
{
if
(
pid
!==
ppid
)
{
map
.
set
(
pid
,
new
ProcessTreeNode
(
pid
,
ppid
,
command
,
args
)
)
;
}
}
)
;
}
catch
(
err
)
{
return
undefined
;
}
const
values
=
map
.
values
(
)
;
for
(
const
p
of
values
)
{
const
parent
=
map
.
get
(
p
.
ppid
)
;
if
(
parent
&&
parent
!==
p
)
{
if
(
!
parent
.
children
)
{
parent
.
children
=
[
]
;
}
parent
.
children
.
push
(
p
)
;
}
}
if
(
!
isNaN
(
rootPid
)
&&
rootPid
>
0
)
{
return
map
.
get
(
rootPid
)
;
}
return
map
.
get
(
0
)
;
}
export
function
getProcesses
(
one
:
(
pid
:
number
,
ppid
:
number
,
command
:
string
,
args
:
string
,
date
?:
number
)
=>
void
)
:
Promise
<
void
>
{
// returns a function that aggregates chunks of data until one or more complete lines are received and passes them to a callback.
function
lines
(
callback
:
(
a
:
string
)
=>
void
)
{
let
unfinished
=
''
;
// unfinished last line of chunk
return
(
data
:
string
|
Buffer
)
=>
{
const
lines
=
data
.
toString
(
)
.
split
(
/
\r
?
\n
/
)
;
const
finishedLines
=
lines
.
slice
(
0
,
lines
.
length
-
1
)
;
finishedLines
[
0
]
=
unfinished
+
finishedLines
[
0
]
;
// complete previous unfinished line
unfinished
=
lines
[
lines
.
length
-
1
]
;
// remember unfinished last line of this chunk for next round
for
(
const
s
of
finishedLines
)
{
callback
(
s
)
;
}
}
;
}
return
new
Promise
(
(
resolve
,
reject
)
=>
{
let
proc
:
ChildProcessWithoutNullStreams
;
if
(
process
.
platform
===
'win32'
)
{
// attributes columns are in alphabetic order!
const
CMD_PAT
=
/
^
(
.
*
)
\s
+
(
[
0
-
9
]
+
)
\.
[
0
-
9
]
+
[
+
-
]
[
0
-
9
]
+
\s
+
(
[
0
-
9
]
+
)
\s
+
(
[
0
-
9
]
+
)
$
/
;
const
wmic
=
join
(
process
.
env
[
'WINDIR'
]
||
'C:\\Windows'
,
'System32'
,
'wbem'
,
'WMIC.exe'
)
;
proc
=
spawn
(
wmic
,
[
'process'
,
'get'
,
'CommandLine,CreationDate,ParentProcessId,ProcessId'
]
)
;
proc
.
stdout
.
setEncoding
(
'utf8'
)
;
proc
.
stdout
.
on
(
'data'
,
lines
(
line
=>
{
let
matches
=
CMD_PAT
.
exec
(
line
.
trim
(
)
)
;
if
(
matches
&&
matches
.
length
===
5
)
{
const
pid
=
Number
(
matches
[
4
]
)
;
const
ppid
=
Number
(
matches
[
3
]
)
;
const
date
=
Number
(
matches
[
2
]
)
;
let
args
=
matches
[
1
]
.
trim
(
)
;
if
(
!
isNaN
(
pid
)
&&
!
isNaN
(
ppid
)
&&
args
)
{
let
command
=
args
;
if
(
args
[
0
]
===
'"'
)
{
const
end
=
args
.
indexOf
(
'"'
,
1
)
;
if
(
end
>
0
)
{
command
=
args
.
substr
(
1
,
end
-
1
)
;
args
=
args
.
substr
(
end
+
2
)
;
}
}
else
{
const
end
=
args
.
indexOf
(
' '
)
;
if
(
end
>
0
)
{
command
=
args
.
substr
(
0
,
end
)
;
args
=
args
.
substr
(
end
+
1
)
;
}
else
{
args
=
''
;
}
}
one
(
pid
,
ppid
,
command
,
args
,
date
)
;
}
}
}
)
)
;
}
else
if
(
process
.
platform
===
'darwin'
)
{
// OS X
proc
=
spawn
(
'/bin/ps'
,
[
'-x'
,
'-o'
,
`pid,ppid,comm=
${
'a'
.
repeat
(
256
)
}
,command`
]
)
;
proc
.
stdout
.
setEncoding
(
'utf8'
)
;
proc
.
stdout
.
on
(
'data'
,
lines
(
line
=>
{
const
pid
=
Number
(
line
.
substr
(
0
,
5
)
)
;
const
ppid
=
Number
(
line
.
substr
(
6
,
5
)
)
;
const
command
=
line
.
substr
(
12
,
256
)
.
trim
(
)
;
const
args
=
line
.
substr
(
269
+
command
.
length
)
;
if
(
!
isNaN
(
pid
)
&&
!
isNaN
(
ppid
)
)
{
one
(
pid
,
ppid
,
command
,
args
)
;
}
}
)
)
;
}
else
{
// linux
proc
=
spawn
(
'/bin/ps'
,
[
'-ax'
,
'-o'
,
'pid:6,ppid:6,comm:20,command'
]
)
;
// we specify the column width explicitly
proc
.
stdout
.
setEncoding
(
'utf8'
)
;
proc
.
stdout
.
on
(
'data'
,
lines
(
line
=>
{
// the following substr arguments must match the column width specified for the "ps" command above
// regular substr is deprecated
const
pid
=
Number
(
substr
(
line
,
0
,
6
)
)
;
const
ppid
=
Number
(
substr
(
line
,
7
,
6
)
)
;
const
shortName
=
substr
(
line
,
14
,
20
)
.
trim
(
)
const
fullCommand
=
substr
(
line
,
35
)
let
command
=
shortName
;
let
args
=
fullCommand
;
const
pos
=
fullCommand
.
indexOf
(
shortName
)
;
if
(
pos
>=
0
)
{
// binaries with spaces in path may not work
// possible solution to read directly from /proc
const
commandEndPositionMaybe
=
fullCommand
.
indexOf
(
" "
,
pos
+
shortName
.
length
)
;
const
commandEndPosition
=
commandEndPositionMaybe
<
0
?
fullCommand
.
length
:
commandEndPositionMaybe
;
command
=
fullCommand
.
substring
(
0
,
commandEndPosition
)
args
=
fullCommand
.
substring
(
commandEndPosition
)
.
trimStart
(
)
}
if
(
!
isNaN
(
pid
)
&&
!
isNaN
(
ppid
)
)
{
one
(
pid
,
ppid
,
command
,
args
)
;
}
}
)
)
;
}
proc
.
on
(
'error'
,
err
=>
{
reject
(
err
)
;
}
)
;
proc
.
stderr
.
setEncoding
(
'utf8'
)
;
proc
.
stderr
.
on
(
'data'
,
data
=>
{
const
e
=
data
.
toString
(
)
;
if
(
e
.
indexOf
(
'screen size is bogus'
)
>=
0
)
{
// ignore this error silently; see https://github.com/microsoft/vscode/issues/75932
}
else
{
reject
(
new
Error
(
data
.
toString
(
)
)
)
;
}
}
)
;
proc
.
on
(
'close'
,
(
code
,
signal
)
=>
{
if
(
code
===
0
)
{
resolve
(
)
;
}
else
if
(
code
!==
null
&&
code
>
0
)
{
reject
(
new
Error
(
`process terminated with exit code:
${
code
}
`
)
)
;
}
if
(
signal
)
{
reject
(
new
Error
(
`process terminated with signal:
${
signal
}
`
)
)
;
}
}
)
;
proc
.
on
(
'exit'
,
(
code
,
signal
)
=>
{
if
(
typeof
code
===
'number'
)
{
if
(
code
===
0
)
{
//resolve();
}
else
if
(
code
>
0
)
{
reject
(
new
Error
(
`process terminated with exit code:
${
code
}
`
)
)
;
}
}
if
(
signal
)
{
reject
(
new
Error
(
`process terminated with signal:
${
signal
}
`
)
)
;
}
}
)
;
}
)
;
}
function
substr
(
str
:
string
,
startIndex
:
number
,
length
?:
number
)
{
return
str
.
slice
(
startIndex
,
length
!=
undefined
?
startIndex
+
length
:
str
.
length
)
}
Back
|
FazBrowse Home
|
New Git URL