C 函数调用和参数跟踪 - 测试用例和模拟生成

发布于 2025-01-08 16:16:59 字数 359 浏览 2 评论 0原文

我在嵌入式系统上有一个由相当旧的 C 代码组成的大型代码库,不幸的是没有自动化测试用例/套件。这使得重组和重构代码成为一项危险的任务。

手动编写测试用例非常耗时,因此我认为应该可以自动化该过程的至少一部分,例如通过跟踪所有函数调用并记录输入和输出值。然后我可以在测试用例中使用这些值(这不适用于所有功能,但至少适用于某些功能)。也可能可以根据收集的数据创建模拟函数。

拥有这样的测试用例将使重构成为一项危险性较小的活动。

是否有任何解决方案可以做到这一点?如果我必须自己编写代码,最简单的方法是什么?

我考虑过使用ctags来查找函数定义,并将它们包装在记录参数值的函数中。另一种可能性可能是 gcc 编译器插件。

I have a large code base of quite old C code on an embedded system and unfortunately there are no automated test cases/suites. This makes restructuring and refactoring code a dangerous task.

Manually writing test cases is very time consuming, so I thought that it should be possible to automate at least some part of this process for instance by tracing all the function calls and recording of the input and output values. I could then use these values in the test cases (this would not work for all but at least for some functions). It would probably also be possible to create mock functions based on the gathered data.

Having such test cases would make refactoring a less dangerous activity.

Are there any solutions that already can do this? What would be the easiest way to get this to work if I had to code it myself?

I thought about using ctags to find the function definitions, and wrapping them in a function that records the parameter values. Another possibility would probably be a gcc compiler plugin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-01-15 16:16:59

有一个 gcc 选项“-finstrument-functions”,您可以使用该机制为每个函数的进入/退出定义自己的回调。

谷歌一下,你可以找到很多很好的例子。

[编辑] 使用此 gcc 选项的回调,您只能跟踪函数的进入/退出,而不能跟踪参数。但通过一些技巧,您也可以跟踪参数。 (遍历当前帧指针以获取堆栈上的参数)。

这里有一篇文章谈论实现的想法:

http://linuxgazette.net/151/melinte。此外

,取决于您的嵌入式系统,在linux上您可以尝试像ltrace这样的东西来显示参数(如strace方式)。有许多工具可以在 Linux 上的用户空间或内核空间中进行函数跟踪工作,ftrace/ust/ltrace/utrace/strace/systemtap/。无论如何,如果您不添加任何硬调试代码,就不可能以正确的方式显示参数。如果您接受添加入口/出口调试信息的努力,那么事情就会容易得多。

这里还有一个类似的帖子讨论这个问题。

跟踪 Linux 中本地函数调用的工具

There is a gcc option "-finstrument-functions", which mechanism you can use to define your own callbacks for each funtion's entry/exit.

Google it and you can find many good examples.

[Edit] with this gcc option's call back you can only track the function's entry/exit,not the params. but with some tricks you may also track the params. (walk through the current frame pointer to get the param on the stack).

Here is an article talk about the idea of the implementation:

http://linuxgazette.net/151/melinte.html

Furthermore, depends on your embedded system, on linux you can try something like ltrace to show the params(like the strace way). There are many tools do the function trace work either in userspace or kernelspace on linux, ftrace/ust/ltrace/utrace/strace/systemtap/. Anyway, if you do not add any hard debugging code, it's not possible to display the params in the correct way. If you accept the efforts to add entry/exit debugging infomation, then it's much easier.

Also here is a similar thread talk about this problem.

Tool to trace local function calls in Linux

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文