Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Debugging C MEX S-Functions

About Debugging C MEX S-Functions

This section provides high-level tips on how to debug C MEX S-functions within the Simulink environment and using third-party software. The following lists highlight some of the more common errors made when writing an S-function. For a more detailed analysis, use the debugger provided with your C compiler.

The examples at the end of this section show how to debug a C MEX S-function during simulation, using third-party software.

Refer to your compiler documentation for further information on debugging files.

Debugging C MEX S-Functions in the Simulink Environment

Before you begin, make sure you have a good understanding of how to write C S-functions and the required callback methods. For assistance:

If your S-function is not compiling, first ensure that the mex command is properly configured and your S-function includes all necessary files:

If the mex command compiles your S-function, but your S-function does not simulate or the simulation produces incorrect results, inspect your S-function's source code to ensure that:

The following table describes additional common S-function constructs that can lead to compilation and simulation errors.

Does your S-function...Look for...
Use for loops to assign memory?Instances where your S-function might inadvertently assign values outside of the array bounds.
Use global variables?Locations in the code where the global variables can be corrupted. If you have multiple instances of your S-function in a model, they can write over the same memory location.
Allocate memory?Memory your S-function does not deallocate. Always free memory that your S-function allocates, using the malloc and free commands to allocate and deallocate memory, respectively.
Have direct feedthrough?An incorrect direct feedthrough flag setting in your S-function. An S-function can access its inputs in the mdlOutputs method only if it specifies that the input ports have direct feedthrough. Accessing input signals in mdlOutputs when the input port direct feedthrough flag is set to false leads to indeterminate behavior. To check if you have a direct feedthrough flag incorrectly set, you can turn on the model property TryForcingSFcnDF using the command
set_param(model_name,'TryForcingSFcnDF','on')
This command specifies that all S-functions in the model model_name have a direct feedthrough flag of true for all their input ports. After you turn on this property, if your simulation produces correct answers without causing an algebraic loop, one of your S-functions in the model potentially set an incorrect direct feedthrough flag. Consult the file
matlabroot/simulink/src/sfuntmpl_directfeed.txt

for more information on diagnosing direct feedthrough errors.

Access input signals correctly?Instances in the code where your S-function uses incorrect macros to access input signals, for example when accessing a discontiguous signal. Discontiguous signals result when an S-function input port is fed by a Selector block that selects every other element of a vector signal. For discontiguous input signals, use the following commands:
// In mdlInitializeSizes
ssSetInputPortRequiredContiguous(S, 0, 0);
   
// In mdlOutputs, access the inputs using 
InputRealPtrsType uPtrs1 = 
    ssGetInputPortRealSignalPtrs(S,0);   
For contiguous input signals, use the following commands:
// In mdlInitializeSizes
ssSetInputPortRequiredContiguous(S, 0, 1);

// In mdlOutputs, access the inputs using
const real_T  *u0  = 
   (const real_T*) ssGetInputPortSignal(S,0); 

/* If ssSetInputPortRequiredContiguous is 0, 
ssGetInputPortSignal returns an invalid pointer.*/ 

Debugging Techniques

You can use the following techniques for additional assistance with debugging your S-function.

Debugging C MEX S-Functions Using Third-Party Software

You can debug and profile the algorithm portion of your S-function using third-party software if you separate the algorithm from the S-function's Simulink interface. You cannot debug and profile the S-function's interface with the Simulink engine because the Simulink interface code does not ship with the product.

You can additionally use third-party software to debug an S-function during simulation, as shown in the following two examples. These examples use the Simulink model sfcndemo_timestwo.mdl and the C MEX S-function timestwo.c.

Debugging C MEX S-Functions Using the Microsoft Visual C++ .NET Environment

Before beginning the example, save the files sfcndemo_timestwo.mdl and timestwo.c into your working directory.

  1. Open the Simulink model sfcndemo_timestwo.mdl.

  2. Create a debuggable version of the MEX-file by compiling the C-file using the mex command with the -g option.

    mex -g timestwo.c

    The -g option creates the executable timestwo.mexw32 with debugging symbols included. At this point, you may want to simulate the sfcndemo_timestwo model to ensure it runs properly.

  3. Without exiting the MATLAB environment, start the Microsoft Development Environment.

  4. From the Microsoft Development Environment menu bar, select Tools > Debug Processes.

  5. In the Processes dialog box that opens:

    1. Select the MATLAB.exe process in the Available Processes list.

    2. Click Attach.

  6. In the Attach to Process dialog box that opens:

    1. Select Native in the list of program types to debug.

    2. Click OK.

    You should now be attached to the MATLAB process.

  7. Click Close on the Processes dialog box.

  8. From the Microsoft Development Environment File menu, select Open > File. Select the timestwo.c source files from the file browser that opens.

  9. Set a breakpoint on the desired line of code by right-clicking on the line and selecting Insert Breakpoint from the context menu. If you have not previously run the model, the breakpoint may show up with a question mark, indicating that the executable is not loaded. Subsequently running the model loads the .mexw32 file and removes the question mark from the breakpoint.

  10. Start the simulation from the sfcndemo_timestwo Simulink model. You should be running the S-function in the Microsoft Development Environment and can debug the file within that environment.

Debugging C MEX S-Functions on The Open Group UNIX Platforms

Before beginning the example, save the files sfcndemo_timestwo.mdl and timestwo.c into your working directory.

  1. Open the Simulink model sfcndemo_timestwo.mdl.

  2. Create a debuggable version of the MEX-file by compiling the C-file using the mex command with the -g option

    mex -g timestwo.c

    The -g option creates the executable timestwo.mexw32 with debugging symbols included. At this point, you may want to simulate the sfcndemo_timestwo model to ensure it runs properly.

  3. Exit the MATLAB environment.

  4. Start the MATLAB environment in debugging mode using the following command.

    matlab -D<nameOfDebugger>

    The -D flag starts the MATLAB environment within the specified debugger. For example, to use the dbx debugging tool on Sun™ Solaris™ platform, enter the following command.

    matlab -Ddbx
  5. Once the debugger has loaded, continue loading the MATLAB environment by typing run at the debugger prompt.

    (dbx) run
    Running: matlab
    (process id 9375)
  6. After the MATLAB environment starts, enable debugging by entering the following command at the MATLAB command prompt.

    dbmex on
  7. Open the sfcndemo_timestwo Simulink model.

  8. Simulate the model. This brings you into the debugger.

  9. Set breakpoints in the source code, for example:

    (dbx) stop in mdlOutputs
    (2) stop in `timestwo.mexs64`timestwo.c`mdlOutputs
    (dbx) file timestwo.c
  10. Issue the cont command to continue.

    (dbx) cont
  11. Use your debugger routines to debug the S-function.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS