-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcat.go
49 lines (41 loc) · 1 KB
/
memcat.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
package main
import (
"fmt"
"flag"
"os"
"strconv"
"github.com/westonbelk/memcat/pkg/procfs"
)
var pidFlag int
func initFlags() {
flag.IntVar(&pidFlag, "pid", -1, "PID to export the memory of. Defaults to self.")
flag.Parse()
}
func main() {
initFlags()
fmt.Fprintf(os.Stderr, "attaching to pid %d\n", pidFlag)
// determine the pid
pid := procfs.Pid("self")
if pidFlag > 0 {
pid = procfs.Pid(strconv.Itoa(pidFlag))
}
// read current mapped memory spaces
maps, err := procfs.ReadMap(pid)
if err != nil {
panic(err)
}
// create process struct
process := procfs.Process{
Pid: pid,
Maps: maps,
}
// read maps and output to stdout
for _, mapsEntry := range process.Maps {
if mapsEntry.Perms.Read {
err := process.PipeBytes(mapsEntry)
if err != nil {
fmt.Fprintf(os.Stderr, "%s for pathname %s\n", err, mapsEntry.Pathname)
}
}
}
}