建立C代码的调用树
我有大量用 C 编写的代码,但并不是我自己编写的。我希望在代码中创建一个调用结构的概述以供参考。也就是说:我希望知道代码中的不同函数调用了哪些(非标准)函数,从而创建不同函数的层次结构或树。是否有任何免费的、Unix 兼容的程序(这意味着没有 Visual Studio,但 Vim 插件或类似的东西就可以了)可以做到这一点,或者我必须自己编写一些可以做到这一点的东西?
I have a large code written in C, but I did not write all of it myself. I wish to create an overview of the call structure in the code for reference. That is: I wish to know what (non-standard) functions are called by the different functions in the code, and thus create a hierarchy or a tree of the different functions. Are there any free, Unix compatible programs (that means no Visual Studio, but a Vim plugin or such would be neat) that can do this, or will I have to write something that can do this myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有关可用工具的概述,请参阅
http://en.wikipedia.org/wiki/Call_graph
有一个 Vim 插件 C Call-名为 CCTree 的树浏览器
http://www.vim.org/scripts/script.php?script_id=2368
For an overview of available tools see
http://en.wikipedia.org/wiki/Call_graph
There is a Vim plugin C Call-Tree Explorer called CCTree
http://www.vim.org/scripts/script.php?script_id=2368
Doxygen 也可以这样做,但必须启用它。
Doxygen does that too, it has to be enabled though.
正如您提到的 Vim 插件,请查看 http://sites.google.com/site/vimcctree /。它使用 CScope 生成树,因此您需要首先生成源文件的 CScope 数据库。
As you mentioned a Vim plug-in, check out http://sites.google.com/site/vimcctree/. It uses CScope to generate the tree, so you will need to first generate a CScope db of your source files.
看一下 http://www.gson.org/egypt/ 这使用 GCC 来处理代码并从它发出的 AST 中提取程序内的相互依赖关系。
Have a look at http://www.gson.org/egypt/ This uses GCC to process the code and extracts the interdependencies within the program from the AST it emits.
gprof 会做到这一点。它还生成一个执行配置文件,但这样做时会创建一个调用树。
gprof will do that. It also generates an execution profile, but in doing so it creates a call tree.
clang++
(C 预处理器)和opt
(llvm 优化器)可用于生成带有图形的点文件。然后可以使用dot
来生成绘图。示例:
这适用于 clang/llvm 14,其他版本可能需要一些不同的参数(例如,较旧的
opt
可能需要-analysis
而不是-enable -new-pm=0
)。clang++
(C preprocessor) andopt
(llvm optimizer) can be used to generate the dot file with graph. Thendot
can be used to generate drawing.An example:
This works with clang/llvm 14, other versions may need a bit different parameters (eg. an older
opt
may need-analyse
instead of-enable-new-pm=0
).我刚刚下载了 SourceTrail (https://github.com/CoatiSoftware/Sourcetrail/releases) 并它做了我想要的,这非常接近我认为你想要的。
(我想要的是找出哪些例程调用了我正在考虑更改或需要理解的函数)。
请注意,它不再被维护,但它完全符合我的要求。它在 Windows 和 Linux 下运行,并且使得查找谁调用函数变得非常简单(以及根据需要跟踪该函数的调用树)。如果你关心的话,它有一个 GUI(是 GUI?无论如何)。
它自己进行解析,但运行时间并不长,可能与编译代码的时间大约相同或稍短。
但是,如果您只想要文本,或者不想使用图形用户界面,或者不想让它扫描代码,那么这不适合您。
(注释 - 就我而言,我过度关注一两个函数,并且不关心正在调用哪些系统函数。我花了一些时间删除所有需要的包含文件(因为我运行了解析)一台机器(一台 Linux 机器)没有我正在查看的 Windows 程序所需的所有包含文件,然后在另一台(Windows)机器上进行了探索,我应该提到,它运行得很好。将整个源代码树从我的 Linux 机器复制到我的Windows 机器(包括 Sourcetail 项目文件),加载 Sourcetail 并让它加载项目 - 完成。)
I just downloaded SourceTrail (https://github.com/CoatiSoftware/Sourcetrail/releases) and it did what I wanted, which was pretty close to what I think you want.
(What I wanted was to find out what routines called the function I was considering changing, or needed to understand).
Note that it is no longer maintained, but it did exactly what I wanted. It runs under Windows and Linux, and made finding who calls a function pretty trivial (as well as following that function's call tree down as needed). If you care, it has a GUI (is a GUI? whatever).
It does the parsing itself, but it didn't take very long to run, perhaps about the same time or a little less than compiling the code.
But if you want text only, or don't want to use a gui, or don't want to have it scan the code, this isn't for you.
(Notes - in my case, I was hyper-focused on one or 2 functions, and didn't care what system functions were being called. I spent some time stubbing out all the include files that were needed (since I ran the parse on one machine (A Linux machine) that didn't have all the include files needed for the Windows program I was looking at, and then did the exploration on a different (Windows) machine. Which, I should mention, worked perfectly. I just copied the entire source tree from my Linux machine to my Windows machine (which included the Sourcetail project file), loaded Sourcetail and had it load the project - done.)