Coder Social home page Coder Social logo

tymciox / memoryleakembedded Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 145 KB

A library for detecting memory leaks in embedded systems

License: MIT License

CMake 0.09% Makefile 0.04% C 99.87%
c cpp debugger embedded memory-leak low-resource rtos

memoryleakembedded's Introduction

MemoryLeak library

The MemoryLeak library helps identify memory leaks in embedded C programs by tracking allocated and unreleased memory using a linked list.

MemoryLeak is specifically designed for devices that may not have the capability to run more advanced memory management tools. For example, it can be effectively utilized with semihosting, offering a practical solution for memory leak detection on resource-constrained devices.

MemoryLeak is specifically designed for data collection purposes. To visualize and analyze the collected data, you can utilize the related project HeapVisualizationEmbedded.

Alt text

Contents

Overview

The MemoryLeak library enhances memory management by overriding the behavior of standard functions โ€” malloc, calloc, and free. Allocated memory details are recorded within nodes of a linked list. Upon memory release, the associated node is removed from the linked list.

To facilitate memory leak detection, at the end of the program or during specific checkpoints, users can check for memory leaks. There are two ways to obtain information about allocated but unreleased memory: print to console and store in a file.

Usage

  1. Clone Git repository and include the src/ directory in your project's compilation,
  2. If you want to enable or disable, uncomment or comment the flag #define MEMORY_LEAK_EMBEDDED inside the source file memory_leak.h,
  3. Inlcude the header file #include "memory_leak.h" in all of your source files,
  4. Overwrite functions in src/interface.c with your own implementation,
  5. While the program is running, you need to call a function: memory_leak_print_result() or memory_leak_write_result_to_a_file(),
  6. If you used memory_leak_print_result() copy the result and save it to a .txt file,
  7. Display your result on a graph and table

How to overwrite interface source file

  • print(char *string_info) - Required only if you print the result, otherwise leave it empty

  • get_thread_name(char *destination_thread_name) - Optional

  • get_time(unsigned int *destination_time) - Required, SysTick is recommended

  • open_file(void), close_file(void), write_file(const char *single_info, const unsigned int len) - Required only if you are writing to a file, otherwise leave it empty

How to run an example on a Windows

  1. Preconditions, you need:
  • Windows 10/11 system,
  • make,
  • GNU Compiler Collection (e.g. MinGW).
  1. Launch powershell in the main directory:
PS C:\memory_leak> make
gcc -Wall -Wextra -g -o example -Isrc src/memory_leak.c src/interface.c example.c
PS C:\memory_leak> .\example.exe
Memory Leak Summary
************************
587875281,2,000001D9232EFCE0,example.c,17,Thread 1
587875281,4,000001D9232F1AD0,example.c,19,Thread 1
587875296,120,000001D9232EFBD0,example.c,36,Thread 2
587875296,20,000001D9232EDEB0,example.c,75,Thread 3
587875328,40,000001D9232EDB40,example.c,79,Thread 3
587875328,140,000001D9232F2300,example.c,40,Thread 2
587875343,110,000001D9232F2480,example.c,42,Thread 2
587875359,60,000001D9232F0660,example.c,83,Thread 3
587875359,120,000001D9232F2570,example.c,44,Thread 2
587875375,130,000001D9232F26D0,example.c,46,Thread 2
***********************
  1. Copy the result and save it to a .txt file.
  2. Display your result on a graph and table

How to build unit tests

  1. Preconditions, you need:
  • Windows 10/11 system,
  • cmake,
  • GNU Compiler Collection (e.g. MinGW).
  1. Launch powershell in the main directory:
PS C:\memory_leak> cmake -S . -B BuildTest -G 'MinGW Makefiles'
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.9s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/tymci/Desktop/memory_leak/BuildTest

PS C:\memory_leak> cd .\BuildTest\

PS C:\memory_leak\BuildTest> make
[ 11%] Building C object test/CMakeFiles/Unity.dir/cunity/unity.c.obj
[ 22%] Building C object test/CMakeFiles/Unity.dir/cunity/unity_fixture.c.obj
[ 33%] Linking C static library libUnity.a
[ 33%] Built target Unity
[ 44%] Building C object CMakeFiles/memory_leak.dir/src/memory_leak.c.obj
[ 55%] Building C object CMakeFiles/memory_leak.dir/src/interface.c.obj
[ 66%] Building C object CMakeFiles/memory_leak.dir/test/TestProductionCode_main.c.obj
[ 77%] Building C object CMakeFiles/memory_leak.dir/test/TestProductionCode_Runner.c.obj
[ 88%] Building C object CMakeFiles/memory_leak.dir/test/TestProductionCode.c.obj
[100%] Linking C executable memory_leak.exe
[100%] Built target memory_leak

PS C:\Users\tymci\Desktop\memory_leak\BuildTest> .\memory_leak.exe    
Unity test run 1 of 1
....

-----------------------
4 Tests 0 Failures 0 Ignored
OK

memoryleakembedded's People

Contributors

tymciox avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.