-
Notifications
You must be signed in to change notification settings - Fork 42
/
explore.go
486 lines (438 loc) · 14.1 KB
/
explore.go
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package crit
import (
"fmt"
"path/filepath"
"strconv"
criu_core "github.com/checkpoint-restore/go-criu/v7/crit/images/criu-core"
"github.com/checkpoint-restore/go-criu/v7/crit/images/fdinfo"
"github.com/checkpoint-restore/go-criu/v7/crit/images/fs"
"github.com/checkpoint-restore/go-criu/v7/crit/images/mm"
"github.com/checkpoint-restore/go-criu/v7/crit/images/pagemap"
"github.com/checkpoint-restore/go-criu/v7/crit/images/pstree"
)
// PsTree represents the process tree
type PsTree struct {
PID uint32 `json:"pid"`
PgID uint32 `json:"pgid"`
SID uint32 `json:"sid"`
Comm string `json:"comm"`
Process *pstree.PstreeEntry `json:"-"`
Core *criu_core.CoreEntry `json:"-"`
Children []*PsTree `json:"children,omitempty"`
}
// ExplorePs constructs the process tree and returns the root process
func (c *crit) ExplorePs() (*PsTree, error) {
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"), &pstree.PstreeEntry{})
if err != nil {
return nil, err
}
processes := make(map[uint32]*PsTree)
var psTreeRoot *PsTree
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*pstree.PstreeEntry)
pID := process.GetPid()
coreImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("core-%d.img", pID)), &criu_core.CoreEntry{})
if err != nil {
return nil, err
}
coreData := coreImg.Entries[0].Message.(*criu_core.CoreEntry)
ps := &PsTree{
PID: pID,
PgID: process.GetPgid(),
SID: process.GetSid(),
Comm: coreData.GetTc().GetComm(),
Process: process,
Core: coreData,
}
// If there is no parent process, then it is the root
if process.GetPpid() == 0 {
psTreeRoot = ps
}
processes[pID] = ps
}
for _, ps := range processes {
parent := ps.Process.GetPpid()
if parent != 0 {
processes[parent].Children = append(processes[parent].Children, ps)
}
}
return psTreeRoot, nil
}
// Fd represents the file descriptors opened in a single process
type Fd struct {
PId uint32 `json:"pid"`
Files []*File `json:"files,omitempty"`
}
// File represents a single opened file
type File struct {
Fd string `json:"fd"`
Type string `json:"type,omitempty"`
Path string `json:"path"`
}
// ExploreFds searches the process tree for open files
// and returns a list of PIDs with the corresponding files
func (c *crit) ExploreFds() ([]*Fd, error) {
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"), &pstree.PstreeEntry{})
if err != nil {
return nil, err
}
fds := make([]*Fd, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*pstree.PstreeEntry)
pID := process.GetPid()
// Get file with object IDs
idsImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("ids-%d.img", pID)), &criu_core.TaskKobjIdsEntry{})
if err != nil {
return nil, err
}
filesID := idsImg.Entries[0].Message.(*criu_core.TaskKobjIdsEntry).GetFilesId()
// Get open file descriptors
fdInfoImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("fdinfo-%d.img", filesID)), &fdinfo.FdinfoEntry{})
if err != nil {
return nil, err
}
fdEntry := Fd{PId: pID}
for _, fdInfoEntry := range fdInfoImg.Entries {
fdInfo := fdInfoEntry.Message.(*fdinfo.FdinfoEntry)
filePath, err := getFilePath(c.inputDirPath,
fdInfo.GetId(), fdInfo.GetType())
if err != nil {
return nil, err
}
file := File{
Fd: strconv.FormatUint(uint64(fdInfo.GetFd()), 10),
Type: fdInfo.GetType().String(),
Path: filePath,
}
fdEntry.Files = append(fdEntry.Files, &file)
}
// Get chroot and chdir info
fsImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("fs-%d.img", pID)), &fs.FsEntry{})
if err != nil {
return nil, err
}
fs := fsImg.Entries[0].Message.(*fs.FsEntry)
filePath, err := getFilePath(c.inputDirPath,
fs.GetCwdId(), fdinfo.FdTypes_REG)
if err != nil {
return nil, err
}
fdEntry.Files = append(fdEntry.Files, &File{
Fd: "cwd",
Path: filePath,
})
filePath, err = getFilePath(c.inputDirPath,
fs.GetRootId(), fdinfo.FdTypes_REG)
if err != nil {
return nil, err
}
fdEntry.Files = append(fdEntry.Files, &File{
Fd: "root",
Path: filePath,
})
// Omit if the process has no file descriptors
if len(fdEntry.Files) == 0 {
continue
}
fds = append(fds, &fdEntry)
}
return fds, nil
}
// MemMap represents the memory mapping of a single process
type MemMap struct {
PId uint32 `json:"pid"`
Exe string `json:"exe"`
Mems []*Mem `json:"mems,omitempty"`
}
// Mem represents the memory mapping of a single file
type Mem struct {
Start string `json:"start"`
End string `json:"end"`
Protection string `json:"protection"`
Resource string `json:"resource,omitempty"`
}
// ExploreMems traverses the process tree and returns a
// list of processes with the corresponding memory mapping
func (c *crit) ExploreMems() ([]*MemMap, error) {
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"), &pstree.PstreeEntry{})
if err != nil {
return nil, err
}
vmaIDMap, vmaID := make(map[uint64]int), 0
// Use a closure to handle the ID counter
getVmaID := func(shmId uint64) int {
if _, ok := vmaIDMap[shmId]; !ok {
vmaIDMap[shmId] = vmaID
vmaID++
}
return vmaIDMap[shmId]
}
memMaps := make([]*MemMap, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*pstree.PstreeEntry)
pID := process.GetPid()
// Get memory mappings
mmImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("mm-%d.img", pID)), &mm.MmEntry{})
if err != nil {
return nil, err
}
mmInfo := mmImg.Entries[0].Message.(*mm.MmEntry)
exePath, err := getFilePath(c.inputDirPath,
mmInfo.GetExeFileId(), fdinfo.FdTypes_REG)
if err != nil {
return nil, err
}
memMap := MemMap{
PId: pID,
Exe: exePath,
}
for _, vma := range mmInfo.GetVmas() {
mem := Mem{
Start: strconv.FormatUint(vma.GetStart(), 16),
End: strconv.FormatUint(vma.GetEnd(), 16),
}
switch status := vma.GetStatus(); {
// Pages used by a file
case status&((1<<7)|(1<<6)) != 0:
file, err := getFilePath(c.inputDirPath,
uint32(vma.GetShmid()), fdinfo.FdTypes_REG)
if err != nil {
return nil, err
}
if vma.GetPgoff() != 0 {
mem.Resource = fmt.Sprintf("%s + 0x%x", file, vma.GetPgoff())
}
if status&(1<<7) != 0 {
mem.Resource = fmt.Sprint(mem.Resource, " (s)")
}
case status&(1<<3) != 0:
mem.Resource = "[vdso]"
case status&(1<<2) != 0:
mem.Resource = "[vsyscall]"
case status&(1<<1) != 0:
mem.Resource = "[stack]"
// 0x0100 (256) indicates that the page grows downwards
case vma.GetFlags()&0x0100 != 0:
mem.Resource = "[stack?]"
case status&(1<<11) != 0:
mem.Resource = fmt.Sprintf("packet[%d]", getVmaID(vma.GetShmid()))
case status&(1<<10) != 0:
mem.Resource = fmt.Sprintf("ips[%d]", getVmaID(vma.GetShmid()))
case status&(1<<8) != 0:
mem.Resource = fmt.Sprintf("shmem[%d]", getVmaID(vma.GetShmid()))
}
if vma.GetStatus()&1 == 0 {
mem.Resource = fmt.Sprint(mem.Resource, " *")
}
// Check page protection
r, w, x := "-", "-", "-"
prot := vma.GetProt()
if prot&1 != 0 {
r = "r"
}
if prot&2 != 0 {
w = "w"
}
if prot&4 != 0 {
x = "x"
}
mem.Protection = fmt.Sprint(r, w, x)
memMap.Mems = append(memMap.Mems, &mem)
}
memMaps = append(memMaps, &memMap)
}
return memMaps, nil
}
// RssMap represents the resident set size mapping of a single process
type RssMap struct {
PId uint32 `json:"pid"`
/*
walrus -> walruses
radius -> radii
If you code without breaks,
rss -> rsi :P
*/
Rsses []*Rss `json:"rss,omitempty"`
}
// Rss represents a single resident set size mapping
type Rss struct {
PhyAddr string `json:"phy_addr,omitempty"`
PhyPages int64 `json:"phy_pages,omitempty"`
Vmas []*Vma `json:"vmas,omitempty"`
Resource string `json:"resource,omitempty"`
}
// Vma represents a single virtual memory area
type Vma struct {
Addr string `json:"addr,omitempty"`
Pages int64 `json:"pages,omitempty"`
}
// ExploreRss traverses the process tree and returns
// a list of processes with their RSS mappings
func (c *crit) ExploreRss() ([]*RssMap, error) {
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"), &pstree.PstreeEntry{})
if err != nil {
return nil, err
}
rssMaps := make([]*RssMap, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*pstree.PstreeEntry)
pID := process.GetPid()
// Get virtual memory addresses
mmImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("mm-%d.img", pID)), &mm.MmEntry{})
if err != nil {
return nil, err
}
vmas := mmImg.Entries[0].Message.(*mm.MmEntry).GetVmas()
// Get physical memory addresses
pagemapImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("pagemap-%d.img", pID)), &pagemap.PagemapEntry{})
if err != nil {
return nil, err
}
vmaIndex, vmaIndexPrev := 0, -1
rssMap := RssMap{PId: pID}
// Skip pagemap head entry
for _, pagemapEntry := range pagemapImg.Entries[1:] {
pagemapData := pagemapEntry.Message.(*pagemap.PagemapEntry)
rss := Rss{
PhyAddr: strconv.FormatUint(pagemapData.GetVaddr(), 16),
PhyPages: int64(pagemapData.GetNrPages()),
}
for vmas[vmaIndex].GetEnd() <= pagemapData.GetVaddr() {
vmaIndex++
}
// Compute last virtual address
pagemapEnd := pagemapData.GetVaddr() + (uint64(pagemapData.GetNrPages()) << 12)
for vmas[vmaIndex].GetStart() < pagemapEnd {
if vmaIndex == vmaIndexPrev {
// Use tilde to indicate that VMA is of the previous pagemap entry
rss.Vmas = append(rss.Vmas, &Vma{Addr: "~"})
vmaIndex++
continue
}
rss.Vmas = append(rss.Vmas, &Vma{
Addr: strconv.FormatUint(vmas[vmaIndex].GetStart(), 16),
Pages: int64(vmas[vmaIndex].GetEnd()-vmas[vmaIndex].GetStart()) >> 12,
})
// Pages used by a file
if vmas[vmaIndex].GetStatus()&((1<<6)|(1<<7)) != 0 {
file, err := getFilePath(c.inputDirPath,
uint32(vmas[vmaIndex].GetShmid()), fdinfo.FdTypes_REG)
if err != nil {
return nil, err
}
rss.Resource = file
}
// Set reference to current index before increment
vmaIndexPrev = vmaIndex
vmaIndex++
}
vmaIndex--
rssMap.Rsses = append(rssMap.Rsses, &rss)
}
rssMaps = append(rssMaps, &rssMap)
}
return rssMaps, nil
}
// Sk represents the sockets associated with a single process
type Sk struct {
PId uint32 `json:"pid"`
Sockets []*Socket `json:"sockets"`
}
// Socket represents a single socket
type Socket struct {
Fd uint32 `json:"fd"`
FdType string `json:"fd_type"`
Family string `json:"family,omitempty"`
Protocol string `json:"protocol,omitempty"`
Type string `json:"type,omitempty"`
State string `json:"state,omitempty"`
SrcAddr string `json:"src_addr,omitempty"`
SrcPort uint32 `json:"src_port,omitempty"`
DestAddr string `json:"dest_addr,omitempty"`
DestPort uint32 `json:"dest_port,omitempty"`
SendBuf string `json:"send_buf,omitempty"`
RecvBuf string `json:"recv_buf,omitempty"`
}
// ExploreSk searches the process tree for sockets
// and returns a list of PIDs with the associated sockets
func (c *crit) ExploreSk() ([]*Sk, error) {
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"), &pstree.PstreeEntry{})
if err != nil {
return nil, err
}
sks := make([]*Sk, 0)
for _, entry := range psTreeImg.Entries {
process := entry.Message.(*pstree.PstreeEntry)
pID := process.GetPid()
// Get file with object IDs
idsImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("ids-%d.img", pID)), &criu_core.TaskKobjIdsEntry{})
if err != nil {
return nil, err
}
filesID := idsImg.Entries[0].Message.(*criu_core.TaskKobjIdsEntry).GetFilesId()
// Get open file descriptors
fdInfoImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("fdinfo-%d.img", filesID)), &fdinfo.FdinfoEntry{})
if err != nil {
return nil, err
}
skEntry := Sk{PId: pID}
for _, fdInfoEntry := range fdInfoImg.Entries {
fdInfo := fdInfoEntry.Message.(*fdinfo.FdinfoEntry)
file, err := getFile(c.inputDirPath, fdInfo.GetId())
if err != nil {
return nil, err
}
socket := Socket{
Fd: fdInfo.GetFd(),
FdType: fdInfo.GetType().String(),
}
switch fdInfo.GetType() {
case fdinfo.FdTypes_INETSK:
if isk := file.GetIsk(); isk != nil {
socket.State = getSkState(tcpState(isk.GetState()))
socket.Family = getAddressFamily(isk.GetFamily())
socket.Protocol = getSkProtocol(isk.GetProto())
socket.Type = getSkType(isk.GetType())
socket.SrcAddr = processIP(isk.GetSrcAddr())
socket.SrcPort = isk.GetSrcPort()
socket.DestAddr = processIP(isk.GetDstAddr())
socket.DestPort = isk.GetDstPort()
socket.SendBuf = countBytes(int64(isk.GetOpts().GetSoSndbuf()))
socket.RecvBuf = countBytes(int64(isk.GetOpts().GetSoRcvbuf()))
}
case fdinfo.FdTypes_UNIXSK:
if usk := file.GetUsk(); usk != nil {
socket.State = getSkState(tcpState(usk.GetState()))
socket.Type = getSkType(usk.GetType())
socket.SrcAddr = string(usk.GetName())
socket.SendBuf = countBytes(int64(usk.GetOpts().GetSoSndbuf()))
socket.RecvBuf = countBytes(int64(usk.GetOpts().GetSoRcvbuf()))
}
case fdinfo.FdTypes_PACKETSK:
if psk := file.GetPsk(); psk != nil {
socket.Type = getSkType(psk.GetProtocol())
socket.Protocol = getSkProtocol(psk.GetProtocol())
socket.SendBuf = countBytes(int64(psk.GetOpts().GetSoSndbuf()))
socket.RecvBuf = countBytes(int64(psk.GetOpts().GetSoRcvbuf()))
}
case fdinfo.FdTypes_NETLINKSK:
if nlsk := file.GetNlsk(); nlsk != nil {
socket.State = getSkState(tcpState(nlsk.GetState()))
socket.Protocol = getSkProtocol(nlsk.GetProtocol())
socket.Type = getSkType(nlsk.GetProtocol())
socket.SendBuf = countBytes(int64(nlsk.GetOpts().GetSoSndbuf()))
socket.RecvBuf = countBytes(int64(nlsk.GetOpts().GetSoRcvbuf()))
}
default:
continue
}
skEntry.Sockets = append(skEntry.Sockets, &socket)
}
// Omit if the process has no associated sockets
if len(skEntry.Sockets) == 0 {
continue
}
sks = append(sks, &skEntry)
}
return sks, nil
}