Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request PR again after modifying build & test 1.22. #187

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go: [ '1.20', '1.19' ]
go: [ '1.22' ]

steps:
- name: "Checkout"
Expand Down
11 changes: 11 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"line-length": false,
"MD033": {
"allowed_elements": [
"a",
"p",
"img",
"h1"
]
}
}
60 changes: 33 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# README

<p align="center"><img src="images/gopher.png" alt="gopher"></p>

<h1 align="center">go-callvis</h1>
Expand All @@ -9,14 +11,14 @@
<a href="https://gophers.slack.com/archives/go-callvis"><img src="https://img.shields.io/badge/gophers%20slack-%23go--callvis-ff69b4.svg" alt="Slack channel"></a>
</p>

<p align="center"><b>go-callvis</b> is a development tool to help visualize call graph of a Go program using interactive view.</p>
<p align="center">**go-callvis** is a development tool to help visualize call graph of a Go program using interactive view.</p>

---

## Introduction

The purpose of this tool is to provide developers with a visual overview of a Go program using data from call graph
and its relations with packages and types. This is especially useful in larger projects where the complexity of
The purpose of this tool is to provide developers with a visual overview of a Go program using data from call graph
and its relations with packages and types. This is especially useful in larger projects where the complexity of
the code much higher or when you are just simply trying to understand code of somebody else.

### Features
Expand All @@ -37,7 +39,7 @@ the code much higher or when you are just simply trying to understand code of so

### How it works

