-
Notifications
You must be signed in to change notification settings - Fork 572
/
versions_libc.go
127 lines (113 loc) · 3.98 KB
/
versions_libc.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
package python
import (
"fmt"
"io"
"os"
"regexp"
"sort"
"strconv"
log2 "github.com/go-kit/log"
"github.com/go-kit/log/level"
)
var reMuslVersion = regexp.MustCompile("1\\.([12])\\.(\\d+)\\D")
func GetMuslVersionFromFile(f string) (Version, error) {
muslFD, err := os.Open(f)
if err != nil {
return Version{}, fmt.Errorf("couldnot determine musl version %s %w", f, err)
}
defer muslFD.Close()
return GetMuslVersionFromReader(muslFD)
}
// GetMuslVersionFromReader return minor musl version. For example 1 for 1.1.44 and 2 for 1.2.4
func GetMuslVersionFromReader(r io.Reader) (Version, error) {
m, err := rgrep(r, reMuslVersion)
if err != nil {
return Version{}, fmt.Errorf("rgrep musl version %w", err)
}
minor, _ := strconv.Atoi(string(m[1]))
patch, _ := strconv.Atoi(string(m[2]))
return Version{Major: 1, Minor: minor, Patch: patch}, nil
}
var reGlibcVersion = regexp.MustCompile("glibc 2\\.(\\d+)\\D")
func GetGlibcVersionFromFile(f string) (Version, error) {
muslFD, err := os.Open(f)
if err != nil {
return Version{}, fmt.Errorf("couldnot determine glibc version %s %w", f, err)
}
defer muslFD.Close()
return GetGlibcVersionFromReader(muslFD)
}
func GetGlibcVersionFromReader(r io.Reader) (Version, error) {
m, err := rgrep(r, reGlibcVersion)
if err != nil {
return Version{}, fmt.Errorf("rgrep python version %w", err)
}
minor, err := strconv.Atoi(string(m[1]))
if err != nil {
return Version{}, fmt.Errorf("error trying to grep musl minor version %s, %w", string(m[0]), err)
}
return Version{Major: 2, Minor: minor}, nil
}
func GetGlibcOffsets(version Version) (PerfLibc, bool, error) {
offsets, ok := glibcOffsets[version]
if ok {
return PerfLibc{
Musl: false,
PthreadSize: offsets.PthreadSize,
PthreadSpecific1stblock: offsets.PthreadSpecific1stblock,
}, false, nil
}
versions := make([]Version, 0, len(glibcOffsets))
for v := range glibcOffsets {
versions = append(versions, v)
}
sort.Slice(versions, func(i, j int) bool {
return versions[i].Compare(&versions[j]) < 0
})
if version.Compare(&versions[len(versions)-1]) > 0 {
return PerfLibc{
Musl: false,
PthreadSize: glibcOffsets[versions[len(versions)-1]].PthreadSize,
PthreadSpecific1stblock: glibcOffsets[versions[len(versions)-1]].PthreadSpecific1stblock,
}, true, nil
}
return PerfLibc{}, false, fmt.Errorf("unsupported glibc version %v", version)
}
func GetLibc(l log2.Logger, pid uint32, info ProcInfo) (PerfLibc, error) {
if info.Glibc == nil && info.Musl == nil {
return PerfLibc{}, fmt.Errorf("could not determine libc version %d, no libc found", pid)
}
if info.Musl != nil {
muslPath := fmt.Sprintf("/proc/%d/root%s", pid, info.Musl[0].Pathname)
muslVersion, err := GetMuslVersionFromFile(muslPath)
if err != nil {
return PerfLibc{}, fmt.Errorf("couldnot determine musl version %s %w", muslPath, err)
}
res := PerfLibc{Musl: true}
mo, guess, err := getVersionGuessing(muslVersion, muslOffsets)
if err != nil {
return PerfLibc{}, fmt.Errorf("unsupported musl version %w %+v", err, muslVersion)
}
if guess {
_ = level.Warn(l).Log("msg", "musl offsets were not found, but guessed from the closest version")
}
res.PthreadSize = mo.PthreadSize
res.PthreadSpecific1stblock = mo.PthreadTsd
_ = level.Debug(l).Log("msg", "musl offsets", "offsets", fmt.Sprintf("%+v", res))
return res, nil
}
glibcPath := fmt.Sprintf("/proc/%d/root%s", pid, info.Glibc[0].Pathname)
glibcVersion, err := GetGlibcVersionFromFile(glibcPath)
if err != nil {
return PerfLibc{}, fmt.Errorf("couldnot determine glibc version %s %w", glibcPath, err)
}
res, guess, err := GetGlibcOffsets(glibcVersion)
if err != nil {
return PerfLibc{}, fmt.Errorf("unsupported glibc version %w %+v", err, glibcVersion)
}
if guess {
_ = level.Warn(l).Log("msg", "glibc offsets were not found, but guessed from the closest version")
}
_ = level.Debug(l).Log("msg", "glibc offsets", "offsets", fmt.Sprintf("%+v", res))
return res, nil
}