Debugging software in virtual environments

Debugging software in virtual environments

Imagine you are developing the software of a complex cyber-physical system, e.g. laboratory automation system, industrial robot, etc.

Developing Software in a virtualized environment

To manage complexity, you might develop the software in a virtual environment, wherein the physical and software system environment are simulated.

But what if your system-under-test (SUT) does not react to the inputs of your simulated environment as expected or some system-level tests fail?

This is where debugging comes into play:
You need to debug your software on system level in your virtual environment. But this can be quite tricky, as you now have to debug a composite system, which consists of your SUT and its simulated environment.

Example: Debugging software for a temperature controller

Example: Developing cyber physical systems in virtual environments
Example: Developing a cyber-physical system in a virtual environment

You are developing the software for a temperature controller that switches on a heater depending on the ambient temperature. The ambient temperature is measured with three temperature sensors. The required behavior of the controller software is to turn the heater on when the average temperature of the system drops below 18°C.

To develop the controller software in a virtual environment, you simulate the three temperature sensors with a simulation tool. The tool provides the three temperature values and consumes the resulting heating state of the controller software. This allows you to visualize the time evolution of the heating state depending on the temperature values and to observe if your controller software reacts to its environment in the required way. It also allows you to conduct automated tests of the controller software without the actual controller hardware.

Visualizing input/output to your SUT in a simulation tool
Visualizing input/output to your SUT in a simulation tool

The upper figure shows that the heater turns on when the three temperature values are low, and turned off when the values are high. This corresponds to the required behavior. But, at second 37 and 49, the temperature values are high and the heater suddenly turns on, which is not the required behavior. This is obviously a bug in the controller software, which you need to find. Take a look at the function ControlTemperature in the controller software which is responsible for switching the heating state:

Debugging software in virtual environments

The function reads the three temperature sensor values from the simulation tool and calculates the average temperature. Based on the average temperature, it decides if heating is required and afterwards sends the updated heating state back to the simulation tool.

In order to find the root of the undesired behavior, you need to debug the controller software together with its simulated environment.

Problems when debugging software in virtual environments

Logical divergence of SUT and simulation environment

You could set a breakpoint in the controller software, step through it, and examine what happens during execution. But your environment simulation keeps running and does not wait for your controller software, which is in debug mode. Consequently, the simulation and your controller software will soon no longer operate on the same time base. Thus, it would be beneficial to couple the execution of the debugger and your simulation environment. In consequence, you avoid logical divergence of your SUT and the environment.

Visualizing and monitoring debug data over time

Another problem in this context is the visualization of critical quantities. The simulation tool visualizes and monitors input (sensor values) and output (heating state) values of your SUT. However, to analyze the internal variables of your SUT during the simulation you use the watch window of your IDE. It is difficult to successfully analyze external (input/output) and internal quantities simultaneously with a common timeline in two different tools. If you had the input/output values and the internal variables of your SUT next to each other, it would be easy to see correlations between unexpected changes of output values and suspicious changes of internal variables.

Debugging the temperature controller software

Applying the above findings to the temperature controller example, the visualization of the internal stack-variable averageTemperature next to the heating state in the simulation tool reveals a bug with the averageTemperature:

Visualizing internal debug data (averageTemperature) in a simulation tool
Visualizing internal debug data (averageTemperature) in a simulation tool

What you can see immediately: The unexpected change in the heating state is related to a sudden jump of the averageTemperature variable to negative values. This indicates that the bug can be found in the CalcAverageTemperature function.

Debugging software in virtual environments.

It turns out that the wrong data type (int8_t) was chosen for the variable TemperatureSum, which led to an overflow at sufficiently high temperatures.

Conclusions

In this example, you could see the advantage of coupling the debugger with the virtual environment. Then you get a deeper insight into the time evolution of the SUT. It is very helpful to visualize internal debug expressions in the simulation tool. You can easily explore hidden correlations between external behavior and internal quantities. Ultimately, this enables a combination of black-box testing with white-box analysis at a system level.

Video HowTo: Software debugging in a virtual environment

Check out our video tutorial for the actual implementation of those debugging features with CANoe/CANoe4SW as simulation and virtual test environment.

Get started with your projects

Share:

Legal Notice

This text is the intellectual property of the author and is copyrighted by coderskitchen.com. You are welcome to reuse the thoughts from this blog post. However, the author must always be mentioned with a link to this post!

Leave a Comment

Related Posts

Daniel Lehner

Software Testing in the World of IoT

This interview answers the question “How can I reduce the testing effort for the software of my IoT system?” The expert from Vector explains, how

Daniel Lehner

Reducing efforts in system testing

Which are the best methods to reduce efforts in system testing? In this second part of the Coders Kitchen interview, system testing experts Hans Quecke

Traditional system testing vs. model-based system testing - Coders Kitchen
Daniel Lehner

Traditional vs. model-based system testing

Interview with system testing experts Simone Gronau and Hans Quecke about typical challenges in defining system tests, and how model-based methods might offer an alternative.