ondrajz / go-callvis

Visualize call graph of a Go program using Graphviz
https://ofabry.github.io/go-callvis
MIT License
5.91k stars 408 forks source link

Visualize call graph of Go libs #7

Closed suntong closed 7 years ago

suntong commented 7 years ago

Can go-callvis be used to visualize call graph for Go libs? I got go-callvis: no main packages by doing so, and no output generated.

Here are details:

gpkg=github.com/andrew-d/goscrape

$ go-callvis -limit $gpkg -focus $gpkg $gpkg | dot -Tpng -o /tmp/out.png
go-callvis: no main packages

$ ls /tmp/out.png
ls: cannot access '/tmp/out.png': No such file or directory
ondrajz commented 7 years ago

Main package is required for the analysis used, because all the calls in the callgraph tree begins in main (and inits).

In package you don't really know what will be called, since it's just a package without some start point. Program that will use the package can call anything or nothing.

suntong commented 7 years ago

Ok. Thanks.

suntong commented 7 years ago

Reopening the issue as I'm still unable to visualize call graph for Go libs, despite I've given it some start point:

go-callvis -group pkg,type -limit github.com/knq/xo github.com/knq/xo/examples/sqlite3 | dot -Tpng -o /tmp/out.png

and the result looks like this:

http://img.vim-cn.com/32/9823a165bf43ec1ce6fa5cacfee09b663543fe.png

I.e., the visualization is only limited within the "examples/sqlite3", but not down to the xo lib level, which is what I had hoped for, when using the -limit github.com/knq/xo parameter.

As a comparison, here is what the level of details of xo lib call graph I'm hoping to see -- http://img.vim-cn.com/a3/c9865f9ef44f1994b760bb3c5c058c37838df0.png

So it is possible then? Thanks.

ondrajz commented 7 years ago

By default the -focus flag is set to main so it only shows calls from and to that package.

You can use an empty string (-focus="") to show all the calls for the program. Although I warn you that it might generate huge output if you don't use -limit or -nostd.

suntong commented 7 years ago

Thanks a lot for your answer.

So I tried it:

go-callvis -group pkg,type -focus="" -limit github.com/knq/xo github.com/knq/xo/examples/sqlite3 | dot -Tpng -o /tmp/out.png

however, the result is almost like what I posted before:

http://img.vim-cn.com/70/d93fee55166de1f43504fd87e901b79c4fa7f7.png

Do you have a full command like above that I can try out myself please? It'd be great if you have and can try with any of the following packages:

github.com/andybalholm/cascadia/ github.com/beevik/etree/ github.com/deckarep/golang-set/ github.com/fatih/camelcase/ github.com/gnewton/chidley/ github.com/goji/ github.com/moovweb/gokogiri/ github.com/spf13/cobra/ github.com/zenazn/goji/

Or any smaller but useful packages that you have locally.

Thanks

ondrajz commented 7 years ago

I have tried project cobra's program github.com/spf13/cobra/cobra using:

go-callvis -group pkg -focus="" -limit github.com/spf13/cobra github.com/spf13/cobra/cobra | dot -Tpng -o cobra.png

Output: cobra Are you looking for something like this?

I don't really know what happens inside github.com/knq/xo/examples/sqlite3 since it's not published, but could it be that the output you got from go-callvis has the only calls that happen for that example (for github.com/knq/xo path prefix)?

ondrajz commented 7 years ago

By the way, what did you use to generate the graph you sent before? http://img.vim-cn.com/a3/c9865f9ef44f1994b760bb3c5c058c37838df0.png

suntong commented 7 years ago

Thanks a lot for your answer. I tried a lot of other packages afterwards, all worked fine. Then I suddenly realized that the ko package I was initially investigating was a generated code. :-( the worst one to pick. :-(

So, all good now. Thanks again for your help.

BTW, the graph I sent before, http://img.vim-cn.com/a3/c9865f9ef44f1994b760bb3c5c058c37838df0.png, was generated using go's native callgraph command, before I knew of go-callvis. There were much tedious manual cleanup afterward, that's why I'm interested in the automatically way provided by go-callvis. Thanks again for providing that.