forked from fkie-cad/yapscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listMem.go
86 lines (69 loc) · 2.15 KB
/
listMem.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
package app
import (
"fmt"
"strconv"
"github.com/dustin/go-humanize"
"github.com/fkie-cad/yapscan/procio"
"github.com/targodan/go-errors"
"github.com/urfave/cli/v2"
)
func listMemory(c *cli.Context) error {
err := initAppAction(c)
if err != nil {
return err
}
if c.NArg() != 1 {
return errors.Newf("expected exactly one argument, got %d", c.NArg())
}
pid_, err := strconv.ParseUint(c.Args().Get(0), 10, 64)
if err != nil {
return errors.Newf("\"%s\" is not a pid", c.Args().Get(0))
}
pid := int(pid_)
f, err := filterFromArgs(c)
if err != nil {
return err
}
fmt.Printf("Filters: %s\n\n", f.Description())
proc, err := procio.OpenProcess(pid)
if err != nil {
return errors.Newf("could not open process with pid %d, reason: %w", pid, err)
}
format := "%19s %8s %8s %3s %13s %7s %s\n"
fmt.Printf(format, "Address", "Size", "RSS", "", "Type", "State", "Path")
fmt.Printf("-------------------+--------+--------+---+-------------+-------+------\n")
var estimatedRAMIncrease uintptr
segments, err := proc.MemorySegments()
if err != nil {
return errors.Newf("could not enumerate memory segments of process %d, reason: %w", pid, err)
}
for _, seg := range segments {
fRes := f.Filter(seg)
if !fRes.Result {
continue
}
estimatedRAMIncrease += seg.EstimateRAMIncreaseByScanning()
filepath := ""
if seg.MappedFile != nil {
filepath = seg.MappedFile.Path()
}
fmt.Printf(format, procio.FormatMemorySegmentAddress(seg), humanize.Bytes(uint64(seg.Size)), humanize.Bytes(uint64(seg.RSS)), seg.CurrentPermissions, seg.Type, seg.State, filepath)
if c.Bool("list-subdivided") {
for i, sseg := range seg.SubSegments {
addr := procio.FormatMemorySegmentAddress(sseg)
if i+1 < len(seg.SubSegments) {
addr = "├" + addr
} else {
addr = "└" + addr
}
filepath = ""
if sseg.MappedFile != nil {
filepath = sseg.MappedFile.Path()
}
fmt.Printf(format, addr, humanize.Bytes(uint64(sseg.Size)), sseg.CurrentPermissions, sseg.Type, sseg.State, filepath)
}
}
}
fmt.Printf("Estimated RAM increase by scanning this process: %s\n", humanize.Bytes(uint64(estimatedRAMIncrease)))
return nil
}