forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcMapsScanner.swift
256 lines (234 loc) · 6.93 KB
/
ProcMapsScanner.swift
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
//===--- ProcMapsScanner.swift --------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Defines ProcMapsScanner, which is for scanning the /proc/<pid>/maps
// pseudofiles on Linux.
//
//===----------------------------------------------------------------------===//
#if os(Linux)
import Swift
// Lines in /proc/pid/maps files match the following regex:
//
// ^(?<start>[A-Fa-f0-9]+)-(?<end>[A-Fa-f0-9]+)\s+
// (?<perms>[-rwxsp]{4})\s+
// (?<offset>[A-Fa-f0-9]+)\s+
// (?<major>[A-Fa-f0-9]+):(?<minor>[A-Fa-f0-9]+)\s+
// (?<inode>\d+)\s+
// (?<pathname>.*)\s*$
struct ProcMapsScanner<S: StringProtocol>: Sequence, IteratorProtocol {
typealias SS = S.SubSequence
struct Match {
var start: SS
var end: SS
var perms: SS
var offset: SS
var major: SS
var minor: SS
var inode: SS
var pathname: SS
}
private enum State {
case start
case scanningStart
case scanningEnd
case afterEnd
case scanningPerms(Int)
case afterPerms
case scanningOffset
case afterOffset
case scanningMajor
case scanningMinor
case afterMinor
case scanningInode
case afterInode
case scanningPathname
case scanningPathnameWhitespace
}
private var procMaps: S
private var procMapsUTF8: S.UTF8View
private var ndx: S.UTF8View.Index
init(_ string: S) {
procMaps = string
procMapsUTF8 = string.utf8
ndx = procMapsUTF8.startIndex
}
mutating func scanMatch() -> Match? {
var match: Match = Match(start: "",
end: "",
perms: "",
offset: "",
major: "",
minor: "",
inode: "",
pathname: "")
var currentChunk = ndx
var state: State = .start
func isPerm(_ ch: UInt8) -> Bool {
return ch == UInt8(ascii: "-") || ch == UInt8(ascii: "r")
|| ch == UInt8(ascii: "w") || ch == UInt8(ascii: "x")
|| ch == UInt8(ascii: "s") || ch == UInt8(ascii: "p")
}
func isDecimal(_ ch: UInt8) -> Bool {
return ch >= UInt8(ascii: "0") && ch <= UInt8(ascii: "9")
}
func isHex(_ ch: UInt8) -> Bool {
return ch >= UInt8(ascii: "A") && ch <= UInt8(ascii: "F")
|| ch >= UInt8(ascii: "a") && ch <= UInt8(ascii: "f")
|| ch >= UInt8(ascii: "0") && ch <= UInt8(ascii: "9")
}
func isWhitespace(_ ch: UInt8) -> Bool {
return ch == UInt8(ascii: " ") || ch == UInt8(ascii: "\t")
}
while ndx < procMapsUTF8.endIndex {
let ch = procMapsUTF8[ndx]
let next = procMapsUTF8.index(after: ndx)
switch state {
case .start:
if !isHex(ch) {
return nil
}
state = .scanningStart
case .scanningStart:
if ch == UInt8(ascii: "-") {
match.start = procMaps[currentChunk..<ndx]
state = .scanningEnd
currentChunk = next
} else if !isHex(ch) {
return nil
}
case .scanningEnd:
if isWhitespace(ch) {
match.end = procMaps[currentChunk..<ndx]
state = .afterEnd
} else if !isHex(ch) {
return nil
}
case .afterEnd:
if isPerm(ch) {
currentChunk = ndx
state = .scanningPerms(1)
} else if !isWhitespace(ch) {
return nil
}
case let .scanningPerms(length):
if length == 4 {
if isWhitespace(ch) {
match.perms = procMaps[currentChunk..<ndx]
state = .afterPerms
} else {
return nil
}
} else if isPerm(ch) {
state = .scanningPerms(length + 1)
} else {
return nil
}
case .afterPerms:
if isHex(ch) {
currentChunk = ndx
state = .scanningOffset
} else if !isWhitespace(ch) {
return nil
}
case .scanningOffset:
if isWhitespace(ch) {
match.offset = procMaps[currentChunk..<ndx]
state = .afterOffset
} else if !isHex(ch) {
return nil
}
case .afterOffset:
if isHex(ch) {
currentChunk = ndx
state = .scanningMajor
} else if !isWhitespace(ch) {
return nil
}
case .scanningMajor:
if ch == UInt8(ascii: ":") {
match.major = procMaps[currentChunk..<ndx]
state = .scanningMinor
currentChunk = next
} else if !isHex(ch) {
return nil
}
case .scanningMinor:
if isWhitespace(ch) {
match.minor = procMaps[currentChunk..<ndx]
state = .afterMinor
} else if !isHex(ch) {
return nil
}
case .afterMinor:
if isDecimal(ch) {
currentChunk = ndx
state = .scanningInode
} else if !isWhitespace(ch) {
return nil
}
case .scanningInode:
if isWhitespace(ch) {
match.inode = procMaps[currentChunk..<ndx]
state = .afterInode
} else if !isDecimal(ch) {
return nil
}
case .afterInode:
if ch == 0x0a {
ndx = next
return match
} else if !isWhitespace(ch) {
currentChunk = ndx
state = .scanningPathname
}
case .scanningPathname:
if isWhitespace(ch) || ch == 0x0a {
match.pathname = procMaps[currentChunk..<ndx]
state = .scanningPathnameWhitespace
if ch == 0x0a {
ndx = next
return match
}
}
case .scanningPathnameWhitespace:
if !isWhitespace(ch) {
state = .scanningPathname
} else if ch == 0x0a {
ndx = next
return match
}
}
ndx = next
}
if case .scanningPathname = state {
match.pathname = procMaps[currentChunk...]
}
return match
}
mutating func next() -> Match? {
while ndx != procMapsUTF8.endIndex {
if let match = scanMatch() {
return match
}
// If we failed to match, skip to end of line and retry
while ndx != procMapsUTF8.endIndex {
let ch = procMapsUTF8[ndx]
ndx = procMapsUTF8.index(after: ndx)
if ch == 0x0a {
break
}
}
}
return nil
}
}
#endif // os(Linux)