我正在STM32F042上开发一个应用程序。我从makefile驱动一切,包括我的单元测试。我使用OpenOCD和ST链接来闪现目标。我的单元测试在主机和目标上运行。主机单元测试驱动程序在成功时从main()返回0,在失败时返回非零,因此makefile知道测试是否通过。makefile在目标上闪烁并启动测试,但不知道它们是成功还是失败。嵌入式测试应用程序打开一个红色LED显示失败,绿色打开通过,所以我知道-现在我想自动化这一步。
我想在代码中设置两个断点,一个在失败处理程序中,一个在main末尾,并告诉OpenOCD,如果它碰到一个或另一个断点,就以零或非零状态退出。
因此,我的问题归结为两个具体的问题:
发布于 2019-09-27 15:04:25
我的结局是这样的。对于每个目标单元测试,我启动一个OpenOCD服务器并与gdb连接。Gdb运行一个脚本,它设置两个断点,一个用于成功,一个用于失败。如果它碰到任何一个断点,就会关闭OCD服务器,并使用与shell通信成功和失败的代码退出。要在主机上运行相同的测试,我只需将它们编译为常规可执行文件。
Makefile:
# target unit test binaries
foo_tests.elf bar_tests.elf baz_tests.elf bop_tests.elf: unit_test_runner.ao
# disable optimization for target unit test driver to avoid optimizing
# away functions that serve as breakpoint labels
unit_test_runner.ao: CFLAGS += -O0 -g
# link target unit test binaries for semihosting
%_tests.elf: ARM_LDLIBS += -specs=rdimon.specs -lrdimon
# host unit test binaries
foo_tests bar_time_tests baz_tests bop_tests: unit_test_runner.o
# run target unit test binaries through gdb and OpenOCD; redirecting stderr
# leaves printf output from `assert()' clearly visible on the console
%.tut: %.elf
openocd -f interface/stlink-v2-1.cfg -f target/stm32f0x.cfg 2> $@.log &
gdb-multiarc -batch-silent -x tut.gdb $< 2> $@-gdb.log
# run host binary
%.run: %
./$*
tests: foo_tests.run bar_time_tests.run baz_tests.run bop_tests.run \
foo_tests.tut bar_time_tests.tut baz_tests.tut bop_tests.tuttut.gdb:
target remote localhost:3333
monitor arm semihosting enable # let assert()'s printf() through
monitor reset halt
load
monitor reset init
break success # set breakpoint on function `sucess()'
commands # on hitting this bp, execute the following:
monitor shutdown # shutdown OpenOCD server
quit 0 # exit GDB with success code
end
break failure # set breakpoint on function `sucess()'
commands
monitor shutdown
quit 1 # exit GDB with failure code
end
continueunit_test_runner.c:
#include <stdlib.h>
/* These two functions serve as labels where gdb can place
breakpoints. */
void success() {}
void failure() {}
/* Implementation detail for `assert()' macro */
void assertion_failure(const char *file,
int line,
const char *function,
const char *expression)
{
printf("assertion failure in %s:%d (%s): `%s'\n",
file, line, function, expression);
failure();
exit(1);
}
/* This function is necessary for ARM semihosting */
extern void initialise_monitor_handles(void);
int main(int argc, char* argv[])
{
#ifdef __arm__
initialise_monitor_handles();
#endif
tests(); /* client code implements this function */
success();
return 0;
}https://stackoverflow.com/questions/57807905
复制相似问题