1. Valgrind

Valgrind is a programming tool for memory debugging, memory leak detection, and profiling. It shows memory leaks and deallocation errors in the program during runtime.

2. Valgrind Installation

Add valgrind to the package group will enable the valgrind support for your platform 

File : meta-cmf-raspberrypi/recipes-core/packagegroups/packagegroup-rdk-oss-broadband.bbappend  

diff --git a/recipes-core/packagegroups/packagegroup-rdk-oss-broadband.bbappend b/recipes-core/packagegroups/packagegroup-rdk-oss-broadband.bbappend 

index a74b9e4..93dee0f 100644 

--- a/recipes-core/packagegroups/packagegroup-rdk-oss-broadband.bbappend 

+++ b/recipes-core/packagegroups/packagegroup-rdk-oss-broadband.bbappend 

@@ -12,6 +12,11 @@ RDEPENDS_packagegroup-rdk-oss-broadband_append = " \ 

     rtl8192eu \ 

     rtl88x2bu \ 

     ethtool \ 

+    valgrind \ 

" 

3. Build steps

Follow the link for RPi build steps- RDK-B R-Pi Build guide

4. How to use valgrind for memory check? 

On a successful build, flash the binary to the board. Once the board is up, run the Valgrind command in the two ways shown below.

4.1. From console

valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/<component> 

The memory leak summary will be seen on the console 

Sample: 
root@RaspberryPi-Gateway:~# valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/sampleAppn 

==12907== Memcheck, a memory error detector 

==12907== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 

==12907== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info 

==12907== Command: /usr/bin/sampleAppn 

==12907== 

Hello Sample Program     1600439223.000000 

==12907== Invalid write of size 1 

==12907==    at 0x8458: ??? (in /usr/bin/sampleAppn) 

==12907==  Address 0x4909462 is 0 bytes after a block of size 10 alloc'd 

==12907==    at 0x48294CC: malloc (in /usr/lib/valgrind/vgpreload_memcheck-arm-linux.so) 

==12907== 

Hello Sample Program     1600439225.000000 

Hello Sample Program     1600439227.000000 

Hello Sample Program     1600439229.000000 

Hello Sample Program     1600439231.000000 

Hello Sample Program     1600439233.000000 

Hello Sample Program     1600439235.000000 

Hello Sample Program     1600439237.000000 

Hello Sample Program     1600439239.000000 

Hello Sample Program     1600439241.000000 

Hello Sample Program     1600439243.000000 

Hello Sample Program     1600439245.000000 

Hello Sample Program     1600439247.000000 

Hello Sample Program     1600439249.000000 

Hello Sample Program     1600439251.000000 

Hello Sample Program     1600439253.000000 

Hello Sample Program     1600439255.000000 

Hello Sample Program     1600439257.000000 

Hello Sample Program     1600439259.000000 

Hello Sample Program     1600439261.000000 

Hello Sample Program     1600439263.000000 

Hello Sample Program     1600439265.000000 

Hello Sample Program     1600439267.000000 

Hello Sample Program     1600439269.000000 

Hello Sample Program     1600439271.000000 

Hello Sample Program     1600439273.000000 

Hello Sample Program     1600439275.000000 

Hello Sample Program     1600439277.000000 

Hello Sample Program     1600439279.000000 

==12907== 

==12907== FILE DESCRIPTORS: 3 open at exit. 

==12907== Open file descriptor 2: /dev/pts/0 

==12907==    <inherited from parent> 

==12907== 

==12907== Open file descriptor 1: /dev/pts/0 

==12907==    <inherited from parent> 

==12907== 

==12907== Open file descriptor 0: /dev/pts/0 

==12907==    <inherited from parent> 

==12907== 

==12907== 

==12907== HEAP SUMMARY: 

==12907==     in use at exit: 290 bytes in 29 blocks 

==12907==   total heap usage: 30 allocs, 1 frees, 1,314 bytes allocated 

==12907== 

==12907== 290 bytes in 29 blocks are definitely lost in loss record 1 of 1 

==12907==    at 0x48294CC: malloc (in /usr/lib/valgrind/vgpreload_memcheck-arm-linux.so) 

==12907== 

==12907== LEAK SUMMARY: 

==12907==    definitely lost: 290 bytes in 29 blocks  >> This indicates the memory leak 

