From c7aae269be43f810204d8741ae75abb60fbcd7f0 Mon Sep 17 00:00:00 2001 From: minias Date: Wed, 26 Jun 2024 15:13:38 +0800 Subject: [PATCH] #177 update& markdown lint update --- .markdownlint.json | 11 +++++++++ README.md | 60 +++++++++++++++++++++++++--------------------- analysis.go | 37 +++++++++------------------- go.mod | 2 +- main.go | 2 +- 5 files changed, 57 insertions(+), 55 deletions(-) create mode 100644 .markdownlint.json diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100644 index 0000000..7e0d6ce --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,11 @@ +{ + "line-length": false, + "MD033": { + "allowed_elements": [ + "a", + "p", + "img", + "h1" + ] + } +} diff --git a/README.md b/README.md index d8c7068..8ac0187 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# README +

gopher

go-callvis

@@ -9,14 +11,14 @@ Slack channel

-

go-callvis is a development tool to help visualize call graph of a Go program using interactive view.

+

**go-callvis** is a development tool to help visualize call graph of a Go program using interactive view.

--- ## 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 @@ -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 @@ -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 ` +`go-callvis ` HTTP server is listening on [http://localhost:7878/](http://localhost:7878/) by default, use option `-http="ADDR:PORT"` to change HTTP server address. @@ -88,48 +90,50 @@ The output format defaults to `svg`, use option `-format=` 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. @@ -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. diff --git a/analysis.go b/analysis.go index 1ca13d0..e45b616 100644 --- a/analysis.go +++ b/analysis.go @@ -5,10 +5,6 @@ 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" @@ -16,8 +12,13 @@ import ( "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" ) @@ -25,13 +26,12 @@ import ( 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 @@ -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 @@ -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) } diff --git a/go.mod b/go.mod index 8ccf285..432240e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ofabry/go-callvis -go 1.19 +go 1.22 require ( github.com/goccy/go-graphviz v0.1.3 diff --git a/main.go b/main.go index 57af775..c6b4224 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ var ( outputFile = flag.String("file", "", "output filename - omit to use server mode") outputFormat = flag.String("format", "svg", "output file format [svg | png | jpg | ...]") cacheDir = flag.String("cacheDir", "", "Enable caching to avoid unnecessary re-rendering, you can force rendering by adding 'refresh=true' to the URL query or emptying the cache directory") - callgraphAlgo = flag.String("algo", string(CallGraphTypePointer), fmt.Sprintf("The algorithm used to construct the call graph. Possible values inlcude: %q, %q, %q", + callgraphAlgo = flag.String("algo", string(CallGraphTypeStatic), fmt.Sprintf("The algorithm used to construct the call graph. Possible values inlcude: %q, %q, %q", CallGraphTypeStatic, CallGraphTypeCha, CallGraphTypeRta)) debugFlag = flag.Bool("debug", false, "Enable verbose log.")