---
url: /howto/generated-code-tracing-for-simulink.md
---

# Simulink Models and Code Generation

Simulink is often used to model controllers for complex physical systems.
Since the modelled logic will be used in embedded controllers, there are code-generation tools that generate low-level code from the Simulink models.

This page describes how Teamscale supports connecting the generated code to the Simulink model elements from which the code was generated.
For an overview of all Simulink features in Teamscale, see [Teamscale for Simulink and Stateflow](../analyzing-simulink-models/).

\[\[toc]]

## Problem Description

In a typical use case, the code generated from a Simulink model will not be maintained directly.
If there are problems, they will be addressed in the Simulink model and the next generator run will overwrite the generated code.
The generated code is not directly maintained and therefore, code-readability findings on the generated code are not interesting.

However, findings concerning correctness or performance in the generated code are interesting and should be addressed.
Even more interesting would be code-coverage of the generated code.
How much of the original Simulink model is actually covered by testing of the generated code?

## Linking generated code to Simulink models

Teamscale supports linking "back" from generated code to the Simulink model for the code generator [`PLCcoder`](https://de.mathworks.com/help/plccoder/index.html) from Mathworks.
PLCcoder generates Structured Text code (IEC 61131-3).
The code contains hints that allow us to determine the Simulink blocks from which the code was generated.
![Generated-Code Example](./plcCodeExample.png)

To enable this, Teamscale requires a special meta-information `traceInfo.mat` file that PLCcoder generates when executed with the `Generate traceability option`.

If the file is provided, Teamscale will map code regions in the generated code to Simulink blocks.
For any mapped code region:

* Findings on the code will be copied and attached to the Simulink blocks.
  ![Generated-Code Example](./simulinkGeneratedCodeFinding.png)
* Coverage on the code will be mapped to the corresponding Simulink model.
  The coverage per model will appear in the new metric `Model Line Coverage`.
  ![Generated-Code Example](./simulinkGeneratedCodeCoverage.png)
* The code view in the Teamscale UI will provide a link from the comment (e.g., `'<S1>/Weight'`) to the Simulink block.
  Use CTRL + left click to follow the link.

## Setup of the generated-code tracing

To enable generated-code tracing, several options need to be enabled in the project configuration and analysis profile.

1. In the analysis profile editor, enable analysis for the languages `IEC 61131-3 ST` and `Simulink and Stateflow` by selecting them from `Language & Tool Enablement`.
   IEC is required to get findings/coverage to be mapped.
   Simulink is required to have something to map the findings/coverage to.

   ![Generated-Code Example](./SimulinkGeneratedCodeNewAnalysisProfile1.png)

2. As a final step in the analysis profile editor, make sure to enable the option `Enable Simulink tracing` under `Advanced Options`.

   ![Generated-Code Example](./SimulinkGeneratedCodeNewAnalysisProfile2.png)

3. In the `Included file names` option in the project settings, make sure to include your IEC files (e.g., `**.st`) and your Simulink files (e.g., `**.slx`).

4. In the `Analysis report mapping` option in the project settings (`Expert Options` of the connector), add the entry `**/traceInfo.mat -> SIMULINK_MAT`.

   ![Analysis report mapping in the expert options of the connector](./SimulinkGeneratedCodeProjectConfig.png)

5. The generated Structured Text code and the corresponding `traceInfo.mat` file must be committed to the analyzed code repository or to another content source configured in the Teamscale project (e.g., [Artifactory/S3](../uploading-external-data/#import-via-artifactory-s3)).
   Since code links might change over time, the `traceInfo.mat` file should be regenerated and committed each time the code is regenerated.
   When code and `traceInfo.mat` file are committed they should ideally match the Simulink models stored in the current repository commit.

## Limitations

Due to the rather complex relations between Simulink models, generated code, and the `traceInfo.mat` file, we need to make some assumptions on the usage scenarios.
These assumptions lead to the following limitations of the code-tracing feature:

1. We can consume only one `traceInfo.mat` file per project and branch.
   This means that at any time only one `traceInfo.mat` file will be used by Teamscale.
   If a new commit introduces a new file, its information will overwrite the information from any previous file (even if they have different file paths and the "old" file still exists in the repository).
2. If the Simulink models change without new code generation, then Teamscale can't maintain correct tracing.
   Teamscale will provide the old links as far as possible, but over time more and more links will break until new generated code is committed.
