简体   繁体   English

创建调用图

[英]Creating call graph

I am looking for a possibility to generate a call graph for Go projects.我正在寻找为 Go 项目生成调用图的可能性。 Something similar to Doxygen's diagram functionality for C++ classes (with the option CALL_GRAPH=YES).类似于Doxygen 的C++ 类的图表功能(带有选项 CALL_GRAPH=YES)。

So far I found到目前为止我发现

http://saml.rilspace.org/profiling-and-creating-call-graphs-for-go-programs-with-go-tool-pprof http://saml.rilspace.org/profiling-and-creating-call-graphs-for-go-programs-with-go-tool-pprof
or或者
http://blog.golang.org/profiling-go-programs http://blog.golang.org/profiling-go-programs

This samples the call stack of your program 100 times per second while the program is running and creates a graph useful for profiling.这会在程序运行时每秒对程序的调用堆栈进行 100 次采样,并创建一个对分析有用的图表。 If your program spends most of its time in functions not relevant to you, I found this solution not very usefull.如果您的程序大部分时间都花在与您无关的功能上,我发现这个解决方案不是很有用。

Then there is this:然后是这个:

https://godoc.org/golang.org/x/tools/go/callgraph/static https://godoc.org/golang.org/x/tools/go/callgraph/static

which from its description sounds like what I would need, but there seem to be no docs and I don't understand how to use it.从它的描述来看,这听起来像是我需要的,但似乎没有文档,我不明白如何使用它。

I also found我还发现

https://github.com/davecheney/graphpkg/blob/master/README.mdhttps://github.com/davecheney/graphpkg/blob/master/README.md
and
https://github.com/paetzke/go-dep-graph/blob/master/README.org https://github.com/paetzke/go-dep-graph/blob/master/README.org

but they create only dependency graphs.但他们只创建依赖图。

Take a look here:看看这里:

func main() {
   defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
   // Rest of program
}

Build and run your program as per normal.按照正常方式构建并运行您的程序。 You'll see the profiling hook mentioned:你会看到提到的分析钩子:

2015/07/12 09:02:02 profile: cpu profiling enabled, cpu.pprof

Run your program (bench it, run through it, etc) to generate the profile during runtime.运行您的程序(测试它,运行它等)以在运行时生成配置文件。 Once you've hit what you want, quit and then generate the call-graph:一旦你找到你想要的东西,退出然后生成调用图:

go tool pprof -pdf -output cgraph.pdf $YOURPROGBINARY cpu.pprof

You can also run go tool pprof $YOURPROGBINARY cpu.pprof to get an interactive prompt where you can call top10 or web to generate an svg.您还可以运行go tool pprof $YOURPROGBINARY cpu.pprof来获得交互式提示,您可以在其中调用top10web来生成 svg。 Type help at the pprof prompt to get a list of commands.在 pprof 提示符下键入help以获取命令列表。

eg - here's the CPU profile for a buffer pool implementation I wrote:例如 - 这是我编写的缓冲池实现的 CPU 配置文件:

~/Desktop go tool pprof poolio cpu.pprof
Entering interactive mode (type "help" for commands)
(pprof) top5
24770ms of 35160ms total (70.45%)
Dropped 217 nodes (cum <= 175.80ms)
Showing top 5 nodes out of 74 (cum >= 650ms)
    flat  flat%   sum%        cum   cum%
 12520ms 35.61% 35.61%    12520ms 35.61%  runtime.mach_semaphore_wait
  9300ms 26.45% 62.06%     9360ms 26.62%  syscall.Syscall
  1380ms  3.92% 65.98%     2120ms  6.03%  encoding/json.(*encodeState).string
  1030ms  2.93% 68.91%     1030ms  2.93%  runtime.kevent
   540ms  1.54% 70.45%      650ms  1.85%  runtime.mallocgc

And here's a quick way to generate a PNG from the prompt:这是从提示中生成 PNG 的快速方法:

(pprof) png > graph.png
Generating report in graph.png

Which outputs this:输出这个:

callgraph-example-poolio

You were close with … /x/tools/go/callgraph/static .你很接近 ... /x/tools/go/callgraph/static I'm pretty sure go install golang.org/x/tools/cmd/callgraph is what you want.我很确定go install golang.org/x/tools/cmd/callgraph是你想要的。 Once installed run it without arguments to see it's full help/usage.安装后不带参数运行它以查看它的完整帮助/用法。