It runs [pointer analysis](https://godoc.org/golang.org/x/tools/go/pointer) to construct the call graph of the program and
It runs [pointer analysis](https://godoc.org/golang.org/x/tools/go/pointer) to construct the call graph of the program and
uses the data to generate output in [dot format](http://www.graphviz.org/content/dot-language), which can be rendered with Graphviz tools.

## Quick start
Expand Down Expand Up @@ -76,7 +78,7 @@ make install

To use the interactive view provided by a web server that serves SVG images of focused packages, you can simply run:

`go-callvis <target package>`
`go-callvis <target package>`

HTTP server is listening on [http://localhost:7878/](http://localhost:7878/) by default, use option `-http="ADDR:PORT"` to change HTTP server address.

Expand All @@ -88,48 +90,50 @@ The output format defaults to `svg`, use option `-format=<svg|png|jpg|...>` to p

#### Options

```
```sh
Usage of go-callvis:
-debug
Enable verbose log.
Enable verbose log.
-file string
output filename - omit to use server mode
output filename - omit to use server mode
-cacheDir string
Enable caching to avoid unnecessary re-rendering.
Enable caching to avoid unnecessary re-rendering.
-focus string
Focus specific package using name or import path. (default "main")
Focus specific package using name or import path. (default "main")
-format string
output file format [svg | png | jpg | ...] (default "svg")
output file format [svg | png | jpg | ...] (default "svg")
-graphviz
Use Graphviz's dot program to render images.
Use Graphviz's dot program to render images.
-group string
Grouping functions by packages and/or types [pkg, type] (separated by comma) (default "pkg")
Grouping functions by packages and/or types [pkg, type] (separated by comma) (default "pkg")
-http string
HTTP service address. (default ":7878")
HTTP service address. (default ":7878")
-ignore string
Ignore package paths containing given prefixes (separated by comma)
Ignore package paths containing given prefixes (separated by comma)
-include string
Include package paths with given prefixes (separated by comma)
Include package paths with given prefixes (separated by comma)
-limit string
Limit package paths to given prefixes (separated by comma)
Limit package paths to given prefixes (separated by comma)
-minlen uint
Minimum edge length (for wider output). (default 2)
Minimum edge length (for wider output). (default 2)
-nodesep float
Minimum space between two adjacent nodes in the same rank (for taller output). (default 0.35)
Minimum space between two adjacent nodes in the same rank (for taller output). (default 0.35)
-nointer
Omit calls to unexported functions.
Omit calls to unexported functions.
-nostd
Omit calls to/from packages in standard library.
Omit calls to/from packages in standard library.
-rankdir
Direction of graph layout [LR | RL | TB | BT] (default "LR")
Direction of graph layout [LR | RL | TB | BT] (default "LR")
-skipbrowser
Skip opening browser.
Skip opening browser.
-tags build tags
a list of build tags to consider satisfied during the build. For more information about build tags, see the description of build constraints in the documentation for the go/build package
a list of build tags to consider satisfied during the build. For more information about build tags, see the description of build constraints in the documentation for the go/build package
-tests
Include test code.
Include test code.
-algo string
Use specific algorithm for package analyzer: static, cha or rta (default "static")
-version
Show version and exit.
Show version and exit.
```

Run `go-callvis -h` to list all supported options.
Expand Down Expand Up @@ -181,15 +185,17 @@ Join [#go-callvis](https://gophers.slack.com/archives/go-callvis) channel at [go
### How to help

Did you find any bugs or have some suggestions?

- Feel free to open [new issue](https://github.com/ofabry/go-callvis/issues/new) or start discussion in the slack channel.

Do you want to contribute to the project?

- Fork the repository and open a pull request. [Here](https://github.com/ofabry/go-callvis/projects/1) you can find TODO features.

---

#### Roadmap

##### The *interactive tool* described below has been published as a *separate project* called [goexplorer](https://github.com/ofabry/goexplorer)!
##### The *interactive tool* described below has been published as a *separate project* called [goexplorer](https://github.com/ofabry/goexplorer)

> Ideal goal of this project is to make web app that would locally store the call graph data and then provide quick access of the call graphs for any package of your dependency tree. At first it would show an interactive map of overall dependencies between packages and then by selecting particular package it would show the call graph and provide various options to alter the output dynamically.
39 changes: 12 additions & 27 deletions analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ import (
"fmt"
"go/build"
"go/types"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/callgraph/rta"
"golang.org/x/tools/go/callgraph/static"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"

"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/callgraph/rta"
"golang.org/x/tools/go/callgraph/static"

"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/pointer"

"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)

type CallGraphType string

const (
CallGraphTypeStatic CallGraphType = "static"
CallGraphTypeCha = "cha"
CallGraphTypeRta = "rta"
CallGraphTypePointer = "pointer"
CallGraphTypeStatic CallGraphType = "static"
CallGraphTypeCha CallGraphType = "cha"
CallGraphTypeRta CallGraphType = "rta"
)

//==[ type def/func: analysis ]===============================================
// ==[ type def/func: analysis ]===============================================
type renderOpts struct {
cacheDir string
focus string
Expand Down Expand Up @@ -60,7 +60,7 @@ func mainPackages(pkgs []*ssa.Package) ([]*ssa.Package, error) {
return mains, nil
}

//==[ type def/func: analysis ]===============================================
// ==[ type def/func: analysis ]===============================================
type analysis struct {
opts *renderOpts
prog *ssa.Program
Expand Down Expand Up @@ -116,21 +116,6 @@ func (a *analysis) DoAnalysis(
roots = append(roots, main.Func("main"))
}
graph = rta.Analyze(roots, true).CallGraph
case CallGraphTypePointer:
mains, err := mainPackages(prog.AllPackages())
if err != nil {
return err
}
mainPkg = mains[0]
config := &pointer.Config{
Mains: mains,
BuildCallGraph: true,
}
ptares, err := pointer.Analyze(config)
if err != nil {
return err
}
graph = ptares.CallGraph
default:
return fmt.Errorf("invalid call graph type: %s", a.opts.algo)
}
Expand Down Expand Up @@ -231,7 +216,7 @@ func (a *analysis) OverrideByHTTP(r *http.Request) {
if inc := r.FormValue("include"); inc != "" {
a.opts.include[0] = inc
}
return
//return
}

// basically do printOutput() with previously checking
Expand Down
21 changes: 14 additions & 7 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

This document contains examples of various projects from Github.

#### Projects
- [Syncthing](https://github.com/syncthing/syncthing)
- [Docker](https://github.com/docker/docker)
- [Travis CI Worker](https://github.com/travis-ci/worker)
## Projects

- [Syncthing](https://github.com/syncthing/syncthing)
- [Docker](https://github.com/docker/docker)
- [Travis CI Worker](https://github.com/travis-ci/worker)

## Syncthing

Expand All @@ -20,7 +20,8 @@ cd $GOPATH/src/github.com/syncthing/syncthing
./build.sh
```

# Generate graph and launch webserver
## Generate graph and launch webserver

```sh
go-callvis -focus upgrade -group pkg,type -limit github.com/syncthing/syncthing -ignore github.com/syncthing/syncthing/lib/logger github.com/syncthing/syncthing/cmd/syncthing
```
Expand All @@ -34,26 +35,31 @@ go-callvis -focus upgrade -group pkg,type -limit github.com/syncthing/syncthing
```sh
go-callvis -format=png -file=syncthing_focus -focus upgrade -limit github.com/syncthing/syncthing github.com/syncthing/syncthing/cmd/syncthing
```

---

### Grouping by _packages_

[![syncthing example output pkg](../images/syncthing_group.png)](https://raw.githubusercontent.com/ofabry/go-callvis/master/images/syncthing_group.png)

# Generate graph focused on module 'upgrade', output to PNG file
## Generate graph focused on module 'upgrade', output to PNG file

```sh
go-callvis -format=png -file=syncthing_group -focus upgrade -group pkg -limit github.com/syncthing/syncthing github.com/syncthing/syncthing/cmd/syncthing
```

---

### Ignoring package _logger_

[![syncthing example output ignore](../images/syncthing_ignore.png)](https://raw.githubusercontent.com/ofabry/go-callvis/master/images/syncthing_ignore.png)

# Generate graph focused on module 'upgrade' and ignoring 'logger', output to webserver
## Generate graph focused on module 'upgrade' and ignoring 'logger', output to webserver

```sh
go-callvis -focus upgrade -group pkg -ignore github.com/syncthing/syncthing/lib/logger -limit github.com/syncthing/syncthing github.com/syncthing/syncthing/cmd/syncthing
```

---

## Docker
Expand All @@ -63,6 +69,7 @@ go-callvis -focus upgrade -group pkg -ignore github.com/syncthing/syncthing/lib/
```sh
go-callvis -format=png -file=docker -limit github.com/docker/docker -ignore github.com/docker/docker/vendor github.com/docker/docker/cmd/docker | dot -Tpng -o docker.png
```

---

## Travis CI Worker
Expand Down
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module github.com/ofabry/go-callvis

go 1.19
go 1.22

require (
github.com/goccy/go-graphviz v0.1.1
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
golang.org/x/tools v0.8.0
github.com/goccy/go-graphviz v0.1.3
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
golang.org/x/tools v0.22.0
)

require (
github.com/fogleman/gg v1.3.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/image v0.6.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
)
Loading
Loading