UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

AR# 63280

Valgrind on Linux for Zynq

Description

This description guides you through getting Valgrind up and running with Linux on a ZC702 board.


About Valgrind

Valgrind is a GPL'd system for debugging and profiling Linux programs. 

With Valgrind's tool suite you can automatically detect many memory management and threading bugs, avoiding hours of frustrating bug-hunting, making your programs more stable. 

You can also perform detailed profiling to help speed up your programs.

Why should you use Valgrind?
 
Valgrind will save you hours of debugging time.

With Valgrind tools you can automatically detect many memory management and threading bugs.

This gives you confidence that your programs are free of many common bugs, some of which would take hours to find manually, or might never be found at all.
 
You can find and eliminate bugs before they become a problem.


Solution

This Answer Record covers how to do the following:

  • Building the filesystem
  • Setting up and building Linux with PetaLinux
  • Partitioning the SD-card
  • Creating the filesystem on the SD card
  • Booting into the EXT3 partition filesystem
  • Running Valgrind



    Building the filesystem:

     

    There are different tools you can use to build file systems, like Yocto, Buildroot and others. 

    In this example I will be using Buildroot:

    Get Buildroot:

    1) Get Buildroot:

    #git clone git://git.buildroot.net/buildroot

     2) Configure for the platform you will be running on:

    #cd buildroot

    #[buildroot ]make menuconfig

    Target options --->


    3) Select root filesystem types:

    Filesystem images --->


    4) Select to use a static dev management as below: -> system_configuration


    4) Enter ttyPS0 for TTY port, as below: -> system_configuration --> getty options ->


    Make sure the libraries are not stripped.

    Build options --->


    Enable Valgrind:

    1) Target packages -> Debugging, profiling and benchmark



    Exit the menuconfig and save changes.

    Within the buildroot filesystem, add "ttyPS" option to the static device table as seen below: 

    #[buildroot]vi buildroot/system/device_table_dev.txt



    Exit and build with the following command:

    #[buildroot] make 

    Final images are located in <path>/Buildroot/output/images/


    Setting up and building Linux with PetaLinux:

     

    Create a Petalinux project:

    #source /<path>/petalinux-v2014.4-final/settings.csh 

    #petalinux-create -t project -s /<path>/Xilinx-ZC702-v2014.4-final.bsp

    #cd Xilinx-ZC702-2014.4

    #[Xilinx-ZC702-2014.4 ] petalinux-config


    Change the Root filesystem type to SD card as in the example below:

    Image Packaging Configuration --->



    Build the Linux image image.ub with the following command:

    [Xilinx-ZC702-2014.4 ] petalinux-build


    Partition the SD-card which will be used with the filesystem:

    We will be booting Linux into a filesystem residing on an EXT3 partition on the SD card.

    Therefore, partition the SD card for FAT and EXT3 using a partitioning software tool. 

    In this case I used EaseUS Partition Master Free edition.






    Create the filesystem on the SD card:

     

    Copy "BOOT.BIN" and "image.ub" from the "Xilinx-ZC702-2014.4/pre-built/linux/images/" directory to the FAT filesystem within the Petalinux project and "rootfs.tar" from the Buildroot project "buildroot/output/images".

    Insert the SD CARD in the windows machine and use Windows Explorer to copy the files to the SD card.



    Put the SD card into the slot on the ZC702 board and boot from the SD-card.

    To boot from an SD card, flip bit 3 and 4 to "1", the rest "0", on the SW16 switch on the ZC702 board.

    When booted, log in with user and password "root" and install the filesystem onto the EXT3 partition.


    See the following example:

    #mount /dev/mmcblk0p1 /mnt/

    # ls -l /mnt/

    -rwxr-xr-x    1 root     root       4403064 Oct 24  2014 BOOT.BIN

    -rwxr-xr-x    1 root     root       3587900 Dec 16  2014 image.ub

    -rwxr-xr-x    1 root     root     196515840 Dec 18  2014 rootfs.tar

    # mkdir /ext3

    # mount /dev/mmcblk0p2 /ext3/

    # cd /ext3

    # tar xvf /mnt/rootfs.tar

    # ls -l /ext3

    total 64b

    b

    lr

    wxrwxrwx    1 root     root            11 Jan  1  1970 linuxrc -> bin/busybox

    pt

    drwxr-xr-x    2 root     root          4096 Dec 12  2014 proc

    drwx------    2 root     root          4096 Apr 30 13:53 root

    lrwxrwxrwx    1 root     root             3 Jan  1  1970 run-> tmp

    drwxr-xr-x    2 root     root          4096 Jan  1  1970 sbin

    drwxr-xr-x    2 root     root          4096 Dec 12  2014 sys

    drwxrwxrwt    3 root     root          4096 Apr 30 13:40 tmp

    drwxr-xr-x    7 root     root          4096 Jan  1  1970 usr

    drwxr-xr-x    5 root     root          4096 Jan  1  1970 var

    #
    Boot into the EXT3 partition filesystem

     

    Now we want to change out the image.ub from the SD card to the new one, which will be using the ext3 filesystem instead of the image.ub on the SD card.

    Copy "image.ub" from the "Xilinx-ZC702-2014.4/images/linux" directory to the SD-card.

    If there would be any problems with the new image, it would make sense to rename the old image so you can revert to it again.

    Put the SD card back into the development board and boot from SD card.

    You should now be able to boot Linux and Valgrind should be installed. You can test it on a binary file like this:

    buildroot login: root

    # which v

    d

    #/usr/bin/Valgrind

    Running Valgrind

    #valgrind --tool=memcheck ./hello.elf
    ==682== Memcheck, a memory error detector
    ==682== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.

    ==682== Command: ./hello.elf

    ==682==

    Hello World!!!

    ==682== HEAP SUMMARY:

    ==682==     in use at exit: 0 bytes in 0 blocks

    ==682==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated

    ==682==

    ==682== All heap blocks were freed -- no leaks are possible

    ==682==

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

    ==682== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

    #

     Code example:

    Analyzing the following code will give the results below:


    #include <stdlib.h>

      void f(void)

      {

       int * x = malloc(10 * sizeof(int);

         x[10] = 0;        // problem 1: heap block overrun

     }              // problem 2: memory leak -- x not freed

     

      int main(void)

      {

         f();

         return0;

     }

     

    #valgrind --tool=memcheck ./test.arm.elf

    ==713== Memcheck, a memory error detector

    ==713==

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

    ==713== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info

    ==713== Command: ./test.arm.elf

    ==713==

    ==713== Invalid write of size 4

    ==713==    at 0x8540: f (test.c:24)

    ==713==    by 0x8557: main (test.c:29)

    ==713==  Address 0x49a0050 is 0 bytes after a block of size 40 alloc'd

    ==713==    at 0x4834338: malloc (vg_replace_malloc.c:296)

    ==713==    by 0x852B: f (test.c:23)

    ==713==    by 0x8557: main (test.c:29)

    ==713==

    ==713==

    ==713== HEAP SUMMARY:

    ==713==     in use at exit: 40 bytes in 1 blocks

    ==713==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated

    ==713==

    ==713== LEAK SUMMARY:

    ==713==    definitely lost: 40 bytes in 1 blocks

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

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

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

    ==713==         suppressed: 0 bytes in 0 blocks

    ==713== Rerun with --leak-check=full to see details of leaked memory

    ==713==

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

    ==713== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 


    To create a trace dump of the program, execute the following command:

    # valgrind --tool=callgrind ./hello.elf

    This will create a file named callgrind.out.<pid>, for example callgrind.out.27005.


    Copy this file (callgrind.out.27005) to where you have kcachegrind installed and start it with the file create above. 

    For example, Kcachegrind callgrind.out.27005

    kcachegrind "visualisation tool for the Valgrind profiler"

    It should look similar to the following:




    If you do not have Kcachegrind installed, this can be done on an Ubuntu machine using the following command:

    #sudo apt-get install  kcachegrind



    AR# 63280
    Date Created 01/12/2015
    Last Updated 05/05/2015
    Status Active
    Type General Article
    Devices
    • Zynq-7000
    Tools
    • PetaLinux - 2014.4