{"id":1665,"date":"2012-12-06T13:36:55","date_gmt":"2012-12-06T18:36:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=1665"},"modified":"2017-01-04T15:39:10","modified_gmt":"2017-01-04T20:39:10","slug":"debugging-cc-s-functions","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2012\/12\/06\/debugging-cc-s-functions\/","title":{"rendered":"Debugging C\/C++ S-Functions"},"content":{"rendered":"<p>If you are going to work with C or C++ S-Functions, you will probably need to do some debugging.<\/p> \r\n\r\n<p>My advice is: <strong>Don't go with the <em>printf debugging<\/em> approach<\/strong>. I did that for too long and now I realize it was a waste of my time. Once you discover how simple and more efficient it is to use an actual debugger when developing C Mex S-Functions, you will never go back.<\/p>\r\n\r\n<p>This week I want to show how easy it is to attach an external debugger to MATLAB and debug s-functions. The following is an adaptation for C-Mex S-functions of the MATLAB documentation page Debug C\/C++ Language MEX-Files.<\/p>\r\n\r\n<p><strong>Setting up the compiler<\/strong><\/p>\r\n\r\n<p>To begin, you need a development environment that provides source code debugging. In this post, I will use Microsoft Visual Studio 2010 Express on the Windows operating system and <a href=\"http:\/\/www.gnu.org\/software\/gdb\/\">GDB, the GNU Project debugger<\/a>, on Linux.<\/p>\r\n\r\n<p>Once your compiler is installed, you need to select it using:<\/p>\r\n\r\n<code>mex -setup<\/code>\r\n\r\n<p><strong>Compiling with debug symbols<\/strong><\/p>\r\n\r\n<p>You need to compile the S-function source file with the <tt>-g<\/tt> option, which builds the file with debugging symbols. For example:<\/p>\r\n\r\n<p><code>mex -g timestwo.c<\/code><\/p>\r\n\r\n<p>On Windows, you will see an additional PDB file containing the debug symbols:<\/p>\r\n\r\n<p><\/p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q4\/debug_symbols_file.png\" alt=\"PDB file continaing debug symbols\" \/><\/p>\r\n\r\n<p><strong>Attaching to the MATLAB process in Visual Studio<\/strong><\/p>\r\n\r\n<p>In Visual Studio, go to the Tools menu to attach to the MATLAB process:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q4\/attach_process.png\" alt=\"Attaching a process in Visual Studio.\" \/><br><em>***If you do not see the Attach to Process option, enable Expert Settings by going to: \"Tools -> Settings -> Expert settings\".<\/em><\/p>\r\n\r\n<p>This will launch a window where you can select MATLAB.exe and click Attach.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q4\/attach_window_full.png\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q4\/attach_window_small.png\" alt=\"Add Breakpoints\" \/><\/a><\/p>\r\n\r\n<p>Open the source file of your S-function in Visual Studio and set some breakpoints:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q4\/breakpointsMSVC.png\" alt=\"Add Breakpoints\" \/><\/p>\r\n\r\n<p><strong>Attaching to the MATLAB process using GDB<\/strong><\/p>\r\n\r\n<p>If you are a Linux user, you have many options to debug your S-Function. I will show you the one I typically use.<\/p>\r\n\r\n<p>First, launch a terminal and identify the MATLAB process ID using a line like:<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2012Q4\/getProcessID.png\" alt=\"get Process ID\" \/><\/p>\r\n\r\n<p>Then launch GDB by typing <strong>gdb<\/strong> in the terminal.<\/p>\r\n\r\n<p>In GDB, the first thing you do is to attach to the MATLAB process. For the example above, you would type:<\/p>\r\n\r\n<code>attach 8736<\/code>\r\n\r\n<p>Once GDB is attached to the MATLAB process, you can set breakpoints and watchpoints, for example:<\/p>\r\n\r\n<code>break timestwo.c:mdlOutputs<\/code>\r\n\r\n<p>Finally, restore the execution of MATLAB by executing:<\/p>\r\n\r\n<code>cont<\/code>\r\n\r\n<p><strong>Debug<\/strong><\/p>\r\n\r\n<p>Whether you are on Linux or Windows, hit play in your model. The focus should automatically go to the debugger as soon as a breakpoint is reached. From here, consult the documentation of your debugger on how to take advantage of its specific features.<\/p>\r\n\r\n<p><strong>Only for Debugging<\/strong><\/p>\r\n\r\n<p>It might seems strange to have to say this, but a debugging process like this should only be done when you are investigating a problem.  When debugging you have access to the inner workings of a running program.  It is possible to use the debugger in such a way as to corrupt that session of MATLAB and Simulink.  For this reason, only use this process when you are debugging.<\/p>\r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>How do you debug your S-Functions? Please share with us your tips and tricks by leaving a <a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=1665&#comment\">comment here<\/a><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>If you are going to work with C or C++ S-Functions, you will probably need to do some debugging. \r\n\r\nMy advice is: Don't go with the printf debugging approach. I did that for too long and now I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2012\/12\/06\/debugging-cc-s-functions\/\">read more >><\/a><\/p>","protected":false},"author":41,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,87],"tags":[451,459],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1665"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=1665"}],"version-history":[{"count":19,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1665\/revisions"}],"predecessor-version":[{"id":6250,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/1665\/revisions\/6250"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}