(In general, the things under … /x/tools/ are somewhat reusable packages with command line front-ends living under … /x/tools/cmd , you can install them all with go install golang.org/x/tools/cmd/... , the literal /... matches all sub-packages). (通常,... /x/tools/下的东西是一些可重用的包,命令行前端位于... /x/tools/cmd ,您可以使用go install golang.org/x/tools/cmd/...安装它们go install golang.org/x/tools/cmd/... ,文字/...匹配所有子包)。

Eg running just callgraph produces usage output that starts with:例如,只运行callgraph会产生以以下内容开头的用法输出:

callgraph : display the the call graph of a Go program. callgraph : 显示 Go 程序的调用图。

Usage:用法:

callgraph [-algo=static|cha|rta|pta] [-test] [-format=...] <args>...

Flags:标志:

-algo Specifies the call-graph construction algorithm, one of: -algo指定调用图构造算法,其中之一:

 static static calls only (unsound) cha Class Hierarchy Analysis rta Rapid Type Analysis pta inclusion-based Points-To Analysis The algorithms are ordered by increasing precision in their treatment of dynamic calls (and thus also computational cost). RTA and PTA require a whole program (main or test), and include only functions reachable from main.

-test Include the package's tests in the analysis. -test在分析中包括包的测试。

-format Specifies the format in which each call graph edge is displayed. -format指定显示每个调用图边的格式。 One of:之一:

 digraph output suitable for input to golang.org/x/tools/cmd/digraph. graphviz output in AT&T GraphViz (.dot) format.

It can produce arbitrary formatted output (using Go's template syntax) or graphviz or digraph output.它可以生成任意格式的输出(使用 Go 的模板语法)或 graphviz 或 digraph 输出。 The last is a tool you can install with go install golang.org/x/tools/cmd/digraph (and once again, full/help usage is seen by running it without arguments) and can answer queries about arbitrary directed graphs (including call graphs obviously).最后一个是你可以使用go install golang.org/x/tools/cmd/digraph安装的工具(再次,通过不带参数运行它可以看到完整/帮助的使用情况)并且可以回答关于任意有向图的查询(包括调用图很明显)。

Another approach, which does usegolang.org/x/tools go/callgraph is the ofabry/go-callvis project:另一种使用golang.org/x/tools go/callgraphofabry/go-callvis项目:
(Go 1.13+) (转到 1.13+)

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.此工具的目的是使用来自调用图的数据及其与包和类型的关系,为开发人员提供 Go 程序的可视化概览。

  • support for Go modules!支持 Go 模块!
  • focus specific package in the program专注于程序中的特定包
  • click on package to quickly switch the focus using interactive viewer单击包以使用交互式查看器快速切换焦点
  • group functions by package and/or methods by type按包和/或按类型对方法进行分组
  • filter packages to specific import path prefixes过滤包到特定的导入路径前缀
  • ignore funcs from standard library忽略标准库中的函数
  • omit various types of function calls省略各种类型的函数调用

Example (based on this Go code project ):示例(基于此Go 代码项目):

https://raw.githubusercontent.com/ofabry/go-callvis/master/images/main.png

How it works这个怎么运作

It runs pointer analysis to construct the call graph of the program and uses the data to generate output in dot format , which can be rendered with Graphviz tools.它运行指针分析来构建程序的调用图,并使用数据生成点格式的输出,可以使用 Graphviz 工具进行渲染。

To use the interactive view provided by a web server that serves SVG images of focused packages, you can simply run:要使用 Web 服务器提供的交互式视图来提供聚焦包的 SVG 图像,您只需运行:

 go-callvis <target package>

HTTP server is listening on http://localhost:7878/ by default, use option -http="ADDR:PORT" to change HTTP server address. HTTP 服务器默认监听 http://localhost:7878/,使用选项-http="ADDR:PORT"更改 HTTP 服务器地址。

我最近使用了 golang callgraph,我用 python + callgraph 构建了一个名为 CallingViewer 的 web 工具: https://github.com/fiefdx/CallingViewer ,它可能很粗糙,但它工作,下面的截图:CallingViewer 的截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM