在Ceedling中以命令钩子运行Valgrind时遇错误的解决办法
问题:Ceedling联动Valgrind时,测试失败/内存泄漏触发异常
按照教程配置Ceedling与Valgrind配合使用,测试通过时一切正常,但当测试用例失败或Valgrind检测到内存泄漏时,Ceedling会抛出异常终止运行。
Ceedling运行时的错误输出如下:
> Shell executed command: `valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=10 build/test/out/test_soc_soh_module/test_soc_soh_module.out` > Produced output: test_soc_soh_module.c:24:test_calculates_soc_correctly:FAIL: Expected 13 Was 1 ----------------------- 1 Tests 1 Failures 0 Ignored FAIL ==62521== Memcheck, a memory error detector ==62521== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==62521== Using Valgrind-3.24.0 and LibVEX; rerun with -h for copyright info ==62521== Command: build/test/out/test_soc_soh_module/test_soc_soh_module.out ==62521== ==62521== ==62521== HEAP SUMMARY: ==62521== in use at exit: 0 bytes in 0 blocks ==62521== total heap usage: 1 allocs, 1 frees, 4,096 bytes allocated ==62521== ==62521== All heap blocks were freed -- no leaks are possible ==62521== ==62521== For lists of detected and suppressed errors, rerun with: -s ==62521== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) > And terminated with exit code: [1] 🪲 ERROR: Exception raised in plugin `command_hooks` within build hook :post_test_fixture_execute Plugin | CommandHooks > :post_test... 🧨 EXCEPTION: undefined method `split' for nil:NilClass 🌱 Ceedling could not complete operations because of errors Plugin | CommandHooks > :post_error...
单独执行上述Valgrind命令时,能正常输出测试失败信息和Valgrind检测结果,无异常:
valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all --errors-for-leak-kinds=all --error-exitcode=10 build/test/out/test_soc_soh_module/test_soc_soh_module.out ==62928== Memcheck, a memory error detector ==62928== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al. ==62928== Using Valgrind-3.24.0 and LibVEX; rerun with -h for copyright info ==62928== Command: build/test/out/test_soc_soh_module/test_soc_soh_module.out ==62928== test_soc_soh_module.c:24:test_calculates_soc_correctly:FAIL: Expected 13 Was 1 ----------------------- 1 Tests 1 Failures 0 Ignored FAIL ==62928== ==62928== HEAP SUMMARY: ==62928== in use at exit: 0 bytes in 0 blocks ==62928== total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated ==62928== ==62928== All heap blocks were freed -- no leaks are possible ==62928== ==62928== For lists of detected and suppressed errors, rerun with: -s ==62928== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
解决方案
- 修复钩子脚本的nil值判断:异常提示是
undefined method 'split' for nil:NilClass,说明command_hooks插件的后置钩子脚本中,某个变量未正确赋值就调用了split方法。找到project.yml配置的钩子脚本路径,在调用split前添加变量非空判断逻辑。 - 区分退出码场景:测试失败时测试程序返回退出码1,Valgrind检测到泄漏时返回配置的
--error-exitcode=10,钩子脚本需要分别处理这两种场景的输出解析,避免因测试失败导致的输出格式变化引发变量为空。 - 调整Ceedling或Valgrind配置:修改
project.yml中Valgrind的调用参数,或者调整Ceedling对测试输出的解析规则,确保在测试失败或泄漏检测时,插件能正确捕获输出内容,不会出现空变量。
内容的提问来源于stack exchange,提问作者Asith Warnakulasuriya
相关产品推荐
相关产品推荐

