Skip to content

Commit

Permalink
callgraph functionality will be added later
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelewski committed Jul 21, 2023
1 parent 90f06aa commit 8b48c7a
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 440 deletions.
99 changes: 1 addition & 98 deletions instrgen/driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,23 @@ import (
"encoding/json"
"errors"
"fmt"
alib "go.opentelemetry.io/contrib/instrgen/lib"
"go.opentelemetry.io/contrib/instrgen/rewriters"
"go/ast"
"go/build"
"go/parser"
"go/printer"
"go/token"
"go/types"
"golang.org/x/tools/go/loader"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

alib "go.opentelemetry.io/contrib/instrgen/lib"
)

func usage() error {
fmt.Println("\nusage driver --command [file pattern] replace entrypoint")

Check warning on line 34 in instrgen/driver/main.go

View check run for this annotation

Codecov / codecov/patch

instrgen/driver/main.go#L34

Added line #L34 was not covered by tests
fmt.Println("\tcommand:")
fmt.Println("\t\tinject (injects open telemetry calls into project code)")
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
}

Expand All @@ -59,71 +51,6 @@ type InstrgenCmd struct {
EntryPoint EntryPoint
}

// Load whole go program.
func LoadProgram(projectPath string, ginfo *types.Info) (*loader.Program, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
conf := loader.Config{ParserMode: parser.ParseComments}
conf.Build = &build.Default
conf.Build.CgoEnabled = false
conf.Build.Dir = filepath.Join(cwd, projectPath)
conf.Import(projectPath)
var mutex = &sync.RWMutex{}
conf.AfterTypeCheck = func(info *loader.PackageInfo, files []*ast.File) {
for k, v := range info.Defs {
mutex.Lock()
ginfo.Defs[k] = v
mutex.Unlock()
}
for k, v := range info.Uses {
mutex.Lock()
ginfo.Uses[k] = v
mutex.Unlock()
}
for k, v := range info.Selections {
mutex.Lock()
ginfo.Selections[k] = v
mutex.Unlock()
}
}
return conf.Load()

}

func makeCallGraph(packagePattern string, prog *loader.Program, ginfo *types.Info) map[alib.FuncDescriptor][]alib.FuncDescriptor {
var backwardCallGraph map[alib.FuncDescriptor][]alib.FuncDescriptor

interfaces := alib.GetInterfaces(ginfo.Defs)
funcsInfo := alib.FindFuncDecls(prog, ginfo, interfaces, packagePattern)
backwardCallGraph = alib.BuildCallGraph(prog, ginfo, funcsInfo, packagePattern)

return backwardCallGraph
}

func makeRootFunctions(prog *loader.Program, ginfo *types.Info, packagePattern string) []alib.FuncDescriptor {
var rootFunctions []alib.FuncDescriptor
interfaces := alib.GetInterfaces(ginfo.Defs)
rootFunctions = append(rootFunctions, alib.FindRootFunctions(prog, ginfo, interfaces, packagePattern)...)
return rootFunctions
}

func dumpCallGraph(callGraph map[alib.FuncDescriptor][]alib.FuncDescriptor) {
fmt.Println("\n\tchild parent")
for k, v := range callGraph {
fmt.Print("\n\t", k)
fmt.Print(" ", v)
}
}

func dumpRootFunctions(rootFunctions []alib.FuncDescriptor) {
fmt.Println("rootfunctions:")
for _, fun := range rootFunctions {
fmt.Println("\t" + fun.TypeHash())
}
}

func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
Expand All @@ -142,11 +69,6 @@ func executeCommand(command string, projectPath string, packagePattern string, r
if err != nil {
return err
}
ginfo := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}

switch command {
case "--inject":
Expand All @@ -165,25 +87,6 @@ func executeCommand(command string, projectPath string, packagePattern string, r
log.Fatal(err)

Check warning on line 87 in instrgen/driver/main.go

View check run for this annotation

Codecov / codecov/patch

instrgen/driver/main.go#L84-L87

Added lines #L84 - L87 were not covered by tests
}
return nil
case "--dumpcfg":
prog, err := LoadProgram(projectPath, ginfo)
if err != nil {
fmt.Println(err)
return err
}

backwardCallGraph := makeCallGraph(packagePattern, prog, ginfo)
dumpCallGraph(backwardCallGraph)
return nil
case "--rootfunctions":
prog, err := LoadProgram(projectPath, ginfo)
if err != nil {
fmt.Println(err)
return err
}
rootFunctions := makeRootFunctions(prog, ginfo, packagePattern)
dumpRootFunctions(rootFunctions)
return nil
case "--prune":
entry := strings.Split(entryPoint, ".")
data := InstrgenCmd{projectPath, packagePattern, "prune", "yes",
Expand Down
Loading

0 comments on commit 8b48c7a

Please sign in to comment.