From ed794337e713f1f4db02855c7e73c8b41a3c20ed Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Sat, 15 Sep 2018 04:59:07 +0800 Subject: [PATCH] [posix] enhance reset implementation to support code coverage (#3071) --- examples/apps/cli/Makefile.am | 4 ++++ examples/apps/cli/main.c | 20 ++++++++++++++++++++ examples/apps/ncp/Makefile.am | 4 ++++ examples/apps/ncp/main.c | 20 ++++++++++++++++++++ examples/platforms/posix/Makefile.am | 4 ++++ examples/platforms/posix/misc.c | 20 ++++---------------- examples/platforms/posix/system.c | 6 ------ src/posix/Makefile.am | 4 ++++ src/posix/main.c | 17 ++++++++++++++--- src/posix/platform/misc.c | 22 ++++------------------ 10 files changed, 78 insertions(+), 43 deletions(-) diff --git a/examples/apps/cli/Makefile.am b/examples/apps/cli/Makefile.am index 04259b2f6..9fbd003ae 100644 --- a/examples/apps/cli/Makefile.am +++ b/examples/apps/cli/Makefile.am @@ -129,6 +129,10 @@ ot_cli_mtd_LDFLAGS += -Wl,-Ma endif if OPENTHREAD_BUILD_COVERAGE +CPPFLAGS_COMMON += \ + -DOPENTHREAD_ENABLE_COVERAGE \ + $(NULL) + CLEANFILES = $(wildcard *.gcda *.gcno) endif # OPENTHREAD_BUILD_COVERAGE diff --git a/examples/apps/cli/main.c b/examples/apps/cli/main.c index e35ac5e37..6306d7989 100644 --- a/examples/apps/cli/main.c +++ b/examples/apps/cli/main.c @@ -37,6 +37,15 @@ #include "openthread-system.h" +#if OPENTHREAD_EXAMPLES_POSIX +#include +#include + +jmp_buf gResetJump; + +void __gcov_flush(); +#endif + #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES void *otPlatCAlloc(size_t aNum, size_t aSize) { @@ -58,6 +67,17 @@ int main(int argc, char *argv[]) { otInstance *sInstance; +#if OPENTHREAD_EXAMPLES_POSIX + if (setjmp(gResetJump)) + { + alarm(0); +#if OPENTHREAD_ENABLE_COVERAGE + __gcov_flush(); +#endif + execvp(argv[0], argv); + } +#endif + #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES size_t otInstanceBufferLength = 0; uint8_t *otInstanceBuffer = NULL; diff --git a/examples/apps/ncp/Makefile.am b/examples/apps/ncp/Makefile.am index 8fb373a26..77db7276a 100644 --- a/examples/apps/ncp/Makefile.am +++ b/examples/apps/ncp/Makefile.am @@ -180,6 +180,10 @@ ot_ncp_radio_LDFLAGS += -Wl,-Ma endif if OPENTHREAD_BUILD_COVERAGE +CPPFLAGS_COMMON += \ + -DOPENTHREAD_ENABLE_COVERAGE \ + $(NULL) + CLEANFILES = $(wildcard *.gcda *.gcno) endif # OPENTHREAD_BUILD_COVERAGE diff --git a/examples/apps/ncp/main.c b/examples/apps/ncp/main.c index f2fbfe15b..3c6792fd7 100644 --- a/examples/apps/ncp/main.c +++ b/examples/apps/ncp/main.c @@ -36,6 +36,15 @@ #include "openthread-system.h" +#if OPENTHREAD_EXAMPLES_POSIX +#include +#include + +jmp_buf gResetJump; + +void __gcov_flush(); +#endif + #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES void *otPlatCAlloc(size_t aNum, size_t aSize) { @@ -57,6 +66,17 @@ int main(int argc, char *argv[]) { otInstance *sInstance; +#if OPENTHREAD_EXAMPLES_POSIX + if (setjmp(gResetJump)) + { + alarm(0); +#if OPENTHREAD_ENABLE_COVERAGE + __gcov_flush(); +#endif + execvp(argv[0], argv); + } +#endif + #if OPENTHREAD_ENABLE_MULTIPLE_INSTANCES size_t otInstanceBufferLength = 0; uint8_t *otInstanceBuffer = NULL; diff --git a/examples/platforms/posix/Makefile.am b/examples/platforms/posix/Makefile.am index e7f2e9e23..f9f64c0cd 100644 --- a/examples/platforms/posix/Makefile.am +++ b/examples/platforms/posix/Makefile.am @@ -71,6 +71,10 @@ libopenthread_posix_a_LIBADD = \ $(shell find $(top_builddir)/examples/platforms/utils $(Dash)type f $(Dash)name "*.o") if OPENTHREAD_BUILD_COVERAGE +libopenthread_posix_a_CPPFLAGS += \ + -DOPENTHREAD_ENABLE_COVERAGE \ + $(NULL) + CLEANFILES = $(wildcard *.gcda *.gcno) endif # OPENTHREAD_BUILD_COVERAGE diff --git a/examples/platforms/posix/misc.c b/examples/platforms/posix/misc.c index 04c0950da..8e79d682f 100644 --- a/examples/platforms/posix/misc.c +++ b/examples/platforms/posix/misc.c @@ -29,6 +29,7 @@ #include "platform-posix.h" #ifndef _WIN32 +#include #include #endif @@ -37,8 +38,7 @@ #include "openthread-system.h" #ifndef _WIN32 -extern int gArgumentsCount; -extern char **gArguments; +extern jmp_buf gResetJump; #endif static otPlatResetReason sPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON; @@ -56,23 +56,11 @@ void otPlatReset(otInstance *aInstance) #else // elif OPENTHREAD_PLATFORM_USE_PSEUDO_RESET // Restart the process using execvp. - char *argv[gArgumentsCount + 1]; - - for (int i = 0; i < gArgumentsCount; ++i) - { - argv[i] = gArguments[i]; - } - - argv[gArgumentsCount] = NULL; - otSysDeinit(); platformUartRestore(); - alarm(0); - - execvp(argv[0], argv); - perror("reset failed"); - exit(EXIT_FAILURE); + longjmp(gResetJump, 1); + assert(false); #endif // else OPENTHREAD_PLATFORM_USE_PSEUDO_RESET diff --git a/examples/platforms/posix/system.c b/examples/platforms/posix/system.c index 5ab78a386..1a3c9a66b 100644 --- a/examples/platforms/posix/system.c +++ b/examples/platforms/posix/system.c @@ -59,9 +59,6 @@ extern bool gPlatformPseudoResetWasRequested; static volatile bool gTerminate = false; #ifndef _WIN32 -int gArgumentsCount = 0; -char **gArguments = NULL; - static void handleSignal(int aSignal) { (void)aSignal; @@ -90,9 +87,6 @@ void otSysInit(int aArgCount, char *aArgVector[]) openlog(basename(aArgVector[0]), LOG_PID, LOG_USER); setlogmask(setlogmask(0) & LOG_UPTO(LOG_NOTICE)); - gArgumentsCount = aArgCount; - gArguments = aArgVector; - signal(SIGTERM, &handleSignal); signal(SIGHUP, &handleSignal); #endif diff --git a/src/posix/Makefile.am b/src/posix/Makefile.am index dff3d76d5..a480e0273 100644 --- a/src/posix/Makefile.am +++ b/src/posix/Makefile.am @@ -145,6 +145,10 @@ ot_cli_LDFLAGS += -Wl,-Ma endif if OPENTHREAD_BUILD_COVERAGE +CPPFLAGS_COMMON += \ + -DOPENTHREAD_ENABLE_COVERAGE \ + $(NULL) + CLEANFILES = $(wildcard *.gcda *.gcno) endif # OPENTHREAD_BUILD_COVERAGE diff --git a/src/posix/main.c b/src/posix/main.c index fb0d1f1b3..8b58923c4 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -29,6 +29,8 @@ #include #include +#include +#include #include #define OPENTHREAD_POSIX_APP_NCP 1 @@ -47,6 +49,10 @@ #include "openthread-system.h" +jmp_buf gResetJump; + +void __gcov_flush(); + void otTaskletsSignalPending(otInstance *aInstance) { (void)aInstance; @@ -56,7 +62,14 @@ int main(int argc, char *argv[]) { otInstance *sInstance; -pseudo_reset: + if (setjmp(gResetJump)) + { + alarm(0); +#if OPENTHREAD_ENABLE_COVERAGE + __gcov_flush(); +#endif + execvp(argv[0], argv); + } otSysInit(argc, argv); @@ -81,8 +94,6 @@ pseudo_reset: otInstanceFinalize(sInstance); - goto pseudo_reset; - return 0; } diff --git a/src/posix/platform/misc.c b/src/posix/platform/misc.c index 012142cc2..24bf0897a 100644 --- a/src/posix/platform/misc.c +++ b/src/posix/platform/misc.c @@ -28,14 +28,14 @@ #include "platform-posix.h" +#include #include #include #include "openthread-system.h" -extern int gArgumentsCount; -extern char **gArguments; +extern jmp_buf gResetJump; static otPlatResetReason sPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON; bool gPlatformPseudoResetWasRequested; @@ -43,31 +43,17 @@ static otPlatMcuPowerState gPlatMcuPowerState = OT_PLAT_MCU_POWER_STATE_ON; void otPlatReset(otInstance *aInstance) { - int i = 0; - // Restart the process using execvp. - char *argv[gArgumentsCount + 1]; - #if OPENTHREAD_PLATFORM_USE_PSEUDO_RESET gPlatformPseudoResetWasRequested = true; sPlatResetReason = OT_PLAT_RESET_REASON_SOFTWARE; #else // elif OPENTHREAD_PLATFORM_USE_PSEUDO_RESET - for (i = 0; i < gArgumentsCount; ++i) - { - argv[i] = gArguments[i]; - } - - argv[gArgumentsCount] = NULL; - otSysDeinit(); platformUartRestore(); - alarm(0); - - execvp(argv[0], argv); - perror("reset failed"); - exit(EXIT_FAILURE); + longjmp(gResetJump, 1); + assert(false); #endif // else OPENTHREAD_PLATFORM_USE_PSEUDO_RESET