How to use Valgrind with TimeStorm

The Valgrind tool suite includes a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behavior. More information on Valgrind is available here: http://valgrind.org/

TimeStorm includes Valgrind plugins and you can use them to profile applications running on your localhost or remote target using memcheck and other Valgrind tools massif, helgrind and cachegrind.

Note: You must have Valgrind installed on target for remote profiling.

This document will show How to Use Valgrind with TimeStorm. To run Valgrind in TimeStorm.

  1. Open your C/C++ Project. If you want to learn how to use valgrind, go to Learn valgrind by creating a C Project using the Valgrind sample section below.
  2. Add Debugging information flag to Compiler
  3. Build Project.
  4. Set up Valgrind configuration for a Remote target.
  5. Start Valgrind profiling and view the results in Valgrind editor.

1. Open your C/C++ Project:

Open your project using project explorer (we are using a C/C++ project created using the valgrind sample) as shown below.

2. Add Debugging information flag to Compiler:

  1. Make sure you are in the C/C++ perspective (Window → Perspective → Open Perspective → Other → C/C++) in TimeStorm.
  2. Right-click your project, and select properties.
  3. Expand C/C++ Build > Settings, and select the Tool Settings tab.
  4. Select GCC C/C++ Compiler → Debugging → Debug level as g or g1 or g3. For more information on debug levels, please refer to https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

3. Build Project:

Right-click your project, and select Build Project to build and display the build logs in the console as shown below.

4. Set up Valgrind configuration for a Remote target:

A TimeStorm Valgrind Configuration is a collection of settings that are required to download and run an application. The TimeStorm Valgrind configuration is persistent and can be launched multiple times to profile the application. To profile the application on the remote target, you have to create a TimeStorm Valgrind configuration.

To create a TimeStorm Valgrind configuration, right-click on the project and click on TimeStorm Profiling Tools → Valgrind as shown below.

Click on the “Valgrind” option and this will open the “TimeStorm Profiling Tools Configurations” dialog as shown below.

Note that an entry is created under TimeStorm Valgrind Remote category with the project name appended with the Active build configuration, and other values are filled in with the default values that should work. The Timestorm Profiling Tools Configurations entries are categories in different tabs on the right-hand panel and are explained below. To profile the application, mostly you have to just select the remote target on the target tab and click the Profile button.

Main Tab:

In the Main tab, you can change the C/C++application by searching the project. You can change the project too if you want to use this configuration for a different project.

Arguments tab:

In the Arguments tab, you can specify the arguments to be passed to the application program. The remote working directory is set as ‘.’, which means that the directory specified in the target definition is used as the working directory.

Target tab:

The Target tab shown below, allows you to select the hardware target used when you Profile the application.

In the Target tab, use the drop-down list to select a hardware target that you have already created. If you have not yet created a hardware target or want to change the settings for an existing target, click ‘Manage targets’ to open the Hardware Targets management utility.

Valgrind Options Tab:

The Valgrind options tab shown below, allows you to select Tool to run. Valgrind supports four types of tools:

  1. Massif
  2. Memcheck
  3. Helgrind
  4. Cachegrind

1. Massif: Massif is a heap profiler. It measures how much heap memory your program uses. This includes both the useful space, and the extra bytes allocated for book-keeping and alignment purposes. For more information, please refer to https://www.valgrind.org/docs/manual/ms-manual.html#ms-manual.overview. To run Massif, select Massif in ‘Tool to run’ dropdown.

2. Memcheck: Memcheck can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behavior. For more information, please refer to https://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.overview. To run Memcheck, select Memcheck in ‘Tool to run’ dropdown.

3. Helgrind: Helgrind is a Valgrind tool for detecting synchronisation errors in C, C++ and Fortran programs that use the POSIX pthreads threading primitives. For more information, please refer to the https://www.valgrind.org/docs/manual/hg-manual.html#hg-manual.overview. To run Helgrind, select Helgrind in the ‘Tool to run’ dropdown.

4. Cachegrind: Cachegrind simulates how your program interacts with a machine's cache hierarchy. For more information, please refer to the https://www.valgrind.org/docs/manual/cg-manual.html#cg-manual.overview. To run Cachegrind, select Cachegrind in the ‘Tool to run’ dropdown.

To illustrate, select Memcheck in the ‘Tool to run’ dropdown. You can tweak Valgrind by changing the settings on the General Options, Suppressions, and Memcheck Options tabs. The default settings should work in most cases.

General Options tab screenshot as shown below:

Suppressions tab screenshot as shown below:

Memcheck Options tab screenshot as shown below:

Environment tab:

In the ‘Environment’ tab, you can set environment variables for the target.

The panel shown above contains the following buttons:

Add — Click the ‘Add’ button to create a new entry, and the ‘New Environment Variable’ dialog will appear, as shown below. Click the ‘Variables’ button to select the variables that you want to use. Then, click ‘OK’ twice.

Select — Click the ‘Select‘ button to import environment variables from the host file system.

Edit — To change an entry, select it from the list, and click the ‘Edit’ button.

