-
Notifications
You must be signed in to change notification settings - Fork 659
/
kallsyms.go
74 lines (64 loc) · 1.41 KB
/
kallsyms.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
package kallsyms
import (
"bufio"
"bytes"
"io"
"os"
"sync"
)
var kernelModules struct {
sync.RWMutex
// function to kernel module mapping
kmods map[string]string
}
// KernelModule returns the kernel module, if any, a probe-able function is contained in.
func KernelModule(fn string) (string, error) {
kernelModules.RLock()
kmods := kernelModules.kmods
kernelModules.RUnlock()
if kmods == nil {
kernelModules.Lock()
defer kernelModules.Unlock()
kmods = kernelModules.kmods
}
if kmods != nil {
return kmods[fn], nil
}
f, err := os.Open("/proc/kallsyms")
if err != nil {
return "", err
}
defer f.Close()
kmods, err = loadKernelModuleMapping(f)
if err != nil {
return "", err
}
kernelModules.kmods = kmods
return kmods[fn], nil
}
// FlushKernelModuleCache removes any cached information about function to kernel module mapping.
func FlushKernelModuleCache() {
kernelModules.Lock()
defer kernelModules.Unlock()
kernelModules.kmods = nil
}
func loadKernelModuleMapping(f io.Reader) (map[string]string, error) {
mods := make(map[string]string)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := bytes.Fields(scanner.Bytes())
if len(fields) < 4 {
continue
}
switch string(fields[1]) {
case "t", "T":
mods[string(fields[2])] = string(bytes.Trim(fields[3], "[]"))
default:
continue
}
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
return mods, nil
}