MATLAB Answers

How to debug MEX-file compiled with MinGW64 and -g flags

129 views (last 30 days)
How to debug MEX-file compiled with MinGW64 and -g flags

Accepted Answer

MathWorks Support Team
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.
 
  5 Comments
langrg
langrg on 25 Oct 2019
I got problem at step 8, I found solution, so I share it.
If you get the following error message:
"Lauching mexdebug" has encoutered a problem.
Error with gdb --version
Then, in Matlab command window, enter:
getenv('MW_MINGW64_LOC')
You'll get the directory path where "gdb.exe" file used by Matlab is installed. Copy this path (path of file "gdb.exe", not only the directory) at step 7 on "GDB debugger" field.
You can now click on "Debug", it should work.

Sign in to comment.

More Answers (9)

Adam Gogacz
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.
Thanks,
Adam

Chuong Nguyen
Chuong Nguyen on 10 Jun 2017
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?
  1 Comment
Lithe
Lithe on 13 Jun 2017
I just tried applying this procedure and ran into two issues. Fortunately, both were small and easily resolved.
1. the version of gdb included with the latest version of TDM-GCC MinGW has a compatibility problem with Eclipse. For more information, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=474311
I was able to solve this problem by using an earlier version. By trial and error, I found that the following version works:
tdm64-gcc-4.7.1-3.exe
(Note, this issue has nothing to do with MATLAB, per se).
2. As noted in the previous message, the task does not come up as MATLAB.com. The correct task is one of the generically named tasks called "Program" (at least in windows 10). But by clicking on the name, you can quickly identify the correct task (see attachment).
With these changes to the procedure, it works great.

Sign in to comment.


Chuong Nguyen
Chuong Nguyen on 10 Jun 2017
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 [15920] 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
  1 Comment
Anton Drovyannikov
Anton Drovyannikov on 21 Aug 2017
Maybe, this is because you use not compatable with matlab debugging version of gdb. I've got the same problem and solution was to install minGW64 (https://sourceforge.net/projects/mingw-w64/), version 4.9.1 (it uses GNU gdb (GDB) 7.8.1 ver.), Architecture x86_64, threads win32, exception dwarf, Build version 3. And also 64bit Matlab and eclipse(oxygen, neon, luna)

Sign in to comment.


Esfandiar Farahvashi
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!

Patrick Schmidt
Patrick Schmidt on 19 Apr 2018
Edited: Patrick Schmidt on 19 Apr 2018
I've followed the steps in this tutorial, but when I press debug I get an error: "gdborig.exe has stopped working". Does somebody have an idea why this could happen?
  1 Comment
gvreddy
gvreddy on 21 Nov 2018
Hi Patrick, I got same error. Issue resolved after using gdb-7.8.1. You can download gdb(gdb32-7.9.1-tdm-1.zip) from https://sourceforge.net/projects/tdm-gcc/files/GDB/

Sign in to comment.


LodL93
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
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?
Cheers Jacob

Till Blaha
Till Blaha on 25 Sep 2018
Hey Jacob,
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:
https://stackoverflow.com/questions/35826635/clion-gdb-current-version-is-gnu-gdb-gdb-cygwin-7-10-1-1-7-10-1-supporte
Greetings, Till

Jeno Boka
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!