Remove — To delete an entry, select it from the list, and click the ‘Remove’ button

Copy — To copy environment variables and values, select it from the list, and click the ‘Copy’ button.

Paste — To paste environment variables and values, click the ‘Paste’ button.

Source tab:

Source Lookup Path — This field shows the project being run, as well as any projects that it references.

Add — You can add arbitrary source file locations by using the Add button. These locations are searched after the generic locations, from the top to the bottom item in an order.

Edit, Remove, Up, Down — The other buttons to the right of the list allow you to edit, remove, or reorder the list items, and to restore the default information.

Search for duplicate source files on the path — Selecting this checkbox causes TimeStorm to notify the user if it is unable to determine which source file corresponds to an executing binary file. For example, if you have two source files with the same filename in different directories of the search path, TimeStorm is unable to determine which file is related to the binary file with that name. If this checkbox is selected, TimeStorm displays a message and asks the user to select which file to use. This checkbox is not selected by default.

Common tab:

The ‘Common’ tab, sets options for sharing this TimeStorm Profiling Tools Configuration among multiple projects.

The options with the Common tab include:

Local File — By default, Timestorm Profiling Tools configurations are saved with the workspace state files, so they can only be used with projects in the current workspace. (This setting corresponds to the ‘Local file’ radio button on this panel.) However, you can make the configuration available for use in other workspaces by choosing the ‘Shared file’ radio button.

Shared File — When you select this option, the Timestorm Profiling Tools configuration is saved as a .launch file that can be imported into another TimeStorm workspace. Optionally, you can specify a different location for the file so that it is more easily accessible to multiple projects.

Display in Favorites Menu — You can select whether to include this TimeStorm Profiling Tools configuration on the context menu. For example, if you select the ‘TimeStorm Profiling Tools’ checkbox, the TimeStorm Profiling Tools configuration always appears in the ‘Profiling History’ submenu.

You can choose to include the TimeStorm Profiling Configuration in either menu, both menus, or neither menu.

Launch in Background — Selecting the ‘Launch in background’ checkbox causes this profile configuration to run as a background thread. Running as a background thread is the default value. Clear this checkbox if you want to run in the foreground, along with other TimeStorm Processing.

5. Start Valgrind profiling and view the results in Valgrind editor:

After setting the values in all the tabs, click the Profile button at the bottom of the Profile configuration dialog. This will connect your host to your target, download the application to the target, and execute the application on the target. The commands executed on the target and the program output progress are displayed in the console view. TimeStorm will run memcheck on the target, upload memcheck’s output to the host and display the results in the Valgrind View in the bottom of the C/C++ perspective and display the Valgrind error markers in the source code.

Valgrind view displays the memcheck results in a tree view. The tree can be expanded to view the stack trace. The results include the stack trace, source file with the line number, and the memory address. Double-clicking on a line will open the corresponding source file in the source editor with error markers on the lines that have the memory leak.

Learn valgrind by creating a C Project using the Valgrind sample:

To create a Valgrind sample project, open the ‘New Project’ wizard by choosing File → New → Project from the main menu OR click the ‘New’ button and use the drop-down menu to the right of the button to choose ‘Project’ as shown below.

Expand C/C++, choose a C/C++ Project, and click Next. To illustrate the Sample Valgrind Project (Samples → Valgrind)

Project page

  1. Enter a Project Name.
  2. Under the ‘Project Type’ heading, expand Samples, and select the Valgrind project.
  3. Under the Toolchains heading select the TimeStorm Cross-Compile Toolchain.
  4. Click ‘Next’ to bring up the ‘Basic Settings’ as shown below.

    Basic settings page

    On this page, set the basic properties of the project like Author, Copyright notice, Greeting message, and Source (where the project source files are to be stored within the project).

    Click ‘Next’ to bring up the ‘Select Configurations’ as shown below.

    Select build configuration page

    Build Configuration is a set of pre-defined compiler and linker settings that are used to build a project.

    By default, TimeStorm creates three Build Configurations for new projects:

    Debug — This setting performs no code optimization and attaches complete debugging information. These settings are designed to make debugging as easy as possible.

    Release — This setting has the highest compiler code optimization settings. The output will not include any debugging symbols. Code compiled with this configuration is ready for production use.

    GnuProfiler — This setting builds the software with debugging information as well as code to collect profiling information via GNU gprof.

    You can change the build configuration settings before creating the project by clicking the ‘Advanced Settings…’ button. You can also change these settings after creating the project.

    Click Next to continue.

    Select SDK page:

    The Select SDK page show below allows you to choose the SDK you want to use with the project. Select the SDK Type and then choose the SDK.

    Click Finish to create and exit the wizard. The new project is created and displayed in the Project Explorer view as shown below.

    After creating the Sample Valgrind Project, follow the above steps (Step2: Add Debugging information flag to Compiler, Step3: Build Project, Step4:Set up Valgrind configuration for a Remote target, Step5:Start Valgrind profiling and view the results in Valgrind editor) to run Valgrind on a remote Target.