How to debug MEX-file compiled with MinGW64 and -g flags
152 views (last 30 days)
MathWorks Support Team on 29 Jan 2016
Follow the steps below to debug a MEX-file compiled with MingW64 and the -g flag:
1. Start Eclipse and choose a new workspace.
Download Eclipse from link <https://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/lunasr2>
2. Use "File" -> "New" -> "Project"... and select "General"->"Project"
3. Choose a name for your project and click "Finish"
4. Use "File"->"New"->"Other"..." and then "General"-> "Untitled Text File" to create a new text file.
Enter the following in the text file
handle SIGSEGV nostop
Then save the file as "gdbinit" in "mexdebug" folder as shown in the figure.
5. Drag the C/Cpp source code of your MEX-file into the project. When asked to copy or link choose "Link to file" so that the source codes stay in their original locations.
6. Go to "Run" -> "Debug Configurations...". Select "C/C++ Attach to Application" and select "New" button.
7. On the "Debugger" tab enter the location of the file created under 4 as "GDB command file".
8. Click "Debug"
9. Select MATLAB.exe from the list.
10. The debugger will suspend MATLAB. Use "Resume" button to continue running MATLAB.
11. Open the C/Cpp code in Eclipse and place a breakpoint.
12. Run the MEX-file in MATLAB
13. When the breakpoint is hit you will stop in the debugger and can for example step through the code and inspect variables.
More Answers (9)
Adam Gogacz on 3 Dec 2018
I've never used Eclipse for debugging, can someone provide more clarification on going from step 8 to step 9? I was never presented with a "Select Processes" window.
Thank you very much, but for my case, when the window "select process" open, I can't see the matlab.exe process in the windows?, can you help me to solve the problem?
and when I detect "Program 15920" is the link to matlab, I press "ok" but again, Eclipse show the error:
Error in final launch sequence Failure to attach to process: MATLAB.exe  Error: Failed to execute MI command: -target-attach 15920 Error message from debugger back end: Can't attach to process.
can you help me to solve this problem
Esfandiar Farahvashi on 5 Apr 2018
I have followed the guide and successfully attached eclipse to my MATLAB process. I also linked the Header and CPP files with my project. However, if I insert break points in the cpp file, MATLAB will not pause during the calling to the mex. It simply calls it and continues without any breakpoints beeing triggered. I would highly appreciate any suggestions!
LodL93 on 8 May 2018
Debugging a mex-file works very very easy with QT Creator instead of Eclipse (what not worked for me). Just select "Attach to running application" as shown in the image, and then select the Matlab Process. You then can set your breakpoints in the source files of your Mex-file, and even dive into linked libraries.
I need to emphasize that QT Creator is a very good IDE for coding C/C++. ;))
Jacob Mannhardt on 2 Aug 2018
Thank you for this tutorial. Unfortunately the Attach Process Window doesn't show up when I hit debug. I am absolutely new to Eclipse and debugging. Matlab is open, but I don't think that that's the issue. Any ideas?
Till Blaha on 25 Sep 2018
I had the exact same issue and I am, just like you a complete beginner on this subject. It turned out not to be a MATLAB related issue. Any gdb version after 7.8 has some sort of issue with eclipse (or the other way around, I don't know). See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=474311
Unfortunately for us, 7.8 is no longer available in the repos of cygwin (they start at 7.9). Fortunately for us, this guy has found them somewhere, uploaded the 7.8 version and wrote a detailed explanation on how to install it (takes around 20 minutes). After that everything worked like a charm:
Jeno Boka on 29 Oct 2019
Edited: Jeno Boka on 29 Oct 2019
You may get the PID from MATLAB console using the following command:
pid = feature('getpid') % retruns the PID of the MATLAB instance
For Eclipse Version: 2019-09 R (4.13.0)
I used GDB ver 7.8.1 as newer versions are not working, as others have already stated.