-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallstack.go
90 lines (76 loc) · 2.26 KB
/
callstack.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
// CallStackRoot wraps the root CallStack item for the function tree
type CallStackRoot struct {
Root *CallStack `json:"downward"`
}
// CallStack represents a node in the function tree. It holds a function name,
// and its children are the functions (other CallStack nodes) invoked by the
// function.
type CallStack struct {
Name string `json:"name"`
Depth int
Children []*CallStack `json:"children"`
}
// Write generates the callstack as a json file
func (cs *CallStackRoot) Write(outpath string) error {
data, err := json.Marshal(cs)
if err != nil {
return fmt.Errorf("Error creating callstack json: %s", err)
}
if err := ioutil.WriteFile(outpath, data, 0755); err != nil {
return fmt.Errorf("Error writing json data: %s", err)
}
return nil
}
// Functions is a map of function names to Function objects
type Functions map[string][]*Function
// BuildCallStack generates a function tree from the defined Functions.
// It starts at the main function since Go programs begin execution at func main()
// recursively builds the function call tree. Functions not invoked in the program
// will not appear in the CallStack.
func (f Functions) BuildCallStack() *CallStackRoot {
fns, ok := f["main"]
if !ok || len(fns) == 0 {
return nil
}
entryFn := fns[0]
cs := &CallStack{}
f.callStackHelper(entryFn, cs, []string{}, 0)
return &CallStackRoot{cs}
}
func (f Functions) callStackHelper(fn *Function, cs *CallStack, seen []string, depth int) {
seen = append(seen, fn.Info())
cs.Name = fn.Info()
cs.Depth = depth
for _, fnName := range fn.Calls {
fnDefs, ok := f[fnName]
if !ok || len(fnDefs) == 0 {
// function is defined externally
cs.Children = append(cs.Children,
&CallStack{Name: fmt.Sprintf("%s (external)", fnName), Depth: depth + 1})
continue
}
childFn := fnDefs[0]
if alreadySeen(seen, childFn) {
// avoid loops
cs.Children = append(cs.Children, &CallStack{Name: childFn.Info(), Depth: depth + 1})
continue
}
childCS := &CallStack{}
f.callStackHelper(childFn, childCS, seen, depth+1)
cs.Children = append(cs.Children, childCS)
}
}
func alreadySeen(seen []string, fn *Function) bool {
for _, s := range seen {
if s == fn.Info() {
return true
}
}
return false
}