...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package mapreader
17
18 import (
19 "fmt"
20 "os"
21 "strconv"
22 "strings"
23
24 "github.com/pkg/errors"
25 )
26
27
28 type Entry struct {
29 StartAddress uint64
30 EndAddress uint64
31 Privilege string
32 PaddingSize uint64
33 Path string
34 }
35
36
37
38 func Read(pid int) ([]Entry, error) {
39 data, err := os.ReadFile(fmt.Sprintf("/proc/%d/maps", pid))
40 if err != nil {
41 return nil, errors.WithStack(err)
42 }
43
44 lines := strings.Split(string(data), "\n")
45
46 var entries []Entry
47 for _, line := range lines {
48 sections := strings.Split(line, " ")
49 if len(sections) < 3 {
50 continue
51 }
52
53 var path string
54
55 if len(sections) > 5 {
56 path = sections[len(sections)-1]
57 }
58
59 addresses := strings.Split(sections[0], "-")
60 startAddress, err := strconv.ParseUint(addresses[0], 16, 64)
61 if err != nil {
62 return nil, errors.WithStack(err)
63 }
64 endAddresses, err := strconv.ParseUint(addresses[1], 16, 64)
65 if err != nil {
66 return nil, errors.WithStack(err)
67 }
68
69 privilege := sections[1]
70
71 paddingSize, err := strconv.ParseUint(sections[2], 16, 64)
72 if err != nil {
73 return nil, errors.WithStack(err)
74 }
75
76 entries = append(entries, Entry{
77 startAddress,
78 endAddresses,
79 privilege,
80 paddingSize,
81 path,
82 })
83 }
84
85 return entries, nil
86 }
87