-
Notifications
You must be signed in to change notification settings - Fork 501
/
main.go
150 lines (139 loc) · 4.65 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"go/ast"
"log"
"os"
alib "go.opentelemetry.io/contrib/instrgen/lib"
)
func usage() error {
fmt.Println("\nusage driver --command [path to go project] [package pattern]")
fmt.Println("\tcommand:")
fmt.Println("\t\tinject (injects open telemetry calls into project code)")
fmt.Println("\t\tinject-dump-ir (injects open telemetry calls into project code and intermediate passes)")
fmt.Println("\t\tprune (prune open telemetry calls")
fmt.Println("\t\tdumpcfg (dumps control flow graph)")
fmt.Println("\t\trootfunctions (dumps root functions)")
return nil
}
func makeAnalysis(projectPath string, packagePattern string, debug bool) *alib.Analysis {
var rootFunctions []alib.FuncDescriptor
interfaces := alib.FindInterfaces(projectPath, packagePattern)
rootFunctions = append(rootFunctions, alib.FindRootFunctions(projectPath, packagePattern)...)
funcDecls := alib.FindFuncDecls(projectPath, packagePattern, interfaces)
backwardCallGraph := alib.BuildCallGraph(projectPath, packagePattern, funcDecls, interfaces)
fmt.Println("\n\tchild parent")
for k, v := range backwardCallGraph {
fmt.Print("\n\t", k)
fmt.Print(" ", v)
}
fmt.Println("")
analysis := &alib.Analysis{
ProjectPath: projectPath,
PackagePattern: packagePattern,
RootFunctions: rootFunctions,
FuncDecls: funcDecls,
Callgraph: backwardCallGraph,
Interfaces: interfaces,
Debug: debug}
return analysis
}
// Prune.
func Prune(projectPath string, packagePattern string, debug bool) ([]*ast.File, error) {
analysis := makeAnalysis(projectPath, packagePattern, debug)
return analysis.Execute(&alib.OtelPruner{}, otelPrunerPassSuffix)
}
// Parsing algorithm works as follows. It goes through all function
// decls and infer function bodies to find call to AutotelEntryPoint
// A parent function of this call will become root of instrumentation
// Each function call from this place will be instrumented automatically.
func executeCommand(command string, projectPath string, packagePattern string) error {
if command == "--inject" {
_, err := Prune(projectPath, packagePattern, false)
if err != nil {
return err
}
analysis := makeAnalysis(projectPath, packagePattern, false)
err = ExecutePasses(analysis)
if err != nil {
return err
}
fmt.Println("\tinstrumentation done")
return nil
}
if command == "--inject-dump-ir" {
_, err := Prune(projectPath, packagePattern, true)
if err != nil {
return err
}
analysis := makeAnalysis(projectPath, packagePattern, true)
err = ExecutePassesDumpIr(analysis)
if err != nil {
return err
}
fmt.Println("\tinstrumentation done")
return nil
}
if command == "--dumpcfg" {
var funcDecls map[alib.FuncDescriptor]bool
var backwardCallGraph map[alib.FuncDescriptor][]alib.FuncDescriptor
interfaces := alib.FindInterfaces(projectPath, packagePattern)
funcDecls = alib.FindFuncDecls(projectPath, packagePattern, interfaces)
backwardCallGraph = alib.BuildCallGraph(projectPath, packagePattern, funcDecls, interfaces)
fmt.Println("\n\tchild parent")
for k, v := range backwardCallGraph {
fmt.Print("\n\t", k)
fmt.Print(" ", v)
}
return nil
}
if command == "--rootfunctions" {
var rootFunctions []alib.FuncDescriptor
rootFunctions = append(rootFunctions, alib.FindRootFunctions(projectPath, packagePattern)...)
fmt.Println("rootfunctions:")
for _, fun := range rootFunctions {
fmt.Println("\t" + fun.TypeHash())
}
return nil
}
if command == "--prune" {
_, err := Prune(projectPath, packagePattern, false)
if err != nil {
return err
}
return nil
}
return errors.New("unknown command")
}
func checkArgs(args []string) error {
if len(args) != 4 {
_ = usage()
return errors.New("wrong arguments")
}
return nil
}
func main() {
fmt.Println("autotel compiler")
err := checkArgs(os.Args)
if err != nil {
return
}
err = executeCommand(os.Args[1], os.Args[2], os.Args[3])
if err != nil {
log.Fatal(err)
}
}