Skip to content

Commit

Permalink
Added valgrind to container and updated sample and README to show exa… (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
jophippe committed Jul 24, 2024
1 parent 89c41b4 commit 6bedcdf
Show file tree
Hide file tree
Showing 39 changed files with 109 additions and 137 deletions.
3 changes: 0 additions & 3 deletions AzureIoT_StoreAndForward/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions AzureSphereSquirrel/HLCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions BalancingRobot/Software/HighLevelApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions BalancingRobot/Software/RTOS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 0 additions & 30 deletions CO2_MonitorHealthySpaces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions CO2_MonitorHealthySpaces/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions DNSServiceDiscoveryUnicast/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 0 additions & 3 deletions DRAMClickboard/DRAMClickboard_HighLevelApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 0 additions & 4 deletions EAP-TLS_Solution/EAP-TLS Client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions EncryptedStorage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions Grove_16x2_RGB_LCD/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions HeapTracker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions LittleFs_Encrypted_RemoteDisk/src/HighLevelApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions LittleFs_RemoteDisk/src/HighLevelApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 0 additions & 3 deletions MQTT-C_Client/src/HighLevelApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion NativeBlink/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions NativeBlink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
8 changes: 8 additions & 0 deletions NativeBlink/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-Debug-Leak",
"displayName": "x86-Debug-Leak",
"inherits": "x86-Debug",
"cacheVariables": {
"LEAK": true
}
}
],
"buildPresets": [
Expand Down
80 changes: 78 additions & 2 deletions NativeBlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...".

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions NativeBlink/launch.vs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
]
}
Loading

0 comments on commit 6bedcdf

Please sign in to comment.