diff --git a/CMakeLists.txt b/CMakeLists.txt index 61c49b1381..d27212f60f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,10 @@ option( KICAD_BUILD_PARALLEL_CL_MP "Build in parallel using the /MP compiler option (default OFF for safety reasons)" OFF ) +option( KICAD_USE_VALGRIND + "Build KiCad with valgrind stack tracking enabled." + OFF ) + # when option KICAD_SCRIPTING OR KICAD_SCRIPTING_MODULES is enabled: # PYTHON_EXECUTABLE can be defined when invoking cmake # ( use -DPYTHON_EXECUTABLE=/python.exe or python2 ) @@ -161,6 +165,10 @@ set( CMAKE_POSITION_INDEPENDENT_CODE ON ) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) +# Enable additional valgrind instrumentation for stack tracking in libcontext +if( KICAD_USE_VALGRIND ) + add_definitions( -DKICAD_USE_VALGRIND ) +endif() # CMP0063: CMake < 3.3 does not handle hidden visibility for static libraries, # and 3.3 is backwards compatible when the minimum version is smaller than 3.3. diff --git a/include/tool/coroutine.h b/include/tool/coroutine.h index 60144ebc0b..ed7ab07680 100644 --- a/include/tool/coroutine.h +++ b/include/tool/coroutine.h @@ -31,6 +31,10 @@ #include +#ifdef KICAD_USE_VALGRIND +#include +#endif + #include #include @@ -65,8 +69,8 @@ private: { enum { - FROM_ROOT, // a stub was called/a corutine was resumed from the main-stack context - FROM_ROUTINE, // a stub was called/a coroutine was resumed fron a coroutine context + FROM_ROOT, // a stub was called/a coroutine was resumed from the main-stack context + FROM_ROUTINE, // a stub was called/a coroutine was resumed from a coroutine context CONTINUE_AFTER_ROOT // a function sent a request to invoke a function on the main // stack context } type; // invocation type @@ -140,11 +144,17 @@ public: m_callContext( nullptr ), m_callee( nullptr ), m_retVal( 0 ) +#ifdef KICAD_USE_VALGRIND + ,valgrind_stack( 0 ) +#endif { } ~COROUTINE() { +#ifdef KICAD_USE_VALGRIND + VALGRIND_STACK_DEREGISTER( valgrind_stack ); +#endif } public: @@ -306,6 +316,10 @@ private: // correct the stack size stackSize -= size_t( ( (ptrdiff_t) m_stack.get() + stackSize ) - (ptrdiff_t) sp ); + +#ifdef KICAD_USE_VALGRIND + valgrind_stack = VALGRIND_STACK_REGISTER( sp, m_stack.get() ); +#endif #endif m_callee = libcontext::make_fcontext( sp, stackSize, callerStub ); @@ -389,6 +403,10 @@ private: CALLEE_STORAGE m_callee; ReturnType m_retVal; + +#ifdef KICAD_USE_VALGRIND + uint32_t valgrind_stack; +#endif }; #endif