-
Notifications
You must be signed in to change notification settings - Fork 5
/
functree.go
108 lines (98 loc) · 2.44 KB
/
functree.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
package uprobe
import (
debugelf "debug/elf"
"fmt"
"strings"
"github.com/jschwinger233/ufuncgraph/elf"
log "github.com/sirupsen/logrus"
)
type FuncTree struct {
Name string
FpOffset uint64
RetOffsets []uint64
Children []*FuncTree
Err error
}
func (t *FuncTree) Traverse(f func(int, *FuncTree, *FuncTree) bool) {
t.Visit(0, nil, t, f)
}
func (t *FuncTree) Visit(layer int, parent, self *FuncTree, f func(int, *FuncTree, *FuncTree) bool) {
cont := f(layer, parent, self)
if cont {
for _, tree := range t.Children {
tree.Visit(layer+1, t, tree, f)
}
}
}
func (t *FuncTree) Print(indent int) {
prefix := strings.Repeat(" ", indent)
rets := []string{}
for _, ret := range t.RetOffsets {
rets = append(rets, fmt.Sprintf("%x", ret))
}
if t.Err == nil {
log.Infof("%s%s: %x %s\n", prefix, t.Name, t.FpOffset, rets)
} else {
log.Warnf("%s%s: %s\n", prefix, t.Name, t.Err)
}
for _, tree := range t.Children {
tree.Print(indent + 2)
}
}
func parseFuncTrees(elf *elf.ELF, wildcards, exWildcards []string, searchDepth int) (trees []*FuncTree, err error) {
funcnamesMatchedWildcards := func(wildcards []string) (funcnames []string, err error) {
symbols, _, err := elf.Symbols()
if err != nil {
return
}
for _, symbol := range symbols {
if debugelf.ST_TYPE(symbol.Info) == debugelf.STT_FUNC {
for _, wc := range wildcards {
if MatchWildcard(wc, symbol.Name) {
funcnames = append(funcnames, symbol.Name)
break
}
}
}
}
return
}
var parseFuncTree func(name string, depth int, ex []string) *FuncTree
parseFuncTree = func(name string, depth int, ex []string) (tree *FuncTree) {
tree = &FuncTree{Name: name}
funcnames, err := elf.FuncCalledBy(name)
if err != nil {
tree.Err = err
return
}
tree.FpOffset, tree.Err = elf.FuncFramePointerOffset(name)
if tree.Err != nil {
return
}
tree.RetOffsets, tree.Err = elf.FuncRetOffsets(name)
if tree.Err != nil {
return
}
for _, wc := range ex {
if MatchWildcard(wc, name) {
tree.Err = fmt.Errorf("excluded by %s", wc)
break
}
}
if depth == 0 {
return
}
for _, funcname := range funcnames {
tree.Children = append(tree.Children, parseFuncTree(funcname, depth-1, ex))
}
return
}
funcnames, err := funcnamesMatchedWildcards(wildcards)
if err != nil {
return
}
for _, funcname := range funcnames {
trees = append(trees, parseFuncTree(funcname, searchDepth, exWildcards))
}
return
}