This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from DavadDi/skbtracer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ksym.go
79 lines (67 loc) · 1.48 KB
/
ksym.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
package main
// Copied from github.com/cilium/pwru, thank Duan Jiong <[email protected]>
import (
"bufio"
"os"
"sort"
"strconv"
"strings"
)
type ksym struct {
addr uint64
name string
}
type byAddr []*ksym
func (a byAddr) Len() int { return len(a) }
func (a byAddr) Less(i, j int) bool { return a[i].addr < a[j].addr }
func (a byAddr) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type Addr2Name struct {
Addr2NameMap map[uint64]*ksym
Addr2NameSlice []*ksym
}
func (a *Addr2Name) findNearestSym(ip uint64) string {
total := len(a.Addr2NameSlice)
i, j := 0, total
for i < j {
h := int(uint(i+j) >> 1)
if a.Addr2NameSlice[h].addr <= ip {
if h+1 < total && a.Addr2NameSlice[h+1].addr > ip {
return a.Addr2NameSlice[h].name
}
i = h + 1
} else {
j = h
}
}
return a.Addr2NameSlice[i-1].name
}
func GetAddrs() (Addr2Name, error) {
a2n := Addr2Name{
Addr2NameMap: make(map[uint64]*ksym),
}
file, err := os.Open("/proc/kallsyms")
if err != nil {
return a2n, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
name := line[2]
addr, err := strconv.ParseUint(line[0], 16, 64)
if err != nil {
return a2n, err
}
sym := &ksym{
addr: addr,
name: name,
}
a2n.Addr2NameMap[addr] = sym
a2n.Addr2NameSlice = append(a2n.Addr2NameSlice, sym)
}
if err := scanner.Err(); err != nil {
return a2n, err
}
sort.Sort(byAddr(a2n.Addr2NameSlice))
return a2n, nil
}