Step 2: Debugging C/C++ Code Using Visual Studio - 2020.2 English

Vivado Design Suite Tutorial: Model-Based DSP Design using Add-on for MATLAB and Simulink (UG1498)

Document ID
UG1498
Release Date
2021-01-22
Version
2020.2 English
  1. Specify the debug tool using the xmcImportFunctionSettings command. In the MATLAB command prompt, type the following:
    >> xmcImportFunctionSettings('build','debug','compiler','Visual Studio');

    Press Enter.

    This command picks the installed Visual Studio in your machine for debugging.

  2. Observe the version and location of Visual Studio from the MATLAB console.

  3. Type the following in the MATLAB console to get more information on this command and also to set different version of Visual studio.
    >>help xmcImportFunctionSettings 
  4. Invoke Visual Studio from your install directory to start debugging C/C++ code.

  5. In the Visual Studio startup page, open the C/C++ file that you want to debug.

    Click on File > open > File and browse to the location where the Color Detection example design resides. Select the file RGB2HSV_wrap.h and click Open.





  6. Observe that RGB2HSV_wrap.h opens in the Visual Studio as shown in the following figure.

  7. Now set the break point in the RGB2HSV_wrap.h.

    To set the break point, click on the grey color space at line number. A red dot appears there, as shown in the following figure. This indicates that the break point has been set at the corresponding line.



  8. Next, attach the MATLAB process to the debugger by clicking the Attach icon in the tool bar as shown in the following figure.

  9. In the Attach to Process dialog box, which is opened, search for the process MATLAB.exe.

    When attaching Visual Studio to the MATLAB process, you must make sure to set the Attach to field to choose Native Code from the drop down menu as highlighted above.

    Click on Attach button. Your MATLAB process is attached to the debugger.

  10. Now, go back to the Simulink model and start simulating the color_detection design.

  11. The simulation process may take some time to initialize. Once this is done, switch back to the Visual Studio GUI to start debugging.

    If any Exception is thrown by Visual Studio, simply uncheck Break when this exception type is thrown in the Exception settings and click Continue.



  12. You can now see the simulation hitting the break point in Visual Studio.

  13. To debug, you can use the Step Over icon in the tool bar.

    Important:

    Click the Step Into icon to execute the next line of the code. If the line involves a call to an operation, it steps into the operation’s implementation and breaks the execution on the first action of that implementation.

    Clicking the Step Over icon does not step into the line’s implementation details, but steps to the next line of code after the call.

  14. Observe the values of variables at each corresponding step, as you progress with debugging.

  15. You can change the breakpoint to a different line, by removing the initial break point, and setting a new one for example at Line 22 and clicking the Continue button from tool bar.

    Now you can observe the break point hitting Line 22.

  16. Now, remove all the break points and click the Continue button. You can see the model running.
    Note: As the design is running in debug mode, the simulation may progress slowly.
    Note: You can always set a break point as long, as the design is simulating.
  17. Once you are done with debugging, you can the click Stop Debugging button in the tool bar.

  18. To come out of debugging mode in MATLAB, type the following command in the MATLAB console and press Enter.
    >> xmcImportFunctionSettings('build','release');

    Now, you can run the imported C/C++ code in release mode.