核心转储和 gcov 覆盖率报告

发布于 2024-11-01 18:32:57 字数 273 浏览 5 评论 0原文

我正在对多线程程序进行压力测试并收集覆盖率。 据我所知,当程序被 _exit() 或某些信号(例如 SIGABRT、SIGSEGV 等)终止时,gcov 不会生成 .gcda 文件。

当程序崩溃时,信号会生成核心文件,并且不会生成 gcov 覆盖率数据。显然我可以处理信号并生成覆盖数据,但在这种情况下我无法生成核心转储文件。但我想生成核心转储和 gcov 数据文件来找出崩溃的原因。

我的问题是,有没有办法在没有信号的情况下生成核心转储,或者有没有办法在程序突然终止时生成 gcov 覆盖数据文件?

I'm doing stress testing on multi-threaded program and collecting coverage as well.
As far as I know, gcov doesn't produce .gcda files when program is terminated by _exit() or some signals such as SIGABRT, SIGSEGV and so on.

When the program crashes, core file is generated by signal and gcov coverage data isn't generated. Obviously I could handle the signal and generate the coverage data but in this case I couldn't generate core dump file. But I'd like to generate both core dump and gcov data file to figure out the cause of the crash.

My question is that is there any way to generate core dump without signals or is there any way to generate gcov coverage data file when the program abruptly terminates?

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

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

发布评论

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

评论(4

绝不服输 2024-11-08 18:32:57

如果您需要自动进行代码覆盖率回归测试。试试这个:

https: //www.osadl.org/Dumping-gcov-data-at-runtime-simple-ex.online-coverage-analysis.0.html

在程序的“main.c”中放置:

static unsigned long long i = 0;
void __gcov_flush(void); /* check in gcc sources gcc/gcov-io.h for the prototype */

void my_handler(int signum)
{
  printf("received signal\n");
  printf("%llu\n", i);
  __gcov_flush(); /* dump coverage data on receiving SIGUSR1 */
}

int main(int argc, char **argv)
{
  struct sigaction new_action, old_action;
  int n;

  /* setup signal hander */
  new_action.sa_handler = my_handler;
  sigemptyset(&new_action.sa_mask);
  new_action.sa_flags = 0;

  sigaction(SIGUSR1, NULL, &old_action);
  if (old_action.sa_handler != SIG_IGN)
    sigaction (SIGUSR1, &new_action, NULL);
  //blah......

然后重新构建你的程序并运行:

$ ./hello &
$ killall -USR1 hello
received signal
2514147346

这样它仍然应该生成 .gcda 文件

$ gcov hello
File 'hello.c'
Lines executed:100.00% of 14
hello.c:creating 'hello.c.gcov'

If you have need to do regression testing of code coverage automatedly. Try this:

https://www.osadl.org/Dumping-gcov-data-at-runtime-simple-ex.online-coverage-analysis.0.html

Inside your program's "main.c" put:

static unsigned long long i = 0;
void __gcov_flush(void); /* check in gcc sources gcc/gcov-io.h for the prototype */

void my_handler(int signum)
{
  printf("received signal\n");
  printf("%llu\n", i);
  __gcov_flush(); /* dump coverage data on receiving SIGUSR1 */
}

int main(int argc, char **argv)
{
  struct sigaction new_action, old_action;
  int n;

  /* setup signal hander */
  new_action.sa_handler = my_handler;
  sigemptyset(&new_action.sa_mask);
  new_action.sa_flags = 0;

  sigaction(SIGUSR1, NULL, &old_action);
  if (old_action.sa_handler != SIG_IGN)
    sigaction (SIGUSR1, &new_action, NULL);
  //blah......

Then re-build your program and run:

$ ./hello &
$ killall -USR1 hello
received signal
2514147346

this way it should still generate .gcda files

$ gcov hello
File 'hello.c'
Lines executed:100.00% of 14
hello.c:creating 'hello.c.gcov'
怼怹恏 2024-11-08 18:32:57

您需要做的是,在开始测量测试覆盖率之前修复错误。

如果您的程序未通过其他测试,那么覆盖率信息无论如何都是毫无意义的。崩溃显然是某种故障,因此您需要解决这个问题。

修复错误,然后您就可以了解您的(无故障)程序的测试效果如何。

如果您编写一个自动化测试来重现崩溃,以确保它随后不会回归,也许会有帮助?

What you need to do is, fix the bugs before you start measuring test coverage.

If your program is failing other tests, coverage information is meaningless anyway. Crashing is clearly a failure of some kind, so you need to fix this.

Fix the bugs, then you can find out how effectively your (non-faulty) program is being tested.

Perhaps it will help if you write an automated test to reproduce the crash, to ensure it doesn't subsequently regress?

暮年慕年 2024-11-08 18:32:57

我大力提倡在运行测试时仅使用代码覆盖率 - 确定性测试。通过单元测试获得 100% 的线路覆盖率是可能的(也是可取的)。

另外,对于测试,如果您确实遇到某种崩溃,很容易在源代码管理中禁用测试,直到其修复。

I am a big advocate of only using code coverage whilst running tests - deterministic tests. Its possible (and desirable) to get 100% line coverage with unit tests.

Also with tests, if you do get a crash of some sort it would be easy to disable the test in source control until its fixed.

音盲 2024-11-08 18:32:57

查看 valgrind (或发布代码,以便我们提供帮助)

Have a look at valgrind (or post code so we can help)

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