Test your code from the command line using cspybat

IAR Embedded Workbench provides a rich user interface for debugging your code, but you can also execute C-SPY in batch mode if you use the command line utility cspybat. This utility lets you run cyclic tests from the command line.

In this article, we introduce how to use cspybat and test a code example with changing parameters from the Standard I/O interface.

Create a test project

The first step is to create a project in the IAR Embedded Workbench IDE. I will use the following code as an example:

#include <stdio.h>
#include <stdio.h>
int rgb_to_luminance(int r, int g,int b)
int luminance;
luminance = (int)(0.299 * r + 0.587 * g + 0.114 * b);
return luminance;

int main()
int luminance;
int input_r, input_g, input_b;
scanf("%d",&input_r); printf("%d\n",input_r);
scanf("%d",&input_g); printf("%d\n",input_g);
scanf("%d",&input_b); printf("%d\n",input_b);

luminance = rgb_to_luminance(input_r,input_g,input_b);
printf("luminance:%d\n", luminance);

return 0;

This code receives three parameters, r (red), g (green) and b (blue) from the standard I/O, and then displays the converted luminance value. This type of conversion is often used to create monochrome images from color images.

To be able to run this code, you should check that "Semihosted" is selected as the low-level library. I also recommend selecting "Via semihosting" for the "stdout/stderr" setting.


You should also make sure that the other options are set up correctly before starting the debug session, for example the device selection and the debugger configurations.

When you have finished setting up your project, click "Make" and then "Download and Debug".


Click "Go" and the Terminal I/O window will be opened with the Input field selected.


Enter ‘50’, ‘100’ and ‘150’ in the Input field.


The result is also shown in the Terminal I/O window.


Close your debug session.

Create a batch file

You can find a batch template file under the settings directory in your project directory.


Open the file with an editor such as notepad.

Find and copy the following long line under ":debugFile":

"D:\Program Files (x86)\IAR Systems\Embedded Workbench
7.3\common\bin\cspybat" -f
"D:\CSPYBAT\settings\project.Debug.general.xcl" "--debug_file=%~1" --
backend -f "D:\CSPYBAT\settings\project.Debug.driver.xcl"

Create a new batch file with an appropriate file name, such as "run_cspybat.bat", paste the line, and modify the debug file name to the actual executable object file.

"D:\Program Files (x86)\IAR Systems\ Embedded Workbench 
7.3\common\bin\cspybat" -f
"D:\CSPYBAT\settings\project.Debug.general.xcl" "--
debug_file=D:\CSPYBAT\Debug\Exe\c.out" --backend -f

Open the command prompt window and go to the settings directory:

D:\>cd D:\CSPYBAT\settings

Call the batch file that you have created:


This will launch cspybat and the program will require input from you:


Enter ‘50’, ‘100’ and ‘150’, and you will see the result:


Get the Standard input from a text file

Windows batch provides a redirect interface that enables you use a text file as an input for the standard input.

Create a text file named for example "data.txt" directly in the settings folder:


Run the previous batch file with a redirect to your text file:

D:\CSPYBAT\settings>run_cspybat.bat < data.txt

Now, you will get the result without typing the parameters:


This is a very simple example intended to give you and overview of how to use cspybat for testing. In order to run unit tests, you can use a similar setup to prepare the parameter data in the text file, write a “for” loop for getting parameters and run the test function. You can also compare the results to ensure that they are equal to the expected values.


To work in a GUI is most of the time the most convenient way to develop an embedded project. On the other hand, repetitive tasks can be automated with a command line utility. IAR Embedded Workbench provides the command line utility cspybat for the C-SPY Debugger and you can use it to run boundary value test, check the effects of type casts, check the accuracy of float calculation, etc.

To learn more about cspybat, please refer to the C-SPY Debugging Guide, which you can reach from the Help menu in IAR Embedded Workbench.

This article is written by Kyota Yokoo, Field Applications Engineer at IAR Systems Japan.

© IAR Systems 1995-2016 - All rights reserved.