Added valgrind to container and updated sample and README to show exa… (#137)

* Added valgrind to container and updated sample and README to show example of checking for memory leaks

* Updated sample and README to include Visual Studio usage, removed unnecessary lines

* Fixed builds of existing samples by removing the deprecated azsphere_configure_tools and azsphere_configure_api

* Add AzureSphereSquirrel to excludes since it uses the broken DEBUG_LIB feature

* Missing comma
This commit is contained in:
Jonathan Phippen 2024-07-24 01:49:52 -07:00 коммит произвёл GitHub
Родитель 89c41b42a1
Коммит 6bedcdfa1e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
39 изменённых файлов: 109 добавлений и 137 удалений

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(AzureIoT C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} "")
add_compile_definitions(PC_HOST_IP="YOUR_PC_IP_ADDRESS")

Просмотреть файл

@ -5,8 +5,6 @@ cmake_minimum_required (VERSION 3.20)
project (HLCore CXX)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
set(DISABLE_DYNAMIC true)
set(SQ_DISABLE_INSTALLER true)
set(SQ_DISABLE_HEADER_INSTALLER true)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(BalancingRobot_HighLevel C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
# to enable UdpLog, uncomment the next line.
# add_compile_definitions(USE_SOCKET_LOG)

Просмотреть файл

@ -5,8 +5,6 @@ cmake_minimum_required (VERSION 3.11)
#set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
project (BalancingRobot_RealTime C ASM)
azsphere_configure_tools(TOOLS_REVISION "23.05")
SET(CMAKE_ASM_FLAGS "-mcpu=cortex-m4")
add_compile_definitions(OSAI_AZURE_RTOS)

Просмотреть файл

@ -878,36 +878,6 @@ static void InitPeripheralsAndHandlers(void)
}
```
## This project is evergreen
This project automatically updates the CMake Azure Sphere configuration to match the Azure Sphere SDK you've installed on your computer. This is fine for a sample, but for production applications, setting the Azure Sphere configuration in CMake should be intentional and tested.
In the CMakeLists.txt file, the *auto_generate_azsphere_config* CMake function is called to generate the azsphere_configure_tools and azsphere_configure_api settings.
```makefile
if (EXISTS "${CMAKE_SOURCE_DIR}/Tools/cmake/azsphere_config.cmake")
include(Tools/cmake/azsphere_config.cmake)
auto_generate_azsphere_config()
else()
# For information on setting tools revision and target API set see
# https://learn.microsoft.com/en-us/azure-sphere/app-development/using-cmake-functions
azsphere_configure_tools(TOOLS_REVISION "21.07")
azsphere_configure_api(TARGET_API_SET "10")
endif()
```
For production applications, remove this block and replace it with the following CMake commands.
```makefile
azsphere_configure_tools(TOOLS_REVISION "<YOUR_TOOLS_VERSION>")
azsphere_configure_api(TARGET_API_SET "<THE_TARGET_API_SET_YOU_ARE_TESTING_AGAINST")
```
<!-- ---
## Understanding exits codes

Просмотреть файл

@ -7,21 +7,6 @@ project("co2monitor" C)
include(azsphere_board.txt)
if (EXISTS "${CMAKE_SOURCE_DIR}/Tools/cmake/azsphere_config.cmake")
include(Tools/cmake/azsphere_config.cmake)
auto_generate_azsphere_config()
else()
# For information on setting tools revision and target api set see
# https://learn.microsoft.com/en-us/azure-sphere/app-development/using-cmake-functions
azsphere_configure_tools(TOOLS_REVISION "21.07")
azsphere_configure_api(TARGET_API_SET "10")
endif()
if (SCD4x)
set(scd4x
"AzureSphereDrivers/embedded-i2c-scd4x/scd4x_i2c.c"

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.20)
project(DNSServiceDiscovery C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c eventloop_timer_utilities.c dns-sd.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c curl)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required (VERSION 3.20)
project (DRAMClickboard C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
# External Library Add
add_library(DRAM_Click STATIC dram.c)

Просмотреть файл

@ -4,10 +4,6 @@
cmake_minimum_required(VERSION 3.10)
project(EAP_TLS_Client C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
# Create executable
add_executable(${PROJECT_NAME} main.c eventloop_timer_utilities.c parson.c "lib/_environment_config.c" "lib/eap_tls_lib.c" "lib/web_api_client.c" "tests/tests.c")
target_link_libraries(${PROJECT_NAME} applibs pthread gcc_s c curl tlsutils)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required (VERSION 3.20)
project (EncryptedStorage C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
# Create executable
add_executable (${PROJECT_NAME} main.c)
target_link_libraries (${PROJECT_NAME} applibs pthread gcc_s c wolfssl)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(Grove16x2RGBLCD_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
utils.c

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(HeapTracker C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c "heap_tracker_lib.c")
# Here we wrap the native memory allocation functions

Просмотреть файл

@ -30,10 +30,6 @@ aux_source_directory(drivers DRIVERS_SRCS)
aux_source_directory(drivers/modbus MODBUS_DRIVERS_SRCS)
aux_source_directory(libutils LIBUTILS_SRCS)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
option(ENABLE_SERIAL_LOG "Enable Serial Log" OFF)
if(ENABLE_SERIAL_LOG)
message(STATUS "Enable Serial Log")

Просмотреть файл

@ -3,7 +3,6 @@
cmake_minimum_required(VERSION 3.11)
project(MT3620_IDC_RTApp C)
azsphere_configure_tools(TOOLS_REVISION "20.10")
# Create executable
add_executable(${PROJECT_NAME} main.c Socket.c lib/VectorTable.c lib/GPIO.c lib/UART.c lib/Print.c lib/GPT.c lib/MBox.c)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.20)
project(LittleFs_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
crypt.c

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(LittleFs_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
curlFunctions.c

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(SDCard_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
SDCardViaRtCore.c

Просмотреть файл

@ -5,8 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(IntercoreComms_RTApp_MT3620_BareMetal C)
azsphere_configure_tools(TOOLS_REVISION "21.07")
# Enable/Disable runtime debug messages
# add_compile_definitions(SHOW_DEBUG_INFO)

Просмотреть файл

@ -16,9 +16,6 @@ add_compile_definitions(MQTT_USE_WOLFSSL)
#
###################################################################################################################
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_subdirectory("AzureSphereDevX" out)
add_library(mqttc STATIC

Просмотреть файл

@ -1,6 +1,6 @@
FROM mcr.microsoft.com/azurespheresdk:latest
RUN apt update && \
apt install -y libncurses5 gdb && \
apt install -y libncurses5 gdb valgrind && \
apt autoremove -y && \
apt clean -y

Просмотреть файл

@ -6,12 +6,13 @@ cmake_minimum_required(VERSION 3.20)
project(HelloWorld_HighLevelApp C)
include(CTest)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c led.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c)
if(LEAK)
target_compile_definitions(${PROJECT_NAME} PUBLIC LEAK)
endif()
# TARGET_HARDWARE and TARGET_DEFINITION relate to the hardware definition targeted by this sample.
# When using this sample with other hardware, replace TARGET_HARDWARE with the name of that hardware.
# For example, to target the Avnet MT3620 Starter Kit, use the value "avnet_mt3620_sk".

Просмотреть файл

@ -52,6 +52,14 @@
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-Debug-Leak",
"displayName": "x86-Debug-Leak",
"inherits": "x86-Debug",
"cacheVariables": {
"LEAK": true
}
}
],
"buildPresets": [

Просмотреть файл

@ -24,7 +24,7 @@ This sample does not require any additional hardware.
This sample requires the following additional software:
- [Docker Desktop](https://www.docker.com/products/docker-desktop/)
- [VS Code](https://code.visualstudio.com/download)
- [VS Code](https://code.visualstudio.com/download) OR [Visual Studio](https://visualstudio.microsoft.com/downloads/)
- [Dev Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension for VS Code
## Setup
@ -38,7 +38,9 @@ docker pull mcr.microsoft.com/azurespheresdk:latest
Ensure that Docker Desktop is configured to run Linux containers. Right-click Docker Desktop menu; if you see the option to "Switch to Linux containers", then select it. (If you see the option to "Switch to Windows containers", then Docker Desktop is already configured to run Linux containers.)
## Build and run the sample
## VS Code
### Build and run the sample
1. Open VS Code, open the Command Palette (`Ctrl + Shift + P`), and run "Dev Containers - Open Folder in Container...".
@ -83,6 +85,80 @@ When debugging, open the Terminal Window (Ctrl + `) to see program output, which
![Screenshot that shows how to run the CTests.](.media/run_ctests.png)
### Check for memory leaks
1. Use the Toolbar to select the configure preset "x86-Debug-Leak", and wait for CMake to finish generating the cache.
(Alternatively, open the Command Palette, run the command "CMake: Select Configure Preset", select the "x86-Debug-Leak" Preset.)
2. Use the Toolbar to build.
(Alternatively, open the Command Palette and run the command "CMake: Build" to build.)
3. Open the Terminal Window (Ctrl + `) and run the following command to start running Valgrind on the app:
```
valgrind --leak-check=full --show-leak-kinds=definite out/x86-Debug-Leak/HelloWorld_HighLevelApp
```
4. After the app runs for a while, stop the app (Ctrl + C) and observe the output from Valgrind. Note that Valgrind correctly identifies memory that is definitely lost in main():
```
HEAP SUMMARY:
in use at exit: 146,511 bytes in 7 blocks
total heap usage: 7 allocs, 0 frees, 146,511 bytes allocated
8 bytes in 2 blocks are definitely lost in loss record 2 of 6
at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x10931F: main (main.c:66)
```
## Visual Studio
### Build and run the sample
1. Open this folder in Visual Studio and wait for CMake to finish generating.
2. Select the "Reopen folder in container" link in the Dev Container Info Bar.
(Alternatively, select the "[Dev Container]" option from the Target System dropdown menu)
3. Select the "x86-Debug" configuration from the Configuration dropdown menu and wait for CMake to finish generating.
4. Open the Build menu and select the "Build All" command (`Ctrl + Shift + B`) to build.
5. Select the "HelloWorld_HighLevelApp (Native)" item from the Startup Item dropdown menu and Select the "Start Debugging" command from the Debug Menu (`F5`) to start debugging.
### Observe the output
When debugging, the output should appear in the Output Window, which should show the GPIO_SetValue call with 1 and 0 to simulate blinking an LED.
### Run Tests
1. Open the Test menu and select the "Test Explorer" command to open the Test Explorer Window.
2. Click on the Play icon to run all the tests or right-click on a test and select the "Run" command to run an individual test.
3. Right-click on a test and select the "Debug" command to start debugging an individual test.
### Check for memory leaks
1. Select the "x86-Debug-Leak" configuration from the Configuration dropdown menu and wait for CMake to finish generating.
2. Open the Build menu and select the "Build All" command (`Ctrl + Shift + B`) to build.
3. Open the Debug menu and select the "Start Valgrind" command under the "Other Debug Targets" sub-menu to start running Valgrind on the app.
4. After the app runs for a while, open the Debug menu and select the "Stop Valgrind" command under the "Other Debug Targets" sub-menu to stop running Valgrind.
5. The Valgrind window will show that Valgrind correctly found a memory leak in main() as shown below, and if main.c is opened, the memory leak will be underlined with a warning message.
```
HEAP SUMMARY:
in use at exit: 146,511 bytes in 7 blocks
total heap usage: 7 allocs, 0 frees, 146,511 bytes allocated
8 bytes in 2 blocks are definitely lost in loss record 2 of 6
at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x10931F: main (main.c:66)
```
## Project expectations
### Expected support for the code

Просмотреть файл

@ -9,6 +9,16 @@
"applicationPath": "${debugInfo.target}",
"imagePath": "${debugInfo.targetImage}",
"partnerComponents": []
},
{
"type": "cppgdb",
"name": "HelloWorld_HighLevelApp (Native)",
"project": "CMakeLists.txt",
"projectTarget": "HelloWorld_HighLevelApp",
"debuggerConfiguration": "gdb",
"MIMode": "gdb",
"args": [],
"env": {}
}
]
}

Просмотреть файл

@ -9,6 +9,7 @@
// - gpio (digital input for button)
// - log (displays messages in the Device Output window during debugging)
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
@ -60,5 +61,10 @@ int main(void)
nanosleep(&sleepTime, NULL);
LED_Off(fd);
nanosleep(&sleepTime, NULL);
#ifdef LEAK
// Intentionally leak here to show an example of using valgrind to detect memory leaks
int* leak = (int*)malloc(sizeof(int));
*leak = 1;
#endif
}
}

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(PrintNetworkInterfaceAddresses_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(OSNetworkRequirementChecker-HLApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c eventloop_timer_utilities.c dns-helper.c ntp-helper.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c)

Просмотреть файл

@ -5,8 +5,6 @@ cmake_minimum_required (VERSION 3.10)
project (PWMAudioRT C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
# Create executable
add_executable(${PROJECT_NAME} main.c lib/VectorTable.c lib/GPT.c lib/GPIO.c)
target_link_libraries (${PROJECT_NAME})

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(RS_485_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c eventloop_timer_utilities.c rs485_hl_driver.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c)

Просмотреть файл

@ -9,8 +9,6 @@ add_executable(${PROJECT_NAME} main.c Socket.c ringBuffer.c rs485_driver.c lib/V
target_link_libraries(${PROJECT_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_DEPENDS ${CMAKE_SOURCE_DIR}/linker.ld)
azsphere_configure_tools(TOOLS_REVISION "23.05")
# Add MakeImage post-build command
azsphere_target_add_image_package(${PROJECT_NAME})

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(SetTimeFromLocation_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME} main.c location_from_ip.c parson.c settime.c httpGet.c)
target_link_libraries(${PROJECT_NAME} applibs gcc_s c curl)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(LittleFs_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
curlFunctions.c

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(TranslatorCognitiveServices_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
parson.c

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(SphereUdpLogSender C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
udplog.c

Просмотреть файл

@ -11,9 +11,6 @@ cmake_minimum_required(VERSION 3.10)
project(VS1053AudioStream_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
# ENABLE NPR INTERNET RADIO STREAM ##########################################################################################
#
# add_compile_definitions(ENABLE_RADIO_STREAMING)

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(WiFiProvisioningViaNfc_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
include_directories(${CMAKE_SOURCE_DIR} M24SR)
add_executable(${PROJECT_NAME}

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(ConfigureWifiViaAppResource_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
AppResourceWiFiConfig.c

Просмотреть файл

@ -5,9 +5,6 @@ cmake_minimum_required(VERSION 3.10)
project(WifiConfigurationViaUart_HighLevelApp C)
azsphere_configure_tools(TOOLS_REVISION "23.05")
azsphere_configure_api(TARGET_API_SET "16")
add_executable(${PROJECT_NAME}
main.c
SerialWiFiConfig.c

Просмотреть файл

@ -9,6 +9,7 @@ exclude_paths=[
"AzureSphereDevX",
"threadx"
"AzureIoT_StoreAndForward/src/SimpleFileSystem",
"AzureSphereSquirrel/HLCore",
"AzureSphereSquirrel/HLCore/squirrel",
"BalancingRobot/Software/RTOS/mt3620_m4_software",
"CO2_MonitorHealthySpaces/src/AzureSphereDrivers",
@ -103,7 +104,7 @@ for p in cmakelists:
azsphere_project = False
with open(p,"r") as f:
for line in f:
if "azsphere_configure_tools" in line:
if "azsphere_target_add_image_package" in line:
azsphere_project = True
if not azsphere_project: