Skip to content

Commit

Permalink
Keep symbols visible in Debug builds (and in test executables), so th…
Browse files Browse the repository at this point in the history
…at backtraces show more function names
  • Loading branch information
graebm committed Aug 21, 2024
1 parent 2608a04 commit c499292
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
9 changes: 7 additions & 2 deletions cmake/AwsCFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
include(CheckCCompilerFlag)
include(CheckIncludeFile)
include(CheckSymbolExists)
include(CMakeParseArguments) # needed for CMake v3.4 and lower

option(AWS_ENABLE_LTO "Enables LTO on libraries. Ensure this is set on all consumed targets, or linking will fail" OFF)
option(LEGACY_COMPILER_SUPPORT "This enables builds with compiler versions such as gcc 4.1.2. This is not a 'supported' feature; it's just a best effort." OFF)
Expand Down Expand Up @@ -255,5 +254,11 @@ function(aws_set_common_properties target)
target_compile_definitions(${target} PRIVATE ${AWS_C_DEFINES_PRIVATE} PUBLIC ${AWS_C_DEFINES_PUBLIC})
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE C C_STANDARD 99 C_STANDARD_REQUIRED ON)
set_target_properties(${target} PROPERTIES INTERPROCEDURAL_OPTIMIZATION ${_ENABLE_LTO_EXPR}>)
set_target_properties(${target} PROPERTIES C_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)

# Don't hide symbols in Debug builds.
# We do this so that backtraces are more likely to show function names.
# We mostly use backtraces to diagnose memory leaks.
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set_target_properties(${target} PROPERTIES C_VISIBILITY_PRESET hidden CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
endif ()
endfunction()
13 changes: 3 additions & 10 deletions cmake/AwsTestHarness.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ function(generate_test_driver driver_exe_name)
add_executable(${driver_exe_name} ${CMAKE_CURRENT_BINARY_DIR}/test_runner.c ${TESTS})
aws_set_common_properties(${driver_exe_name} NO_WEXTRA NO_PEDANTIC)

# Some versions of CMake (3.9-3.11) generate a test_runner.c file with
# a strncpy() call that triggers the "stringop-overflow" warning in GCC 8.1+
# This warning doesn't exist until GCC 7 though, so test for it before disabling.
if (NOT MSVC)
check_c_compiler_flag(-Wno-stringop-overflow HAS_WNO_STRINGOP_OVERFLOW)
if (HAS_WNO_STRINGOP_OVERFLOW)
SET_SOURCE_FILES_PROPERTIES(test_runner.c PROPERTIES COMPILE_FLAGS -Wno-stringop-overflow)
endif()
endif()

aws_add_sanitizers(${driver_exe_name} ${${PROJECT_NAME}_SANITIZERS})

target_link_libraries(${driver_exe_name} PRIVATE ${PROJECT_NAME})
Expand All @@ -57,6 +47,9 @@ function(generate_test_driver driver_exe_name)
target_compile_definitions(${driver_exe_name} PRIVATE AWS_UNSTABLE_TESTING_API=1)
target_include_directories(${driver_exe_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR})

# Export symbols so that backtraces are more likely to show function names.
set_target_properties(${driver_exe_name} PROPERTIES ENABLE_EXPORTS ON)

foreach(name IN LISTS TEST_CASES)
add_test(${name} ${driver_exe_name} "${name}")
set_tests_properties("${name}" PROPERTIES SKIP_RETURN_CODE ${SKIP_RETURN_CODE_VALUE})
Expand Down
11 changes: 5 additions & 6 deletions tests/memtrace_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static int s_test_memtrace_stacks(struct aws_allocator *allocator, void *ctx) {
/* only bother to run this test if the platform can do a backtrace */
void *probe_stack[1];
if (!aws_backtrace(probe_stack, 1)) {
return 0;
return AWS_OP_SKIP;
}

test_logger_init(&s_test_logger, allocator, AWS_LL_TRACE, 0);
Expand Down Expand Up @@ -146,27 +146,26 @@ static int s_test_memtrace_stacks(struct aws_allocator *allocator, void *ctx) {
/* if this is not a debug build, there may not be symbols, so the test cannot
* verify if a best effort was made */
#if defined(DEBUG_BUILD)
fprintf(stderr, "---LOGS BEGIN---\n%s\n---LOGS END---\n", test_logger->log_buffer.buffer);
fprintf(stderr, "%s\n", test_logger->log_buffer.buffer);
char s_alloc_1_addr[32];
char s_alloc_2_addr[32];
char s_alloc_3_addr[32];
char s_alloc_4_addr[32];
# if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4054) /* type cast function pointer to data pointer */
# endif

snprintf(s_alloc_1_addr, AWS_ARRAY_SIZE(s_alloc_1_addr), "0x%tx", (uintptr_t)(void *)s_alloc_1);
snprintf(s_alloc_2_addr, AWS_ARRAY_SIZE(s_alloc_2_addr), "0x%tx", (uintptr_t)(void *)s_alloc_2);
snprintf(s_alloc_3_addr, AWS_ARRAY_SIZE(s_alloc_3_addr), "0x%tx", (uintptr_t)(void *)s_alloc_3);
snprintf(s_alloc_4_addr, AWS_ARRAY_SIZE(s_alloc_4_addr), "0x%tx", (uintptr_t)(void *)s_alloc_4);
# pragma warning(pop)
# endif /* defined(_MSC_VER) */

const char *log_buffer = (const char *)test_logger->log_buffer.buffer;
ASSERT_TRUE(strstr(log_buffer, "s_alloc_1") || strstr(log_buffer, s_alloc_1_addr));
ASSERT_TRUE(strstr(log_buffer, "s_alloc_2") || strstr(log_buffer, s_alloc_2_addr));
ASSERT_TRUE(strstr(log_buffer, "s_alloc_3") || strstr(log_buffer, s_alloc_3_addr));
ASSERT_TRUE(strstr(log_buffer, "s_alloc_4") || strstr(log_buffer, s_alloc_4_addr));
#endif
#endif /* DEBUG_BUILD */

/* reset log */
aws_byte_buf_reset(&test_logger->log_buffer, true);
Expand Down

0 comments on commit c499292

Please sign in to comment.