==12907==    indirectly lost: 0 bytes in 0 blocks 

==12907==      possibly lost: 0 bytes in 0 blocks 

==12907==    still reachable: 0 bytes in 0 blocks 

==12907==         suppressed: 0 bytes in 0 blocks 

==12907== 

==12907== For counts of detected and suppressed errors, rerun with: -v 

==12907== ERROR SUMMARY: 30 errors from 2 contexts (suppressed: 6 from 3) 

4.2. From service file 

Invoke the Valgrind command from the service file. Once the service is started, it checks for memory leaks

root@RaspberryPi-Gateway:~# cat /lib/systemd/system/sample.service 

[Unit] 

Description=Example sampleapp systemd service. 

After=CcspPandMSsp.service 

[Service] 

Type=simple 

ExecStart=/bin/sh -c 'valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/sampleAppn' 

ExecStop=/bin/sh -c ' echo "Sample service Stopped" ' 

[Install] 

WantedBy=multi-user.target 
  • Now start/restart the service using systemctl command 
  • The memory leak summary will be seen on systemctl status <service> 
root@RaspberryPi-Gateway:~# systemctl status sample 

● sample.service - Example sampleapp systemd service. 

   Loaded: loaded (/lib/systemd/system/sample.service; disabled; vendor preset: enabled) 

   Active: active (running) since Fri 2020-09-18 14:50:22 UTC; 6s ago 

 Main PID: 21280 (memcheck-arm-li) 

   CGroup: /system.slice/sample.service 

           └─21280 valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes /usr/bin/sampleAppn 

 

Sep 18 14:50:22 RaspberryPi-Gateway systemd[1]: Started Example sampleapp systemd service.. 

Sep 18 14:50:22 RaspberryPi-Gateway sh[21280]: ==21280== Memcheck, a memory error detector 

Sep 18 14:50:22 RaspberryPi-Gateway sh[21280]: ==21280== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. 

Sep 18 14:50:22 RaspberryPi-Gateway sh[21280]: ==21280== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info 

Sep 18 14:50:22 RaspberryPi-Gateway sh[21280]: ==21280== Command: /usr/bin/sampleAppn 

Sep 18 14:50:22 RaspberryPi-Gateway sh[21280]: ==21280== 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280== LEAK SUMMARY: 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280==    definitely lost: 290 bytes in 29 blocks 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280==    indirectly lost: 0 bytes in 0 blocks 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280==      possibly lost: 0 bytes in 0 blocks 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280==    still reachable: 0 bytes in 0 blocks 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280==         suppressed: 0 bytes in 0 blocks 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280== 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280== For counts of detected and suppressed errors, rerun with: -v 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21280]: ==21280== ERROR SUMMARY: 30 errors from 2 contexts (suppressed: 6 from 3) 

Sep 18 14:51:25 RaspberryPi-Gateway sh[21757]: Sample service Stopped 

 

5. Defining the options:

--tool=memcheck: "To the check the memory"
​
--leak-check=full: "each individual leak will be shown in detail"
  
--show-reachable=yes: "When enabled, the leak detector also shows "reachable" and "indirectly lost" blocks"
​
--num-callers=20: "Specifies the maximum number of entries shown in stack traces that identify program locations"
​
--track-fds=yes: "When enabled, Valgrind will print out a list of open file descriptors on exit."
​
/usr/bin/<component>: "Component's binary path"

6. Journalctl

The journalctl command is a powerful tool used to query and view logs collected by systemd. To increase the size of log files, you can update the following settings in the /etc/systemd/journald.conf file.

Filter Logs by Service: journalctl -u service


vi /etc/systemd/journald.conf
[Journal]
Storage=volatile
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
RateLimitIntervalSec=0
RateLimitBurst=0
SystemMaxUse=500M
#SystemKeepFree=
SystemMaxFileSize=100M
#SystemMaxFiles=100
RuntimeMaxUse=1G
#RuntimeKeepFree=
RuntimeMaxFileSize=100M
#RuntimeMaxFiles=100
#MaxRetentionSec=
#MaxFileSec=1month
ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K
#ReadKMsg=yes
#Audit=yes

NOTE: To get more debug info, compile the component with debug symbols enabled. 



  • No labels